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