-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblocks.php
More file actions
241 lines (213 loc) · 6.55 KB
/
blocks.php
File metadata and controls
241 lines (213 loc) · 6.55 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Plugin Name: Mindspun Responsive Blocks
* Description: Fully-responsive, easy-to-use Gutenberg blocks for your site.
* Requires at least: 6.2
* Requires PHP: 7.4
* Version: 0.18.1
* Author: Mindspun
* Author URI: https://www.mindspun.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: mrblx
*/
use MRBLX\Admin\SettingsPage;
use MRBLX\API\FormEndpoint;
use MRBLX\Facades\Styles;
use MRBLX\Providers\StylesProvider;
use MRBLX\Vendor\Mindspun\Framework\Autoloader;
use MRBLX\CSSBuilder;
use MRBLX\Vendor\Mindspun\Framework\Providers\GlobalsProvider;
if ( ! defined( 'ABSPATH' ) ) {
exit;
// Exit if accessed directly.
}
require_once __DIR__ . '/vendor-prefixed/autoload.php';
Autoloader::autoload( 'MRBLX', __DIR__ . '/includes' );
/* Constants */
if ( ! defined( 'MRBLX_OPTION' ) ) {
define( 'MRBLX_OPTION', 'mrblx_option' );
}
if ( ! defined( 'MRBLX_REST_NAMESPACE' ) ) {
define( 'MRBLX_REST_NAMESPACE', 'mindspun/blocks/v1' );
}
/* Providers */
GlobalsProvider::provide();
StylesProvider::provide();
/**
* Helper function to build CSS from the block attributes using the given selector.
*
* @param string $id
* @param array $attributes
* @param array $options
* @return string
* @noinspection PhpUnused
*/
function mrblx_build_css( string $id, array $attributes, array $options = array() ): string {
$builder = new CSSBuilder();
$builder->add_attributes( $attributes );
return $builder->to_css( $id, $options );
}
/**
* Returns the CSS for one block, not including inner blocks.
*
* @param array $block
* @return string
*/
function mrblx_block_css( array $block ): string {
$css = '';
$registry = WP_Block_Type_Registry::get_instance();
$attrs = $block['attrs'] ?? array();
$block_id = $attrs['blockId'] ?? '';
if ( $block_id ) {
/* @var array $attribute */
foreach ( $attrs as $attr => $value ) {
if ( is_array( $value ) && ! empty( $value ) ) {
/* The selector can be specified inside the array or as a 'magic' property of the attribute. */
$selector = $value['__selector__'] ?? null;
if ( ! $selector ) {
$block_type = $registry->get_registered( $block['blockName'] );
if ( $block_type ) {
$selector = $block_type->attributes[ $attr ]['selector'] ?? null;
}
}
/* An attribute is a style if it is named 'style' or has a selector */
if ( 'style' === $attr || $selector ) {
$css .= mrblx_build_css( "mrblx-$block_id", $value, array( 'selector' => $selector ) );
}
}
}
}
return $css;
}
/**
* Generate a style for the given block if needed.
*
* @param array $block
* @return void
*/
function mrblx_style_block( array $block ): void {
$attrs = $block['attrs'] ?? array();
$block_id = $attrs['blockId'] ?? '';
if ( $block_id ) {
$css = mrblx_block_css( $block );
if ( $css ) {
Styles::add( $css );
}
}
}
add_action(
'enqueue_block_editor_assets',
function () {
$handle = '@mindspun/mrblx';
wp_enqueue_style( $handle );
}
);
add_action(
'admin_enqueue_scripts',
function () {
$handle = '@mindspun/mrblx';
$asset_path = __DIR__ . '/dist/mrblx.asset.php';
$args = require_once $asset_path;
$style_path = plugins_url( '/dist/mrblx.css', __FILE__ );
wp_register_style( $handle, $style_path, array( 'wp-codemirror' ), $args['version'] );
$script_path = plugins_url( '/dist/mrblx.js', __FILE__ );
if ( ! wp_register_script( $handle, $script_path, $args['dependencies'], $args['version'] ) ) {
error_log( 'Failed to register script: mrblx.js' );
}
}
);
/* Custom Block Editor category for our blocks. */
add_filter(
'block_categories_all',
function ( $categories ) {
$category = array(
'slug' => 'mindspun-responsive-blocks',
'title' => 'Mindspun Responsive Blocks',
);
return array_merge( array( $category ), $categories );
}
);
/* Register our blocks. */
add_action(
'init',
function () {
foreach ( scandir( __DIR__ . '/dist' ) as $name ) {
if ( ! str_contains( $name, '.' ) ) {
register_block_type( __DIR__ . '/dist/' . $name );
}
}
}
);
/**
* Registers our front-end style.
*
* @return void
*/
function mrblx_enqueue_style() {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data( __FILE__ );
wp_enqueue_style(
'style-mrblx',
plugins_url( 'dist/style-mrblx.css', __FILE__ ),
array(),
$plugin_data['Version']
);
$styles = Styles::get_clean();
if ( $styles ) {
wp_add_inline_style( 'style-mrblx', $styles );
}
}
/* Global stylesheet for blocks that need it. This style is used in both the front-end and the editor. */
add_action( 'wp_footer', 'mrblx_enqueue_style' );
add_action( 'admin_enqueue_scripts', 'mrblx_enqueue_style' );
/* Build the styles */
add_filter(
'render_block',
function ( $block_content, $block ) {
mrblx_style_block( $block );
return $block_content;
},
10,
2,
);
/* Admin / Settings page */
add_action(
'admin_menu',
function () {
$page = new SettingsPage();
add_action( 'admin_init', array( $page, 'admin_init' ) );
}
);
/* REST API (if enabled) */
add_action(
'rest_api_init',
function () {
$options = get_option( MRBLX_OPTION );
if ( $options['rest_api_enabled'] ?? true ) {
$endpoint = new FormEndpoint();
register_rest_route(
MRBLX_REST_NAMESPACE,
'/form',
array(
'methods' => 'POST',
'callback' => array( $endpoint, 'post' ),
'permission_callback' => '__return_true',
)
);
}
}
);
/* Make the REST URL available to the form block. */
add_action(
'enqueue_block_editor_assets',
function () {
wp_add_inline_script(
'mindspun-form-editor-script',
'window.mrblxData = window.mrblxData || {}; window.mrblxData.rest_url = "' . esc_url_raw( rest_url( MRBLX_REST_NAMESPACE ) ) . '";',
'before'
);
}
);