PluginProbe ʕ •ᴥ•ʔ
Security Optimizer – The All-In-One Protection Plugin / 1.6.2
Security Optimizer – The All-In-One Protection Plugin v1.6.2
1.6.3 1.6.2 1.6.1 trunk 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0
sg-security / vendor / siteground / siteground-i18n / src / i18n_Service.php
sg-security / vendor / siteground / siteground-i18n / src Last commit date
i18n_Service.php 1 month ago
i18n_Service.php
160 lines
1 <?php
2 /**
3 * SiteGround i18n Service.
4 */
5
6 namespace SiteGround_i18n;
7
8 use CharlesRumley\PoToJson;
9 use SiteGround_Helper\Helper_Service;
10
11 /**
12 * SiteGround_i18n_Service class.
13 */
14 class i18n_Service {
15
16 /**
17 * Variable holding the text domain.
18 *
19 * @var string
20 */
21 public $sg_textdomain;
22
23 /**
24 * Variable holding the plugin folder.
25 *
26 * @var string
27 */
28 public $folder;
29
30 /**
31 * Class construct.
32 *
33 * @since 1.0.0
34 *
35 * @param string $textdomain The text domain that will be used for the instance.
36 * @param string $folder The folder that will be used for the instance.
37 */
38 public function __construct( $textdomain, $folder = '' ) {
39 $this->sg_textdomain = $textdomain;
40 $this->folder = empty( $folder ) ? $textdomain : $folder;
41 }
42
43 /**
44 * Load the plugin textdomain.
45 *
46 * @since 1.0.0
47 */
48 public function load_textdomain() {
49 load_plugin_textdomain(
50 $this->sg_textdomain,
51 false,
52 $this->folder . '/languages'
53 );
54 }
55
56 /**
57 * Update json translations on translations update.
58 *
59 * @since 1.0.0
60 *
61 * @param {WP_Upgrader} $upgrader WP_Upgrader instance.
62 * @param array $extra Array of bulk item update data.
63 */
64 public function update_json_translations( $upgrader, $extra ) {
65 // Bail if we don't update the translations.
66 if (
67 'update' !== $extra['action'] &&
68 'translation' !== $extra['type']
69 ) {
70 return;
71 }
72
73 // Bail if there are no translations.
74 if ( empty( $extra['translations'] ) ) {
75 return;
76 }
77
78 // Check for SiteGround Optimizer translations.
79 $keys = array_keys( array_column( $extra['translations'], 'slug' ), $this->sg_textdomain );
80
81 // Bail if there are no plugin translations.
82 if ( empty( $keys ) ) {
83 return;
84 }
85
86 // Setup the WP Filesystem.
87 $wp_filesystem = Helper_Service::setup_wp_filesystem();
88
89 // Init the convertor class.
90 $po_to_json = new PoToJson();
91
92 foreach ( $keys as $key ) {
93 // Convert a PO file to Jed-compatible JSON.
94 $json = $po_to_json
95 ->withPoFile( WP_CONTENT_DIR . '/languages/plugins/' . $this->folder . '-' . $extra['translations'][ $key ]['language'] . '.po' )
96 ->toJedJson( false, $this->sg_textdomain );
97
98 // Convert and get the json content.
99 $content = json_decode( $json, true );
100
101 // Build the json filepath.
102 $json_filepath = WP_CONTENT_DIR . '/languages/plugins/' . $this->folder . '-' . $extra['translations'][ $key ]['language'] . '.json';
103
104 // Create the file if donesn't exists.
105 if ( ! is_file( $json_filepath ) ) {
106 // Create the new file.
107 $wp_filesystem->touch( $json_filepath );
108 }
109
110 // Add the translations to the file.
111 $wp_filesystem->put_contents(
112 $json_filepath,
113 json_encode( $content['locale_data'][ $this->sg_textdomain ] )
114 );
115 }
116 }
117
118 /**
119 * Get i18n strings as a JSON-encoded string
120 *
121 * @since 1.0.0
122 *
123 * @return string The locale as JSON
124 */
125 public function get_i18n_data_json() {
126 // Get the user locale.
127 $locale = get_user_locale();
128
129 // Possible langugaes paths.
130 $dirs = array(
131 'wp-content/languages/plugins/',
132 'wp-content/plugins/' . $this->folder . '/languages/json/',
133 );
134
135 foreach ( $dirs as $dir ) {
136 // Build the full path to the file.
137 $i18n_json = ABSPATH . $dir . $this->sg_textdomain . '-' . $locale . '.json';
138
139 // Check if the files exists and it's readable.
140 if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
141 // Get the locale data.
142 $locale_data = @file_get_contents( $i18n_json );
143 if ( $locale_data ) {
144 return $locale_data;
145 }
146 }
147 }
148
149 // Return valid empty Jed locale.
150 return json_encode(
151 array(
152 '' => array(
153 'domain' => $this->sg_textdomain,
154 'lang' => is_admin() ? get_user_locale() : get_locale(),
155 ),
156 )
157 );
158 }
159 }
160