From b7d53ee176e16ff5b603f8956641906f9a4080d3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 19 Dec 2024 17:23:50 +0100 Subject: [PATCH 1/5] make-pot: scan any `theme.json` file in any level --- features/makepot.feature | 97 ++++++++++++++++++++++++++++++++++++++ src/MakePotCommand.php | 20 ++++---- src/ThemeJsonExtractor.php | 29 ------------ 3 files changed, 107 insertions(+), 39 deletions(-) delete mode 100644 src/ThemeJsonExtractor.php diff --git a/features/makepot.feature b/features/makepot.feature index 9ff975a..6039374 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3651,6 +3651,35 @@ Feature: Generate a POT file of a WordPress project msgid "Black" """ + Scenario: Skips theme.json file if excluding it + Given an empty foo-theme directory + And a foo-theme/theme.json file: + """ + { + "version": "1", + "settings": { + "color": { + "palette": [ + { "slug": "black", "color": "#000000", "name": "Black" } + ] + } + } + } + """ + + When I try `wp i18n make-pot foo-theme --exclude=theme.json` + Then STDOUT should be: + """ + Success: POT file successfully generated. + """ + And the foo-theme/foo-theme.pot file should exist + But the foo-theme/foo-theme.pot file should not contain: + """ + msgctxt "Color name" + msgid "Black" + """ + + Scenario: Extract strings from the top-level section of theme.json files Given an empty foo-theme directory And a foo-theme/theme.json file: @@ -3972,3 +4001,71 @@ Feature: Generate a POT file of a WordPress project """ msgid "foo-plugin/longertests/foo-plugin.php" """ + + Scenario: Extract strings from theme.json files in any level + Given an empty foo-project directory + And a foo-project/theme.json file: + """ + { + "version": "1", + "title": "My style variation", + "description": "My style variation description" + } + """ + + And a foo-project/nested/theme.json file: + """ + { + "version": "1", + "title": "Nested style variation", + "description": "Nested style variation description" + } + """ + + And a foo-project/nested/notatheme.json file: + """ + { + "version": "1", + "title": "Not extracted style variation", + "description": "Not extracted style variation description" + } + """ + + When I try `wp i18n make-pot foo-project` + Then STDOUT should be: + """ + Success: POT file successfully generated. + """ + And the foo-project/foo-project.pot file should exist + And the foo-project/foo-project.pot file should contain: + """ + #: theme.json + msgctxt "Style variation name" + msgid "My style variation" + """ + And the foo-project/foo-project.pot file should contain: + """ + #: theme.json + msgctxt "Style variation description" + msgid "My style variation description" + """ + And the foo-project/foo-project.pot file should contain: + """ + #: nested/theme.json + msgctxt "Style variation name" + msgid "Nested style variation" + """ + And the foo-project/foo-project.pot file should contain: + """ + #: nested/theme.json + msgctxt "Style variation description" + msgid "Nested style variation description" + """ + And the foo-project/foo-project.pot file should not contain: + """ + msgid "Not extract style variation" + """ + And the foo-project/foo-project.pot file should not contain: + """ + msgid "Not extracted style variation description" + """ diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index f3e84de..6575010 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -693,7 +693,7 @@ protected function extract_strings() { [ 'schema' => JsonSchemaExtractor::BLOCK_JSON_SOURCE, 'schemaFallback' => JsonSchemaExtractor::BLOCK_JSON_FALLBACK, - // Only look for block.json files, nothing else. + // Only look for block.json files in any folder, nothing else. 'restrictFileNames' => [ 'block.json' ], 'include' => $this->include, 'exclude' => $this->exclude, @@ -704,18 +704,18 @@ protected function extract_strings() { } if ( ! $this->skip_theme_json ) { - // This will look for the top-level theme.json file, as well as - // any JSON file within the top-level styles/ directory. - ThemeJsonExtractor::fromDirectory( + JsonSchemaExtractor::fromDirectory( $this->source, $translations, [ - 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, - 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, - 'include' => $this->include, - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], - 'addReferences' => $this->location, + // Only look for theme.json files in any folder, nothing else. + 'restrictFileNames' => [ 'theme.json' ], + 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, + 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, + 'include' => $this->include, + 'exclude' => $this->exclude, + 'extensions' => [ 'json' ], + 'addReferences' => $this->location, ] ); } diff --git a/src/ThemeJsonExtractor.php b/src/ThemeJsonExtractor.php deleted file mode 100644 index c8981aa..0000000 --- a/src/ThemeJsonExtractor.php +++ /dev/null @@ -1,29 +0,0 @@ - Date: Thu, 19 Dec 2024 18:11:28 +0100 Subject: [PATCH 2/5] Add `restrictDirectories` option for style variations --- src/IterableCodeExtractor.php | 19 +++++++++++++++---- src/MakePotCommand.php | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 8611ca2..51b3e53 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -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 */ @@ -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 ); diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 6575010..8688096 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -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() ); From cd7a72bca1db68cdc27529f1ac1a8f1ccea03afd Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 19 Dec 2024 18:38:53 +0100 Subject: [PATCH 3/5] Update test Signed-off-by: Pascal Birchler --- features/makepot.feature | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index 6039374..dfcb852 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3785,7 +3785,13 @@ Feature: Generate a POT file of a WordPress project """ Scenario: Extract strings from style variations - Given an empty foo-theme/styles directory + Given an empty foo-theme directory + And a foo-theme/style.css file: + """ + /* + Theme Name: foo theme + */ + """ And a foo-theme/styles/my-style.json file: """ { From 08a807e6f091cfb8a7d6205fdffab158fdf4f1a0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 19 Dec 2024 20:44:52 +0100 Subject: [PATCH 4/5] Update test Signed-off-by: Pascal Birchler --- features/makepot.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index dfcb852..6be84b9 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3828,7 +3828,7 @@ Feature: Generate a POT file of a WordPress project """ When I try `wp i18n make-pot foo-theme` - Then STDOUT should be: + Then STDOUT should contain: """ Success: POT file successfully generated. """ From c44ff637415ce48a35f3028ac7c73116e949564e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 20 Dec 2024 11:07:18 +0100 Subject: [PATCH 5/5] Add test for style variation in subfolder Signed-off-by: Pascal Birchler --- features/makepot.feature | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index 6be84b9..a955a3d 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3809,7 +3809,7 @@ Feature: Generate a POT file of a WordPress project } } """ - And a foo-theme/incorrect/styles/my-style.json file: + And a foo-theme/styles/deeply/nested/variation.json file: """ { "version": "1", @@ -3826,6 +3826,23 @@ Feature: Generate a POT file of a WordPress project } } """ + And a foo-theme/incorrect/styles/my-style.json file: + """ + { + "version": "1", + "settings": { + "blocks": { + "core/paragraph": { + "color": { + "palette": [ + { "slug": "red", "color": "#ff00000", "name": "Red" } + ] + } + } + } + } + } + """ When I try `wp i18n make-pot foo-theme` Then STDOUT should contain: @@ -3838,10 +3855,15 @@ Feature: Generate a POT file of a WordPress project msgctxt "Color name" msgid "Black" """ - And the foo-theme/foo-theme.pot file should not contain: + And the foo-theme/foo-theme.pot file should contain: """ + msgctxt "Color name" msgid "White" """ + And the foo-theme/foo-theme.pot file should not contain: + """ + msgid "Red" + """ Scenario: Extract strings from the patterns directory Given an empty foo-theme/patterns directory