Skip to content

Commit

Permalink
Add restrictDirectories option for style variations
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Dec 19, 2024
1 parent b7d53ee commit be093af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ trait IterableCodeExtractor {
* @param array $options {
* Optional. An array of options passed down to static::fromString()
*
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'.
* @type bool $wpExtractPatterns Extract 'Title' and 'Description' headers in pattern files. Default 'false'.
* @type array $restrictFileNames Skip all files which are not included in this array.
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'.
* @type bool $wpExtractPatterns Extract 'Title' and 'Description' headers in pattern files. Default 'false'.
* @type array $restrictFileNames Skip all files which are not included in this array.
* @type array $restrictDirectories Skip all directories which are not included in this array.
* }
* @return null
*/
Expand All @@ -38,8 +39,18 @@ public static function fromFile( $file_or_files, Translations $translations, arr
}
}

$relative_file_path = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $file ) ), '/' );

// Make sure a relative file path is added as a comment.
$options['file'] = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $file ) ), '/' );
$options['file'] = $relative_file_path;

if ( ! empty( $options['restrictDirectories'] ) ) {
$top_level_dirname = explode( '/', $relative_file_path )[0];

if ( ! in_array( $top_level_dirname, $options['restrictDirectories'], true ) ) {
continue;
}
}

$text = file_get_contents( $file );

Expand Down
18 changes: 18 additions & 0 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,24 @@ protected function extract_strings() {
'addReferences' => $this->location,
]
);

// Themes can have style variations in the top-level "styles" folder.
// They're like theme.json but can have any name.
if ( $is_theme ) {
JsonSchemaExtractor::fromDirectory(
$this->source,
$translations,
[
'restrictDirectories' => [ 'styles' ],
'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE,
'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK,
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'json' ],
'addReferences' => $this->location,
]
);
}
}
} catch ( \Exception $e ) {
WP_CLI::error( $e->getMessage() );
Expand Down

0 comments on commit be093af

Please sign in to comment.