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

Add file references for plugin/theme headers #377

Merged
merged 2 commits into from
Jan 16, 2024
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
34 changes: 34 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@ Feature: Generate a POT file of a WordPress project
Description of the plugin
"""

Scenario: Adds file references for file headers.
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
* Plugin URI: https://example.com
* Description: Plugin Description
* Version: 0.1.0
* Author:
* Author URI:
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: foo-plugin
* Domain Path: /languages
*/
"""

When I try `wp i18n make-pot foo-plugin foo-plugin.pot`
Then the foo-plugin.pot file should exist
And the foo-plugin.pot file should contain:
"""
Description of the plugin
"""
And the foo-plugin.pot file should contain:
"""
#: foo-plugin.php
"""
And the foo-plugin.pot file should contain:
"""
Plugin Description
"""

Scenario: Adds copyright comments
When I run `wp scaffold plugin hello-world`

Expand Down
20 changes: 17 additions & 3 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class MakePotCommand extends WP_CLI_Command {
*/
protected $main_file_data = [];

/**
* @var string
*/
protected $main_file_path;

/**
* @var bool
*/
Expand Down Expand Up @@ -481,7 +486,8 @@ protected function get_main_file_data() {
WP_CLI::log( 'Theme stylesheet detected.' );
WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() ), 'make-pot' );

$this->project_type = 'theme';
$this->project_type = 'theme';
$this->main_file_path = $file->getRealPath();

return $theme_data;
}
Expand All @@ -502,7 +508,8 @@ protected function get_main_file_data() {
WP_CLI::log( 'Theme stylesheet detected.' );
WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() . '/style.css' ), 'make-pot' );

$this->project_type = 'theme';
$this->project_type = 'theme';
$this->main_file_path = $file->getRealPath();

return $theme_data;
}
Expand All @@ -523,7 +530,8 @@ protected function get_main_file_data() {
WP_CLI::log( 'Plugin file detected.' );
WP_CLI::debug( sprintf( 'Plugin file: %s', $file->getRealPath() ), 'make-pot' );

$this->project_type = 'plugin';
$this->project_type = 'plugin';
$this->main_file_path = $file->getRealPath();

return $plugin_data;
}
Expand Down Expand Up @@ -622,6 +630,12 @@ protected function extract_strings() {
$translation->addExtractedComment( sprintf( '%s of the plugin', $header ) );
}

if ( $this->main_file_path && $this->location ) {
$translation->addReference(
str_replace( "$this->source/", '', $this->main_file_path )
);
}

$translations[] = $translation;
}

Expand Down