2014-05-28 16:06:00 +00:00
|
|
|
<?php
|
|
|
|
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' ) );
|
|
|
|
add_filter( 'upgrader_post_install', array( $this, 'version_update' ), 999, 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get WordPress language
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-06-12 10:57:49 +00:00
|
|
|
public static function get_language() {
|
2014-05-28 16:06:00 +00:00
|
|
|
if ( defined( 'WPLANG' ) && '' != WPLANG ) {
|
|
|
|
return WPLANG;
|
|
|
|
}
|
|
|
|
return 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get language package URI.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_language_package_uri() {
|
2014-05-29 20:05:38 +00:00
|
|
|
return $this->repo . WC_VERSION . '/packages/' . $this->get_language() . '.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',
|
|
|
|
'language' => $this->get_language(),
|
|
|
|
'version' => WC_VERSION,
|
|
|
|
'updated' => date( 'Y-m-d H:i:s' ),
|
|
|
|
'package' => $this->get_language_package_uri(),
|
|
|
|
'autoupdate' => 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if has available translation update
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has_available_update() {
|
|
|
|
$version = get_option( 'woocommerce_language_pack_version', '0' );
|
|
|
|
|
|
|
|
if ( version_compare( $version, WC_VERSION, '<' ) && 'en' !== $this->get_language() ) {
|
|
|
|
|
|
|
|
if ( $this->check_if_language_pack_exists() ) {
|
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
|
|
|
|
update_option( 'woocommerce_language_pack_version', WC_VERSION );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-04 16:01:38 +00:00
|
|
|
/**
|
|
|
|
* Configure the WooCommerce translation upgrade notice
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function configure_woocommerce_upgrade_notice() {
|
|
|
|
$notices = get_option( 'woocommerce_admin_notices', array() );
|
|
|
|
if ( false === array_search( 'translation_upgrade', $notices ) ) {
|
|
|
|
$notices[] = 'translation_upgrade';
|
|
|
|
|
|
|
|
update_option( 'woocommerce_admin_notices', $notices );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 16:06:00 +00:00
|
|
|
/**
|
|
|
|
* Check if language pack exists
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function check_if_language_pack_exists() {
|
|
|
|
$response = wp_remote_get( $this->get_language_package_uri(), array( 'sslverify' => false, 'timeout' => 60 ) );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the language version in database
|
|
|
|
*
|
|
|
|
* @param bool $response Install response (true = success, false = fail)
|
|
|
|
* @param array $hook_extra Extra arguments passed to hooked filters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function version_update( $response, $hook_extra ) {
|
|
|
|
if ( $response ) {
|
|
|
|
if (
|
|
|
|
( isset( $hook_extra['language_update_type'] ) && 'plugin' == $hook_extra['language_update_type'] )
|
|
|
|
&& ( isset( $hook_extra['language_update']->slug ) && 'woocommerce' == $hook_extra['language_update']->slug )
|
|
|
|
) {
|
2014-06-04 16:01:38 +00:00
|
|
|
// Update the language pack version
|
2014-05-28 16:06:00 +00:00
|
|
|
update_option( 'woocommerce_language_pack_version', WC_VERSION );
|
2014-06-04 16:01:38 +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 );
|
2014-05-28 16:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
new WC_Language_Pack_Upgrader();
|