-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap-blocks.php
117 lines (108 loc) · 4.11 KB
/
snap-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
<?php
/**
* Plugin Name: Snap blocks
* Description: This is an example block library created for testing purposes.
* Requires at least: 5.9
* Requires PHP: 7.0
* Version: 0.1.0
* Author: Atomic Smash
* Author URI: https://www.atomicsmash.co.uk/
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: block-test
*
* @package block-test
*/
define('BLOCKS_DIR', plugin_dir_path( __FILE__ ).'build/blocks/');
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function blocks_init() {
try {
set_error_handler(function($errno, $errstr, $errfile, $errline) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
$build_directory = scandir(BLOCKS_DIR);
restore_error_handler();
} catch (Exception $e) {
if (is_admin()) {
if (wp_get_environment_type() === "development" || wp_get_environment_type() === "local") {
wp_die("Blocks directory missing in snap blocks plugin. You may need to run `npm run build` or `npm run dev`");
} else {
error_log("Blocks directory missing in snap blocks plugin.");
wp_die("There was a fatal error with the snap blocks plugin. Please install a previous version and inform the Atomic Smash team.");
}
} else {
return;
}
}
register_block_assets_by_block_name();
$blocksFolders = array_filter(array_diff($build_directory, array('..', '.')), function($file_or_directory) {
return is_dir(BLOCKS_DIR . $file_or_directory);
});
foreach ($blocksFolders as $block) {
register_block_type( __DIR__ . '/build/blocks/' . $block . '/block.json' );
}
}
add_action( 'init', 'blocks_init' );
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle) {
return $needle !== '' ? substr($haystack, -strlen($needle)) === $needle : true;
}
}
/**
* Here we register all our JS and CSS files ready to be enqueued.
* The block names are then referenced in and enqueued from the block.json files of the block.
*/
function register_block_assets_by_block_name() {
$assets = include( plugin_dir_path( __FILE__ ) . 'build/assets.php');
$registeredBlockStyles = array();
foreach( $assets as $block_path => $asset ) {
if (!str_starts_with($block_path, 'blocks/')) {
continue;
}
list($block_name, $filename) = explode('/', str_replace('blocks/','',$block_path));
list($script_name) = explode('.', $filename);
if ($script_name === 'index') {
$script_handle = $block_name . '-script';
} else if ($script_name === 'editor') {
$script_handle = $block_name . '-editor-script';
} else if ($script_name === 'view') {
$script_handle = $block_name . '-view-script';
} else {
$script_handle = $script_name;
}
wp_register_script($script_handle, plugins_url( 'build/'.$block_path, __FILE__), $asset['dependencies'], $asset['version'], false);
if (!in_array($block_name, $registeredBlockStyles, true)) {
$directoryFiles = array_diff(scandir(BLOCKS_DIR . $block_name), array('..', '.', $filename));
foreach ($directoryFiles as $file) {
if (!str_ends_with($file, '.css')) {
continue;
}
$stylesheet_name = explode('.',$file)[0];
if ($stylesheet_name === 'style') {
$stylesheet_handle = $block_name . '-styles';
} else if ($stylesheet_name === 'editor-styles') {
$stylesheet_handle = $block_name . '-editor-styles';
} else {
$stylesheet_handle = $stylesheet_name;
}
wp_register_style($stylesheet_handle, plugins_url( 'build/blocks/'.$block_name . '/' . $file, __FILE__), array(), null, false);
}
$registeredBlockStyles[] = $block_name;
}
}
}