-
Notifications
You must be signed in to change notification settings - Fork 52
/
WordPressCodeExtractor.php
136 lines (112 loc) · 3.82 KB
/
WordPressCodeExtractor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace WP_CLI\I18n;
use Gettext\Extractors\PhpCode;
use Gettext\Translation;
use Gettext\Translations;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use WP_CLI;
class WordPressCodeExtractor extends PhpCode {
protected static $dir = '';
public static $options = [
'extractComments' => [ 'translators', 'Translators' ],
'constants' => [],
'functions' => [
'__' => 'text_domain',
'esc_attr__' => 'text_domain',
'esc_html__' => 'text_domain',
'_e' => 'text_domain',
'esc_attr_e' => 'text_domain',
'esc_html_e' => 'text_domain',
'_x' => 'text_context_domain',
'_ex' => 'text_context_domain',
'esc_attr_x' => 'text_context_domain',
'esc_html_x' => 'text_context_domain',
'_n' => 'single_plural_number_domain',
'_nx' => 'single_plural_number_context_domain',
'_n_noop' => 'single_plural_domain',
'_nx_noop' => 'single_plural_context_domain',
// Compat.
'_' => 'gettext', // Same as 'text_domain'.
// Deprecated.
'_c' => 'text_domain',
'_nc' => 'single_plural_number_domain',
'__ngettext' => 'single_plural_number_domain',
'__ngettext_noop' => 'single_plural_domain',
],
];
/**
* {@inheritdoc}
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
$options += static::$options;
$functions = new WordPressFunctionsScanner( $string );
if ( $options['extractComments'] !== false ) {
$functions->enableCommentsExtraction( $options['extractComments'] );
}
$functions->saveGettextFunctions( $translations, $options );
}
/**
* {@inheritdoc}
*/
public static function fromFile( $file, Translations $translations, array $options = [] ) {
foreach ( self::getFiles( $file ) as $f ) {
// Make sure a relative file path is added as a comment.
$options['file'] = ltrim( str_replace( static::$dir, '', $f ), '/' );
$string = self::readFile( $f );
if ( $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 );
}
}
/**
* Recursively extracts the translations from a directory.
*
* @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
*/
public static function fromDirectory( $dir, Translations $translations, array $options = [] ) {
static::$dir = $dir;
$files = static::getFilesFromDirectory( $dir );
try {
if ( ! empty( $files ) ) {
static::fromFile( $files, $translations, $options );
}
} catch ( \Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
static::$dir = '';
}
/**
* Recursively gets all PHP files within a directory.
*
* @param string $dir A path of a directory.
*
* @return array File list.
*/
protected static function getFilesFromDirectory( $dir ) {
$filtered_files = [];
try {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::CHILD_FIRST
);
/** @var \DirectoryIterator $file */
foreach ( $files as $file ) {
if ( $file->isFile() && 'php' === $file->getExtension()) {
$filtered_files[] = $file->getPathname();
}
}
} catch ( \Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
return $filtered_files;
}
}