2014-05-28 16:06:00 +00:00
|
|
|
<?php
|
2014-09-20 18:51:01 +00:00
|
|
|
|
2014-05-28 16:06:00 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Language Pack Upgrader class
|
|
|
|
*
|
|
|
|
* Downloads the last language pack.
|
|
|
|
*
|
|
|
|
* @class WC_Language_Pack_Upgrader
|
|
|
|
* @version 2.2.0
|
|
|
|
* @package WooCommerce/Classes/Language
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Language_Pack_Upgrader {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Languages repository
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-05-29 20:05:38 +00:00
|
|
|
protected $repo = 'https://github.com/woothemes/woocommerce-language-packs/raw/v';
|
2014-05-28 16:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the language pack upgrader
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) );
|
2014-09-08 15:07:47 +00:00
|
|
|
add_filter( 'upgrader_pre_download', array( $this, 'version_update' ), 10, 2 );
|
2015-02-11 14:38:52 +00:00
|
|
|
add_action( 'woocommerce_installed', array( $this, 'has_available_update' ) );
|
2015-04-29 12:05:02 +00:00
|
|
|
add_action( 'update_option_WPLANG', array( $this, 'updated_language_option' ), 10, 2 );
|
2014-09-12 21:55:50 +00:00
|
|
|
add_filter( 'admin_init', array( $this, 'manual_language_update' ), 999 );
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get language package URI.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-29 12:05:02 +00:00
|
|
|
public function get_language_package_uri( $locale = null ) {
|
|
|
|
if ( is_null( $locale ) ) {
|
|
|
|
$locale = get_locale();
|
|
|
|
}
|
|
|
|
return $this->repo . WC_VERSION . '/packages/' . $locale . '.zip';
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for language updates
|
|
|
|
*
|
|
|
|
* @param object $data Transient update data
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function check_for_update( $data ) {
|
|
|
|
if ( $this->has_available_update() ) {
|
|
|
|
$data->translations[] = array(
|
|
|
|
'type' => 'plugin',
|
|
|
|
'slug' => 'woocommerce',
|
2014-09-04 10:33:30 +00:00
|
|
|
'language' => get_locale(),
|
2014-05-28 16:06:00 +00:00
|
|
|
'version' => WC_VERSION,
|
|
|
|
'updated' => date( 'Y-m-d H:i:s' ),
|
|
|
|
'package' => $this->get_language_package_uri(),
|
|
|
|
'autoupdate' => 1
|
|
|
|
);
|
|
|
|
}
|
2014-09-12 21:55:50 +00:00
|
|
|
|
2014-05-28 16:06:00 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:05:02 +00:00
|
|
|
/**
|
|
|
|
* Triggered when WPLANG is changed
|
|
|
|
* @param string $old
|
|
|
|
* @param string $new
|
|
|
|
*/
|
|
|
|
public function updated_language_option( $old, $new ) {
|
|
|
|
$this->has_available_update( $new );
|
|
|
|
}
|
|
|
|
|
2014-05-28 16:06:00 +00:00
|
|
|
/**
|
|
|
|
* Check if has available translation update
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-04-29 12:05:02 +00:00
|
|
|
public function has_available_update( $locale = null ) {
|
|
|
|
if ( is_null( $locale ) ) {
|
|
|
|
$locale = get_locale();
|
|
|
|
}
|
2014-05-28 16:06:00 +00:00
|
|
|
|
2015-04-29 12:05:02 +00:00
|
|
|
if ( 'en_US' === $locale ) {
|
2015-03-02 16:49:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:49:59 +00:00
|
|
|
$version = get_option( 'woocommerce_language_pack_version', array( '0', $locale ) );
|
|
|
|
|
2015-03-02 16:49:27 +00:00
|
|
|
if ( ! is_array( $version ) || version_compare( $version[0], WC_VERSION, '<' ) || $version[1] !== $locale ) {
|
2015-04-29 12:05:02 +00:00
|
|
|
if ( $this->check_if_language_pack_exists( $locale ) ) {
|
2014-06-04 16:01:38 +00:00
|
|
|
$this->configure_woocommerce_upgrade_notice();
|
2014-05-28 16:06:00 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Updated the woocommerce_language_pack_version to avoid searching translations for this release again
|
2015-04-29 12:05:02 +00:00
|
|
|
update_option( 'woocommerce_language_pack_version', array( WC_VERSION, $locale ) );
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-04 16:01:38 +00:00
|
|
|
/**
|
|
|
|
* Configure the WooCommerce translation upgrade notice
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function configure_woocommerce_upgrade_notice() {
|
2015-01-20 15:23:34 +00:00
|
|
|
WC_Admin_Notices::add_notice( 'translation_upgrade' );
|
2014-06-04 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 16:06:00 +00:00
|
|
|
/**
|
|
|
|
* Check if language pack exists
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function check_if_language_pack_exists() {
|
2015-05-21 13:45:28 +00:00
|
|
|
$response = wp_safe_remote_get( $this->get_language_package_uri(), array( 'timeout' => 60 ) );
|
2014-05-28 16:06:00 +00:00
|
|
|
|
|
|
|
if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the language version in database
|
|
|
|
*
|
2014-09-08 15:11:47 +00:00
|
|
|
* This updates the database while the download the translation package and ensures that not generate download loop
|
|
|
|
* If the installation fails you can redo it in: WooCommerce > Sistem Status > Tools > Force Translation Upgrade
|
2014-09-08 15:07:47 +00:00
|
|
|
*
|
|
|
|
* @param bool $reply Whether to bail without returning the package (default: false)
|
|
|
|
* @param string $package Package URL
|
2014-05-28 16:06:00 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-09-08 15:07:47 +00:00
|
|
|
public function version_update( $reply, $package ) {
|
|
|
|
if ( $package === $this->get_language_package_uri() ) {
|
2014-09-12 21:55:50 +00:00
|
|
|
$this->save_language_version();
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 15:07:47 +00:00
|
|
|
return $reply;
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 21:55:50 +00:00
|
|
|
/**
|
|
|
|
* Save language version
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function save_language_version() {
|
|
|
|
// Update the language pack version
|
2015-04-29 12:05:02 +00:00
|
|
|
update_option( 'woocommerce_language_pack_version', array( WC_VERSION, get_locale() ) );
|
2014-09-12 21:55:50 +00:00
|
|
|
|
|
|
|
// Remove the translation upgrade notice
|
|
|
|
$notices = get_option( 'woocommerce_admin_notices', array() );
|
|
|
|
$notices = array_diff( $notices, array( 'translation_upgrade' ) );
|
|
|
|
update_option( 'woocommerce_admin_notices', $notices );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manual language update
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function manual_language_update() {
|
|
|
|
if (
|
|
|
|
is_admin()
|
|
|
|
&& current_user_can( 'update_plugins' )
|
|
|
|
&& isset( $_GET['page'] )
|
|
|
|
&& 'wc-status' == $_GET['page']
|
|
|
|
&& isset( $_GET['action'] )
|
|
|
|
&& 'translation_upgrade' == $_GET['action']
|
|
|
|
) {
|
|
|
|
|
|
|
|
$url = wp_nonce_url( admin_url( 'admin.php?page=wc-status&tab=tools&action=translation_upgrade' ), 'language_update' );
|
|
|
|
$tools_url = admin_url( 'admin.php?page=wc-status&tab=tools' );
|
|
|
|
|
|
|
|
if ( ! isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) ) {
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 2 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( false === ( $creds = request_filesystem_credentials( $url, '', false, false, null ) ) ) {
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! WP_Filesystem( $creds ) ) {
|
|
|
|
request_filesystem_credentials( $url, '', true, false, null );
|
|
|
|
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download the language pack
|
2015-05-21 13:45:28 +00:00
|
|
|
$response = wp_safe_remote_get( $this->get_language_package_uri(), array( 'timeout' => 60 ) );
|
2014-09-12 21:55:50 +00:00
|
|
|
if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
|
|
|
|
global $wp_filesystem;
|
|
|
|
|
|
|
|
$upload_dir = wp_upload_dir();
|
|
|
|
$file = trailingslashit( $upload_dir['path'] ) . get_locale() . '.zip';
|
|
|
|
|
|
|
|
// Save the zip file
|
|
|
|
if ( ! $wp_filesystem->put_contents( $file, $response['body'], FS_CHMOD_FILE ) ) {
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unzip the file to wp-content/languages/plugins directory
|
|
|
|
$dir = trailingslashit( WP_LANG_DIR ) . 'plugins/';
|
|
|
|
$unzip = unzip_file( $file, $dir );
|
|
|
|
if ( true !== $unzip ) {
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 3 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the package file
|
|
|
|
$wp_filesystem->delete( $file );
|
|
|
|
|
|
|
|
// Update the language pack version
|
|
|
|
$this->save_language_version();
|
|
|
|
|
|
|
|
// Redirect and show a success message
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 1 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
// Don't have a valid package for the current language!
|
|
|
|
wp_redirect( add_query_arg( array( 'translation_updated' => 4 ), $tools_url ) );
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new WC_Language_Pack_Upgrader();
|