PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/salejunction/yith-essential-kit-for-woocommerce-1/modules/yith-woocommerce-wishlist/includes/class.yith-wcwl-shortcode.php

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