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

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