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

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