-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathclass-registration.php
More file actions
1291 lines (1094 loc) · 42.8 KB
/
Copy pathclass-registration.php
File metadata and controls
1291 lines (1094 loc) · 42.8 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
/**
* Class for Export/Import logic.
*
* @package ThemeIsle
*/
namespace ThemeIsle\GutenbergBlocks;
use ThemeIsle\GutenbergBlocks\Main, ThemeIsle\GutenbergBlocks\Pro, ThemeIsle\GutenbergBlocks\Plugins\Stripe_API;
use ThemeIsle\GutenbergBlocks\Plugins\Dashboard;
use ThemeIsle\GutenbergBlocks\Plugins\LimitedOffers;
use ThemeIsle\GutenbergBlocks\Plugins\Template_Cloud;
use ThemeIsle\GutenbergBlocks\Server\AI_Client_Adaptor;
/**
* Class Registration.
*/
class Registration {
/**
* The main instance var.
*
* @var Registration|null
*/
public static $instance = null;
/**
* Flag to list all the blocks.
*
* @var array
*/
public static $blocks = array();
/**
* Flag to list all the blocks dependencies.
*
* @var array
*/
public static $block_dependencies = array();
/**
* The ids of the used widgets in the page.
*
* @var array<string>
*/
public static $widget_used = array(); // TODO: Monitor all the rendered widgets and enqueue the assets.
/**
* Flag to mark that the scripts which have loaded.
*
* @var array
*/
public static $scripts_loaded = array(
'circle-counter' => false,
'content-slider' => false,
'countdown' => false,
'form' => false,
'google-map' => false,
'leaflet-map' => false,
'lottie' => false,
'slider' => false,
'sticky' => false,
'tabs' => false,
'popup' => false,
'progress-bar' => false,
'accordion' => false,
'condition_hide_on' => false,
);
/**
* Flag to mark that the styles which have loaded.
*
* @var array
*/
public static $styles_loaded = array();
/**
* Flag to mark that the FA has been loaded.
*
* @var bool $is_fa_loaded Is FA loaded?
*/
public static $is_fa_loaded = false;
/**
* Get the decoded global defaults for the editor, always as an object.
*
* The option can exist as an empty string or corrupt JSON, which json_decode
* turns into null — and a null localized value crashes every block's Edit
* component when the editor reads per-block defaults from it.
*
* @return object
*/
public static function get_editor_global_defaults() {
$defaults = json_decode( get_option( 'themeisle_blocks_settings_global_defaults', '{}' ) );
return is_object( $defaults ) ? $defaults : new \stdClass();
}
/**
* Initialize the class
*/
public function init() {
add_filter( 'block_categories_all', array( $this, 'block_categories' ), 11, 2 );
add_action( 'init', array( $this, 'register_blocks' ) );
add_action( 'init', array( $this, 'init_amp_blocks' ) );
add_action( 'enqueue_block_assets', array( $this, 'enqueue_assets' ), 1 );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); // Don't change the priority or else Blocks CSS will stop working.
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) );
add_filter( 'render_block', array( $this, 'load_sticky' ), 900, 2 );
add_filter( 'render_block', array( $this, 'subscribe_fa' ), 10, 2 );
add_filter( 'dynamic_sidebar_params', array( $this, 'watch_used_widgets' ), 9999 );
add_filter( 'render_block', array( $this, 'load_condition_hide_on_styles' ), 10, 2 );
add_action(
'wp_footer',
static function () {
if ( Registration::$is_fa_loaded ) {
wp_enqueue_style( 'font-awesome-5' );
wp_enqueue_style( 'font-awesome-4-shims' );
}
},
11
);
}
/**
* Register our custom block category.
*
* @param array $categories All categories.
* @param \WP_Block_Editor_Context $block_editor_context The current block editor context.
*
* @return mixed
* @since 2.0.0
* @access public
* @link https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/#managing-block-categories
*/
public function block_categories( $categories, $block_editor_context ) {
return array_merge(
array(
array(
'slug' => 'themeisle-blocks',
'title' => __( 'Otter', 'otter-blocks' ),
),
array(
'slug' => 'themeisle-woocommerce-blocks',
'title' => __( 'WooCommerce Builder by Otter', 'otter-blocks' ),
),
),
$categories
);
}
/**
* Get block metadata from file.
*
* @param string $metadata_file Metadata file link.
*
* @return mixed
* @since 2.0.0
* @access public
*/
public function get_metadata( $metadata_file ) {
if ( ! file_exists( $metadata_file ) ) {
return false;
}
$metadata = array();
if ( function_exists( 'wpcom_vip_file_get_contents' ) ) {
$metadata = json_decode( wpcom_vip_file_get_contents( $metadata_file ), true );
} else {
$metadata = json_decode( file_get_contents( $metadata_file ), true ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
}
if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
return false;
}
return $metadata;
}
/**
* Register scripts for blocks.
*
* @since 2.0.0
* @access public
*/
public function enqueue_assets() {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/blocks.asset.php';
wp_register_style( 'font-awesome-5', OTTER_BLOCKS_URL . 'assets/fontawesome/css/all.min.css', [], $asset_file['version'] );
wp_register_style( 'font-awesome-4-shims', OTTER_BLOCKS_URL . 'assets/fontawesome/css/v4-shims.min.css', [], $asset_file['version'] );
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/leaflet-map.asset.php';
wp_register_script( 'leaflet', OTTER_BLOCKS_URL . 'assets/leaflet/leaflet.js', [], $asset_file['version'], true );
wp_script_add_data( 'leaflet', 'async', true );
wp_register_script( 'leaflet-gesture-handling', OTTER_BLOCKS_URL . 'build/blocks/leaflet-gesture-handling.js', array( 'leaflet' ), $asset_file['version'], true );
wp_script_add_data( 'leaflet-gesture-handling', 'defer', true );
wp_register_style( 'leaflet', OTTER_BLOCKS_URL . 'assets/leaflet/leaflet.css', [], $asset_file['version'] );
wp_style_add_data( 'leaflet', 'path', OTTER_BLOCKS_PATH . '/assets/leaflet/leaflet.css' );
wp_register_style( 'leaflet-gesture-handling', OTTER_BLOCKS_URL . 'assets/leaflet/leaflet-gesture-handling.min.css', [], $asset_file['version'] );
wp_style_add_data( 'leaflet-gesture-handling', 'path', OTTER_BLOCKS_PATH . '/assets/leaflet/leaflet-gesture-handling.min.css' );
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/lottie.asset.php';
wp_register_script( 'lottie-player', OTTER_BLOCKS_URL . 'assets/lottie/lottie-player.min.js', [], $asset_file['version'], true );
wp_script_add_data( 'lottie-player', 'async', true );
wp_register_script( 'dotlottie-player', OTTER_BLOCKS_URL . 'assets/lottie/dotlottie-player.min.js', [], $asset_file['version'], true );
wp_script_add_data( 'dotlottie-player', 'async', true );
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/slider.asset.php';
wp_register_script( 'glidejs', OTTER_BLOCKS_URL . 'assets/glide/glide.min.js', [], $asset_file['version'], true );
wp_script_add_data( 'glidejs', 'async', true );
wp_register_style( 'glidejs-core', OTTER_BLOCKS_URL . 'assets/glide/glide.core.min.css', [], $asset_file['version'] );
wp_style_add_data( 'glidejs-core', 'path', OTTER_BLOCKS_PATH . '/assets/glide/glide.core.min.css' );
wp_register_style( 'glidejs-theme', OTTER_BLOCKS_URL . 'assets/glide/glide.theme.min.css', [], $asset_file['version'] );
wp_style_add_data( 'glidejs-theme', 'path', OTTER_BLOCKS_PATH . '/assets/glide/glide.theme.min.css' );
}
/**
* Load Gutenberg blocks.
*
* @since 2.0.0
* @access public
*/
public function enqueue_block_editor_assets() {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/blocks.asset.php';
$current_screen = get_current_screen();
if ( 'widgets' === $current_screen->base ) {
if ( in_array( 'wp-edit-post', $asset_file['dependencies'] ) ) {
unset( $asset_file['dependencies'][ array_search( 'wp-editor', $asset_file['dependencies'] ) ] );
unset( $asset_file['dependencies'][ array_search( 'wp-edit-post', $asset_file['dependencies'] ) ] );
}
if ( class_exists( 'WooCommerce' ) ) {
array_push( $asset_file['dependencies'], 'wc-blocks-data-store', 'wc-price-format' );
}
}
wp_register_script( 'otter-vendor', OTTER_BLOCKS_URL . 'build/blocks/vendor.js', array( 'react', 'react-dom' ), $asset_file['version'], true );
wp_enqueue_script(
'otter-blocks',
OTTER_BLOCKS_URL . 'build/blocks/blocks.js',
array_merge(
$asset_file['dependencies'],
array( 'otter-vendor', 'glidejs', 'lottie-player', 'dotlottie-player' )
),
$asset_file['version'],
true
);
wp_set_script_translations( 'otter-blocks', 'otter-blocks' );
// Separate bundle: loads only when the Patterns Library module is on.
if ( boolval( get_option( 'themeisle_blocks_settings_patterns_library', true ) ) ) {
$patterns_asset = include OTTER_BLOCKS_PATH . '/build/patterns-library/index.asset.php';
wp_enqueue_script(
'otter-patterns-library',
OTTER_BLOCKS_URL . 'build/patterns-library/index.js',
array_merge( $patterns_asset['dependencies'], array( 'otter-blocks' ) ),
$patterns_asset['version'],
true
);
wp_set_script_translations( 'otter-patterns-library', 'otter-blocks' );
if ( file_exists( OTTER_BLOCKS_PATH . '/build/patterns-library/index.css' ) ) {
wp_enqueue_style(
'otter-patterns-library',
OTTER_BLOCKS_URL . 'build/patterns-library/index.css',
array(),
$patterns_asset['version']
);
}
}
if ( defined( 'THEMEISLE_GUTENBERG_GOOGLE_MAPS_API' ) ) {
$api = THEMEISLE_GUTENBERG_GOOGLE_MAPS_API;
} else {
$api = false;
}
global $wp_roles;
$is_wp_ai_backend = AI_Client_Adaptor::BACKEND_WP === AI_Client_Adaptor::resolve_backend();
$can_track = 'yes' === get_option( 'otter_blocks_logger_flag', false );
$themeisle_gutenberg = array(
'hasNeve' => defined( 'NEVE_VERSION' ),
'hasPro' => Pro::is_pro_installed(),
'isProActive' => Pro::is_pro_active(),
'upgradeLink' => tsdk_translate_link( tsdk_utmify( Pro::get_url(), 'editor', Pro::get_reference() ) ),
'patternsLink' => tsdk_translate_link( tsdk_utmify( Pro::get_patterns_url(), 'editor', Pro::get_reference() ) ),
'should_show_upsell' => Pro::should_show_upsell(),
'assetsPath' => OTTER_BLOCKS_URL . 'assets',
'updatePath' => admin_url( 'update-core.php' ),
'optionsPath' => admin_url( 'admin.php?page=otter' ),
'mapsAPI' => $api,
'hasStripeAPI' => Stripe_API::has_keys(),
'globalDefaults' => self::get_editor_global_defaults(),
'themeDefaults' => Main::get_global_defaults(),
'imageSizes' => function_exists( 'is_wpcom_vip' ) ? array( 'thumbnail', 'medium', 'medium_large', 'large' ) : get_intermediate_image_sizes(), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_intermediate_image_sizes_get_intermediate_image_sizes
'isWPVIP' => function_exists( 'is_wpcom_vip' ),
'canTrack' => $can_track ? true : false,
'userRoles' => $wp_roles->roles,
'isBlockEditor' => 'post' === $current_screen->base,
'postTypes' => get_post_types(
[
'public' => true,
'exclude_from_search' => false,
]
),
'rootUrl' => get_site_url(),
'restRoot' => get_rest_url( null, 'otter/v1' ),
'isPrettyPermalinks' => boolval( get_option( 'permalink_structure' ) ),
'showOnboarding' => $this->show_onboarding(),
'ratingScale' => get_option( 'themeisle_blocks_settings_review_scale', false ),
'hasModule' => array(
'aiToolbar' => boolval( get_option( 'themeisle_blocks_settings_block_ai_toolbar_module', true ) ),
'blockCSS' => boolval( get_option( 'themeisle_blocks_settings_css_module', true ) ),
'blockAnimations' => boolval( get_option( 'themeisle_blocks_settings_blocks_animation', true ) ),
'blockConditions' => boolval( get_option( 'themeisle_blocks_settings_block_conditions', true ) ),
'patternsLibrary' => boolval( get_option( 'themeisle_blocks_settings_patterns_library', true ) ),
'dynamicContent' => boolval( get_option( 'themeisle_blocks_settings_dynamic_content', true ) ),
),
'isLegacyPre59' => version_compare( get_bloginfo( 'version' ), '5.8.22', '<=' ),
'isAncestorTypeAvailable' => version_compare( get_bloginfo( 'version' ), '5.9.22', '>=' ),
'version' => OTTER_BLOCKS_VERSION,
'isRTL' => is_rtl(),
'highlightDynamicText' => get_option( 'themeisle_blocks_settings_highlight_dynamic', true ),
'hasOpenAiKey' => $is_wp_ai_backend || ! empty( get_option( 'themeisle_open_ai_api_key' ) ),
'aiClientActive' => $is_wp_ai_backend,
'aiClientSupported' => function_exists( 'wp_ai_client_prompt' ),
'hasAIProvider' => AI_Client_Adaptor::is_available(),
'connectorsUrl' => esc_url( admin_url( 'options-connectors.php' ) ),
'hasPatternSources' => Template_Cloud::has_used_pattern_sources(),
'proPatterns' => boolval( get_option( 'themeisle_blocks_settings_patterns_library', true ) ) ? Patterns::get_upsell_patterns() : array(),
);
if ( $can_track ) {
$themeisle_gutenberg['telemetry'] = array(
'loggerData' => get_option(
'otter_blocks_logger_data',
array(
'blocks' => array(),
'templates' => array(),
)
),
'firstSaveDone' => (bool) get_option( 'otter_activation_first_save', false ),
);
}
wp_localize_script(
'otter-blocks',
'themeisleGutenberg',
$themeisle_gutenberg
);
add_filter( 'themeisle-sdk/survey/' . OTTER_PRODUCT_SLUG, array( Dashboard::class, 'get_survey_metadata' ), 10, 2 );
do_action( 'themeisle_internal_page', OTTER_PRODUCT_SLUG, 'editor' );
}
/**
* Whether to show onboarding or not.
*
* @since 2.0.13
* @access public
*/
public function show_onboarding() {
$onboarding_option = get_option( 'themeisle_blocks_settings_onboarding', true );
$installed_thru_sdk = get_option( 'themeisle_sdk_promotions_otter_installed', false );
$otter_blocks_install = get_option( 'otter_blocks_install' );
if ( defined( 'ENABLE_OTTER_PRO_DEV' ) ) {
return false;
}
if ( ! $onboarding_option ) {
return false;
}
if ( $installed_thru_sdk && $otter_blocks_install > strtotime( '-2 days' ) ) {
return false;
}
return true;
}
/**
* Load frontend assets for our blocks.
*
* @since 2.0.0
* @access public
*/
public function enqueue_block_assets() {
global $wp_query, $wp_registered_sidebars;
if ( is_admin() ) {
// In the editor (including the iframed canvas) enqueue the editor
// styles on `enqueue_block_assets` so WordPress loads them into the
// iframe natively. Enqueuing on `enqueue_block_editor_assets` would
// load them only in the parent document, and WordPress 6.9+ warns
// when it copies such styles into the iframe.
//
// Scripts enqueued here would also be injected into the iframe
// natively (via `_wp_get_iframed_editor_assets()`), but the iframe
// assets are resolved once on editor load, with no way to add them
// later. Heavy per-block scripts (Leaflet, Lottie, Glide) are
// therefore NOT enqueued here — they would load in every editor
// session regardless of the blocks used. Instead they are copied
// into the iframe on demand by `copyScriptAssetToIframe()` in
// `src/blocks/helpers/block-utility.js`, which keeps them lazy at
// the cost of client-side readiness tracking.
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/blocks.asset.php';
wp_enqueue_style( 'otter-editor', OTTER_BLOCKS_URL . 'build/blocks/editor.css', array( 'wp-edit-blocks', 'font-awesome-5', 'font-awesome-4-shims' ), $asset_file['version'] );
return;
}
if ( is_singular() ) {
$this->enqueue_dependencies();
} elseif ( ! is_null( $wp_query->posts ) && 0 < count( $wp_query->posts ) ) {
$posts = wp_list_pluck( $wp_query->posts, 'ID' );
foreach ( $posts as $post ) {
$this->enqueue_dependencies( $post );
}
}
add_filter(
'the_content',
function ( $content ) {
$this->enqueue_dependencies();
return $content;
}
);
$has_widgets = false;
foreach ( $wp_registered_sidebars as $key => $sidebar ) {
if ( is_active_sidebar( $key ) ) {
$has_widgets = true;
break;
}
}
if ( $has_widgets ) {
add_filter(
'wp_footer',
function ( $content ) {
$this->enqueue_dependencies( 'widgets' );
return $content;
}
);
}
if ( function_exists( 'get_block_templates' ) && ( current_theme_supports( 'block-templates' ) || current_theme_supports( 'block-template-parts' ) ) ) {
$this->enqueue_dependencies( 'block-templates' );
}
}
/**
* Handler which checks the blocks used and enqueue the assets which needs.
*
* @since 2.0.0
* @param string|int|null $post Current post.
* @access public
*/
public function enqueue_dependencies( $post = null ) {
$content = '';
if ( 'widgets' === $post ) {
$post = self::get_active_widgets_content();
} elseif ( 'block-templates' === $post ) {
global $_wp_current_template_content;
$content = '';
$slugs = array();
// If we have template content (full block templates), extract template part slugs.
if ( ! empty( $_wp_current_template_content ) ) {
$template_blocks = parse_blocks( $_wp_current_template_content );
foreach ( $template_blocks as $template_block ) {
if ( 'core/template-part' === $template_block['blockName'] && isset( $template_block['attrs']['slug'] ) ) {
$slugs[] = $template_block['attrs']['slug'];
}
}
// Get the specific template parts referenced in the template.
$templates_parts = get_block_templates( array( 'slug__in' => $slugs ), 'wp_template_part' );
foreach ( $templates_parts as $templates_part ) {
if ( ! empty( $templates_part->content ) && ! empty( $templates_part->slug ) && in_array( $templates_part->slug, $slugs ) ) {
$content .= $templates_part->content;
}
}
$content .= $_wp_current_template_content;
} else {
// Fallback for classic themes with block-template-parts only.
// Get all template parts since we can't determine which ones are used.
$templates_parts = get_block_templates( array(), 'wp_template_part' );
foreach ( $templates_parts as $templates_part ) {
if ( ! empty( $templates_part->content ) ) {
$content .= $templates_part->content;
}
}
}
$post = $content;
} else {
$content = get_the_content( null, false, $post );
}
$this->enqueue_block_styles( $post );
if ( has_block( 'core/block', $post ) ) {
$blocks = parse_blocks( $content );
$blocks = array_filter(
$blocks,
function ( $block ) {
return 'core/block' === $block['blockName'] && isset( $block['attrs']['ref'] );
}
);
foreach ( $blocks as $block ) {
$this->enqueue_dependencies( $block['attrs']['ref'] );
}
}
if ( ( did_action( 'parse_request' ) && function_exists( 'amp_is_request' ) && amp_is_request() ) || is_admin() ) {
return;
}
if ( ! self::$scripts_loaded['circle-counter'] && has_block( 'themeisle-blocks/circle-counter', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/circle-counter.asset.php';
wp_register_script( 'otter-circle-counter', OTTER_BLOCKS_URL . 'build/blocks/circle-counter.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-circle-counter', 'defer', true );
}
if ( ! self::$scripts_loaded['countdown'] && has_block( 'themeisle-blocks/countdown', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/countdown.asset.php';
wp_register_script( 'otter-countdown', OTTER_BLOCKS_URL . 'build/blocks/countdown.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-countdown', 'defer', true );
$offset = (float) get_option( 'gmt_offset' );
$hours = (int) $offset;
$minutes = ( $offset - $hours );
$sign = ( $offset < 0 ) ? '-' : '+';
$abs_hour = abs( $hours );
$abs_mins = abs( $minutes * 60 );
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
wp_localize_script(
'otter-countdown',
'themeisleGutenbergCountdown',
array(
'i18n' => array(
'second' => __( 'Second', 'otter-blocks' ),
'seconds' => __( 'Seconds', 'otter-blocks' ),
'minute' => __( 'Minute', 'otter-blocks' ),
'minutes' => __( 'Minutes', 'otter-blocks' ),
'hour' => __( 'Hour', 'otter-blocks' ),
'hours' => __( 'Hours', 'otter-blocks' ),
'day' => __( 'Day', 'otter-blocks' ),
'days' => __( 'Days', 'otter-blocks' ),
),
'timezone' => $tz_offset,
)
);
add_action(
'wp_head',
function () {
echo '
<style type="text/css" data-source="otter-blocks">
[class*="o-countdown-trigger-on-end-"] {
transition: opacity 1s ease;
}
[class*="o-countdown-trigger-on-end-"].o-cntdn-bhv-show, [class*="o-countdown-trigger-on-end-"].o-cntdn-bhv-hide:not(.o-cntdn-ready), [class*="o-countdown-trigger-on-end-"].o-cntdn-bhv-hide.o-cntdn-hide, [data-intv-start]:not(.o-cntdn-ready) {
height: 0px !important;
max-height: 0px !important;
min-height: 0px !important;
visibility: hidden;
box-sizing: border-box;
margin: 0px !important;
padding: 0px !important;
opacity: 0;
}
.wp-block-themeisle-blocks-countdown:not(.o-cntdn-ready) {
visibility: hidden;
}
[class*="o-countdown-trigger-on-end-"].o-cntdn-bhv-show {
opacity: 0;
}
</style>
';
}
);
}
if ( ! self::$scripts_loaded['form'] && has_block( 'themeisle-blocks/form', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/form.asset.php';
wp_register_script( 'otter-form', OTTER_BLOCKS_URL . 'build/blocks/form.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-form', 'defer', true );
wp_localize_script(
'otter-form',
'themeisleGutenbergForm',
array(
'reRecaptchaSitekey' => get_option( 'themeisle_google_captcha_api_site_key' ),
'reRecaptchaAPIURL' => apply_filters( 'otter_blocks_recaptcha_api_url', 'https://www.google.com/recaptcha/api.js' ),
'turnstileSitekey' => get_option( 'themeisle_cloudflare_turnstile_site_key' ),
'turnstileAPIURL' => apply_filters( 'otter_blocks_turnstile_api_url', 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit' ),
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'messages' => array(
'submission' => __( 'Form submission from', 'otter-blocks' ),
'captcha-not-loaded' => __( 'Captcha is not loaded. Please check your browser plugins to allow it.', 'otter-blocks' ),
'check-captcha' => __( 'Please check the captcha.', 'otter-blocks' ),
'invalid-email' => __( 'The email address is invalid!', 'otter-blocks' ),
'already-registered' => __( 'The email was already registered!', 'otter-blocks' ),
'try-again' => __( 'Error. Something is wrong with the server! Try again later.', 'otter-blocks' ),
'privacy' => __( 'I have read and agree to the privacy statement.', 'otter-blocks' ),
'too-many-files' => __( 'Too many files loaded. Maximum is: ', 'otter-blocks' ),
'big-file' => __( 'File size is to big. The limit is: ', 'otter-blocks' ),
'invalid-file' => __( 'Invalid files type. The submitted files could not be processed.', 'otter-blocks' ),
'confirmingSubmission' => __( 'Confirming submission', 'otter-blocks' ),
),
)
);
}
if ( ! self::$scripts_loaded['google-map'] && has_block( 'themeisle-blocks/google-map', $post ) ) {
$apikey = get_option( 'themeisle_google_map_block_api_key' );
// Don't output anything if there is no API key.
if ( null !== $apikey && ! empty( $apikey ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/maps.asset.php';
wp_register_script( 'otter-google-map', OTTER_BLOCKS_URL . 'build/blocks/maps.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-google-map', 'defer', true );
wp_register_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=' . esc_attr( $apikey ) . '&libraries=places&callback=initMapScript', array( 'otter-google-map' ), '', true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_script_add_data( 'google-maps', 'defer', true );
}
}
if ( ! self::$scripts_loaded['leaflet-map'] && has_block( 'themeisle-blocks/leaflet-map', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/leaflet-map.asset.php';
wp_register_script(
'otter-leaflet',
OTTER_BLOCKS_URL . 'build/blocks/leaflet-map.js',
array_merge(
$asset_file['dependencies'],
array( 'leaflet', 'leaflet-gesture-handling' )
),
$asset_file['version'],
true
);
wp_script_add_data( 'otter-leaflet', 'defer', true );
}
if ( ! self::$scripts_loaded['lottie'] && has_block( 'themeisle-blocks/lottie', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/lottie.asset.php';
wp_register_script( 'lottie-interactivity', OTTER_BLOCKS_URL . 'assets/lottie/lottie-interactivity.min.js', array( 'lottie-player', 'dotlottie-player' ), $asset_file['version'], true );
wp_script_add_data( 'lottie-interactivity', 'async', true );
wp_register_script(
'otter-lottie',
OTTER_BLOCKS_URL . 'build/blocks/lottie.js',
array_merge(
$asset_file['dependencies'],
array( 'lottie-player', 'dotlottie-player', 'lottie-interactivity' )
),
$asset_file['version'],
true
);
wp_script_add_data( 'otter-lottie', 'defer', true );
}
if ( ! self::$scripts_loaded['slider'] && has_block( 'themeisle-blocks/slider', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/slider.asset.php';
wp_register_script(
'otter-slider',
OTTER_BLOCKS_URL . 'build/blocks/slider.js',
array_merge(
$asset_file['dependencies'],
array( 'glidejs' )
),
$asset_file['version'],
true
);
wp_script_add_data( 'otter-slider', 'async', true );
wp_localize_script(
'otter-slider',
'themeisleGutenbergSlider',
array(
'isRTL' => is_rtl(),
'next' => __( 'Next Slide', 'otter-blocks' ),
'prev' => __( 'Previous Slide', 'otter-blocks' ),
)
);
}
if ( ! self::$scripts_loaded['content-slider'] && has_block( 'themeisle-blocks/content-slider', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/content-slider.asset.php';
wp_register_script(
'otter-content-slider',
OTTER_BLOCKS_URL . 'build/blocks/content-slider.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
wp_script_add_data( 'otter-content-slider', 'defer', true );
wp_localize_script(
'otter-content-slider',
'themeisleGutenbergContentSlider',
array(
/* translators: %d: slide number. */
'goToSlide' => __( 'Go to slide %d', 'otter-blocks' ),
)
);
}
if ( ! self::$scripts_loaded['tabs'] && has_block( 'themeisle-blocks/tabs', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/tabs.asset.php';
wp_register_script( 'otter-tabs', OTTER_BLOCKS_URL . 'build/blocks/tabs.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-tabs', 'defer', true );
}
if (
! self::$scripts_loaded['popup'] &&
( has_block( 'themeisle-blocks/popup', $post ) || has_block( 'themeisle-blocks/modal', $post ) )
) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/popup.asset.php';
wp_register_script( 'otter-popup', OTTER_BLOCKS_URL . 'build/blocks/popup.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-popup', 'defer', true );
wp_localize_script(
'otter-popup',
'themeisleGutenberg',
array(
'isPreview' => is_preview(),
)
);
}
if ( ! self::$scripts_loaded['progress-bar'] && has_block( 'themeisle-blocks/progress-bar', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/progress-bar.asset.php';
wp_register_script( 'otter-progress-bar', OTTER_BLOCKS_URL . 'build/blocks/progress-bar.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-progress-bar', 'defer', true );
}
if ( ! self::$scripts_loaded['accordion'] && has_block( 'themeisle-blocks/accordion', $post ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/accordion.asset.php';
wp_register_script( 'otter-accordion', OTTER_BLOCKS_URL . 'build/blocks/accordion.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_script_add_data( 'otter-accordion', 'defer', true );
}
}
/**
* Enqueue block styles.
*
* @since 2.0.0
* @param null $post Current post.
* @access public
*/
public function enqueue_block_styles( $post ) {
$asset_file = null;
foreach ( self::$blocks as $block ) {
if ( in_array( $block, self::$styles_loaded ) || ! has_block( 'themeisle-blocks/' . $block, $post ) ) {
continue;
}
// Shared styles.
if ( 'modal' === $block ) {
$block = 'popup';
}
$block_path = OTTER_BLOCKS_PATH . '/build/blocks/' . $block;
$style = OTTER_BLOCKS_URL . 'build/blocks/' . $block . '/style.css';
if ( ! file_exists( $block_path ) && defined( 'OTTER_PRO_BUILD_PATH' ) ) {
$block_path = OTTER_PRO_BUILD_PATH . $block;
$style = OTTER_PRO_BUILD_URL . $block . '/style.css';
}
$metadata_file = trailingslashit( $block_path ) . 'block.json';
$style_path = trailingslashit( $block_path ) . 'style.css';
$metadata = $this->get_metadata( $metadata_file );
if ( false === $metadata ) {
continue;
}
// Read the shared asset file once, only when a matching block exists.
if ( null === $asset_file ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/blocks.asset.php';
}
$deps = array();
if ( isset( self::$block_dependencies[ $block ] ) ) {
$deps = self::$block_dependencies[ $block ];
}
if ( file_exists( $style_path ) && ! empty( $metadata['style'] ) ) {
wp_register_style(
$metadata['style'],
$style,
$deps,
$asset_file['version']
);
wp_style_add_data( $metadata['style'], 'path', $style_path );
}
array_push( self::$styles_loaded, $block );
}
}
/**
* Blocks Registration.
*
* @since 2.0.0
* @access public
*/
public function register_blocks() {
$dynamic_blocks = array(
'about-author' => '\ThemeIsle\GutenbergBlocks\Render\About_Author_Block',
'form-captcha' => '\ThemeIsle\GutenbergBlocks\Render\Form_Captcha_Block',
'form-nonce' => '\ThemeIsle\GutenbergBlocks\Render\Form_Nonce_Block',
'google-map' => '\ThemeIsle\GutenbergBlocks\Render\Google_Map_Block',
'leaflet-map' => '\ThemeIsle\GutenbergBlocks\Render\Leaflet_Map_Block',
'plugin-cards' => '\ThemeIsle\GutenbergBlocks\Render\Plugin_Card_Block',
'posts-grid' => '\ThemeIsle\GutenbergBlocks\Render\Posts_Grid_Block',
'review' => '\ThemeIsle\GutenbergBlocks\Render\Review_Block',
'sharing-icons' => '\ThemeIsle\GutenbergBlocks\Render\Sharing_Icons_Block',
'stripe-checkout' => '\ThemeIsle\GutenbergBlocks\Render\Stripe_Checkout_Block',
'form-multiple-choice' => '\ThemeIsle\GutenbergBlocks\Render\Form_Multiple_Choice_Block',
);
$dynamic_blocks = apply_filters( 'otter_blocks_register_dynamic_blocks', $dynamic_blocks );
self::$blocks = array(
'about-author',
'accordion',
'accordion-item',
'advanced-column',
'advanced-columns',
'advanced-heading',
'button',
'button-group',
'circle-counter',
'content-slider',
'countdown',
'flip',
'font-awesome-icons',
'form',
'form-captcha',
'form-input',
'form-nonce',
'form-textarea',
'form-multiple-choice',
'google-map',
'icon-list',
'icon-list-item',
'leaflet-map',
'lottie',
'plugin-cards',
'popup',
'modal',
'posts-grid',
'pricing',
'progress-bar',
'review',
'service',
'sharing-icons',
'slider',
'stripe-checkout',
'tabs',
'tabs-item',
'testimonials',
'timeline',
'timeline-item',
);
self::$blocks = apply_filters( 'otter_blocks_register_blocks', self::$blocks );
$this->enqueue_assets();
self::$block_dependencies = array(
'leaflet-map' => array( 'leaflet', 'leaflet-gesture-handling' ),
'slider' => array( 'glidejs-core', 'glidejs-theme' ),
);
$local_dependencies = array_merge(
self::$block_dependencies,
array(
'button-group' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'font-awesome-icons' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'icon-list-item' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'plugin-cards' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'timeline-item' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
)
);
// Shared asset file (version/deps) for all blocks — read once, not per block.
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/blocks.asset.php';
foreach ( self::$blocks as $block ) {
$block_path = OTTER_BLOCKS_PATH . '/build/blocks/' . $block;
$editor_style = OTTER_BLOCKS_URL . 'build/blocks/' . $block . '/editor.css';
if ( ! file_exists( $block_path ) && defined( 'OTTER_PRO_BUILD_PATH' ) ) {
$block_path = OTTER_PRO_BUILD_PATH . $block;
$editor_style = OTTER_PRO_BUILD_URL . $block . '/editor.css';
}
$metadata_file = trailingslashit( $block_path ) . 'block.json';
$editor_style_path = trailingslashit( $block_path ) . 'editor.css';
$metadata = $this->get_metadata( $metadata_file );
if ( false === $metadata ) {
continue;
}
$deps = array();
if ( isset( $local_dependencies[ $block ] ) ) {
$deps = $local_dependencies[ $block ];
}
if ( file_exists( $editor_style_path ) && ! empty( $metadata['editorStyle'] ) ) {
wp_register_style(
$metadata['editorStyle'],
$editor_style,
$deps,
$asset_file['version']
);
}
if ( isset( $dynamic_blocks[ $block ] ) ) {
$classname = $dynamic_blocks[ $block ];
$renderer = new $classname();
if ( method_exists( $renderer, 'render' ) ) {
register_block_type_from_metadata(
$metadata_file,
array(
'render_callback' => array( $renderer, 'render' ),
)
);
continue;
}
}
register_block_type_from_metadata( $metadata_file );
}
}
/**
* Initialize AMP blocks.
*
* @since 1.0.0
* @access public
*/
public function init_amp_blocks() {
$classnames = array(
'\ThemeIsle\GutenbergBlocks\Render\AMP\Circle_Counter_Block',
'\ThemeIsle\GutenbergBlocks\Render\AMP\Lottie_Block',