-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 53-generator
- Loading branch information
Showing
6 changed files
with
202 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
namespace WP_CLI\I18n; | ||
|
||
use Gettext\Translation; | ||
use Gettext\Translations; | ||
use RecursiveCallbackFilterIterator; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use DirectoryIterator; | ||
use WP_CLI; | ||
|
||
trait IterableCodeExtractor { | ||
|
||
private static $dir = ''; | ||
|
||
/** | ||
* Extract the translations from a file. | ||
* | ||
* @param array|string $file A path of a file or files | ||
* @param Translations $translations The translations instance to append the new translations. | ||
* @param array $options { | ||
* Optional. An array of options passed down to static::fromString() | ||
* | ||
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'. | ||
* } | ||
* @return null | ||
*/ | ||
public static function fromFile( $file, Translations $translations, array $options = [] ) { | ||
foreach ( static::getFiles( $file ) as $f ) { | ||
// Make sure a relative file path is added as a comment. | ||
$options['file'] = ltrim( str_replace( static::$dir, '', $f ), '/' ); | ||
|
||
$string = file_get_contents( $f ); | ||
|
||
if ( ! $string ) { | ||
WP_CLI::debug( | ||
sprintf( | ||
'Could not load file %1s', | ||
$f | ||
) | ||
); | ||
|
||
continue; | ||
} | ||
|
||
if ( ! empty ( $options['wpExtractTemplates'] ) ) { | ||
$headers = MakePotCommand::get_file_data_from_string( $string, [ 'Template Name' => 'Template Name' ] ); | ||
|
||
if ( ! empty( $headers[ 'Template Name'])) { | ||
$translation = new Translation( '', $headers[ 'Template Name'] ); | ||
$translation->addExtractedComment('Template Name of the theme' ); | ||
|
||
$translations[] = $translation; | ||
} | ||
} | ||
|
||
static::fromString( $string, $translations, $options ); | ||
} | ||
} | ||
|
||
/** | ||
* Extract the translations from a file. | ||
* | ||
* @param string $dir Root path to start the recursive traversal in. | ||
* @param Translations $translations The translations instance to append the new translations. | ||
* @param array $options { | ||
* Optional. An array of options passed down to static::fromString() | ||
* | ||
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'. | ||
* @type array $exclude A list of path to exclude. Default []. | ||
* @type array $extensions A list of extensions to process. Default []. | ||
* } | ||
* @return null | ||
*/ | ||
public static function fromDirectory( $dir, Translations $translations, array $options = [] ) { | ||
static::$dir = $dir; | ||
|
||
$files = static::getFilesFromDirectory( $dir, isset( $options['exclude'] ) ? $options['exclude'] : [], $options['extensions'] ); | ||
|
||
if ( ! empty( $files ) ) { | ||
static::fromFile( $files, $translations, $options ); | ||
} | ||
|
||
static::$dir = ''; | ||
} | ||
|
||
/** | ||
* Recursively gets all PHP files within a directory. | ||
* | ||
* @param string $dir A path of a directory. | ||
* @param array $exclude List of files and directories to skip. | ||
* @param array $extensions List of filename extensions to process. | ||
* | ||
* @return array File list. | ||
*/ | ||
public static function getFilesFromDirectory( $dir, array $exclude = [], $extensions = [] ) { | ||
$filtered_files = []; | ||
|
||
$files = new RecursiveIteratorIterator( | ||
new RecursiveCallbackFilterIterator( | ||
new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ), | ||
function ( $file, $key, $iterator ) use ( $exclude, $extensions ) { | ||
/** @var DirectoryIterator $file */ | ||
if ( in_array( $file->getBasename(), $exclude, true ) ) { | ||
return false; | ||
} | ||
|
||
// Check for more complex paths, e.g. /some/sub/folder. | ||
foreach( $exclude as $path_or_file ) { | ||
if ( false !== mb_ereg( preg_quote( '/' . $path_or_file ) . '$', $file->getPathname() ) ) { | ||
return false; | ||
} | ||
} | ||
|
||
/** @var RecursiveCallbackFilterIterator $iterator */ | ||
if ( $iterator->hasChildren() ) { | ||
return true; | ||
} | ||
|
||
return ( $file->isFile() && in_array( $file->getExtension(), $extensions, TRUE ) ); | ||
} | ||
), | ||
RecursiveIteratorIterator::CHILD_FIRST | ||
); | ||
|
||
foreach ( $files as $file ) { | ||
/** @var DirectoryIterator $file */ | ||
if ( ! $file->isFile() || ! in_array( $file->getExtension(), $extensions, TRUE ) ) { | ||
continue; | ||
} | ||
|
||
$filtered_files[] = $file->getPathname(); | ||
} | ||
|
||
return $filtered_files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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=<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 ); | ||
|
||
|
@@ -326,6 +337,7 @@ protected function makepot() { | |
// Extract 'Template Name' headers in theme files. | ||
'wpExtractTemplates' => isset( $file_data['Theme Name'] ), | ||
'exclude' => $this->exclude, | ||
'extensions' => [ 'php' ], | ||
] ); | ||
|
||
if ( ! $this->skip_js ) { | ||
|
@@ -334,6 +346,7 @@ protected function makepot() { | |
$this->translations, | ||
[ | ||
'exclude' => $this->exclude, | ||
'extensions' => [ 'js' ], | ||
] | ||
); | ||
} | ||
|
@@ -414,6 +427,10 @@ protected function set_default_headers() { | |
$this->translations->setHeader( 'Last-Translator', 'FULL NAME <EMAIL@ADDRESS>' ); | ||
$this->translations->setHeader( 'Language-Team', 'LANGUAGE <[email protected]>' ); | ||
$this->translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION ); | ||
|
||
foreach( $this->headers as $key => $value ) { | ||
$this->translations->setHeader( $key, $value ); | ||
} | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.