1 <?php
2
3 4 5 6 7 8 9 10 11 12 13
14
15 namespace WC_POS;
16
17 class i18n {
18
19 private $github_url;
20
21 22 23
24 public function __construct() {
25
26
27
28
29 $this->github_url = 'https://raw.githubusercontent.com/kilbot/WooCommerce-POS-Language-Packs/master/';
30
31
32 $this->load_plugin_textdomain();
33 add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_check' ) );
34 add_filter( 'upgrader_pre_download', array( $this, 'upgrader_pre_download' ), 10, 3 );
35 add_filter( 'woocommerce_pos_enqueue_scripts', array( $this, 'js_locale' ) );
36 add_filter( 'woocommerce_pos_admin_enqueue_scripts', array( $this, 'js_locale' ) );
37
38
39 add_action( 'wp_ajax_wc_pos_update_translations', array( $this, 'update_translations' ) );
40 }
41
42 43 44
45 public function load_plugin_textdomain() {
46
47 $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-pos' );
48
49 load_textdomain( 'woocommerce-pos', \WP_LANG_DIR . '/woocommerce-pos/woocommerce-pos-' . $locale . '.mo' );
50 load_textdomain( 'woocommerce-pos', \WP_LANG_DIR . '/plugins/woocommerce-pos-' . $locale . '.mo' );
51
52
53 if ( ! is_admin() && is_pos() ) {
54 load_textdomain( 'woocommerce', \WP_LANG_DIR . '/woocommerce/woocommerce-admin-' . $locale . '.mo' );
55 load_textdomain( 'woocommerce', \WP_LANG_DIR . '/plugins/woocommerce-admin-' . $locale . '.mo' );
56 }
57
58 }
59
60 61 62 63 64 65 66
67 public function update_check( $transient, $force = false ) {
68 $locale = get_locale();
69
70
71
72
73 if ( empty( $transient->checked ) || strpos( $locale, 'en_' ) === 0 ) {
74 return $transient;
75 }
76
77
78 $request = wp_remote_get(
79 $this->github_url . 'package.json',
80 array( 'timeout' => 45 )
81 );
82
83 if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
84 return $transient;
85 }
86
87
88 $response = json_decode( wp_remote_retrieve_body( $request ) );
89 $transient = apply_filters( 'woocommerce_pos_language_packs_upgrade', $transient, $response, $this->github_url, $force );
90 if ( !isset( $response->locales->$locale ) ) {
91 return $transient;
92 }
93
94
95 $new = strtotime( $response->locales->$locale );
96 $options = get_option( 'woocommerce_pos_language_packs' );
97
98 if ( isset( $options[ $locale ] ) && $options[ $locale ] >= $new && !$force ) {
99 return $transient;
100 }
101
102
103 $transient->translations[] = array(
104 'type' => 'plugin',
105 'slug' => 'woocommerce-pos',
106 'language' => $locale,
107 'version' => VERSION,
108 'updated' => date( 'Y-m-d H:i:s', $new ),
109 'package' => $this->github_url . 'packages/woocommerce-pos-' . $locale . '.zip',
110 'autoupdate' => 1
111 );
112
113 return $transient;
114
115 }
116
117 118 119 120 121 122 123 124 125 126
127 public function upgrader_pre_download( $reply, $package, $upgrader ) {
128
129 if ( isset( $upgrader->skin->language_update )
130 && $upgrader->skin->language_update->slug == 'woocommerce-pos'
131 ) {
132
133 $options = get_option( 'woocommerce_pos_language_packs', array() );
134 $locale = get_locale();
135 $options[ $locale ] = current_time( 'timestamp' );
136 if ( !add_option( 'woocommerce_pos_language_packs', $options, '', 'no' ) ) {
137 update_option( 'woocommerce_pos_language_packs', $options );
138 }
139 }
140
141 return $reply;
142 }
143
144 145 146
147 public function update_translations() {
148
149 AJAX::check_ajax_referer();
150
151 header( "Content-Type: text/event-stream" );
152 header( "Cache-Control: no-cache" );
153 header( "Access-Control-Allow-Origin: *" );
154
155 echo ":" . str_repeat( " ", 2048 ) . \PHP_EOL;
156
157 $this->manual_update();
158
159 die();
160 }
161
162 163 164
165 public function manual_update() {
166 ob_start();
167 $locale = get_locale();
168 $creds = request_filesystem_credentials( $_GET[ 'security' ], '', false, false, null );
169
170
171 $this->flush( sprintf( __( 'Updating translations for %1$s (%2$s)…' ), 'WooCommerce POS', $locale ) );
172
173 $transient = (object)array( 'checked' => true );
174 $update = $this->update_check( $transient, true );
175
176 if ( empty( $update->translations ) ) {
177
178 $this->flush( 'No translations found for ' . $locale . '. <a href="mailto:support@woopos.com.au">Contact us</a> if you would like to help translate WooCommerce POS into your language.' );
179 $this->flush( 'complete' );
180
181 return;
182 }
183
184 if ( !$creds || !WP_Filesystem( $creds ) ) {
185
186 $this->flush( __( 'Translation update failed.' ) );
187 $this->flush( 'complete' );
188
189 return;
190 }
191
192 foreach ( $update->translations as $translation ) {
193
194
195 $this->flush( sprintf( __( 'Downloading translation from <span class="code">%s</span>…' ), $translation[ 'package' ] ) );
196
197 $response = wp_remote_get(
198 $translation[ 'package' ],
199 array( 'sslverify' => false, 'timeout' => 60, 'filename' => $locale . '.zip' )
200 );
201
202 if ( is_wp_error( $response ) || ( $response[ 'response' ][ 'code' ] < 200 || $response[ 'response' ][ 'code' ] >= 300 ) ) {
203
204 $this->flush( __( 'Translation update failed.' ) );
205 continue;
206 }
207
208 global $wp_filesystem;
209
210 $upload_dir = wp_upload_dir();
211 $file = trailingslashit( $upload_dir[ 'path' ] ) . $locale . '.zip';
212
213
214 if ( !$wp_filesystem->put_contents( $file, $response[ 'body' ], \FS_CHMOD_FILE ) ) {
215
216 $this->flush( __( 'Translation update failed.' ) );
217 continue;
218 }
219
220
221 $dir = trailingslashit( \WP_LANG_DIR ) . 'plugins/';
222 $unzip = unzip_file( $file, $dir );
223 if ( true !== $unzip ) {
224
225 $this->flush( __( 'Translation update failed.' ) );
226 continue;
227 }
228
229
230 $wp_filesystem->delete( $file );
231
232
233 $key = str_replace( '-', '_', $translation[ 'slug' ] ) . '_language_packs';
234 $options = get_option( $key, array() );
235 $options[ $locale ] = current_time( 'timestamp' );
236 if ( !add_option( $key, $options, '', 'no' ) ) {
237 update_option( $key, $options );
238 }
239
240
241 $this->flush( __( 'Translation updated successfully.' ) );
242
243 }
244
245 $this->flush( 'complete' );
246
247 return;
248
249 }
250
251 252 253 254 255
256 private function flush( $data ) {
257 echo 'data:' . $data . \PHP_EOL;
258 echo \PHP_EOL;
259 ob_flush();
260 flush();
261 }
262
263 264 265 266 267 268
269 public function js_locale( array $scripts ) {
270 $locale = apply_filters( 'plugin_locale', get_locale(), PLUGIN_NAME );
271 $dir = PLUGIN_PATH . 'languages/js/';
272 $url = PLUGIN_URL . 'languages/js/';
273 list( $country ) = explode( '_', $locale );
274
275 if ( is_readable( $dir . $locale . '.js' ) ) {
276 $scripts[ 'locale' ] = $url . $locale . '.js';
277 } elseif ( is_readable( $dir . $country . '.js' ) ) {
278 $scripts[ 'locale' ] = $url . $country . '.js';
279 }
280
281 return $scripts;
282 }
283
284 285 286 287 288 289
290 static public function currency_denominations( $code = '' ) {
291 if ( !$code ) {
292 $code = get_woocommerce_currency();
293 }
294 $denominations = json_decode( file_get_contents( PLUGIN_PATH . 'includes/denominations.json' ) );
295
296 return isset( $denominations->$code ) ? $denominations->$code : $denominations;
297 }
298
299 }