Skip to content

Commit be093af

Browse files
committed
Add restrictDirectories option for style variations
1 parent b7d53ee commit be093af

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/IterableCodeExtractor.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ trait IterableCodeExtractor {
2323
* @param array $options {
2424
* Optional. An array of options passed down to static::fromString()
2525
*
26-
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'.
27-
* @type bool $wpExtractPatterns Extract 'Title' and 'Description' headers in pattern files. Default 'false'.
28-
* @type array $restrictFileNames Skip all files which are not included in this array.
26+
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'.
27+
* @type bool $wpExtractPatterns Extract 'Title' and 'Description' headers in pattern files. Default 'false'.
28+
* @type array $restrictFileNames Skip all files which are not included in this array.
29+
* @type array $restrictDirectories Skip all directories which are not included in this array.
2930
* }
3031
* @return null
3132
*/
@@ -38,8 +39,18 @@ public static function fromFile( $file_or_files, Translations $translations, arr
3839
}
3940
}
4041

42+
$relative_file_path = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $file ) ), '/' );
43+
4144
// Make sure a relative file path is added as a comment.
42-
$options['file'] = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $file ) ), '/' );
45+
$options['file'] = $relative_file_path;
46+
47+
if ( ! empty( $options['restrictDirectories'] ) ) {
48+
$top_level_dirname = explode( '/', $relative_file_path )[0];
49+
50+
if ( ! in_array( $top_level_dirname, $options['restrictDirectories'], true ) ) {
51+
continue;
52+
}
53+
}
4354

4455
$text = file_get_contents( $file );
4556

src/MakePotCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,24 @@ protected function extract_strings() {
718718
'addReferences' => $this->location,
719719
]
720720
);
721+
722+
// Themes can have style variations in the top-level "styles" folder.
723+
// They're like theme.json but can have any name.
724+
if ( $is_theme ) {
725+
JsonSchemaExtractor::fromDirectory(
726+
$this->source,
727+
$translations,
728+
[
729+
'restrictDirectories' => [ 'styles' ],
730+
'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE,
731+
'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK,
732+
'include' => $this->include,
733+
'exclude' => $this->exclude,
734+
'extensions' => [ 'json' ],
735+
'addReferences' => $this->location,
736+
]
737+
);
738+
}
721739
}
722740
} catch ( \Exception $e ) {
723741
WP_CLI::error( $e->getMessage() );

0 commit comments

Comments
 (0)