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

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

427 lines 12.2 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' ], 20 );
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 $params = [
279 'exists' => $exists,
280 'classes' => $classes,
281 'product_id' => $product->get_id(),
282 'parent_product_id' => $product_parent ?: $product->get_id(),
283 'product_type' => $product_type,
284 'button_text' => $button_text,
285 'show_button_text' => ! empty( Fns::get_option( 'modules', 'compare', 'button_text', '' ) ),
286 'left_text' => apply_filters( 'rtsb/module/compare/button_left_text', '' ),
287 'right_text' => apply_filters( 'rtsb/module/compare/button_right_text', '' ),
288 ];
289 $atts = apply_filters( 'rtsb/module/compare/button_params', $params );
290
291 $atts['icon_html'] = apply_filters( 'rtsb/module/compare/icon_html', $icon_html, $atts );
292
293 Fns::load_template( 'compare/button', $atts );
294 }
295
296
297 /**
298 * Print "Add to compare" button
299 *
300 * @return void
301 */
302 public function button_hook() {
303
304 if ( ! apply_filters( 'rtsb/module/compare/show_button', true ) ) {
305 return;
306 }
307 global $product;
308 if ( $product instanceof WC_Product ) {
309 do_action( 'rtsb/modules/compare/print_button', $product->get_id() );
310 }
311 }
312
313
314 /**
315 * List Shortcode callable function
316 *
317 * @param array $atts Shortcode attributes.
318 * @param string $content Shortcode content.
319 *
320 * @return string [HTML]
321 */
322 public function list_shortcode( $atts, $content = '' ) {
323 $this->handle = Fns::optimized_handle( $this->handle );
324
325 wp_enqueue_style( $this->handle );
326 wp_enqueue_script( $this->handle );
327
328 /* Fetch From option data */
329
330 $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
331
332 /* Product and Field */
333 $products = CompareFns::instance()->get_products_data();
334 $field_ids = array_merge( [ 'primary' ], CompareFns::instance()->get_list_field_ids() );
335
336 $return_to_shop_text = Fns::get_option( 'modules', 'compare', 'return_to_shop_text', esc_html__( 'Return to shop', 'shopbuilder' ) );
337
338 $default_atts = [
339 'compare' => self::instance(),
340 'products' => $products,
341 'field_ids' => $field_ids,
342 'return_to_shop_text' => $return_to_shop_text,
343 'empty_text' => ! empty( $empty_text ) ? $empty_text : '',
344 ];
345
346 $atts = shortcode_atts( $default_atts, $atts, $content );
347 $args = apply_filters( 'rtsb/module/compare/list_args', $atts );
348
349 return Fns::load_template( 'compare/list', $args, true );
350 }
351
352
353 /**
354 * Compare button Shortcode callable function
355 *
356 * @return string [HTML]
357 */
358 public function button_shortcode() {
359 $this->handle = Fns::optimized_handle( $this->handle );
360
361 wp_enqueue_style( $this->handle );
362 wp_enqueue_script( $this->handle );
363
364 global $product;
365
366 if ( ! $product instanceof WC_Product ) {
367 return '';
368 }
369 ob_start();
370
371 do_action( 'rtsb/modules/compare/print_button', $product->get_id() );
372
373 return ob_get_clean();
374 }
375
376 /**
377 * Compare counter Shortcode callable function
378 *
379 * @param array $atts Shortcode attributes.
380 * @param string $content Shortcode content.
381 *
382 * @return string [HTML]
383 */
384 public function counter_shortcode( $atts, $content = '' ) {
385 wp_enqueue_style( Fns::optimized_handle( $this->handle ) );
386
387 $product_ids = CompareFns::instance()->get_compared_product_ids();
388
389 $button_text = esc_html__( 'Compare', 'shopbuilder' );
390 $page_url = CompareFns::instance()->get_page_url();
391
392 $default_atts = [
393 'product_ids' => $product_ids,
394 'item_count' => count( $product_ids ),
395 'page_url' => $page_url,
396 'button_text' => $button_text,
397 'text' => '',
398 ];
399
400 $atts = shortcode_atts( $default_atts, $atts, $content );
401 $count_attr = apply_filters( 'rtsb/module/compare/counter_args', $atts );
402
403 return Fns::load_template( 'compare/counter', $count_attr, true );
404 }
405
406
407 /**
408 * Add specific body class when the Wishlist page is opened
409 *
410 * @param array $classes Existing boy classes.
411 *
412 * @return array
413 */
414 public function add_body_class( $classes ) {
415
416 $compare_page_id = Fns::get_option( 'modules', 'compare', 'page', 0, 'number' );
417
418 if ( ! empty( $compare_page_id ) && is_page( $compare_page_id ) ) {
419 $classes[] = 'rtsb-compare-page';
420 $classes[] = 'woocommerce';
421 $classes[] = 'woocommerce-page';
422 }
423
424 return $classes;
425 }
426 }
427