@@ -664,33 +664,61 @@ public static function clean_cached_data() {
664664 static ::$ i18n_schema = null ;
665665 }
666666
667+ /**
668+ * Returns an array of all nested json files within a given directory.
669+ *
670+ * @since 6.2.0
671+ *
672+ * @param string $dir The directory to recursively iterate and list files of.
673+ * @return array The merged array.
674+ */
675+ private static function recursively_iterate_json ( $ dir ) {
676+ $ nested_files = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $ dir ) );
677+ $ nested_json_files = iterator_to_array ( new RegexIterator ( $ nested_files , '/^.+\.json$/i ' , RecursiveRegexIterator::GET_MATCH ) );
678+ return $ nested_json_files ;
679+ }
680+
681+
667682 /**
668683 * Returns the style variations defined by the theme.
669684 *
670685 * @since 6.0.0
686+ * @since 6.2.0 Returns parent theme variations if theme is a child.
671687 *
672688 * @return array
673689 */
674690 public static function get_style_variations () {
675- $ variations = array ();
676- $ base_directory = get_stylesheet_directory () . '/styles ' ;
691+ $ variation_files = array ();
692+ $ variations = array ();
693+ $ base_directory = get_stylesheet_directory () . '/styles ' ;
694+ $ template_directory = get_template_directory () . '/styles ' ;
677695 if ( is_dir ( $ base_directory ) ) {
678- $ nested_files = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $ base_directory ) );
679- $ nested_html_files = iterator_to_array ( new RegexIterator ( $ nested_files , '/^.+\.json$/i ' , RecursiveRegexIterator::GET_MATCH ) );
680- ksort ( $ nested_html_files );
681- foreach ( $ nested_html_files as $ path => $ file ) {
682- $ decoded_file = wp_json_file_decode ( $ path , array ( 'associative ' => true ) );
683- if ( is_array ( $ decoded_file ) ) {
684- $ translated = static ::translate ( $ decoded_file , wp_get_theme ()->get ( 'TextDomain ' ) );
685- $ variation = ( new WP_Theme_JSON ( $ translated ) )->get_raw_data ();
686- if ( empty ( $ variation ['title ' ] ) ) {
687- $ variation ['title ' ] = basename ( $ path , '.json ' );
696+ $ variation_files = static ::recursively_iterate_json ( $ base_directory );
697+ }
698+ if ( is_dir ( $ template_directory ) && $ template_directory !== $ base_directory ) {
699+ $ variation_files_parent = static ::recursively_iterate_json ( $ template_directory );
700+ // If the child and parent variation file basename are the same, only include the child theme's.
701+ foreach ( $ variation_files_parent as $ parent_path => $ parent ) {
702+ foreach ( $ variation_files as $ child_path => $ child ) {
703+ if ( basename ( $ parent_path ) === basename ( $ child_path ) ) {
704+ unset( $ variation_files_parent [ $ parent_path ] );
688705 }
689- $ variations [] = $ variation ;
690706 }
691707 }
708+ $ variation_files = array_merge ( $ variation_files , $ variation_files_parent );
709+ }
710+ ksort ( $ variation_files );
711+ foreach ( $ variation_files as $ path => $ file ) {
712+ $ decoded_file = wp_json_file_decode ( $ path , array ( 'associative ' => true ) );
713+ if ( is_array ( $ decoded_file ) ) {
714+ $ translated = static ::translate ( $ decoded_file , wp_get_theme ()->get ( 'TextDomain ' ) );
715+ $ variation = ( new WP_Theme_JSON ( $ translated ) )->get_raw_data ();
716+ if ( empty ( $ variation ['title ' ] ) ) {
717+ $ variation ['title ' ] = basename ( $ path , '.json ' );
718+ }
719+ $ variations [] = $ variation ;
720+ }
692721 }
693722 return $ variations ;
694723 }
695-
696724}
0 commit comments