Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions blockbase/inc/rest-api.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

/**
* Removes the style variations of Blockbase child themes inherited by the parent theme.
* Removes all 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.
* @param array $handler Route handler used for the request.
*
* @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 ) {
function blockbase_remove_all_style_variations_from_child_themes( $response, $handler ) {
if ( ! isset( $handler['callback'] ) || ! is_array( $handler['callback'] ) ) {
return $response;
}
Expand All @@ -32,4 +32,41 @@ 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 );
add_filter( 'rest_request_before_callbacks', 'blockbase_remove_all_style_variations_from_child_themes', 10, 2 );

/**
* Removes any specific style variation from Blockbase child theme, via theme.json.
*
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client.
* @param array $handler Route handler used for the request.
* @param WP_REST_Request $request Request used to generate the response.
*
* @return mixed
*/
function blockbase_remove_style_variations_from_child_themes( $response, $handler, $request ) {
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;
$remove_variations = wp_get_global_settings( array( 'custom', 'excludedParentStyleVariations' ) );

// Global styles are overridden when Gutenberg plugin is used.
$check_class = class_exists( 'WP_REST_Global_Styles_Controller_Gutenberg' ) ? 'WP_REST_Global_Styles_Controller_Gutenberg' : 'WP_REST_Global_Styles_Controller';

if ( is_a( $handler_class, $check_class ) && 'get_theme_items' === $handler_method && ! empty( $remove_variations ) ) {
$i = 0;
foreach( $response->data as $element ) {
if ( in_array( $element['title'], $remove_variations ) ) {
unset( $response->data[ $i ] );
}
$i++;
}
// Reset array to avoid errors due to items being removed.
$response->data = array_values( $response->data );
}
return $response;
}

add_filter( 'rest_request_after_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 3 );