-
Notifications
You must be signed in to change notification settings - Fork 47
/
blocks.php
145 lines (119 loc) · 3.94 KB
/
blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Gutenberg Blocks setup
*
* @package TenUpScaffold\Core
*/
namespace TenUpTheme\Blocks;
use TenUpTheme\Blocks\Example;
/**
* Set up blocks
*
* @return void
*/
function setup() {
$n = function( $function ) {
return __NAMESPACE__ . "\\$function";
};
add_action( 'enqueue_block_editor_assets', $n( 'blocks_editor_styles' ) );
add_filter( 'block_categories_all', $n( 'blocks_categories' ), 10, 2 );
add_action( 'init', $n( 'register_theme_blocks' ) );
add_action( 'init', $n( 'block_patterns_and_categories' ) );
/*
If you are using the block library, remove the blocks you don't need.
add_filter( 'tenup_available_blocks', function ( $blocks ) {
if ( ! empty( $blocks['integrated-hero'] ) ) {
unset( $blocks['integrated-hero'] );
}
return $blocks;
} );
*/
}
/**
* Add in blocks that are registered in this theme
*
* @return void
*/
function register_theme_blocks() {
// Filter the plugins URL to allow us to have blocks in themes with linked assets. i.e editorScripts
add_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10, 2 );
// Require custom blocks.
require_once TENUP_THEME_BLOCK_DIR . '/example-block/register.php';
// Call block register functions for each block.
Example\register();
// Remove the filter after we register the blocks
remove_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10, 2 );
}
/**
* Filter the plugins_url to allow us to use assets from theme.
*
* @param string $url The plugins url
* @param string $path The path to the asset.
*
* @return string The overridden url to the block asset.
*/
function filter_plugins_url( $url, $path ) {
$file = preg_replace( '/\.\.\//', '', $path );
return trailingslashit( get_stylesheet_directory_uri() ) . $file;
}
/**
* Enqueue editor-only JavaScript/CSS for blocks.
*
* @return void
*/
function blocks_editor_styles() {
wp_enqueue_style(
'editor-style',
TENUP_THEME_TEMPLATE_URL . '/dist/css/editor-style.css',
[],
TENUP_THEME_VERSION
);
}
/**
* Filters the registered block categories.
*
* @param array $categories Registered categories.
*
* @return array Filtered categories.
*/
function blocks_categories( $categories ) {
return array_merge(
$categories,
array(
array(
'slug' => 'tenup-scaffold-blocks',
'title' => __( 'Custom Blocks', 'tenup-theme' ),
),
)
);
}
/**
* Manage block patterns and block pattern categories
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns/
*
* @return void
*/
function block_patterns_and_categories() {
/*
## Examples
// Register block pattern
register_block_pattern(
'tenup/block-pattern',
array(
'title' => __( 'Two buttons', 'tenup' ),
'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
'content' => "<!-- wp:buttons {\"align\":\"center\"} -->\n<div class=\"wp-block-buttons aligncenter\"><!-- wp:button {\"backgroundColor\":\"very-dark-gray\",\"borderRadius\":0} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius\">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button -->\n\n<!-- wp:button {\"textColor\":\"very-dark-gray\",\"borderRadius\":0,\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius\">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
)
);
// Unregister a block pattern
unregister_block_pattern( 'tenup/block-pattern' );
// Register a block pattern category
register_block_pattern_category(
'client-name',
array( 'label' => __( 'Client Name', 'tenup' ) )
);
// Unregister a block pattern category
unregister_block_pattern('client-name')
*/
}