PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.4.2
ShopBuilder – WooCommerce Builder For Elementor v3.4.2
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 / Compare / CompareFrontEnd.php

CompareFrontEnd.php in ShopBuilder – WooCommerce Builder For Elementor 3.4.2, at app/Modules/Compare/CompareFrontEnd.php

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