Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jul 31, 2024
Merged
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
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
Loading