From 7cc6cba642879707da021b4d79d9a295333912d3 Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 6 Apr 2023 13:12:23 +0200 Subject: [PATCH 1/6] Blockbase: Remove style variations from child themes --- blockbase/functions.php | 1 + blockbase/inc/rest-api.php | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 blockbase/inc/rest-api.php diff --git a/blockbase/functions.php b/blockbase/functions.php index f4fb72de2e..1345846a68 100644 --- a/blockbase/functions.php +++ b/blockbase/functions.php @@ -103,6 +103,7 @@ function blockbase_scripts() { } require get_template_directory() . '/inc/fonts/custom-fonts.php'; +require get_template_directory() . '/inc/rest-api.php'; // Force menus to reload diff --git a/blockbase/inc/rest-api.php b/blockbase/inc/rest-api.php new file mode 100644 index 0000000000..6d39436e99 --- /dev/null +++ b/blockbase/inc/rest-api.php @@ -0,0 +1,63 @@ +get_data(); + if ( ! is_array( $variations ) ) { + return $response; + } + + $template_directory = get_template_directory() . '/styles'; + if ( ! is_dir( $template_directory ) ) { + return $response; + } + + $variation_titles_parent = array(); + $files_parent = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $template_directory ) ); + $variation_files_parent = iterator_to_array( new RegexIterator( $files_parent, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ); + foreach ( $variation_files_parent as $path => $file ) { + $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); + if ( ! is_array( $decoded_file ) ) { + continue; + } + + if ( empty( $decoded_file['title'] ) ) { + $variation_title_parent = basename( $path, '.json' ); + } else { + $variation_title_parent = translate_with_gettext_context( $decoded_file['title'], 'Style variation name', wp_get_theme()->get( 'TextDomain' ) ); + } + $variation_titles_parent[] = $variation_title_parent; + } + + $response->set_data( array_filter( $variations, function( $variation ) use ( $variation_titles_parent ) { + return ! in_array( $variation['title'], $variation_titles_parent, true ); + } ) ); + return $response; +} +add_filter( 'rest_request_after_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); From 1d48de6a7f376f2efc47c553f1780a8db66404d5 Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 6 Apr 2023 15:18:43 +0200 Subject: [PATCH 2/6] Make code more legible --- blockbase/inc/rest-api.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/blockbase/inc/rest-api.php b/blockbase/inc/rest-api.php index 6d39436e99..c5a6a1f462 100644 --- a/blockbase/inc/rest-api.php +++ b/blockbase/inc/rest-api.php @@ -24,7 +24,9 @@ function blockbase_remove_style_variations_from_child_themes( $response, $handle return $response; } - if ( ! is_child_theme() ) { + $base_directory = get_stylesheet_directory() . '/styles'; + $template_directory = get_template_directory() . '/styles'; + if ( ! is_dir( $template_directory ) || $template_directory === $base_directory ) { return $response; } @@ -33,11 +35,6 @@ function blockbase_remove_style_variations_from_child_themes( $response, $handle return $response; } - $template_directory = get_template_directory() . '/styles'; - if ( ! is_dir( $template_directory ) ) { - return $response; - } - $variation_titles_parent = array(); $files_parent = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $template_directory ) ); $variation_files_parent = iterator_to_array( new RegexIterator( $files_parent, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ); @@ -55,9 +52,13 @@ function blockbase_remove_style_variations_from_child_themes( $response, $handle $variation_titles_parent[] = $variation_title_parent; } - $response->set_data( array_filter( $variations, function( $variation ) use ( $variation_titles_parent ) { - return ! in_array( $variation['title'], $variation_titles_parent, true ); - } ) ); + $variations = array_filter( + $variations, + function( $variation ) use ( $variation_titles_parent ) { + return ! in_array( $variation['title'], $variation_titles_parent, true ); + } + ); + $response->set_data( $variations ); return $response; } add_filter( 'rest_request_after_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); From 3fbcae07d28469d080d9a6125acbdb5508fcd657 Mon Sep 17 00:00:00 2001 From: mmtr Date: Thu, 6 Apr 2023 16:08:17 +0200 Subject: [PATCH 3/6] Ignore phpcs rule --- blockbase/inc/rest-api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/blockbase/inc/rest-api.php b/blockbase/inc/rest-api.php index c5a6a1f462..89a5172f8a 100644 --- a/blockbase/inc/rest-api.php +++ b/blockbase/inc/rest-api.php @@ -47,6 +47,7 @@ function blockbase_remove_style_variations_from_child_themes( $response, $handle if ( empty( $decoded_file['title'] ) ) { $variation_title_parent = basename( $path, '.json' ); } else { + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain $variation_title_parent = translate_with_gettext_context( $decoded_file['title'], 'Style variation name', wp_get_theme()->get( 'TextDomain' ) ); } $variation_titles_parent[] = $variation_title_parent; From f51deda48147f384ade8a13e0b42a483aa59fa9b Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 12 Apr 2023 12:34:14 +0200 Subject: [PATCH 4/6] Add `template_directory` filter to bypass the "is child theme" check and thus prevent the style variations from being inherited. --- blockbase/inc/rest-api.php | 55 +++++++------------------------------- 1 file changed, 10 insertions(+), 45 deletions(-) diff --git a/blockbase/inc/rest-api.php b/blockbase/inc/rest-api.php index 89a5172f8a..374be9f60a 100644 --- a/blockbase/inc/rest-api.php +++ b/blockbase/inc/rest-api.php @@ -4,62 +4,27 @@ * Removes the style variations of Blockbase child themes inherited by the parent theme. * * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. - * Usually a WP_REST_Response or WP_Error. - * @param array $handler Route handler used for the request. + * @param array $handler Route handler used for the request. * - * @return WP_REST_Response Filtered response without the inherited style variations. + * @return WP_REST_Response|WP_HTTP_Response|WP_Error|mixed Result to send to the client. */ function blockbase_remove_style_variations_from_child_themes( $response, $handler ) { - if ( ! $response instanceof WP_REST_Response ) { - return $response; - } - if ( ! isset( $handler['callback'] ) || ! is_array( $handler['callback'] ) ) { return $response; } $handler_class = isset( $handler['callback'][0] ) ? $handler['callback'][0] : null; $handler_method = isset( $handler['callback'][1] ) ? $handler['callback'][1] : null; - if ( ! is_a( $handler_class, 'WP_REST_Global_Styles_Controller' ) || 'get_theme_items' !== $handler_method ) { - return $response; - } - - $base_directory = get_stylesheet_directory() . '/styles'; - $template_directory = get_template_directory() . '/styles'; - if ( ! is_dir( $template_directory ) || $template_directory === $base_directory ) { - return $response; - } - - $variations = $response->get_data(); - if ( ! is_array( $variations ) ) { - return $response; - } - - $variation_titles_parent = array(); - $files_parent = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $template_directory ) ); - $variation_files_parent = iterator_to_array( new RegexIterator( $files_parent, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ); - foreach ( $variation_files_parent as $path => $file ) { - $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); - if ( ! is_array( $decoded_file ) ) { - continue; - } - if ( empty( $decoded_file['title'] ) ) { - $variation_title_parent = basename( $path, '.json' ); - } else { - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain - $variation_title_parent = translate_with_gettext_context( $decoded_file['title'], 'Style variation name', wp_get_theme()->get( 'TextDomain' ) ); - } - $variation_titles_parent[] = $variation_title_parent; + /* + * Prevents Blockbase child themes from being considered child themes in the + * `wp/v2/global-styles/themes/:theme/variations` API endpoint, so they don't + * inherit the style variations from the parent theme. + */ + if ( is_a( $handler_class, 'WP_REST_Global_Styles_Controller' ) && 'get_theme_items' === $handler_method ) { + add_filter( 'template_directory', 'get_stylesheet_directory' ); } - $variations = array_filter( - $variations, - function( $variation ) use ( $variation_titles_parent ) { - return ! in_array( $variation['title'], $variation_titles_parent, true ); - } - ); - $response->set_data( $variations ); return $response; } -add_filter( 'rest_request_after_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); +add_filter( 'rest_request_before_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); From e8e4bdceaf941581d09e62d88ef870fdb618fbe3 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Wed, 12 Apr 2023 09:52:42 -0400 Subject: [PATCH 5/6] Made the opt-out of parent style variations configurable per child theme --- blockbase/inc/rest-api.php | 10 +++++++++- jackson/theme.json | 3 ++- kingsley/theme.json | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/blockbase/inc/rest-api.php b/blockbase/inc/rest-api.php index 374be9f60a..057ba33c03 100644 --- a/blockbase/inc/rest-api.php +++ b/blockbase/inc/rest-api.php @@ -27,4 +27,12 @@ function blockbase_remove_style_variations_from_child_themes( $response, $handle return $response; } -add_filter( 'rest_request_before_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); + +function blockbase_check_for_opt_out_parent_variations() { + $opt_out = wp_get_global_settings( array( 'custom', 'optOutOfParentStyleVariations' ) ); + if ( $opt_out ) { + add_filter( 'rest_request_before_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); + } +} + +add_action( 'init', 'blockbase_check_for_opt_out_parent_variations', 99 ); diff --git a/jackson/theme.json b/jackson/theme.json index 3ab5747adc..9ba6480cc0 100644 --- a/jackson/theme.json +++ b/jackson/theme.json @@ -24,7 +24,8 @@ "primary": "var(--wp--preset--color--foreground)", "secondary": "var(--wp--preset--color--foreground)", "tertiary": "var(--wp--preset--color--background)" - } + }, + "optOutOfParentStyleVariations": true } }, "styles": { diff --git a/kingsley/theme.json b/kingsley/theme.json index 5e2181bf6c..68b67f5bd0 100644 --- a/kingsley/theme.json +++ b/kingsley/theme.json @@ -24,7 +24,8 @@ "primary": "var(--wp--preset--color--foreground)", "secondary": "var(--wp--preset--color--foreground)", "tertiary": "var(--wp--preset--color--background)" - } + }, + "optOutOfParentStyleVariations": true } }, "styles": { From b2f858d5e7b15bcd61d045ae60173b11f6ee0e77 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Wed, 12 Apr 2023 13:46:02 -0400 Subject: [PATCH 6/6] Move opt out check inside of api filter and added default value --- blockbase/inc/rest-api.php | 12 +++--------- blockbase/theme.json | 1 + 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/blockbase/inc/rest-api.php b/blockbase/inc/rest-api.php index 057ba33c03..271834d2ee 100644 --- a/blockbase/inc/rest-api.php +++ b/blockbase/inc/rest-api.php @@ -15,24 +15,18 @@ function blockbase_remove_style_variations_from_child_themes( $response, $handle $handler_class = isset( $handler['callback'][0] ) ? $handler['callback'][0] : null; $handler_method = isset( $handler['callback'][1] ) ? $handler['callback'][1] : null; + $opt_out = wp_get_global_settings( array( 'custom', 'optOutOfParentStyleVariations' ) ); /* * Prevents Blockbase child themes from being considered child themes in the * `wp/v2/global-styles/themes/:theme/variations` API endpoint, so they don't * inherit the style variations from the parent theme. */ - if ( is_a( $handler_class, 'WP_REST_Global_Styles_Controller' ) && 'get_theme_items' === $handler_method ) { + if ( $opt_out && is_a( $handler_class, 'WP_REST_Global_Styles_Controller' ) && 'get_theme_items' === $handler_method ) { add_filter( 'template_directory', 'get_stylesheet_directory' ); } return $response; } -function blockbase_check_for_opt_out_parent_variations() { - $opt_out = wp_get_global_settings( array( 'custom', 'optOutOfParentStyleVariations' ) ); - if ( $opt_out ) { - add_filter( 'rest_request_before_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); - } -} - -add_action( 'init', 'blockbase_check_for_opt_out_parent_variations', 99 ); +add_filter( 'rest_request_before_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 ); diff --git a/blockbase/theme.json b/blockbase/theme.json index 5010804c7b..7ed7679a82 100644 --- a/blockbase/theme.json +++ b/blockbase/theme.json @@ -93,6 +93,7 @@ ] }, "custom": { + "optOutOfParentStyleVariations": false, "alignment": { "alignedMaxWidth": "50%" },