PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / WishList / WishlistFrontEnd.php

WishlistFrontEnd.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at app/Modules/WishList/WishlistFrontEnd.php

445 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Wishlist Module Class
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Modules\WishList;
9
10 use RadiusTheme\SB\Helpers\Fns;
11 use RadiusTheme\SB\Traits\SingletonTrait;
12 use WC_Product;
13
14 // Do not allow directly accessing this file.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 'This script cannot be accessed directly.' );
17 }
18
19 /**
20 * Wishlist Module Class
21 */
22 class WishlistFrontEnd {
23
24 /**
25 * Singleton Instance
26 */
27 use SingletonTrait;
28
29 /**
30 * Asset Handle
31 *
32 * @var string
33 */
34 private $handle = 'rtsb-wishlist';
35
36 /**
37 * Class constructor.
38 */
39 public function __construct() {
40 add_filter( 'body_class', [ $this, 'add_body_class' ] );
41 add_filter( 'rtsb/module/wishlist/show_button', [ $this, 'wishlist_button_show_hide' ], 12 );
42 add_action( 'rtsb/modules/wishlist/frontend/display', [ $this, 'button_hook' ] );
43
44 add_action( 'rtsb/modules/wishlist/print_button', [ $this, 'print_button' ] );
45 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ], 99 ); // 99 Is Working to load after main script.
46
47 // ShortCode.
48 add_shortcode( 'rtsb_wishlist', [ $this, 'list_shortcode' ] );
49 add_shortcode( 'rtsb_wishlist_button', [ $this, 'button_shortcode' ] );
50 add_shortcode( 'rtsb_wishlist_counter', [ $this, 'counter_shortcode' ] );
51
52 $this->attach_button();
53 }
54
55 /**
56 * Hide Wishlist button.
57 *
58 * @param boolean $is_enable Wishlist Permission.
59 * @return boolean
60 */
61 public function wishlist_button_show_hide( $is_enable ) {
62 $enable_login_limit = Fns::get_option( 'modules', 'wishlist', 'enable_login_limit', false, 'checkbox' );
63 $hide_for_non_logged_in = Fns::get_option( 'modules', 'wishlist', 'hide_wishlist_non_logged_in', false, 'checkbox' );
64 if ( $enable_login_limit && $hide_for_non_logged_in && ! is_user_logged_in() ) {
65 $is_enable = false;
66 }
67 return $is_enable;
68 }
69
70 /**
71 * Script.
72 *
73 * @return void
74 */
75 public function enqueue() {
76 // Enqueue assets.
77 $this->handle = Fns::enqueue_module_assets( $this->handle, 'wishlist' );
78
79 if ( is_user_logged_in() && is_account_page() ) {
80 wp_enqueue_script( 'wc-add-to-cart' );
81 wp_enqueue_script( 'wc-add-to-cart-variation' );
82 }
83
84 $params = apply_filters(
85 'rtsb/module/wishlist/js_params',
86 [
87 'product_id' => get_the_ID(),
88 'resturl' => get_rest_url(),
89 'isLoggedIn' => is_user_logged_in(),
90 'pageUrl' => WishlistFns::instance()->get_page_url(),
91 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
92 'notice' => [
93 'add_to_wishlist' => esc_html__( 'Add to wishlist', 'shopbuilder' ),
94 'remove_wishlist' => esc_html__( 'Remove from list', 'shopbuilder' ),
95 'button_text' => Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Add to wishlist', 'shopbuilder' ) ),
96 'added' => Fns::get_option( 'modules', 'wishlist', 'notice_added_text', esc_html__( 'Product added!', 'shopbuilder' ) ),
97 'removed' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'Product removed!', 'shopbuilder' ) ),
98 'exist' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'The product is already in your wishlist!', 'shopbuilder' ) ),
99 'browse' => Fns::get_option( 'modules', 'wishlist', 'browse_list_text', esc_html__( 'Browse wishlist', 'shopbuilder' ) ),
100 'error' => esc_html__( 'Security error!', 'shopbuilder' ),
101 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ),
102 ],
103 ]
104 );
105
106 wp_localize_script( $this->handle, 'rtsbWishlistParams', $params );
107 }
108
109 /**
110 * Add the "Add to Wishlist" button. Needed to use in wp_head hook.
111 *
112 * @return void
113 * @since 1.0.0
114 */
115 public function attach_button() {
116 $positions = apply_filters(
117 'rtsb/module/wishlist/product_btn_positions',
118 [
119 'before_cart_btn' => [
120 'hook' => 'woocommerce_before_add_to_cart_button',
121 'priority' => 20,
122 ],
123 'after_add_to_cart' => [
124 'hook' => 'woocommerce_single_product_summary',
125 'priority' => 31,
126 ],
127 'after_thumbnail' => [
128 'hook' => 'woocommerce_product_thumbnails',
129 'priority' => 21,
130 ],
131 'after_summary' => [
132 'hook' => 'woocommerce_after_single_product_summary',
133 'priority' => 11,
134 ],
135 'custom' => [
136 'hook' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_name', '' ),
137 'priority' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_priority', 10 ),
138 ],
139 ]
140 );
141
142 // Add the link "Add to wishlist".
143 $product_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_product_page', true, 'checkbox' );
144 $product_btn_position = Fns::get_option( 'modules', 'wishlist', 'product_btn_position', 'after_add_to_cart' );
145 if ( $product_btn_enable && 'shortcode' !== $product_btn_position && isset( $positions[ $product_btn_position ]['hook'] ) ) {
146 add_action(
147 $positions[ $product_btn_position ]['hook'],
148 [
149 $this,
150 'button_hook',
151 ],
152 isset( $positions[ $product_btn_position ]['priority'] ) ? absint( $positions[ $product_btn_position ]['priority'] ) : ''
153 );
154 }
155 // check if Add to wishlist button is enabled for loop.
156 $loop_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_on_loop', false, 'checkbox' );
157 if ( ! $loop_btn_enable ) {
158 return;
159 }
160
161 $positions = apply_filters(
162 'rtsb/module/wishlist/loop_btn_position',
163 [
164 'before_add_to_cart' => [
165 'hook' => 'woocommerce_after_shop_loop_item',
166 'priority' => 7,
167 ],
168 'after_add_to_cart' => [
169 'hook' => 'woocommerce_after_shop_loop_item',
170 'priority' => 15,
171 ],
172 'custom' => [
173 'hook' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_name', '' ),
174 'priority' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_priority', 10 ),
175 ],
176 ]
177 );
178
179 // Add the link "Add to wishlist" in the loop.
180 $loop_btn_position = Fns::get_option( 'modules', 'wishlist', 'loop_btn_position', 'after_add_to_cart' );
181
182 if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) {
183 add_action( $positions[ $loop_btn_position ]['hook'], [ $this, 'button_hook' ], isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' );
184 }
185
186 // Add the link "Add to wishlist" for Gutenberg blocks.
187 add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 );
188 }
189
190
191 /**
192 * Add ATW button to Products block item
193 *
194 * @param string $item_html HTML of the single block item.
195 * @param array $data Data used to render the item.
196 * @param WC_Product $product Current product.
197 *
198 * @return string Filtered HTML.
199 */
200 public function add_button_for_block( $item_html, $data, $product ) {
201 // Add the link "Add to wishlist" in the loop.
202 $loop_btn_position = Fns::get_option( 'modules', 'wishlist', 'shop_btn_position', 'after_add_to_cart' );
203 ob_start();
204 $this->print_button( $product->get_id() );
205 $button = ob_get_clean();
206 $parts = [];
207
208 preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts );
209
210 if ( ! $parts || count( $parts ) < 5 ) {
211 return $item_html;
212 }
213
214 // removes first match (entire match).
215 array_shift( $parts );
216
217 // removes empty parts.
218 $parts = array_filter( $parts );
219
220 // searches for index to cut parts array.
221 switch ( $loop_btn_position ) {
222
223 case 'before_add_to_cart':
224 $index = 2;
225 break;
226 case 'after_add_to_cart':
227 $index = 3;
228 break;
229 default:
230 $index = 0;
231 break;
232 }
233
234 // if index is found, stitch button in correct position.
235 if ( $index ) {
236 $first_set = array_slice( $parts, 0, $index );
237 $second_set = array_slice( $parts, $index );
238
239 $parts = array_merge(
240 $first_set,
241 (array) $button,
242 $second_set
243 );
244
245 // replace li classes.
246 $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 add-to-wishlist-' . $loop_btn_position . '"', $parts[0] );
247 }
248
249 // join all parts together and return item.
250 return implode( '', $parts );
251 }
252
253 /**
254 * Print "Add to compare" button
255 *
256 * @return void
257 */
258 public function button_hook() {
259 if ( ! apply_filters( 'rtsb/module/wishlist/show_button', true ) ) {
260 return;
261 }
262 global $product;
263 if ( ! $product instanceof WC_Product ) {
264 return;
265 }
266 do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() );
267 }
268
269 /**
270 * Print "Add to compare" button
271 *
272 * @param int $product_id Product ID.
273 *
274 * @return void
275 */
276 public function print_button( $product_id = 0 ) {
277 global $product;
278
279 if ( ! $product instanceof WC_Product && $product_id ) {
280 $product = wc_get_product( $product_id );
281 }
282
283 // product parent.
284 $product_parent = $product->get_parent_id();
285
286 // button class.
287 $classes = [];
288 // check if product is already in wishlist.
289 $exists = WishlistFns::instance()->is_exists_in_wishlist( $product->get_id() );
290 if ( $exists ) {
291 $classes[] = 'rtsb-wishlist-remove';
292 $button_text = esc_html__( 'Remove from list', 'shopbuilder' );
293 $button_text = apply_filters( 'rtsb/modules/wishlist/remove_from_list_button_text', $button_text );
294 $icon_html = '<i class="rtsb-icon rtsb-icon-heart"></i>';
295 } else {
296 $classes[] = 'rtsb-wishlist-add';
297 $icon_html = '<i class="rtsb-icon rtsb-icon-heart-empty"></i>';
298 // labels & icons settings.
299 $button_text = Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Add to wishlist', 'shopbuilder' ) );
300 // button text.
301 $button_text = apply_filters( 'rtsb/module/wishlist/add_to_list_button_txt', $button_text );
302 }
303
304 // get product type.
305 $product_type = $product->get_type();
306 $params = [
307 'exists' => $exists,
308 'classes' => $classes,
309 'product_id' => $product->get_id(),
310 'parent_product_id' => $product_parent ?: $product->get_id(),
311 'product_type' => $product_type,
312 'button_text' => $button_text,
313 'show_button_text' => ! empty( Fns::get_option( 'modules', 'wishlist', 'button_text', '' ) ),
314 'left_text' => apply_filters( 'rtsb/module/wishlist/button_left_text', '' ),
315 'right_text' => apply_filters( 'rtsb/module/wishlist/button_right_text', '' ),
316 ];
317
318 // let third party developer filter options.
319 $atts = apply_filters( 'rtsb/module/wishlist/button_params', $params );
320
321 // set fragment options.
322 $atts['icon_html'] = apply_filters( 'rtsb/module/wishlist/icon_html', $icon_html, $atts );
323
324 Fns::load_template( 'wishlist/button', $atts );
325 }
326
327
328 /**
329 * Add specific body class when the Wishlist page is opened
330 *
331 * @param array $classes Existing boy classes.
332 *
333 * @return array
334 */
335 public function add_body_class( $classes ) {
336 $wishlist_page_id = Fns::get_option( 'modules', 'wishlist', 'page', '', 'number' );
337 if ( ! empty( $wishlist_page_id ) && is_page( $wishlist_page_id ) ) {
338 $classes[] = 'woocommerce-wishlist rtsb-wishlist-page';
339 $classes[] = 'woocommerce';
340 $classes[] = 'woocommerce-page';
341 }
342
343 return $classes;
344 }
345
346
347 /**
348 * Table List Shortcode callable function
349 *
350 * @param array $atts Shortcode attributes.
351 * @param string $content Shortcode content.
352 *
353 * @return string [HTML]
354 */
355 public function list_shortcode( $atts, $content = '' ) {
356 $this->handle = Fns::optimized_handle( $this->handle );
357
358 wp_enqueue_style( $this->handle );
359 wp_enqueue_script( $this->handle );
360
361 /* Fetch From option data */
362 $empty_text = Fns::get_option( 'modules', 'wishlist', 'empty_table_text', esc_html__( 'No product found at your wishlist.', 'shopbuilder' ) );
363 /* Product and Field */
364 $products = WishlistFns::instance()->get_products_data();
365 $field_ids = WishlistFns::instance()->get_field_ids();
366
367 $custom_headings = [];
368
369 $default_atts = [
370 'products' => $products,
371 'field_ids' => $field_ids,
372 'empty_text' => ! empty( $empty_text ) ? $empty_text : '',
373 ];
374
375 $atts = shortcode_atts( $default_atts, $atts, $content );
376 $args = apply_filters( 'rtsb/module/wishlist/list_args', $atts );
377
378 return Fns::load_template( 'wishlist/list', $args, true );
379 }
380
381 /**
382 * Wishlist button Shortcode callable function
383 *
384 * @return string [HTML]
385 */
386 public function button_shortcode() {
387 $this->handle = Fns::optimized_handle( $this->handle );
388
389 wp_enqueue_style( $this->handle );
390 wp_enqueue_script( $this->handle );
391
392 ob_start();
393 global $product;
394
395 if ( ! $product instanceof WC_Product ) {
396 return '';
397 }
398
399 do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() );
400
401 return ob_get_clean();
402 }
403
404 /**
405 * Counter Shortcode callable function
406 *
407 * @param array $atts Shortcode attributes.
408 * @param string $content Shortcode content.
409 *
410 * @return string [HTML]
411 */
412 public function counter_shortcode( $atts, $content = '' ) {
413 wp_enqueue_style( Fns::optimized_handle( $this->handle ) );
414
415 $enable_login_limit = Fns::get_option( 'modules', 'wishlist', 'enable_login_limit', false, 'checkbox' );
416 $products = WishlistFns::instance()->get_wishlist_ids();
417 if ( ! is_user_logged_in() && $enable_login_limit ) {
418 $button_text = esc_html__( 'Please login', 'shopbuilder' );
419 } else {
420 $button_text = esc_html__( 'Wishlist', 'shopbuilder' );
421 }
422 $icon_html = '<i class="rtsb-icon rtsb-icon-heart"></i>';
423 $page_url = WishlistFns::instance()->get_page_url();
424 $default_atts = [
425 'products' => $products,
426 'item_count' => count( $products ),
427 'page_url' => $page_url,
428 'button_text' => $button_text,
429 'enable_login_limit' => $enable_login_limit,
430 'text' => '',
431 'show_icon' => '', // 'yes' for show.
432 ];
433
434 $atts = shortcode_atts( $default_atts, $atts, $content );
435 $count_attr = apply_filters( 'rtsb/module/wishlist/counter_args', $atts );
436 if ( 'yes' === ( $count_attr['show_icon'] ?? '' ) ) {
437 $count_attr['icon_html'] = apply_filters( 'rtsb/module/wishlist/counter/icon_html', $icon_html, $count_attr );
438 } else {
439 $count_attr['icon_html'] = '';
440 }
441
442 return Fns::load_template( 'wishlist/counter', $count_attr, true );
443 }
444 }
445