Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
707 lines (619 loc) · 21.9 KB
/
Copy pathfunctions.php
File metadata and controls
707 lines (619 loc) · 21.9 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
<?php
/**
* Extendable functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package Extendable
* @since Extendable 1.0
*/
if ( ! defined( 'EXTENDABLE_THEME_VERSION' ) ) {
$extendable_theme_version = wp_get_theme()->get( 'Version' );
define( 'EXTENDABLE_THEME_VERSION', is_string( $extendable_theme_version ) ? $extendable_theme_version : '1.0.0' );
}
if ( ! function_exists( 'extendable_support' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* @since Extendable 1.0
*
* @return void
*/
function extendable_support() {
// Add support for block styles.
add_theme_support( 'wp-block-styles' );
global $wp_version;
// Add style for WordPress older versions.
if ( version_compare( $wp_version, '6.0.2', '<=' ) ) {
$editor_style = array(
'style.css',
'/assets/css/deprecate-style.css',
);
} else {
$editor_style = 'style.css';
}
$editor_style = array_merge( (array) $editor_style, array( 'assets/css/inverse.css' ) );
if ( class_exists( 'WooCommerce' ) ) {
$editor_style = array_merge( (array) $editor_style, array( 'assets/css/woocommerce.css' ) );
}
// Enqueue editor styles.
add_editor_style( $editor_style );
}
endif;
add_action( 'after_setup_theme', 'extendable_support' );
if ( ! function_exists( 'extendable_styles' ) ) :
/**
* Enqueue styles.
*
* @since Extendable 1.0
*
* @return void
*/
function extendable_styles() {
// Register theme stylesheet.
wp_register_style(
'extendable-style',
get_template_directory_uri() . '/style.css',
array(),
EXTENDABLE_THEME_VERSION
);
// Enqueue theme stylesheet.
wp_enqueue_style( 'extendable-style' );
wp_enqueue_style(
'extendable-inverse-style',
get_template_directory_uri() . '/assets/css/inverse.css',
array( 'extendable-style' ),
EXTENDABLE_THEME_VERSION
);
if ( class_exists( 'WooCommerce' ) ) {
wp_enqueue_style(
'extendable-woocommerce-style',
get_template_directory_uri() . '/assets/css/woocommerce.css',
array( 'extendable-style' ),
EXTENDABLE_THEME_VERSION
);
}
global $wp_version;
if ( version_compare( $wp_version, '6.0.2', '<=' ) ) {
// Register deprecate stylesheet.
wp_register_style(
'extendable-deprecate-style',
get_template_directory_uri() . '/assets/css/deprecate-style.css',
array(),
EXTENDABLE_THEME_VERSION
);
// Enqueue deprecate stylesheet.
wp_enqueue_style( 'extendable-deprecate-style' );
}
}
endif;
add_action( 'wp_enqueue_scripts', 'extendable_styles' );
/**
* Enqueue block-specific styles.
*
* @since Extendable 2.0.11
*
* @return void
*/
function extendable_enqueue_block_styles() {
// Check for specific blocks and enqueue their styles.
if ( has_block( 'contact-form-7/contact-form-selector' ) ) {
wp_enqueue_style(
'extendable-contact-form-7-style',
get_template_directory_uri() . '/assets/css/contact-form-7.css',
array(),
EXTENDABLE_THEME_VERSION
);
}
if ( has_block( 'wpforms/form-selector' ) ) {
wp_enqueue_style(
'extendable-wpforms-style',
get_template_directory_uri() . '/assets/css/wpforms.css',
array(),
EXTENDABLE_THEME_VERSION
);
}
}
add_action( 'enqueue_block_assets', 'extendable_enqueue_block_styles' );
/**
* Registers pattern categories.
*
* @since Extendable 1.0
*
* @return void
*/
function extendable_register_pattern_categories() {
$block_pattern_categories = array(
'header' => array( 'label' => __( 'Headers', 'extendable' ) ),
'footer' => array( 'label' => __( 'Footers', 'extendable' ) ),
);
/**
* Filters the theme block pattern categories.
*
* @since Extendable 1.0
*
* @param array[] $block_pattern_categories {
* An associative array of block pattern categories, keyed by category name.
*
* @type array[] $properties {
* An array of block category properties.
*
* @type string $label A human-readable label for the pattern category.
* }
* }
*/
$block_pattern_categories = apply_filters( 'extendable_block_pattern_categories', $block_pattern_categories );
foreach ( $block_pattern_categories as $name => $properties ) {
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) {
register_block_pattern_category( $name, $properties );
}
}
}
add_action( 'init', 'extendable_register_pattern_categories', 9 );
/**
* Hide the system block styles from the style picker. Patterns and design styles set them;
* WordPress still registers them, so their CSS prints.
*
* @since Extendable 2.1.10
*
* @return void
*/
function extendable_hide_block_presets() {
wp_enqueue_style(
'extendable-hide-block-presets',
get_template_directory_uri() . '/assets/css/hide-block-presets.css',
array(),
EXTENDABLE_THEME_VERSION
);
}
add_action( 'enqueue_block_editor_assets', 'extendable_hide_block_presets' );
/**
* Turn off the WordPress emoji script, styles and image swaps. Browsers draw emoji
* themselves; the fallback loads emoji images from wordpress.org without consent.
*
* @since Extendable 2.1.10
*
* @return void
*/
function extendable_disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'embed_head', 'print_emoji_detection_script' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
}
add_action( 'init', 'extendable_disable_emojis' );
// The admin emoji hooks are added after init.
add_action( 'admin_init', 'extendable_disable_emojis' );
/**
* Include animation functionality
*/
require_once get_template_directory() . '/inc/animations.php';
/**
* Plugins load before the theme, so Masteriyo's class is already known here.
*/
if ( class_exists( 'Masteriyo\Masteriyo' ) ) {
require_once get_template_directory() . '/inc/masteriyo.php';
}
/**
* Add primary-foreground duotone to extendify demo Site Logo block.
*
* @param array $parsed_block Parsed block data.
* @return array Filtered block data.
*/
function extendable_add_duotone_to_extendify_demo_site_logo( array $parsed_block ): array {
if ( 'core/site-logo' !== $parsed_block['blockName'] ) {
return $parsed_block;
}
$logo_url = $parsed_block['attrs']['url'] ?? '';
if ( '' === $logo_url ) {
$logo_id = (int) get_theme_mod( 'custom_logo' );
$logo_url = $logo_id ? wp_get_attachment_url( $logo_id ) : '';
}
if ( '' === $logo_url ) {
return $parsed_block;
}
$logo_file = wp_basename( $logo_url );
$allowed_prefixes = array( 'extendify-demo-', 'ext-custom-logo-' );
$matches = false;
foreach ( $allowed_prefixes as $prefix ) {
if ( function_exists( 'str_starts_with' ) ) {
$matches = str_starts_with( $logo_file, $prefix );
} else {
$matches = 0 === strpos( $logo_file, $prefix );
}
if ( $matches ) {
break;
}
}
if ( ! $matches ) {
return $parsed_block;
}
$parsed_block['attrs']['style']['color']['duotone'] =
'var:preset|duotone|primary-foreground';
return $parsed_block;
}
add_filter( 'render_block_data', 'extendable_add_duotone_to_extendify_demo_site_logo', 10 );
/**
* Exclude WooCommerce Templates from the Block Editor When WooCommerce Is Inactive
*
* @package Extendable
* @since Extendable 2.0.21
*
* @param WP_Block_Template[] $templates The block templates found.
* @return WP_Block_Template[]
*/
function extendable_exclude_wc_block_templates( $templates ) {
if ( ! class_exists( 'WooCommerce' ) ) {
$wc_template_slugs = array(
'archive-product',
'single-product',
'product-search-results',
'page-cart',
'page-checkout',
'order-confirmation',
'checkout',
);
foreach ( $templates as $key => $template ) {
if ( isset( $template->slug ) && in_array( $template->slug, $wc_template_slugs, true ) ) {
unset( $templates[ $key ] );
}
}
}
return $templates;
}
add_filter( 'get_block_templates', 'extendable_exclude_wc_block_templates' );
/**
* WooCommerce prints upsells after product-details, which sits in the buy box; the template has its own row.
*
* @package Extendable
*/
function extendable_remove_wc_upsells_hook() {
if ( function_exists( 'is_product' ) && is_product() ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
}
}
add_action( 'template_redirect', 'extendable_remove_wc_upsells_hook' );
/**
* Header helper.
*
* @package Extendable
* @since Extendable 2.1.5
*/
function extendable_enqueue_header_scripts() {
wp_enqueue_script(
'extendable-header',
get_template_directory_uri() . '/assets/js/header.js',
array(),
EXTENDABLE_THEME_VERSION,
true
);
}
add_action( 'wp_enqueue_scripts', 'extendable_enqueue_header_scripts' );
/**
* Navigation customizations
*
* @package Extendable
* @since Extendable 2.0.23
*/
if ( ! function_exists( 'extendable_enqueue_navigation_customizations' ) ) :
/**
* Enqueue the JS that fetches logo & site title to customize the mobile navigation.
*/
function extendable_enqueue_navigation_customizations() {
$logo_id = get_theme_mod( 'custom_logo' );
$logo_url = $logo_id ? wp_get_attachment_image_url( $logo_id, 'full' ) : '';
$site_title = get_bloginfo( 'name' );
wp_enqueue_script(
'extendable-navigation_customizations',
get_template_directory_uri() . '/assets/js/navigation-customization.js',
array(),
EXTENDABLE_THEME_VERSION,
true
);
wp_localize_script(
'extendable-navigation_customizations',
'ExtendableNavData',
array(
'logoUrl' => $logo_url,
'siteTitle' => $site_title,
)
);
}
endif;
add_action( 'wp_enqueue_scripts', 'extendable_enqueue_navigation_customizations' );
/**
* Set default template for new pages in the block editor (auto-drafts)
*
* @since Extendable 2.0.26
*
* @param WP_REST_Response $response The page's REST response.
* @param WP_Post $post The page.
* @return WP_REST_Response
*/
function extendable_set_default_template_for_auto_drafts( WP_REST_Response $response, WP_Post $post ) {
if ( 'page' !== $post->post_type ) {
return $response;
}
if ( 'auto-draft' !== $post->post_status ) {
return $response;
}
$current_template = isset( $response->data['template'] ) ? $response->data['template'] : '';
if ( ! empty( $current_template ) && 'page' !== $current_template ) {
return $response;
}
$response->data['template'] = 'page-with-title';
return $response;
}
add_filter( 'rest_prepare_page', 'extendable_set_default_template_for_auto_drafts', 10, 2 );
/**
* Set default template for new pages when saved/published
*
* @since Extendable 2.0.28
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
* @return void
*/
function extendable_set_default_template_for_new_pages( $post_id, $post, $update ) {
if ( 'page' !== $post->post_type ) {
return;
}
if ( $update ) {
return;
}
if ( 'revision' === $post->post_status ) {
return;
}
// A page template would replace WooCommerce's cart, checkout and account templates on its own pages.
if ( has_block( 'woocommerce/cart', $post ) || has_block( 'woocommerce/checkout', $post ) || has_shortcode( $post->post_content, 'woocommerce_my_account' ) ) {
return;
}
$current_template = get_page_template_slug( $post_id );
// If no template is set or it's the default template, set our default.
if ( empty( $current_template ) || 'page' === $current_template ) {
update_post_meta( $post_id, '_wp_page_template', 'page-with-title' );
}
}
add_action( 'wp_insert_post', 'extendable_set_default_template_for_new_pages', 10, 3 );
/**
* Render WooCommerce's cart, checkout and account pages with their store templates
* even when an older Extendable saved page-with-title on them.
*
* @param array $templates Template candidates for the page.
* @return array
*/
function extendable_store_pages_skip_page_with_title( $templates ) {
if ( ! function_exists( 'wc_get_page_id' ) ) {
return $templates;
}
$store_pages = array( wc_get_page_id( 'cart' ), wc_get_page_id( 'checkout' ), wc_get_page_id( 'myaccount' ) );
if ( ! in_array( get_queried_object_id(), $store_pages, true ) ) {
return $templates;
}
return array_values( array_diff( $templates, array( 'page-with-title' ) ) );
}
add_filter( 'page_template_hierarchy', 'extendable_store_pages_skip_page_with_title' );
/**
* WooCommerce saves its own product card into the cart page's cross-sells, out of the
* templates' reach, so render the shop's card under the related products heading instead.
*
* @param array $parsed_block Parsed block data.
* @return array Filtered block data.
*/
function extendable_cross_sells_use_shop_card( array $parsed_block ): array {
if ( 'woocommerce/product-collection' !== $parsed_block['blockName'] ) {
return $parsed_block;
}
if ( 'woocommerce/product-collection/cross-sells' !== ( $parsed_block['attrs']['collection'] ?? '' ) ) {
return $parsed_block;
}
$heading = '';
foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
if ( 'core/heading' === $inner_block['blockName'] ) {
$heading = sprintf(
'<!-- wp:heading {"style":{"spacing":{"margin":{"top":"var:preset|spacing|40","bottom":"0"}},"color":{"text":"var(--wp--custom--color--text-weak)"}},"fontSize":"medium-plus"} --><h2 class="wp-block-heading has-text-color has-medium-plus-font-size" style="color:var(--wp--custom--color--text-weak);margin-top:var(--wp--preset--spacing--40);margin-bottom:0">%s</h2><!-- /wp:heading -->',
esc_html( trim( wp_strip_all_tags( $inner_block['innerHTML'] ) ) )
);
break;
}
}
$attrs = $parsed_block['attrs'];
$attrs['style']['spacing']['blockGap'] = 'var:preset|spacing|30';
$markup = sprintf(
'<!-- wp:woocommerce/product-collection %s --><div class="wp-block-woocommerce-product-collection">%s<!-- wp:woocommerce/product-template --><!-- wp:pattern {"slug":"extendable/hidden-shop-product"} /--><!-- /wp:woocommerce/product-template --></div><!-- /wp:woocommerce/product-collection -->',
serialize_block_attributes( $attrs ),
$heading
);
return parse_blocks( $markup )[0];
}
add_filter( 'render_block_data', 'extendable_cross_sells_use_shop_card' );
/**
* Only $parent_block says this is the empty cart's own copy, so mark it here
* before the swap below would otherwise hit every Newest Products block.
*
* @param array $parsed_block Parsed block data.
* @param array $source_block Unmodified copy of $parsed_block, as parsed from the source.
* @param WP_Block|null $parent_block The parent block instance, if this one is nested.
* @return array Filtered block data.
*/
function extendable_mark_empty_cart_product_new( array $parsed_block, array $source_block, $parent_block = null ): array {
if ( 'woocommerce/product-new' !== $parsed_block['blockName'] ) {
return $parsed_block;
}
if ( ! $parent_block instanceof WP_Block || 'woocommerce/empty-cart-block' !== $parent_block->name ) {
return $parsed_block;
}
$parsed_block['attrs']['__extendableEmptyCart'] = true;
return $parsed_block;
}
add_filter( 'render_block_data', 'extendable_mark_empty_cart_product_new', 10, 3 );
/**
* WooCommerce also saves its own grid block into the cart page's empty-cart content, out of
* the templates' reach, so render the shop's card there too, the same fix already used for
* the cart's cross-sells list.
*
* @param string $block_content Rendered block content.
* @param array $parsed_block Parsed block data.
* @return string Filtered block content.
*/
function extendable_empty_cart_new_products_use_shop_card( string $block_content, array $parsed_block ): string {
if ( empty( $parsed_block['attrs']['__extendableEmptyCart'] ) ) {
return $block_content;
}
$columns = (int) ( $parsed_block['attrs']['columns'] ?? 4 );
$rows = (int) ( $parsed_block['attrs']['rows'] ?? 1 );
$markup = sprintf(
'<!-- wp:woocommerce/product-collection %s --><div class="wp-block-woocommerce-product-collection"><!-- wp:woocommerce/product-template --><!-- wp:pattern {"slug":"extendable/hidden-shop-product"} /--><!-- /wp:woocommerce/product-template --></div><!-- /wp:woocommerce/product-collection -->',
serialize_block_attributes(
array(
'queryId' => 0,
'query' => array(
'woocommerceAttributes' => array(),
'woocommerceStockStatus' => array( 'instock', 'outofstock', 'onbackorder' ),
'woocommerceOnSale' => false,
'woocommerceHandPickedProducts' => array(),
'taxQuery' => array(),
'isProductCollectionBlock' => true,
'perPage' => $columns * $rows,
'pages' => 1,
'offset' => 0,
'postType' => 'product',
'order' => 'desc',
'orderBy' => 'date',
'search' => '',
'exclude' => array(),
'inherit' => false,
'featured' => false,
),
'tagName' => 'div',
'displayLayout' => array(
'type' => 'flex',
'columns' => $columns,
'shrinkColumns' => true,
),
'style' => array( 'spacing' => array( 'blockGap' => 'var:preset|spacing|30' ) ),
)
)
);
return do_blocks( $markup );
}
add_filter( 'render_block', 'extendable_empty_cart_new_products_use_shop_card', 10, 2 );
/**
* Hide block style variations from editor UI while keeping them registered
*/
function extendable_hide_block_style_variations() {
$css = '
.block-editor-block-styles__item[aria-label*="Brutalism 1"],
.block-editor-block-styles__item[aria-label*="Organic 1"],
.block-editor-block-styles__item[aria-label*="Soft 1"],
.block-editor-block-styles__item[aria-label*="Gradient 1"],
.block-editor-block-styles__item[aria-label*="Matrix 1"],
.block-editor-block-styles__item[aria-label*="Prism 1"],
.block-editor-block-styles__item[aria-label*="Wave 1"] {
display: none !important;
}
';
wp_add_inline_style( 'wp-edit-blocks', $css );
}
add_action( 'enqueue_block_editor_assets', 'extendable_hide_block_style_variations' );
/**
* Hide block style variations from site editor style panel
*/
function extendable_hide_site_editor_block_style_variations() {
$css = '
/* Hide specific design aesthetic variations in site editor */
.components-navigator-button[id*="brutalism-1--"],
.components-navigator-button[id*="organic-1--"],
.components-navigator-button[id*="soft-1--"],
.components-navigator-button[id*="gradient-1--"],
.components-navigator-button[id*="matrix-1--"],
.components-navigator-button[id*="prism-1--"],
.components-navigator-button[id*="wave-1--"] {
display: none !important;
}
/* Hide the Style Variations heading if all variations are hidden */
.components-h-stack:has(.components-navigator-button[id*="ext-preset--"]:not([style*="display: none"])) .edit-site-global-styles-subtitle:contains("Style Variations") {
display: none !important;
}
/* Alternative approach - hide the entire Style Variations section */
.edit-site-global-styles-subtitle:contains("Style Variations") + .components-item-group {
display: none !important;
}
.edit-site-global-styles-subtitle:contains("Style Variations") {
display: none !important;
}
';
wp_add_inline_style( 'wp-edit-site', $css );
}
add_action( 'admin_enqueue_scripts', 'extendable_hide_site_editor_block_style_variations' );
/**
* Temporary fix: Hide matrix-1, prism-1, wave-1 variations when Extendify < 2.4.0.
*
* @param WP_REST_Response $response The REST response.
* @param array $handler The route handler.
* @param WP_REST_Request $request The REST request.
* @return WP_REST_Response
*/
function extendable_filter_global_styles_rest_response( $response, $handler, $request ) {
$route = $request->get_route();
if ( strpos( $route, '/wp/v2/global-styles/themes/' ) === false ) {
return $response;
}
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();
$should_filter = false;
foreach ( $plugins as $plugin_file => $plugin_data ) {
if ( 'extendify-local' === $plugin_data['TextDomain'] && is_plugin_active( $plugin_file ) ) {
if ( version_compare( $plugin_data['Version'], '2.4.0', '<' ) ) {
$should_filter = true;
}
break;
}
}
if ( ! $should_filter ) {
return $response;
}
$patterns = array( 'matrix-1', 'prism-1', 'wave-1' );
$data = $response->get_data();
if ( isset( $data['styles']['blocks'] ) && is_array( $data['styles']['blocks'] ) ) {
foreach ( $data['styles']['blocks'] as $block_name => $block_data ) {
if ( isset( $block_data['variations'] ) && is_array( $block_data['variations'] ) ) {
foreach ( $block_data['variations'] as $variation_slug => $variation_data ) {
foreach ( $patterns as $pattern ) {
if ( strpos( $variation_slug, $pattern ) !== false ) {
unset( $data['styles']['blocks'][ $block_name ]['variations'][ $variation_slug ] );
break;
}
}
}
if ( empty( $data['styles']['blocks'][ $block_name ]['variations'] ) ) {
unset( $data['styles']['blocks'][ $block_name ]['variations'] );
}
}
}
}
if ( isset( $data['_links']['wp:block-style-variations'] ) ) {
$data['_links']['wp:block-style-variations'] = array_filter(
$data['_links']['wp:block-style-variations'],
function ( $variation ) use ( $patterns ) {
$href = $variation['href'] ?? '';
foreach ( $patterns as $pattern ) {
if ( strpos( $href, $pattern ) !== false ) {
return false;
}
}
return true;
}
);
$data['_links']['wp:block-style-variations'] = array_values( $data['_links']['wp:block-style-variations'] );
}
$response->set_data( $data );
return $response;
}
add_filter( 'rest_post_dispatch', 'extendable_filter_global_styles_rest_response', 10, 3 );