WCML_Dependencies

The WooCommerce Multilingual WCML Dependencies class.

Defined (1)

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

/inc/class-wcml-dependencies.php  
  1. class WCML_Dependencies{ 
  2.  
  3.  
  4. private $missing = array(); 
  5. private $err_message = ''; 
  6. private $allok = true; 
  7.  
  8. function __construct() { 
  9.  
  10. if(is_admin()) { 
  11. add_action('wp_ajax_wcml_fix_strings_language', array($this, 'fix_strings_language')); // TODO: remove after WPML release with support strings in different languages 
  12.  
  13. add_action('init', array($this, 'check_wpml_config'), 100);  
  14.  
  15.  
  16. }  
  17.  
  18. function check() { 
  19. global $woocommerce_wpml, $sitepress, $woocommerce; 
  20.  
  21. if(!defined('ICL_SITEPRESS_VERSION') || ICL_PLUGIN_INACTIVE || is_null( $sitepress ) || !class_exists('SitePress')) { 
  22. $this->missing['WPML'] = WCML_Links::generate_tracking_link('https://wpml.org/'); 
  23. $this->allok = false; 
  24. } elseif(version_compare(ICL_SITEPRESS_VERSION, '3.4', '<')) { 
  25. add_action('admin_notices', array($this, '_old_wpml_warning')); 
  26. $this->allok = false; 
  27.  
  28. if( !class_exists( 'WooCommerce' ) || !function_exists( 'WC' ) ) { 
  29. $this->missing['WooCommerce'] = 'http://www.woothemes.com/woocommerce/'; 
  30. $this->allok = false; 
  31. }elseif( ( defined('WC_VERSION') && version_compare( WC_VERSION , '2.1', '<' ) ) || ( isset( $woocommerce->version ) && version_compare( $woocommerce->version , '2.1', '<' ) ) ) { 
  32. add_action('admin_notices', array($this, '_old_wc_warning')); 
  33. $this->allok = false; 
  34.  
  35. if(!defined('WPML_TM_VERSION')) { 
  36. $this->missing['WPML Translation Management'] = WCML_Links::generate_tracking_link('https://wpml.org/'); 
  37. $this->allok = false; 
  38. }elseif(version_compare(WPML_TM_VERSION, '1.9', '<')) { 
  39. add_action('admin_notices', array($this, '_old_wpml_tm_warning')); 
  40. $this->allok = false; 
  41.  
  42. if(!defined('WPML_ST_VERSION')) { 
  43. $this->missing['WPML String Translation'] = WCML_Links::generate_tracking_link('https://wpml.org/'); 
  44. $this->allok = false; 
  45. }elseif(version_compare(WPML_ST_VERSION, '2.0', '<')) { 
  46. add_action('admin_notices', array($this, '_old_wpml_st_warning')); 
  47. $this->allok = false; 
  48.  
  49. if(!defined('WPML_MEDIA_VERSION')) { 
  50. $this->missing['WPML Media'] = WCML_Links::generate_tracking_link('https://wpml.org/'); 
  51. $this->allok = false; 
  52. }elseif(version_compare(WPML_MEDIA_VERSION, '2.1', '<')) { 
  53. add_action('admin_notices', array($this, '_old_wpml_media_warning')); 
  54. $this->allok = false; 
  55.  
  56. if ($this->missing) { 
  57. add_action('admin_notices', array($this, '_missing_plugins_warning')); 
  58.  
  59. if($this->allok) { 
  60. $this->check_for_incompatible_permalinks(); 
  61.  
  62. if(isset($sitepress)) { 
  63. $this->allok = $this->allok & $sitepress->setup(); 
  64. }else{ 
  65. $this->load_twig_support(); 
  66.  
  67. return $this->allok; 
  68.  
  69. /** 
  70. * Adds admin notice. 
  71. */ 
  72. public function _old_wpml_warning() { 
  73. ?> 
  74. <div class="message error"><p><?php printf(__('WooCommerce Multilingual is enabled but not effective. It is not compatible with <a href="%s">WPML</a> versions prior %s.',  
  75. 'woocommerce-multilingual'), WCML_Links::generate_tracking_link('https://wpml.org/'), '3.4'); ?></p></div> 
  76. <?php } 
  77.  
  78. function _old_wc_warning() { 
  79. ?> 
  80. <div class="message error"><p><?php printf(__('WooCommerce Multilingual is enabled but not effective. It is not compatible with <a href="%s">Woocommerce</a> versions prior %s.',  
  81. 'woocommerce-multilingual'), 'http://www.woothemes.com/woocommerce/', '2.1' ); ?></p></div> 
  82. <?php } 
  83.  
  84. public function _old_wpml_tm_warning() { 
  85. ?> 
  86. <div class="message error"><p><?php printf(__('WooCommerce Multilingual is enabled but not effective. It is not compatible with <a href="%s">WPML Translation Management</a> versions prior %s.',  
  87. 'woocommerce-multilingual'), WCML_Links::generate_tracking_link('https://wpml.org/'), '1.9'); ?></p></div> 
  88. <?php } 
  89.  
  90. public function _old_wpml_st_warning() { 
  91. ?> 
  92. <div class="message error"><p><?php printf(__('WooCommerce Multilingual is enabled but not effective. It is not compatible with <a href="%s">WPML String Translation</a> versions prior %s.',  
  93. 'woocommerce-multilingual'), WCML_Links::generate_tracking_link('https://wpml.org/'), '2.0'); ?></p></div> 
  94. <?php } 
  95.  
  96. public function _old_wpml_media_warning() { 
  97. ?> 
  98. <div class="message error"><p><?php printf(__('WooCommerce Multilingual is enabled but not effective. It is not compatible with <a href="%s">WPML Media</a> versions prior %s.',  
  99. 'woocommerce-multilingual'), WCML_Links::generate_tracking_link('https://wpml.org/'), '2.1'); ?></p></div> 
  100. <?php } 
  101.  
  102.  
  103. /** 
  104. * Adds admin notice. 
  105. */ 
  106. public function _missing_plugins_warning() { 
  107.  
  108. $missing = ''; 
  109. $counter = 0; 
  110. foreach ($this->missing as $title => $url) { 
  111. $counter ++; 
  112. if ($counter == sizeof($this->missing)) {  
  113. $sep = ''; 
  114. } elseif ($counter == sizeof($this->missing) - 1) {  
  115. $sep = ' ' . __('and', 'woocommerce-multilingual') . ' '; 
  116. } else {  
  117. $sep = ', '; 
  118. $missing .= '<a href="' . $url . '">' . $title . '</a>' . $sep;  
  119. } ?> 
  120.  
  121. <div class="message error"><p><?php printf(__('WooCommerce Multilingual is enabled but not effective. It requires %s in order to work.', 'woocommerce-multilingual'), $missing); ?></p></div> 
  122. <?php 
  123.  
  124. /** 
  125. * For all the urls to work we need either: 
  126. * 1) the shop page slug must be the same in all languages 
  127. * 2) or the shop prefix disabled in woocommerce settings 
  128. * one of these must be true for product urls to work 
  129. * if none of these are true, display a warning message 
  130. */ 
  131. private function check_for_incompatible_permalinks() {  
  132. global $sitepress, $sitepress_settings, $pagenow; 
  133.  
  134. // WooCommerce 2.x specific checks 
  135. $permalinks = get_option('woocommerce_permalinks', array('product_base' => '')); 
  136. if (empty($permalinks['product_base'])) {  
  137. return; 
  138.  
  139. $message = sprintf('Because this site uses the default permalink structure, you cannot use slug translation for product permalinks.', 'woocommerce-multilingual'); 
  140. $message .= '<br /><br />'; 
  141. $message .= sprintf('Please choose a different permalink structure or disable slug translation.', 'woocommerce-multilingual'); 
  142. $message .= '<br /><br />';  
  143. $message .= '<a href="' . admin_url('options-permalink.php') . '">' . __('Permalink settings', 'woocommerce-multilingual') . '</a>'; 
  144. $message .= ' | '; 
  145. $message .= '<a href="' . admin_url('admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=mcsetup#icl_custom_posts_sync_options') . '">' . __('Configure products slug translation', 'woocommerce-multilingual') . '</a>'; 
  146.  
  147.  
  148. // Check if translated shop pages have the same slug (only 1.x) 
  149. $allsame = true;  
  150. if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) { 
  151. } else { 
  152. $shop_page_id = get_option('woocommerce_shop_page_id', false); 
  153. if (!empty($shop_page_id)) {  
  154. $slug = @get_post($shop_page_id)->post_name; 
  155. $languages = $sitepress->get_active_languages(); 
  156. if (sizeof($languages) < 2) {  
  157. return; 
  158. foreach ($languages as $language) {  
  159. if ($language['code'] != $sitepress->get_default_language()) { 
  160. $translated_shop_page_id = apply_filters( 'translate_object_id', $shop_page_id, 'page', false, $language['code']); 
  161. if (!empty($translated_shop_page_id)) {  
  162. $translated_slug = get_post($translated_shop_page_id)->post_name; 
  163. if (!empty($translated_slug) && $translated_slug != $slug) {  
  164. $allsame = false;  
  165. break; 
  166.  
  167. // Check if slug translation is enabled 
  168. $compatible = true; 
  169. $permalink_structure = get_option('permalink_structure');  
  170. if ( empty($permalink_structure) 
  171. && !empty($sitepress_settings['posts_slug_translation']['on']) 
  172. && !empty($sitepress_settings['posts_slug_translation']['types']) 
  173. && $sitepress_settings['posts_slug_translation']['types']['product']) { 
  174. $compatible = false; 
  175.  
  176. // display messages 
  177. if (!$allsame) { 
  178. $this->err_message = '<div class="message error"><p>'.printf(__('If you want different slugs for shop pages (%s/%s), you need to disable the shop prefix for products in <a href="%s">WooCommerce Settings</a>', 'woocommerce-multilingual'),  
  179. $slug, $translated_slug, admin_url("admin.php?page=woocommerce_settings&tab=pages")).'</p></div>'; 
  180. add_action('admin_notices', array($this, 'plugin_notice_message')); 
  181.  
  182. if (!$compatible && ($pagenow == 'options-permalink.php' || (isset($_GET['page']) && $_GET['page'] == 'wpml-wcml'))) { 
  183. $this->err_message = '<div class="message error"><p>'.$message.' </p></div>'; 
  184. add_action('admin_notices', array($this, 'plugin_notice_message')); 
  185.  
  186. public function plugin_notice_message() { 
  187. echo $this->err_message; 
  188.  
  189. public function fix_strings_language() { 
  190. $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); 
  191. if(!$nonce || !wp_verify_nonce($nonce, 'wcml_fix_strings_language')) { 
  192. die('Invalid nonce'); 
  193.  
  194. $ret = array(); 
  195.  
  196. $ret['_wpnonce'] = wp_create_nonce('icl_sw_form'); 
  197.  
  198. $ret['success_1'] = ' ' . sprintf(__('Finished! You can visit the %sstrings translation%s screen to translate the strings now.', 'woocommerce-multilingual'), '<a href="' . admin_url('admin.php?page=' . WPML_ST_FOLDER . '/menu/string-translation.php') . '">', '</a>'); 
  199.  
  200.  
  201. echo json_encode($ret); 
  202.  
  203. exit; 
  204.  
  205.  
  206. public function check_wpml_config() { 
  207. global $sitepress_settings; 
  208.  
  209. if(empty($sitepress_settings)) return; 
  210.  
  211. $file = realpath(WCML_PLUGIN_PATH . '/wpml-config.xml'); 
  212. if(!file_exists($file)) { 
  213. $this->xml_config_errors[] = __('wpml-config.xml file missing from WooCommerce Multilingual folder.', 'woocommerce-multilingual'); 
  214. }else{ 
  215. $config = icl_xml2array(file_get_contents($file));  
  216.  
  217. if(isset($config['wpml-config'])) { 
  218.  
  219. //custom-fields 
  220. if(isset($config['wpml-config']['custom-fields'])) { 
  221. if(isset($config['wpml-config']['custom-fields']['custom-field']['value'])) { //single 
  222. $cfs[] = $config['wpml-config']['custom-fields']['custom-field']; 
  223. }else{ 
  224. foreach($config['wpml-config']['custom-fields']['custom-field'] as $cf) { 
  225. $cfs[] = $cf; 
  226.  
  227. if($cfs) 
  228. foreach($cfs as $cf) { 
  229. if(!isset($sitepress_settings['translation-management']['custom_fields_translation'][$cf['value']])) continue;  
  230.  
  231. $effective_config_value = $sitepress_settings['translation-management']['custom_fields_translation'][$cf['value']]; 
  232. $correct_config_value = $cf['attr']['action'] == 'copy' ? 1 : ($cf['attr']['action'] == 'translate' ? 2: 0); 
  233.  
  234. if($effective_config_value != $correct_config_value) { 
  235. $this->xml_config_errors[] = sprintf(__('Custom field %s configuration from wpml-config.xml file was altered!', 'woocommerce-multilingual'), '<i>' . $cf['value'] . '</i>'); 
  236.  
  237.  
  238. //custom-types 
  239. if(isset($config['wpml-config']['custom-types'])) { 
  240. if(isset($config['wpml-config']['custom-types']['custom-type']['value'])) { //single 
  241. $cts[] = $config['wpml-config']['custom-types']['custom-type']; 
  242. }else{ 
  243. foreach($config['wpml-config']['custom-types']['custom-type'] as $cf) { 
  244. $cts[] = $cf; 
  245.  
  246. if($cts) 
  247. foreach($cts as $ct) { 
  248. if(!isset($sitepress_settings['custom_posts_sync_option'][$ct['value']])) continue; 
  249. $effective_config_value = $sitepress_settings['custom_posts_sync_option'][$ct['value']]; 
  250. $correct_config_value = $ct['attr']['translate']; 
  251.  
  252. if($effective_config_value != $correct_config_value) { 
  253. $this->xml_config_errors[] = sprintf(__('Custom type %s configuration from wpml-config.xml file was altered!', 'woocommerce-multilingual'), '<i>' . $ct['value'] . '</i>'); 
  254.  
  255.  
  256. //taxonomies 
  257. if(isset($config['wpml-config']['taxonomies'])) { 
  258. if(isset($config['wpml-config']['taxonomies']['taxonomy']['value'])) { //single 
  259. $txs[] = $config['wpml-config']['taxonomies']['taxonomy']; 
  260. }else{ 
  261. foreach($config['wpml-config']['taxonomies']['taxonomy'] as $cf) { 
  262. $txs[] = $cf; 
  263.  
  264. if($txs) 
  265. foreach($txs as $tx) { 
  266. if(!isset($sitepress_settings['taxonomies_sync_option'][$tx['value']])) continue; 
  267. $effective_config_value = $sitepress_settings['taxonomies_sync_option'][$tx['value']]; 
  268. $correct_config_value = $tx['attr']['translate']; 
  269.  
  270. if($effective_config_value != $correct_config_value) { 
  271. $this->xml_config_errors[] = sprintf(__('Custom taxonomy %s configuration from wpml-config.xml file was altered!', 'woocommerce-multilingual'), '<i>' . $tx['value'] . '</i>'); 
  272.  
  273.  
  274.  
  275. public function required_plugin_install_link($repository = 'wpml') { 
  276.  
  277. if( class_exists('WP_Installer_API') ) { 
  278. $url = WP_Installer_API::get_product_installer_link($repository); 
  279. }else{ 
  280. $url = WCML_Links::generate_tracking_link('https://wpml.org/'); 
  281.  
  282. return $url; 
  283.  
  284. /** 
  285. * The support for the Twig templates comes from WPML by default 
  286. * When WPML is not active, WCML will load it 
  287. */ 
  288. private function load_twig_support() { 
  289.  
  290. if ( !class_exists( 'Twig_Autoloader' ) ) { 
  291. Twig_Autoloader::register(); 
  292.  
  293.