Skip to content

Commit

Permalink
Rebase trunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Jan 11, 2023
1 parent 8f0d703 commit a5b4967
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 237 deletions.
43 changes: 33 additions & 10 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,24 +670,48 @@ public static function clean_cached_data() {
}

/**
* Returns the style variations defined by the theme.
* Returns an array of all nested json files within a given directory.
*
* @since 5.8.0
*
* @since 6.0.0
* @param dir $dir The directory to recursively iterate and list files of.
* @return array The merged array.
*/
private static function recursively_iterate_JSON( $dir ) {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
$nested_json_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
return $nested_json_files;
}

/**
* Returns the style variations defined by the theme (parent and child).
*
* @return array
*/
public static function get_style_variations() {
$variations = array();
$base_directory = get_stylesheet_directory() . '/styles';
$variations = array();
$base_directory = get_stylesheet_directory() . '/styles';
$template_directory = get_template_directory() . '/styles';
if ( is_dir( $base_directory ) ) {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
$nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
ksort( $nested_html_files );
foreach ( $nested_html_files as $path => $file ) {
$variation_files = static::recursively_iterate_JSON( $base_directory );
if ( $template_directory !== $base_directory && is_dir( $template_directory ) ) {
$variation_files_parent = static::recursively_iterate_JSON( $template_directory );
// If the child and parent variation file basename are the same, only include the child theme's.
foreach ( $variation_files_parent as $parent_path => $parent ) {
foreach ( $variation_files as $child_path => $child ) {
if ( basename( $parent_path ) === basename( $child_path ) ) {
unset( $variation_files_parent[ $parent_path ] );
}
}
}
$variation_files = array_merge( $variation_files, $variation_files_parent );
}
ksort( $variation_files );
foreach ( $variation_files as $path => $file ) {
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) {
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
$variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data();
$variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
if ( empty( $variation['title'] ) ) {
$variation['title'] = basename( $path, '.json' );
}
Expand All @@ -697,5 +721,4 @@ public static function get_style_variations() {
}
return $variations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,30 @@ public function get_theme_item( $request ) {

return $response;
}

/**
* Returns the given theme global styles variations.
*
* @since 6.0.0
* @since 6.2.0 Returns parent theme variations, if they exist.
*
* @param WP_REST_Request $request The request instance.
*
* @return WP_REST_Response|WP_Error
*/
public function get_theme_items( $request ) {
if ( get_stylesheet() !== $request['stylesheet'] ) {
// This endpoint only supports the active or parent theme for now.
return new WP_Error(
sprintf( '%s', $request['template'] ),
__( 'Theme not found.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations();
$response = rest_ensure_response( $variations );

return $response;
}
}
227 changes: 0 additions & 227 deletions lib/experimental/class-wp-theme-json-resolver-gutenberg.php

This file was deleted.

0 comments on commit a5b4967

Please sign in to comment.