PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.1
ShopBuilder – WooCommerce Builder For Elementor v3.2.1
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 +71 -64 2.0.33.2.1 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,19 @@
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();
277 + $product_type = $product->get_type();
273 278 $params = [
274 279 'exists' => $exists,
275 280 'classes' => $classes,
276 - 'product_id' => $current_product->get_id(),
277 - 'parent_product_id' => $current_product_parent ?: $current_product->get_id(),
281 + 'product_id' => $product->get_id(),
282 + 'parent_product_id' => $product_parent ?: $product->get_id(),
278 283 'product_type' => $product_type,
279 284 'button_text' => $button_text,
280 - 'show_button_text' => false,
285 + 'show_button_text' => ! empty( Fns::get_option( 'modules', 'compare', 'button_text', '' ) ),
281 286 'left_text' => apply_filters( 'rtsb/module/compare/button_left_text', '' ),
282 287 'right_text' => apply_filters( 'rtsb/module/compare/button_right_text', '' ),
283 288 ];
284 289 $atts = apply_filters( 'rtsb/module/compare/button_params', $params );
@@ -285,9 +290,8 @@
285 290
286 291 $atts['icon_html'] = apply_filters( 'rtsb/module/compare/icon_html', $icon_html, $atts );
287 292
288 293 Fns::load_template( 'compare/button', $atts );
289 -
290 294 }
291 295
292 296
293 297 /**
@@ -309,17 +313,19 @@
309 313
310 314 /**
311 315 * List Shortcode callable function
312 316 *
313 - * @param array $atts
314 - * @param string $content
317 + * @param array $atts Shortcode attributes.
318 + * @param string $content Shortcode content.
315 319 *
316 320 * @return string [HTML]
317 321 */
318 322 public function list_shortcode( $atts, $content = '' ) {
319 - wp_enqueue_style( 'rtsb-modules' );
320 - wp_enqueue_script( 'rtsb-compare' );
323 + $this->handle = Fns::optimized_handle( $this->handle );
321 324
325 + wp_enqueue_style( $this->handle );
326 + wp_enqueue_script( $this->handle );
327 +
322 328 /* Fetch From option data */
323 329
324 330 $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
325 331
@@ -324,9 +330,9 @@
324 330 $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
325 331
326 332 /* Product and Field */
327 333 $products = CompareFns::instance()->get_products_data();
328 - $field_ids = [ 'primary' ] + CompareFns::instance()->get_list_field_ids();
334 + $field_ids = array_merge( [ 'primary' ], CompareFns::instance()->get_list_field_ids() );
329 335
330 336 $return_to_shop_text = Fns::get_option( 'modules', 'compare', 'return_to_shop_text', esc_html__( 'Return to shop', 'shopbuilder' ) );
331 337
332 338 $default_atts = [
@@ -346,17 +352,18 @@
346 352
347 353 /**
348 354 * Compare button Shortcode callable function
349 355 *
350 - * @param array $atts
351 - * @param string $content
352 - *
353 356 * @return string [HTML]
354 357 */
355 - public function button_shortcode( $atts, $content = '' ) {
356 - wp_enqueue_style( 'rtsb-modules' );
357 - wp_enqueue_script( 'rtsb-compare' );
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 +
358 364 global $product;
365 +
359 366 if ( ! $product instanceof WC_Product ) {
360 367 return '';
361 368 }
362 369 ob_start();
@@ -368,15 +375,15 @@
368 375
369 376 /**
370 377 * Compare counter Shortcode callable function
371 378 *
372 - * @param array $atts
373 - * @param string $content
379 + * @param array $atts Shortcode attributes.
380 + * @param string $content Shortcode content.
374 381 *
375 382 * @return string [HTML]
376 383 */
377 384 public function counter_shortcode( $atts, $content = '' ) {
378 - wp_enqueue_style( 'rtsb-modules' );
385 + wp_enqueue_style( Fns::optimized_handle( $this->handle ) );
379 386
380 387 $product_ids = CompareFns::instance()->get_compared_product_ids();
381 388
382 389 $button_text = esc_html__( 'Compare', 'shopbuilder' );