YITH_WCWL_Init

Initiator class.

Defined (1)

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

/includes/class.yith-wcwl-init.php  
  1. class YITH_WCWL_Init { 
  2.  
  3. /** 
  4. * Single instance of the class 
  5. * @var \YITH_WCWL_Init 
  6. * @since 2.0.0 
  7. */ 
  8. protected static $instance; 
  9.  
  10. /** 
  11. * CSS selectors used to style buttons. 
  12. * @var array 
  13. * @since 1.0.0 
  14. */ 
  15. public $rules; 
  16.  
  17. /** 
  18. * Front end colors options. 
  19. * @var array 
  20. * @since 1.0.0 
  21. */ 
  22. public $colors_options; 
  23.  
  24. /** 
  25. * Plugin version 
  26. * @var string 
  27. * @since 1.0.0 
  28. */ 
  29. public $version = '2.1.1'; 
  30.  
  31. /** 
  32. * Plugin database version 
  33. * @var string 
  34. * @since 1.0.0 
  35. */ 
  36. public $db_version = '2.0.0'; 
  37.  
  38. /** 
  39. * Positions of the button "Add to Wishlist" 
  40. * @var array 
  41. * @access private 
  42. * @since 1.0.0 
  43. */ 
  44. private $_positions; 
  45.  
  46. /** 
  47. * Store class yith_WCWL_Install. 
  48. * @var object 
  49. * @access private 
  50. * @since 1.0.0 
  51. */ 
  52. private $_yith_wcwl_install; 
  53.  
  54. /** 
  55. * Returns single instance of the class 
  56. * @return \YITH_WCWL_Init 
  57. * @since 2.0.0 
  58. */ 
  59. public static function get_instance() { 
  60. if( is_null( self::$instance ) ) { 
  61. self::$instance = new self(); 
  62.  
  63. return self::$instance; 
  64.  
  65. /** 
  66. * Constructor 
  67. * @since 1.0.0 
  68. */ 
  69. public function __construct() { 
  70. define( 'YITH_WCWL_VERSION', $this->version ); 
  71. define( 'YITH_WCWL_DB_VERSION', $this->db_version ); 
  72.  
  73. $this->_yith_wcwl_install = YITH_WCWL_Install(); 
  74. $this->_positions = apply_filters( 'yith_wcwl_positions', array( 
  75. 'add-to-cart' => array( 'hook' => 'woocommerce_single_product_summary', 'priority' => 31 ),  
  76. 'thumbnails' => array( 'hook' => 'woocommerce_product_thumbnails', 'priority' => 21 ),  
  77. 'summary' => array( 'hook' => 'woocommerce_after_single_product_summary', 'priority' => 11 ) 
  78. ) ); 
  79. $this->rules = apply_filters( 'yith_wcwl_colors_rules', array( 
  80. 'add_to_wishlist' => '.woocommerce a.add_to_wishlist.button.alt',  
  81.  
  82. 'add_to_wishlist_hover' => '.woocommerce a.add_to_wishlist.button.alt:hover',  
  83.  
  84. 'add_to_cart' => '.woocommerce .wishlist_table a.add_to_cart.button.alt',  
  85.  
  86. 'add_to_cart_hover' => '.woocommerce .wishlist_table a.add_to_cart.button.alt:hover',  
  87.  
  88. 'button_style_1' => '.woocommerce a.button.ask-an-estimate-button,  
  89. .woocommerce .hidden-title-form button,  
  90. .yith-wcwl-wishlist-new .create-wishlist-button,  
  91. .wishlist_manage_table tfoot button.submit-wishlist-changes,  
  92. .yith-wcwl-wishlist-search-form button.wishlist-search-button',  
  93.  
  94. 'button_style_1_hover' => '.woocommerce a.button.ask-an-estimate-button:hover,  
  95. .woocommerce .hidden-title-form button:hover,  
  96. .yith-wcwl-wishlist-new .create-wishlist-button:hover,  
  97. .wishlist_manage_table tfoot button.submit-wishlist-changes:hover,  
  98. .yith-wcwl-wishlist-search-form button.wishlist-search-button:hover',  
  99.  
  100. 'button_style_2' => '.woocommerce .wishlist-title a.show-title-form,  
  101. .woocommerce .hidden-title-form a.hide-title-form,  
  102. .wishlist_manage_table tfoot a.create-new-wishlist',  
  103.  
  104. 'button_style_2_hover' => '.woocommerce .wishlist-title a.show-title-form:hover,  
  105. .woocommerce .hidden-title-form a.hide-title-form:hover,  
  106. .wishlist_manage_table tfoot a.create-new-wishlist:hover',  
  107.  
  108. 'wishlist_table' => '.woocommerce table.shop_table.wishlist_table',  
  109.  
  110. 'headers' => '.wishlist_table thead,  
  111. .wishlist_table tfoot,  
  112. .yith-wcwl-wishlist-new,  
  113. .yith-wcwl-wishlist-search-form,  
  114. .widget_yith-wcwl-lists ul.dropdown li.current a,  
  115. .widget_yith-wcwl-lists ul.dropdown li a:hover,  
  116. .selectBox-dropdown-menu.selectBox-options li.selectBox-selected a,  
  117. .selectBox-dropdown-menu.selectBox-options li.selectBox-hover a' 
  118. ) ); 
  119.  
  120. $db_colors = get_option( 'yith_wcwl_frontend_css_colors' ); 
  121.  
  122. $this->colors_options = wp_parse_args( 
  123. maybe_unserialize( $db_colors ),  
  124. apply_filters( 
  125. 'yith_wcwl_colors_options', array( 
  126. 'add_to_wishlist' => array( 'background' => '#333333', 'color' => '#FFFFFF', 'border_color' => '#333333' ),  
  127. 'add_to_wishlist_hover' => array( 'background' => '#4F4F4F', 'color' => '#FFFFFF', 'border_color' => '#4F4F4F' ),  
  128. 'add_to_cart' => array( 'background' => '#333333', 'color' => '#FFFFFF', 'border_color' => '#333333' ),  
  129. 'add_to_cart_hover' => array( 'background' => '#4F4F4F', 'color' => '#FFFFFF', 'border_color' => '#4F4F4F' ),  
  130. 'button_style_1' => array( 'background' => '#333333', 'color' => '#FFFFFF', 'border_color' => '#333333' ),  
  131. 'button_style_1_hover' => array( 'background' => '#4F4F4F', 'color' => '#FFFFFF', 'border_color' => '#4F4F4F' ),  
  132. 'button_style_2' => array( 'background' => '#FFFFFF', 'color' => '#858484', 'border_color' => '#c6c6c6' ),  
  133. 'button_style_2_hover' => array( 'background' => '#4F4F4F', 'color' => '#FFFFFF', 'border_color' => '#4F4F4F' ),  
  134. 'wishlist_table' => array( 'background' => '#FFFFFF', 'color' => '#6d6c6c', 'border_color' => '#FFFFFF' ),  
  135. 'headers' => array( 'background' => '#F4F4F4' ) 
  136. ); 
  137.  
  138. if ( empty( $db_colors ) ) { 
  139. update_option( 'yith_wcwl_frontend_css_colors', maybe_serialize( $this->colors_options ) ); 
  140.  
  141. if ( get_option( 'yith_wcwl_enabled' ) == 'yes' ) { 
  142. add_action( 'init', array( $this, 'init' ), 0 ); 
  143. add_action( 'wp_head', array( $this, 'detect_javascript' ), 0 ); 
  144. add_action( 'wp_head', array( $this, 'add_button' ) ); 
  145. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles_and_stuffs' ) ); 
  146. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  147. add_filter( 'body_class', array( $this, 'add_body_class' ) ); 
  148.  
  149. // add YITH WooCommerce Frequently Bought Together Premium shortcode 
  150. add_action( 'yith_wcwl_after_wishlist_form', array( $this, 'yith_wcfbt_shortcode' ), 10, 1 ); 
  151.  
  152. // YITH WCWL Loaded 
  153. do_action( 'yith_wcwl_loaded' ); 
  154.  
  155. /** 
  156. * Initiator method. 
  157. * @return void 
  158. * @since 1.0.0 
  159. */ 
  160. public function init() { 
  161. // update cookie from old version to new one 
  162. $this->_update_cookies(); 
  163. $this->_destroy_serialized_cookies(); 
  164.  
  165. if ( is_user_logged_in() ) { 
  166. YITH_WCWL()->details['user_id'] = get_current_user_id(); 
  167.  
  168. //check whether any products are added to wishlist, then after login add to the wishlist if not added 
  169. $cookie = yith_getcookie( 'yith_wcwl_products' ); 
  170. if( ! empty( $cookie ) && is_array( $cookie ) ) { 
  171. foreach ( $cookie as $details ) { 
  172. YITH_WCWL()->details['add_to_wishlist'] = $details['prod_id']; 
  173. YITH_WCWL()->details['wishlist_id'] = $details['wishlist_id']; 
  174. YITH_WCWL()->details['quantity'] = $details['quantity']; 
  175. YITH_WCWL()->details['user_id'] = get_current_user_id(); 
  176.  
  177. $ret_val = YITH_WCWL()->add(); 
  178.  
  179. yith_destroycookie( 'yith_wcwl_products' ); 
  180.  
  181. /** 
  182. * Add the "Add to Wishlist" button. Needed to use in wp_head hook. 
  183. * @return void 
  184. * @since 1.0.0 
  185. */ 
  186. public function add_button() { 
  187. global $post; 
  188.  
  189. $this->_positions = apply_filters( 'yith_wcwl_positions', $this->_positions ); 
  190.  
  191. if ( ! isset( $post ) || ! is_object( $post ) ) { 
  192. return; 
  193.  
  194. // Add the link "Add to wishlist" 
  195. $position = get_option( 'yith_wcwl_button_position' ); 
  196. $position = empty( $position ) ? 'add-to-cart' : $position; 
  197.  
  198. if ( $position != 'shortcode' ) { 
  199. add_action( $this->_positions[$position]['hook'], create_function( '', 'echo do_shortcode( "[yith_wcwl_add_to_wishlist]" );' ), $this->_positions[$position]['priority'] ); 
  200.  
  201. // Free the memory. Like it needs a lot of memory... but this is rock! 
  202.  
  203. /** 
  204. * Add specific body class when the Wishlist page is opened 
  205. * @param array $classes 
  206. * @return array 
  207. * @since 1.0.0 
  208. */ 
  209. public function add_body_class( $classes ) { 
  210. $wishlist_page_id = yith_wcwl_object_id( get_option( 'yith_wcwl_wishlist_page_id' ) ); 
  211.  
  212. if ( is_page( $wishlist_page_id ) ) { 
  213. $classes[] = 'woocommerce-wishlist'; 
  214. $classes[] = 'woocommerce'; 
  215. $classes[] = 'woocommerce-page'; 
  216.  
  217. return $classes; 
  218.  
  219. /** 
  220. * Enqueue styles, scripts and other stuffs needed in the <head>. 
  221. * @return void 
  222. * @since 1.0.0 
  223. */ 
  224. public function enqueue_styles_and_stuffs() { 
  225. $woocommerce_base = WC()->template_path(); 
  226. $assets_path = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/'; 
  227.  
  228. $located = locate_template( array( 
  229. $woocommerce_base . 'wishlist.css',  
  230. 'wishlist.css' 
  231. ) ); 
  232.  
  233. wp_register_style( 'jquery-selectBox', YITH_WCWL_URL . 'assets/css/jquery.selectBox.css', array(), '1.2.0' ); 
  234. wp_register_style( 'yith-wcwl-main', YITH_WCWL_URL . 'assets/css/style.css', array(), $this->version ); 
  235. wp_register_style( 'yith-wcwl-user-main', str_replace( get_stylesheet_directory(), get_stylesheet_directory_uri(), $located ), array(), $this->version ); 
  236. wp_register_style( 'yith-wcwl-font-awesome', YITH_WCWL_URL . 'assets/css/font-awesome.min.css', array(), '4.3.0' ); 
  237.  
  238. wp_enqueue_style( 'woocommerce_prettyPhoto_css', $assets_path . 'css/prettyPhoto.css' ); 
  239. wp_enqueue_style( 'jquery-selectBox' ); 
  240.  
  241. if ( ! $located ) { 
  242. wp_enqueue_style( 'yith-wcwl-main' ); 
  243. else { 
  244. wp_enqueue_style( 'yith-wcwl-user-main' ); 
  245.  
  246. wp_enqueue_style( 'yith-wcwl-font-awesome' ); 
  247.  
  248. // Add frontend CSS for buttons 
  249. $colors_styles = array(); 
  250. $frontend_css = ''; 
  251. if ( get_option( 'yith_wcwl_frontend_css' ) == 'no' ) { 
  252. if ( is_array( $this->colors_options ) && ! empty( $this->colors_options ) ) { 
  253. foreach ( $this->colors_options as $name => $option ) { 
  254.  
  255. $colors_styles[ $name ] = ''; 
  256.  
  257. if ( is_array( $option ) && ! empty( $option ) ) { 
  258. foreach ( $option as $id => $value ) { 
  259. $colors_styles[ $name ] .= str_replace( '_', '-', $id ) . ':' . $value . ';'; 
  260.  
  261. if ( is_array( $this->rules ) && ! empty( $this->rules ) ) { 
  262. foreach ( $this->rules as $id => $rule ) { 
  263. $frontend_css .= $rule . '{' . $colors_styles[ $id ] . '}'; 
  264.  
  265. ?> 
  266. <style> 
  267. <?php 
  268. echo get_option( 'yith_wcwl_custom_css' ) . $frontend_css; 
  269.  
  270. if( get_option( 'yith_wcwl_rounded_corners' ) == 'yes' ) { 
  271. echo '.wishlist_table .add_to_cart, a.add_to_wishlist.button.alt { border-radius: 16px; -moz-border-radius: 16px; -webkit-border-radius: 16px; }'; 
  272. ?> 
  273. </style> 
  274. <?php 
  275.  
  276. /** 
  277. * Enqueue plugin scripts. 
  278. * @return void 
  279. * @since 1.0.0 
  280. */ 
  281. public function enqueue_scripts() { 
  282. $woocommerce_base = WC()->template_path(); 
  283. $assets_path = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/'; 
  284. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  285.  
  286. $located = locate_template( array( 
  287. $woocommerce_base . 'wishlist.js',  
  288. 'wishlist.js' 
  289. ) ); 
  290.  
  291. wp_enqueue_script( 'prettyPhoto', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js', array( 'jquery' ), '3.1.6', true ); 
  292. wp_enqueue_script( 'jquery-selectBox', YITH_WCWL_URL . 'assets/js/jquery.selectBox.min.js', array( 'jquery' ), '1.2.0', true ); 
  293. wp_register_script( 'jquery-yith-wcwl', YITH_WCWL_URL . 'assets/js/jquery.yith-wcwl.js', array( 'jquery', 'jquery-selectBox' ), $this->version, true ); 
  294. wp_register_script( 'jquery-yith-wcwl-user', str_replace( get_stylesheet_directory(), get_stylesheet_directory_uri(), $located ), array( 'jquery', 'jquery-selectBox' ), $this->version, true ); 
  295.  
  296. $yith_wcwl_l10n = array( 
  297. 'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ),  
  298. 'redirect_to_cart' => get_option( 'yith_wcwl_redirect_cart' ),  
  299. 'multi_wishlist' => get_option( 'yith_wcwl_multi_wishlist_enable' ) == 'yes' ? true : false,  
  300. 'hide_add_button' => apply_filters( 'yith_wcwl_hide_add_button', true ),  
  301. 'is_user_logged_in' => is_user_logged_in(),  
  302. 'ajax_loader_url' => YITH_WCWL_URL . 'assets/images/ajax-loader.gif',  
  303. 'remove_from_wishlist_after_add_to_cart' => get_option( 'yith_wcwl_remove_after_add_to_cart' ),  
  304. 'labels' => array( 
  305. 'cookie_disabled' => __( 'We are sorry, but this feature is available only if cookies are enabled on your browser.', 'yith-woocommerce-wishlist' ),  
  306. 'added_to_cart_message' => sprintf( '<div class="woocommerce-message">%s</div>', apply_filters( 'yith_wcwl_added_to_cart_message', __( 'Product correctly added to cart', 'yith-woocommerce-wishlist' ) ) ) 
  307. ),  
  308. 'actions' => array( 
  309. 'add_to_wishlist_action' => 'add_to_wishlist',  
  310. 'remove_from_wishlist_action' => 'remove_from_wishlist',  
  311. 'move_to_another_wishlist_action' => 'move_to_another_wishlsit',  
  312. 'reload_wishlist_and_adding_elem_action' => 'reload_wishlist_and_adding_elem' 
  313. ); 
  314.  
  315. if ( ! $located ) { 
  316. wp_enqueue_script( 'jquery-yith-wcwl' ); 
  317. wp_localize_script( 'jquery-yith-wcwl', 'yith_wcwl_l10n', $yith_wcwl_l10n ); 
  318. else { 
  319. wp_enqueue_script( 'jquery-yith-wcwl-user' ); 
  320. wp_localize_script( 'jquery-yith-wcwl-user', 'yith_wcwl_l10n', $yith_wcwl_l10n ); 
  321.  
  322. /** 
  323. * Remove the class no-js when javascript is activated 
  324. * We add the action at the start of head, to do this operation immediatly, without gap of all libraries loading 
  325. * @return void 
  326. * @since 1.0.0 
  327. */ 
  328. public function detect_javascript() { 
  329. if( ! defined( 'YIT' ) ): 
  330. ?> 
  331. <script type="text/javascript">document.documentElement.className = document.documentElement.className + ' yes-js js_active js'</script> 
  332. <?php 
  333. endif; 
  334.  
  335. /** 
  336. * Destroy serialize cookies, to prevent major vulnerability 
  337. * @return void 
  338. * @since 2.0.7 
  339. */ 
  340. private function _destroy_serialized_cookies() { 
  341. $name = 'yith_wcwl_products'; 
  342.  
  343. if ( isset( $_COOKIE[$name] ) && is_serialized( stripslashes( $_COOKIE[ $name ] ) ) ) { 
  344. $_COOKIE[ $name ] = json_encode( array() ); 
  345. yith_destroycookie( $name ); 
  346.  
  347. /** 
  348. * Update old wishlist cookies 
  349. * @return void 
  350. * @since 2.0.0 
  351. */ 
  352. private function _update_cookies() { 
  353. $cookie = yith_getcookie( 'yith_wcwl_products' ); 
  354. $new_cookie = array(); 
  355.  
  356. if( ! empty( $cookie ) ) { 
  357. foreach ( $cookie as $item ) { 
  358. if ( ! isset( $item['add-to-wishlist'] ) ) { 
  359. return; 
  360.  
  361. $new_cookie[] = array( 
  362. 'prod_id' => $item['add-to-wishlist'],  
  363. 'quantity' => isset( $item['quantity'] ) ? $item['quantity'] : 1,  
  364. 'wishlist_id' => false 
  365. ); 
  366.  
  367. yith_setcookie( 'yith_wcwl_products', $new_cookie ); 
  368.  
  369. /** 
  370. * Add Frequently Bought Together shortcode to wishlist page 
  371. * @param mixed $meta 
  372. * @author Francesco Licandro 
  373. */ 
  374. public function yith_wcfbt_shortcode( $meta ) { 
  375.  
  376. if( ! ( defined( 'YITH_WFBT' ) && YITH_WFBT ) || get_option( 'yith_wfbt_enable_integration' ) == 'no' ) { 
  377. return; 
  378.  
  379. $products = YITH_WCWL()->get_products( 
  380. array( 
  381. 'wishlist_id' => is_user_logged_in() ? $meta['ID'] : '' 
  382. )); 
  383.  
  384. $ids = array(); 
  385. // take id of products in wishlist 
  386. foreach( $products as $product ) { 
  387. $ids[] = $product['prod_id']; 
  388.  
  389. if( empty( $ids ) ) { 
  390. return; 
  391.  
  392. do_shortcode( '[yith_wfbt products="' . implode( ', ', $ids ) . '"]' );