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

Remove project header comments for comments audit #123

Merged
merged 1 commit into from Jan 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion features/makepot.feature
Expand Up @@ -615,7 +615,7 @@ Feature: Generate a POT file of a WordPress project
__( 'Hello World', 'foo-plugin' );
"""

When I try `wp i18n make-pot foo-plugin --debug`
When I try `wp i18n make-pot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Expand All @@ -626,6 +626,39 @@ Feature: Generate a POT file of a WordPress project
Warning: The string "Hello World" has 2 different translator comments. (foo-plugin.php:7)
"""

Scenario: Does not print a warning for translator comments clashing with meta data
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Plugin name
* Plugin URI: https://example.com
* Author URI: https://example.com
*/

/* translators: This is a comment */
__( 'Plugin name', 'foo-plugin' );

/* Translators: This is another comment! */
__( 'https://example.com', 'foo-plugin' );
"""

When I try `wp i18n make-pot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And STDERR should not contain:
"""
The string "Plugin name" has 2 different translator comments.
"""
And STDERR should not contain:
"""
The string "https://example.com" has 3 different translator comments.
"""

Scenario: Prints a warning for strings without translatable content
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
Expand Down
51 changes: 40 additions & 11 deletions src/MakePotCommand.php
Expand Up @@ -83,6 +83,11 @@ class MakePotCommand extends WP_CLI_Command {
*/
protected $file_comment;

/**
* @var string
*/
protected $project_type = 'generic';
schlessera marked this conversation as resolved.
Show resolved Hide resolved

/**
* These Regexes copied from http://php.net/manual/en/function.sprintf.php#93552
* and adjusted for better precision and updated specs.
Expand Down Expand Up @@ -167,16 +172,16 @@ class MakePotCommand extends WP_CLI_Command {
* [--include=<paths>]
* : Comma-separated list of files and paths that should be used for string extraction.
* If provided, only these files and folders will be taken into account for string extraction.
* For example, `--include="src,my-file.php` will ignore anything besides `my-file.php` and files in the `src` directory.
* Simple glob patterns can be used, i.e. `--include=foo-*.php` includes any PHP file with the `foo-` prefix.
* Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`.
* For example, `--include="src,my-file.php` will ignore anything besides `my-file.php` and files in the `src`
* directory. Simple glob patterns can be used, i.e. `--include=foo-*.php` includes any PHP file with the `foo-`
* prefix. Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`.
*
* [--exclude=<paths>]
* : Comma-separated list of files and paths that should be skipped for string extraction.
* For example, `--exclude=".github,myfile.php` would ignore any strings found within `myfile.php` or the `.github` folder.
* Simple glob patterns can be used, i.e. `--exclude=foo-*.php` excludes any PHP file with the `foo-` prefix.
* Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`.
* The following files and folders are always excluded: node_modules, .git, .svn, .CVS, .hg, vendor, *.min.js.
* For example, `--exclude=".github,myfile.php` would ignore any strings found within `myfile.php` or the `.github`
* folder. Simple glob patterns can be used, i.e. `--exclude=foo-*.php` excludes any PHP file with the `foo-`
* prefix. Leading and trailing slashes are ignored, i.e. `/my/directory/` is the same as `my/directory`. The
* following files and folders are always excluded: node_modules, .git, .svn, .CVS, .hg, vendor, *.min.js.
*
* [--headers=<headers>]
* : Array in JSON format of custom headers which will be added to the POT file. Defaults to empty array.
Expand All @@ -185,7 +190,8 @@ class MakePotCommand extends WP_CLI_Command {
* : Skips JavaScript string extraction. Useful when this is done in another build step, e.g. through Babel.
*
* [--skip-audit]
* : Skips string audit where it tries to find possible mistakes in translatable strings. Useful when running in an automated environment.
* : Skips string audit where it tries to find possible mistakes in translatable strings. Useful when running in an
* automated environment.
*
* [--file-comment=<file-comment>]
* : String that should be added as a comment to the top of the resulting POT file.
Expand All @@ -196,7 +202,8 @@ class MakePotCommand extends WP_CLI_Command {
* This file is distributed under the same license as the Example Plugin package.
* ```
*
* If a plugin or theme specifies a license in their main plugin file or stylesheet, the comment looks like this:
* If a plugin or theme specifies a license in their main plugin file or stylesheet, the comment looks like
* this:
*
* ```
* Copyright (C) 2018 Example Plugin Author
Expand All @@ -213,7 +220,8 @@ class MakePotCommand extends WP_CLI_Command {
* $ wp i18n make-pot . languages/my-plugin.pot
*
* # Create a POT file for the continents and cities list in WordPress core.
* $ wp i18n make-pot . continents-and-cities.pot --include="wp-admin/includes/continents-cities.php" --ignore-domain
* $ wp i18n make-pot . continents-and-cities.pot --include="wp-admin/includes/continents-cities.php"
* --ignore-domain
*
* @when before_wp_load
*
Expand Down Expand Up @@ -417,6 +425,8 @@ protected function get_main_file_data() {
WP_CLI::log( 'Theme stylesheet detected.' );
WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $stylesheet ), 'make-pot' );

$this->project_type = 'theme';

return $theme_data;
}
}
Expand All @@ -440,6 +450,8 @@ protected function get_main_file_data() {
WP_CLI::log( 'Plugin file detected.' );
WP_CLI::debug( sprintf( 'Plugin file: %s', $plugin_file ), 'make-pot' );

$this->project_type = 'plugin';

return $plugin_data;
}
}
Expand Down Expand Up @@ -615,7 +627,24 @@ protected function audit_strings( $translations ) {

// Check 2: Flag strings with different translator comments.
if ( $translation->hasExtractedComments() ) {
$comments = $translation->getExtractedComments();
$comments = $translation->getExtractedComments();

// Remove plugin header information from comments.
$comments = array_filter(
$comments,
function ( $comment ) {
/** @var string $comment */
/** @var string $file_header */
foreach ( $this->get_file_headers( $this->project_type ) as $file_header ) {
if ( 0 === strpos( $comment, $file_header ) ) {
return null;
}
}

return $comment;
}
);

$comments_count = count( $comments );

if ( $comments_count > 1 ) {
Expand Down