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

Improve theme patterns and styles lookup #320

Merged
merged 6 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
37 changes: 37 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3646,6 +3646,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:
Expand All @@ -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
Expand All @@ -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:
"""
<?php
/**
* Title: Other pattern title.
* Description: Other pattern description.
*/
"""
And a foo-theme/style.css file:
"""
/*
Expand All @@ -3695,3 +3724,11 @@ Feature: Generate a POT file of a WordPress project
msgid "My pattern description."
msgstr ""
"""
And the foo-theme/foo-theme.pot file should not contain:
"""
msgid "Other pattern title."
"""
And the foo-theme/foo-theme.pot file should not contain:
"""
msgid "Other pattern description."
"""
3 changes: 2 additions & 1 deletion src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static function fromFile( $file_or_files, Translations $translations, arr
}
}

if ( ! empty( $options['wpExtractPatterns'] ) ) {
// Patterns are only supported when in a top-level patterns/ folder.
if ( ! empty( $options['wpExtractPatterns'] ) && 0 === strpos( $options['file'], 'patterns/' ) ) {
$headers = MakePotCommand::get_file_data_from_string(
$string,
[
Expand Down
38 changes: 9 additions & 29 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand All @@ -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 ) );
Expand All @@ -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' ],
Expand All @@ -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,
Expand Down Expand Up @@ -679,26 +672,13 @@ protected function extract_strings() {
}

if ( ! $this->skip_theme_json ) {
// Look for the top-level theme.json file, nothing else.
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.
// 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,
[
'include' => array_merge( $this->include, array( 'styles' ) ),
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'json' ],
]
Expand Down
9 changes: 9 additions & 0 deletions src/ThemeJsonExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down