YITH_Woocompare_Admin

Admin class.

Defined (1)

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

/includes/class.yith-woocompare-admin.php  
  1. class YITH_Woocompare_Admin { 
  2.  
  3.  
  4. /** 
  5. * Plugin options 
  6. * @var array 
  7. * @access public 
  8. * @since 1.0.0 
  9. */ 
  10. public $options = array(); 
  11.  
  12. /** 
  13. * Plugin version 
  14. * @var string 
  15. * @since 1.0.0 
  16. */ 
  17. public $version = YITH_WOOCOMPARE_VERSION; 
  18.  
  19. /** 
  20. * @var $_panel Panel Object 
  21. */ 
  22. protected $_panel; 
  23.  
  24. /** 
  25. * @var $_premium string Premium tab template file name 
  26. */ 
  27. protected $_premium = 'premium.php'; 
  28.  
  29. /** 
  30. * @var string Premium version landing link 
  31. */ 
  32. protected $_premium_landing = 'https://yithemes.com/themes/plugins/yith-woocommerce-compare'; 
  33.  
  34. /** 
  35. * @var string Compare panel page 
  36. */ 
  37. protected $_panel_page = 'yith_woocompare_panel'; 
  38.  
  39. /** 
  40. * Various links 
  41. * @var string 
  42. * @access public 
  43. * @since 1.0.0 
  44. */ 
  45. public $doc_url = 'http://yithemes.com/docs-plugins/yith-woocommerce-compare/'; 
  46.  
  47. /** 
  48. * Constructor 
  49. * @access public 
  50. * @since 1.0.0 
  51. */ 
  52. public function __construct() { 
  53.  
  54. add_action( 'admin_menu', array( $this, 'register_panel' ), 5) ; 
  55.  
  56. //Add action links 
  57. add_filter( 'plugin_action_links_' . plugin_basename( YITH_WOOCOMPARE_DIR . '/' . basename( YITH_WOOCOMPARE_FILE ) ), array( $this, 'action_links' ) ); 
  58. add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 4 ); 
  59.  
  60. add_action( 'yith_woocompare_premium', array( $this, 'premium_tab' ) ); 
  61.  
  62. add_action( 'admin_init', array( $this, 'register_pointer' ) ); 
  63. add_action( 'admin_init', array( $this, 'default_options'), 99 ); 
  64.  
  65. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles_scripts' ), 20 ); 
  66.  
  67. add_action( 'woocommerce_admin_field_woocompare_image_width', array( $this, 'admin_fields_woocompare_image_width' ) ); 
  68. add_action( 'woocommerce_admin_field_woocompare_attributes', array( $this, 'admin_fields_attributes' ), 10, 1 ); 
  69. if( version_compare( preg_replace( '/-beta-([0-9]+)/', '', WC()->version ), '2.4', '<' ) ) { 
  70. add_action( 'woocommerce_update_option_woocompare_attributes', array( $this, 'admin_update_custom_option_pre_24' ), 10, 1 ); 
  71. else { 
  72. add_filter( 'woocommerce_admin_settings_sanitize_option_yith_woocompare_fields_attrs', array( $this, 'admin_update_custom_option' ), 10, 3 ); 
  73.  
  74. // YITH WCWL Loaded 
  75. do_action( 'yith_woocompare_loaded' ); 
  76.  
  77. /** 
  78. * Action Links 
  79. * add the action links to plugin admin page 
  80. * @param $links | links plugin array 
  81. * @return mixed Array 
  82. * @since 1.0 
  83. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  84. * @return mixed 
  85. * @use plugin_action_links_{$plugin_file_name} 
  86. */ 
  87. public function action_links( $links ) { 
  88.  
  89. $links[] = '<a href="' . admin_url( "admin.php?page={$this->_panel_page}" ) . '">' . __( 'Settings', 'yith-woocommerce-compare' ) . '</a>'; 
  90. if ( ! ( defined( 'YITH_WOOCOMPARE_PREMIUM' ) && YITH_WOOCOMPARE_PREMIUM ) ) { 
  91. $links[] = '<a href="' . $this->get_premium_landing_uri() . '" target="_blank">' . __( 'Premium Version', 'yith-woocommerce-compare' ) . '</a>'; 
  92.  
  93. return $links; 
  94.  
  95. /** 
  96. * Add a panel under YITH Plugins tab 
  97. * @return void 
  98. * @since 1.0 
  99. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  100. * @use /Yit_Plugin_Panel class 
  101. * @see plugin-fw/lib/yit-plugin-panel.php 
  102. */ 
  103. public function register_panel() { 
  104.  
  105. if ( ! empty( $this->_panel ) ) { 
  106. return; 
  107.  
  108. $admin_tabs = array( 
  109. 'general' => __( 'Settings', 'yith-woocommerce-compare' ),  
  110. ); 
  111.  
  112. if ( ! ( defined( 'YITH_WOOCOMPARE_PREMIUM' ) && YITH_WOOCOMPARE_PREMIUM ) ) { 
  113. $admin_tabs['premium'] = __( 'Premium Version', 'yith-woocommerce-compare' ); 
  114.  
  115. $args = array( 
  116. 'create_menu_page' => true,  
  117. 'parent_slug' => '',  
  118. 'page_title' => _x( 'Compare', 'Admin Plugin Name', 'yith-woocommerce-compare' ),  
  119. 'menu_title' => _x( 'Compare', 'Admin Plugin Name', 'yith-woocommerce-compare' ),  
  120. 'capability' => 'manage_options',  
  121. 'parent' => '',  
  122. 'parent_page' => 'yit_plugin_panel',  
  123. 'page' => $this->_panel_page,  
  124. 'admin-tabs' => apply_filters( 'yith_woocompare_admin_tabs', $admin_tabs ),  
  125. 'options-path' => YITH_WOOCOMPARE_DIR . '/plugin-options' 
  126. ); 
  127.  
  128.  
  129. /** === Fixed: not updated theme === */ 
  130. if ( ! class_exists( 'YIT_Plugin_Panel_WooCommerce' ) ) { 
  131. require_once( YITH_WOOCOMPARE_DIR . 'plugin-fw/lib/yit-plugin-panel-wc.php' ); 
  132.  
  133. $this->_panel = new YIT_Plugin_Panel_WooCommerce( $args ); 
  134. $this->options = $this->_panel->get_main_array_options(); 
  135.  
  136. /** 
  137. * Set default custom options 
  138. */ 
  139. public function default_options() { 
  140. $this->_default_options(); 
  141.  
  142. /** 
  143. * Premium Tab Template 
  144. * Load the premium tab template on admin page 
  145. * @return void 
  146. * @since 1.0 
  147. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  148. * @return void 
  149. */ 
  150. public function premium_tab() { 
  151. $premium_tab_template = YITH_WOOCOMPARE_TEMPLATE_PATH . '/admin/' . $this->_premium; 
  152. if ( file_exists( $premium_tab_template ) ) { 
  153. include_once( $premium_tab_template ); 
  154.  
  155.  
  156. /** 
  157. * plugin_row_meta 
  158. * add the action links to plugin admin page 
  159. * @param $plugin_meta 
  160. * @param $plugin_file 
  161. * @param $plugin_data 
  162. * @param $status 
  163. * @return Array 
  164. * @since 1.0 
  165. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  166. * @use plugin_row_meta 
  167. */ 
  168. public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) { 
  169.  
  170. if ( defined( 'YITH_WOOCOMPARE_INIT' ) && YITH_WOOCOMPARE_INIT == $plugin_file ) { 
  171. $plugin_meta[] = '<a href="' . $this->doc_url . '" target="_blank">' . __( 'Plugin Documentation', 'yith-woocommerce-compare' ) . '</a>'; 
  172.  
  173. return $plugin_meta; 
  174.  
  175. /** 
  176. * Register Pointer 
  177. */ 
  178. public function register_pointer() { 
  179.  
  180. if( ! class_exists( 'YIT_Pointers' ) ) { 
  181. include_once( 'plugin-fw/lib/yit-pointers.php' ); 
  182.  
  183. $args[] = array( 
  184. 'screen_id' => 'plugins',  
  185. 'pointer_id' => 'yith_woocompare_panel',  
  186. 'target' => '#toplevel_page_yit_plugin_panel',  
  187. 'content' => sprintf( '<h3> %s </h3> <p> %s </p>',  
  188. __( 'YITH WooCommerce Compare Activated', 'yith-woocommerce-compare' ),  
  189. apply_filters( 'yith_woocompare_activated_pointer_content', sprintf( __( 'In the YIT Plugin tab you can find the YITH WooCommerce Compare options. With this menu, you can access to all the settings of our plugins that you have activated. YITH WooCommerce Compare is available in an outstanding PREMIUM version with many new options, <a href="%s">discover it now</a>.', 'yith-woocommerce-compare' ), $this->get_premium_landing_uri() ) ) 
  190. ),  
  191. 'position' => array( 'edge' => 'left', 'align' => 'center' ),  
  192. 'init' => YITH_WOOCOMPARE_INIT 
  193. ); 
  194.  
  195. $args[] = array( 
  196. 'screen_id' => 'update',  
  197. 'pointer_id' => 'yith_woocompare_panel',  
  198. 'target' => '#toplevel_page_yit_plugin_panel',  
  199. 'content' => sprintf( '<h3> %s </h3> <p> %s </p>',  
  200. __( 'YITH WooCommerce Compare Updated', 'yith-woocommerce-compare' ),  
  201. apply_filters( 'yith_woocompare_updated_pointer_content', sprintf( __( 'From now on, you can find all the options of YITH WooCommerce Compare under YIT Plugin -> Compare instead of WooCommerce -> Settings -> Compare, as in the previous version. When one of our plugins is updated, a new voice will be added to this menu. YITH WooCommerce Compare has been updated with new available options, <a href="%s">discover the PREMIUM version.</a>', 'yith-woocommerce-compare' ), $this->get_premium_landing_uri() ) ) 
  202. ),  
  203. 'position' => array( 'edge' => 'left', 'align' => 'center' ),  
  204. 'init' => YITH_WOOCOMPARE_INIT 
  205. ); 
  206.  
  207. YIT_Pointers()->register( $args ); 
  208.  
  209. /** 
  210. * Get the premium landing uri 
  211. * @since 1.0.0 
  212. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  213. * @return string The premium landing link 
  214. */ 
  215. public function get_premium_landing_uri() { 
  216. return defined( 'YITH_REFER_ID' ) ? $this->_premium_landing . '?refer_id=' . YITH_REFER_ID : $this->_premium_landing.'?refer_id=1030585'; 
  217.  
  218. /** 
  219. * Default options 
  220. * Sets up the default options used on the settings page 
  221. * @access protected 
  222. * @return void 
  223. * @since 1.0.0 
  224. */ 
  225. protected function _default_options() { 
  226.  
  227. foreach ( $this->options as $section ) { 
  228.  
  229. foreach ( $section as $value ) { 
  230.  
  231. if ( isset( $value['std'] ) && isset( $value['id'] ) ) { 
  232.  
  233. if ( $value['type'] == 'image_width' ) { 
  234. add_option( $value['id'], $value['std'] ); 
  235. } elseif ( $value['type'] == 'woocompare_attributes' ) { 
  236.  
  237. $value_id = str_replace( '_attrs', '', $value['id'] ); 
  238.  
  239. $in_db = get_option( $value_id ); 
  240. $in_db_original = get_option( $value['id'] ); 
  241.  
  242. // if options is already in db and not reset defaults continue 
  243. if ( $in_db && $in_db_original != 'all' ) { 
  244. continue; 
  245.  
  246. if ( $value['default'] == 'all' ) { 
  247. $fields = YITH_Woocompare_Helper::standard_fields(); 
  248. $all = array(); 
  249.  
  250. foreach ( array_keys( $fields ) as $field ) { 
  251. $all[ $field ] = true; 
  252.  
  253. update_option( $value_id, $all ); 
  254. } else { 
  255. update_option( $value_id, $value['std'] ); 
  256.  
  257. /** 
  258. * Create new Woocommerce admin field: checkboxes 
  259. * @access public 
  260. * @param array $value 
  261. * @return void 
  262. * @since 1.0.0 
  263. */ 
  264. public function admin_fields_attributes( $value ) { 
  265. $fields = YITH_Woocompare_Helper::standard_fields(); 
  266. $all = array(); 
  267. $checked = get_option( str_replace( '_attrs', '', $value['id'] ), $value['default'] == 'all' ? $all : array() ); 
  268.  
  269. foreach ( array_keys( $fields ) as $field ) { 
  270. $all[ $field ] = true; 
  271. // then add fields that are not still saved 
  272. foreach ( $checked as $k => $v ) { 
  273. unset( $all[ $k ] ); 
  274. $checkboxes = array_merge( $checked, $all ); 
  275.  
  276.  
  277. ?> 
  278. <tr valign="top"> 
  279. <th scope="row" class="titledesc"> 
  280. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo $value['name']; ?></label> 
  281. </th> 
  282.  
  283. <td class="forminp attributes"> 
  284. <p class="description"><?php echo $value['desc'] ?></p> 
  285. <ul class="fields"> 
  286. <?php foreach ( $checkboxes as $slug => $checked ) : 
  287. if( ! isset( $fields[ $slug ] ) ) 
  288. continue; 
  289. ?> 
  290. <li> 
  291. <label> 
  292. <input type="checkbox" name="<?php echo $value['id'] ?>[]" id="<?php echo $value['id'] ?>_<?php echo $slug ?>" value="<?php echo $slug ?>"<?php checked( $checked ) ?> /> <?php echo $fields[ $slug ] ?> 
  293. </label> 
  294. </li> 
  295. <?php 
  296. endforeach; 
  297. ?> 
  298. </ul> 
  299. <input type="hidden" name="<?php echo $value['id'] ?>_positions" value="<?php echo implode( ', ', array_keys( $checkboxes ) ) ?>" /> 
  300. </td> 
  301. </tr> 
  302. <?php 
  303.  
  304. /** 
  305. * Create new Woocommerce admin field: yit_wc_image_width 
  306. * @access public 
  307. * @param array $value 
  308. * @return void 
  309. * @since 1.0.0 
  310. */ 
  311. public function admin_fields_woocompare_image_width( $value ) { 
  312.  
  313. $width = WC_Admin_Settings::get_option( $value['id'] . '[width]', $value['default']['width'] ); 
  314. $height = WC_Admin_Settings::get_option( $value['id'] . '[height]', $value['default']['height'] ); 
  315. $crop = WC_Admin_Settings::get_option( $value['id'] . '[crop]' ); 
  316. $crop = ( $crop == 'on' || $crop == '1' ) ? 1 : 0; 
  317. $crop = checked( 1, $crop, false ); 
  318.  
  319. ?> 
  320. <tr valign="top"> 
  321. <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th> 
  322. <td class="forminp image_width_settings"> 
  323.  
  324. <input name="<?php echo esc_attr( $value['id'] ); ?>[width]" id="<?php echo esc_attr( $value['id'] ); ?>-width" type="text" size="3" value="<?php echo $width; ?>" /> × 
  325. <input name="<?php echo esc_attr( $value['id'] ); ?>[height]" id="<?php echo esc_attr( $value['id'] ); ?>-height" type="text" size="3" value="<?php echo $height; ?>" />px 
  326.  
  327. <label><input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" <?php echo $crop; ?> /> <?php _e( 'Do you want to hard crop the image?', 'yith-woocommerce-compare' ); ?> 
  328. </label> 
  329. <p class="description"><?php echo $value['desc'] ?></p> 
  330.  
  331. </td> 
  332. </tr><?php 
  333.  
  334.  
  335. /** 
  336. * Save the admin field: slider 
  337. * @access public 
  338. * @param mixed $value 
  339. * @param mixed $option 
  340. * @param mixed $raw_value 
  341. * @return mixed 
  342. * @since 1.0.0 
  343. */ 
  344. public function admin_update_custom_option( $value, $option, $raw_value ) { 
  345.  
  346. $val = array(); 
  347. $checked_fields = isset( $_POST[ $option['id'] ] ) ? maybe_unserialize( $_POST[ $option['id'] ] ) : array(); 
  348. $fields = array_map( 'trim', explode( ', ', $_POST[ $option['id'] . '_positions' ] ) ); 
  349.  
  350. foreach ( $fields as $field ) { 
  351. $val[ $field ] = in_array( $field, $checked_fields ); 
  352.  
  353. update_option( str_replace( '_attrs', '', $option['id'] ), $val ); 
  354.  
  355. return $value; 
  356.  
  357. /** 
  358. * Save the admin field: slider 
  359. * @access public 
  360. * @param mixed $value 
  361. * @return void 
  362. * @since 1.0.0 
  363. */ 
  364. public function admin_update_custom_option_pre_24( $value ) { 
  365.  
  366. $val = array(); 
  367. $checked_fields = isset( $_POST[ $value['id'] ] ) ? $_POST[ $value['id'] ] : array(); 
  368. $fields = array_map( 'trim', explode( ', ', $_POST[ $value['id'] . '_positions' ] ) ); 
  369.  
  370. foreach ( $fields as $field ) { 
  371. $val[ $field ] = in_array( $field, $checked_fields ); 
  372.  
  373. update_option( str_replace( '_attrs', '', $value['id'] ), $val ); 
  374.  
  375. /** 
  376. * Enqueue admin styles and scripts 
  377. * @access public 
  378. * @return void 
  379. * @since 1.0.0 
  380. */ 
  381. public function enqueue_styles_scripts() { 
  382.  
  383. if ( isset( $_GET['page'] ) && $_GET['page'] == 'yith_woocompare_panel' ) { 
  384. wp_enqueue_script( 'jquery-ui' ); 
  385. wp_enqueue_script( 'jquery-ui-core' ); 
  386. wp_enqueue_script( 'jquery-ui-mouse' ); 
  387. wp_enqueue_script( 'jquery-ui-slider' ); 
  388. wp_enqueue_script( 'jquery-ui-sortable' ); 
  389.  
  390. wp_enqueue_style( 'yith_woocompare_admin', YITH_WOOCOMPARE_URL . 'assets/css/admin.css' ); 
  391. wp_enqueue_script( 'yith_woocompare', YITH_WOOCOMPARE_URL . 'assets/js/woocompare-admin.js', array( 'jquery', 'jquery-ui-sortable' ) ); 
  392.  
  393. do_action( 'yith_woocompare_enqueue_styles_scripts' );