YITH_WCWL

WooCommerce Wishlist.

Defined (1)

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

/includes/class.yith-wcwl.php  
  1. class YITH_WCWL { 
  2. /** 
  3. * Single instance of the class 
  4. * @var \YITH_WCWL 
  5. * @since 2.0.0 
  6. */ 
  7. protected static $instance; 
  8.  
  9. /** 
  10. * Errors array 
  11. *  
  12. * @var array 
  13. * @since 1.0.0 
  14. */ 
  15. public $errors; 
  16.  
  17. /** 
  18. * Last operation token 
  19. * @var string 
  20. * @since 2.0.0 
  21. */ 
  22. public $last_operation_token; 
  23.  
  24. /** 
  25. * Details array 
  26. *  
  27. * @var array 
  28. * @since 1.0.0 
  29. */ 
  30. public $details; 
  31.  
  32. /** 
  33. * Messages array 
  34. *  
  35. * @var array 
  36. * @since 1.0.0 
  37. */ 
  38. public $messages; 
  39.  
  40. /** 
  41. * Returns single instance of the class 
  42. * @return \YITH_WCWL 
  43. * @since 2.0.0 
  44. */ 
  45. public static function get_instance() { 
  46. if( is_null( self::$instance ) ) { 
  47. self::$instance = new self( $_REQUEST ); 
  48.  
  49. return self::$instance; 
  50.  
  51. /** 
  52. * Constructor. 
  53. *  
  54. * @param array $details 
  55. * @return \YITH_WCWL 
  56. * @since 1.0.0 
  57. */ 
  58. public function __construct( $details ) { 
  59. // set details for actions 
  60. $this->details = $details; 
  61.  
  62. // init main plugin classes 
  63. $this->wcwl_init = YITH_WCWL_Init(); 
  64.  
  65. if( is_admin() ) { 
  66. $this->wcwl_admin_init = YITH_WCWL_Admin_Init(); 
  67.  
  68. // load plugin-fw 
  69. add_action( 'plugins_loaded', array( $this, 'plugin_fw_loader' ), 15 ); 
  70.  
  71. // add rewrite rule 
  72. add_action( 'init', array( $this, 'add_rewrite_rules' ), 0 ); 
  73. add_filter( 'query_vars', array( $this, 'add_public_query_var' ) ); 
  74.  
  75. // add to wishlist 
  76. add_action( 'init', array( $this, 'add_to_wishlist' ) ); 
  77. add_action( 'wp_ajax_add_to_wishlist', array( $this, 'add_to_wishlist_ajax' ) ); 
  78. add_action( 'wp_ajax_nopriv_add_to_wishlist', array( $this, 'add_to_wishlist_ajax' ) ); 
  79.  
  80. // remove from wishlist 
  81. add_action( 'init', array( $this, 'remove_from_wishlist' ) ); 
  82. add_action( 'wp_ajax_remove_from_wishlist', array( $this, 'remove_from_wishlist_ajax' ) ); 
  83. add_action( 'wp_ajax_nopriv_remove_from_wishlist', array( $this, 'remove_from_wishlist_ajax' ) ); 
  84.  
  85. // add to wishlist and reload 
  86. add_action( 'wp_ajax_reload_wishlist_and_adding_elem', array( $this, 'reload_wishlist_and_adding_elem_ajax' ) ); 
  87. add_action( 'wp_ajax_nopriv_reload_wishlist_and_adding_elem', array( $this, 'reload_wishlist_and_adding_elem_ajax' ) ); 
  88.  
  89. // remove from wishlist after add to cart 
  90. add_action( 'woocommerce_add_to_cart', array( $this, 'remove_from_wishlist_after_add_to_cart' ) ); 
  91.  
  92. // redirect to cart after add to cart in wishlist view 
  93. add_filter( 'woocommerce_product_add_to_cart_url', array( $this, 'redirect_to_cart' ), 10, 2 ); 
  94. add_filter( 'woocommerce_add_to_cart_redirect', array( $this, 'yith_wfbt_redirect_after_add_to_cart' ), 10, 1 ); 
  95.  
  96. // print notices in wishlist page 
  97. add_action( 'yith_wcwl_before_wishlist_title', array( $this, 'print_notices' ) ); 
  98.  
  99. // add filter for font-awesome compatibility 
  100. add_filter( 'option_yith_wcwl_add_to_wishlist_icon', array( $this, 'update_font_awesome_classes' ) ); 
  101. add_filter( 'option_yith_wcwl_add_to_cart_icon', array( $this, 'update_font_awesome_classes' ) ); 
  102.  
  103. /** === PLUGIN FW LOADER === */ 
  104.  
  105. /** 
  106. * Loads plugin fw, if not yet created 
  107. * @return void 
  108. * @since 2.0.0 
  109. */ 
  110. public function plugin_fw_loader() { 
  111. if ( ! defined( 'YIT_CORE_PLUGIN' ) ) { 
  112. global $plugin_fw_data; 
  113. if( ! empty( $plugin_fw_data ) ) { 
  114. $plugin_fw_file = array_shift( $plugin_fw_data ); 
  115. require_once( $plugin_fw_file ); 
  116.  
  117. /** === ITEMS METHODS === */ 
  118.  
  119. /** 
  120. * Add a product in the wishlist. 
  121. *  
  122. * @return string "error", "true" or "exists" 
  123. * @since 1.0.0 
  124. */ 
  125. public function add() { 
  126. global $wpdb, $sitepress; 
  127. $prod_id = ( isset( $this->details['add_to_wishlist'] ) && is_numeric( $this->details['add_to_wishlist'] ) ) ? $this->details['add_to_wishlist'] : false; 
  128. $wishlist_id = ( isset( $this->details['wishlist_id'] ) && strcmp( $this->details['wishlist_id'], 0 ) != 0 ) ? $this->details['wishlist_id'] : false; 
  129. $quantity = ( isset( $this->details['quantity'] ) ) ? (int) $this->details['quantity'] : 1; 
  130. $user_id = ( ! empty( $this->details['user_id'] ) ) ? $this->details['user_id'] : false; 
  131. $wishlist_name = ( ! empty( $this->details['wishlist_name'] ) ) ? $this->details['wishlist_name'] : ''; 
  132.  
  133. do_action( 'yith_wcwl_adding_to_wishlist', $prod_id, $wishlist_id, $user_id ); 
  134.  
  135. // filtering params 
  136. $prod_id = apply_filters( 'yith_wcwl_adding_to_wishlist_prod_id', $prod_id ); 
  137. $wishlist_id = apply_filters( 'yith_wcwl_adding_to_wishlist_wishlist_id', $wishlist_id ); 
  138. $quantity = apply_filters( 'yith_wcwl_adding_to_wishlist_quantity', $quantity ); 
  139. $user_id = apply_filters( 'yith_wcwl_adding_to_wishlist_user_id', $user_id ); 
  140. $wishlist_name = apply_filters( 'yith_wcwl_adding_to_wishlist_wishlist_name', $wishlist_name ); 
  141.  
  142. if( defined('ICL_SITEPRESS_VERSION') ) { 
  143. $prod_id = yit_wpml_object_id( $prod_id, 'product', true, $sitepress->get_default_language() ); 
  144.  
  145. if ( $prod_id == false ) { 
  146. $this->errors[] = __( 'An error occurred while adding products to the wishlist.', 'yith-woocommerce-wishlist' ); 
  147. return "error"; 
  148.  
  149. //check for existence, product ID, variation ID, variation data, and other cart item data 
  150. if( strcmp( $wishlist_id, 'new' ) != 0 && $this->is_product_in_wishlist( $prod_id, $wishlist_id ) ) { 
  151. if( $wishlist_id != false ) { 
  152. $wishlist = $this->get_wishlist_detail( $wishlist_id ); 
  153. $this->last_operation_token = $wishlist['wishlist_token']; 
  154. else{ 
  155. $this->last_operation_token = false; 
  156.  
  157. return "exists"; 
  158.  
  159. if( $user_id != false ) { 
  160.  
  161. $insert_args = array( 
  162. 'prod_id' => $prod_id,  
  163. 'user_id' => $user_id,  
  164. 'quantity' => $quantity,  
  165. 'dateadded' => date( 'Y-m-d H:i:s' ) 
  166. ); 
  167.  
  168. if( ! empty( $wishlist_id ) && strcmp( $wishlist_id, 'new' ) != 0 ) { 
  169. $insert_args[ 'wishlist_id' ] = $wishlist_id; 
  170.  
  171. $wishlist = $this->get_wishlist_detail( $insert_args[ 'wishlist_id' ] ); 
  172. $this->last_operation_token = $wishlist['wishlist_token']; 
  173. elseif( strcmp( $wishlist_id, 'new' ) == 0 ) { 
  174. $response = function_exists( 'YITH_WCWL_Premium' ) ? YITH_WCWL_Premium()->add_wishlist() : $this->add_wishlist(); 
  175.  
  176. if( $response == "error" ) { 
  177. return "error"; 
  178. else{ 
  179. $insert_args[ 'wishlist_id' ] = $response; 
  180.  
  181. $wishlist = $this->get_wishlist_detail( $insert_args[ 'wishlist_id' ] ); 
  182. $this->last_operation_token = $wishlist['wishlist_token']; 
  183. elseif( empty( $wishlist_id ) ) { 
  184. $wishlist_id = $this->generate_default_wishlist( $user_id ); 
  185. $insert_args[ 'wishlist_id' ] = $wishlist_id; 
  186.  
  187. if( $this->is_product_in_wishlist( $prod_id, $wishlist_id ) ) { 
  188. return "exists"; 
  189.  
  190. $result = $wpdb->insert( $wpdb->yith_wcwl_items, $insert_args ); 
  191.  
  192. if( $result ) { 
  193. if( $this->last_operation_token ) { 
  194. delete_transient( 'yith_wcwl_wishlist_count_' . $this->last_operation_token ); 
  195.  
  196. if( $user_id ) { 
  197. delete_transient( 'yith_wcwl_user_default_count_' . $user_id ); 
  198. delete_transient( 'yith_wcwl_user_total_count_' . $user_id ); 
  199. else { 
  200. $cookie = array( 
  201. 'prod_id' => $prod_id,  
  202. 'quantity' => $quantity,  
  203. 'wishlist_id' => $wishlist_id 
  204. ); 
  205.  
  206. $wishlist = yith_getcookie( 'yith_wcwl_products' ); 
  207. $found = $this->is_product_in_wishlist( $prod_id, $wishlist_id ); 
  208.  
  209. if( ! $found ) { 
  210. $wishlist[] = $cookie; 
  211.  
  212. yith_setcookie( 'yith_wcwl_products', $wishlist ); 
  213.  
  214. $result = true; 
  215.  
  216. if( $result ) { 
  217. do_action( 'yith_wcwl_added_to_wishlist', $prod_id, $wishlist_id, $user_id ); 
  218. return "true"; 
  219. else { 
  220. $this->errors[] = __( 'An error occurred while adding products to wishlist.', 'yith-woocommerce-wishlist' ); 
  221. return "error"; 
  222.  
  223. /** 
  224. * Remove an entry from the wishlist. 
  225. * @param $id int|bool Deprecated 
  226. * @return bool 
  227. * @since 1.0.0 
  228. */ 
  229. public function remove( $id = false ) { 
  230. global $wpdb, $sitepress; 
  231.  
  232. if( ! empty( $id ) ) { 
  233. _deprecated_argument( 'YITH_WCWL->remove()', '2.0.0', __( 'The "Remove" option now does not require any parameter' ) ); 
  234.  
  235. $prod_id = ( isset( $this->details['remove_from_wishlist'] ) && is_numeric( $this->details['remove_from_wishlist'] ) ) ? $this->details['remove_from_wishlist'] : false; 
  236. $wishlist_id = ( isset( $this->details['wishlist_id'] ) && is_numeric( $this->details['wishlist_id'] ) ) ? $this->details['wishlist_id'] : false; 
  237. $user_id = ( ! empty( $this->details['user_id'] ) ) ? $this->details['user_id'] : false; 
  238.  
  239. if( defined('ICL_SITEPRESS_VERSION') ) { 
  240. $prod_id = yit_wpml_object_id( $prod_id, 'product', true, $sitepress->get_default_language() ); 
  241.  
  242. if( $prod_id == false ) { 
  243. return false; 
  244.  
  245. if ( is_user_logged_in() ) { 
  246. $sql = "DELETE FROM {$wpdb->yith_wcwl_items} WHERE user_id = %d AND prod_id = %d"; 
  247. $sql_args = array( 
  248. $user_id,  
  249. $prod_id 
  250. ); 
  251.  
  252. if( empty( $wishlist_id ) ) { 
  253. $wishlist_id = $this->generate_default_wishlist( get_current_user_id() ); 
  254.  
  255. $wishlist = $this->get_wishlist_detail( $wishlist_id ); 
  256. $this->last_operation_token = $wishlist['wishlist_token']; 
  257.  
  258. $sql .= " AND wishlist_id = %d"; 
  259. $sql_args[] = $wishlist_id; 
  260.  
  261. $result = $wpdb->query( $wpdb->prepare( $sql, $sql_args ) ); 
  262.  
  263. if ( $result ) { 
  264. if( $this->last_operation_token ) { 
  265. delete_transient( 'yith_wcwl_wishlist_count_' . $this->last_operation_token ); 
  266.  
  267. if( $user_id ) { 
  268. delete_transient( 'yith_wcwl_user_default_count_' . $user_id ); 
  269. delete_transient( 'yith_wcwl_user_total_count_' . $user_id ); 
  270.  
  271. return true; 
  272. else { 
  273. $this->errors[] = __( 'An error occurred while removing products from the wishlist', 'yith-woocommerce-wishlist' ); 
  274. return false; 
  275. else { 
  276. $wishlist = yith_getcookie( 'yith_wcwl_products' ); 
  277.  
  278. foreach( $wishlist as $key => $item ) { 
  279. if( $item['wishlist_id'] == $wishlist_id && $item['prod_id'] == $prod_id ) { 
  280. unset( $wishlist[ $key ] ); 
  281.  
  282. yith_setcookie( 'yith_wcwl_products', $wishlist ); 
  283.  
  284. return true; 
  285.  
  286. /** 
  287. * Check if the product exists in the wishlist. 
  288. * @param int $product_id Product id to check 
  289. * @param int|bool $wishlist_id Wishlist where to search (use false to search in default wishlist) 
  290. * @return bool 
  291. * @since 1.0.0 
  292. */ 
  293. public function is_product_in_wishlist( $product_id, $wishlist_id = false ) { 
  294. global $wpdb, $sitepress; 
  295.  
  296. $exists = false; 
  297.  
  298. if( defined('ICL_SITEPRESS_VERSION') ) { 
  299. $product_id = yit_wpml_object_id( $product_id, 'product', true, $sitepress->get_default_language() ); 
  300.  
  301. if( is_user_logged_in() ) { 
  302. $sql = "SELECT COUNT(*) as `cnt` FROM `{$wpdb->yith_wcwl_items}` WHERE `prod_id` = %d AND `user_id` = %d"; 
  303. $sql_args = array( 
  304. $product_id,  
  305. $this->details['user_id'] 
  306. ); 
  307.  
  308. if( $wishlist_id != false ) { 
  309. $sql .= " AND `wishlist_id` = %d"; 
  310. $sql_args[] = $wishlist_id; 
  311. elseif( $default_wishlist_id = $this->generate_default_wishlist( get_current_user_id() ) ) { 
  312. $sql .= " AND `wishlist_id` = %d"; 
  313. $sql_args[] = $default_wishlist_id; 
  314. else{ 
  315. $sql .= " AND `wishlist_id` IS NULL"; 
  316.  
  317. $results = $wpdb->get_var( $wpdb->prepare( $sql, $sql_args ) ); 
  318. $exists = (bool) ( $results > 0 ); 
  319. else { 
  320. $wishlist = yith_getcookie( 'yith_wcwl_products' ); 
  321.  
  322. if( $wishlist && is_array( $wishlist ) ) { 
  323. foreach ( $wishlist as $key => $item ) { 
  324. if ( $item['wishlist_id'] == $wishlist_id && $item['prod_id'] == $product_id ) { 
  325. $exists = true; 
  326.  
  327. return apply_filters( 'yith_wcwl_is_product_in_wishlist', $exists, $product_id, $wishlist_id ); 
  328.  
  329. /** 
  330. * Retrieve elements of the wishlist for a specific user 
  331. * @param $args mixed Arguments array; it may contains any of the following:<br/> 
  332. * [<br/> 
  333. * 'user_id' // Owner of the wishlist; default to current user logged in (if any), or false for cookie wishlist<br/> 
  334. * 'product_id' // Product to search in the wishlist<br/> 
  335. * 'wishlist_id' // wishlist_id for a specific wishlist, false for default, or all for any wishlist<br/> 
  336. * 'wishlist_token' // wishlist token, or false as default<br/> 
  337. * 'wishlist_visibility' // all, visible, public, shared, private<br/> 
  338. * 'is_default' => // whether searched wishlist should be default one <br/> 
  339. * 'id' => false, // only for table select<br/> 
  340. * 'limit' => false, // pagination param; number of items per page. 0 to get all items<br/> 
  341. * 'offset' => 0 // pagination param; offset for the current set. 0 to start from the first item<br/> 
  342. * ] 
  343. * @return array 
  344. * @since 2.0.0 
  345. */ 
  346. public function get_products( $args = array() ) { 
  347. global $wpdb; 
  348.  
  349. $default = array( 
  350. 'user_id' => ( is_user_logged_in() ) ? get_current_user_id(): false,  
  351. 'product_id' => false,  
  352. 'wishlist_id' => false, //wishlist_id for a specific wishlist, false for default, or all for any wishlist 
  353. 'wishlist_token' => false,  
  354. 'wishlist_visibility' => 'all', // all, visible, public, shared, private 
  355. 'is_default' => false,  
  356. 'id' => false, // only for table select 
  357. 'limit' => false,  
  358. 'offset' => 0 
  359. ); 
  360.  
  361. $args = wp_parse_args( $args, $default ); 
  362. extract( $args ); 
  363.  
  364. if( ! empty( $user_id ) || ! empty( $wishlist_token ) || ! empty( $wishlist_id ) ) { 
  365. $hidden_products = yith_wcwl_get_hidden_products(); 
  366.  
  367. $sql = "SELECT * 
  368. FROM `{$wpdb->yith_wcwl_items}` AS i 
  369. LEFT JOIN {$wpdb->yith_wcwl_wishlists} AS l ON l.`ID` = i.`wishlist_id` 
  370. INNER JOIN {$wpdb->posts} AS p ON p.ID = i.prod_id  
  371. WHERE 1 AND p.post_type = %s AND p.post_status = %s"; 
  372. $sql .= $hidden_products ? " AND p.ID NOT IN ( " . implode( ', ', $hidden_products ) . " )" : ""; 
  373.  
  374. $sql_args = array( 
  375. 'product',  
  376. 'publish' 
  377. ); 
  378.  
  379. if( ! empty( $user_id ) ) { 
  380. $sql .= " AND i.`user_id` = %d"; 
  381. $sql_args[] = $user_id; 
  382.  
  383. if( ! empty( $product_id ) ) { 
  384. $sql .= " AND i.`prod_id` = %d"; 
  385. $sql_args[] = $product_id; 
  386.  
  387. if( ! empty( $wishlist_id ) && $wishlist_id != 'all' ) { 
  388. $sql .= " AND i.`wishlist_id` = %d"; 
  389. $sql_args[] = $wishlist_id; 
  390. elseif( ( empty( $wishlist_id ) ) && empty( $wishlist_token ) && empty( $is_default ) ) { 
  391. $sql .= " AND i.`wishlist_id` IS NULL"; 
  392.  
  393. if( ! empty( $wishlist_token ) ) { 
  394. $sql .= " AND l.`wishlist_token` = %s"; 
  395. $sql_args[] = $wishlist_token; 
  396.  
  397. if( ! empty( $wishlist_visibility ) && $wishlist_visibility != 'all' ) { 
  398. switch( $wishlist_visibility ) { 
  399. case 'visible': 
  400. $sql .= " AND ( l.`wishlist_privacy` = %d OR l.`wishlist_privacy` = %d )"; 
  401. $sql_args[] = 0; 
  402. $sql_args[] = 1; 
  403. break; 
  404. case 'public': 
  405. $sql .= " AND l.`wishlist_privacy` = %d"; 
  406. $sql_args[] = 0; 
  407. break; 
  408. case 'shared': 
  409. $sql .= " AND l.`wishlist_privacy` = %d"; 
  410. $sql_args[] = 1; 
  411. break; 
  412. case 'private': 
  413. $sql .= " AND l.`wishlist_privacy` = %d"; 
  414. $sql_args[] = 2; 
  415. break; 
  416. default: 
  417. $sql .= " AND l.`wishlist_privacy` = %d"; 
  418. $sql_args[] = 0; 
  419. break; 
  420.  
  421. if( ! empty( $is_default ) ) { 
  422. if( ! empty( $user_id ) ) { 
  423. $this->generate_default_wishlist( $user_id ); 
  424.  
  425. $sql .= " AND l.`is_default` = %d"; 
  426. $sql_args[] = $is_default; 
  427.  
  428. if( ! empty( $id ) ) { 
  429. $sql .= " AND `i.ID` = %d"; 
  430. $sql_args[] = $id; 
  431.  
  432. $sql .= " GROUP BY i.prod_id, l.ID"; 
  433.  
  434. if( ! empty( $limit ) && isset( $offset ) ) { 
  435. $sql .= " LIMIT " . $offset . ", " . $limit; 
  436.  
  437. $wishlist = $wpdb->get_results( $wpdb->prepare( $sql, $sql_args ), ARRAY_A ); 
  438. else{ 
  439. $wishlist = yith_getcookie( 'yith_wcwl_products' ); 
  440. $hidden_products = yith_wcwl_get_hidden_products(); 
  441.  
  442. $query = "SELECT ID FROM {$wpdb->posts} AS p 
  443. WHERE post_type = %s AND post_status = %s"; 
  444. $query .= ! empty( $hidden_products ) ? ( " AND p.ID NOT IN ( " . implode( ', ', $hidden_products ). " )" ) : ""; 
  445.  
  446. $query_args = array( 
  447. 'product',  
  448. 'publish' 
  449. ); 
  450.  
  451. $existing_products = $wpdb->get_col( $wpdb->prepare( $query, $query_args ) ); 
  452.  
  453. foreach( $wishlist as $key => $cookie ) { 
  454. if( ! in_array( $cookie['prod_id'], $existing_products ) ) { 
  455. unset( $wishlist[ $key ] ); 
  456.  
  457. if( ! empty( $product_id ) && $cookie['prod_id'] != $product_id ) { 
  458. unset( $wishlist[ $key ] ); 
  459.  
  460. if( ( ! empty( $wishlist_id ) && $wishlist_id != 'all' ) && $cookie['wishlist_id'] != $wishlist_id ) { 
  461. unset( $wishlist[ $key ] ); 
  462.  
  463. if( ! empty( $limit ) && isset( $offset ) ) { 
  464. $wishlist = array_slice( $wishlist, $offset, $limit ); 
  465.  
  466. return $wishlist; 
  467.  
  468. /** 
  469. * Retrieve the number of products in the wishlist. 
  470. * @param $wishlist_token string|bool Wishlist token if any; false for default wishlist 
  471. * @return int 
  472. * @since 1.0.0 
  473. */ 
  474. public function count_products( $wishlist_token = false ) { 
  475. global $wpdb; 
  476.  
  477. if( is_user_logged_in() || $wishlist_token != false ) { 
  478. if( ! empty( $wishlist_token ) ) { 
  479. $count = get_transient( 'yith_wcwl_wishlist_count_' . $wishlist_token ); 
  480. else{ 
  481. $count = get_transient( 'yith_wcwl_user_default_count_' . get_current_user_id() ); 
  482.  
  483. if( true || false === $count ) { 
  484. $hidden_products = yith_wcwl_get_hidden_products(); 
  485.  
  486. $sql = "SELECT i.`prod_id` AS `cnt` 
  487. FROM `{$wpdb->yith_wcwl_items}` AS i 
  488. LEFT JOIN `{$wpdb->yith_wcwl_wishlists}` AS l ON l.ID = i.wishlist_id 
  489. INNER JOIN `{$wpdb->posts}` AS p ON i.`prod_id` = p.`ID` 
  490. WHERE p.`post_type` = %s AND p.`post_status` = %s"; 
  491. $sql .= $hidden_products ? " AND p.ID NOT IN ( " . implode( ', ', $hidden_products ) . " )" : ""; 
  492. $args = array( 
  493. 'product',  
  494. 'publish' 
  495. ); 
  496.  
  497. if ( ! empty( $wishlist_token ) ) { 
  498. $sql .= " AND l.`wishlist_token` = %s"; 
  499. $args[] = $wishlist_token; 
  500. } else { 
  501. $sql .= " AND l.`is_default` = %d AND l.`user_id` = %d"; 
  502. $args[] = 1; 
  503. $args[] = get_current_user_id(); 
  504.  
  505. $sql .= " GROUP BY i.prod_id, l.ID"; 
  506.  
  507. $query = $wpdb->prepare( $sql, $args ); 
  508. $count = count( $wpdb->get_col( $query ) ); 
  509.  
  510. $transient_name = ! empty( $wishlist_token ) ? ( 'yith_wcwl_wishlist_count_' . $wishlist_token ) : ( 'yith_wcwl_user_default_count_' . get_current_user_id() ); 
  511. set_transient( $transient_name, $count, WEEK_IN_SECONDS ); 
  512.  
  513. return $count; 
  514. else { 
  515. $cookie = yith_getcookie( 'yith_wcwl_products' ); 
  516. $hidden_products = yith_wcwl_get_hidden_products(); 
  517.  
  518. $query = "SELECT ID FROM {$wpdb->posts} AS p 
  519. WHERE post_type = %s AND post_status = %s"; 
  520. $query .= ! empty( $hidden_products ) ? ( " AND p.ID NOT IN ( " . implode( ', ', $hidden_products ). " )" ) : ""; 
  521. $query_args = array( 
  522. 'product',  
  523. 'publish' 
  524. ); 
  525.  
  526. $existing_products = $wpdb->get_col( $wpdb->prepare( $query, $query_args ) ); 
  527. $wishlist_products = array(); 
  528.  
  529. if( ! empty( $cookie ) ) { 
  530. foreach( $cookie as $elem ) { 
  531. $wishlist_products[] = $elem['prod_id']; 
  532.  
  533. $wishlist_products = array_intersect( $wishlist_products, $existing_products ); 
  534.  
  535. return count( $wishlist_products ); 
  536.  
  537. /** 
  538. * Count all user items in wishlists 
  539. * @return int Count of items added all over wishlist from current user 
  540. * @since 2.0.12 
  541. */ 
  542. public function count_all_products() { 
  543. global $wpdb; 
  544.  
  545. if( is_user_logged_in() ) { 
  546. $user_id = get_current_user_id(); 
  547.  
  548. if( false === $count = get_transient( 'yith_wcwl_user_total_count_' . $user_id ) ) { 
  549. $hidden_products = yith_wcwl_get_hidden_products(); 
  550.  
  551. $sql = "SELECT COUNT( i.`prod_id` ) AS `cnt` 
  552. FROM `{$wpdb->yith_wcwl_items}` AS i 
  553. WHERE i.`user_id` = %d AND i.`prod_id` IN ( 
  554. SELECT ID 
  555. FROM {$wpdb->posts} AS p 
  556. WHERE p.`post_type` = %s AND p.`post_status` = %s"; 
  557.  
  558. $sql .= ! empty( $hidden_products ) ? ( " AND p.ID NOT IN ( " . implode( ', ', $hidden_products ). " )" ) : ""; 
  559.  
  560. $sql .= ")"; 
  561.  
  562. $query = $wpdb->prepare( $sql, array( $user_id, 'product', 'publish' ) ); 
  563. $count = $wpdb->get_var( $query ); 
  564.  
  565. set_transient( 'yith_wcwl_user_total_count_' . $user_id, $count, WEEK_IN_SECONDS ); 
  566.  
  567. return $count; 
  568. else { 
  569. $cookie = yith_getcookie( 'yith_wcwl_products' ); 
  570. $hidden_products = yith_wcwl_get_hidden_products(); 
  571.  
  572. $query = "SELECT ID FROM {$wpdb->posts} AS p 
  573. WHERE post_type = %s AND post_status = %s"; 
  574. $query .= ! empty( $hidden_products ) ? ( " AND p.ID NOT IN ( " . implode( ', ', $hidden_products ). " )" ) : ""; 
  575.  
  576. $query_args = array( 
  577. 'product',  
  578. 'publish' 
  579. ); 
  580.  
  581. $existing_products = $wpdb->get_col( $wpdb->prepare( $query, $query_args ) ); 
  582. $wishlist_products = array(); 
  583.  
  584. if( ! empty( $cookie ) ) { 
  585. foreach( $cookie as $elem ) { 
  586. $wishlist_products[] = $elem['prod_id']; 
  587.  
  588. $wishlist_products = array_intersect( $wishlist_products, $existing_products ); 
  589.  
  590. return count( $wishlist_products ); 
  591.  
  592. /** 
  593. * Count number of times a product was added to users wishlists 
  594. * @param $product_id int|bool Product id; false will force method to use global product 
  595. * @return int Number of times the product was added to wishlist 
  596. * @since 2.0.13 
  597. */ 
  598. public function count_add_to_wishlist( $product_id = false ) { 
  599. global $product, $wpdb; 
  600.  
  601. $product_id = ! ( $product_id ) ? yit_get_product_id( $product ) : $product_id; 
  602.  
  603. if( ! $product_id ) { 
  604. return 0; 
  605.  
  606. $query = "SELECT COUNT( DISTINCT( user_id ) ) FROM {$wpdb->yith_wcwl_items} WHERE prod_id = %d"; 
  607. $res = $wpdb->get_var( $wpdb->prepare( $query, $product_id ) ); 
  608.  
  609. return $res; 
  610.  
  611. /** 
  612. * Count product occurrences in users wishlists 
  613. * @param $product_id int 
  614. * @return int 
  615. * @since 2.0.0 
  616. */ 
  617. public function count_product_occurrences( $product_id ) { 
  618. global $wpdb; 
  619. $sql = "SELECT COUNT(*) FROM {$wpdb->yith_wcwl_items} WHERE `prod_id` = %d"; 
  620.  
  621. return $wpdb->get_var( $wpdb->prepare( $sql, $product_id ) ); 
  622.  
  623. /** 
  624. * Retrieve details of a product in the wishlist. 
  625. * @param int $product_id 
  626. * @param int|bool $wishlist_id 
  627. * @return array 
  628. * @since 1.0.0 
  629. */ 
  630. public function get_product_details( $product_id, $wishlist_id = false ) { 
  631. return $this->get_products( 
  632. array( 
  633. 'prod_id' => $product_id,  
  634. 'wishlist_id' => $wishlist_id 
  635. ); 
  636.  
  637. /** === WISHLISTS METHODS === */ 
  638.  
  639. /** 
  640. * Add a new wishlist for the user. 
  641. * @return string "error", "exists" or id of the inserted wishlist 
  642. * @since 2.0.0 
  643. */ 
  644. public function add_wishlist() { 
  645. $user_id = ( ! empty( $this->details['user_id'] ) ) ? $this->details['user_id'] : false; 
  646.  
  647. if( $user_id == false ) { 
  648. $this->errors[] = __( 'You need to log in before creating a new wishlist', 'yith-woocommerce-wishlist' ); 
  649. return "error"; 
  650.  
  651. return $this->generate_default_wishlist( $user_id ); 
  652.  
  653. /** 
  654. * Update wishlist with arguments passed as second parameter 
  655. * @param $wishlist_id int 
  656. * @param $args array Array of parameters to user in $wpdb->update 
  657. * @return bool 
  658. * @since 2.0.0 
  659. */ 
  660. public function update_wishlist( $wishlist_id, $args = array() ) { 
  661. return false; 
  662.  
  663. /** 
  664. * Delete indicated wishlist 
  665. * @param $wishlist_id int 
  666. * @return bool 
  667. * @since 2.0.0 
  668. */ 
  669. public function remove_wishlist( $wishlist_id ) { 
  670. return false; 
  671.  
  672. /** 
  673. * Retrieve all the wishlist matching specified arguments 
  674. * @param $args mixed Array of valid arguments<br/> 
  675. * [<br/> 
  676. * 'id' // Wishlist id to search, if any<br/> 
  677. * 'user_id' // User owner<br/> 
  678. * 'wishlist_slug' // Slug of the wishlist to search<br/> 
  679. * 'wishlist_name' // Name of the wishlist to search<br/> 
  680. * 'wishlist_token' // Token of the wishlist to search<br/> 
  681. * 'wishlist_visibility' // Wishlist visibility: all, visible, public, shared, private<br/> 
  682. * 'user_search' // String to match against first name / last name or email of the wishlist owner<br/> 
  683. * 'is_default' // Whether wishlist should be default or not<br/> 
  684. * 'orderby' // Column used to sort final result (could be any wishlist lists column)<br/> 
  685. * 'order' // Sorting order<br/> 
  686. * 'limit' // Pagination param: maximum number of elements in the set. 0 to retrieve all elements<br/> 
  687. * 'offset' // Pagination param: offset for the current set. 0 to start from the first item<br/> 
  688. * 'show_empty' // Whether to show empty lists os not<br/> 
  689. * ] 
  690. * @return array 
  691. * @since 2.0.0 
  692. */ 
  693. public function get_wishlists( $args = array() ) { 
  694. global $wpdb; 
  695.  
  696. $default = array( 
  697. 'id' => false,  
  698. 'user_id' => ( is_user_logged_in() ) ? get_current_user_id(): false,  
  699. 'wishlist_slug' => false,  
  700. 'wishlist_name' => false,  
  701. 'wishlist_token' => false,  
  702. 'wishlist_visibility' => 'all', // all, visible, public, shared, private 
  703. 'user_search' => false,  
  704. 'is_default' => false,  
  705. 'orderby' => 'ID',  
  706. 'order' => 'DESC',  
  707. 'limit' => false,  
  708. 'offset' => 0,  
  709. 'show_empty' => true 
  710. ); 
  711.  
  712. $args = wp_parse_args( $args, $default ); 
  713. extract( $args ); 
  714.  
  715. $sql = "SELECT l.*"; 
  716.  
  717. if( ! empty( $user_search ) ) { 
  718. $sql .= ", u.user_email, umn.meta_value AS first_name, ums.meta_value AS last_name"; 
  719.  
  720. $sql .= " FROM `{$wpdb->yith_wcwl_wishlists}` AS l"; 
  721.  
  722. if( ! empty( $user_search ) || ( ! empty($orderby ) && $orderby == 'user_login' ) ) { 
  723. $sql .= " LEFT JOIN `{$wpdb->users}` AS u ON l.`user_id` = u.ID"; 
  724.  
  725. if( ! empty( $user_search ) ) { 
  726. $sql .= " LEFT JOIN `{$wpdb->usermeta}` AS umn ON umn.`user_id` = u.`ID`"; 
  727. $sql .= " LEFT JOIN `{$wpdb->usermeta}` AS ums ON ums.`user_id` = u.`ID`"; 
  728.  
  729. $sql .= " WHERE 1"; 
  730.  
  731. if( ! empty( $user_id ) ) { 
  732. $sql .= " AND l.`user_id` = %d"; 
  733.  
  734. $sql_args = array( 
  735. $user_id 
  736. ); 
  737.  
  738. if( ! empty( $user_search ) ) { 
  739. $sql .= " AND ( umn.`meta_key` LIKE %s AND ums.`meta_key` LIKE %s AND ( u.`user_email` LIKE %s OR umn.`meta_value` LIKE %s OR ums.`meta_value` LIKE %s ) )"; 
  740. $sql_args[] = 'first_name'; 
  741. $sql_args[] = 'last_name'; 
  742. $sql_args[] = "%" . $user_search . "%"; 
  743. $sql_args[] = "%" . $user_search . "%"; 
  744. $sql_args[] = "%" . $user_search . "%"; 
  745.  
  746. if( ! empty( $is_default ) ) { 
  747. $sql .= " AND l.`is_default` = %d"; 
  748. $sql_args[] = $is_default; 
  749.  
  750. if( ! empty( $id ) ) { 
  751. $sql .= " AND l.`ID` = %d"; 
  752. $sql_args[] = $id; 
  753.  
  754. if( isset( $wishlist_slug ) && $wishlist_slug !== false ) { 
  755. $sql .= " AND l.`wishlist_slug` = %s"; 
  756. $sql_args[] = sanitize_title_with_dashes( $wishlist_slug ); 
  757.  
  758. if( ! empty( $wishlist_token ) ) { 
  759. $sql .= " AND l.`wishlist_token` = %s"; 
  760. $sql_args[] = $wishlist_token; 
  761.  
  762. if( ! empty( $wishlist_name ) ) { 
  763. $sql .= " AND l.`wishlist_name` LIKE %s"; 
  764. $sql_args[] = "%" . $wishlist_name . "%"; 
  765.  
  766. if( ! empty( $wishlist_visibility ) && $wishlist_visibility != 'all' ) { 
  767. switch( $wishlist_visibility ) { 
  768. case 'visible': 
  769. $sql .= " AND ( l.`wishlist_privacy` = %d OR l.`is_public` = %d )"; 
  770. $sql_args[] = 0; 
  771. $sql_args[] = 1; 
  772. break; 
  773. case 'public': 
  774. $sql .= " AND l.`wishlist_privacy` = %d"; 
  775. $sql_args[] = 0; 
  776. break; 
  777. case 'shared': 
  778. $sql .= " AND l.`wishlist_privacy` = %d"; 
  779. $sql_args[] = 1; 
  780. break; 
  781. case 'private': 
  782. $sql .= " AND l.`wishlist_privacy` = %d"; 
  783. $sql_args[] = 2; 
  784. break; 
  785. default: 
  786. $sql .= " AND l.`wishlist_privacy` = %d"; 
  787. $sql_args[] = 0; 
  788. break; 
  789.  
  790. if( empty( $show_empty ) ) { 
  791. $sql .= " AND l.`ID` IN ( SELECT wishlist_id FROM {$wpdb->yith_wcwl_items} )"; 
  792.  
  793. if( ! empty( $orderby ) && isset( $order ) ) { 
  794. $sql .= " ORDER BY " . $orderby . " " . $order; 
  795.  
  796. if( ! empty( $limit ) && isset( $offset ) ) { 
  797. $sql .= " LIMIT " . $offset . ", " . $limit; 
  798.  
  799. if( ! empty( $sql_args ) ) { 
  800. $sql = $wpdb->prepare( $sql, $sql_args ); 
  801.  
  802. $lists = $wpdb->get_results( $sql, ARRAY_A ); 
  803.  
  804. return $lists; 
  805.  
  806. /** 
  807. * Returns details of a wishlist, searching it by wishlist id 
  808. * @param $wishlist_id int 
  809. * @return array 
  810. * @since 2.0.0 
  811. */ 
  812. public function get_wishlist_detail( $wishlist_id ) { 
  813. global $wpdb; 
  814.  
  815. $sql = "SELECT * FROM {$wpdb->yith_wcwl_wishlists} WHERE `ID` = %d"; 
  816. return $wpdb->get_row( $wpdb->prepare( $sql, $wishlist_id ), ARRAY_A ); 
  817.  
  818. /** 
  819. * Returns details of a wishlist, searching it by wishlist token 
  820. * @param $wishlist_token string 
  821. * @return array 
  822. * @since 2.0.0 
  823. */ 
  824. public function get_wishlist_detail_by_token( $wishlist_token ) { 
  825. global $wpdb; 
  826.  
  827. $sql = "SELECT * FROM {$wpdb->yith_wcwl_wishlists} WHERE `wishlist_token` = %s"; 
  828. return $wpdb->get_row( $wpdb->prepare( $sql, $wishlist_token ), ARRAY_A ); 
  829.  
  830. /** 
  831. * Generate default wishlist for a specific user, adding all NULL items of the user to it 
  832. * @param $user_id int 
  833. * @return int Default wishlist id 
  834. * @since 2.0.0 
  835. */ 
  836. public function generate_default_wishlist( $user_id ) { 
  837. global $wpdb; 
  838.  
  839. $wishlists = $this->get_wishlists( array( 
  840. 'user_id' => $user_id,  
  841. 'is_default' => 1 
  842. ) ); 
  843.  
  844. if( ! empty( $wishlists ) ) { 
  845. $default_user_wishlist = $wishlists[0]['ID']; 
  846. $this->last_operation_token = $wishlists[0]['wishlist_token']; 
  847. else{ 
  848. $token = $this->generate_wishlist_token(); 
  849. $this->last_operation_token = $token; 
  850.  
  851. $wpdb->insert( $wpdb->yith_wcwl_wishlists, array( 
  852. 'user_id' => apply_filters( 'yith_wcwl_default_wishlist_user_id', $user_id ),  
  853. 'wishlist_slug' => apply_filters( 'yith_wcwl_default_wishlist_slug', '' ),  
  854. 'wishlist_token' => $token,  
  855. 'wishlist_name' => apply_filters( 'yith_wcwl_default_wishlist_name', '' ),  
  856. 'wishlist_privacy' => apply_filters( 'yith_wcwl_default_wishlist_privacy', 0 ),  
  857. 'is_default' => 1 
  858. ) ); 
  859.  
  860. $default_user_wishlist = $wpdb->insert_id; 
  861.  
  862. $sql = "UPDATE {$wpdb->yith_wcwl_items} SET wishlist_id = %d WHERE user_id = %d AND wishlist_id IS NULL"; 
  863. $sql_args = array( 
  864. $default_user_wishlist,  
  865. $user_id 
  866. ); 
  867.  
  868. $wpdb->query( $wpdb->prepare( $sql, $sql_args ) ); 
  869.  
  870. return $default_user_wishlist; 
  871.  
  872. /** 
  873. * Generate a token to visit wishlist 
  874. * @return string token 
  875. * @since 2.0.0 
  876. */ 
  877. public function generate_wishlist_token() { 
  878. global $wpdb; 
  879. $count = 0; 
  880. $sql = "SELECT COUNT(*) FROM `{$wpdb->yith_wcwl_wishlists}` WHERE `wishlist_token` = %s"; 
  881.  
  882. do { 
  883. $dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
  884. $nchars = 12; 
  885. $token = ""; 
  886.  
  887. for( $i = 0; $i <= $nchars - 1; $i++ ) { 
  888. $token .= $dictionary[ mt_rand( 0, strlen( $dictionary ) - 1 ) ]; 
  889.  
  890. $count = $wpdb->get_var( $wpdb->prepare( $sql, $token ) ); 
  891. while( $count != 0 ); 
  892.  
  893. return $token; 
  894.  
  895. /** 
  896. * Returns an array of users that created and populated a public wishlist 
  897. * @param $args mixed Array of valid arguments<br/> 
  898. * [<br/> 
  899. * 'search' // String to match against first name / last name / user login or user email of wishlist owner<br/> 
  900. * 'limit' // Pagination param: number of items to show in one page. 0 to show all items<br/> 
  901. * 'offset' // Pagination param: offset for the current set. 0 to start from the first item<br/> 
  902. * ] 
  903. * @return array 
  904. * @since 2.0.0 
  905. */ 
  906. public function get_users_with_wishlist( $args = array() ) { 
  907. global $wpdb; 
  908.  
  909. $default = array( 
  910. 'search' => false,  
  911. 'limit' => false,  
  912. 'offset' => 0 
  913. ); 
  914.  
  915. $args = wp_parse_args( $args, $default ); 
  916. extract( $args ); 
  917.  
  918. $sql = "SELECT DISTINCT i.user_id 
  919. FROM {$wpdb->yith_wcwl_items} AS i 
  920. LEFT JOIN {$wpdb->yith_wcwl_wishlists} AS l ON i.wishlist_id = l.ID"; 
  921.  
  922. if( ! empty( $search ) ) { 
  923. $sql .= " LEFT JOIN `{$wpdb->users}` AS u ON l.`user_id` = u.ID"; 
  924. $sql .= " LEFT JOIN `{$wpdb->usermeta}` AS umn ON umn.`user_id` = u.`ID`"; 
  925. $sql .= " LEFT JOIN `{$wpdb->usermeta}` AS ums ON ums.`user_id` = u.`ID`"; 
  926.  
  927. $sql .= " WHERE l.wishlist_privacy = %d"; 
  928. $sql_args = array( 0 ); 
  929.  
  930. if( ! empty( $search ) ) { 
  931. $sql .= " AND ( umn.`meta_key` LIKE %s AND ums.`meta_key` LIKE %s AND ( u.`user_email` LIKE %s OR u.`user_login` LIKE %s OR umn.`meta_value` LIKE %s OR ums.`meta_value` LIKE %s ) )"; 
  932. $sql_args[] = 'first_name'; 
  933. $sql_args[] = 'last_name'; 
  934. $sql_args[] = "%" . $search . "%"; 
  935. $sql_args[] = "%" . $search . "%"; 
  936. $sql_args[] = "%" . $search . "%"; 
  937. $sql_args[] = "%" . $search . "%"; 
  938.  
  939. if( ! empty( $limit ) && isset( $offset ) ) { 
  940. $sql .= " LIMIT " . $offset . ", " . $limit; 
  941.  
  942. $res = $wpdb->get_col( $wpdb->prepare( $sql, $sql_args ) ); 
  943. return $res; 
  944.  
  945. /** 
  946. * Count users that have public wishlists 
  947. * @param $search string 
  948. * @return int 
  949. * @since 2.0.0 
  950. */ 
  951. public function count_users_with_wishlists( $search ) { 
  952. return count( $this->get_users_with_wishlist( array( 'search' => $search ) ) ); 
  953.  
  954. /** === GENERAL METHODS === */ 
  955.  
  956. /** 
  957. * Add rewrite rules for wishlist 
  958. * @return void 
  959. * @since 2.0.0 
  960. */ 
  961. public function add_rewrite_rules() { 
  962. global $wp_query; 
  963. $wishlist_page_id = isset( $_POST['yith_wcwl_wishlist_page_id'] ) ? $_POST['yith_wcwl_wishlist_page_id'] : get_option( 'yith_wcwl_wishlist_page_id' ); 
  964. $wishlist_page_id = yith_wcwl_object_id( $wishlist_page_id ); 
  965.  
  966. if( empty( $wishlist_page_id ) ) { 
  967. return; 
  968.  
  969. $wishlist_page = get_post( $wishlist_page_id ); 
  970. $wishlist_page_slug = $wishlist_page ? $wishlist_page->post_name : false; 
  971.  
  972. if ( empty( $wishlist_page_slug ) ) { 
  973. return; 
  974.  
  975. $regex_paged = '(([^/]+/)*' . $wishlist_page_slug . ')(/(.*))?/page/([0-9]{1, })/?$'; 
  976. $regex_simple = '(([^/]+/)*' . $wishlist_page_slug . ')(/(.*))?/?$'; 
  977.  
  978. add_rewrite_rule( $regex_paged, 'index.php?pagename=$matches[1]&wishlist-action=$matches[4]&paged=$matches[5]', 'top' ); 
  979. add_rewrite_rule( $regex_simple, 'index.php?pagename=$matches[1]&wishlist-action=$matches[4]', 'top' ); 
  980.  
  981. $rewrite_rules = get_option( 'rewrite_rules' ); 
  982.  
  983. if( ! array_key_exists( $regex_paged, $rewrite_rules ) || ! array_key_exists( $regex_paged, $rewrite_rules ) ) { 
  984. flush_rewrite_rules(); 
  985.  
  986. /** 
  987. * Adds public query var for wishlist 
  988. * @param $public_var array 
  989. * @return array 
  990. * @since 2.0.0 
  991. */ 
  992. public function add_public_query_var( $public_var ) { 
  993. $public_var[] = 'wishlist-action'; 
  994. $public_var[] = 'wishlist_id'; 
  995.  
  996. return $public_var; 
  997.  
  998. /** 
  999. * Get all errors in HTML mode or simple string. 
  1000. *  
  1001. * @param bool $html 
  1002. * @return string 
  1003. * @since 1.0.0 
  1004. */ 
  1005. public function get_errors( $html = true ) { 
  1006. return implode( ( $html ? '<br />' : ', ' ), $this->errors ); 
  1007.  
  1008. /** 
  1009. * Build wishlist page URL. 
  1010. * @param $action string 
  1011. *  
  1012. * @return string 
  1013. * @since 1.0.0 
  1014. */ 
  1015. public function get_wishlist_url( $action = '' ) { 
  1016. global $sitepress; 
  1017. $wishlist_page_id = yith_wcwl_object_id( get_option( 'yith_wcwl_wishlist_page_id' ) ); 
  1018. $wishlist_permalink = get_the_permalink( $wishlist_page_id ); 
  1019.  
  1020. $action_params = explode( '/', $action ); 
  1021. $view = $action_params[0]; 
  1022. $data = isset( $action_params[1] ) ? $action_params[1] : ''; 
  1023.  
  1024. if( $action == 'view' && empty( $data ) ) { 
  1025. return $wishlist_permalink; 
  1026.  
  1027. if( get_option( 'permalink_structure' ) && ! defined( 'ICL_PLUGIN_PATH' ) ) { 
  1028. $wishlist_permalink = trailingslashit( $wishlist_permalink ); 
  1029. $base_url = trailingslashit( $wishlist_permalink . $action ); 
  1030. else{ 
  1031. $base_url = $wishlist_permalink; 
  1032. $params = array(); 
  1033.  
  1034. if( ! empty( $data ) ) { 
  1035. $params['wishlist-action'] = $view; 
  1036.  
  1037. if( $view == 'view' ) { 
  1038. $params['wishlist_id'] = $data; 
  1039. elseif( $view == 'user' ) { 
  1040. $params['user_id'] = $data; 
  1041. else{ 
  1042. $params['wishlist-action'] = $view; 
  1043.  
  1044. $base_url = add_query_arg( $params, $base_url ); 
  1045.  
  1046. if( defined( 'ICL_PLUGIN_PATH' ) && $sitepress->get_current_language() != $sitepress->get_default_language() ) { 
  1047. $base_url = add_query_arg( 'lang', $sitepress->get_current_language(), $base_url ); 
  1048.  
  1049. return apply_filters( 'yith_wcwl_wishlist_page_url', esc_url_raw( $base_url ), $action ); 
  1050.  
  1051. /** 
  1052. * Build the URL used to remove an item from the wishlist. 
  1053. * @param int $item_id 
  1054. * @return string 
  1055. * @since 1.0.0 
  1056. */ 
  1057. public function get_remove_url( $item_id ) { 
  1058. return esc_url( add_query_arg( 'remove_from_wishlist', $item_id ) ); 
  1059.  
  1060. /** 
  1061. * Build the URL used to add an item in the wishlist. 
  1062. * @return string 
  1063. * @since 1.0.0 
  1064. */ 
  1065. public function get_addtowishlist_url() { 
  1066. _deprecated_function( 'YITH_WCWL::get_addtowishlist_url', '2.0.16' ); 
  1067.  
  1068. global $product; 
  1069.  
  1070. return esc_url( add_query_arg( 'add_to_wishlist', $product->id ) ); 
  1071.  
  1072. /** 
  1073. * Build the URL used to add an item to the cart from the wishlist. 
  1074. *  
  1075. * @param int $id 
  1076. * @param int|string $user_id 
  1077. * @return string 
  1078. * @since 1.0.0 
  1079. */ 
  1080. public function get_addtocart_url( $id, $user_id = '' ) { 
  1081. _deprecated_function( 'YITH_WCWL::get_addtocart_url', '2.0.0' ); 
  1082.  
  1083. $product = wc_get_product( $id ); 
  1084.  
  1085. if ( $product->is_type( 'variable' ) ) { 
  1086. return get_permalink( $id ); 
  1087.  
  1088. $url = YITH_WCWL_URL . 'add-to-cart.php?wishlist_item_id=' . rtrim( $id, '_' ); 
  1089.  
  1090. if( $user_id != '' ) { 
  1091. $url .= '&id=' . $user_id; 
  1092.  
  1093. return $url; 
  1094.  
  1095. /** 
  1096. * Build the URL used for an external/affiliate product. 
  1097. * @deprecated 
  1098. * @param $id 
  1099. * @return string 
  1100. */ 
  1101. public function get_affiliate_product_url( $id ) { 
  1102. _deprecated_function( 'YITH_WCWL::get_affiliate_product_url', '2.0.0' ); 
  1103.  
  1104. return get_post_meta( $id, '_product_url', true ); 
  1105.  
  1106. /** 
  1107. * Build an URL with the nonce added. 
  1108. *  
  1109. * @param string $action 
  1110. * @param string $url 
  1111. * @return string 
  1112. * @since 1.0.0 
  1113. */ 
  1114. public function get_nonce_url( $action, $url = '' ) { 
  1115. _deprecated_function( 'YITH_WCWL::get_nonce_url', '2.0.16' ); 
  1116.  
  1117. return esc_url( add_query_arg( '_n', wp_create_nonce( 'yith-wcwl-' . $action ), $url ) ); 
  1118.  
  1119. /** 
  1120. * Prints wc notice for wishlist pages 
  1121. * @return void 
  1122. * @since 2.0.5 
  1123. */ 
  1124. public function print_notices() { 
  1125. if( function_exists( 'wc_print_notices' ) ) { 
  1126. wc_print_notices(); 
  1127.  
  1128. /** === FONTAWESOME FIX === */ 
  1129.  
  1130. /** 
  1131. * Modernize font-awesome class, for old wishlist users 
  1132. * @param $class string Original font-awesome class 
  1133. * @return string Filtered font-awesome class 
  1134. * @since 2.0.2 
  1135. */ 
  1136. public function update_font_awesome_classes( $class ) { 
  1137. $exceptions = array( 
  1138. 'icon-envelope' => 'fa-envelope-o',  
  1139. 'icon-star-empty' => 'fa-star-o',  
  1140. 'icon-ok' => 'fa-check',  
  1141. 'icon-zoom-in' => 'fa-search-plus',  
  1142. 'icon-zoom-out' => 'fa-search-minus',  
  1143. 'icon-off' => 'fa-power-off',  
  1144. 'icon-trash' => 'fa-trash-o',  
  1145. 'icon-share' => 'fa-share-square-o',  
  1146. 'icon-check' => 'fa-check-square-o',  
  1147. 'icon-move' => 'fa-arrows',  
  1148. 'icon-file' => 'fa-file-o',  
  1149. 'icon-time' => 'fa-clock-o',  
  1150. 'icon-download-alt' => 'fa-download',  
  1151. 'icon-download' => 'fa-arrow-circle-o-down',  
  1152. 'icon-upload' => 'fa-arrow-circle-o-up',  
  1153. 'icon-play-circle' => 'fa-play-circle-o',  
  1154. 'icon-indent-left' => 'fa-dedent',  
  1155. 'icon-indent-right' => 'fa-indent',  
  1156. 'icon-facetime-video' => 'fa-video-camera',  
  1157. 'icon-picture' => 'fa-picture-o',  
  1158. 'icon-plus-sign' => 'fa-plus-circle',  
  1159. 'icon-minus-sign' => 'fa-minus-circle',  
  1160. 'icon-remove-sign' => 'fa-times-circle',  
  1161. 'icon-ok-sign' => 'fa-check-circle',  
  1162. 'icon-question-sign' => 'fa-question-circle',  
  1163. 'icon-info-sign' => 'fa-info-circle',  
  1164. 'icon-screenshot' => 'fa-crosshairs',  
  1165. 'icon-remove-circle' => 'fa-times-circle-o',  
  1166. 'icon-ok-circle' => 'fa-check-circle-o',  
  1167. 'icon-ban-circle' => 'fa-ban',  
  1168. 'icon-share-alt' => 'fa-share',  
  1169. 'icon-resize-full' => 'fa-expand',  
  1170. 'icon-resize-small' => 'fa-compress',  
  1171. 'icon-exclamation-sign' => 'fa-exclamation-circle',  
  1172. 'icon-eye-open' => 'fa-eye',  
  1173. 'icon-eye-close' => 'fa-eye-slash',  
  1174. 'icon-warning-sign' => 'fa-warning',  
  1175. 'icon-folder-close' => 'fa-folder',  
  1176. 'icon-resize-vertical' => 'fa-arrows-v',  
  1177. 'icon-resize-horizontal' => 'fa-arrows-h',  
  1178. 'icon-twitter-sign' => 'fa-twitter-square',  
  1179. 'icon-facebook-sign' => 'fa-facebook-square',  
  1180. 'icon-thumbs-up' => 'fa-thumbs-o-up',  
  1181. 'icon-thumbs-down' => 'fa-thumbs-o-down',  
  1182. 'icon-heart-empty' => 'fa-heart-o',  
  1183. 'icon-signout' => 'fa-sign-out',  
  1184. 'icon-linkedin-sign' => 'fa-linkedin-square',  
  1185. 'icon-pushpin' => 'fa-thumb-tack',  
  1186. 'icon-signin' => 'fa-sign-in',  
  1187. 'icon-github-sign' => 'fa-github-square',  
  1188. 'icon-upload-alt' => 'fa-upload',  
  1189. 'icon-lemon' => 'fa-lemon-o',  
  1190. 'icon-check-empty' => 'fa-square-o',  
  1191. 'icon-bookmark-empty' => 'fa-bookmark-o',  
  1192. 'icon-phone-sign' => 'fa-phone-square',  
  1193. 'icon-hdd' => 'fa-hdd-o',  
  1194. 'icon-hand-right' => 'fa-hand-o-right',  
  1195. 'icon-hand-left' => 'fa-hand-o-left',  
  1196. 'icon-hand-up' => 'fa-hand-o-up',  
  1197. 'icon-hand-down' => 'fa-hand-o-down',  
  1198. 'icon-circle-arrow-left' => 'fa-arrow-circle-left',  
  1199. 'icon-circle-arrow-right' => 'fa-arrow-circle-right',  
  1200. 'icon-circle-arrow-up' => 'fa-arrow-circle-up',  
  1201. 'icon-circle-arrow-down' => 'fa-arrow-circle-down',  
  1202. 'icon-fullscreen' => 'fa-arrows-alt',  
  1203. 'icon-beaker' => 'fa-flask',  
  1204. 'icon-paper-clip' => 'fa-paperclip',  
  1205. 'icon-sign-blank' => 'fa-square',  
  1206. 'icon-pinterest-sign' => 'fa-pinterest-square',  
  1207. 'icon-google-plus-sign' => 'fa-google-plus-square',  
  1208. 'icon-envelope-alt' => 'fa-envelope',  
  1209. 'icon-comment-alt' => 'fa-comment-o',  
  1210. 'icon-comments-alt' => 'fa-comments-o' 
  1211. ); 
  1212.  
  1213. if( in_array( $class, array_keys( $exceptions ) ) ) { 
  1214. $class = $exceptions[ $class ]; 
  1215.  
  1216. $class = str_replace( 'icon-', 'fa-', $class ); 
  1217.  
  1218. return $class; 
  1219.  
  1220. /** === REQUEST HANDLING METHODS === */ 
  1221.  
  1222. /** 
  1223. * Adds an element to wishlist when default AJAX method cannot be used 
  1224. * @return void 
  1225. * @since 2.0.0 
  1226. */ 
  1227. public function add_to_wishlist() { 
  1228. // add item to wishlist when javascript is not enabled 
  1229. if( isset( $_GET['add_to_wishlist'] ) ) { 
  1230. $this->add(); 
  1231.  
  1232. /** 
  1233. * Removes an element from wishlist when default AJAX method cannot be used 
  1234. * @return void 
  1235. * @since 2.0.0 
  1236. */ 
  1237. public function remove_from_wishlist() { 
  1238. // remove item from wishlist when javascript is not enabled 
  1239. if( isset( $_GET['remove_from_wishlist'] ) ) { 
  1240. $this->remove(); 
  1241.  
  1242. /** 
  1243. * Removes an element after add to cart, if option is enabled in panel 
  1244. * @return void 
  1245. * @since 2.0.0 
  1246. */ 
  1247. public function remove_from_wishlist_after_add_to_cart() { 
  1248. if( get_option( 'yith_wcwl_remove_after_add_to_cart' ) == 'yes' ) { 
  1249. if( isset( $_REQUEST['remove_from_wishlist_after_add_to_cart'] ) ) { 
  1250.  
  1251. $this->details['remove_from_wishlist'] = $_REQUEST['remove_from_wishlist_after_add_to_cart']; 
  1252.  
  1253. if ( isset( $_REQUEST['wishlist_id'] ) ) { 
  1254. $this->details['wishlist_id'] = $_REQUEST['wishlist_id']; 
  1255. elseif( yith_wcwl_is_wishlist() ) { 
  1256. $this->details['remove_from_wishlist'] = $_REQUEST['add-to-cart']; 
  1257.  
  1258. if ( isset( $_REQUEST['wishlist_id'] ) ) { 
  1259. $this->details['wishlist_id'] = $_REQUEST['wishlist_id']; 
  1260.  
  1261. $this->remove(); 
  1262.  
  1263. /** 
  1264. * Redirect to cart after "Add to cart" button pressed on wishlist table 
  1265. * @param $url string Original redirect url 
  1266. * @param $product \WC_Product Product added to cart 
  1267. * @return string Redirect url 
  1268. * @since 2.0.0 
  1269. */ 
  1270. public function redirect_to_cart( $url, $product ) { 
  1271. global $yith_wcwl_wishlist_token; 
  1272.  
  1273. $wishlist = $this->get_wishlist_detail_by_token( $yith_wcwl_wishlist_token ); 
  1274. $wishlist_id = $wishlist['ID']; 
  1275.  
  1276. if( $product->is_type( 'simple' ) && get_option( 'yith_wcwl_redirect_cart' ) == 'yes' ) { 
  1277. if( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && yith_wcwl_is_wishlist() ) { 
  1278. $url = add_query_arg( 'add-to-cart', yit_get_product_id( $product ), wc_get_cart_url() ); 
  1279.  
  1280. if( ! $product->is_type( 'external' ) && get_option( 'yith_wcwl_remove_after_add_to_cart' ) == 'yes' ) { 
  1281. if( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && yith_wcwl_is_wishlist() ) { 
  1282. $url = add_query_arg( 
  1283. array( 
  1284. 'remove_from_wishlist_after_add_to_cart' => yit_get_product_id( $product ),  
  1285. 'wishlist_id' => $wishlist_id,  
  1286. 'wishlist_token' => $yith_wcwl_wishlist_token 
  1287. ),  
  1288. $url 
  1289. ); 
  1290.  
  1291. return apply_filters( 'yit_wcwl_add_to_cart_redirect_url', esc_url( $url ) ); 
  1292.  
  1293. /** 
  1294. * AJAX: add to wishlist action 
  1295. *  
  1296. * @return void 
  1297. * @since 1.0.0 
  1298. */ 
  1299. public function add_to_wishlist_ajax() { 
  1300. $return = $this->add(); 
  1301. $message = ''; 
  1302. $user_id = isset( $this->details['user_id'] ) ? $this->details['user_id'] : false; 
  1303. $wishlists = array(); 
  1304.  
  1305. if( $return == 'true' ) { 
  1306. $message = apply_filters( 'yith_wcwl_product_added_to_wishlist_message', get_option( 'yith_wcwl_product_added_text' ) ); 
  1307. elseif( $return == 'exists' ) { 
  1308. $message = apply_filters( 'yith_wcwl_product_already_in_wishlist_message', get_option( 'yith_wcwl_already_in_wishlist_text' ) ); 
  1309. elseif( count( $this->errors ) > 0 ) { 
  1310. $message = apply_filters( 'yith_wcwl_error_adding_to_wishlist_message', $this->get_errors() ); 
  1311.  
  1312. if( $user_id != false ) { 
  1313. $wishlists = $this->get_wishlists( array( 'user_id' => $user_id ) ); 
  1314.  
  1315. wp_send_json( 
  1316. array( 
  1317. 'result' => $return,  
  1318. 'message' => $message,  
  1319. 'user_wishlists' => $wishlists,  
  1320. 'wishlist_url' => $this->get_wishlist_url( 'view' . ( isset( $this->last_operation_token ) ? ( '/' . $this->last_operation_token ) : false ) ),  
  1321. ); 
  1322.  
  1323. /** 
  1324. * AJAX: remove from wishlist action 
  1325. *  
  1326. * @return void 
  1327. * @since 1.0.0 
  1328. */ 
  1329. public function remove_from_wishlist_ajax() { 
  1330. $wishlist_token = isset( $this->details['wishlist_token'] ) ? $this->details['wishlist_token'] : false; 
  1331. $count = $this->count_products( $wishlist_token ); 
  1332. $message = ''; 
  1333.  
  1334. if( $count != 0 ) { 
  1335. if ( $this->remove() ) { 
  1336. $message = apply_filters( 'yith_wcwl_product_removed_text', __( 'Product successfully removed.', 'yith-woocommerce-wishlist' ) ); 
  1337. $count --; 
  1338. else { 
  1339. $message = apply_filters( 'yith_wcwl_unable_to_remove_product_message', __( 'Error. Unable to remove the product from the wishlist.', 'yith-woocommerce-wishlist' ) ); 
  1340. else{ 
  1341. $message = apply_filters( 'yith_wcwl_no_product_to_remove_message', __( 'No products were added to the wishlist', 'yith-woocommerce-wishlist' ) ); 
  1342.  
  1343. wc_add_notice( $message ); 
  1344.  
  1345. $atts = array( 'wishlist_id' => $wishlist_token ); 
  1346. if( isset( $this->details['pagination'] ) ) { 
  1347. $atts['pagination'] = $this->details['pagination']; 
  1348.  
  1349. if( isset( $this->details['per_page'] ) ) { 
  1350. $atts['per_page'] = $this->details['per_page']; 
  1351.  
  1352. echo YITH_WCWL_Shortcode::wishlist( $atts ); 
  1353. die(); 
  1354.  
  1355. /******************************************* 
  1356. * INTEGRATION WC Frequently Bought Together 
  1357. *******************************************/ 
  1358.  
  1359. /** 
  1360. * AJAX: reload wishlist and adding elem action 
  1361. * @return void 
  1362. * @since 1.0.0 
  1363. */ 
  1364. public function reload_wishlist_and_adding_elem_ajax() { 
  1365.  
  1366. $return = $this->add(); 
  1367. $message = ''; 
  1368. $type_msg = 'success'; 
  1369.  
  1370. if( $return == 'true' ) { 
  1371. $message = apply_filters( 'yith_wcwl_product_added_to_wishlist_message', get_option( 'yith_wcwl_product_added_text' ) ); 
  1372. elseif( $return == 'exists' ) { 
  1373. $message = apply_filters( 'yith_wcwl_product_already_in_wishlist_message', get_option( 'yith_wcwl_already_in_wishlist_text' ) ); 
  1374. $type_msg = 'error'; 
  1375. else { 
  1376. $message = apply_filters( 'yith_wcwl_product_removed_text', __( 'An error as occurred.', 'yith-woocommerce-wishlist' ) ); 
  1377. $type_msg = 'error'; 
  1378.  
  1379. $wishlist_token = isset( $this->details['wishlist_token'] ) ? $this->details['wishlist_token'] : false; 
  1380.  
  1381. $atts = array( 'wishlist_id' => $wishlist_token ); 
  1382. if( isset( $this->details['pagination'] ) ) { 
  1383. $atts['pagination'] = $this->details['pagination']; 
  1384.  
  1385. if( isset( $this->details['per_page'] ) ) { 
  1386. $atts['per_page'] = $this->details['per_page']; 
  1387.  
  1388. ob_start(); 
  1389.  
  1390. wc_add_notice( $message, $type_msg ); 
  1391.  
  1392. echo '<div>'. YITH_WCWL_Shortcode::wishlist( $atts ) . '</div>'; 
  1393.  
  1394. echo ob_get_clean(); 
  1395. die(); 
  1396.  
  1397.  
  1398. /** 
  1399. * redirect after add to cart from YITH WooCommerce Frequently Bought Together Premium shortcode 
  1400. * @since 1.0.0 
  1401. */ 
  1402. public function yith_wfbt_redirect_after_add_to_cart( $url ) { 
  1403. if( ! isset( $_REQUEST['yith_wfbt_shortcode'] ) ) { 
  1404. return $url; 
  1405.  
  1406. $cart_url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : WC()->cart->get_cart_url(); 
  1407.  
  1408. return get_option( 'yith_wcwl_redirect_cart' ) == 'yes' ? $cart_url : $this->get_wishlist_url();