-
Notifications
You must be signed in to change notification settings - Fork 46
/
blocks.php
145 lines (117 loc) · 3.59 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 TenUpTheme
*/
namespace TenUpTheme\Blocks;
use TenUpTheme\Utility;
/**
* Set up blocks
*
* @return void
*/
function setup() {
$n = function ( $function ) {
return __NAMESPACE__ . "\\$function";
};
add_action( 'enqueue_block_editor_assets', $n( 'blocks_editor_styles' ) );
add_action( 'init', $n( 'register_theme_blocks' ) );
add_action( 'init', $n( 'register_block_pattern_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;
} );
*/
}
/**
* Automatically registers all blocks that are located within the includes/blocks directory
*
* @return void
*/
function register_theme_blocks() {
global $wp_version;
$is_pre_wp_6 = version_compare( $wp_version, '6.0', '<' );
if ( $is_pre_wp_6 ) {
// 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 );
}
// Register all the blocks in the theme
if ( file_exists( TENUP_THEME_BLOCK_DIST_DIR ) ) {
$block_json_files = glob( TENUP_THEME_BLOCK_DIST_DIR . '*/block.json' );
// auto register all blocks that were found.
foreach ( $block_json_files as $filename ) {
$block_folder = dirname( $filename );
$block_options = [];
$markup_file_path = $block_folder . '/markup.php';
if ( file_exists( $markup_file_path ) ) {
// only add the render callback if the block has a file called markup.php in it's directory
$block_options['render_callback'] = function ( $attributes, $content, $block ) use ( $block_folder ) {
// create helpful variables that will be accessible in markup.php file
$context = $block->context;
// get the actual markup from the markup.php file
ob_start();
include $block_folder . '/markup.php';
return ob_get_clean();
};
}
register_block_type_from_metadata( $block_folder, $block_options );
}
}
if ( $is_pre_wp_6 ) {
// 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-overrides',
TENUP_THEME_TEMPLATE_URL . '/dist/css/editor-style-overrides.css',
[],
Utility\get_asset_info( 'editor-style-overrides', 'version' )
);
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
wp_enqueue_script(
'editor-style-overrides',
TENUP_THEME_TEMPLATE_URL . '/dist/js/editor-style-overrides.js',
Utility\get_asset_info( 'editor-style-overrides', 'dependencies' ),
Utility\get_asset_info( 'editor-style-overrides', 'version' ),
true
);
}
}
/**
* Register block pattern categories
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns/
*
* @return void
*/
function register_block_pattern_categories() {
// Register a block pattern category
register_block_pattern_category(
'10up-theme',
[ 'label' => __( '10up Theme', 'tenup-theme' ) ]
);
}