admin-menus
4 weeks ago
currencies
4 weeks ago
template-classes
4 weeks ago
translation-editor
4 weeks ago
class-wcml-ajax-setup.php
4 weeks ago
class-wcml-attributes.php
4 weeks ago
class-wcml-capabilities.php
4 weeks ago
class-wcml-cart-removed-items-widget.php
1 year ago
class-wcml-cart-switch-lang-functions.php
4 weeks ago
class-wcml-cart-sync-warnings.php
4 weeks ago
class-wcml-cart.php
4 weeks ago
class-wcml-comments.php
4 weeks ago
class-wcml-compatibility.php
4 weeks ago
class-wcml-coupons.php
4 weeks ago
class-wcml-dependencies.php
4 weeks ago
class-wcml-emails.php
4 weeks ago
class-wcml-endpoints.php
4 weeks ago
class-wcml-install.php
4 weeks ago
class-wcml-languages-upgrader.php
4 weeks ago
class-wcml-locale.php
4 weeks ago
class-wcml-orders.php
4 weeks ago
class-wcml-products-screen-options.php
4 weeks ago
class-wcml-products.php
4 weeks ago
class-wcml-reports.php
4 weeks ago
class-wcml-requests.php
4 weeks ago
class-wcml-resources.php
4 weeks ago
class-wcml-store-pages.php
4 weeks ago
class-wcml-terms.php
4 weeks ago
class-wcml-tp-support.php
4 weeks ago
class-wcml-troubleshooting.php
4 weeks ago
class-wcml-upgrade.php
4 weeks ago
class-wcml-url-translation.php
4 weeks ago
class-wcml-wc-gateways.php
4 weeks ago
class-wcml-wc-shipping.php
4 weeks ago
class-wcml-wc-strings.php
4 weeks ago
class-wcml-widgets.php
4 weeks ago
constants.php
4 weeks ago
functions-pure.php
4 weeks ago
functions.php
4 weeks ago
installer-loader.php
4 weeks ago
missing-php-functions.php
4 weeks ago
wcml-core-functions.php
4 weeks ago
wcml-switch-lang-request.php
4 weeks ago
class-wcml-languages-upgrader.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Languages_Upgrader { |
| 4 | |
| 5 | |
| 6 | |
| 7 | public function __construct() { |
| 8 | |
| 9 | add_action( 'icl_update_active_languages', [ $this, 'download_woocommerce_translations_for_active_languages' ] ); |
| 10 | add_filter( 'upgrader_pre_download', [ $this, 'version_update' ], 10, 2 ); |
| 11 | add_action( 'admin_notices', [ $this, 'translation_upgrade_notice' ] ); |
| 12 | add_action( 'wp_ajax_hide_wcml_translations_message', [ $this, 'hide_wcml_translations_message' ] ); |
| 13 | |
| 14 | $this->load_js(); |
| 15 | } |
| 16 | |
| 17 | public function download_woocommerce_translations( $lang_code, $wc_version ) { |
| 18 | global $sitepress; |
| 19 | |
| 20 | $locale = $sitepress->get_locale( $lang_code ); |
| 21 | |
| 22 | if ( $locale !== 'en_US' && $this->has_available_update( $locale, $wc_version ) ) { |
| 23 | |
| 24 | $wc_version = $wc_version ?: WC_VERSION; |
| 25 | |
| 26 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 27 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 28 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 29 | require_once ABSPATH . 'wp-admin/includes/template.php'; |
| 30 | |
| 31 | $url = 'update-core.php?action=do-translation-upgrade'; |
| 32 | $nonce = 'upgrade-translations'; |
| 33 | $title = ''; |
| 34 | $context = WP_LANG_DIR; |
| 35 | |
| 36 | $upgrader = new Language_Pack_Upgrader( new Automatic_Upgrader_Skin( compact( 'url', 'nonce', 'title', 'context' ) ) ); |
| 37 | |
| 38 | $upgr_object = []; |
| 39 | $upgr_object[0] = new stdClass(); |
| 40 | $upgr_object[0]->type = 'plugin'; |
| 41 | $upgr_object[0]->slug = 'woocommerce'; |
| 42 | $upgr_object[0]->language = $locale; |
| 43 | $upgr_object[0]->version = $wc_version; |
| 44 | $upgr_object[0]->updated = date( 'Y-m-d H:i:s' ); |
| 45 | $upgr_object[0]->package = $this->get_language_pack_uri( $locale, $wc_version ); |
| 46 | $upgr_object[0]->autoupdate = 1; |
| 47 | |
| 48 | $ob_level_before = ob_get_level(); |
| 49 | |
| 50 | $upgrader->bulk_upgrade( $upgr_object ); |
| 51 | |
| 52 | $ob_level_after = ob_get_level(); |
| 53 | if ( $ob_level_after > $ob_level_before ) { |
| 54 | ob_end_clean(); |
| 55 | } |
| 56 | |
| 57 | $this->save_translation_version( $locale, false, $wc_version ); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | public function download_woocommerce_translations_for_active_languages( $wc_version = false ) { |
| 64 | global $sitepress, $woocommerce_wpml; |
| 65 | |
| 66 | $active_languages = $sitepress->get_active_languages(); |
| 67 | |
| 68 | $current_language = $sitepress->get_current_language(); |
| 69 | |
| 70 | foreach ( $active_languages as $language ) { |
| 71 | |
| 72 | $this->download_woocommerce_translations( $language['code'], $wc_version ); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | $sitepress->switch_lang( $current_language ); |
| 77 | |
| 78 | if ( isset( $woocommerce_wpml->url_translation ) ) { |
| 79 | $woocommerce_wpml->url_translation->register_product_and_taxonomy_bases(); |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public function get_language_pack_uri( $locale, $version = false ) { |
| 85 | |
| 86 | if ( ! $version ) { |
| 87 | $version = WC_VERSION; |
| 88 | } |
| 89 | |
| 90 | $repo = 'https://downloads.wordpress.org/translation/plugin/woocommerce/'; |
| 91 | return $repo . $version . '/' . $locale . '.zip'; |
| 92 | } |
| 93 | |
| 94 | public function version_update( $reply, $package ) { |
| 95 | |
| 96 | $notices = maybe_unserialize( get_option( 'wcml_translations_upgrade_notice' ) ); |
| 97 | |
| 98 | if ( ! is_array( $notices ) ) { |
| 99 | return $reply; |
| 100 | } |
| 101 | |
| 102 | foreach ( $notices as $key => $locale ) { |
| 103 | if ( strstr( $package, 'woocommerce' ) && strstr( $package, $locale ) ) { |
| 104 | |
| 105 | $this->save_translation_version( $locale, $key ); |
| 106 | |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $reply; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | public function save_translation_version( $locale, $key = false, $wc_version = false ) { |
| 115 | |
| 116 | $notices = maybe_unserialize( get_option( 'wcml_translations_upgrade_notice' ) ); |
| 117 | |
| 118 | update_option( 'woocommerce_language_pack_version_' . $locale, [ $wc_version ?: WC_VERSION, $locale ] ); |
| 119 | |
| 120 | if ( is_array( $notices ) ) { |
| 121 | |
| 122 | if ( ! $key ) { |
| 123 | $key = array_search( $locale, $notices ); |
| 124 | } |
| 125 | |
| 126 | unset( $notices[ $key ] ); |
| 127 | |
| 128 | update_option( 'wcml_translations_upgrade_notice', $notices ); |
| 129 | |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | public function has_available_update( $locale, $wc_version = false ) { |
| 135 | $wc_version = $wc_version ?: WC_VERSION; |
| 136 | |
| 137 | $version = get_option( 'woocommerce_language_pack_version_' . $locale, [ '0', $locale ] ); |
| 138 | |
| 139 | $is_new_version = ! is_array( $version ) || version_compare( $version[0], $wc_version, '<' ) || $version[1] !== $locale; |
| 140 | $mo_file_absent = ! file_exists( sprintf( WP_LANG_DIR . '/plugins/woocommerce-%s.mo', $locale ) ); |
| 141 | |
| 142 | $notices = maybe_unserialize( get_option( 'wcml_translations_upgrade_notice', [] ) ); |
| 143 | |
| 144 | if ( 'en_US' !== $locale && ( $is_new_version || $mo_file_absent ) ) { |
| 145 | if ( $this->check_if_language_pack_exists( $locale, $wc_version ) ) { |
| 146 | |
| 147 | if ( ! $notices || ! in_array( $locale, $notices ) ) { |
| 148 | $notices[] = $locale; |
| 149 | |
| 150 | update_option( 'wcml_translations_upgrade_notice', $notices ); |
| 151 | update_option( 'hide_wcml_translations_message', 0 ); |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } else { |
| 156 | update_option( 'woocommerce_language_pack_version_' . $locale, [ $wc_version, $locale ] ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | public function check_if_language_pack_exists( $locale, $wc_version ) { |
| 165 | |
| 166 | $response = wp_safe_remote_get( $this->get_language_pack_uri( $locale, $wc_version ), [ 'timeout' => 60 ] ); |
| 167 | |
| 168 | if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && $response['body'] !== '404 File not found' ) { |
| 169 | return true; |
| 170 | } else { |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | public function translation_upgrade_notice() { |
| 177 | $screen = get_current_screen(); |
| 178 | $notices = maybe_unserialize( get_option( 'wcml_translations_upgrade_notice' ) ); |
| 179 | |
| 180 | if ( 'update-core' !== $screen->id && ! empty( $notices ) && ! get_option( 'hide_wcml_translations_message' ) ) { |
| 181 | |
| 182 | $lang_notices = new WCML_Languages_Upgrade_Notice( $notices ); |
| 183 | $lang_notices->show(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | public function hide_wcml_translations_message() { |
| 188 | $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 189 | if ( ! $nonce || ! wp_verify_nonce( $nonce, 'hide_wcml_translations_message' ) ) { |
| 190 | die( 'Invalid nonce' ); |
| 191 | } |
| 192 | update_option( 'hide_wcml_translations_message', true ); |
| 193 | |
| 194 | die(); |
| 195 | } |
| 196 | |
| 197 | public function load_js() { |
| 198 | |
| 199 | wp_register_script( 'wcml-lang-notice', WCML_PLUGIN_URL . '/res/js/languages_notice' . WCML_JS_MIN . '.js', [ 'jquery' ], WCML_VERSION, true ); |
| 200 | wp_enqueue_script( 'wcml-lang-notice' ); |
| 201 | |
| 202 | wp_localize_script( |
| 203 | 'wcml-lang-notice', |
| 204 | 'wcml_language_upgrade_notices', |
| 205 | [ |
| 206 | 'dont_close' => esc_html__( "Downloading translations... Please don't close this page.", 'woocommerce-multilingual' ), |
| 207 | ] |
| 208 | ); |
| 209 | |
| 210 | } |
| 211 | |
| 212 | } |
| 213 |