Make WordPress Core


Ignore:
Timestamp:
04/05/2022 09:50:13 AM (4 years ago)
Author:
youknowriad
Message:

Block Editor: Backport the Global Styles Variations endpoint.

This include the /global-styles/themes/{theme}/variations rest endpoint into core.
The endpoint will be used by the site editor to display alternative theme styles to the user.

Props gziolo, oandregal.
See #55505.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r52744 r53072  
    450450        }
    451451
     452        /**
     453         * Returns the style variations defined by the theme.
     454         *
     455         * @since 6.0.0
     456         *
     457         * @return array
     458         */
     459        public static function get_style_variations() {
     460                $variations     = array();
     461                $base_directory = get_stylesheet_directory() . '/styles';
     462                if ( is_dir( $base_directory ) ) {
     463                        $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
     464                        $nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
     465                        ksort( $nested_html_files );
     466                        foreach ( $nested_html_files as $path => $file ) {
     467                                $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
     468                                if ( is_array( $decoded_file ) ) {
     469                                        $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
     470                                        $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
     471                                        if ( empty( $variation['title'] ) ) {
     472                                                $variation['title'] = basename( $path, '.json' );
     473                                        }
     474                                        $variations[] = $variation;
     475                                }
     476                        }
     477                }
     478                return $variations;
     479        }
    452480}
Note: See TracChangeset for help on using the changeset viewer.