Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make-pot: scan any theme.json file in any level #424

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 129 additions & 4 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -3756,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:
"""
{
Expand All @@ -3774,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",
Expand All @@ -3791,9 +3826,26 @@ 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 be:
Then STDOUT should contain:
"""
Success: POT file successfully generated.
"""
Expand All @@ -3803,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
Expand Down Expand Up @@ -3972,3 +4029,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"
"""
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];
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

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

$text = file_get_contents( $file );

Expand Down
38 changes: 28 additions & 10 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -704,20 +704,38 @@ 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.
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
'restrictFileNames' => [ 'theme.json' ],
'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE,
'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK,
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'json' ],
'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 ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
JsonSchemaExtractor::fromDirectory(
$this->source,
$translations,
[
'restrictDirectories' => [ 'styles' ],
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
'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
29 changes: 0 additions & 29 deletions src/ThemeJsonExtractor.php

This file was deleted.

Loading