Skip to content

Commit

Permalink
Check for naming collisions via basename.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Dec 16, 2022
1 parent 2ba5884 commit c88af8b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/experimental/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ private static function remove_JSON_comments( $array ) {
}

/**
* Adds all nested json files within a given directory to a given array.
* Returns an array of all nested json files within a given directory.
*
* @param dir $dir The directory to recursively iterate and list files of.
* @param array $array The array to add to found json files to.
* @param dir $dir The directory to recursively iterate and list files of.
* @return array The merged array.
*/
private static function recursively_iterate_JSON( $dir, $array ) {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
return array_merge( $array, iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) ) );
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;
}

/**
Expand All @@ -192,13 +192,22 @@ private static function recursively_iterate_JSON( $dir, $array ) {
* @return array
*/
public static function get_style_variations() {
$variations = array();
$base_directory = get_stylesheet_directory() . '/styles';
$parent_theme_directory = get_template_directory() . '/styles';
$variations = array();
$base_directory = get_stylesheet_directory() . '/styles';
$template_directory = get_template_directory() . '/styles';
if ( is_dir( $base_directory ) ) {
$variation_files = static::recursively_iterate_JSON( $base_directory, array() );
if ( $parent_theme_directory !== $base_directory && is_dir( $parent_theme_directory ) ) {
$variation_files = static::recursively_iterate_JSON( $parent_theme_directory, $variation_files );
$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 ) {
Expand Down

1 comment on commit c88af8b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3715198814
📝 Reported issues:

Please sign in to comment.