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

Extract pattern metadata (title & description) #312

Merged
merged 10 commits into from
Apr 6, 2022
12 changes: 12 additions & 0 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ trait IterableCodeExtractor {
* 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' headers in pattern files. Default 'false'.
* @type array $restrictFileNames Skip all files which are not included in this array.
* }
* @return null
Expand Down Expand Up @@ -65,6 +66,17 @@ public static function fromFile( $file_or_files, Translations $translations, arr
}
}

if ( ! empty( $options['wpExtractPatterns'] ) ) {
$headers = MakePotCommand::get_file_data_from_string( $string, [ 'Title' => 'Title' ] );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

if ( ! empty( $headers['Title'] ) ) {
$translation = new Translation( '', $headers['Title'] );
$translation->addExtractedComment( 'Title of the pattern' );

$translations[] = $translation;
}
}

static::fromString( $string, $translations, $options );
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ protected function extract_strings() {
PhpCodeExtractor::fromDirectory( $this->source, $translations, $options );
}

if ( ! $this->skip_php ) {
$options = [
// Extract 'Title' headers from pattern files.
oandregal marked this conversation as resolved.
Show resolved Hide resolved
'wpExtractPatterns' => isset( $this->main_file_data['Theme Name'] ),
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
'include' => array_merge( $this->include, array( 'patterns' ) ),
Copy link
Contributor

@ocean90 ocean90 May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissspidy Do you know why this hardcodes patterns here? Since these are PHP files they should be included by default. Now you explicitly have to exclude the patterns when you for example are using --include=wp-admin/*. to only include wp-admin strings.

Related: https://meta.trac.wordpress.org/changeset/11809

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the idea was to make this more efficient and only scan these files when needed.

But you're right that this is already covered by the call above. I'll create a PHP to combine these.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'exclude' => $this->exclude,
'extensions' => [ 'php' ],
];
PhpCodeExtractor::fromDirectory( $this->source, $translations, $options );
}

if ( ! $this->skip_blade ) {
$options = [
'include' => $this->include,
Expand Down