PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.3.0
ShopBuilder – WooCommerce Builder For Elementor v2.3.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 2.3.0, at app/Modules/Compare/CompareFrontEnd.php

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