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

429 lines 14.1 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 use SingletonTrait;
18
19 public function __construct() {
20
21 //Template
22 add_action( 'init', [ $this, 'attach_button' ] );
23 add_filter( 'body_class', [ $this, 'add_body_class' ] );
24
25 add_action( 'rtsb/modules/wishlist/frontend/display', [ $this, 'button_hook' ] );
26 // Add do_action('rtsb/modules/wishlist/frontend/display' ); for display anywhere.
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
37
38 public function enqueue() {
39 wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION );
40 wp_register_script( 'rtsb-wishlist', rtsb()->get_assets_uri( 'modules/wishlist.js' ), [
41 'jquery',
42 'rtsb-public',
43 ], RTSB_VERSION, true );
44 wp_enqueue_style( 'rtsb-modules' );
45 wp_enqueue_script( 'rtsb-wishlist' );
46 if ( is_user_logged_in() && is_account_page() && is_wc_endpoint_url( WishlistFns::WC_MENU_SLUG ) ) {
47 wp_enqueue_script('wc-add-to-cart');
48 wp_enqueue_script('wc-add-to-cart-variation');
49 }
50
51 $params = apply_filters( 'rtsb/module/wishlist/js_params', [
52 'product_id' => get_the_ID(),
53 'resturl' => get_rest_url(),
54 'isLoggedIn' => is_user_logged_in(),
55 'pageUrl' => wc_get_account_endpoint_url( 'wishlist' ),
56 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
57 'notice' => [
58 'add_to_wishlist' => esc_html__( 'Add to wishlist', 'shopbuilder' ),
59 'remove_wishlist' => esc_html__( 'Remove from list', 'shopbuilder' ),
60 'added' => Fns::get_option( 'modules', 'wishlist', 'notice_added_text', esc_html__( 'Product added!', 'shopbuilder' ) ),
61 'removed' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'Product removed!', 'shopbuilder' ) ),
62 'exist' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'The product is already in your wishlist!', 'shopbuilder' ) ),
63 'browse' => Fns::get_option( 'modules', 'wishlist', 'browse_list_text', esc_html__( 'Browse wishlist', 'shopbuilder' ) ),
64 'error' => esc_html__( 'Something went wrong!.', 'shopbuilder' ),
65 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ),
66 ],
67 ] );
68
69 wp_localize_script( 'rtsb-wishlist', 'rtsbWishlistParams', $params );
70 }
71
72 /**
73 * Add the "Add to Wishlist" button. Needed to use in wp_head hook.
74 *
75 * @return void
76 * @since 1.0.0
77 */
78 public function attach_button() {
79 $positions = apply_filters(
80 'rtsb/module/wishlist/product_btn_positions',
81 [
82 'before_cart_btn' => [
83 'hook' => 'woocommerce_before_add_to_cart_button',
84 'priority' => 20,
85 ],
86 'after_add_to_cart' => [
87 'hook' => 'woocommerce_single_product_summary',
88 'priority' => 31,
89 ],
90 'after_thumbnail' => [
91 'hook' => 'woocommerce_product_thumbnails',
92 'priority' => 21,
93 ],
94 'after_summary' => [
95 'hook' => 'woocommerce_after_single_product_summary',
96 'priority' => 11,
97 ],
98 'custom' => [
99 'hook' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_name', '' ),
100 'priority' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_priority', 10 ),
101 ],
102 ]
103 );
104
105 // Add the link "Add to wishlist".
106 $product_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_product_page', true, 'checkbox' );
107 $product_btn_position = Fns::get_option( 'modules', 'wishlist', 'product_btn_position', 'after_add_to_cart' );
108 if ( $product_btn_enable && 'shortcode' !== $product_btn_position && isset( $positions[ $product_btn_position ]['hook'] ) ) {
109 add_action( $positions[ $product_btn_position ]['hook'], [
110 $this,
111 'button_hook'
112 ], isset( $positions[ $product_btn_position ]['priority'] ) ? absint( $positions[ $product_btn_position ]['priority'] ) : '' );
113 }
114 // check if Add to wishlist button is enabled for loop.
115 $loop_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_on_loop', false, 'checkbox' );
116 if ( ! $loop_btn_enable ) {
117 return;
118 }
119
120 $positions = apply_filters(
121 'rtsb/module/wishlist/loop_btn_position',
122 [
123 'top_thumbnail' => [
124 'hook' => 'woocommerce_before_shop_loop_item',
125 'priority' => 5,
126 ],
127 'before_add_to_cart' => [
128 'hook' => 'woocommerce_after_shop_loop_item',
129 'priority' => 7,
130 ],
131 'after_add_to_cart' => [
132 'hook' => 'woocommerce_after_shop_loop_item',
133 'priority' => 15,
134 ],
135 'custom' => [
136 'hook' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_name', '' ),
137 'priority' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_priority', 10 ),
138 ],
139 ]
140 );
141
142 // Add the link "Add to wishlist" in the loop.
143 $shop_btn_position = Fns::get_option( 'modules', 'wishlist', 'loop_btn_position', 'after_add_to_cart' );
144
145 if ( 'shortcode' !== $shop_btn_position && isset( $positions[ $shop_btn_position ]['hook'] ) ) {
146 add_action( $positions[ $shop_btn_position ]['hook'], [
147 $this,
148 'button_hook'
149 ], isset( $positions[ $shop_btn_position ]['priority'] ) ? $positions[ $shop_btn_position ]['priority'] : '' );
150 }
151
152 // Add the link "Add to wishlist" for Gutenberg blocks.
153 add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 );
154
155
156 // rtsb/module/wishlist/show_button
157 }
158
159
160 /**
161 * Add ATW button to Products block item
162 *
163 * @param string $item_html HTML of the single block item.
164 * @param array $data Data used to render the item.
165 * @param WC_Product $product Current product.
166 *
167 * @return string Filtered HTML.
168 */
169 public function add_button_for_block( $item_html, $data, $product ) {
170 // Add the link "Add to wishlist" in the loop.
171 $shop_btn_position = Fns::get_option( 'modules', 'wishlist', 'shop_btn_position', 'after_add_to_cart' );
172 ob_start();
173 $this->print_button( $product->get_id() );
174 $button = ob_get_clean();
175 $parts = [];
176
177 preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts );
178
179 if ( ! $parts || count( $parts ) < 5 ) {
180 return $item_html;
181 }
182
183 // removes first match (entire match).
184 array_shift( $parts );
185
186 // removes empty parts.
187 $parts = array_filter( $parts );
188
189 // searches for index to cut parts array.
190 switch ( $shop_btn_position ) {
191 case 'top_thumbnail':
192 $index = 1;
193 break;
194 case 'before_add_to_cart':
195 $index = 2;
196 break;
197 case 'after_add_to_cart':
198 $index = 3;
199 break;
200 default:
201 $index = 0;
202 break;
203 }
204
205 // if index is found, stitch button in correct position.
206 if ( $index ) {
207 $first_set = array_slice( $parts, 0, $index );
208 $second_set = array_slice( $parts, $index );
209
210 $parts = array_merge(
211 $first_set,
212 (array) $button,
213 $second_set
214 );
215
216 // replace li classes.
217 $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 add-to-wishlist-' . $shop_btn_position . '"', $parts[0] );
218 }
219
220 // join all parts together and return item.
221 return implode( '', $parts );
222 }
223
224 /**
225 * Returns HTML for ATW button
226 *
227 * @param int $product_id Optional product id (if empty global product will be used instead by the shortcode).
228 *
229 * @return string HTML for ATW button.
230 */
231 public function get_button( $product_id = false ) {
232 $shortcode_tag = 'rtsb_wishlist_button';
233 $options = [];
234 $text_options = '';
235
236 if ( $product_id ) {
237 $options['product_id'] = $product_id;
238 }
239
240 if ( ! empty( $options ) ) {
241 foreach ( $options as $option_key => $option_value ) {
242 $text_options .= " $option_key=\"$option_value\"";
243 }
244 }
245
246 $shortcode = "[$shortcode_tag $text_options]";
247
248 return do_shortcode( $shortcode );
249 }
250
251
252 /**
253 * Print "Add to compare" button
254 *
255 * @return void
256 */
257 public function button_hook() {
258
259 if ( ! apply_filters( 'rtsb/module/wishlist/show_button', true ) ) {
260 return;
261 }
262 global $product;
263
264 do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() );
265 }
266
267 /**
268 * @return string|void
269 */
270 public function print_button( $product_id = 0 ) {
271
272 global $product;
273
274 // product object.
275 $current_product = $product_id ? wc_get_product( $product_id ) : false;
276 $current_product = $current_product ?: $product;
277
278 if ( ! $current_product instanceof WC_Product ) {
279 return '';
280 }
281
282 // product parent.
283 $current_product_parent = $current_product->get_parent_id();
284
285 // button class.
286 $classes = [];
287 // check if product is already in wishlist.
288 $exists = WishlistFns::instance()->is_exists_in_wishlist( $current_product->get_id() );
289 if ( $exists ) {
290 $classes[] = 'rtsb-wishlist-remove';
291 $button_text = Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Remove from list', 'shopbuilder' ) );
292 $button_text = apply_filters( 'rtsb/modules/wishlist/remove_from_list_button_text', $button_text );
293 $icon_html = '<i class="rtsb-icon rtsb-icon-heart"></i>';
294 } else {
295 $classes[] = 'rtsb-wishlist-add';
296 $icon_html = '<i class="rtsb-icon rtsb-icon-heart-empty"></i>';
297 // labels & icons settings.
298 $button_text = Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Add to wishlist', 'shopbuilder' ) );
299 // button text.
300 $button_text = apply_filters( 'rtsb/module/wishlist/add_to_list_button_txt', $button_text );
301 }
302
303 // get product type.
304 $product_type = $current_product->get_type();
305 $params = [
306 'exists' => $exists,
307 'classes' => $classes,
308 'product_id' => $current_product->get_id(),
309 'parent_product_id' => $current_product_parent ?: $current_product->get_id(),
310 'product_type' => $product_type,
311 'button_text' => $button_text,
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 do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() );
393 }
394 return ob_get_clean();
395 }
396
397 public function counter_shortcode( $atts, $content = '' ) {
398 wp_enqueue_style( 'rtsb-modules' );
399
400 $enable_login_limit = Fns::get_option( 'modules', 'wishlist', 'enable_login_limit', false, 'checkbox');
401
402 $myaccount_url = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );
403
404 $products = WishlistFns::instance()->get_wishlist_ids();
405 if ( ! is_user_logged_in() && $enable_login_limit ) {
406 $button_text = esc_html__( 'Please login', 'shopbuilder' );
407 $page_url = $myaccount_url;
408 } else {
409 $button_text = esc_html__( 'Wishlist', 'shopbuilder' );
410 $page_url = wc_get_account_endpoint_url( 'wishlist' );
411 }
412
413 $default_atts = [
414 'products' => $products,
415 'item_count' => count( $products ),
416 'page_url' => $page_url,
417 'button_text' => $button_text,
418 'enable_login_limit' => $enable_login_limit,
419 'text' => '',
420 ];
421
422 $atts = shortcode_atts( $default_atts, $atts, $content );
423 $count_attr = apply_filters( 'rtsb/module/wishlist/counter_args', $atts );
424
425 return Fns::load_template( 'wishlist/counter', $count_attr, true );
426 }
427
428 }
429