| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Wishlist Module Class | |
| 4 | + * | |
| 5 | + * @package RadiusTheme\SB | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace RadiusTheme\SB\Modules\WishList; |
| 4 | 9 | |
| 5 | 10 | use RadiusTheme\SB\Helpers\Fns; |
| @@ -10,8 +15,11 @@ | ||
| 10 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | 16 | exit( 'This script cannot be accessed directly.' ); |
| 12 | 17 | } |
| 13 | 18 | |
| 19 | +/** | |
| 20 | + * Wishlist Module Class | |
| 21 | + */ | |
| 14 | 22 | class WishlistFrontEnd { |
| 15 | 23 | |
| 16 | 24 | /** |
| 17 | 25 | * Singleton Instance |
| @@ -17,19 +25,27 @@ | ||
| 17 | 25 | * Singleton Instance |
| 18 | 26 | */ |
| 19 | 27 | use SingletonTrait; |
| 20 | 28 | |
| 29 | + /** | |
| 30 | + * Asset Handle | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + private $handle = 'rtsb-wishlist'; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Class constructor. | |
| 38 | + */ | |
| 21 | 39 | public function __construct() { |
| 22 | - // Template | |
| 23 | 40 | add_filter( 'body_class', [ $this, 'add_body_class' ] ); |
| 24 | 41 | add_filter( 'rtsb/module/wishlist/show_button', [ $this, 'wishlist_button_show_hide' ], 12 ); |
| 25 | - // Note:: Add do_action('rtsb/modules/wishlist/frontend/display' ); for display anywhere. | |
| 26 | 42 | add_action( 'rtsb/modules/wishlist/frontend/display', [ $this, 'button_hook' ] ); |
| 27 | 43 | |
| 28 | 44 | add_action( 'rtsb/modules/wishlist/print_button', [ $this, 'print_button' ] ); |
| 29 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 45 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ], 99 ); // 99 Is Working to load after main script. | |
| 30 | 46 | |
| 31 | - // ShortCode | |
| 47 | + // ShortCode. | |
| 32 | 48 | add_shortcode( 'rtsb_wishlist', [ $this, 'list_shortcode' ] ); |
| 33 | 49 | add_shortcode( 'rtsb_wishlist_button', [ $this, 'button_shortcode' ] ); |
| 34 | 50 | add_shortcode( 'rtsb_wishlist_counter', [ $this, 'counter_shortcode' ] ); |
| 35 | 51 | |
| @@ -56,21 +72,11 @@ | ||
| 56 | 72 | * |
| 57 | 73 | * @return void |
| 58 | 74 | */ |
| 59 | 75 | public function enqueue() { |
| 60 | - wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION ); | |
| 61 | - wp_register_script( | |
| 62 | - 'rtsb-wishlist', | |
| 63 | - rtsb()->get_assets_uri( 'modules/wishlist.js' ), | |
| 64 | - [ | |
| 65 | - 'jquery', | |
| 66 | - 'rtsb-public', | |
| 67 | - ], | |
| 68 | - RTSB_VERSION, | |
| 69 | - true | |
| 70 | - ); | |
| 71 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 72 | - wp_enqueue_script( 'rtsb-wishlist' ); | |
| 76 | + // Enqueue assets. | |
| 77 | + $this->handle = Fns::enqueue_module_assets( $this->handle, 'wishlist' ); | |
| 78 | + | |
| 73 | 79 | if ( is_user_logged_in() && is_account_page() ) { |
| 74 | 80 | wp_enqueue_script( 'wc-add-to-cart' ); |
| 75 | 81 | wp_enqueue_script( 'wc-add-to-cart-variation' ); |
| 76 | 82 | } |
| @@ -96,9 +102,9 @@ | ||
| 96 | 102 | ], |
| 97 | 103 | ] |
| 98 | 104 | ); |
| 99 | 105 | |
| 100 | - wp_localize_script( 'rtsb-wishlist', 'rtsbWishlistParams', $params ); | |
| 106 | + wp_localize_script( $this->handle, 'rtsbWishlistParams', $params ); | |
| 101 | 107 | } |
| 102 | 108 | |
| 103 | 109 | /** |
| 104 | 110 | * Add the "Add to Wishlist" button. Needed to use in wp_head hook. |
| @@ -176,9 +182,8 @@ | ||
| 176 | 182 | if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) { |
| 177 | 183 | add_action( $positions[ $loop_btn_position ]['hook'], [ $this, 'button_hook' ], isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' ); |
| 178 | 184 | } |
| 179 | 185 | |
| 180 | - // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility. | |
| 181 | 186 | // Add the link "Add to wishlist" for Gutenberg blocks. |
| 182 | 187 | add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); |
| 183 | 188 | } |
| 184 | 189 | |
| @@ -250,25 +255,26 @@ | ||
| 250 | 255 | * |
| 251 | 256 | * @return void |
| 252 | 257 | */ |
| 253 | 258 | public function button_hook() { |
| 254 | - | |
| 255 | 259 | if ( ! apply_filters( 'rtsb/module/wishlist/show_button', true ) ) { |
| 256 | 260 | return; |
| 257 | 261 | } |
| 258 | 262 | global $product; |
| 259 | - | |
| 263 | + if ( ! $product instanceof WC_Product ) { | |
| 264 | + return; | |
| 265 | + } | |
| 260 | 266 | do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() ); |
| 261 | 267 | } |
| 262 | 268 | |
| 263 | 269 | /** |
| 270 | + * Print "Add to compare" button | |
| 271 | + * | |
| 272 | + * @param int $product_id Product ID. | |
| 273 | + * | |
| 264 | 274 | * @return void |
| 265 | 275 | */ |
| 266 | 276 | public function print_button( $product_id = 0 ) { |
| 267 | - // Wishlist button will not show in ajax call for below codes. | |
| 268 | -// if ( ! apply_filters( 'rtsb/module/wishlist/show_button', true ) ) { | |
| 269 | -// return; | |
| 270 | -// } | |
| 271 | 277 | global $product; |
| 272 | 278 | |
| 273 | 279 | if ( ! $product instanceof WC_Product && $product_id ) { |
| 274 | 280 | $product = wc_get_product( $product_id ); |
| @@ -273,12 +279,8 @@ | ||
| 273 | 279 | if ( ! $product instanceof WC_Product && $product_id ) { |
| 274 | 280 | $product = wc_get_product( $product_id ); |
| 275 | 281 | } |
| 276 | 282 | |
| 277 | - // if ( ! $current_product instanceof WC_Product ) { | |
| 278 | - // return ''; | |
| 279 | - // } | |
| 280 | - | |
| 281 | 283 | // product parent. |
| 282 | 284 | $product_parent = $product->get_parent_id(); |
| 283 | 285 | |
| 284 | 286 | // button class. |
| @@ -307,9 +309,9 @@ | ||
| 307 | 309 | 'product_id' => $product->get_id(), |
| 308 | 310 | 'parent_product_id' => $product_parent ?: $product->get_id(), |
| 309 | 311 | 'product_type' => $product_type, |
| 310 | 312 | 'button_text' => $button_text, |
| 311 | - 'show_button_text' => false, | |
| 313 | + 'show_button_text' => ! empty( Fns::get_option( 'modules', 'wishlist', 'button_text', '' ) ), | |
| 312 | 314 | 'left_text' => apply_filters( 'rtsb/module/wishlist/button_left_text', '' ), |
| 313 | 315 | 'right_text' => apply_filters( 'rtsb/module/wishlist/button_right_text', '' ), |
| 314 | 316 | ]; |
| 315 | 317 | |
| @@ -344,17 +346,19 @@ | ||
| 344 | 346 | |
| 345 | 347 | /** |
| 346 | 348 | * Table List Shortcode callable function |
| 347 | 349 | * |
| 348 | - * @param array $atts | |
| 349 | - * @param string $content | |
| 350 | + * @param array $atts Shortcode attributes. | |
| 351 | + * @param string $content Shortcode content. | |
| 350 | 352 | * |
| 351 | 353 | * @return string [HTML] |
| 352 | 354 | */ |
| 353 | 355 | public function list_shortcode( $atts, $content = '' ) { |
| 354 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 355 | - wp_enqueue_script( 'rtsb-wishlist' ); | |
| 356 | + $this->handle = Fns::optimized_handle( $this->handle ); | |
| 356 | 357 | |
| 358 | + wp_enqueue_style( $this->handle ); | |
| 359 | + wp_enqueue_script( $this->handle ); | |
| 360 | + | |
| 357 | 361 | /* Fetch From option data */ |
| 358 | 362 | $empty_text = Fns::get_option( 'modules', 'wishlist', 'empty_table_text', esc_html__( 'No product found at your wishlist.', 'shopbuilder' ) ); |
| 359 | 363 | /* Product and Field */ |
| 360 | 364 | $products = WishlistFns::instance()->get_products_data(); |
| @@ -376,19 +380,19 @@ | ||
| 376 | 380 | |
| 377 | 381 | /** |
| 378 | 382 | * Wishlist button Shortcode callable function |
| 379 | 383 | * |
| 380 | - * @param array $atts | |
| 381 | - * @param string $content | |
| 382 | - * | |
| 383 | 384 | * @return string [HTML] |
| 384 | 385 | */ |
| 385 | - public function button_shortcode( $atts, $content = '' ) { | |
| 386 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 387 | - wp_enqueue_script( 'rtsb-wishlist' ); | |
| 386 | + public function button_shortcode() { | |
| 387 | + $this->handle = Fns::optimized_handle( $this->handle ); | |
| 388 | 388 | |
| 389 | + wp_enqueue_style( $this->handle ); | |
| 390 | + wp_enqueue_script( $this->handle ); | |
| 391 | + | |
| 389 | 392 | ob_start(); |
| 390 | 393 | global $product; |
| 394 | + | |
| 391 | 395 | if ( ! $product instanceof WC_Product ) { |
| 392 | 396 | return ''; |
| 393 | 397 | } |
| 394 | 398 | |
| @@ -396,24 +400,28 @@ | ||
| 396 | 400 | |
| 397 | 401 | return ob_get_clean(); |
| 398 | 402 | } |
| 399 | 403 | |
| 404 | + /** | |
| 405 | + * Counter Shortcode callable function | |
| 406 | + * | |
| 407 | + * @param array $atts Shortcode attributes. | |
| 408 | + * @param string $content Shortcode content. | |
| 409 | + * | |
| 410 | + * @return string [HTML] | |
| 411 | + */ | |
| 400 | 412 | public function counter_shortcode( $atts, $content = '' ) { |
| 401 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 413 | + wp_enqueue_style( Fns::optimized_handle( $this->handle ) ); | |
| 402 | 414 | |
| 403 | 415 | $enable_login_limit = Fns::get_option( 'modules', 'wishlist', 'enable_login_limit', false, 'checkbox' ); |
| 404 | - | |
| 405 | - $myaccount_url = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ); | |
| 406 | - | |
| 407 | - $products = WishlistFns::instance()->get_wishlist_ids(); | |
| 416 | + $products = WishlistFns::instance()->get_wishlist_ids(); | |
| 408 | 417 | if ( ! is_user_logged_in() && $enable_login_limit ) { |
| 409 | 418 | $button_text = esc_html__( 'Please login', 'shopbuilder' ); |
| 410 | - $page_url = $myaccount_url; | |
| 411 | 419 | } else { |
| 412 | 420 | $button_text = esc_html__( 'Wishlist', 'shopbuilder' ); |
| 413 | - $page_url = wc_get_account_endpoint_url( 'wishlist' ); | |
| 414 | 421 | } |
| 415 | - | |
| 422 | + $icon_html = '<i class="rtsb-icon rtsb-icon-heart"></i>'; | |
| 423 | + $page_url = WishlistFns::instance()->get_page_url(); | |
| 416 | 424 | $default_atts = [ |
| 417 | 425 | 'products' => $products, |
| 418 | 426 | 'item_count' => count( $products ), |
| 419 | 427 | 'page_url' => $page_url, |
| @@ -419,12 +427,18 @@ | ||
| 419 | 427 | 'page_url' => $page_url, |
| 420 | 428 | 'button_text' => $button_text, |
| 421 | 429 | 'enable_login_limit' => $enable_login_limit, |
| 422 | 430 | 'text' => '', |
| 431 | + 'show_icon' => '', // 'yes' for show. | |
| 423 | 432 | ]; |
| 424 | 433 | |
| 425 | 434 | $atts = shortcode_atts( $default_atts, $atts, $content ); |
| 426 | 435 | $count_attr = apply_filters( 'rtsb/module/wishlist/counter_args', $atts ); |
| 436 | + if ( 'yes' === ( $count_attr['show_icon'] ?? '' ) ) { | |
| 437 | + $count_attr['icon_html'] = apply_filters( 'rtsb/module/wishlist/counter/icon_html', $icon_html, $count_attr ); | |
| 438 | + } else { | |
| 439 | + $count_attr['icon_html'] = ''; | |
| 440 | + } | |
| 427 | 441 | |
| 428 | 442 | return Fns::load_template( 'wishlist/counter', $count_attr, true ); |
| 429 | 443 | } |
| 430 | 444 | } |