Completed
Push — enhance/js-i18n ( 86883a )
by
unknown
12:34
created

class.jetpack-i18n.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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.' );
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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() );
0 ignored issues
show
The method get_po_file_path() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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() );
0 ignored issues
show
The method get_i18n_js_file_path() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 );
0 ignored issues
show
The method get_po_file_path() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
		$js_file          = $this->get_i18n_js_file_path( $locale );
0 ignored issues
show
The method get_i18n_js_file_path() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 );
0 ignored issues
show
The method parse_po_file() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
93
94
		// Convert entries to JSON
95
		$json      = $this->po2json(
0 ignored issues
show
The method po2json() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Equals sign not aligned correctly; expected 1 space but found 6 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
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 );
0 ignored issues
show
The method create_js_language_file() does not seem to exist on object<Jetpack_I18n>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}