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 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
37 changes: 37 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
"""
<?php
/**
* Title: Other pattern title.
* Description: Other pattern description.
*/
"""
And a foo-theme/style.css file:
"""
/*
Expand All @@ -3736,3 +3765,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 = FileDataExtractor::get_file_data_from_string(
$string,
[
Expand Down
43 changes: 10 additions & 33 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,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 @@ -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 ) );
Expand All @@ -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' ],
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/ThemeJsonExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace WP_CLI\I18n;

use Gettext\Extractors\Extractor;
use Gettext\Extractors\ExtractorInterface;
use Gettext\Translations;
use WP_CLI;
use WP_CLI\Utils;

final class ThemeJsonExtractor extends JsonSchemaExtractor {

/**
* @inheritdoc
*/
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;
}

parent::fromString( $string, $translations, $options );
}
}