PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.4.0
ShopBuilder – WooCommerce Builder For Elementor v3.4.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
← All changes | app/Modules/Compare/CompareFrontEnd.php +78 -65 2.0.23.4.0 View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +/**
3 + * Compare Module Class
4 + *
5 + * @package RadiusTheme\SB
6 + */
2 7
3 8 namespace RadiusTheme\SB\Modules\Compare;
4 9
5 10 use RadiusTheme\SB\Helpers\Fns;
@@ -10,50 +15,48 @@
10 15 if ( ! defined( 'ABSPATH' ) ) {
11 16 exit( 'This script cannot be accessed directly.' );
12 17 }
13 18
14 -
19 +/**
20 + * Compare Module Class
21 + */
15 22 class CompareFrontEnd {
16 23 use SingletonTrait;
17 24
25 + /**
26 + * Asset Handle
27 + *
28 + * @var string
29 + */
30 + private $handle = 'rtsb-compare';
31 +
32 + /**
33 + * Class constructor.
34 + */
18 35 public function __construct() {
19 - // Template
20 -
21 36 add_filter( 'body_class', [ $this, 'add_body_class' ] );
22 -
23 - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );
24 -
37 + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ], 99 );
25 38 add_action( 'rtsb/modules/compare/frontend/display', [ $this, 'button_hook' ] );
26 - // Add do_action('rtsb/modules/compare/frontend/display' ); for display anywhere.
27 -
28 39 add_action( 'rtsb/modules/compare/print_button', [ $this, 'print_button' ] );
29 40
30 - // ShortCode
41 + // ShortCode.
31 42 add_shortcode( 'rtsb_compare_list', [ $this, 'list_shortcode' ] );
32 43 add_shortcode( 'rtsb_compare_button', [ $this, 'button_shortcode' ] );
33 44 add_shortcode( 'rtsb_compare_counter', [ $this, 'counter_shortcode' ] );
34 45
35 46 $this->attach_button();
36 -
37 47 }
38 48
39 -
49 + /**
50 + * Enqueue assets.
51 + *
52 + * @return void
53 + */
40 54 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(
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(
56 59 'rtsb/module/compare/js_params',
57 60 [
58 61 'product_id' => get_the_ID(),
59 62 'resturl' => get_rest_url(),
@@ -60,17 +63,19 @@
60 63 'isLoggedIn' => is_user_logged_in(),
61 64 'pageUrl' => CompareFns::instance()->get_page_url(),
62 65 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
63 66 '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' ),
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,
68 73 ],
69 74 ]
70 75 );
71 76
72 - wp_localize_script( 'rtsb-compare', 'rtsbCompareParams', $params );
77 + wp_localize_script( $this->handle, 'rtsbCompareParams', $params );
73 78 }
74 79
75 80 /**
76 81 * Add the "Add to Wishlist" button. Needed to use in wp_head hook.
@@ -98,9 +103,9 @@
98 103 'hook' => 'woocommerce_after_single_product_summary',
99 104 'priority' => 11,
100 105 ],
101 106
102 - 'custom' => [
107 + 'custom' => [
103 108 'hook' => Fns::get_option( 'modules', 'compare', 'product_custom_hook_name', '' ),
104 109 'priority' => Fns::get_option( 'modules', 'compare', 'product_custom_hook_priority', 10 ),
105 110 ],
106 111 ]
@@ -162,9 +167,8 @@
162 167 isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : ''
163 168 );
164 169 }
165 170
166 - // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility.
167 171 // Add the link "Add to wishlist" for Gutenberg blocks.
168 172 add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 );
169 173 }
170 174
@@ -171,10 +175,10 @@
171 175
172 176 /**
173 177 * Add ATW button to Products block item
174 178 *
175 - * @param string $item_html HTML of the single block item.
176 - * @param array $data Data used to render the item.
179 + * @param string $item_html HTML of the single block item.
180 + * @param array $data Data used to render the item.
177 181 * @param WC_Product $product Current product.
178 182 *
179 183 * @return string Filtered HTML.
180 184 */
@@ -232,32 +236,32 @@
232 236 }
233 237
234 238
235 239 /**
236 - * @return string|void
240 + * Print "Add to compare" button
241 + *
242 + * @param int $product_id Product ID.
243 + *
244 + * @return void
237 245 */
238 246 public function print_button( $product_id = 0 ) {
239 247 global $product;
240 248
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 '';
249 + if ( ! $product instanceof WC_Product && $product_id ) {
250 + $product = wc_get_product( $product_id );
247 251 }
248 252
249 253 // product parent.
250 - $current_product_parent = $current_product->get_parent_id();
254 + $product_parent = $product->get_parent_id();
251 255
252 256 // button class.
253 257 $classes = [];
254 258 // check if product is already in compare list.
255 259 $icon_html = '<i class="rtsb-icon rtsb-icon-arrows-cw"></i>';
256 - $exists = CompareFns::instance()->is_exists_in_list( $current_product->get_id() );
260 + $exists = CompareFns::instance()->is_exists_in_list( $product->get_id() );
257 261 if ( $exists ) {
258 262 $classes[] = 'rtsb-compare-remove';
259 - $button_text = Fns::get_option( 'modules', 'compare', 'button_text', esc_html__( 'Remove from list', 'shopbuilder' ) );
263 + $button_text = esc_html__( 'Remove from list', 'shopbuilder' );
260 264
261 265 $button_text = apply_filters( 'rtsb/modules/compare/remove_from_list_button_text', $button_text );
262 266 } else {
263 267 $classes[] = 'rtsb-compare-add';
@@ -267,18 +271,25 @@
267 271
268 272 // button text.
269 273 $button_text = apply_filters( 'rtsb/module/compare/add_to_list_button_txt', $button_text );
270 274 }
275 +
271 276 // get product type.
272 - $product_type = $current_product->get_type();
273 - $params = [
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 = [
274 285 'exists' => $exists,
275 286 'classes' => $classes,
276 - 'product_id' => $current_product->get_id(),
277 - 'parent_product_id' => $current_product_parent ?: $current_product->get_id(),
287 + 'product_id' => $product->get_id(),
288 + 'parent_product_id' => $product_parent ?: $product->get_id(),
278 289 'product_type' => $product_type,
279 290 'button_text' => $button_text,
280 - 'show_button_text' => false,
291 + 'show_button_text' => $show_button_text,
281 292 'left_text' => apply_filters( 'rtsb/module/compare/button_left_text', '' ),
282 293 'right_text' => apply_filters( 'rtsb/module/compare/button_right_text', '' ),
283 294 ];
284 295 $atts = apply_filters( 'rtsb/module/compare/button_params', $params );
@@ -285,9 +296,8 @@
285 296
286 297 $atts['icon_html'] = apply_filters( 'rtsb/module/compare/icon_html', $icon_html, $atts );
287 298
288 299 Fns::load_template( 'compare/button', $atts );
289 -
290 300 }
291 301
292 302
293 303 /**
@@ -309,17 +319,19 @@
309 319
310 320 /**
311 321 * List Shortcode callable function
312 322 *
313 - * @param array $atts
314 - * @param string $content
323 + * @param array $atts Shortcode attributes.
324 + * @param string $content Shortcode content.
315 325 *
316 326 * @return string [HTML]
317 327 */
318 328 public function list_shortcode( $atts, $content = '' ) {
319 - wp_enqueue_style( 'rtsb-modules' );
320 - wp_enqueue_script( 'rtsb-compare' );
329 + $this->handle = Fns::optimized_handle( $this->handle );
321 330
331 + wp_enqueue_style( $this->handle );
332 + wp_enqueue_script( $this->handle );
333 +
322 334 /* Fetch From option data */
323 335
324 336 $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
325 337
@@ -324,9 +336,9 @@
324 336 $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
325 337
326 338 /* Product and Field */
327 339 $products = CompareFns::instance()->get_products_data();
328 - $field_ids = [ 'primary' ] + CompareFns::instance()->get_list_field_ids();
340 + $field_ids = array_merge( [ 'primary' ], CompareFns::instance()->get_list_field_ids() );
329 341
330 342 $return_to_shop_text = Fns::get_option( 'modules', 'compare', 'return_to_shop_text', esc_html__( 'Return to shop', 'shopbuilder' ) );
331 343
332 344 $default_atts = [
@@ -346,17 +358,18 @@
346 358
347 359 /**
348 360 * Compare button Shortcode callable function
349 361 *
350 - * @param array $atts
351 - * @param string $content
352 - *
353 362 * @return string [HTML]
354 363 */
355 - public function button_shortcode( $atts, $content = '' ) {
356 - wp_enqueue_style( 'rtsb-modules' );
357 - wp_enqueue_script( 'rtsb-compare' );
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 +
358 370 global $product;
371 +
359 372 if ( ! $product instanceof WC_Product ) {
360 373 return '';
361 374 }
362 375 ob_start();
@@ -368,15 +381,15 @@
368 381
369 382 /**
370 383 * Compare counter Shortcode callable function
371 384 *
372 - * @param array $atts
373 - * @param string $content
385 + * @param array $atts Shortcode attributes.
386 + * @param string $content Shortcode content.
374 387 *
375 388 * @return string [HTML]
376 389 */
377 390 public function counter_shortcode( $atts, $content = '' ) {
378 - wp_enqueue_style( 'rtsb-modules' );
391 + wp_enqueue_style( Fns::optimized_handle( $this->handle ) );
379 392
380 393 $product_ids = CompareFns::instance()->get_compared_product_ids();
381 394
382 395 $button_text = esc_html__( 'Compare', 'shopbuilder' );