-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathclass-blocks.php
More file actions
2168 lines (1869 loc) · 67.5 KB
/
Copy pathclass-blocks.php
File metadata and controls
2168 lines (1869 loc) · 67.5 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* LazyBlocks blocks.
*
* @package lazyblocks
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* LazyBlocks_Blocks class. Class to work with LazyBlocks CPT.
*/
class LazyBlocks_Blocks {
/**
* Transient key prefix for blocks cache.
*
* @var string
*/
const BLOCKS_CACHE_KEY_PREFIX = 'lzb_blocks_cache_';
/**
* Default block icon cache.
*
* @var string|null
*/
private static $default_icon = null;
/**
* Cache hash for current request (includes controls and filter callbacks).
*
* @var string|null
*/
private static $cache_hash = null;
/**
* Rules to sanitize SVG
*
* @var array
*/
private $kses_svg = array(
'svg' => array(
'class' => true,
'aria-hidden' => true,
'aria-labelledby' => true,
'role' => true,
'xmlns' => true,
'width' => true,
'height' => true,
'viewbox' => true, // <= Must be lower case!
),
'g' => array( 'fill' => true ),
'title' => array( 'title' => true ),
'path' => array(
'd' => true,
'fill' => true,
),
'rect' => array(
'fill' => true,
'opacity' => true,
'width' => true,
'height' => true,
'rx' => true,
'transform' => true,
),
);
/**
* LazyBlocks_Blocks constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'init', array( $this, 'remove_custom_fields_support' ), 150 );
// It is important to have priority higher than 10 to prevent possible conflicts.
// https://github.com/nk-crew/lazy-blocks/issues/247 .
add_filter( 'allowed_block_types_all', array( $this, 'allowed_block_types_all' ), 100, 2 );
// Additional elements in blocks list table.
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
add_filter( 'disable_months_dropdown', array( $this, 'disable_months_dropdown' ), 10, 2 );
add_filter( 'post_class', array( $this, 'post_class' ), 10, 3 );
add_filter( 'post_row_actions', array( $this, 'post_row_actions' ), 10, 2 );
add_filter( 'manage_lazyblocks_posts_columns', array( $this, 'manage_posts_columns' ) );
add_filter( 'manage_lazyblocks_posts_custom_column', array( $this, 'manage_posts_custom_column' ), 10, 2 );
// Actions.
add_filter( 'bulk_actions-edit-lazyblocks', array( $this, 'bulk_actions_edit' ) );
add_filter( 'handle_bulk_actions-edit-lazyblocks', array( $this, 'handle_bulk_actions_edit' ), 10, 3 );
// Sanitize block configs.
add_filter( 'lzb/get_blocks', array( $this, 'sanitize_block_configs' ), 100 );
// Disable different post statuses.
add_action( 'save_post', array( $this, 'normalize_lazyblocks_post_status' ), 20, 2 );
// Prevent direct meta API writes from bypassing block builder sanitization.
add_filter( 'add_post_metadata', array( $this, 'guard_unfiltered_block_meta' ), 10, 5 );
add_filter( 'update_post_metadata', array( $this, 'guard_unfiltered_block_meta' ), 10, 5 );
// Disabled the display of statuses in the list of blocks and replaced the Draft title in the submenu to Inactive.
add_filter( 'views_edit-lazyblocks', array( $this, 'change_activation_views_labels' ) );
// Cache invalidation hooks.
add_action( 'save_post_lazyblocks', array( $this, 'clear_blocks_cache' ) );
add_action( 'delete_post', array( $this, 'maybe_clear_blocks_cache_on_delete' ) );
add_action( 'wp_trash_post', array( $this, 'maybe_clear_blocks_cache_on_delete' ) );
add_action( 'untrash_post', array( $this, 'maybe_clear_blocks_cache_on_delete' ) );
// Plugin and theme activation/deactivation/update cache invalidation.
add_action( 'activated_plugin', array( $this, 'clear_blocks_cache' ) );
add_action( 'deactivated_plugin', array( $this, 'clear_blocks_cache' ) );
add_action( 'switch_theme', array( $this, 'clear_blocks_cache' ) );
add_action( 'upgrader_process_complete', array( $this, 'maybe_clear_blocks_cache_on_upgrade' ), 10, 2 );
// Manual cache clear action.
add_action( 'admin_init', array( $this, 'handle_manual_cache_clear' ) );
// Add cache clear link to admin.
add_filter( 'views_edit-lazyblocks', array( $this, 'add_clear_cache_link' ) );
// add gutenberg blocks assets.
if ( function_exists( 'register_block_type' ) ) {
// add custom block categories.
add_filter( 'block_categories_all', array( $this, 'block_categories_all' ), 100 );
// It is important to have priority higher than 10 to allow users register
// custom blocks using standard `init` hook.
add_action( 'init', array( $this, 'register_block' ), 20 );
add_action( 'init', array( $this, 'register_block_render' ), 20 );
}
}
/**
* Display inactive state and disable other post statuses in the list of all blocks.
*
* @param array $post_states - Block States.
* @param WP_Post $post - Post Object with all post parameters.
* @return array
*/
public function display_post_states( $post_states, $post ) {
if ( 'lazyblocks' === $post->post_type ) {
$post_states = array();
if ( 'draft' === $post->post_status ) {
$post_states['lazyblocks-inactive'] = __( 'Inactive', 'lazy-blocks' );
}
}
return $post_states;
}
/**
* Change the labels of the views in the blocks list.
*
* @param array $views - list of html links to views.
* @return array
*/
public function change_activation_views_labels( $views ) {
if ( isset( $views['draft'] ) ) {
// Replace the entire draft view with "Inactive" while keeping the count intact.
$views['draft'] = preg_replace(
'/^(<a [^>]*>).*?(<span class="count">.*?<\/span>)/',
'$1' . __( 'Inactive', 'lazy-blocks' ) . ' $2',
$views['draft']
);
}
if ( isset( $views['publish'] ) ) {
// Replace the entire publish view with "Active" while keeping the count intact.
$views['publish'] = preg_replace(
'/^(<a [^>]*>).*?(<span class="count">.*?<\/span>)/',
'$1' . __( 'Active', 'lazy-blocks' ) . ' $2',
$views['publish']
);
}
return $views;
}
/**
* Normalize post status to draft or publish for 'lazyblocks' post type.
* This function ensures that only 'publish' and 'draft' statuses are allowed
* for 'lazyblocks' post type, resetting any other status to 'draft'.
*
* @param int $post_id - Post ID.
* @param WP_Post $post - Post Object with all post parameters.
* @return void
*/
public function normalize_lazyblocks_post_status( $post_id, $post ) {
// Skip if this is an autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if (
'lazyblocks' === $post->post_type &&
! in_array( $post->post_status, array( 'publish', 'draft', 'auto-draft', 'trash' ), true )
) {
// Temporarily remove this function to prevent infinite loops.
remove_action( 'save_post', array( $this, 'normalize_lazyblocks_post_status' ) );
// Update the post status to 'draft'.
wp_update_post(
array(
'ID' => $post_id,
'post_status' => 'draft',
)
);
// Re-add this function to continue monitoring post status changes.
add_action( 'save_post', array( $this, 'normalize_lazyblocks_post_status' ), 20, 2 );
}
}
/**
* Register CPT.
*/
public function register_post_type() {
register_post_type(
'lazyblocks',
array(
'labels' => array(
'menu_name' => __( 'Lazy Blocks', 'lazy-blocks' ),
'name' => __( 'Blocks', 'lazy-blocks' ),
'all_items' => __( 'Blocks', 'lazy-blocks' ),
'singular_name' => __( 'Block', 'lazy-blocks' ),
'add_new_item' => __( 'Add Block', 'lazy-blocks' ),
),
'public' => false,
'has_archive' => false,
'show_ui' => true,
// adding to custom menu manually.
'show_in_menu' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
// phpcs:ignore
'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( lazyblocks()->plugin_path() . 'assets/svg/icon-lazyblocks.svg' ) ),
'menu_position' => 80,
'capabilities' => array(
'edit_post' => 'edit_lazyblock',
'edit_posts' => 'edit_lazyblocks',
'edit_others_posts' => 'edit_other_lazyblocks',
'publish_posts' => 'publish_lazyblocks',
'read_post' => 'read_lazyblock',
'read_private_posts' => 'read_private_lazyblocks',
'delete_posts' => 'delete_lazyblocks',
'delete_post' => 'delete_lazyblock',
),
'rewrite' => true,
'supports' => array(
'title',
'editor',
'revisions',
),
'template' => array(
array(
'lzb-block-builder/main',
),
),
// we can't use it since blocks didn't inserted in some posts.
// 'template_lock' => 'all',.
)
);
}
/**
* Remove custom fields support from block builder.
* Some plugins adds support for custom fields to all post types, but we don't need it in our block builder.
*
* @link https://github.com/nk-crew/lazy-blocks/issues/141
*/
public function remove_custom_fields_support() {
remove_post_type_support( 'lazyblocks', 'custom-fields' );
}
/**
* Allowed blocks for lazyblocks post type.
*
* @param array $allowed_block_types - blocks.
* @param object $editor_context - editor context.
* @return array
*/
public function allowed_block_types_all( $allowed_block_types, $editor_context ) {
if ( empty( $editor_context->post ) || 'lazyblocks' !== $editor_context->post->post_type ) {
return $allowed_block_types;
}
return array( 'lzb-block-builder/main' );
}
/**
* Get the Lazy Blocks role capability matrix.
*
* @return array
*/
public function get_role_caps_matrix() {
return array(
'administrator' => array(
'edit_lazyblock',
'edit_lazyblocks',
'edit_other_lazyblocks',
'publish_lazyblocks',
'read_lazyblock',
'read_private_lazyblocks',
'delete_lazyblocks',
'delete_lazyblock',
),
'editor' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
'author' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
'contributor' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
);
}
/**
* Synchronize Lazy Blocks capabilities for built-in roles.
*
* @return void
*/
public function sync_role_caps() {
foreach ( $this->get_role_caps_matrix() as $role_name => $caps ) {
$role = get_role( $role_name );
if ( ! $role ) {
continue;
}
foreach ( $caps as $capability ) {
$role->add_cap( $capability );
}
}
}
/**
* Add Roles
*
* @deprecated Use sync_role_caps().
*
* @return void
*/
public function add_role_caps() {
$this->sync_role_caps();
}
/**
* Disable month dropdown.
*
* @param array $result disabled dropdown or no.
* @param object $post_type current post type name.
*
* @return array
*/
public function disable_months_dropdown( $result, $post_type ) {
return 'lazyblocks' === $post_type ? true : $result;
}
/**
* Add active/inactive class to row
*
* @param array $classes Array of post classes.
* @param array $classname Additional classes added to the post.
* @param int $post_id The post ID.
* @return array
*/
public function post_class( $classes, $classname, $post_id ) {
if ( ! is_admin() ) {
return $classes;
}
if ( get_post_type( $post_id ) === 'lazyblocks' ) {
$classes[] = get_post_status( $post_id ) === 'publish' ? 'lazyblocks-row-active' : 'lazyblocks-row-inactive';
}
return $classes;
}
/**
* Returns the admin URL for the current post type edit page.
*
* @param array $params Extra URL params.
* @return string
*/
public function get_admin_url( $params = array() ) {
if ( ! isset( $params['paged'] ) && isset( $_GET['paged'] ) ) {
$params['paged'] = intval( $_GET['paged'] );
}
return add_query_arg( $params, admin_url( 'edit.php?post_type=lazyblocks' ) );
}
/**
* Add featured image in lazyblocks list
*
* @param array $actions actions for posts.
* @param object $post current post data.
*
* @return array
*/
public function post_row_actions( $actions = array(), $post = null ) {
if ( ! $post || 'lazyblocks' !== $post->post_type ) {
return $actions;
}
// remove quick edit link.
if ( isset( $actions['inline hide-if-no-js'] ) ) {
unset( $actions['inline hide-if-no-js'] );
}
// add duplicate and export link.
$actions = array_merge(
array_slice( $actions, 0, 1 ),
array(
'duplicate' => sprintf(
'<a href="%1$s" aria-label="%2$s">%3$s</a>',
$this->get_admin_url(
array(
'lazyblocks_duplicate_block' => intval( $post->ID ),
'lazyblocks_duplicate_block_nonce' => wp_create_nonce( 'lzb-duplicate-block-nonce' ),
)
),
sprintf(
// translators: %1$ - post title.
esc_attr__( 'Duplicate “%1$s”', 'lazy-blocks' ),
get_the_title( $post->ID )
),
esc_html__( 'Duplicate', 'lazy-blocks' )
),
'export' => sprintf(
'<a href="%1$s" aria-label="%2$s">%3$s</a>',
$this->get_admin_url(
array(
'lazyblocks_export_block' => intval( $post->ID ),
'lazyblocks_export_nonce' => wp_create_nonce( 'lzb-export-blocks-nonce' ),
)
),
sprintf(
// translators: %1$ - post title.
esc_attr__( 'Export “%1$s”', 'lazy-blocks' ),
get_the_title( $post->ID )
),
esc_html__( 'Export', 'lazy-blocks' )
),
'activate' => sprintf(
'<a href="%1$s" aria-label="%2$s" class="%3$s">%4$s</a>',
$this->get_admin_url(
array(
( 'publish' === $post->post_status ? 'lazyblocks_deactivate_block' : 'lazyblocks_activate_block' ) => intval( $post->ID ),
'lazyblocks_activate_block_nonce' => wp_create_nonce( 'lzb-activate-block-nonce' ),
)
),
sprintf(
// translators: %1$ - post title.
'publish' === $post->post_status ? esc_attr__( 'Deactivate “%1$s”', 'lazy-blocks' ) : esc_attr__( 'Activate “%1$s”', 'lazy-blocks' ),
get_the_title( $post->ID )
),
'publish' === $post->post_status ? 'lazyblocks-deactivate-block' : 'lazyblocks-activate-block',
'publish' === $post->post_status ? esc_html__( 'Deactivate', 'lazy-blocks' ) : esc_html__( 'Activate', 'lazy-blocks' )
),
),
array_slice( $actions, 1 )
);
return $actions;
}
/**
* Bulk actions.
*
* @param array $actions bulk actions for posts.
*
* @return array
*/
public function bulk_actions_edit( $actions = array() ) {
unset( $actions['edit'] );
$actions['export'] = esc_html__( 'Export', 'lazy-blocks' );
$actions['activate'] = esc_html__( 'Activate', 'lazy-blocks' );
$actions['deactivate'] = esc_html__( 'Deactivate', 'lazy-blocks' );
return $actions;
}
/**
* Prepare to bulk export, activate or deactivate blocks.
*
* @param string $redirect redirect url after export or activate/deactivate blocks.
* @param string $action action name.
* @param array $post_ids post ids for export, activate or deactivate blocks.
*
* @return string
*/
public function handle_bulk_actions_edit( $redirect, $action, $post_ids ) {
if ( 'export' === $action ) {
lazyblocks()->tools()->export_json( $post_ids, 'blocks' );
}
if ( 'activate' === $action ) {
lazyblocks()->tools()->activate( $post_ids );
}
if ( 'deactivate' === $action ) {
lazyblocks()->tools()->deactivate( $post_ids );
}
return $redirect;
}
/**
* Add featured image in lazyblocks list
*
* @param array $columns columns of the table.
*
* @return array
*/
public function manage_posts_columns( $columns = array() ) {
$columns = array(
'cb' => $columns['cb'],
'lazyblocks_post_icon' => esc_html__( 'Icon', 'lazy-blocks' ),
'title' => $columns['title'],
'lazyblocks_post_slug' => esc_html__( 'Slug', 'lazy-blocks' ),
'lazyblocks_post_category' => esc_html__( 'Category', 'lazy-blocks' ),
'lazyblocks_post_description' => esc_html__( 'Description', 'lazy-blocks' ),
);
return $columns;
}
/**
* Add thumb to the column
*
* @param bool $column_name column name.
*/
public function manage_posts_custom_column( $column_name = false ) {
global $post;
if ( 'lazyblocks_post_icon' === $column_name ) {
$icon = $this->prepare_block_icon( $this->get_meta_value_by_id( 'lazyblocks_icon' ) );
$admin_url = get_edit_post_link( $post->ID );
echo '<a class="lzb-admin-block-icon" href="' . esc_url( $admin_url ) . '">';
if ( $icon && strpos( $icon, 'dashicons' ) === 0 ) {
echo '<span class="dashicons ' . esc_attr( $icon ) . '"></span>';
} elseif ( $icon ) {
// XSS:OK - this variable is already escaped in the `prepare_block_icon` function.
echo $icon;
}
echo '</a>';
}
if ( 'lazyblocks_post_slug' === $column_name ) {
$slug = $this->get_meta_value_by_id( 'lazyblocks_slug' );
if ( $slug ) {
$namespace = 'lazyblock';
$slug_value = $slug;
if ( strpos( $slug_value, '/' ) ) {
$namespace = explode( '/', $slug_value )[0];
$slug_value = explode( '/', $slug_value )[1];
}
echo '<code class="lzb-admin-block-slug">' . esc_html( $namespace ) . '/' . esc_html( $slug_value ) . '</code>';
} else {
echo '—';
}
}
if ( 'lazyblocks_post_category' === $column_name ) {
$category = $this->get_meta_value_by_id( 'lazyblocks_category' );
if ( $category ) {
$gutenberg_categories = array();
if ( function_exists( 'get_block_categories' ) ) {
$gutenberg_categories = get_block_categories( $post );
} elseif ( function_exists( 'gutenberg_get_block_categories' ) ) {
$gutenberg_categories = gutenberg_get_block_categories( $post );
}
foreach ( $gutenberg_categories as $cat ) {
if ( $cat['slug'] === $category ) {
$category = $cat['title'];
break;
}
}
echo esc_html( $category );
}
}
if ( 'lazyblocks_post_description' === $column_name ) {
$description = $this->get_meta_value_by_id( 'lazyblocks_description' );
echo wp_kses_post( $description );
}
}
/**
* Default block data.
*
* @var array
*/
private $block_defaults = array(
'lazyblocks_controls' => array(),
'lazyblocks_slug' => '',
'lazyblocks_icon' => '',
'lazyblocks_description' => '',
'lazyblocks_keywords' => '',
'lazyblocks_category' => 'text',
'lazyblocks_code_show_preview' => 'always',
'lazyblocks_code_single_output' => 'false',
'lazyblocks_code_output_method' => 'html',
'lazyblocks_code_editor_html' => '',
'lazyblocks_code_editor_callback' => '',
'lazyblocks_code_frontend_html' => '',
'lazyblocks_code_frontend_callback' => '',
'lazyblocks_style_block' => '',
'lazyblocks_style_editor' => '',
'lazyblocks_script_view' => '',
'lazyblocks_styles' => array(),
'lazyblocks_supports_color' => 'false',
'lazyblocks_supports_typography' => 'false',
'lazyblocks_supports_spacing' => 'false',
'lazyblocks_supports_dimensions' => 'false',
'lazyblocks_supports_shadow' => 'false',
'lazyblocks_supports_layout' => 'false',
'lazyblocks_supports_align' => array( 'wide', 'full' ),
'lazyblocks_supports_anchor' => 'false',
'lazyblocks_supports_classname' => 'true',
'lazyblocks_supports_multiple' => 'true',
'lazyblocks_supports_inserter' => 'true',
'lazyblocks_supports_reusable' => 'true',
'lazyblocks_supports_lock' => 'true',
'lazyblocks_supports_html' => 'false',
// Ghost Kit Extensions.
'lazyblocks_supports_ghostkit_effects' => 'false',
'lazyblocks_supports_ghostkit_position' => 'false',
'lazyblocks_supports_ghostkit_spacings' => 'false',
'lazyblocks_supports_ghostkit_frame' => 'false',
'lazyblocks_supports_ghostkit_transform' => 'false',
'lazyblocks_supports_ghostkit_custom_css' => 'false',
'lazyblocks_supports_ghostkit_display' => 'false',
'lazyblocks_supports_ghostkit_attributes' => 'false',
'lazyblocks_condition_post_types' => '',
);
/**
* Return default block data.
*
* @return array
*/
public function get_block_defaults() {
return apply_filters( 'lzb/block_defaults', $this->block_defaults );
}
/**
* Get metabox value by name.
*
* @param string $name - meta name.
* @param mixed $result - result of stored attribute.
*
* @return mixed
*/
private function get_meta_value( $name, $result ) {
$defaults = $this->get_block_defaults();
$default = null;
if ( isset( $defaults[ $name ] ) ) {
$default = $defaults[ $name ];
}
if ( '' === $result && null !== $default ) {
$result = $default;
}
if ( 'true' === $result ) {
$result = true;
} elseif ( 'false' === $result ) {
$result = false;
}
return $result;
}
/**
* Get metabox value by name and post id.
*
* @param string $name - meta name.
* @param int|boolean $id - post id.
* @return mixed
*/
private function get_meta_value_by_id( $name, $id = false ) {
if ( ! $id ) {
global $post;
$id = $post->ID;
}
$result = get_post_meta( $id, $name, true );
return $this->get_meta_value( $name, $result );
}
/**
* Get metabox value by name and block data.
*
* @param string $name - meta name.
* @param array $block_data - supplied block data.
*
* @return mixed
*/
private function get_meta_value_by_block( $name, $block_data ) {
$result = $block_data[ $name ] ?? null;
return $this->get_meta_value( $name, $result );
}
/**
* Sanitize block slug name.
* Keep only alpha and numbers.
* Make it lowercase.
*
* Support also slugs with namespaces like:
* lazyblock/my-block
*
* @param string $slug - slug name.
*
* @return string
*/
public function sanitize_slug( $slug ) {
// Split by namespace separator.
$parts = explode( '/', $slug );
// Sanitize each part.
$sanitized_parts = array_map(
function ( $part ) {
return strtolower( preg_replace( '/[^a-zA-Z0-9\-]+/', '', $part ) );
},
$parts
);
// If we have 2 parts, join them with '/'.
if ( count( $parts ) === 2 ) {
return implode( '/', $sanitized_parts );
}
// Otherwise return just the sanitized string.
return $sanitized_parts[0];
}
/**
* Returns true if the current user is allowed to save unfiltered HTML.
*
* @return bool
*/
public function is_allowed_unfiltered_html() {
$allow_unfiltered_html = current_user_can( 'unfiltered_html' );
return apply_filters( 'lzb/allow_unfiltered_html', $allow_unfiltered_html );
}
/**
* Prevent unsafe block code fields from being written outside save_meta_boxes().
*
* WordPress XML-RPC and custom fields can write post meta directly, bypassing
* the block builder REST endpoint where these fields are normally checked.
*
* @param null|bool $check Whether to short-circuit the metadata update.
* @param int $object_id Post ID.
* @param string $meta_key Meta key.
* @param mixed $_meta_value Metadata value to store.
* @param mixed $_meta_arg Unique flag for add_post_metadata or previous value for update_post_metadata.
*
* @return null|bool
*/
public function guard_unfiltered_block_meta( $check, $object_id, $meta_key, $_meta_value, $_meta_arg ) {
unset( $_meta_value, $_meta_arg );
$unsafe_meta_keys = apply_filters(
'lzb/unfiltered_block_meta_keys',
array(
'lazyblocks_code_editor_html',
'lazyblocks_code_frontend_html',
'lazyblocks_script_view',
)
);
if ( ! in_array( $meta_key, $unsafe_meta_keys, true ) ) {
return $check;
}
if ( 'lazyblocks' !== get_post_type( $object_id ) ) {
return $check;
}
if ( ! $this->is_allowed_unfiltered_html() ) {
return false;
}
return $check;
}
/**
* Save Format metabox
*
* @param int $post_id The post ID.
* @param array $data Metaboxes data for save.
*/
public function save_meta_boxes( $post_id, $data ) {
$defaults = $this->get_block_defaults();
foreach ( $defaults as $meta => $default ) {
$new_meta_value = '';
if ( isset( $data[ $meta ] ) ) {
// convert boolean to string.
if ( is_bool( $data[ $meta ] ) ) {
$data[ $meta ] = $data[ $meta ] ? 'true' : 'false';
}
// icon and editors.
if (
'lazyblocks_icon' === $meta ||
'lazyblocks_code_editor_html' === $meta ||
'lazyblocks_code_frontend_html' === $meta ||
'lazyblocks_style_editor' === $meta ||
'lazyblocks_style_block' === $meta ||
'lazyblocks_script_view' === $meta
) {
// Disallow unfiltered code fields for users without unfiltered_html capability.
if (
(
'lazyblocks_code_editor_html' === $meta ||
'lazyblocks_code_frontend_html' === $meta ||
'lazyblocks_script_view' === $meta
) &&
! $this->is_allowed_unfiltered_html()
) {
continue;
}
$new_meta_value = wp_slash( $data[ $meta ] );
} else {
$new_meta_value = wp_slash( $data[ $meta ] );
// Filter $_POST data for users without the 'unfiltered_html' capability.
if ( ! $this->is_allowed_unfiltered_html() ) {
$new_meta_value = wp_kses_post_deep( $new_meta_value );
}
}
}
// keep only alpha and numbers in slug.
if ( 'lazyblocks_slug' === $meta ) {
$new_meta_value = $this->sanitize_slug( $new_meta_value );
// generate slug from title.
if ( ! $new_meta_value ) {
$new_meta_value = get_the_title();
$new_meta_value = $this->sanitize_slug( $new_meta_value );
}
// if no slug available.
if ( ! $new_meta_value ) {
$new_meta_value = 'no-slug';
}
}
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta, true );
$meta_value_to_check = $meta_value;
$new_meta_value_to_check = $new_meta_value;
if ( is_array( $meta_value_to_check ) ) {
$meta_value_to_check = wp_json_encode( $meta_value_to_check );
}
if ( is_array( $new_meta_value_to_check ) ) {
$new_meta_value_to_check = wp_json_encode( $new_meta_value_to_check );
}
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value_to_check && '' === $meta_value_to_check ) {
add_post_meta( $post_id, $meta, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
} elseif ( $new_meta_value_to_check && $new_meta_value_to_check !== $meta_value_to_check ) {
update_post_meta( $post_id, $meta, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
} elseif ( '' === $new_meta_value_to_check && $meta_value_to_check ) {
delete_post_meta( $post_id, $meta, $meta_value );
}
}
}
/**
* Get metabox data
*
* @param int $post_id The post ID.
*
* @return array|null
*/
public function get_meta_boxes( $post_id ) {
$defaults = $this->get_block_defaults();
$result_meta = array();
foreach ( $defaults as $meta => $default ) {
$result_meta[ $meta ] = $this->get_meta_value_by_id( $meta, $post_id );
}
return $result_meta;
}
/**
* Blocks list.
*
* @var array|null
*/
private $blocks = null;
/**
* Blocks list added by user using add_blocks method.
*
* @var null
*/
private $user_blocks = null;
/**
* Add block.
*
* @param array $data - block data.
*/
public function add_block( $data ) {
if ( null === $this->user_blocks ) {
$this->user_blocks = array();
}
$this->user_blocks[] = apply_filters( 'lzb/add_user_block', $data );
}
/**
* Remove block.
*
* @param string $block_slug - block slug.
*/
public function remove_block( $block_slug ) {
if ( is_array( $this->user_blocks ) ) {
foreach ( $this->user_blocks as $k => $val ) {
if ( isset( $val['slug'] ) && $val['slug'] === $block_slug ) {
unset( $this->user_blocks[ $k ] );
}
}
}
}
/**
* Prepare block controls by adding defaults.
*
* @param array $controls - block controls.
* @param array $all_controls - all registered controls.
*
* @return array
*/
public function prepare_block_controls( $controls, $all_controls ) {
$result = array();
foreach ( (array) $controls as $k => $control ) {
if ( isset( $control['type'] ) && isset( $all_controls[ $control['type'] ] ) && isset( $all_controls[ $control['type'] ]['attributes'] ) ) {
$result[ $k ] = array_merge(
$all_controls[ $control['type'] ]['attributes'],
$control
);
}
}
return $result;
}
/**