PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.10
ShopBuilder – WooCommerce Builder For Elementor v2.1.10
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 2.1.10, at app/Modules/WishList/WishlistFrontEnd.php

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