From d544da886e07e5d38c2f909e8106838192fcc069 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 9 Jul 2024 16:46:31 +0100 Subject: [PATCH] Global Styles: Create new public function to make it easier to expose style variations from other themes --- ...class-wp-theme-json-resolver-gutenberg.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index a2153e639db3ba..7a8738a18e88f5 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -764,8 +764,18 @@ private static function style_variation_has_scope( $variation, $scope ) { * @return array */ public static function get_style_variations( $scope = 'theme' ) { + return static::get_style_variations_from_directory( get_stylesheet_directory() ); + } + + /** + * Returns the style variation files defined by the theme (parent and child). + * + * @since 6.7.0 + * + * @return array An array of style variation files. + */ + protected static function get_style_variation_files_from_current_theme() { $variation_files = array(); - $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; $template_directory = get_template_directory() . '/styles'; if ( is_dir( $base_directory ) ) { @@ -783,6 +793,29 @@ public static function get_style_variations( $scope = 'theme' ) { } $variation_files = array_merge( $variation_files, $variation_files_parent ); } + + return $variation_files; + } + + /** + * Returns the style variations in the given directory. + * + * @since 6.7.0 + * + * @param string $directory The directory to get the style variations from. + * @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. + * @return array + */ + public static function get_style_variations_from_directory( $directory, $scope = 'theme' ) { + $variation_files = array(); + $variations = array(); + if ( is_dir( $directory ) ) { + if ( $directory === get_stylesheet_directory() ) { + $variation_files = static::get_style_variation_files_from_current_theme(); + } else { + $variation_files = static::recursively_iterate_json( $base_directory ); + } + } ksort( $variation_files ); foreach ( $variation_files as $path => $file ) { $decoded_file = self::read_json_file( $path );