/includes/class.yith-wcwl-shortcode.php

  1. <?php 
  2. /** 
  3. * Shortcodes class 
  4. * 
  5. * @author Your Inspiration Themes 
  6. * @package YITH WooCommerce Wishlist 
  7. * @version 1.1.5 
  8. */ 
  9.  
  10. if ( ! defined( 'YITH_WCWL' ) ) { exit; } // Exit if accessed directly 
  11.  
  12. if( ! class_exists( 'YITH_WCWL_Shortcode' ) ) { 
  13. /** 
  14. * YITH WCWL Shortcodes 
  15. * 
  16. * @since 1.0.0 
  17. */ 
  18. class YITH_WCWL_Shortcode { 
  19. /** 
  20. * Print the wishlist HTML. 
  21. * 
  22. * @since 1.0.0 
  23. */ 
  24. public static function wishlist( $atts, $content = null ) { 
  25. global $yith_wcwl_is_wishlist, $yith_wcwl_wishlist_token; 
  26.  
  27. $atts = shortcode_atts( array( 
  28. 'per_page' => 5,  
  29. 'pagination' => 'no',  
  30. 'wishlist_id' => false,  
  31. 'action_params' => get_query_var( 'wishlist-action', false ) 
  32. ), $atts ); 
  33.  
  34. $available_views = apply_filters( 'yith_wcwl_available_wishlist_views', array( 'view', 'user' ) ); 
  35.  
  36. extract( $atts ); 
  37.  
  38. // retrieve options from query string 
  39. $action_params = explode( '/', apply_filters( 'yith_wcwl_current_wishlist_view_params', $action_params ) ); 
  40. $action = ( isset( $action_params[0] ) ) ? $action_params[0] : 'view'; 
  41.  
  42. $user_id = isset( $_GET['user_id'] ) ? $_GET['user_id'] : false; 
  43.  
  44. // init params needed to load correct tempalte 
  45. $additional_params = array(); 
  46. $template_part = 'view'; 
  47.  
  48. /** === WISHLIST TEMPLATE === */ 
  49. if( 
  50. empty( $action ) || 
  51. ( ! empty( $action ) && ( $action == 'view' || $action == 'user' ) ) || 
  52. ( ! empty( $action ) && ( $action == 'manage' || $action == 'create' ) && get_option( 'yith_wcwl_multi_wishlist_enable', false ) != 'yes' ) || 
  53. ( ! empty( $action ) && ! in_array( $action, $available_views ) ) || 
  54. ! empty( $user_id ) 
  55. ) { 
  56. /** 
  57. * someone is requesting a wishlist 
  58. * -if user is not logged in.. 
  59. * -and no wishlist_id is passed, cookie wishlist is loaded 
  60. * -and a wishlist_id is passed, checks if wishlist is public or shared, and shows it only in this case 
  61. * -if user is logged in.. 
  62. * -and no wishlist_id is passed, default wishlist is loaded 
  63. * -and a wishlist_id is passed, checks owner of the wishlist 
  64. * -if wishlist is of the logged user, shows it 
  65. * -if wishlist is of another user, checks if wishlist is public or shared, and shows it only in this case (if user is admin, can see all wishlists) 
  66. */ 
  67.  
  68. if( empty( $wishlist_id ) ) { 
  69. if ( ! empty( $action ) && $action == 'user' ) { 
  70. $user_id = isset( $action_params[1] ) ? $action_params[1] : false; 
  71. $user_id = ( ! $user_id ) ? get_query_var( $user_id, false ) : $user_id; 
  72. $user_id = ( ! $user_id ) ? get_current_user_id() : $user_id; 
  73.  
  74. $wishlists = YITH_WCWL()->get_wishlists( array( 'user_id' => $user_id, 'is_default' => 1 ) ); 
  75.  
  76. if ( ! empty( $wishlists ) && isset( $wishlists[0] ) ) { 
  77. $wishlist_id = $wishlists[0]['wishlist_token']; 
  78. } else { 
  79. $wishlist_id = false; 
  80. } else { 
  81. $wishlist_id = isset( $action_params[1] ) ? $action_params[1] : false; 
  82. $wishlist_id = ( ! $wishlist_id ) ? get_query_var( 'wishlist_id', false ) : $wishlist_id; 
  83.  
  84. $is_user_owner = false; 
  85. $query_args = array(); 
  86.  
  87. if( ! empty( $user_id ) ) { 
  88. $query_args[ 'user_id' ] = $user_id; 
  89. $query_args[ 'is_default' ] = 1; 
  90.  
  91. if( get_current_user_id() == $user_id ) { 
  92. $is_user_owner = true; 
  93. elseif( ! is_user_logged_in() ) { 
  94. if( empty( $wishlist_id ) ) { 
  95. $query_args[ 'wishlist_id' ] = false; 
  96. $is_user_owner = true; 
  97. else{ 
  98. $is_user_owner = false; 
  99.  
  100. $query_args[ 'wishlist_token' ] = $wishlist_id; 
  101. $query_args[ 'wishlist_visibility' ] = 'visible'; 
  102. else{ 
  103. if( empty( $wishlist_id ) ) { 
  104. $query_args[ 'user_id' ] = get_current_user_id(); 
  105. $query_args[ 'is_default' ] = 1; 
  106. $is_user_owner = true; 
  107. else{ 
  108. $wishlist = YITH_WCWL()->get_wishlist_detail_by_token( $wishlist_id ); 
  109. $is_user_owner = $wishlist['user_id'] == get_current_user_id(); 
  110.  
  111. $query_args[ 'wishlist_token' ] = $wishlist_id; 
  112.  
  113. if( ! empty( $wishlist ) && $wishlist['user_id'] != get_current_user_id() ) { 
  114. $query_args[ 'user_id' ] = false; 
  115. if( ! current_user_can( 'manage_options' ) ) { 
  116. $query_args[ 'wishlist_visibility' ] = 'visible'; 
  117.  
  118. // counts number of elements in wishlist for the user 
  119. $count = YITH_WCWL()->count_products( $wishlist_id ); 
  120.  
  121. // sets current page, number of pages and element offset 
  122. $current_page = max( 1, get_query_var( 'paged' ) ); 
  123.  
  124. // sets variables for pagination, if shortcode atts is set to yes 
  125. if( $pagination == 'yes' && $count > 1 ) { 
  126. $pages = ceil( $count / $per_page ); 
  127.  
  128. if( $current_page > $pages ) { 
  129. $current_page = $pages; 
  130.  
  131. $offset = ( $current_page - 1 ) * $per_page; 
  132.  
  133. if( $pages > 1 ) { 
  134. $page_links = paginate_links( array( 
  135. 'base' => esc_url( add_query_arg( array( 'paged' => '%#%' ), YITH_WCWL()->get_wishlist_url( 'view' . '/' . $wishlist_id ) ) ),  
  136. 'format' => '?paged=%#%',  
  137. 'current' => $current_page,  
  138. 'total' => $pages,  
  139. 'show_all' => true 
  140. ) ); 
  141.  
  142. $query_args[ 'limit' ] = $per_page; 
  143. $query_args[ 'offset' ] = $offset; 
  144.  
  145. if( empty( $wishlist_id ) && is_user_logged_in() ) { 
  146. $wishlists = YITH_WCWL()->get_wishlists( array( 'user_id' => get_current_user_id(), 'is_default' => 1 ) ); 
  147. if( ! empty( $wishlists ) ) { 
  148. $wishlist_id = $wishlists[0]['wishlist_token']; 
  149.  
  150. $yith_wcwl_wishlist_token = $wishlist_id; 
  151.  
  152. // retrieve items to print 
  153. $wishlist_items = YITH_WCWL()->get_products( $query_args ); 
  154.  
  155. // retrieve wishlist information 
  156. $wishlist_meta = YITH_WCWL()->get_wishlist_detail_by_token( $wishlist_id ); 
  157. $is_default = $wishlist_meta['is_default'] == 1; 
  158. $wishlist_token = ! empty( $wishlist_meta['wishlist_token'] ) ? $wishlist_meta['wishlist_token'] : false; 
  159.  
  160. // retireve wishlist title 
  161. $default_wishlist_title = get_option( 'yith_wcwl_wishlist_title' ); 
  162.  
  163. $wishlist_title = $is_default ? $default_wishlist_title : $wishlist_meta['wishlist_name']; 
  164.  
  165. // retrieve estimate options 
  166. $show_ask_estimate_button = get_option( 'yith_wcwl_show_estimate_button' ) == 'yes'; 
  167. $ask_estimate_url = false; 
  168. if( $show_ask_estimate_button ) { 
  169. $ask_estimate_url = esc_url( wp_nonce_url( 
  170. add_query_arg( 
  171. 'ask_an_estimate',  
  172. ! empty( $wishlist_token ) ? $wishlist_token : 'false',  
  173. YITH_WCWL()->get_wishlist_url( 'view' . ( ! $is_default ? '/' . $wishlist_token : '' ) ) 
  174. ),  
  175. 'ask_an_estimate',  
  176. 'estimate_nonce' 
  177. ) ); 
  178.  
  179. // retrieve share options 
  180. $share_facebook_enabled = get_option( 'yith_wcwl_share_fb' ) == 'yes'; 
  181. $share_twitter_enabled = get_option( 'yith_wcwl_share_twitter' ) == 'yes'; 
  182. $share_pinterest_enabled = get_option( 'yith_wcwl_share_pinterest' ) == 'yes'; 
  183. $share_googleplus_enabled = get_option( 'yith_wcwl_share_googleplus' ) == 'yes'; 
  184. $share_email_enabled = get_option( 'yith_wcwl_share_email' ) == 'yes'; 
  185.  
  186. $show_date_added = get_option( 'yith_wcwl_show_dateadded' ) == 'yes'; 
  187. $show_add_to_cart = get_option( 'yith_wcwl_add_to_cart_show' ) == 'yes'; 
  188. $repeat_remove_button = get_option( 'yith_wcwl_repeat_remove_button' ) == 'yes'; 
  189.  
  190. $share_enabled = $share_facebook_enabled || $share_twitter_enabled || $share_pinterest_enabled || $share_googleplus_enabled || $share_email_enabled; 
  191.  
  192. $additional_params = array( 
  193. // wishlist items 
  194. 'count' => $count,  
  195. 'wishlist_items' => $wishlist_items,  
  196.  
  197. // wishlist data 
  198. 'wishlist_meta' => $wishlist_meta,  
  199. 'is_default' => $is_default,  
  200. 'is_custom_list' => ! $is_default && $is_user_owner,  
  201. 'wishlist_token' => $wishlist_token,  
  202. 'wishlist_id' => $wishlist_meta['ID'],  
  203. 'is_private' => $wishlist_meta['wishlist_privacy'] == 2,  
  204.  
  205. //page data 
  206. 'page_title' => $wishlist_title,  
  207. 'default_wishlsit_title' => $default_wishlist_title,  
  208. 'current_page' => $current_page,  
  209. 'page_links' => isset( $page_links ) ? $page_links : false,  
  210.  
  211. // user data 
  212. 'is_user_logged_in' => is_user_logged_in(),  
  213. 'is_user_owner' => $is_user_owner,  
  214.  
  215. // view data 
  216. 'show_price' => get_option( 'yith_wcwl_price_show' ) == 'yes',  
  217. 'show_dateadded' => $show_date_added,  
  218. 'show_ask_estimate_button' => $show_ask_estimate_button,  
  219. 'ask_estimate_url' => $ask_estimate_url,  
  220. 'show_stock_status' => get_option( 'yith_wcwl_stock_show' ) == 'yes',  
  221. 'show_add_to_cart' => $show_add_to_cart,  
  222. 'add_to_cart_text' => get_option( 'yith_wcwl_add_to_cart_text' ),  
  223. 'price_excl_tax' => get_option( 'woocommerce_tax_display_cart' ) == 'excl',  
  224. 'show_cb' => false,  
  225. 'repeat_remove_button' => $repeat_remove_button,  
  226. 'show_last_column' => ( $show_date_added && is_user_logged_in() ) || $show_add_to_cart || $repeat_remove_button,  
  227.  
  228. // share data 
  229. 'share_enabled' => $share_enabled,  
  230.  
  231. // template data 
  232. 'template_part' => $template_part,  
  233. 'additional_info' => false,  
  234. 'available_multi_wishlist' => false,  
  235. 'users_wishlists' => array(),  
  236. 'form_action' => esc_url( YITH_WCWL()->get_wishlist_url( 'view' . ( ! $is_default ? '/' . $wishlist_token : '' ) ) ) 
  237. ); 
  238.  
  239. if( $share_enabled ) { 
  240. $share_title = apply_filters( 'yith_wcwl_socials_share_title', __( 'Share on:', 'yith-woocommerce-wishlist' ) ); 
  241. $share_link_url = ( ! empty( $wishlist_id ) ) ? YITH_WCWL()->get_wishlist_url( 'view' . '/' . $wishlist_id ) : YITH_WCWL()->get_wishlist_url( 'user' . '/' . get_current_user_id() ); 
  242. $share_links_title = apply_filters( 'plugin_text', urlencode( get_option( 'yith_wcwl_socials_title' ) ) ); 
  243. $share_twitter_summary = urlencode( str_replace( '%wishlist_url%', '', get_option( 'yith_wcwl_socials_text' ) ) ); 
  244. $share_summary = urlencode( str_replace( '%wishlist_url%', $share_link_url, get_option( 'yith_wcwl_socials_text' ) ) ); 
  245. $share_image_url = urlencode( get_option( 'yith_wcwl_socials_image_url' ) ); 
  246.  
  247. $share_atts = array( 
  248. 'share_facebook_enabled' => $share_facebook_enabled,  
  249. 'share_twitter_enabled' => $share_twitter_enabled,  
  250. 'share_pinterest_enabled' => $share_pinterest_enabled,  
  251. 'share_googleplus_enabled' => $share_googleplus_enabled,  
  252. 'share_email_enabled' => $share_email_enabled,  
  253. 'share_title' => $share_title,  
  254. 'share_link_url' => $share_link_url,  
  255. 'share_link_title' => $share_links_title,  
  256. 'share_twitter_summary' => $share_twitter_summary,  
  257. 'share_summary' => $share_summary,  
  258. 'share_image_url' => $share_image_url 
  259. ); 
  260.  
  261. $additional_params['share_atts'] = $share_atts; 
  262.  
  263. $additional_params = apply_filters( 'yith_wcwl_wishlist_params', $additional_params, $action, $action_params, $pagination, $per_page ); 
  264. $additional_params['template_part'] = isset( $additional_params['template_part'] ) ? $additional_params['template_part'] : $template_part; 
  265.  
  266. $atts = array_merge( 
  267. $atts,  
  268. $additional_params 
  269. ); 
  270.  
  271. // adds attributes list to params to extract in template, so it can be passed through a new get_template() 
  272. $atts['atts'] = $atts; 
  273.  
  274. // apply filters for add to cart buttons 
  275. add_filter( 'woocommerce_loop_add_to_cart_link', array( 'YITH_WCWL_UI', 'alter_add_to_cart_button' ), 10, 2 ); 
  276.  
  277. // sets that we're in the wishlist template 
  278. $yith_wcwl_is_wishlist = true; 
  279.  
  280. $template = yith_wcwl_get_template( 'wishlist.php', $atts, true ); 
  281.  
  282. // we're not in wishlist template anymore 
  283. $yith_wcwl_is_wishlist = false; 
  284. $yith_wcwl_wishlist_token = null; 
  285.  
  286. // remove filters for add to cart buttons 
  287. remove_filter( 'woocommerce_loop_add_to_cart_link', array( 'YITH_WCWL_UI', 'alter_add_to_cart_button' ) ); 
  288.  
  289. return apply_filters( 'yith_wcwl_wishlisth_html', $template, array(), true ); 
  290.  
  291. /** 
  292. * Return "Add to Wishlist" button. 
  293. * 
  294. * @since 1.0.0 
  295. */ 
  296. public static function add_to_wishlist( $atts, $content = null ) { 
  297. global $product; 
  298.  
  299. // product object 
  300. $current_product = ( isset( $atts['product_id'] ) ) ? wc_get_product( $atts['product_id'] ) : false; 
  301. $current_product = $current_product ? $current_product : $product; 
  302. $current_product_id = yit_get_product_id( $current_product ); 
  303.  
  304. $template_part = 'button'; 
  305.  
  306. // labels & icons settings 
  307. $label_option = get_option( 'yith_wcwl_add_to_wishlist_text' ); 
  308. $icon_option = get_option( 'yith_wcwl_add_to_wishlist_icon' ) != 'none' ? get_option( 'yith_wcwl_add_to_wishlist_icon' ) : ''; 
  309.  
  310. $label = apply_filters( 'yith_wcwl_button_label', $label_option ); 
  311. $icon = apply_filters( 'yith_wcwl_button_icon', $icon_option ); 
  312.  
  313. $browse_wishlist = get_option( 'yith_wcwl_browse_wishlist_text' ); 
  314.  
  315. $already_in_wishlist = get_option( 'yith_wcwl_already_in_wishlist_text' ); 
  316.  
  317. $product_added = get_option( 'yith_wcwl_product_added_text' ); 
  318.  
  319. // button class 
  320. $classes = apply_filters( 'yith_wcwl_add_to_wishlist_button_classes', get_option( 'yith_wcwl_use_button' ) == 'yes' ? 'add_to_wishlist single_add_to_wishlist button alt' : 'add_to_wishlist' ); 
  321.  
  322. // default wishlist id 
  323. $default_wishlists = is_user_logged_in() ? YITH_WCWL()->get_wishlists( array( 'is_default' => true ) ) : false; 
  324.  
  325. if( ! empty( $default_wishlists ) ) { 
  326. $default_wishlist = $default_wishlists[0]['ID']; 
  327. else{ 
  328. $default_wishlist = false; 
  329.  
  330. // exists in default wishlist 
  331. $exists = YITH_WCWL()->is_product_in_wishlist( $current_product_id, $default_wishlist ); 
  332.  
  333. // get wishlist url 
  334. $wishlist_url = YITH_WCWL()->get_wishlist_url(); 
  335.  
  336. // get product type 
  337. $product_type = $product->get_type(); 
  338.  
  339. $additional_params = array( 
  340. 'wishlist_url' => $wishlist_url,  
  341. 'exists' => $exists,  
  342. 'product_id' => $current_product_id,  
  343. 'product_type' => $product_type,  
  344. 'label' => $label,  
  345. 'browse_wishlist_text' => $browse_wishlist,  
  346. 'already_in_wishslist_text' => $already_in_wishlist,  
  347. 'product_added_text' => $product_added,  
  348. 'icon' => $icon,  
  349. 'link_classes' => $classes,  
  350. 'available_multi_wishlist' => false,  
  351. 'disable_wishlist' => false 
  352. ); 
  353.  
  354. $additional_params = apply_filters( 'yith_wcwl_add_to_wishlist_params', $additional_params ); 
  355. $additional_params['template_part'] = isset( $additional_params['template_part'] ) ? $additional_params['template_part'] : $template_part; 
  356.  
  357. $atts = shortcode_atts( 
  358. $additional_params,  
  359. $atts 
  360. ); 
  361.  
  362. $atts['icon'] = ! empty( $atts['icon'] ) ? '<i class="fa ' . $atts['icon'] . '"></i>' : ''; 
  363.  
  364. // adds attributes list to params to extract in template, so it can be passed through a new get_template() 
  365. $atts['atts'] = $atts; 
  366.  
  367. $template = yith_wcwl_get_template( 'add-to-wishlist.php', $atts, true ); 
  368.  
  369. return apply_filters( 'yith_wcwl_add_to_wishlisth_button_html', $template, $wishlist_url, $product_type, $exists ); 
  370.  
  371. add_shortcode( 'yith_wcwl_wishlist', array( 'YITH_WCWL_Shortcode', 'wishlist' ) ); 
  372. add_shortcode( 'yith_wcwl_add_to_wishlist', array( 'YITH_WCWL_Shortcode', 'add_to_wishlist' ) ); 
.