YIT_Plugin_Panel_WooCommerce

YIT Plugin Panel for WooCommerce.

Defined (1)

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

/plugin-fw/lib/yit-plugin-panel-wc.php  
  1. class YIT_Plugin_Panel_WooCommerce extends YIT_Plugin_Panel { 
  2.  
  3. /** 
  4. * @var string version of class 
  5. */ 
  6. public $version = '1.0.0'; 
  7.  
  8. /** 
  9. * @var array a setting list of parameters 
  10. */ 
  11. public $settings = array(); 
  12.  
  13. /** 
  14. * @var array a setting list of parameters 
  15. */ 
  16. public $wc_type = array(); 
  17.  
  18. /** 
  19. * @var array 
  20. */ 
  21. protected $_tabs_path_files; 
  22.  
  23. /** 
  24. * Constructor 
  25. * @since 1.0 
  26. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  27. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  28. */ 
  29. public function __construct( $args = array() ) { 
  30.  
  31. $this->wc_type = array( 
  32. 'checkbox',  
  33. 'textarea',  
  34. 'multiselect',  
  35. 'multi_select_countries',  
  36. 'image_width' 
  37. ); 
  38.  
  39. if ( ! empty( $args ) ) { 
  40. $this->settings = $args; 
  41. $this->_tabs_path_files = $this->get_tabs_path_files(); 
  42.  
  43. if( isset( $this->settings['create_menu_page'] ) && $this->settings[ 'create_menu_page'] ) { 
  44. $this->add_menu_page(); 
  45.  
  46. if ( !empty( $this->settings[ 'links' ] ) ) { 
  47. $this->links = $this->settings[ 'links' ]; 
  48.  
  49. add_action( 'admin_init', array( $this, 'set_default_options') ); 
  50. add_action( 'admin_menu', array( $this, 'add_setting_page' ) ); 
  51. add_action( 'admin_menu', array( $this, 'add_premium_version_upgrade_to_menu' ), 100 ); 
  52. add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 100 ); 
  53. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 
  54. add_action( 'admin_init', array( $this, 'woocommerce_update_options' ) ); 
  55. add_filter( 'woocommerce_screen_ids', array( $this, 'add_allowed_screen_id' ) ); 
  56. add_filter( 'woocommerce_admin_settings_sanitize_option', array( $this, 'maybe_unserialize_panel_data' ), 10, 3 ); 
  57.  
  58. /** Add VideoBox and InfoBox */ 
  59. add_action( 'woocommerce_admin_field_boxinfo', array( $this, 'add_infobox' ), 10, 1 ); 
  60. add_action( 'woocommerce_admin_field_videobox', array( $this, 'add_videobox' ), 10, 1 ); 
  61.  
  62. /** WooCommerce 2.4 Support */ 
  63. add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); 
  64.  
  65. /** add YIT Plugin sidebar */ 
  66. $this->sidebar = YIT_Plugin_Panel_Sidebar::instance( $this ); 
  67.  
  68.  
  69. /** 
  70. * Show a tabbed panel to setting page 
  71. * a callback function called by add_setting_page => add_submenu_page 
  72. * @return void 
  73. * @since 1.0 
  74. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  75. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  76. */ 
  77. public function yit_panel() { 
  78. $additional_info = array( 
  79. 'current_tab' => $this->get_current_tab(),  
  80. 'available_tabs' => $this->settings['admin-tabs'],  
  81. 'default_tab' => $this->get_available_tabs( true ), //get default tabs 
  82. 'page' => $this->settings['page'] 
  83. ); 
  84.  
  85. $additional_info = apply_filters( 'yith_admin_tab_params', $additional_info ); 
  86. $additional_info['additional_info'] = $additional_info; 
  87.  
  88. extract( $additional_info ); 
  89. require_once( YIT_CORE_PLUGIN_TEMPLATE_PATH . '/panel/woocommerce/woocommerce-panel.php' ); 
  90.  
  91. /** 
  92. * Show a input fields to upload images 
  93. * @return void 
  94. * @since 1.0 
  95. * @author Emanuela Castorina <emanuela.castorina@yithemes.com> 
  96. */ 
  97.  
  98. public function yit_upload_update( $option_value ) { 
  99. return $option_value; 
  100.  
  101. /** 
  102. * Show a input fields to upload images 
  103. * @return void 
  104. * @since 1.0 
  105. * @author Emanuela Castorina <emanuela.castorina@yithemes.com> 
  106. */ 
  107.  
  108. public function yit_upload( $args = array() ) { 
  109. if ( ! empty( $args ) ) { 
  110. $args['value'] = ( get_option($args['id'])) ? get_option($args['id']) : $args['default']; 
  111. extract( $args ); 
  112.  
  113. include( YIT_CORE_PLUGIN_TEMPLATE_PATH . '/panel/woocommerce/woocommerce-upload.php' ); 
  114.  
  115. /** 
  116. * Add the plugin woocommerce page settings in the screen ids of woocommerce 
  117. * @param $screen_ids 
  118. * @return mixed 
  119. * @since 1.0.0 
  120. * @author Antonino Scarf <antonino.scarfi@yithemes.com> 
  121. */ 
  122. public function add_allowed_screen_id( $screen_ids ) { 
  123. global $admin_page_hooks; 
  124.  
  125. if ( ! isset( $admin_page_hooks[ $this->settings['parent_page'] ] ) ) { 
  126. return $screen_ids; 
  127.  
  128. $screen_ids[] = $admin_page_hooks[ $this->settings['parent_page'] ] . '_page_' . $this->settings['page']; 
  129.  
  130. return $screen_ids; 
  131.  
  132. /** 
  133. * Returns current active tab slug 
  134. * @return string 
  135. * @since 2.0.0 
  136. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  137. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  138. */ 
  139. public function get_current_tab() { 
  140. global $pagenow; 
  141. $tabs = $this->get_available_tabs(); 
  142.  
  143. if ( $pagenow == 'admin.php' && isset( $_REQUEST['tab'] ) && in_array( $_REQUEST['tab'], $tabs ) ) { 
  144. return $_REQUEST['tab']; 
  145. else { 
  146. return $tabs[0]; 
  147.  
  148. /** 
  149. * Return available tabs 
  150. * read all options and show sections and fields 
  151. * @param bool false for all tabs slug, true for current tab 
  152. * @return mixed Array tabs | String current tab 
  153. * @since 1.0 
  154. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  155. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  156. */ 
  157. public function get_available_tabs( $default = false ) { 
  158. $tabs = array_keys( $this->settings['admin-tabs'] ); 
  159. return $default ? $tabs[0] : $tabs; 
  160.  
  161.  
  162. /** 
  163. * Add sections and fields to setting panel 
  164. * read all options and show sections and fields 
  165. * @return void 
  166. * @since 1.0 
  167. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  168. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  169. */ 
  170. public function add_fields() { 
  171. $yit_options = $this->get_main_array_options(); 
  172. $current_tab = $this->get_current_tab(); 
  173.  
  174. if ( ! $current_tab ) { 
  175. return; 
  176.  
  177. woocommerce_admin_fields( $yit_options[$current_tab] ); 
  178.  
  179. /** 
  180. * Print the panel content 
  181. * check if the tab is a wc options tab or custom tab and print the content 
  182. * @return void 
  183. * @since 1.0 
  184. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  185. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  186. * @author Leanza Francesco <leanzafrancesco@gmail.com> 
  187. */ 
  188. public function print_panel_content() { 
  189. $yit_options = $this->get_main_array_options(); 
  190. $current_tab = $this->get_current_tab(); 
  191. $custom_tab_action = $this->is_custom_tab( $yit_options, $current_tab ); 
  192. $hide_sidebar = $this->hide_sidebar( $yit_options, $current_tab ); 
  193.  
  194. if ( $custom_tab_action ) { 
  195. $this->print_custom_tab( $custom_tab_action, $hide_sidebar ); 
  196. return; 
  197. else { 
  198. require_once( YIT_CORE_PLUGIN_TEMPLATE_PATH . '/panel/woocommerce/woocommerce-form.php' ); 
  199.  
  200. /** 
  201. * Update options 
  202. * @return void 
  203. * @since 1.0 
  204. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  205. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  206. * @see woocommerce_update_options function 
  207. * @internal fire two action (before and after update): yit_panel_wc_before_update and yit_panel_wc_after_update 
  208. */ 
  209. public function woocommerce_update_options() { 
  210.  
  211. if ( isset( $_POST['yit_panel_wc_options_nonce'] ) && wp_verify_nonce( $_POST['yit_panel_wc_options_nonce'], 'yit_panel_wc_options_'.$this->settings['page'] ) ) { 
  212.  
  213. do_action( 'yit_panel_wc_before_update' ); 
  214.  
  215. $yit_options = $this->get_main_array_options(); 
  216. $current_tab = $this->get_current_tab(); 
  217.  
  218. if( version_compare( WC()->version, '2.4.0', '>=' ) ) { 
  219. if ( ! empty( $yit_options[ $current_tab ] ) ) { 
  220. foreach ( $yit_options[ $current_tab ] as $option ) { 
  221. if ( isset( $option['id'] ) && isset( $_POST[ $option['id'] ] ) && isset( $option['type' ] ) && ! in_array( $option['type'], $this->wc_type ) ) { 
  222. $_POST[ $option['id'] ] = maybe_serialize( $_POST[ $option['id'] ] ); 
  223.  
  224. foreach($_POST as $name => $value) { 
  225.  
  226. // Check if current POST var name ends with a specific needle and make some stuff here 
  227. $attachment_id_needle = "-yith-attachment-id"; 
  228. $is_hidden_input = (($temp = strlen($name) - strlen($attachment_id_needle)) >= 0 && strpos($name, $attachment_id_needle, $temp) !== FALSE); 
  229. if ($is_hidden_input) { 
  230. // Is an input element of type "hidden" coupled with an input element for selecting an element from the media gallery 
  231. $yit_options[ $current_tab ][$name] = array( 
  232. "type" => "text",  
  233. "id" => $name 
  234. ); 
  235.  
  236. woocommerce_update_options( $yit_options[ $current_tab ] ); 
  237.  
  238. do_action( 'yit_panel_wc_after_update' ); 
  239.  
  240. } elseif( isset( $_REQUEST['yit-action'] ) && $_REQUEST['yit-action'] == 'wc-options-reset' 
  241. && isset( $_POST['yith_wc_reset_options_nonce'] ) && wp_verify_nonce( $_POST['yith_wc_reset_options_nonce'], 'yith_wc_reset_options_'.$this->settings['page'] )) { 
  242.  
  243. do_action( 'yit_panel_wc_before_reset' ); 
  244.  
  245. $yit_options = $this->get_main_array_options(); 
  246. $current_tab = $this->get_current_tab(); 
  247.  
  248. foreach( $yit_options[ $current_tab ] as $id => $option ) { 
  249. if( isset( $option['default'] ) ) { 
  250. update_option( $option['id'], $option['default'] ); 
  251.  
  252. do_action( 'yit_panel_wc_after_reset' ); 
  253.  
  254. /** 
  255. * Add Admin WC Style and Scripts 
  256. * @return void 
  257. * @since 1.0 
  258. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  259. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  260. */ 
  261. public function admin_enqueue_scripts() { 
  262. global $woocommerce, $pagenow; 
  263.  
  264. $woocommerce_version = function_exists( 'WC' ) ? WC()->version : $woocommerce->version; 
  265.  
  266. wp_enqueue_style( 'raleway-font', '//fonts.googleapis.com/css?family=Raleway:400, 500, 600, 700, 800, 100, 200, 300, 900' ); 
  267.  
  268. wp_enqueue_media(); 
  269. wp_enqueue_style( 'woocommerce_admin_styles', $woocommerce->plugin_url() . '/assets/css/admin.css', array(), $woocommerce_version ); 
  270. wp_register_style( 'yit-plugin-style', YIT_CORE_PLUGIN_URL . '/assets/css/yit-plugin-panel.css', $woocommerce_version ); 
  271. wp_register_style( 'colorbox', YIT_CORE_PLUGIN_URL . '/assets/css/colorbox.css', array(), $woocommerce_version ); 
  272. wp_register_style( 'yit-upgrade-to-pro', YIT_CORE_PLUGIN_URL . '/assets/css/yit-upgrade-to-pro.css', array( 'colorbox' ), $woocommerce_version ); 
  273.  
  274. if ( 'customize.php' != $pagenow ) { 
  275. wp_enqueue_style ( 'wp-jquery-ui-dialog' ); 
  276.  
  277. wp_enqueue_style( 'jquery-chosen', YIT_CORE_PLUGIN_URL . '/assets/css/chosen/chosen.css' ); 
  278. wp_enqueue_script( 'jquery-chosen', YIT_CORE_PLUGIN_URL . '/assets/js/chosen/chosen.jquery.js', array( 'jquery' ), '1.1.0', true ); 
  279.  
  280. $woocommerce_settings_deps = array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'iris' ); 
  281.  
  282. if( version_compare( '2.5', $woocommerce_version, '<=' ) ) { 
  283. // WooCommerce > 2.6 
  284. $woocommerce_settings_deps[] = 'select2'; 
  285.  
  286. else { 
  287. // WooCommerce < 2.6 
  288. $woocommerce_settings_deps[] = 'jquery-ui-dialog'; 
  289. $woocommerce_settings_deps[] = 'chosen'; 
  290.  
  291. wp_enqueue_script( 'woocommerce_settings', $woocommerce->plugin_url() . '/assets/js/admin/settings.min.js', $woocommerce_settings_deps, $woocommerce_version, true ); 
  292.  
  293. wp_register_script( 'colorbox', YIT_CORE_PLUGIN_URL . '/assets/js/jquery.colorbox.js', array( 'jquery' ), '1.6.3', true ); 
  294. wp_register_script( 'yit-plugin-panel', YIT_CORE_PLUGIN_URL . '/assets/js/yit-plugin-panel.min.js', array( 'jquery', 'jquery-chosen', 'wp-color-picker', 'jquery-ui-dialog' ), $this->version, true ); 
  295. wp_localize_script( 'woocommerce_settings', 'woocommerce_settings_params', array( 
  296. 'i18n_nav_warning' => __( 'The changes you have made will be lost if you leave this page.', 'yith-plugin-fw' ) 
  297. ) ); 
  298.  
  299. if( 'admin.php' == $pagenow && strpos( get_current_screen()->id, 'yith-plugins_page' ) !== false ) { 
  300. wp_enqueue_style( 'yit-plugin-style' ); 
  301. wp_enqueue_script( 'yit-plugin-panel' ); 
  302.  
  303. if( 'admin.php' == $pagenow && strpos( get_current_screen()->id, 'yith_upgrade_premium_version' ) !== false ) { 
  304. wp_enqueue_style( 'yit-upgrade-to-pro' ); 
  305. wp_enqueue_script( 'colorbox' ); 
  306.  
  307. /** 
  308. * Default options 
  309. * Sets up the default options used on the settings page 
  310. * @access public 
  311. * @return void 
  312. * @since 1.0.0 
  313. */ 
  314. public function set_default_options() { 
  315.  
  316. $default_options = $this->get_main_array_options(); 
  317.  
  318. foreach ($default_options as $section) { 
  319. foreach ( $section as $value ) { 
  320. if ( ( isset( $value['std'] ) || isset( $value['default'] ) ) && isset( $value['id'] ) ) { 
  321. $default_value = ( isset( $value['default'] ) ) ? $value['default'] : $value['std']; 
  322.  
  323. if ( $value['type'] == 'image_width' ) { 
  324. add_option($value['id'].'_width', $default_value); 
  325. add_option($value['id'].'_height', $default_value); 
  326. } else { 
  327. add_option($value['id'], $default_value); 
  328.  
  329.  
  330.  
  331. /** 
  332. * Add the woocommerce body class in plugin panel page 
  333. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  334. * @since 2.0 
  335. * @param $classes The body classes 
  336. * @return array Filtered body classes 
  337. */ 
  338. public function admin_body_class( $admin_body_classes ) { 
  339. global $pagenow; 
  340. return 'admin.php' == $pagenow && substr_count( $admin_body_classes, 'woocommerce' ) == 0 ? $admin_body_classes .= ' woocommerce ' : $admin_body_classes; 
  341.  
  342. /** 
  343. * Maybe unserialize panel data 
  344. * @param $value mixed Option value 
  345. * @param $option mixed Option settings array 
  346. * @param $raw_value string Raw option value 
  347. * @return mixed Filtered return value 
  348. * @author Antonio La Rocca <antonio.larocca@yithemes.com> 
  349. * @since 2.0 
  350. */ 
  351. public function maybe_unserialize_panel_data( $value, $option, $raw_value ) { 
  352.  
  353.  
  354. if( ! version_compare( WC()->version, '2.4.0', '>=' ) || ! isset( $option['type' ] ) || in_array( $option['type'], $this->wc_type ) ) { 
  355. return $value; 
  356.  
  357. $yit_options = $this->get_main_array_options(); 
  358. $current_tab = $this->get_current_tab(); 
  359.  
  360. if( ! empty( $yit_options[ $current_tab ] ) ) { 
  361. foreach( $yit_options[ $current_tab ] as $option_array ) { 
  362. if( isset( $option_array['id'] ) && isset( $option['id'] ) && $option_array['id'] == $option['id'] ) { 
  363. return maybe_unserialize( $value ); 
  364.  
  365. return $value; 
  366.