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