PageRenderTime 99ms CodeModel.GetById 28ms RepoModel.GetById 7ms app.codeStats 0ms

/theme WP a cương/test_wp/wp-content/plugins/yith-woocommerce-wishlist/class.yith-wcwl.php

https://gitlab.com/hop23typhu/list-theme
PHP | 382 lines | 184 code | 55 blank | 143 comment | 35 complexity | f6b2f14e7a11841a09d3c31f62435e6e MD5 | raw file
  1. <?php
  2. /**
  3. * Main class
  4. *
  5. * @author Your Inspiration Themes
  6. * @package YITH WooCommerce Wishlist
  7. * @version 1.0.0
  8. */
  9. if ( !defined( 'YITH_WCWL' ) ) { exit; } // Exit if accessed directly
  10. if( !class_exists( 'YITH_WCWL' ) ) {
  11. /**
  12. * WooCommerce Wishlist
  13. *
  14. * @since 1.0.0
  15. */
  16. class YITH_WCWL {
  17. /**
  18. * Errors array
  19. *
  20. * @var array
  21. * @since 1.0.0
  22. */
  23. public $errors;
  24. /**
  25. * Details array
  26. *
  27. * @var array
  28. * @since 1.0.0
  29. */
  30. public $details;
  31. /**
  32. * Messages array
  33. *
  34. * @var array
  35. * @since 1.0.0
  36. */
  37. public $messages;
  38. /**
  39. * Constructor.
  40. *
  41. * @param array $details
  42. * @return void
  43. * @since 1.0.0
  44. */
  45. public function __construct( $details ) {
  46. // Start a PHP session, if not yet started
  47. if ( ! session_id() )
  48. session_start();
  49. $this->details = $details;
  50. $yith_wcwl_init = new YITH_WCWL_Init();
  51. add_action( 'wp_ajax_add_to_wishlist', array( $this, 'add_to_wishlist_ajax' ) );
  52. add_action( 'wp_ajax_nopriv_add_to_wishlist', array( $this, 'add_to_wishlist_ajax' ) );
  53. add_action( 'wp_ajax_remove_from_wishlist', array( $this, 'remove_from_wishlist_ajax' ) );
  54. add_action( 'wp_ajax_nopriv_remove_from_wishlist', array( $this, 'remove_from_wishlist_ajax' ) );
  55. }
  56. /**
  57. * Check if the product exists in the wishlist.
  58. *
  59. * @param int $product_id
  60. * @return bool
  61. * @since 1.0.0
  62. */
  63. public function is_product_in_wishlist( $product_id ) {
  64. global $wpdb;
  65. $exists = false;
  66. if( is_user_logged_in() ) {
  67. $sql = "SELECT COUNT(*) as `cnt` FROM `" . YITH_WCWL_TABLE . "` WHERE `prod_id` = " . $product_id . " AND `user_id` = " . $this->details['user_id'];
  68. $results = $wpdb->get_results( $sql );
  69. $exists = $results[0]->cnt > 0 ? true : false;
  70. } else {
  71. if( yith_usecookies() ) {
  72. $tmp_products = yith_getcookie( 'yith_wcwl_products' );
  73. } elseif( isset( $_SESSION['yith_wcwl_products'] ) ) {
  74. $tmp_products = $_SESSION['yith_wcwl_products'];
  75. } else {
  76. $tmp_products = array();
  77. }
  78. if( isset( $tmp_products[ $product_id ] ) )
  79. { $exists = 1; }
  80. else
  81. { $exists = 0; }
  82. }
  83. return $exists;
  84. }
  85. /**
  86. * Add a product in the wishlist.
  87. *
  88. * @return string "error", "true" or "exists"
  89. * @since 1.0.0
  90. */
  91. public function add() {
  92. global $wpdb, $woocommerce;
  93. if ( isset( $this->details['add_to_wishlist'] ) && is_numeric( $this->details['add_to_wishlist'] ) ) {
  94. //single product
  95. $quantity = ( isset( $this->details['quantity'] ) ) ? ( int ) $this->details['quantity'] : 1;
  96. //check for existence, product ID, variation ID, variation data, and other cart item data
  97. if( $this->is_product_in_wishlist( $this->details['add_to_wishlist'] ) ) {
  98. return "exists";
  99. }
  100. $return = "error";
  101. if( is_user_logged_in() ) {
  102. $sql = "INSERT INTO `" . YITH_WCWL_TABLE . "` ( `prod_id`, `quantity`, `user_id`, `dateadded` ) VALUES ( " . $this->details['add_to_wishlist'] . " , $quantity, " . $this->details['user_id'] . ", now() )";
  103. $return = $wpdb->query( $sql ); //echo $sql;die;
  104. } elseif( yith_usecookies() ) {
  105. $cookie[$this->details['add_to_wishlist']]['add-to-wishlist'] = $this->details['add_to_wishlist'];
  106. $cookie[$this->details['add_to_wishlist']]['quantity'] = $quantity;
  107. $cookie = $cookie + yith_getcookie( 'yith_wcwl_products' );
  108. yith_setcookie( 'yith_wcwl_products', $cookie );
  109. $return = true;
  110. } else {
  111. $_SESSION['yith_wcwl_products'][$this->details['add_to_wishlist']]['add-to-wishlist'] = $this->details['add_to_wishlist'];
  112. $_SESSION['yith_wcwl_products'][$this->details['add_to_wishlist']]['quantity'] = $quantity;
  113. $return = true;
  114. }
  115. if( $return ) {
  116. return "true";
  117. } else {
  118. $this->errors[] = __( 'Error occurred while adding product to wishlist.', 'yit' );
  119. return "error";
  120. }
  121. }
  122. }
  123. /**
  124. * Remove an entry from the wishlist.
  125. *
  126. * @param int $id Record ID
  127. * @return bool
  128. * @since 1.0.0
  129. */
  130. public function remove( $id ) {
  131. global $wpdb;
  132. if( is_user_logged_in() ) {
  133. $sql = "DELETE FROM `" . YITH_WCWL_TABLE . "` WHERE `ID` = " . $id . " AND `user_id` = " . $this->details['user_id'];
  134. if( $wpdb->query( $sql ) ) {
  135. return true;
  136. } else {
  137. $this->errors[] = __( 'Error occurred while removing product from wishlist', 'yit' );
  138. return false;
  139. }
  140. } elseif( yith_usecookies() ) {
  141. $cookie = yith_getcookie( 'yith_wcwl_products' );
  142. unset( $cookie[$id] );
  143. yith_destroycookie( 'yith_wcwl_products' );
  144. yith_setcookie( 'yith_wcwl_products', $cookie );
  145. return true;
  146. } else {
  147. unset( $_SESSION['yith_wcwl_products'][$id] );
  148. return true;
  149. }
  150. }
  151. /**
  152. * Get all errors in HTML mode or simple string.
  153. *
  154. * @param bool $html
  155. * @return string
  156. * @since 1.0.0
  157. */
  158. public function get_errors( $html = true ) {
  159. return implode( ( $html ? '<br />' : ', ' ), $this->errors );
  160. }
  161. /**
  162. * Retrieve the number of products in the wishlist.
  163. *
  164. * @return int
  165. * @since 1.0.0
  166. */
  167. public function count_products() {
  168. global $wpdb;
  169. if( is_user_logged_in() ) {
  170. $sql = "SELECT COUNT(*) as `cnt` FROM `" . YITH_WCWL_TABLE . "` WHERE `user_id` = " . get_current_user_id();
  171. $results = $wpdb->get_results( $sql );
  172. return $results[0]->cnt;
  173. } elseif( yith_usecookies() ) {
  174. $cookie = yith_getcookie( 'yith_wcwl_products' );
  175. return count( $cookie );
  176. } else {
  177. if( isset( $_SESSION['yith_wcwl_products'] ) )
  178. { return count( $_SESSION['yith_wcwl_products'] ); }
  179. }
  180. return 0;
  181. }
  182. /**
  183. * Retrieve details of a product in the wishlist.
  184. *
  185. * @param int $id
  186. * @param string $request_from
  187. * @return array
  188. * @since 1.0.0
  189. */
  190. public function get_product_details( $id ) {
  191. global $wpdb;
  192. if( is_user_logged_in() ) {
  193. return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM `' . YITH_WCWL_TABLE . '` WHERE `prod_id` = %d', $id ), ARRAY_A );
  194. } elseif( yith_usecookies() ) {
  195. $cookie = yith_getcookie( 'yith_wcwl_products' );
  196. $temp_arr[0] = $cookie[$id];
  197. $temp_arr[0]['prod_id'] = $id;
  198. return $temp_arr;
  199. } else {
  200. $temp_arr[0] = $_SESSION['yith_wcwl_products'][$id];
  201. $temp_arr[0]['prod_id'] = $id;
  202. return $temp_arr;
  203. }
  204. return array();
  205. }
  206. /**
  207. * Build wishlist page URL.
  208. *
  209. * @return string
  210. * @since 1.0.0
  211. */
  212. public function get_wishlist_url() {
  213. return get_permalink( get_option( 'yith_wcwl_wishlist_page_id' ) );
  214. }
  215. /**
  216. * Build wishlist page URL based on user ID.
  217. *
  218. * @param int $user_id
  219. * @return string
  220. * @since 1.0.0
  221. */
  222. public function get_public_wishlist_url( $user_id ) {
  223. return home_url() . '/?id=' . $user_id . '&page_id=' . $page_id;
  224. }
  225. /**
  226. * Build the URL used to remove an item from the wishlist.
  227. *
  228. * @param int $item_id
  229. * @return string
  230. * @since 1.0.0
  231. */
  232. public function get_remove_url( $item_id ) {
  233. return admin_url( 'admin-ajax.php?wishlist_item_id=' . $item_id );
  234. //return YITH_WCWL_URL . 'yith-wcwl-ajax.php?action=remove_from_wishlist&wishlist_item_id=' . $item_id;
  235. }
  236. /**
  237. * Build the URL used to add an item in the wishlist.
  238. *
  239. * @return string
  240. * @since 1.0.0
  241. */
  242. public function get_addtowishlist_url() {
  243. global $product;
  244. $url = YITH_WCWL_URL . "yith-wcwl-ajax.php?action=add_to_wishlist&add_to_wishlist=" . $product->id;
  245. return $url;
  246. }
  247. /**
  248. * Build the URL used to add an item to the cart from the wishlist.
  249. *
  250. * @param int $id
  251. * @param int $user_id
  252. * @return string
  253. * @since 1.0.0
  254. */
  255. public function get_addtocart_url( $id, $user_id = '' ) {
  256. global $yith_wcwl;
  257. //$product = $yith_wcwl->get_product_details( $id );
  258. if ( function_exists( 'get_product' ) )
  259. $product = get_product( $id );
  260. else
  261. $product = new WC_Product( $id );
  262. if ( $product->product_type == 'variable' ) {
  263. return get_permalink( $product->id );
  264. }
  265. $url = YITH_WCWL_URL . 'add-to-cart.php?wishlist_item_id=' . rtrim( $id, '_' );
  266. if( $user_id != '' ) {
  267. $url .= '&id=' . $user_id;
  268. }
  269. return $url;
  270. }
  271. /**
  272. * Build the URL used for an external/affiliate product.
  273. *
  274. * @param $id
  275. * @return string
  276. */
  277. public function get_affiliate_product_url( $id ) {
  278. $product = get_product( $id );
  279. return get_post_meta( $product->id, '_product_url', true );
  280. }
  281. /**
  282. * Build an URL with the nonce added.
  283. *
  284. * @param string $action
  285. * @param string $url
  286. * @return string
  287. * @since 1.0.0
  288. */
  289. public function get_nonce_url( $action, $url = '' ) {
  290. return add_query_arg( '_n', wp_create_nonce( 'yith-wcwl-' . $action ), $url );
  291. }
  292. /**
  293. * AJAX: add to wishlist action
  294. *
  295. * @return void
  296. * @since 1.0.0
  297. */
  298. public function add_to_wishlist_ajax() {
  299. $return = $this->add();
  300. if( $return == 'true' )
  301. { echo $return . '##' . __( 'Product added!', 'yit' ); }
  302. elseif( $return == 'exists' )
  303. { echo $return . '##' . __( 'Product already in the wishlist.', 'yit' ); }
  304. elseif( count( $this->errors ) > 0 )
  305. { echo $this->get_errors(); }
  306. die();
  307. }
  308. /**
  309. * AJAX: remove from wishlist action
  310. *
  311. * @return void
  312. * @since 1.0.0
  313. */
  314. public function remove_from_wishlist_ajax() {
  315. $count = yith_wcwl_count_products();
  316. if( $this->remove( $_GET['wishlist_item_id'] ) )
  317. { echo apply_filters( 'yith_wcwl_product_removed_text', __( 'Product successfully removed.', 'yit' ) ); }
  318. else {
  319. echo '#' . $count . '#';
  320. _e( 'Error. Unable to remove the product from the wishlist.', 'yit' );
  321. }
  322. if( !$count )
  323. { _e( 'No products were added to the wishlist', 'yit' ); }
  324. die();
  325. }
  326. }
  327. }