diff --git a/features/makepot.feature b/features/makepot.feature index 04414d0..9f9bef3 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3687,6 +3687,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": "white", "color": "#ffffff", "name": "White" } + ] + } + } + } + } + } + """ When I try `wp i18n make-pot foo-theme` Then STDOUT should be: @@ -3699,6 +3716,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 @@ -3710,6 +3731,14 @@ Feature: Generate a POT file of a WordPress project * Description: My pattern description. */ """ + And a foo-theme/incorrect/patterns/other-pattern.php file: + """ + 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 ) ) { @@ -614,7 +616,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 ) ); @@ -627,7 +629,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' ], @@ -636,17 +640,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, @@ -699,31 +692,15 @@ protected function extract_strings() { } if ( ! $this->skip_theme_json ) { - // Look for the top-level theme.json file, nothing else. - JsonSchemaExtractor::fromDirectory( - $this->source, - $translations, - [ - 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, - 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, - 'restrictFileNames' => [ 'theme.json' ], - 'include' => $this->include, - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], - 'addReferences' => $this->location, - ] - ); - } - - if ( ! $this->skip_theme_json ) { - // Look for any JSON file within the 'styles' directory. - JsonSchemaExtractor::fromDirectory( + // 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, [ 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, - 'include' => array_merge( $this->include, array( 'styles' ) ), + 'include' => $this->include, 'exclude' => $this->exclude, 'extensions' => [ 'json' ], 'addReferences' => $this->location, diff --git a/src/ThemeJsonExtractor.php b/src/ThemeJsonExtractor.php new file mode 100644 index 0000000..f7ef638 --- /dev/null +++ b/src/ThemeJsonExtractor.php @@ -0,0 +1,29 @@ +