Skip to content

Commit bf6720f

Browse files
authored
Merge pull request #424 from wp-cli/fix/423-themejson
2 parents 0133eb1 + c44ff63 commit bf6720f

File tree

4 files changed

+172
-47
lines changed

4 files changed

+172
-47
lines changed

features/makepot.feature

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3651,6 +3651,35 @@ Feature: Generate a POT file of a WordPress project
36513651
msgid "Black"
36523652
"""
36533653

3654+
Scenario: Skips theme.json file if excluding it
3655+
Given an empty foo-theme directory
3656+
And a foo-theme/theme.json file:
3657+
"""
3658+
{
3659+
"version": "1",
3660+
"settings": {
3661+
"color": {
3662+
"palette": [
3663+
{ "slug": "black", "color": "#000000", "name": "Black" }
3664+
]
3665+
}
3666+
}
3667+
}
3668+
"""
3669+
3670+
When I try `wp i18n make-pot foo-theme --exclude=theme.json`
3671+
Then STDOUT should be:
3672+
"""
3673+
Success: POT file successfully generated.
3674+
"""
3675+
And the foo-theme/foo-theme.pot file should exist
3676+
But the foo-theme/foo-theme.pot file should not contain:
3677+
"""
3678+
msgctxt "Color name"
3679+
msgid "Black"
3680+
"""
3681+
3682+
36543683
Scenario: Extract strings from the top-level section of theme.json files
36553684
Given an empty foo-theme directory
36563685
And a foo-theme/theme.json file:
@@ -3756,7 +3785,13 @@ Feature: Generate a POT file of a WordPress project
37563785
"""
37573786

37583787
Scenario: Extract strings from style variations
3759-
Given an empty foo-theme/styles directory
3788+
Given an empty foo-theme directory
3789+
And a foo-theme/style.css file:
3790+
"""
3791+
/*
3792+
Theme Name: foo theme
3793+
*/
3794+
"""
37603795
And a foo-theme/styles/my-style.json file:
37613796
"""
37623797
{
@@ -3774,7 +3809,7 @@ Feature: Generate a POT file of a WordPress project
37743809
}
37753810
}
37763811
"""
3777-
And a foo-theme/incorrect/styles/my-style.json file:
3812+
And a foo-theme/styles/deeply/nested/variation.json file:
37783813
"""
37793814
{
37803815
"version": "1",
@@ -3791,9 +3826,26 @@ Feature: Generate a POT file of a WordPress project
37913826
}
37923827
}
37933828
"""
3829+
And a foo-theme/incorrect/styles/my-style.json file:
3830+
"""
3831+
{
3832+
"version": "1",
3833+
"settings": {
3834+
"blocks": {
3835+
"core/paragraph": {
3836+
"color": {
3837+
"palette": [
3838+
{ "slug": "red", "color": "#ff00000", "name": "Red" }
3839+
]
3840+
}
3841+
}
3842+
}
3843+
}
3844+
}
3845+
"""
37943846

37953847
When I try `wp i18n make-pot foo-theme`
3796-
Then STDOUT should be:
3848+
Then STDOUT should contain:
37973849
"""
37983850
Success: POT file successfully generated.
37993851
"""
@@ -3803,10 +3855,15 @@ Feature: Generate a POT file of a WordPress project
38033855
msgctxt "Color name"
38043856
msgid "Black"
38053857
"""
3806-
And the foo-theme/foo-theme.pot file should not contain:
3858+
And the foo-theme/foo-theme.pot file should contain:
38073859
"""
3860+
msgctxt "Color name"
38083861
msgid "White"
38093862
"""
3863+
And the foo-theme/foo-theme.pot file should not contain:
3864+
"""
3865+
msgid "Red"
3866+
"""
38103867

38113868
Scenario: Extract strings from the patterns directory
38123869
Given an empty foo-theme/patterns directory
@@ -3972,3 +4029,71 @@ Feature: Generate a POT file of a WordPress project
39724029
"""
39734030
msgid "foo-plugin/longertests/foo-plugin.php"
39744031
"""
4032+
4033+
Scenario: Extract strings from theme.json files in any level
4034+
Given an empty foo-project directory
4035+
And a foo-project/theme.json file:
4036+
"""
4037+
{
4038+
"version": "1",
4039+
"title": "My style variation",
4040+
"description": "My style variation description"
4041+
}
4042+
"""
4043+
4044+
And a foo-project/nested/theme.json file:
4045+
"""
4046+
{
4047+
"version": "1",
4048+
"title": "Nested style variation",
4049+
"description": "Nested style variation description"
4050+
}
4051+
"""
4052+
4053+
And a foo-project/nested/notatheme.json file:
4054+
"""
4055+
{
4056+
"version": "1",
4057+
"title": "Not extracted style variation",
4058+
"description": "Not extracted style variation description"
4059+
}
4060+
"""
4061+
4062+
When I try `wp i18n make-pot foo-project`
4063+
Then STDOUT should be:
4064+
"""
4065+
Success: POT file successfully generated.
4066+
"""
4067+
And the foo-project/foo-project.pot file should exist
4068+
And the foo-project/foo-project.pot file should contain:
4069+
"""
4070+
#: theme.json
4071+
msgctxt "Style variation name"
4072+
msgid "My style variation"
4073+
"""
4074+
And the foo-project/foo-project.pot file should contain:
4075+
"""
4076+
#: theme.json
4077+
msgctxt "Style variation description"
4078+
msgid "My style variation description"
4079+
"""
4080+
And the foo-project/foo-project.pot file should contain:
4081+
"""
4082+
#: nested/theme.json
4083+
msgctxt "Style variation name"
4084+
msgid "Nested style variation"
4085+
"""
4086+
And the foo-project/foo-project.pot file should contain:
4087+
"""
4088+
#: nested/theme.json
4089+
msgctxt "Style variation description"
4090+
msgid "Nested style variation description"
4091+
"""
4092+
And the foo-project/foo-project.pot file should not contain:
4093+
"""
4094+
msgid "Not extract style variation"
4095+
"""
4096+
And the foo-project/foo-project.pot file should not contain:
4097+
"""
4098+
msgid "Not extracted style variation description"
4099+
"""

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: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ protected function extract_strings() {
693693
[
694694
'schema' => JsonSchemaExtractor::BLOCK_JSON_SOURCE,
695695
'schemaFallback' => JsonSchemaExtractor::BLOCK_JSON_FALLBACK,
696-
// Only look for block.json files, nothing else.
696+
// Only look for block.json files in any folder, nothing else.
697697
'restrictFileNames' => [ 'block.json' ],
698698
'include' => $this->include,
699699
'exclude' => $this->exclude,
@@ -704,20 +704,38 @@ protected function extract_strings() {
704704
}
705705

706706
if ( ! $this->skip_theme_json ) {
707-
// This will look for the top-level theme.json file, as well as
708-
// any JSON file within the top-level styles/ directory.
709-
ThemeJsonExtractor::fromDirectory(
707+
JsonSchemaExtractor::fromDirectory(
710708
$this->source,
711709
$translations,
712710
[
713-
'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE,
714-
'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK,
715-
'include' => $this->include,
716-
'exclude' => $this->exclude,
717-
'extensions' => [ 'json' ],
718-
'addReferences' => $this->location,
711+
// Only look for theme.json files in any folder, nothing else.
712+
'restrictFileNames' => [ 'theme.json' ],
713+
'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE,
714+
'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK,
715+
'include' => $this->include,
716+
'exclude' => $this->exclude,
717+
'extensions' => [ 'json' ],
718+
'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() );

src/ThemeJsonExtractor.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)