PluginProbe
Block Enhancements – Extended styling for the Block Editor / 1.2.3
Block Enhancements – Extended styling for the Block Editor v1.2.3
trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 46 releases
block-enhancements / block-enhancements.php

block-enhancements.php in Block Enhancements – Extended styling for the Block Editor 1.2.3, at block-enhancements.php

555 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Block Enhancements
4 * Description: Enhance Gutenberg blocks with practical features such as icons, shadow, transform, transition, responsive typography, text alignment, hover style, etc.
5 * Requires at least: 6.3
6 * Requires PHP: 7.0
7 * Version: 1.2.3
8 * Author: Phi Phan
9 * Author URI: https://boldblocks.net
10 *
11 * @package BlockEnhancements
12 * @copyright Copyright(c) 2022, Phi Phan
13 */
14
15 namespace BlockEnhancements;
16
17 // Exit if accessed directly.
18 defined( 'ABSPATH' ) || exit;
19
20 if ( ! class_exists( BlockEnhancements::class ) ) :
21 /**
22 * The main class
23 */
24 final class BlockEnhancements {
25
26 /**
27 * Plugin version
28 *
29 * @var String
30 */
31 protected $version = '1.2.3';
32
33 /**
34 * Components
35 *
36 * @var Array
37 */
38 protected $components = [];
39
40 /**
41 * Store the main asset file.
42 *
43 * @var array
44 */
45 protected $index_asset = [];
46
47 /**
48 * Plugin instance
49 *
50 * @var BlockEnhancements
51 */
52 private static $instance;
53
54 /**
55 * A dummy constructor
56 */
57 private function __construct() {}
58
59 /**
60 * Initialize the instance.
61 *
62 * @return BlockEnhancements
63 */
64 public static function get_instance() {
65 if ( ! isset( self::$instance ) ) {
66 self::$instance = new BlockEnhancements();
67 self::$instance->initialize();
68 }
69
70 return self::$instance;
71 }
72
73 /**
74 * Kick start function.
75 * Define constants, load dependencies
76 *
77 * @return void
78 */
79 public function initialize() {
80 // Setup constants.
81 $this->setup_constants();
82
83 // Load dependencies.
84 $this->load_dependencies();
85
86 // Register core components.
87 $this->register_core_components();
88
89 // Run hooks.
90 $this->run();
91 }
92
93 /**
94 * Setup constants
95 *
96 * @return void
97 */
98 public function setup_constants() {
99 $this->define_constant( 'BLOCK_ENHANCEMANCES', true );
100 $this->define_constant( 'BLOCK_ENHANCEMANCES_ROOT_FILE', __FILE__ );
101 $this->define_constant( 'BLOCK_ENHANCEMANCES_VERSION', $this->version );
102 $this->define_constant( 'BLOCK_ENHANCEMANCES_PATH', trailingslashit( plugin_dir_path( BLOCK_ENHANCEMANCES_ROOT_FILE ) ) );
103 $this->define_constant( 'BLOCK_ENHANCEMANCES_URL', trailingslashit( plugin_dir_url( BLOCK_ENHANCEMANCES_ROOT_FILE ) ) );
104 $this->define_constant( 'BLOCK_ENHANCEMANCES_EDITOR_SCRIPTS_HANDLE', 'block-enhancements-editor-scripts' );
105 $this->define_constant( 'BLOCK_ENHANCEMANCES_EDITOR_STYLE_HANDLE', 'block-enhancements-editor-style' );
106 $this->define_constant( 'BLOCK_ENHANCEMANCES_FRONTEND_SCRIPTS_HANDLE', 'block-enhancements-frontend-scripts' );
107 $this->define_constant( 'BLOCK_ENHANCEMANCES_FRONTEND_STYLE_HANDLE', 'block-enhancements-frontend-style' );
108 }
109
110 /**
111 * Load core components
112 *
113 * @return void
114 */
115 public function register_core_components() {
116 // Load & register core components.
117 $this->include_file( 'includes/style.php' );
118 $this->include_file( 'includes/settings.php' );
119 $this->include_file( 'includes/icon-library.php' );
120
121 $core_components = [ Style::class, Settings::class, IconLibrary::class ];
122 foreach ( $core_components as $component ) {
123 $this->register_component( $component );
124 }
125 }
126
127 /**
128 * Load dependencies
129 *
130 * @return void
131 */
132 public function load_dependencies() {
133 // Load core component.
134 $this->include_file( 'includes/core-component.php' );
135 }
136
137 /**
138 * Run main hooks
139 *
140 * @return void
141 */
142 public function run() {
143 // Load translations.
144 add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
145
146 // Main hooks.
147 add_action( 'init', [ $this, 'init' ], 5 );
148
149 // Enqueue scripts for editor.
150 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
151
152 // Run all components.
153 foreach ( $this->components as $component ) {
154 $component->run();
155 }
156 }
157
158 /**
159 * Process on init hook
160 *
161 * @return void
162 */
163 public function init() {
164 // Load the index asset file.
165 if ( ! $this->index_asset ) {
166 $this->index_asset = $this->include_file( 'build/index.asset.php' );
167 }
168
169 // Run a hook when the plugin initialized.
170 do_action( 'block-enhancements/init' );
171 }
172
173 /**
174 * Enqueue editor assets
175 *
176 * @return void
177 */
178 public function enqueue_block_editor_assets() {
179 // $index_asset = $this->include_file( 'build/index.asset.php' );
180
181 // Scripts.
182 wp_enqueue_script(
183 BLOCK_ENHANCEMANCES_EDITOR_SCRIPTS_HANDLE,
184 $this->get_file_uri( 'build/index.js' ),
185 $this->index_asset['dependencies'] ?? [],
186 $this->get_script_version( $this->index_asset ),
187 true
188 );
189
190 wp_set_script_translations( BLOCK_ENHANCEMANCES_EDITOR_SCRIPTS_HANDLE, 'block-enhancements' );
191
192 // For REST API requests.
193 wp_localize_script(
194 BLOCK_ENHANCEMANCES_EDITOR_SCRIPTS_HANDLE,
195 'BlockEnhancements',
196 [
197 'restRoot' => esc_url_raw( rest_url() ),
198 'restNonce' => wp_create_nonce( 'wp_rest' ),
199 ]
200 );
201
202 // Block features.
203 wp_localize_script(
204 BLOCK_ENHANCEMANCES_EDITOR_SCRIPTS_HANDLE,
205 'BlockEnhancementsFeatures',
206 $this->get_features()
207 );
208 }
209
210 /**
211 * The default value for allowed blocks
212 *
213 * @return array
214 */
215 public function default_allowed_blocks() {
216 return [
217 'withIcon' => [
218 [ 'name' => 'core/button' ],
219 [ 'name' => 'core/read-more' ],
220 [ 'name' => 'core/list' ],
221 [ 'name' => 'core/heading' ],
222 ],
223 'withTextAlignment' => [
224 [ 'name' => 'core/group' ],
225 [ 'name' => 'core/columns' ],
226 [ 'name' => 'core/column' ],
227 [ 'name' => 'core/media-text' ],
228 [ 'name' => 'core/cover' ],
229 ],
230 'withColor' => [
231 [ 'name' => 'core/button' ],
232 [ 'name' => 'core/read-more' ],
233 [ 'name' => 'boldblocks/svg-block' ],
234 ],
235 'withShadow' => [
236 [ 'name' => 'core/button' ],
237 [ 'name' => 'core/read-more' ],
238 [ 'name' => 'core/image' ],
239 [ 'name' => 'core/group' ],
240 [ 'name' => 'core/columns' ],
241 [ 'name' => 'core/column' ],
242 [ 'name' => 'core/media-text' ],
243 [ 'name' => 'core/cover' ],
244 [ 'name' => 'boldblocks/svg-block' ],
245 ],
246 'withTextShadow' => [
247 [ 'name' => 'core/heading' ],
248 [ 'name' => 'core/paragraph' ],
249 [ 'name' => 'core/group' ],
250 [ 'name' => 'core/columns' ],
251 [ 'name' => 'core/column' ],
252 [ 'name' => 'core/media-text' ],
253 [ 'name' => 'core/cover' ],
254 ],
255 'withTransform' => [
256 [ 'name' => 'core/group' ],
257 [ 'name' => 'core/columns' ],
258 [ 'name' => 'core/column' ],
259 [ 'name' => 'core/media-text' ],
260 [ 'name' => 'core/cover' ],
261 [ 'name' => 'core/button' ],
262 [ 'name' => 'core/read-more' ],
263 [ 'name' => 'core/image' ],
264 [ 'name' => 'core/heading' ],
265 [ 'name' => 'core/paragraph' ],
266 [ 'name' => 'boldblocks/svg-block' ],
267 ],
268 'withTransition' => [
269 [ 'name' => 'core/group' ],
270 [ 'name' => 'core/columns' ],
271 [ 'name' => 'core/column' ],
272 [ 'name' => 'core/media-text' ],
273 [ 'name' => 'core/cover' ],
274 [ 'name' => 'core/button' ],
275 [ 'name' => 'core/read-more' ],
276 [ 'name' => 'core/image' ],
277 [ 'name' => 'core/heading' ],
278 [ 'name' => 'core/paragraph' ],
279 [ 'name' => 'boldblocks/svg-block' ],
280 ],
281 'withTypography' => [
282 [ 'name' => 'core/heading' ],
283 [ 'name' => 'core/paragraph' ],
284 [ 'name' => 'core/button' ],
285 [ 'name' => 'core/post-title' ],
286 [ 'name' => 'core/site-title' ],
287 ],
288 ];
289 }
290
291 /**
292 * Get a list of features
293 *
294 * @return array
295 */
296 public function define_features() {
297 // Get default allowed blocks.
298 $default_allowed_blocks = $this->default_allowed_blocks();
299
300 return [
301 'withIcon' => [
302 'availableBlocks' => apply_filters(
303 'block_enhancements_get_available_blocks_by_feature',
304 [
305 [ 'name' => 'core/button' ],
306 [ 'name' => 'core/read-more' ],
307 [ 'name' => 'core/heading' ],
308 [ 'name' => 'core/post-title' ],
309 [ 'name' => 'core/query-title' ],
310 [ 'name' => 'core/comments-title' ],
311 [ 'name' => 'core/list' ],
312 [ 'name' => 'core/categories' ],
313 [ 'name' => 'core/latest-posts' ],
314 ],
315 'withIcon'
316 ),
317 'allowedDefault' => $default_allowed_blocks['withIcon'],
318 ],
319 'withTextAlignment' => [
320 'availableBlocks' => apply_filters(
321 'block_enhancements_get_available_blocks_by_feature',
322 [
323 [ 'name' => '^(?!(core\/embed))' ],
324 ],
325 'withTextAlignment'
326 ),
327 'allowedDefault' => $default_allowed_blocks['withTextAlignment'],
328 ],
329 'withColor' => [
330 'availableBlocks' => apply_filters(
331 'block_enhancements_get_available_blocks_by_feature',
332 [
333 [ 'name' => '.*' ],
334 ],
335 'withColor'
336 ),
337 'allowedDefault' => $default_allowed_blocks['withColor'],
338 ],
339 'withShadow' => [
340 'availableBlocks' => apply_filters(
341 'block_enhancements_get_available_blocks_by_feature',
342 [
343 [ 'name' => '.*' ],
344 ],
345 'withShadow'
346 ),
347 'allowedDefault' => $default_allowed_blocks['withShadow'],
348 ],
349 'withTextShadow' => [
350 'availableBlocks' => apply_filters(
351 'block_enhancements_get_available_blocks_by_feature',
352 [
353 [ 'name' => '.*' ],
354 ],
355 'withTextShadow'
356 ),
357 'allowedDefault' => $default_allowed_blocks['withTextShadow'],
358 ],
359 'withTransform' => [
360 'availableBlocks' => apply_filters(
361 'block_enhancements_get_available_blocks_by_feature',
362 [
363 [ 'name' => '.*' ],
364 ],
365 'withTransform'
366 ),
367 'allowedDefault' => $default_allowed_blocks['withTransform'],
368 ],
369 'withTransition' => [
370 'availableBlocks' => apply_filters(
371 'block_enhancements_get_available_blocks_by_feature',
372 [
373 [ 'name' => '.*' ],
374 ],
375 'withTransition'
376 ),
377 'allowedDefault' => $default_allowed_blocks['withTransition'],
378 ],
379 'withTypography' => [
380 'availableBlocks' => apply_filters(
381 'block_enhancements_get_available_blocks_by_feature',
382 [
383 [ 'name' => '.*' ],
384 ],
385 'withTypography'
386 ),
387 'allowedDefault' => $default_allowed_blocks['withTypography'],
388 ],
389 ];
390 }
391
392 /**
393 * Build JS params for front-end
394 *
395 * @return array
396 */
397 public function get_features() {
398 $support_features = [];
399
400 // All features.
401 $features = $this->define_features();
402
403 // Allowed blocks.
404 $allowed_blocks = get_option( 'be_allowed_blocks' );
405
406 // Add allowed blocks to $features.
407 foreach ( $features as $feature_name => $feature ) {
408 $support_features[ $feature_name ] = [ 'availableBlocks' => $feature['availableBlocks'] ];
409
410 $allowed_blocks_by_feature = false;
411 if ( is_array( $allowed_blocks ) && count( $allowed_blocks ) > 0 ) {
412 foreach ( $allowed_blocks as $allowed_block ) {
413 // @codingStandardsIgnoreLine WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
414 if ( isset( $allowed_block['featureName'] ) && $allowed_block['featureName'] === $feature_name ) {
415 // @codingStandardsIgnoreLine WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
416 $allowed_blocks_by_feature = $allowed_block['allowedBlocks'];
417 break;
418 }
419 }
420 }
421
422 if ( empty( $allowed_blocks_by_feature ) ) {
423 $allowed_blocks_by_feature = $feature['allowedDefault'];
424 }
425
426 $support_features[ $feature_name ]['allowedBlocks'] = $allowed_blocks_by_feature;
427 }
428
429 return $support_features;
430 }
431
432 /**
433 * Load text domain
434 *
435 * @return void
436 */
437 public function load_textdomain() {
438 load_plugin_textdomain(
439 'block-enhancements',
440 false,
441 plugin_basename( realpath( __DIR__ . '/languages' ) )
442 );
443 }
444
445 /**
446 * Register component
447 *
448 * @param string $classname The class name of the component.
449 * @return void
450 */
451 public function register_component( $classname ) {
452 $this->components[ $classname ] = new $classname( $this );
453 }
454
455 /**
456 * Get a component by class name
457 *
458 * @param string $classname The class name of the component.
459 * @return mixed
460 */
461 public function get_component( $classname ) {
462 return $this->components[ $classname ] ?? false;
463 }
464
465 /**
466 * Define constant
467 *
468 * @param string $name The name of the constant.
469 * @param mixed $value The value of the constant.
470 * @return void
471 */
472 public function define_constant( $name, $value ) {
473 if ( ! defined( $name ) ) {
474 define( $name, $value );
475 }
476 }
477
478 /**
479 * Retrn file path for file or folder.
480 *
481 * @param string $path file path.
482 * @return string
483 */
484 public function get_file_path( $path ) {
485 return BLOCK_ENHANCEMANCES_PATH . $path;
486 }
487
488 /**
489 * Include file path.
490 *
491 * @param string $path file path.
492 * @return string
493 */
494 public function include_file( $path ) {
495 return include_once $this->get_file_path( $path );
496 }
497
498 /**
499 * Get file uri by file path.
500 *
501 * @param string $path file path.
502 * @return string
503 */
504 public function get_file_uri( $path ) {
505 return BLOCK_ENHANCEMANCES_URL . $path;
506 }
507
508 /**
509 * Create version for scripts/styles
510 *
511 * @param array $asset_file
512 * @return string
513 */
514 public function get_script_version( $asset_file = [] ) {
515 if ( empty( $asset_file ) ) {
516 $asset_file = $this->index_asset;
517 }
518 return wp_get_environment_type() !== 'production' ? $asset_file['version'] : BLOCK_ENHANCEMANCES_VERSION;
519 }
520
521 /**
522 * Get the plugin version
523 *
524 * @return string
525 */
526 public function get_plugin_version() {
527 return $this->version;
528 }
529 }
530
531 /**
532 * Kick start
533 *
534 * @return BlockEnhancements instance
535 */
536 function block_enhancements_get_instance() {
537 return BlockEnhancements::get_instance();
538 }
539
540 // Instantiate.
541 block_enhancements_get_instance();
542 endif;
543
544 if ( ! function_exists( __NAMESPACE__ . '\\block_enhancements_activate' ) ) {
545 /**
546 * Trigger an action when the plugin is activated.
547 *
548 * @return void
549 */
550 function block_enhancements_activate() {
551 do_action( 'block_enhancements_activate' );
552 }
553 register_activation_hook( __FILE__, __NAMESPACE__ . '\\block_enhancements_activate' );
554 }
555