1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Jetpack i18n handler. |
5
|
|
|
* |
6
|
|
|
* Takes care of things that are not yet taken care of by the WordPress.org i18n |
7
|
|
|
* infrastructure, like JavaScript language packs. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
defined( 'ABSPATH' ) or die( 'No direct access.' ); |
|
|
|
|
11
|
|
|
|
12
|
|
|
// If the i18n directory and url aren't defined by config, use the default. |
13
|
|
|
if ( ! defined( 'JETPACK_I18N_DIR' ) ) { |
14
|
|
|
define( 'JETPACK_I18N_DIR', WP_CONTENT_DIR . '/jetpack-i18n' ); |
15
|
|
|
} |
16
|
|
|
if ( ! defined( 'JETPACK_I18N_URL' ) ) { |
17
|
|
|
define( 'JETPACK_I18N_URL', WP_CONTENT_URL . '/jetpack-i18n' ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class Jetpack_I18n { |
21
|
|
|
|
22
|
|
|
public static function activate() { |
23
|
|
|
if ( ! file_exists( JETPACK_I18N_DIR ) ) { |
24
|
|
|
wp_mkdir_p( JETPACK_I18N_DIR, 0660 ); |
25
|
|
|
} else if ( ! is_dir( JETPACK_I18N_DIR ) ) { |
26
|
|
|
error_log( 'Expected ' . JETPACK_I18N_DIR . ' to be a directory.' ); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function deactivate() { |
31
|
|
|
if ( is_dir( JETPACK_I18N_DIR ) ) { |
32
|
|
|
|
33
|
|
|
// Delete the i18n files. |
34
|
|
|
$file_mask = 'jetpack-*.js'; |
35
|
|
|
$files = glob( trailingslashit( JETPACK_I18N_DIR ) . $file_mask ); |
36
|
|
|
foreach ( $files as $file ) { |
37
|
|
|
unlink( $file ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Remove the directory. |
41
|
|
|
rmdir( JETPACK_I18N_DIR ); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Queues PO to JSON conversion when needed. |
47
|
|
|
*/ |
48
|
|
|
public function maybe_generate_translation_files() { |
49
|
|
|
$next_event = wp_next_scheduled( |
50
|
|
|
'jetpack_generate_translation_files', |
51
|
|
|
array( get_locale() ) |
52
|
|
|
); |
53
|
|
|
$po_file = $this->get_po_file_path( get_locale() ); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// We can only do conversion if the PO file exists |
56
|
|
|
// and we don't want to queue this up twice. |
57
|
|
|
if ( file_exists( $po_file ) && ( ! $next_event || $next_event < time() ) ) { |
58
|
|
|
$translation_info = wp_get_pomo_file_data( $po_file ); |
59
|
|
|
$revision = strtotime( $translation_info['PO-Revision-Date'] ); |
60
|
|
|
$js_file = $this->get_i18n_js_file_path( get_locale() ); |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* There are 2 case where we'd want to do a conversion; |
64
|
|
|
* - if the JSON file does not exist |
65
|
|
|
* - if the JSON file is out of date |
66
|
|
|
*/ |
67
|
|
|
if ( |
68
|
|
|
! file_exists( $js_file ) |
69
|
|
|
|| $revision > get_option( 'jetpack_i18n_revision_' . get_locale(), 0 ) |
70
|
|
|
) { |
71
|
|
|
wp_schedule_single_event( |
72
|
|
|
time() + 10, |
73
|
|
|
'jetpack_generate_translation_files', |
74
|
|
|
array( get_locale() ) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Generates the translation files for a locale. |
82
|
|
|
* @param string $locale |
83
|
|
|
*/ |
84
|
|
|
public function generate_translation_files( $locale = '' ) { |
85
|
|
|
$locale = $locale ? $locale : get_locale(); |
86
|
|
|
$po_file = $this->get_po_file_path( $locale ); |
|
|
|
|
87
|
|
|
$js_file = $this->get_i18n_js_file_path( $locale ); |
|
|
|
|
88
|
|
|
$translation_info = wp_get_pomo_file_data( $po_file ); |
89
|
|
|
$revision = strtotime( $translation_info['PO-Revision-Date'] ); |
90
|
|
|
|
91
|
|
|
// Parse PO file |
92
|
|
|
$po_data = $this->parse_po_file( $po_file ); |
|
|
|
|
93
|
|
|
|
94
|
|
|
// Convert entries to JSON |
95
|
|
|
$json = $this->po2json( |
|
|
|
|
96
|
|
|
$po_data['headers'], |
97
|
|
|
$po_data['entries'], |
98
|
|
|
'jetpack' |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// Write to file |
102
|
|
|
$this->create_js_language_file( $json, $js_file ); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Record the revision and locale |
105
|
|
|
update_option( 'jetpack_i18n_revision_' . $locale, $revision ); |
106
|
|
|
wp_clear_scheduled_hook( |
107
|
|
|
'jetpack_generate_translation_files', |
108
|
|
|
array( $locale ) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.