Skip to content

Commit

Permalink
Global Styles: Create new public function to make it easier to expose…
Browse files Browse the repository at this point in the history
… style variations from other themes (#63318)

* Global Styles: Create new public function to make it easier to expose style variations from other themes

* pass scope

* fix PHPCS

* remove disconnected changes

Co-authored-by: scruffian <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: carolinan <[email protected]>
  • Loading branch information
4 people authored Jul 31, 2024
1 parent 08d3c86 commit 7206339
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(), $scope );
}

/**
* 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 ) ) {
Expand All @@ -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 ( get_stylesheet_directory() === $directory ) {
$variation_files = static::get_style_variation_files_from_current_theme();
} else {
$variation_files = static::recursively_iterate_json( $directory );
}
}
ksort( $variation_files );
foreach ( $variation_files as $path => $file ) {
$decoded_file = self::read_json_file( $path );
Expand Down

0 comments on commit 7206339

Please sign in to comment.