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

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