| @@ -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,25 +15,31 @@ | ||
| 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 | |
| @@ -34,23 +45,16 @@ | ||
| 34 | 45 | |
| 35 | 46 | $this->attach_button(); |
| 36 | 47 | } |
| 37 | 48 | |
| 38 | - | |
| 49 | + /** | |
| 50 | + * Enqueue assets. | |
| 51 | + * | |
| 52 | + * @return void | |
| 53 | + */ | |
| 39 | 54 | 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' ); | |
| 55 | + // Enqueue assets. | |
| 56 | + $this->handle = Fns::enqueue_module_assets( $this->handle, 'compare' ); | |
| 53 | 57 | $ajax_button_text = Fns::get_option( 'modules', 'compare', 'button_text', esc_html__( 'Compare', 'shopbuilder' ) ); |
| 54 | 58 | $params = apply_filters( |
| 55 | 59 | 'rtsb/module/compare/js_params', |
| 56 | 60 | [ |
| @@ -69,9 +73,9 @@ | ||
| 69 | 73 | ], |
| 70 | 74 | ] |
| 71 | 75 | ); |
| 72 | 76 | |
| 73 | - wp_localize_script( 'rtsb-compare', 'rtsbCompareParams', $params ); | |
| 77 | + wp_localize_script( $this->handle, 'rtsbCompareParams', $params ); | |
| 74 | 78 | } |
| 75 | 79 | |
| 76 | 80 | /** |
| 77 | 81 | * Add the "Add to Wishlist" button. Needed to use in wp_head hook. |
| @@ -163,9 +167,8 @@ | ||
| 163 | 167 | isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' |
| 164 | 168 | ); |
| 165 | 169 | } |
| 166 | 170 | |
| 167 | - // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility. | |
| 168 | 171 | // Add the link "Add to wishlist" for Gutenberg blocks. |
| 169 | 172 | add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); |
| 170 | 173 | } |
| 171 | 174 | |
| @@ -233,9 +236,13 @@ | ||
| 233 | 236 | } |
| 234 | 237 | |
| 235 | 238 | |
| 236 | 239 | /** |
| 237 | - * @return string|void | |
| 240 | + * Print "Add to compare" button | |
| 241 | + * | |
| 242 | + * @param int $product_id Product ID. | |
| 243 | + * | |
| 244 | + * @return void | |
| 238 | 245 | */ |
| 239 | 246 | public function print_button( $product_id = 0 ) { |
| 240 | 247 | global $product; |
| 241 | 248 | |
| @@ -242,12 +249,8 @@ | ||
| 242 | 249 | if ( ! $product instanceof WC_Product && $product_id ) { |
| 243 | 250 | $product = wc_get_product( $product_id ); |
| 244 | 251 | } |
| 245 | 252 | |
| 246 | - // if ( ! $current_product instanceof WC_Product ) { | |
| 247 | - // return ''; | |
| 248 | - // } | |
| 249 | - | |
| 250 | 253 | // product parent. |
| 251 | 254 | $product_parent = $product->get_parent_id(); |
| 252 | 255 | |
| 253 | 256 | // button class. |
| @@ -271,9 +274,15 @@ | ||
| 271 | 274 | } |
| 272 | 275 | |
| 273 | 276 | // get product type. |
| 274 | 277 | $product_type = $product->get_type(); |
| 275 | - $params = [ | |
| 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 = [ | |
| 276 | 285 | 'exists' => $exists, |
| 277 | 286 | 'classes' => $classes, |
| 278 | 287 | 'product_id' => $product->get_id(), |
| 279 | 288 | 'parent_product_id' => $product_parent ?: $product->get_id(), |
| @@ -278,9 +287,9 @@ | ||
| 278 | 287 | 'product_id' => $product->get_id(), |
| 279 | 288 | 'parent_product_id' => $product_parent ?: $product->get_id(), |
| 280 | 289 | 'product_type' => $product_type, |
| 281 | 290 | 'button_text' => $button_text, |
| 282 | - 'show_button_text' => false, | |
| 291 | + 'show_button_text' => $show_button_text, | |
| 283 | 292 | 'left_text' => apply_filters( 'rtsb/module/compare/button_left_text', '' ), |
| 284 | 293 | 'right_text' => apply_filters( 'rtsb/module/compare/button_right_text', '' ), |
| 285 | 294 | ]; |
| 286 | 295 | $atts = apply_filters( 'rtsb/module/compare/button_params', $params ); |
| @@ -310,17 +319,19 @@ | ||
| 310 | 319 | |
| 311 | 320 | /** |
| 312 | 321 | * List Shortcode callable function |
| 313 | 322 | * |
| 314 | - * @param array $atts | |
| 315 | - * @param string $content | |
| 323 | + * @param array $atts Shortcode attributes. | |
| 324 | + * @param string $content Shortcode content. | |
| 316 | 325 | * |
| 317 | 326 | * @return string [HTML] |
| 318 | 327 | */ |
| 319 | 328 | public function list_shortcode( $atts, $content = '' ) { |
| 320 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 321 | - wp_enqueue_script( 'rtsb-compare' ); | |
| 329 | + $this->handle = Fns::optimized_handle( $this->handle ); | |
| 322 | 330 | |
| 331 | + wp_enqueue_style( $this->handle ); | |
| 332 | + wp_enqueue_script( $this->handle ); | |
| 333 | + | |
| 323 | 334 | /* Fetch From option data */ |
| 324 | 335 | |
| 325 | 336 | $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) ); |
| 326 | 337 | |
| @@ -347,17 +358,18 @@ | ||
| 347 | 358 | |
| 348 | 359 | /** |
| 349 | 360 | * Compare button Shortcode callable function |
| 350 | 361 | * |
| 351 | - * @param array $atts | |
| 352 | - * @param string $content | |
| 353 | - * | |
| 354 | 362 | * @return string [HTML] |
| 355 | 363 | */ |
| 356 | - public function button_shortcode( $atts, $content = '' ) { | |
| 357 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 358 | - 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 | + | |
| 359 | 370 | global $product; |
| 371 | + | |
| 360 | 372 | if ( ! $product instanceof WC_Product ) { |
| 361 | 373 | return ''; |
| 362 | 374 | } |
| 363 | 375 | ob_start(); |
| @@ -369,15 +381,15 @@ | ||
| 369 | 381 | |
| 370 | 382 | /** |
| 371 | 383 | * Compare counter Shortcode callable function |
| 372 | 384 | * |
| 373 | - * @param array $atts | |
| 374 | - * @param string $content | |
| 385 | + * @param array $atts Shortcode attributes. | |
| 386 | + * @param string $content Shortcode content. | |
| 375 | 387 | * |
| 376 | 388 | * @return string [HTML] |
| 377 | 389 | */ |
| 378 | 390 | public function counter_shortcode( $atts, $content = '' ) { |
| 379 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 391 | + wp_enqueue_style( Fns::optimized_handle( $this->handle ) ); | |
| 380 | 392 | |
| 381 | 393 | $product_ids = CompareFns::instance()->get_compared_product_ids(); |
| 382 | 394 | |
| 383 | 395 | $button_text = esc_html__( 'Compare', 'shopbuilder' ); |