/inc/admin-menus/class-wcml-admin-menus.php

  1. <?php 
  2.  
  3. class WCML_Admin_Menus{ 
  4.  
  5. private static $woocommerce_wpml; 
  6. private static $sitepress; 
  7. private static $wpdb; 
  8.  
  9. public static function set_up_menus( &$woocommerce_wpml, &$sitepress, &$wpdb, $check_dependencies ) { 
  10. self::$woocommerce_wpml =& $woocommerce_wpml; 
  11. self::$sitepress =& $sitepress; 
  12. self::$wpdb =& $wpdb; 
  13.  
  14. add_action( 'admin_menu', array(__CLASS__, 'register_menus' ), 80 ); 
  15.  
  16. if( self::is_page_without_admin_language_switcher() ) { 
  17. self::remove_wpml_admin_language_switcher(); 
  18.  
  19. if( is_admin() && !is_null( $sitepress ) && $check_dependencies ) { 
  20. add_action( 'admin_footer', array(__CLASS__, 'documentation_links' ) ); 
  21. add_action( 'admin_head', array( __CLASS__, 'hide_multilingual_content_setup_box' ) ); 
  22. add_action( 'admin_init', array( __CLASS__, 'restrict_admin_with_redirect' ) ); 
  23.  
  24. add_filter( 'woocommerce_prevent_admin_access', array( __CLASS__, 'check_user_admin_access' ) ); 
  25.  
  26. add_action( 'admin_head', array( __CLASS__, 'add_menu_warning' ) ); 
  27.  
  28. public static function register_menus() { 
  29. global $WPML_Translation_Management; 
  30.  
  31. if( self::$woocommerce_wpml->check_dependencies || class_exists( 'WooCommerce' ) ) { 
  32.  
  33. add_submenu_page( 
  34. 'woocommerce',  
  35. __( 'WooCommerce Multilingual', 'woocommerce-multilingual' ),  
  36. __( 'WooCommerce Multilingual', 'woocommerce-multilingual' ),  
  37. 'wpml_operate_woocommerce_multilingual',  
  38. 'wpml-wcml',  
  39. array( __CLASS__, 'render_menus' ) 
  40. ); 
  41.  
  42. if( !current_user_can('wpml_manage_woocommerce_multilingual') && current_user_can('wpml_operate_woocommerce_multilingual') ) { 
  43. //force add translations-queue page for shop manager 
  44. $wp_api = self::$sitepress->get_wp_api(); 
  45. if (!$wp_api->current_user_can('wpml_manage_translation_management')) { 
  46. $wp_api->add_submenu_page(null,  
  47. __('Translations', 'wpml-translation-management'), __('Translations', 'wpml-translation-management'),  
  48. 'wpml_operate_woocommerce_multilingual', WPML_TM_FOLDER . '/menu/translations-queue.php', array($WPML_Translation_Management, 'translation_queue_page')); 
  49. } else { 
  50.  
  51. add_menu_page( 
  52. __( 'WooCommerce Multilingual', 'woocommerce-multilingual' ),  
  53. __( 'WooCommerce Multilingual', 'woocommerce-multilingual' ),  
  54. 'wpml_manage_woocommerce_multilingual',  
  55. 'wpml-wcml',  
  56. array( __CLASS__, 'render_menus' ),  
  57. WCML_PLUGIN_URL . '/res/images/icon16.png' 
  58. ); 
  59.  
  60.  
  61. public static function render_menus() { 
  62.  
  63. if( self::$woocommerce_wpml->check_dependencies ) { 
  64. $menus_wrap = new WCML_Menus_Wrap( self::$woocommerce_wpml ); 
  65. $menus_wrap->show(); 
  66. }else{ 
  67. global $sitepress; 
  68. $plugins_wrap = new WCML_Plugins_Wrap( self::$woocommerce_wpml, $sitepress ); 
  69. $plugins_wrap->show(); 
  70.  
  71.  
  72. private static function is_page_without_admin_language_switcher() { 
  73. global $pagenow; 
  74.  
  75. $get_post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : false; 
  76. $get_post = isset( $_GET['post'] ) ? $_GET['post'] : false; 
  77. $get_page = isset( $_GET['page'] ) ? $_GET['page'] : false; 
  78.  
  79. $is_page_wpml_wcml = isset($_GET['page']) && $_GET['page'] == 'wpml-wcml'; 
  80. $is_new_order_or_coupon = in_array( $pagenow, array( 'edit.php', 'post-new.php' ) ) && 
  81. $get_post_type && 
  82. in_array( $get_post_type, array( 'shop_coupon', 'shop_order' ) ); 
  83. $is_edit_order_or_coupon = $pagenow == 'post.php' && $get_post && 
  84. in_array( get_post_type( $get_post ), array( 'shop_coupon', 'shop_order' ) ); 
  85. $is_shipping_zones = $get_page == 'shipping_zones'; 
  86. $is_attributes_page = apply_filters( 'wcml_is_attributes_page', $get_page == 'product_attributes' ); 
  87.  
  88.  
  89. return is_admin() && ( 
  90. $is_page_wpml_wcml || 
  91. $is_new_order_or_coupon || 
  92. $is_edit_order_or_coupon || 
  93. $is_shipping_zones || 
  94. $is_attributes_page 
  95. ); 
  96.  
  97.  
  98. public static function remove_wpml_admin_language_switcher() { 
  99.  
  100. remove_action( 'wp_before_admin_bar_render', array(self::$sitepress, 'admin_language_switcher') ); 
  101.  
  102.  
  103. public static function documentation_links() { 
  104. global $post, $pagenow; 
  105.  
  106. if ( is_null( $post ) ) 
  107. return; 
  108.  
  109. $get_post_type = get_post_type( $post->ID ); 
  110.  
  111. if ( $get_post_type == 'product' && $pagenow == 'edit.php' ) { 
  112. $quick_edit_notice = '<div id="quick_edit_notice" style="display:none;"><p>' . 
  113. sprintf( __( "Quick edit is disabled for product translations. It\'s recommended to use the %s for editing products translations. %s",  
  114. 'woocommerce-multilingual' ), '<a href="' . admin_url( 'admin.php?page=wpml-wcml&tab=products' ) . '" >' . 
  115. __( 'WooCommerce Multilingual products editor', 'woocommerce-multilingual' ) . '</a>',  
  116. '<a href="" class="quick_product_trnsl_link" >' . __( 'Edit this product translation', 'woocommerce-multilingual' ) . '</a>' 
  117. ) . '</p></div>'; 
  118. $quick_edit_notice_prod_link = '<input type="hidden" id="wcml_product_trnsl_link" value="' . admin_url( 'admin.php?page=wpml-wcml&tab=products&prid=' ) . '">'; 
  119. ?> 
  120. <script type="text/javascript"> 
  121. jQuery(".subsubsub").append('<?php echo $quick_edit_notice ?>'); 
  122. jQuery(".subsubsub").append('<?php echo $quick_edit_notice_prod_link ?>'); 
  123. jQuery(".quick_hide a").on('click', function () { 
  124. jQuery(".quick_product_trnsl_link").attr('href', jQuery("#wcml_product_trnsl_link").val() + jQuery(this).closest('tr').attr('id').replace(/post-/, '')); 
  125. }); 
  126.  
  127. //lock feature for translations 
  128. jQuery(document).on('click', '.featured a', function () { 
  129. if (jQuery(this).closest('tr').find('.quick_hide').size() > 0) { 
  130. return false; 
  131.  
  132. }); 
  133. </script> 
  134. <?php 
  135.  
  136. if ( isset($_GET['taxonomy']) ) { 
  137. $pos = strpos( $_GET['taxonomy'], 'pa_' ); 
  138.  
  139. if ( $pos !== false && $pagenow == 'edit-tags.php' ) { 
  140. $prot_link = '<span class="button" style="padding:4px;margin-top:0px; float: left;"><img align="baseline" src="' . ICL_PLUGIN_URL . '/res/img/icon16.png" width="16" height="16" style="margin-bottom:-4px" /> <a href="' . WCML_Links::generate_tracking_link( 'https://wpml.org/documentation/related-projects/woocommerce-multilingual/', 'woocommerce-multilingual', 'documentation', '#3' ) . '" target="_blank" style="text-decoration: none;">' . 
  141. __( 'How to translate attributes', 'sitepress' ) . '<\/a>' . '<\/span><br \/><br \/>'; 
  142. ?> 
  143. <script type="text/javascript"> 
  144. jQuery("table.widefat").before('<?php echo $prot_link ?>'); 
  145. </script> 
  146. <?php 
  147.  
  148. if ( isset($_GET['taxonomy']) && $_GET['taxonomy'] == 'product_cat' ) { 
  149.  
  150. $prot_link = '<span class="button" style="padding:4px;margin-top:0px; float: left;"><img align="baseline" src="' . ICL_PLUGIN_URL . '/res/img/icon16.png" width="16" height="16" style="margin-bottom:-4px" /> <a href="' . WCML_Links::generate_tracking_link( 'https://wpml.org/documentation/related-projects/woocommerce-multilingual/', 'woocommerce-multilingual', 'documentation', '#3' ) . '" target="_blank" style="text-decoration: none;">' . 
  151. __( 'How to translate product categories', 'sitepress' ) . '<\/a>' . '<\/span><br \/><br \/>'; 
  152. ?> 
  153. <script type="text/javascript"> 
  154. jQuery("table.widefat").before('<?php echo $prot_link ?>'); 
  155. </script> 
  156. <?php 
  157.  
  158. public static function hide_multilingual_content_setup_box() { 
  159. remove_meta_box('icl_div_config', convert_to_screen('shop_order'), 'normal'); 
  160. remove_meta_box('icl_div_config', convert_to_screen('shop_coupon'), 'normal'); 
  161.  
  162. public static function restrict_admin_with_redirect() { 
  163. global $pagenow; 
  164.  
  165. $default_lang = self::$sitepress->get_default_language(); 
  166. $current_lang = self::$sitepress->get_current_language(); 
  167.  
  168. if( 
  169. ( $pagenow == 'post.php' && isset( $_GET[ 'post' ] ) ) || 
  170. ( $pagenow == 'admin.php' && 
  171. isset( $_GET[ 'action' ] ) && 
  172. $_GET[ 'action'] == 'duplicate_product' && 
  173. isset( $_GET[ 'post' ] ) 
  174. ) { 
  175. $prod_lang = self::$sitepress->get_language_for_element( $_GET[ 'post' ], 'post_product' ); 
  176.  
  177. if( 
  178. !self::$woocommerce_wpml->settings[ 'trnsl_interface' ] && 
  179. $pagenow == 'post.php' && 
  180. isset( $_GET[ 'post' ] )&& 
  181. get_post_type( $_GET[ 'post' ] ) == 'product' && 
  182. !self::$woocommerce_wpml->products->is_original_product( $_GET[ 'post' ] ) ) 
  183. add_action( 'admin_notices', array( __CLASS__, 'inf_editing_product_in_non_default_lang' ) ); 
  184.  
  185. if( 
  186. self::$woocommerce_wpml->settings[ 'trnsl_interface' ] && 
  187. $pagenow == 'post.php' && 
  188. !is_ajax() && 
  189. isset( $_GET[ 'post' ] ) && 
  190. !self::$woocommerce_wpml->products->is_original_product( $_GET[ 'post' ] ) && 
  191. get_post_type( $_GET[ 'post' ] ) == 'product' 
  192. ) { 
  193. if( 
  194. !isset( $_GET[ 'action' ] ) || 
  195. ( isset( $_GET[ 'action' ] ) && !in_array( $_GET[ 'action' ], array( 'trash', 'delete' ) ) ) 
  196. ) { 
  197. $prid = (int)$_GET[ 'post' ]; 
  198. wp_redirect( admin_url( 'admin.php?page=wpml-wcml&tab=products&prid='.$prid ) ); 
  199. exit; 
  200.  
  201. if( 
  202. self::$woocommerce_wpml->settings[ 'trnsl_interface' ] && 
  203. $pagenow == 'admin.php' && 
  204. isset( $_GET[ 'action' ] ) && 
  205. $_GET[ 'action' ] == 'duplicate_product' && 
  206. $default_lang != $prod_lang ) 
  207. wp_redirect( admin_url( 'admin.php?page=wpml-wcml&tab=products' ) ); 
  208. exit; 
  209.  
  210. public static function inf_editing_product_in_non_default_lang() { 
  211. if( !self::$woocommerce_wpml->settings[ 'dismiss_tm_warning' ] ) { 
  212. $url = $_SERVER['REQUEST_URI']; 
  213.  
  214. $message = '<div class="message error otgs-is-dismissible"><p>'; 
  215. $message .= sprintf( 
  216. __('The recommended way to translate WooCommerce products is using the 
  217. %sWooCommerce Multilingual products translation%s page. 
  218. Please use this page only for translating elements that are not available in the WooCommerce Multilingual products translation table.',  
  219. 'woocommerce-multilingual'),  
  220. '<strong><a href="' . admin_url('admin.php?page=wpml-wcml&tab=products') . '">', '</a></strong>'); 
  221. $message .= '</p><a class="notice-dismiss" href="' . $url . '&wcml_action=dismiss_tm_warning"><span class="screen-reader-text">' . __('Dismiss', 'woocommerce-multilingual') . '</a>'; 
  222. $message .= '</div>'; 
  223.  
  224. echo $message; 
  225.  
  226. public static function check_user_admin_access( $prevent_access ) { 
  227.  
  228. if( self::$woocommerce_wpml->check_dependencies ) { 
  229. $user_lang_pairs = get_user_meta( get_current_user_id(), self::$wpdb->prefix.'language_pairs', true ); 
  230. if( current_user_can( 'wpml_manage_woocommerce_multilingual' ) || !empty( $user_lang_pairs ) ) { 
  231. return false; 
  232.  
  233. return $prevent_access; 
  234.  
  235. public static function add_menu_warning() { 
  236. global $submenu, $menu; 
  237.  
  238. if ( 
  239. class_exists( 'WooCommerce' ) && 
  240. empty( self::$woocommerce_wpml->settings['set_up_wizard_run'] ) || 
  241. empty( self::$woocommerce_wpml->settings['set_up_wizard_run'] ) && 
  242. self::$woocommerce_wpml->settings['set_up_wizard_splash'] 
  243. ) { 
  244. if( isset( $submenu[ 'woocommerce' ] ) ) { 
  245. foreach( $submenu[ 'woocommerce' ] as $key => $menu_item ) { 
  246. if ( $menu_item[ 0 ] == __( 'WooCommerce Multilingual', 'woocommerce-multilingual' ) ) { 
  247. $submenu[ 'woocommerce' ][ $key ][ 0 ] .= '<span class="wcml-menu-warn"><i class="otgs-ico-warning"></i></span>'; 
  248. break; 
  249.  
  250. foreach( $menu as $key => $menu_item ) { 
  251. if ( $menu_item[ 0 ] == __( 'WooCommerce', 'woocommerce' ) ) { 
  252. $menu[ $key ][ 0 ] .= '<span class="wcml-menu-warn"><i class="otgs-ico-warning"></i></span>'; 
  253. break; 
  254.  
.