rest-api.php 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * Removes the style variations of Blockbase child themes inherited by the parent theme.
  4. *
  5. * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client.
  6. * @param array $handler Route handler used for the request.
  7. *
  8. * @return WP_REST_Response|WP_HTTP_Response|WP_Error|mixed Result to send to the client.
  9. */
  10. function blockbase_remove_style_variations_from_child_themes( $response, $handler ) {
  11. if ( ! isset( $handler['callback'] ) || ! is_array( $handler['callback'] ) ) {
  12. return $response;
  13. }
  14. $handler_class = isset( $handler['callback'][0] ) ? $handler['callback'][0] : null;
  15. $handler_method = isset( $handler['callback'][1] ) ? $handler['callback'][1] : null;
  16. $opt_out = wp_get_global_settings( array( 'custom', 'optOutOfParentStyleVariations' ) );
  17. /*
  18. * Prevents Blockbase child themes from being considered child themes in the
  19. * `wp/v2/global-styles/themes/:theme/variations` API endpoint, so they don't
  20. * inherit the style variations from the parent theme.
  21. */
  22. if ( $opt_out && is_a( $handler_class, 'WP_REST_Global_Styles_Controller' ) && 'get_theme_items' === $handler_method ) {
  23. add_filter( 'template_directory', 'get_stylesheet_directory' );
  24. }
  25. return $response;
  26. }
  27. add_filter( 'rest_request_before_callbacks', 'blockbase_remove_style_variations_from_child_themes', 10, 2 );