WCML_Upgrade

The WooCommerce Multilingual WCML Upgrade class.

Defined (1)

The class is defined in the following location(s).

/inc/class-wcml-upgrade.php  
  1. class WCML_Upgrade{ 
  2.  
  3. private $versions = array( 
  4.  
  5. '2.9.9.1',  
  6. '3.1',  
  7. '3.2',  
  8. '3.3',  
  9. '3.5',  
  10. '3.5.4',  
  11. '3.6',  
  12. '3.7',  
  13. '3.7.3',  
  14. '3.7.11',  
  15. '3.8',  
  16. '3.9',  
  17. '3.9.1',  
  18. '4.0',  
  19. '4.1.0' 
  20. ); 
  21.  
  22. function __construct() { 
  23.  
  24. add_action('init', array($this, 'run')); 
  25. add_action('init', array($this, 'setup_upgrade_notices')); 
  26. add_action('admin_notices', array($this, 'show_upgrade_notices')); 
  27.  
  28. add_action('wp_ajax_wcml_hide_notice', array($this, 'hide_upgrade_notice')); 
  29.  
  30. }  
  31.  
  32. function setup_upgrade_notices() { 
  33.  
  34. $wcml_settings = get_option('_wcml_settings'); 
  35. $version_in_db = get_option('_wcml_version'); 
  36.  
  37. if(!empty($version_in_db) && version_compare($version_in_db, '2.9.9.1', '<')) { 
  38. $n = 'varimages'; 
  39. $wcml_settings['notifications'][$n] =  
  40. array( 
  41. 'show' => 1,  
  42. 'text' => __('Looks like you are upgrading from a previous version of WooCommerce Multilingual. Would you like to automatically create translated variations and images?', 'wcml'). 
  43. '<br /><strong>' . 
  44. ' <a href="' . admin_url('admin.php?page=wpml-wcml&tab=troubleshooting') . '">' . __('Yes, go to the troubleshooting page', 'wcml') . '</a> |' . 
  45. ' <a href="#" onclick="jQuery.ajax({type:\'POST\', url: ajaxurl, data:\'action=wcml_hide_notice¬ice='.$n.'\', success:function() {jQuery(\'#' . $n . '\').fadeOut()}});return false;">' . __('No - dismiss', 'wcml') . '</a>' .  
  46. '</strong>' 
  47. ); 
  48. update_option('_wcml_settings', $wcml_settings); 
  49.  
  50.  
  51. function show_upgrade_notices() { 
  52. $wcml_settings = get_option('_wcml_settings'); 
  53. if(!empty($wcml_settings['notifications'])) {  
  54. foreach($wcml_settings['notifications'] as $k => $notification) { 
  55.  
  56. // exceptions 
  57. if(isset($_GET['tab']) && $_GET['tab'] == 'troubleshooting' && $k == 'varimages') continue; 
  58.  
  59. if($notification['show']) { 
  60. ?> 
  61. <div id="<?php echo $k ?>" class="updated"> 
  62. <p><?php echo $notification['text'] ?></p> 
  63. </div> 
  64. <?php  
  65.  
  66. function hide_upgrade_notice($k) { 
  67.  
  68. if(empty($k)) { 
  69. $k = $_POST['notice']; 
  70.  
  71. $wcml_settings = get_option('_wcml_settings'); 
  72. if(isset($wcml_settings['notifications'][$k])) { 
  73. $wcml_settings['notifications'][$k]['show'] = 0; 
  74. update_option('_wcml_settings', $wcml_settings); 
  75.  
  76. function run() { 
  77.  
  78. $version_in_db = get_option('_wcml_version'); 
  79.  
  80. // exception - starting in 2.3.2 
  81. if(empty($version_in_db) && get_option('icl_is_wcml_installed')) { 
  82. $version_in_db = '2.3.2'; 
  83.  
  84. $migration_ran = false; 
  85.  
  86. if($version_in_db && version_compare($version_in_db, WCML_VERSION, '<')) { 
  87.  
  88. foreach($this->versions as $version) { 
  89.  
  90. if(version_compare($version, WCML_VERSION, '<=') && version_compare($version, $version_in_db, '>')) { 
  91.  
  92. $upgrade_method = 'upgrade_' . str_replace('.', '_', $version); 
  93.  
  94. if(method_exists($this, $upgrade_method)) { 
  95. $this->$upgrade_method(); 
  96. $migration_ran = true; 
  97.  
  98.  
  99.  
  100.  
  101. if($migration_ran || empty($version_in_db)) { 
  102. update_option('_wcml_version', WCML_VERSION);  
  103.  
  104. if( get_option( '_wcml_4_1_0_migration_required' ) && class_exists( 'woocommerce' ) ) { 
  105. $this->upgrade_4_1_0(); 
  106. delete_option('_wcml_4_1_0_migration_required' ); 
  107.  
  108. function upgrade_2_9_9_1() { 
  109. global $wpdb; 
  110.  
  111. //migrate exists currencies 
  112. $currencies = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "icl_currencies ORDER BY `id` DESC"); 
  113. foreach($currencies as $currency) { 
  114. if(isset($currency->language_code)) { 
  115. $wpdb->insert($wpdb->prefix .'icl_languages_currencies', array( 
  116. 'language_code' => $currency->language_code,  
  117. 'currency_id' => $currency->id 
  118. ); 
  119.  
  120. $cols = $wpdb->get_col("SHOW COLUMNS FROM {$wpdb->prefix}icl_currencies");  
  121. if(in_array('language_code', $cols)) { 
  122. $wpdb->query("ALTER TABLE {$wpdb->prefix}icl_currencies DROP COLUMN language_code"); 
  123.  
  124. // migrate settings 
  125. $new_settings = array( 
  126. 'is_term_order_synced' => get_option('icl_is_wcml_term_order_synched'),  
  127. 'file_path_sync' => get_option('wcml_file_path_sync'),  
  128. 'is_installed' => get_option('icl_is_wpcml_installed'),  
  129. 'dismiss_doc_main' => get_option('wpml_dismiss_doc_main'),  
  130. 'enable_multi_currency' => get_option('icl_enable_multi_currency'),  
  131. 'currency_converting_option' => get_option('currency_converting_option') 
  132. ); 
  133.  
  134. if(!get_option('_wcml_settings')) { 
  135. add_option('_wcml_settings', $new_settings, false, true); 
  136.  
  137. delete_option('icl_is_wcml_term_order_synced'); 
  138. delete_option('wcml_file_path_sync'); 
  139. delete_option('icl_is_wpcml_installed'); 
  140. delete_option('wpml_dismiss_doc_main'); 
  141. delete_option('icl_enable_multi_currency'); 
  142. delete_option('currency_converting_option'); 
  143.  
  144.  
  145.  
  146. function upgrade_3_1() { 
  147. global $wpdb, $sitepress; 
  148. $wcml_settings = get_option('_wcml_settings'); 
  149.  
  150. if(isset($wcml_settings['enable_multi_currency']) && $wcml_settings['enable_multi_currency'] == 'yes') { 
  151. $wcml_settings['enable_multi_currency'] = WCML_MULTI_CURRENCIES_INDEPENDENT; 
  152. }else{ 
  153. $wcml_settings['enable_multi_currency'] = WCML_MULTI_CURRENCIES_DISABLED; 
  154.  
  155. $wcml_settings['products_sync_date'] = 1; 
  156.  
  157.  
  158. update_option('_wcml_settings', $wcml_settings); 
  159.  
  160. // multi-currency migration 
  161. if($wcml_settings['enable_multi_currency'] == 'yes' && $wcml_settings['currency_converting_option'] == 2) { 
  162.  
  163. // get currencies exchange rates 
  164. $results = $wpdb->get_results("SELECT code, value FROM {$wpdb->prefix}icl_currencies"); 
  165. foreach($results as $row) { 
  166. $exchange_rates[$row->code] = $row->value;  
  167.  
  168. // get languages currencies map 
  169. $results = $wpdb->get_results("SELECT l.language_code, c.code FROM {$wpdb->prefix}icl_languages_currencies l JOIN {$wpdb->prefix}icl_currencies c ON l.currency_id = c.id"); 
  170. foreach($results as $row) { 
  171. $language_currencies[$row->language_code] = $row->code;  
  172.  
  173.  
  174. $results = $wpdb->get_results($wpdb->prepare(" 
  175. SELECT p.ID, t.trid, t.element_type  
  176. FROM {$wpdb->posts} p JOIN {$wpdb->prefix}icl_translations t ON t.element_id = p.ID AND t.element_type IN ('post_product', 'post_product_variation') 
  177. WHERE  
  178. p.post_type in ('product', 'product_variation') AND t.language_code = %s 
  179.  
  180. ", $sitepress->get_default_language())); 
  181.  
  182. // set custom conversion rates 
  183. foreach($results as $row) { 
  184. $translations = $sitepress->get_element_translations($row->trid, $row->element_type); 
  185. $meta = get_post_meta($row->ID); 
  186. $original_prices['_price'] = !empty($meta['_price']) ? $meta['_price'][0] : 0; 
  187. $original_prices['_regular_price'] = !empty($meta['_regular_price']) ? $meta['_regular_price'][0] : 0; 
  188. $original_prices['_sale_price'] = !empty($meta['_sale_price']) ? $meta['_sale_price'][0] : 0; 
  189.  
  190.  
  191. $ccr = array(); 
  192.  
  193. foreach($translations as $translation) { 
  194. if($translation->element_id != $row->ID) { 
  195.  
  196. $meta = get_post_meta($translation->element_id); 
  197. $translated_prices['_price'] = $meta['_price'][0]; 
  198. $translated_prices['_regular_price'] = $meta['_regular_price'][0]; 
  199. $translated_prices['_sale_price'] = $meta['_sale_price'][0]; 
  200.  
  201. if(!empty($translated_prices['_price']) && !empty($original_prices['_price']) && $translated_prices['_price'] != $original_prices['_price']) { 
  202.  
  203. $ccr['_price'][$language_currencies[$translation->language_code]] = $translated_prices['_price'] / $original_prices['_price']; 
  204.  
  205. }  
  206. if(!empty($translated_prices['_regular_price']) && !empty($original_prices['_regular_price']) && $translated_prices['_regular_price'] != $original_prices['_regular_price']) { 
  207.  
  208. $ccr['_regular_price'][$language_currencies[$translation->language_code]] = $translated_prices['_regular_price'] / $original_prices['_regular_price']; 
  209.  
  210. }  
  211. if(!empty($translated_prices['_sale_price']) && !empty($original_prices['_sale_price']) && $translated_prices['_sale_price'] != $original_prices['_sale_price']) { 
  212.  
  213. $ccr['_sale_price'][$language_currencies[$translation->language_code]] = $translated_prices['_sale_price'] / $original_prices['_sale_price'] ; 
  214.  
  215. }  
  216.  
  217.  
  218.  
  219. if($ccr) { 
  220. update_post_meta($row->ID, '_custom_conversion_rate', $ccr);  
  221.  
  222.  
  223.  
  224.  
  225. }  
  226.  
  227.  
  228. function upgrade_3_2() { 
  229.  
  230. WCML_Capabilities::set_up_capabilities(); 
  231.  
  232. //delete not existing currencies in WC 
  233. global $wpdb; 
  234. $currencies = $wpdb->get_results("SELECT id, code FROM " . $wpdb->prefix . "icl_currencies ORDER BY `id` DESC"); 
  235. $wc_currencies = get_woocommerce_currencies(); 
  236. foreach ($currencies as $currency) { 
  237. if(!array_key_exists($currency->code, $wc_currencies)) { 
  238. $wpdb->delete( $wpdb->prefix . 'icl_currencies', array( 'ID' => $currency->id ) ); 
  239.  
  240.  
  241. function upgrade_3_3() { 
  242. global $wpdb, $woocommerce_wpml; 
  243.  
  244. WCML_Capabilities::set_up_capabilities(); 
  245.  
  246. $currencies = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "icl_currencies ORDER BY `id` ASC", OBJECT); 
  247. if($currencies) 
  248. foreach($this->currencies as $currency) { 
  249.  
  250. $woocommerce_wpml->settings['currency_options'][$currency->code]['rate'] = $currency->value; 
  251. $woocommerce_wpml->settings['currency_options'][$currency->code]['updated'] = $currency->changed; 
  252. $woocommerce_wpml->settings['currency_options'][$currency->code]['position'] = 'left'; 
  253. $woocommerce_wpml->settings['currency_options'][$currency->code]['languages'] = $woocommerce_wpml->settings['currencies_languages']; 
  254. unset($woocommerce_wpml->settings['currencies_languages']); 
  255.  
  256. $woocommerce_wpml->update_settings(); 
  257.  
  258.  
  259. $wpdb->query("DROP TABLE `{$wpdb->prefix}icl_currencies`"); 
  260.  
  261.  
  262. function upgrade_3_5() 
  263. global $wpdb; 
  264. $wcml_settings = get_option('_wcml_settings'); 
  265.  
  266. $wcml_settings['products_sync_order'] = 1; 
  267.  
  268. update_option('_wcml_settings', $wcml_settings); 
  269.  
  270. function upgrade_3_5_4() 
  271. flush_rewrite_rules( ); 
  272.  
  273. function upgrade_3_6() 
  274. $wcml_settings = get_option('_wcml_settings'); 
  275.  
  276. $wcml_settings['display_custom_prices'] = 0; 
  277. $wcml_settings['currency_switcher_product_visibility'] = 1; 
  278.  
  279. update_option('_wcml_settings', $wcml_settings); 
  280.  
  281. function upgrade_3_7() { 
  282. global $woocommerce_wpml, $wpdb; 
  283.  
  284. $woocommerce_permalinks = maybe_unserialize( get_option('woocommerce_permalinks') ); 
  285.  
  286. if( is_array( $woocommerce_permalinks ) ) { 
  287. foreach ( $woocommerce_permalinks as $base_key => $base ) { 
  288.  
  289. $base_key = trim( $base_key, '/' ); 
  290.  
  291. if ( $base ) { 
  292. $taxonomy = false; 
  293.  
  294. switch ( $base_key ) { 
  295. case 'category_base': 
  296. $taxonomy = 'product_cat'; 
  297. break; 
  298. case 'tag_base': 
  299. $taxonomy = 'product_tag'; 
  300. break; 
  301. case 'attribute_base': 
  302. $taxonomy = 'attribute'; 
  303. break; 
  304.  
  305. if ( $taxonomy ) { 
  306. $wpdb->update( 
  307. $wpdb->prefix . 'icl_strings',  
  308. array( 
  309. 'context' => 'WordPress',  
  310. 'name' => sprintf( 'URL %s tax slug', $taxonomy ) 
  311. ),  
  312. array( 
  313. 'context' => sprintf( 'URL %s slugs - %s', $taxonomy, $base ),  
  314. 'name' => sprintf( 'Url %s slug: %s', $taxonomy, $base ) 
  315. ); 
  316.  
  317.  
  318.  
  319. $endpoint_keys = array( 'order-pay', 'order-received', 'view-order', 'edit-account', 'edit-address', 'lost-password', 'customer-logout', 'add-payment-method' ); 
  320.  
  321. foreach( $endpoint_keys as $endpoint_key ) { 
  322.  
  323. $wpdb->query( 
  324. $wpdb->prepare( "UPDATE {$wpdb->prefix}icl_strings 
  325. SET context = 'WooCommerce Endpoints', name = %s 
  326. WHERE context = 'WordPress' AND name = %s",  
  327. $endpoint_key, 'Endpoint slug: '. $endpoint_key ) 
  328. ); 
  329.  
  330. // update domain_name_context_md5 value 
  331. $string_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}icl_strings WHERE context = 'WooCommerce Endpoints' AND name = %s", $endpoint_key ) ); 
  332.  
  333. if( $string_id ) { 
  334. $wpdb->query( 
  335. $wpdb->prepare( "UPDATE {$wpdb->prefix}icl_strings 
  336. SET domain_name_context_md5 = %s 
  337. WHERE id = %d",  
  338. md5( $endpoint_key, 'WooCommerce Endpoints' ), $string_id ) 
  339. ); 
  340.  
  341.  
  342. if( !isset($woocommerce_wpml->terms) ) { 
  343. global $sitepress; 
  344. $woocommerce_wpml->terms = new WCML_Terms( $woocommerce_wpml, $sitepress, $wpdb ); 
  345. $woocommerce_wpml->terms->check_if_sync_terms_needed(); 
  346.  
  347. $wcml_settings = get_option('_wcml_settings'); 
  348.  
  349. $wcml_settings['sync_taxonomies_checked'] = 1; 
  350.  
  351. update_option('_wcml_settings', $wcml_settings); 
  352.  
  353.  
  354. //update custom fields for bookings 
  355. $bookable_resources = $wpdb->get_results( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_bookable_resource' AND source_language_code IS NOT NULL"); 
  356.  
  357. foreach( $bookable_resources AS $bookable_resource ) { 
  358. update_post_meta( $bookable_resource->element_id, 'wcml_is_translated', true ); 
  359.  
  360. $bookable_persons = $wpdb->get_results( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_bookable_person' AND source_language_code IS NOT NULL"); 
  361.  
  362. foreach( $bookable_persons AS $bookable_person ) { 
  363. update_post_meta( $bookable_person->element_id, 'wcml_is_translated', true ); 
  364.  
  365.  
  366. function upgrade_3_7_3() 
  367. global $sitepress; 
  368.  
  369. $active_languages = $sitepress->get_active_languages(); 
  370. $current_language = $sitepress->get_current_language(); 
  371.  
  372. foreach( $active_languages as $lang ) { 
  373.  
  374. $sitepress->switch_lang( $lang['code'] ); 
  375.  
  376. $product_cats = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); 
  377.  
  378. _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false ); 
  379.  
  380. $product_tags = get_terms( 'product_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); 
  381.  
  382. _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false ); 
  383.  
  384.  
  385. $sitepress->switch_lang( $current_language ); 
  386.  
  387.  
  388. function upgrade_3_7_11() { 
  389.  
  390. $wcml_settings = get_option('_wcml_settings'); 
  391. $wcml_settings['dismiss_doc_main'] = 1; 
  392. update_option('_wcml_settings', $wcml_settings); 
  393.  
  394. function upgrade_3_8() { 
  395.  
  396. $wcml_settings = get_option('_wcml_settings'); 
  397. $wcml_settings['set_up_wizard_run'] = 1; 
  398.  
  399. if( isset($wcml_settings[ 'attributes_settings' ]) ) { 
  400. $attributes_settings = $wcml_settings['attributes_settings']; 
  401. foreach ( $attributes_settings as $name => $value ) { 
  402. if ( substr( $name, 0, 3 ) != 'pa_' ) { 
  403. unset( $wcml_settings['attributes_settings'] [$name] ); 
  404. $wcml_settings['attributes_settings'] ['pa_' . $name] = $value; 
  405.  
  406. update_option('_wcml_settings', $wcml_settings); 
  407.  
  408.  
  409. function upgrade_3_9() { 
  410. global $wpdb; 
  411.  
  412. $meta_keys_to_fix = array( 
  413. '_price',  
  414. '_regular_price',  
  415. '_sale_price',  
  416. '_sku' 
  417. ); 
  418.  
  419. $sql = " 
  420. UPDATE {$wpdb->postmeta}  
  421. SET meta_value = ''  
  422. WHERE meta_key IN('" . join("', '", $meta_keys_to_fix) . "')  
  423. AND meta_value IS NULL"; 
  424.  
  425. $wpdb->query( $sql ); 
  426.  
  427.  
  428. function upgrade_3_9_1() { 
  429. global $wpdb, $sitepress; 
  430.  
  431. $results = $wpdb->get_results(" 
  432. SELECT p.ID, t.trid, t.element_type 
  433. FROM {$wpdb->posts} p 
  434. JOIN {$wpdb->prefix}icl_translations t ON t.element_id = p.ID AND t.element_type IN ('post_product', 'post_product_variation') 
  435. WHERE p.post_type in ('product', 'product_variation') AND t.source_language_code IS NULL 
  436. "); 
  437.  
  438. foreach( $results as $product ) { 
  439.  
  440. if( get_post_meta( $product->ID, '_manage_stock', true ) === 'yes' ) { 
  441.  
  442. $translations = $sitepress->get_element_translations( $product->trid, $product->element_type ); 
  443.  
  444. $min_stock = false; 
  445.  
  446. //collect min stock 
  447. foreach( $translations as $translation ) { 
  448. $stock = get_post_meta( $translation->element_id, '_stock', true ); 
  449. if( !$min_stock || $stock < $min_stock ) { 
  450. $min_stock = $stock; 
  451.  
  452. //update stock value 
  453. foreach( $translations as $translation ) { 
  454. update_post_meta( $translation->element_id, '_stock', $min_stock ); 
  455.  
  456. function upgrade_4_0() { 
  457. $wcml_settings = get_option( '_wcml_settings' ); 
  458. $wcml_settings[ 'dismiss_tm_warning' ] = 0; 
  459. $wcml_settings['cart_sync']['lang_switch'] = WCML_CART_SYNC; 
  460. $wcml_settings['cart_sync']['currency_switch'] = WCML_CART_SYNC; 
  461.  
  462. update_option('_wcml_settings', $wcml_settings); 
  463.  
  464.  
  465. function upgrade_4_1_0() { 
  466. global $wpdb; 
  467.  
  468. if( !class_exists( 'WooCommerce' ) ) { 
  469. update_option( '_wcml_4_1_0_migration_required', true ); 
  470. }else{ 
  471. $results = $wpdb->get_results(" 
  472. SELECT * 
  473. FROM {$wpdb->postmeta} 
  474. WHERE meta_key LIKE '_price_%' OR meta_key LIKE '_regular_price_%' OR ( meta_key LIKE '_sale_price_%' AND meta_key NOT LIKE '_sale_price_dates%' ) 
  475. "); 
  476.  
  477. foreach( $results as $price ) { 
  478. $formatted_price = wc_format_decimal( $price->meta_value ); 
  479. update_post_meta( $price->post_id, $price->meta_key, $formatted_price ); 
  480.  
  481. if( get_post_type( $price->post_id ) == 'product_variation' ) { 
  482. delete_transient( 'wc_var_prices_'.wp_get_post_parent_id( $price->post_id ) ); 
  483.  
  484.  
  485. $wcml_settings = get_option( '_wcml_settings' ); 
  486.  
  487. if( 
  488. isset( $wcml_settings[ 'currency_switcher_style' ] ) && 
  489. $wcml_settings[ 'currency_switcher_style' ] == 'list' 
  490. ) { 
  491. if( $wcml_settings[ 'wcml_curr_sel_orientation' ] == 'horizontal' ) { 
  492. $switcher_style = 'wcml-horizontal-list'; 
  493. }else{ 
  494. $switcher_style = 'wcml-vertical-list'; 
  495. }else{ 
  496. $switcher_style = 'wcml-dropdown'; 
  497.  
  498. $wcml_settings[ 'currency_switchers' ][ 'product' ] = array( 
  499. 'switcher_style' => $switcher_style,  
  500. 'template' => isset( $wcml_settings[ 'wcml_curr_template' ] ) ? $wcml_settings[ 'wcml_curr_template' ] : '',  
  501. 'widget_title' => '',  
  502. 'color_scheme' => array( 
  503. 'font_current_normal' => '',  
  504. 'font_current_hover' => '',  
  505. 'background_current_normal' => '',  
  506. 'background_current_hover' => '',  
  507. 'font_other_normal' => '',  
  508. 'font_other_hover' => '',  
  509. 'background_other_normal' => '',  
  510. 'background_other_hover' => '',  
  511. 'border_normal' => '' 
  512. ); 
  513.  
  514. $wcml_settings[ 'currency_switcher_additional_css' ] = ''; 
  515. update_option('_wcml_settings', $wcml_settings ); 
  516.