From ba90c4ef7a0ed08abfe477968c5ba52830de4f80 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 2 May 2022 22:19:12 +0200 Subject: [PATCH 1/4] Look for patterns in all directories --- src/MakePotCommand.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index bfe37c61..7fd12bb7 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -588,6 +588,8 @@ protected function extract_strings() { unset( $this->main_file_data['Version'], $this->main_file_data['License'], $this->main_file_data['Domain Path'], $this->main_file_data['Text Domain'] ); + $is_theme = isset( $this->main_file_data['Theme Name'] ); + // Set entries from main file data. foreach ( $this->main_file_data as $header => $data ) { if ( empty( $data ) ) { @@ -596,7 +598,7 @@ protected function extract_strings() { $translation = new Translation( '', $data ); - if ( isset( $this->main_file_data['Theme Name'] ) ) { + if ( $is_theme ) { $translation->addExtractedComment( sprintf( '%s of the theme', $header ) ); } else { $translation->addExtractedComment( sprintf( '%s of the plugin', $header ) ); @@ -609,7 +611,9 @@ protected function extract_strings() { if ( ! $this->skip_php ) { $options = [ // Extract 'Template Name' headers in theme files. - 'wpExtractTemplates' => isset( $this->main_file_data['Theme Name'] ), + 'wpExtractTemplates' => $is_theme, + // Extract 'Title' and 'Description' headers from pattern files. + 'wpExtractPatterns' => $is_theme, 'include' => $this->include, 'exclude' => $this->exclude, 'extensions' => [ 'php' ], @@ -618,17 +622,6 @@ protected function extract_strings() { PhpCodeExtractor::fromDirectory( $this->source, $translations, $options ); } - if ( ! $this->skip_php ) { - $options = [ - // Extract 'Title' and 'Description' headers from pattern files. - 'wpExtractPatterns' => isset( $this->main_file_data['Theme Name'] ), - 'include' => array_merge( $this->include, array( 'patterns' ) ), - 'exclude' => $this->exclude, - 'extensions' => [ 'php' ], - ]; - PhpCodeExtractor::fromDirectory( $this->source, $translations, $options ); - } - if ( ! $this->skip_blade ) { $options = [ 'include' => $this->include, From 345eb7a88443d4db63a8d8e7c7aac935fb197392 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 May 2022 10:24:44 +0200 Subject: [PATCH 2/4] Use different means for including the right files --- features/makepot.feature | 39 ++++++++++++++++++++++++++++++++++- src/IterableCodeExtractor.php | 3 ++- src/MakePotCommand.php | 17 ++------------- src/ThemeJsonExtractor.php | 9 ++++++++ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index 1c1913fc..bd755d67 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3646,7 +3646,24 @@ 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": "white", "color": "#ffffff", "name": "White" } + ] + } + } + } + } + } + """ + When I try `wp i18n make-pot foo-theme` Then STDOUT should be: """ @@ -3658,6 +3675,10 @@ 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: + """ + msgid "White" + """ Scenario: Extract strings from the patterns directory Given an empty foo-theme/patterns directory @@ -3669,6 +3690,14 @@ Feature: Generate a POT file of a WordPress project * Description: My pattern description. */ """ + And a foo-theme/incorrect/patterns/other-pattern.php file: + """ + skip_theme_json ) { - // Look for the top-level theme.json file, nothing else. + // This will look for the top-level theme.json file, as well as + // any JSON file within the top-level styles/ directory. ThemeJsonExtractor::fromDirectory( $this->source, $translations, [ - 'restrictFileNames' => [ 'theme.json' ], 'include' => $this->include, 'exclude' => $this->exclude, 'extensions' => [ 'json' ], ] ); } - - if ( ! $this->skip_theme_json ) { - // Look for any JSON file within the 'styles' directory. - ThemeJsonExtractor::fromDirectory( - $this->source, - $translations, - [ - 'include' => array_merge( $this->include, array( 'styles' ) ), - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], - ] - ); - } } catch ( \Exception $e ) { WP_CLI::error( $e->getMessage() ); } diff --git a/src/ThemeJsonExtractor.php b/src/ThemeJsonExtractor.php index 50e6a8a8..b6f479c4 100644 --- a/src/ThemeJsonExtractor.php +++ b/src/ThemeJsonExtractor.php @@ -23,6 +23,15 @@ final class ThemeJsonExtractor extends Extractor implements ExtractorInterface { */ public static function fromString( $string, Translations $translations, array $options = [] ) { $file = $options['file']; + + // Only support top-level theme.json file or any JSON file within a top-level styles/ folder. + if ( + 'theme.json' !== $file && + 0 !== strpos( $file, 'styles/' ) + ) { + return; + } + WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); $theme_json = json_decode( $string, true ); From d3ef2c4fad773fe00acae661580c14d9ad26124c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 May 2022 10:24:59 +0200 Subject: [PATCH 3/4] Lint fix --- src/MakePotCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 4c8ac998..771f316a 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -678,9 +678,9 @@ protected function extract_strings() { $this->source, $translations, [ - 'include' => $this->include, - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], + 'include' => $this->include, + 'exclude' => $this->exclude, + 'extensions' => [ 'json' ], ] ); } From 4fc2213a135d9444d805f6d6881a96661210aff0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 4 Jul 2022 18:39:55 +0200 Subject: [PATCH 4/4] Fix arrow alignment --- src/MakePotCommand.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 9f29c194..74db870f 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -698,13 +698,12 @@ protected function extract_strings() { $this->source, $translations, [ - - 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, - 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, - 'include' => $this->include, - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], - 'addReferences' => $this->location, + 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, + 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, + 'include' => $this->include, + 'exclude' => $this->exclude, + 'extensions' => [ 'json' ], + 'addReferences' => $this->location, ] ); }