Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
  1 <?php
  2 
  3 /**
  4  * Define the internationalization functionality.
  5  *
  6  * Loads and defines the internationalization files for this plugin
  7  * so that its ready for translation.
  8  *
  9  * @class     WC_POS_i18n
 10  * @package   WooCommerce POS
 11  * @author    Paul Kilmurray <paul@kilbot.com.au>
 12  * @link      http://www.woopos.com.au
 13  */
 14 
 15 namespace WC_POS;
 16 
 17 class i18n {
 18 
 19   private $github_url;
 20 
 21   /**
 22    * Constructor
 23    */
 24   public function __construct() {
 25 
 26     // raw github url for language packs
 27     // todo: use last commit info and switch to cdn
 28     //    $this->github_url = 'https://cdn.rawgit.com/kilbot/WooCommerce-POS-Language-Packs/master/';
 29     $this->github_url = 'https://raw.githubusercontent.com/kilbot/WooCommerce-POS-Language-Packs/master/';
 30 
 31     //    add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
 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     // ajax
 39     add_action( 'wp_ajax_wc_pos_update_translations', array( $this, 'update_translations' ) );
 40   }
 41 
 42   /**
 43    * Load the plugin text domain for translation.
 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     // load woocommerce admin translations
 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    * Check GitHub repo for updated language packs
 62    *
 63    * @param      $transient
 64    * @param bool $force
 65    * @return mixed
 66    */
 67   public function update_check( $transient, $force = false ) {
 68     $locale = get_locale();
 69 
 70     // pre_set_site_transient_update_plugins is called twice
 71     // we only want to act on the second run
 72     // also only continue for non English locales
 73     if ( empty( $transient->checked ) || strpos( $locale, 'en_' ) === 0 ) {
 74       return $transient;
 75     }
 76 
 77     // get package.json from github
 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     // see if translation pack exists
 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     // compare
 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     // update required
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    * Update the database with new language pack date
119    * TODO: is there no later hook for translation install?
120    *
121    * @param $reply
122    * @param $package
123    * @param $upgrader
124    *
125    * @return mixed
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    * Force update translations from AJAX
146    */
147   public function update_translations() {
148     // security
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; // 2 kB padding for IE
156 
157     $this->manual_update();
158 
159     die();
160   }
161 
162   /**
163    * Force update translations
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     /* translators: wordpress */
171     $this->flush( sprintf( __( 'Updating translations for %1$s (%2$s)&#8230;' ), 'WooCommerce POS', $locale ) );
172 
173     $transient = (object)array( 'checked' => true );
174     $update = $this->update_check( $transient, true );
175 
176     if ( empty( $update->translations ) ) {
177       /* note: no translation exists */
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       /* translators: wordpress */
186       $this->flush( __( 'Translation update failed.' ) );
187       $this->flush( 'complete' );
188 
189       return;
190     }
191 
192     foreach ( $update->translations as $translation ) {
193 
194       /* translators: wordpress */
195       $this->flush( sprintf( __( 'Downloading translation from <span class="code">%s</span>&#8230;' ), $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         /* translators: wordpress */
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       // Save the zip file
214       if ( !$wp_filesystem->put_contents( $file, $response[ 'body' ], \FS_CHMOD_FILE ) ) {
215         /* translators: wordpress */
216         $this->flush( __( 'Translation update failed.' ) );
217         continue;
218       }
219 
220       // Unzip the file to wp-content/languages/plugins directory
221       $dir = trailingslashit( \WP_LANG_DIR ) . 'plugins/';
222       $unzip = unzip_file( $file, $dir );
223       if ( true !== $unzip ) {
224         /* translators: wordpress */
225         $this->flush( __( 'Translation update failed.' ) );
226         continue;
227       }
228 
229       // Delete the package file
230       $wp_filesystem->delete( $file );
231 
232       // Update options timestamp
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       /* translators: wordpress */
241       $this->flush( __( 'Translation updated successfully.' ) );
242 
243     }
244 
245     $this->flush( 'complete' );
246 
247     return;
248 
249   }
250 
251   /**
252    * Flush output
253    *
254    * @param $data
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    * Load translations for js plugins
265    *
266    * @param $scripts
267    * @return string
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    * Return currency denomination for a given country code
286    *
287    * @param string $code
288    * @return array
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 }
API documentation generated by ApiGen