-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathblocks.php
More file actions
1895 lines (1625 loc) · 59.6 KB
/
blocks.php
File metadata and controls
1895 lines (1625 loc) · 59.6 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
/**
* The SCF Blocks PHP code.
*
* @package wordpress/secure-custom-fields
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
require_once dirname( __DIR__ ) . '/pro/blocks-auto-inline-editing.php';
use function SCF\Blocks\AutoInlineEditing\apply_inline_editing_attributes_to_render_template;
// Register store.
acf_register_store( 'block-types' );
acf_register_store( 'block-cache' );
acf_register_store( 'block-meta-values' );
// Register block.json support handlers.
add_filter( 'block_type_metadata', 'acf_add_block_namespace' );
add_filter( 'block_type_metadata_settings', 'acf_handle_json_block_registration', 99, 2 );
add_action( 'acf_block_render_template', 'acf_block_render_template', 10, 6 );
/**
* Prefix block names for SCF blocks registered through block.json
*
* @since ACF 6.0.0
*
* @param array $metadata The block metadata array.
* @return array The original array with a prefixed block name if it's an ACF block.
*/
function acf_add_block_namespace( $metadata ) {
if ( acf_is_acf_block_json( $metadata ) ) {
// If the block doesn't already have a namespace, append ACF's.
if ( strpos( $metadata['name'], '/' ) === false ) {
$metadata['name'] = 'acf/' . acf_slugify( $metadata['name'] );
}
}
return $metadata;
}
/**
* Handle an SCF block registered through block.json
*
* @since ACF 6.0.0
*
* @param array $settings The compiled block settings.
* @param array $metadata The raw json metadata.
*
* @return array Block registration settings with ACF required additions.
*/
function acf_handle_json_block_registration( $settings, $metadata ) {
if ( ! acf_is_acf_block_json( $metadata ) ) {
return $settings;
}
/**
* Filters the default ACF block version for blocks registered via block.json.
*
* @since 6.6.0
*
* @param integer $default_acf_block_version The default ACF block version.
* @param array $settings An array of block settings.
* @return integer
*/
$default_acf_block_version = apply_filters( 'acf/blocks/default_block_version', 2, $settings );
// Setup SCF defaults.
$settings = wp_parse_args(
$settings,
array(
'render_template' => false,
'render_callback' => false,
'enqueue_style' => false,
'enqueue_script' => false,
'enqueue_assets' => false,
'post_types' => array(),
'uses_context' => array(),
'supports' => array(),
'attributes' => array(),
'acf_block_version' => $default_acf_block_version,
'validate' => true,
'validate_on_load' => true,
'use_post_meta' => false,
)
);
// Add user provided attributes to SCF's required defaults.
$settings['attributes'] = wp_parse_args(
acf_get_block_type_default_attributes( $metadata ),
$settings['attributes']
);
// Add default SCF 'supports' settings.
$settings['supports'] = wp_parse_args(
$settings['supports'],
array(
'align' => true,
'html' => false,
'mode' => true,
'jsx' => true,
'multiple' => true,
)
);
// Add default SCF 'uses_context' settings.
$settings['uses_context'] = array_values(
array_unique(
array_merge(
$settings['uses_context'],
array(
'postId',
'postType',
)
)
)
);
// Map custom SCF properties from the SCF key, with localization.
$property_mappings = array(
'renderCallback' => 'render_callback',
'renderTemplate' => 'render_template',
'mode' => 'mode',
'blockVersion' => 'acf_block_version',
'postTypes' => 'post_types',
'validate' => 'validate',
'validateOnLoad' => 'validate_on_load',
'usePostMeta' => 'use_post_meta',
'hideFieldsInSidebar' => 'hide_fields_in_sidebar',
'autoInlineEditing' => 'auto_inline_editing',
);
$textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : 'secure-custom-fields';
$i18n_schema = get_block_metadata_i18n_schema();
foreach ( $property_mappings as $key => $mapped_key ) {
if ( isset( $metadata['acf'][ $key ] ) ) {
unset( $settings[ $key ] );
$settings[ $mapped_key ] = $metadata['acf'][ $key ];
if ( $textdomain && isset( $i18n_schema->$key ) ) {
$settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain );
}
}
}
if ( isset( $metadata['apiVersion'] ) ) {
// Use the apiVersion defined in block.json if it exists.
$settings['api_version'] = $metadata['apiVersion'];
} elseif ( $settings['acf_block_version'] >= 3 && version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ) {
// Otherwise, if we're on WP 6.3+ and the block is ACF block version 3 or greater, use apiVersion 3.
$settings['api_version'] = 3;
} else {
// Otherwise, default to apiVersion 2.
$settings['api_version'] = 2;
}
// Add the block name and registration path to settings.
$settings['name'] = $metadata['name'];
$settings['path'] = dirname( $metadata['file'] );
// Prevent blocks that usePostMeta from being nested or saving multiple.
if ( ! empty( $settings['use_post_meta'] ) ) {
$settings['parent'] = array( 'core/post-content' );
$settings['supports']['multiple'] = false;
}
acf_get_store( 'block-types' )->set( $metadata['name'], $settings );
add_action( 'enqueue_block_editor_assets', 'acf_enqueue_block_assets' );
// Ensure our render callback is used.
$settings['render_callback'] = 'acf_render_block_callback';
return $settings;
}
/**
* Check if a block.json block is an SCF block.
*
* @since ACF 6.0.0
*
* @param array $metadata The raw block metadata array.
* @return boolean
*/
function acf_is_acf_block_json( $metadata ) {
return ( isset( $metadata['acf'] ) && $metadata['acf'] );
}
/**
* Registers a block type.
*
* @date 18/2/19
* @since ACF 5.8.0
*
* @param array $block The block settings.
* @return (array|false)
*/
function acf_register_block_type( $block ) {
// Validate block type settings.
$block = acf_validate_block_type( $block );
/**
* Filters the arguments for registering a block type.
*
* @since ACF 5.8.9
*
* @param array $block The array of arguments for registering a block type.
*/
$block = apply_filters( 'acf/register_block_type_args', $block );
// Require name.
if ( ! $block['name'] ) {
$message = __( 'Block type name is required.', 'secure-custom-fields' );
_doing_it_wrong( __FUNCTION__, $message, '5.8.0' ); //phpcs:ignore -- escape not required.
return false;
}
// Bail early if already exists.
if ( acf_has_block_type( $block['name'] ) ) {
/* translators: The name of the block type */
$message = sprintf( __( 'Block type "%s" is already registered.', 'secure-custom-fields' ), $block['name'] );
_doing_it_wrong( __FUNCTION__, $message, '5.8.0' ); //phpcs:ignore -- escape not required.
return false;
}
// Set ACF required attributes.
$block['attributes'] = acf_get_block_type_default_attributes( $block );
/**
* Filters the default ACF block version for blocks registered via acf_register_block_type().
*
* @since 6.6.0
*
* @param integer $default_acf_block_version The default ACF block version.
* @param array $block An array of block settings.
* @return integer
*/
$default_acf_block_version = apply_filters( 'acf/blocks/default_block_version', 1, $block );
if ( ! isset( $block['acf_block_version'] ) ) {
$block['acf_block_version'] = $default_acf_block_version;
}
if ( ! isset( $block['api_version'] ) ) {
if ( $block['acf_block_version'] >= 3 && version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ) {
$block['api_version'] = 3;
} else {
$block['api_version'] = 2;
}
}
// Add to storage.
acf_get_store( 'block-types' )->set( $block['name'], $block );
// Overwrite callback for WordPress registration.
$block['render_callback'] = 'acf_render_block_callback';
// Register block type in WP.
if ( function_exists( 'register_block_type' ) ) {
register_block_type(
$block['name'],
$block
);
}
// Register action.
add_action( 'enqueue_block_editor_assets', 'acf_enqueue_block_assets' );
// Return block.
return $block;
}
/**
* See acf_register_block_type().
*
* @date 18/2/19
* @since ACF 5.7.12
*
* @param array $block The block settings.
* @return (array|false)
*/
function acf_register_block( $block ) {
return acf_register_block_type( $block );
}
/**
* Returns true if a block type exists for the given name.
*
* @since ACF 5.7.12
*
* @param string $name The block type name.
* @return boolean
*/
function acf_has_block_type( $name ) {
return acf_get_store( 'block-types' )->has( $name );
}
/**
* Returns an array of all registered block types.
*
* @since ACF 5.7.12
*
* @return array
*/
function acf_get_block_types() {
return acf_get_store( 'block-types' )->get();
}
/**
* Returns a block type for the given name.
*
* @since ACF 5.7.12
*
* @param string $name The block type name.
* @return (array|null)
*/
function acf_get_block_type( $name ) {
return acf_get_store( 'block-types' )->get( $name );
}
/**
* Removes a block type for the given name.
*
* @since ACF 5.7.12
*
* @param string $name The block type name.
* @return void
*/
function acf_remove_block_type( $name ) {
acf_get_store( 'block-types' )->remove( $name );
}
/**
* Returns an array of default attribute settings for a block type.
*
* @date 19/11/18
* @since ACF 5.8.0
*
* @param array $block_type A block configuration array.
* @return array
*/
function acf_get_block_type_default_attributes( $block_type ) {
$attributes = array(
'name' => array(
'type' => 'string',
'default' => '',
),
'data' => array(
'type' => 'object',
'default' => array(),
),
'align' => array(
'type' => 'string',
'default' => '',
),
'mode' => array(
'type' => 'string',
'default' => '',
),
);
foreach ( acf_get_block_back_compat_attribute_key_array() as $new => $old ) {
if ( isset( $block_type['supports'][ $old ] ) ) {
$block_type['supports'][ $new ] = $block_type['supports'][ $old ];
unset( $block_type['supports'][ $old ] );
}
}
if ( ! empty( $block_type['supports']['alignText'] ) ) {
$attributes['alignText'] = array(
'type' => 'string',
'default' => '',
);
}
if ( ! empty( $block_type['supports']['alignContent'] ) ) {
$attributes['alignContent'] = array(
'type' => 'string',
'default' => '',
);
}
if ( ! empty( $block_type['supports']['fullHeight'] ) ) {
$attributes['fullHeight'] = array(
'type' => 'boolean',
'default' => '',
);
}
// For each of ACF's block attributes, check if the user's block attributes contains a default value we should use.
if ( isset( $block_type['attributes'] ) && is_array( $block_type['attributes'] ) ) {
foreach ( array_keys( $attributes ) as $key ) {
if ( isset( $block_type['attributes'][ $key ] ) && is_array( $block_type['attributes'][ $key ] ) && isset( $block_type['attributes'][ $key ]['default'] ) ) {
$attributes[ $key ]['default'] = $block_type['attributes'][ $key ]['default'];
}
}
}
return $attributes;
}
/**
* Validates a block type ensuring all settings exist.
*
* @since ACF 5.8.0
*
* @param array $block The block settings.
* @return array
*/
function acf_validate_block_type( $block ) {
// Add default settings.
$block = wp_parse_args(
$block,
array(
'name' => '',
'title' => '',
'description' => '',
'category' => 'common',
'icon' => '',
'mode' => 'preview',
'keywords' => array(),
'supports' => array(),
'post_types' => array(),
'uses_context' => array(),
'render_template' => false,
'render_callback' => false,
'enqueue_style' => false,
'enqueue_script' => false,
'enqueue_assets' => false,
)
);
// Generate name with prefix.
if ( $block['name'] ) {
$block['name'] = 'acf/' . acf_slugify( $block['name'] );
}
// Add default 'supports' settings.
$block['supports'] = wp_parse_args(
$block['supports'],
array(
'align' => true,
'html' => false,
'mode' => true,
)
);
// Add default 'uses_context' settings.
$block['uses_context'] = wp_parse_args(
$block['uses_context'],
array(
'postId',
'postType',
)
);
// Correct "Experimental" flags.
if ( isset( $block['supports']['__experimental_jsx'] ) ) {
$block['supports']['jsx'] = $block['supports']['__experimental_jsx'];
}
// Normalize block 'parent' setting.
if ( array_key_exists( 'parent', $block ) ) {
// As of WP 6.8, parent must be an array.
if ( null === $block['parent'] ) {
unset( $block['parent'] );
} elseif ( is_string( $block['parent'] ) ) {
$block['parent'] = array( $block['parent'] );
}
}
// Return block.
return $block;
}
/**
* Prepares a block for use in render_callback by merging in all settings and attributes.
*
* @since ACF 5.8.0
*
* @param array $block The block props.
* @return array|boolean
*/
function acf_prepare_block( $block ) {
// Bail early if no name.
if ( ! isset( $block['name'] ) ) {
return false;
}
// Ensure a block ID is always prefixed with `block_` for meta.
$block['id'] = acf_ensure_block_id_prefix( $block['id'] );
// Get block type and return false if doesn't exist.
$block_type = acf_get_block_type( $block['name'] );
if ( ! $block_type ) {
return false;
}
// Prevent protected attributes being overridden.
$protected = array(
'render_template',
'render_callback',
'enqueue_script',
'enqueue_style',
'enqueue_assets',
'post_types',
'use_post_meta',
);
$block = array_diff_key( $block, array_flip( $protected ) );
// Generate default attributes.
$attributes = array();
foreach ( acf_get_block_type_default_attributes( $block_type ) as $k => $v ) {
$attributes[ $k ] = $v['default'];
}
// Merge together arrays in order of least to most specific.
$block = array_merge( $block_type, $attributes, $block );
// Add backward compatibility attributes.
$block = acf_add_back_compat_attributes( $block );
// Return block.
return $block;
}
/**
* Add backwards compatible attribute values.
*
* @since ACF 6.0.0
*
* @param array $block The original block.
* @return array Modified block array with backwards compatibility attributes.
*/
function acf_add_back_compat_attributes( $block ) {
foreach ( acf_get_block_back_compat_attribute_key_array() as $new => $old ) {
if ( ! empty( $block[ $new ] ) || ( isset( $block[ $new ] ) && ! isset( $block[ $old ] ) ) ) {
$block[ $old ] = $block[ $new ];
}
}
return $block;
}
/**
* Get back compat new values and old values.
*
* @since ACF 6.0.0
*
* @return array back compat key array.
*/
function acf_get_block_back_compat_attribute_key_array() {
return array(
'fullHeight' => 'full_height',
'alignText' => 'align_text',
'alignContent' => 'align_content',
);
}
/**
* The render callback for all ACF blocks.
*
* @date 28/10/20
* @since ACF 5.9.2
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $wp_block The block instance (since WP 5.5).
* @return string The block HTML.
*/
function acf_render_block_callback( $attributes, $content = '', $wp_block = null ) {
$is_preview = false;
$post_id = get_the_ID();
// Set preview flag to true when rendering for the block editor.
if ( is_admin() && acf_is_block_editor() ) {
$is_preview = true;
}
// If ACF's block save method hasn't been called yet, try to initialize a default block.
if ( empty( $attributes['name'] ) && ! empty( $wp_block->name ) ) {
$attributes['name'] = $wp_block->name;
}
// Return rendered block HTML.
return acf_rendered_block( $attributes, $content, $is_preview, $post_id, $wp_block );
}
/**
* Returns the rendered block HTML.
*
* @date 28/2/19
* @since ACF 5.7.13
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param boolean $is_preview Whether or not the block is being rendered for editing preview.
* @param integer $post_id The current post being edited or viewed.
* @param WP_Block $wp_block The block instance (since WP 5.5).
* @param array $context The block context array.
* @param boolean $is_ajax_render Whether or not this is an ACF AJAX render.
* @return string The block HTML.
*/
function acf_rendered_block( $attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = false, $is_ajax_render = false ) {
$registry = WP_Block_Type_Registry::get_instance();
$wp_block_type = $registry->get_registered( $attributes['name'] );
if ( isset( $wp_block_type->acf_block_version ) && $wp_block_type->acf_block_version >= 3 ) {
$mode = 'preview';
$form = false;
} else {
$mode = isset( $attributes['mode'] ) ? $attributes['mode'] : 'auto';
$form = ( 'edit' === $mode && $is_preview );
}
// If context is available from the WP_Block class object and we have no context of our own, use that.
if ( empty( $context ) && ! empty( $wp_block->context ) ) {
$context = $wp_block->context;
}
// Check if we need to generate a block ID.
$force_new_id = false;
if ( acf_block_uses_post_meta( $attributes ) && ! empty( $attributes['id'] ) && empty( $attributes['data'] ) ) {
$force_new_id = true;
}
$attributes['id'] = acf_get_block_id( $attributes, $context, $force_new_id );
// Check if we've already got a cache of this block ID and return it to save rendering if we're in the backend.
if ( $is_preview ) {
$cached_block = acf_get_store( 'block-cache' )->get( $attributes['id'] );
if ( $cached_block ) {
if ( $form ) {
if ( $cached_block['form'] ) {
return $cached_block['html'];
}
} elseif ( ! $cached_block['form'] ) {
return $cached_block['html'];
}
}
}
ob_start();
$validation = false;
if ( $form ) {
// Load the block form since we're in edit mode.
// Set flag for post REST cleanup of media enqueue count during preloads.
acf_set_data( 'acf_did_render_block_form', true );
$block = acf_prepare_block( $attributes );
$block = acf_add_block_meta_values( $block, $post_id );
acf_setup_meta( $block['data'], $block['id'], true );
if ( ! empty( $block['validate'] ) ) {
$validation = acf_get_block_validation_state( $block, false, false, true );
}
$fields = acf_get_block_fields( $block );
if ( $fields ) {
acf_prefix_fields( $fields, "acf-{$block['id']}" );
echo '<div class="acf-block-fields acf-fields" data-block-id="' . esc_attr( $block['id'] ) . '">';
acf_render_fields( $fields, acf_ensure_block_id_prefix( $block['id'] ), 'div', 'field' );
echo '</div>';
} else {
echo acf_get_empty_block_form_html( $attributes['name'] ); //phpcs:ignore -- escaped in function.
}
} else {
if ( $is_preview ) {
acf_set_data( 'acf_doing_block_preview', true );
}
// Capture block render output.
acf_render_block( $attributes, $content, $is_preview, $post_id, $wp_block, $context );
if ( $is_preview && ! $is_ajax_render ) {
/**
* If we're in preloaded preview, we need to get the validation state for a preview too.
* Because the block render resets meta once it's finished to not pollute $post_id, we need to redo that process here.
*/
$block = acf_prepare_block( $attributes );
$block = acf_add_block_meta_values( $block, $post_id );
$block_toolbar_fields = acf_process_block_toolbar_fields( apply_filters( 'acf/blocks/top_toolbar_fields', array(), $block, $content, $is_preview, $post_id, $wp_block, $context ) );
$fields = acf_get_block_fields( $block );
acf_setup_meta( $block['data'], $block['id'], true );
if ( ! empty( $block['validate'] ) ) {
$validation = acf_get_block_validation_state( $block, false, false, true );
}
}
}
$html = ob_get_clean();
$html = is_string( $html ) ? $html : '';
// Replace <InnerBlocks /> placeholder on front-end, or if we're rendering an ACF block inside another ACF block template.
if ( ! $is_preview || doing_action( 'acf_block_render_template' ) ) {
// Escape "$" character to avoid "capture group" interpretation.
$content = str_replace( '$', '\$', $content );
// Wrap content in our acf-inner-container wrapper if necessary.
if ( $wp_block && $wp_block->block_type->acf_block_version > 1 && apply_filters( 'acf/blocks/wrap_frontend_innerblocks', true, $attributes['name'] ) ) {
// Check for a class (or className) provided in the template to become the InnerBlocks wrapper class.
$matches = array();
if ( preg_match( '/<InnerBlocks(?:[^<]+?)(?:class|className)=(?:["\']\W+\s*(?:\w+)\()?["\']([^\'"]+)[\'"]/', $html, $matches ) ) {
$class = isset( $matches[1] ) ? $matches[1] : 'acf-innerblocks-container';
} else {
$class = 'acf-innerblocks-container';
}
$content = '<div class="' . $class . '">' . $content . '</div>';
}
$html = preg_replace( '/<InnerBlocks([\S\s]*?)\/>/', $content, $html );
}
$block_cache = array(
'form' => $form,
'html' => $html,
);
if ( $is_preview && $validation ) {
// If we're in the preview, also store the validation status in the block cache.
$block_cache['validation'] = $validation;
}
if ( isset( $block_toolbar_fields ) ) {
$block_cache['blockToolbarFields'] = $block_toolbar_fields;
}
if ( isset( $fields ) ) {
$block_cache['fields'] = $fields;
}
// Store in cache for preloading if we're in the backend.
acf_get_store( 'block-cache' )->set(
$attributes['id'],
$block_cache
);
acf_set_data( 'acf_doing_block_preview', false );
// Prevent edit forms being output to rest endpoints.
if ( $form && acf_get_data( 'acf_inside_rest_call' ) && apply_filters( 'acf/blocks/prevent_edit_forms_on_rest_endpoints', true ) ) {
return '';
}
return $html;
}
/**
* Renders the block HTML.
*
* @since ACF 5.7.12
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param boolean $is_preview Whether or not the block is being rendered for editing preview.
* @param integer $post_id The current post being edited or viewed.
* @param WP_Block $wp_block The block instance (since WP 5.5).
* @param array $context The block context array.
* @return void|string
*/
function acf_render_block( $attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = false ) {
// Prepare block ensuring all settings and attributes exist.
$block = acf_prepare_block( $attributes );
if ( ! $block ) {
return '';
}
// Find post_id if not defined.
if ( ! $post_id ) {
$post_id = get_the_ID();
}
// Enqueue block type assets.
acf_enqueue_block_type_assets( $block );
$block = acf_add_block_meta_values( $block, $post_id );
// Setup postdata allowing get_field() to work.
acf_setup_meta( $block['data'], $block['id'], true );
// Call render_callback.
if ( is_callable( $block['render_callback'] ) ) {
call_user_func( $block['render_callback'], $block, $content, $is_preview, $post_id, $wp_block, $context );
// Or include template.
} elseif ( $block['render_template'] ) {
do_action( 'acf_block_render_template', $block, $content, $is_preview, $post_id, $wp_block, $context );
}
// Reset postdata.
acf_reset_meta( $block['id'] );
}
/**
* Locate and include an ACF block's template.
*
* @since ACF 6.0.4
*
* @param array $block The block props.
* @param string $content The block content.
* @param boolean $is_preview Whether this is a preview render.
* @param int $post_id The post ID this block is saved to.
* @param object $wp_block The block instance object.
* @param array $context The block context array.
*/
function acf_block_render_template( $block, $content, $is_preview, $post_id, $wp_block, $context ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
// Locate template.
if ( isset( $block['path'] ) && file_exists( $block['path'] . '/' . $block['render_template'] ) ) {
$path = $block['path'] . '/' . $block['render_template'];
} elseif ( file_exists( $block['render_template'] ) ) {
$path = $block['render_template'];
} else {
$path = locate_template( $block['render_template'] );
}
do_action( 'acf/blocks/pre_block_template_render', $block, $content, $is_preview, $post_id, $wp_block, $context );
// Include template.
if ( file_exists( $path ) ) {
if ( $is_preview && ! empty( $block['auto_inline_editing'] ) ) {
$result = apply_inline_editing_attributes_to_render_template( $path, $block, $is_preview );
// In order to allow block render templates to support any html tags,
// we must assume that escaping has already been properly handled by the block render template here.
// Typically we'd use something like wp_kses here, but that would limit the HTML tags that can be used.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $result;
} else {
include $path;
}
} elseif ( $is_preview ) {
echo acf_esc_html( apply_filters( 'acf/blocks/template_not_found_message', '<p>' . __( 'The render template for this ACF Block was not found', 'secure-custom-fields' ) . '</p>' ) );
}
do_action( 'acf/blocks/post_block_template_render', $block, $content, $is_preview, $post_id, $wp_block, $context );
}
/**
* Returns an array of all fields for the given block.
*
* @date 24/10/18
* @since ACF 5.8.0
*
* @param array $block The block props.
* @return array
*/
function acf_get_block_fields( $block ) {
$fields = array();
// We need at least a block name to check.
if ( empty( $block['name'] ) ) {
return $fields;
}
// Get field groups for this block.
$field_groups = acf_get_field_groups(
array(
'block' => $block['name'],
)
);
// Loop over results and append fields.
if ( $field_groups ) {
foreach ( $field_groups as $field_group ) {
$fields = array_merge( $fields, acf_get_fields( $field_group ) );
}
}
return $fields;
}
/**
* Enqueues and localizes block scripts and styles.
*
* @since ACF 5.7.13
*
* @return void
*/
function acf_enqueue_block_assets() {
// Localize text.
acf_localize_text(
array(
'Switch to Edit' => __( 'Switch to Edit', 'secure-custom-fields' ),
'Switch to Preview' => __( 'Switch to Preview', 'secure-custom-fields' ),
'Change content alignment' => __( 'Change content alignment', 'secure-custom-fields' ),
'Error previewing block' => __( 'An error occurred when loading the preview for this block.', 'secure-custom-fields' ),
'Error loading block form' => __( 'An error occurred when loading the block in edit mode.', 'secure-custom-fields' ),
'Edit Block' => __( 'Edit Block', 'secure-custom-fields' ),
'Open Expanded Editor' => __( 'Open Expanded Editor', 'secure-custom-fields' ),
'Error previewing block v3' => __( 'The preview for this block couldn’t be loaded. Review its content or settings for issues.', 'secure-custom-fields' ),
'ACF Block' => __( 'ACF Block', 'secure-custom-fields' ),
'Done' => __( 'Done', 'secure-custom-fields' ),
/* translators: %s: Block type title */
'%s settings' => __( '%s settings', 'secure-custom-fields' ),
)
);
// Get block types.
$block_types = array_map(
function ( $block ) {
// Render Callback may contain a incompatible class for JSON encoding. Turn it into a boolean for the frontend.
$block['render_callback'] = ! empty( $block['render_callback'] );
return $block;
},
acf_get_block_types()
);
// Localize data.
acf_localize_data(
array(
'blockTypes' => array_values( $block_types ),
'postType' => get_post_type(),
'StrictMode' => true,
)
);
// Enqueue script.
$min = defined( 'SCF_DEVELOPMENT_MODE' ) && SCF_DEVELOPMENT_MODE ? '' : '.min';
$blocks_js_path = acf_get_url( "assets/build/js/pro/acf-pro-blocks{$min}.js" );
wp_enqueue_script( 'acf-blocks', $blocks_js_path, array( 'acf-input', 'wp-blocks' ), ACF_VERSION, true );
// Enqueue block assets.
array_map( 'acf_enqueue_block_type_assets', $block_types );
// During the edit screen loading, WordPress renders all blocks in its own attempt to preload data.
// Retrieve any cached block HTML and include this in the localized data.
if ( acf_get_setting( 'preload_blocks' ) ) {
$preloaded_blocks = acf_get_store( 'block-cache' )->get_data();
acf_localize_data(
array(
'preloadedBlocks' => $preloaded_blocks,
)
);
}
}
/**
* Enqueues scripts and styles to load inside the block editor iframe.
* This allows us to do things like style contenteditable, and other inline editing elements.
*
* @since ACF 6.7
*/
function acf_enqueue_in_iframe_styles() {
if ( is_admin() ) {
$min = defined( 'ACF_DEVELOPMENT_MODE' ) && ACF_DEVELOPMENT_MODE ? '' : '.min';
$css_path = acf_get_url( "assets/build/css/pro/acf-styles-in-iframe-for-blocks{$min}.css" );
wp_enqueue_style( 'acf-inline-editing-styles', $css_path, array(), ACF_VERSION, 'all' );
}
}
add_action( 'enqueue_block_assets', 'acf_enqueue_in_iframe_styles' );
/**
* Enqueues scripts and styles for a specific block type.
*
* @since ACF 5.7.13
*
* @param array $block_type The block type settings.
* @return void
*/
function acf_enqueue_block_type_assets( $block_type ) {
// Generate handle from name.
$handle = 'block-' . acf_slugify( $block_type['name'] );
// Enqueue style.
if ( $block_type['enqueue_style'] ) {
wp_enqueue_style( $handle, $block_type['enqueue_style'], array(), ACF_VERSION, 'all' );
}
// Enqueue script.
if ( $block_type['enqueue_script'] ) {
wp_enqueue_script( $handle, $block_type['enqueue_script'], array(), ACF_VERSION, true );
}
// Enqueue assets callback.
if ( $block_type['enqueue_assets'] && is_callable( $block_type['enqueue_assets'] ) ) {
call_user_func( $block_type['enqueue_assets'], $block_type );
}
}
/**
* Handles the ajax request for block data.
*
* @since ACF 5.7.13
*
* @return void
*/
function acf_ajax_fetch_block() {
// Validate ajax request.
if ( ! acf_verify_ajax() ) {
wp_send_json_error();
}