YITH_WCWL_Shortcode

YITH WCWL Shortcodes.

Defined (1)

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

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