-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathbuild-module-headings-translations.php
92 lines (80 loc) · 2.74 KB
/
build-module-headings-translations.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
<?php
$file_contents = "<?php
// Do not edit this file. It's generated by `jetpack/tools/build-module-headings-translations.php`
/**
* For a given module, return an array with translated name, description and recommended description.
*
* @param string \$key Module file name without .php
*
* @return array
*/
function jetpack_get_module_i18n( \$key ) {
\tstatic \$modules;
\tif ( ! isset( \$modules ) ) {
\t\t\$modules = array(";
$jp_dir = dirname( dirname( __FILE__ ) ) . '/';
$files = glob( "{$jp_dir}modules/*.php" );
$tags = array(
'Other' => array(),
);
foreach ( $files as $file ) {
$absolute_path = $file;
$relative_path = str_replace( $jp_dir, '', $file );
$_file_contents = '';
$file = fopen( $absolute_path, 'r' );
$file_data = fread( $file, 8192 );
fclose( $file );
// Make sure we catch CR-only line endings.
$file_data = str_replace( "\r", "\n", $file_data );
$all_headers = array(
'name' => 'Module Name',
'description' => 'Module Description',
'recommended description' => 'Jumpstart Description',
'tags' => 'Module Tags',
);
foreach ( $all_headers as $field => $regex ) {
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
$string = trim( preg_replace( "/\s*(?:\*\/|\?>).*/", '', $match[1] ) );
$string = addcslashes( $string, "''" );
if ( 'Module Tags' === $regex ) {
$module_tags = array_map( 'trim', explode( ',', $string ) );
foreach ( $module_tags as $tag ) {
$tags[ $tag ][] = $relative_path;
}
} else {
$_file_contents .= "\t\t\t\t'{$field}' => _x( '{$string}', '{$regex}', 'jetpack' ),\n";
}
}
}
if ( $_file_contents ) {
$file_contents .= "\n\t\t\t'" . str_replace( '.php', '', basename( $absolute_path ) ) . "' => array(\n$_file_contents\t\t\t),\n";
}
}
$file_contents .= "\t\t);
\t}";
$file_contents .= "\n\treturn \$modules[ \$key ];
}";
$file_contents .= "
/**
* For a given module tag, return its translated version.
*
* @param string \$key Module tag as is in each module heading.
*
* @return string
*/";
$file_contents .= "\nfunction jetpack_get_module_i18n_tag( \$key ) {
\tstatic \$module_tags;
\tif ( ! isset( \$module_tags ) ) {";
$file_contents .= "\n\t\t\$module_tags = array(";
foreach ( $tags as $tag => $files ) {
$file_contents .= "\n\t\t\t// Modules with `{$tag}` tag:\n";
foreach ( $files as $file ) {
$file_contents .= "\t\t\t// - {$file}\n";
}
$file_contents .= "\t\t\t'{$tag}' =>_x( '{$tag}', 'Module Tag', 'jetpack' ),\n";
}
$file_contents .= "\t\t);
\t}";
$file_contents .= "\n\treturn ! empty( \$module_tags[ \$key ] ) ? \$module_tags[ \$key ] : '';
}\n";
file_put_contents( "{$jp_dir}modules/module-headings.php", $file_contents );