diff --git a/README.md b/README.md index c47089a6..7abc792b 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,9 @@ Scans PHP and JavaScript files, as well as theme stylesheets for translatable st By default, the following files and folders are ignored: node_modules, .git, .svn, .CVS, .hg, vendor. Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`. + [--headers=] + Array in JSON format of custom headers which will be added to the POT file. Defaults to empty array. + [--skip-js] Skips JavaScript string extraction. Useful when this is done in another build step, e.g. through Babel. diff --git a/features/makepot.feature b/features/makepot.feature index 70fba32e..9811ad7e 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -99,6 +99,28 @@ Feature: Generate a POT file of a WordPress plugin "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n" """ + Scenario: Sets custom Report-Msgid-Bugs-To + When I run `wp scaffold plugin hello-world` + + When I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot --headers='{"Report-Msgid-Bugs-To":"https://github.com/hello-world/hello-world/"}'` + And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain: + """ + "Report-Msgid-Bugs-To: https://github.com/hello-world/hello-world/\n" + """ + And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain: + """ + "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n" + """ + + Scenario: Sets custom header + When I run `wp scaffold plugin hello-world` + + When I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot --headers='{"X-Poedit-Basepath":".."}'` + And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain: + """ + "X-Poedit-Basepath: ..\n" + """ + Scenario: Sets the last translator and the language team When I run `wp scaffold plugin hello-world` diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 840b2553..66a1a5c7 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -53,6 +53,11 @@ class MakePotCommand extends WP_CLI_Command { */ protected $skip_js = false; + /** + * @var array + */ + protected $headers = []; + /** * @var string */ @@ -91,6 +96,9 @@ class MakePotCommand extends WP_CLI_Command { * By default, the following files and folders are ignored: node_modules, .git, .svn, .CVS, .hg, vendor. * Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`. * + * [--headers=] + * : Array in JSON format of custom headers which will be added to the POT file. Defaults to empty array. + * * [--skip-js] * : Skips JavaScript string extraction. Useful when this is done in another build step, e.g. through Babel. * @@ -102,9 +110,12 @@ class MakePotCommand extends WP_CLI_Command { * @when before_wp_load */ public function __invoke( $args, $assoc_args ) { - $this->source = realpath( $args[0] ); - $this->slug = Utils\get_flag_value( $assoc_args, 'slug', Utils\basename( $this->source ) ); - $this->skip_js = Utils\get_flag_value( $assoc_args, 'skip-js', $this->skip_js ); + $array_arguments = array( 'headers' ); + $assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments ); + $this->source = realpath( $args[0] ); + $this->slug = Utils\get_flag_value( $assoc_args, 'slug', Utils\basename( $this->source ) ); + $this->skip_js = Utils\get_flag_value( $assoc_args, 'skip-js', $this->skip_js ); + $this->headers = Utils\get_flag_value( $assoc_args, 'headers', $this->headers ); $ignore_domain = Utils\get_flag_value( $assoc_args, 'ignore-domain', false ); @@ -413,6 +424,10 @@ protected function set_default_headers() { $this->translations->setHeader( 'Report-Msgid-Bugs-To', $meta['msgid-bugs-address'] ); $this->translations->setHeader( 'Last-Translator', 'FULL NAME ' ); $this->translations->setHeader( 'Language-Team', 'LANGUAGE ' ); + + foreach( $this->headers as $key => $value ) { + $this->translations->setHeader( $key, $value ); + } } /**