| @@ -1,430 +1,431 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace RadiusTheme\SB\Modules\WishList; | |
| 4 | - | |
| 5 | -use RadiusTheme\SB\Helpers\Fns; | |
| 6 | -use RadiusTheme\SB\Traits\SingletonTrait; | |
| 7 | -use WC_Product; | |
| 8 | - | |
| 9 | -// Do not allow directly accessing this file. | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit( 'This script cannot be accessed directly.' ); | |
| 12 | -} | |
| 13 | - | |
| 14 | -class WishlistFrontEnd { | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * Singleton Instance | |
| 18 | - */ | |
| 19 | - use SingletonTrait; | |
| 20 | - | |
| 21 | - public function __construct() { | |
| 22 | - // Template | |
| 23 | - add_filter( 'body_class', [ $this, 'add_body_class' ] ); | |
| 24 | - 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 | - add_action( 'rtsb/modules/wishlist/frontend/display', [ $this, 'button_hook' ] ); | |
| 27 | - | |
| 28 | - add_action( 'rtsb/modules/wishlist/print_button', [ $this, 'print_button' ] ); | |
| 29 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 30 | - | |
| 31 | - // ShortCode | |
| 32 | - add_shortcode( 'rtsb_wishlist', [ $this, 'list_shortcode' ] ); | |
| 33 | - add_shortcode( 'rtsb_wishlist_button', [ $this, 'button_shortcode' ] ); | |
| 34 | - add_shortcode( 'rtsb_wishlist_counter', [ $this, 'counter_shortcode' ] ); | |
| 35 | - | |
| 36 | - $this->attach_button(); | |
| 37 | - } | |
| 38 | - | |
| 39 | - /** | |
| 40 | - * Hide Wishlist button. | |
| 41 | - * | |
| 42 | - * @param boolean $is_enable Wishlist Permission. | |
| 43 | - * @return boolean | |
| 44 | - */ | |
| 45 | - public function wishlist_button_show_hide( $is_enable ) { | |
| 46 | - $enable_login_limit = Fns::get_option( 'modules', 'wishlist', 'enable_login_limit', false, 'checkbox' ); | |
| 47 | - $hide_for_non_logged_in = Fns::get_option( 'modules', 'wishlist', 'hide_wishlist_non_logged_in', false, 'checkbox' ); | |
| 48 | - if ( $enable_login_limit && $hide_for_non_logged_in && ! is_user_logged_in() ) { | |
| 49 | - $is_enable = false; | |
| 50 | - } | |
| 51 | - return $is_enable; | |
| 52 | - } | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Script. | |
| 56 | - * | |
| 57 | - * @return void | |
| 58 | - */ | |
| 59 | - 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' ); | |
| 73 | - if ( is_user_logged_in() && is_account_page() ) { | |
| 74 | - wp_enqueue_script( 'wc-add-to-cart' ); | |
| 75 | - wp_enqueue_script( 'wc-add-to-cart-variation' ); | |
| 76 | - } | |
| 77 | - | |
| 78 | - $params = apply_filters( | |
| 79 | - 'rtsb/module/wishlist/js_params', | |
| 80 | - [ | |
| 81 | - 'product_id' => get_the_ID(), | |
| 82 | - 'resturl' => get_rest_url(), | |
| 83 | - 'isLoggedIn' => is_user_logged_in(), | |
| 84 | - 'pageUrl' => WishlistFns::instance()->get_page_url(), | |
| 85 | - 'rest_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 86 | - 'notice' => [ | |
| 87 | - 'add_to_wishlist' => esc_html__( 'Add to wishlist', 'shopbuilder' ), | |
| 88 | - 'remove_wishlist' => esc_html__( 'Remove from list', 'shopbuilder' ), | |
| 89 | - 'button_text' => Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Add to wishlist', 'shopbuilder' ) ), | |
| 90 | - 'added' => Fns::get_option( 'modules', 'wishlist', 'notice_added_text', esc_html__( 'Product added!', 'shopbuilder' ) ), | |
| 91 | - 'removed' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'Product removed!', 'shopbuilder' ) ), | |
| 92 | - 'exist' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'The product is already in your wishlist!', 'shopbuilder' ) ), | |
| 93 | - 'browse' => Fns::get_option( 'modules', 'wishlist', 'browse_list_text', esc_html__( 'Browse wishlist', 'shopbuilder' ) ), | |
| 94 | - 'error' => esc_html__( 'Something went wrong!.', 'shopbuilder' ), | |
| 95 | - 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ), | |
| 96 | - ], | |
| 97 | - ] | |
| 98 | - ); | |
| 99 | - | |
| 100 | - wp_localize_script( 'rtsb-wishlist', 'rtsbWishlistParams', $params ); | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * Add the "Add to Wishlist" button. Needed to use in wp_head hook. | |
| 105 | - * | |
| 106 | - * @return void | |
| 107 | - * @since 1.0.0 | |
| 108 | - */ | |
| 109 | - public function attach_button() { | |
| 110 | - $positions = apply_filters( | |
| 111 | - 'rtsb/module/wishlist/product_btn_positions', | |
| 112 | - [ | |
| 113 | - 'before_cart_btn' => [ | |
| 114 | - 'hook' => 'woocommerce_before_add_to_cart_button', | |
| 115 | - 'priority' => 20, | |
| 116 | - ], | |
| 117 | - 'after_add_to_cart' => [ | |
| 118 | - 'hook' => 'woocommerce_single_product_summary', | |
| 119 | - 'priority' => 31, | |
| 120 | - ], | |
| 121 | - 'after_thumbnail' => [ | |
| 122 | - 'hook' => 'woocommerce_product_thumbnails', | |
| 123 | - 'priority' => 21, | |
| 124 | - ], | |
| 125 | - 'after_summary' => [ | |
| 126 | - 'hook' => 'woocommerce_after_single_product_summary', | |
| 127 | - 'priority' => 11, | |
| 128 | - ], | |
| 129 | - 'custom' => [ | |
| 130 | - 'hook' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_name', '' ), | |
| 131 | - 'priority' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_priority', 10 ), | |
| 132 | - ], | |
| 133 | - ] | |
| 134 | - ); | |
| 135 | - | |
| 136 | - // Add the link "Add to wishlist". | |
| 137 | - $product_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_product_page', true, 'checkbox' ); | |
| 138 | - $product_btn_position = Fns::get_option( 'modules', 'wishlist', 'product_btn_position', 'after_add_to_cart' ); | |
| 139 | - if ( $product_btn_enable && 'shortcode' !== $product_btn_position && isset( $positions[ $product_btn_position ]['hook'] ) ) { | |
| 140 | - add_action( | |
| 141 | - $positions[ $product_btn_position ]['hook'], | |
| 142 | - [ | |
| 143 | - $this, | |
| 144 | - 'button_hook', | |
| 145 | - ], | |
| 146 | - isset( $positions[ $product_btn_position ]['priority'] ) ? absint( $positions[ $product_btn_position ]['priority'] ) : '' | |
| 147 | - ); | |
| 148 | - } | |
| 149 | - // check if Add to wishlist button is enabled for loop. | |
| 150 | - $loop_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_on_loop', false, 'checkbox' ); | |
| 151 | - if ( ! $loop_btn_enable ) { | |
| 152 | - return; | |
| 153 | - } | |
| 154 | - | |
| 155 | - $positions = apply_filters( | |
| 156 | - 'rtsb/module/wishlist/loop_btn_position', | |
| 157 | - [ | |
| 158 | - 'before_add_to_cart' => [ | |
| 159 | - 'hook' => 'woocommerce_after_shop_loop_item', | |
| 160 | - 'priority' => 7, | |
| 161 | - ], | |
| 162 | - 'after_add_to_cart' => [ | |
| 163 | - 'hook' => 'woocommerce_after_shop_loop_item', | |
| 164 | - 'priority' => 15, | |
| 165 | - ], | |
| 166 | - 'custom' => [ | |
| 167 | - 'hook' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_name', '' ), | |
| 168 | - 'priority' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_priority', 10 ), | |
| 169 | - ], | |
| 170 | - ] | |
| 171 | - ); | |
| 172 | - | |
| 173 | - // Add the link "Add to wishlist" in the loop. | |
| 174 | - $loop_btn_position = Fns::get_option( 'modules', 'wishlist', 'loop_btn_position', 'after_add_to_cart' ); | |
| 175 | - | |
| 176 | - if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) { | |
| 177 | - add_action( $positions[ $loop_btn_position ]['hook'], [ $this, 'button_hook' ], isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' ); | |
| 178 | - } | |
| 179 | - | |
| 180 | - // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility. | |
| 181 | - // Add the link "Add to wishlist" for Gutenberg blocks. | |
| 182 | - add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); | |
| 183 | - } | |
| 184 | - | |
| 185 | - | |
| 186 | - /** | |
| 187 | - * Add ATW button to Products block item | |
| 188 | - * | |
| 189 | - * @param string $item_html HTML of the single block item. | |
| 190 | - * @param array $data Data used to render the item. | |
| 191 | - * @param WC_Product $product Current product. | |
| 192 | - * | |
| 193 | - * @return string Filtered HTML. | |
| 194 | - */ | |
| 195 | - public function add_button_for_block( $item_html, $data, $product ) { | |
| 196 | - // Add the link "Add to wishlist" in the loop. | |
| 197 | - $loop_btn_position = Fns::get_option( 'modules', 'wishlist', 'shop_btn_position', 'after_add_to_cart' ); | |
| 198 | - ob_start(); | |
| 199 | - $this->print_button( $product->get_id() ); | |
| 200 | - $button = ob_get_clean(); | |
| 201 | - $parts = []; | |
| 202 | - | |
| 203 | - preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts ); | |
| 204 | - | |
| 205 | - if ( ! $parts || count( $parts ) < 5 ) { | |
| 206 | - return $item_html; | |
| 207 | - } | |
| 208 | - | |
| 209 | - // removes first match (entire match). | |
| 210 | - array_shift( $parts ); | |
| 211 | - | |
| 212 | - // removes empty parts. | |
| 213 | - $parts = array_filter( $parts ); | |
| 214 | - | |
| 215 | - // searches for index to cut parts array. | |
| 216 | - switch ( $loop_btn_position ) { | |
| 217 | - | |
| 218 | - case 'before_add_to_cart': | |
| 219 | - $index = 2; | |
| 220 | - break; | |
| 221 | - case 'after_add_to_cart': | |
| 222 | - $index = 3; | |
| 223 | - break; | |
| 224 | - default: | |
| 225 | - $index = 0; | |
| 226 | - break; | |
| 227 | - } | |
| 228 | - | |
| 229 | - // if index is found, stitch button in correct position. | |
| 230 | - if ( $index ) { | |
| 231 | - $first_set = array_slice( $parts, 0, $index ); | |
| 232 | - $second_set = array_slice( $parts, $index ); | |
| 233 | - | |
| 234 | - $parts = array_merge( | |
| 235 | - $first_set, | |
| 236 | - (array) $button, | |
| 237 | - $second_set | |
| 238 | - ); | |
| 239 | - | |
| 240 | - // replace li classes. | |
| 241 | - $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 add-to-wishlist-' . $loop_btn_position . '"', $parts[0] ); | |
| 242 | - } | |
| 243 | - | |
| 244 | - // join all parts together and return item. | |
| 245 | - return implode( '', $parts ); | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * Print "Add to compare" button | |
| 250 | - * | |
| 251 | - * @return void | |
| 252 | - */ | |
| 253 | - public function button_hook() { | |
| 254 | - | |
| 255 | - if ( ! apply_filters( 'rtsb/module/wishlist/show_button', true ) ) { | |
| 256 | - return; | |
| 257 | - } | |
| 258 | - global $product; | |
| 259 | - | |
| 260 | - do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() ); | |
| 261 | - } | |
| 262 | - | |
| 263 | - /** | |
| 264 | - * @return void | |
| 265 | - */ | |
| 266 | - 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 | - global $product; | |
| 272 | - | |
| 273 | - if ( ! $product instanceof WC_Product && $product_id ) { | |
| 274 | - $product = wc_get_product( $product_id ); | |
| 275 | - } | |
| 276 | - | |
| 277 | - // if ( ! $current_product instanceof WC_Product ) { | |
| 278 | - // return ''; | |
| 279 | - // } | |
| 280 | - | |
| 281 | - // product parent. | |
| 282 | - $product_parent = $product->get_parent_id(); | |
| 283 | - | |
| 284 | - // button class. | |
| 285 | - $classes = []; | |
| 286 | - // check if product is already in wishlist. | |
| 287 | - $exists = WishlistFns::instance()->is_exists_in_wishlist( $product->get_id() ); | |
| 288 | - if ( $exists ) { | |
| 289 | - $classes[] = 'rtsb-wishlist-remove'; | |
| 290 | - $button_text = esc_html__( 'Remove from list', 'shopbuilder' ); | |
| 291 | - $button_text = apply_filters( 'rtsb/modules/wishlist/remove_from_list_button_text', $button_text ); | |
| 292 | - $icon_html = '<i class="rtsb-icon rtsb-icon-heart"></i>'; | |
| 293 | - } else { | |
| 294 | - $classes[] = 'rtsb-wishlist-add'; | |
| 295 | - $icon_html = '<i class="rtsb-icon rtsb-icon-heart-empty"></i>'; | |
| 296 | - // labels & icons settings. | |
| 297 | - $button_text = Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Add to wishlist', 'shopbuilder' ) ); | |
| 298 | - // button text. | |
| 299 | - $button_text = apply_filters( 'rtsb/module/wishlist/add_to_list_button_txt', $button_text ); | |
| 300 | - } | |
| 301 | - | |
| 302 | - // get product type. | |
| 303 | - $product_type = $product->get_type(); | |
| 304 | - $params = [ | |
| 305 | - 'exists' => $exists, | |
| 306 | - 'classes' => $classes, | |
| 307 | - 'product_id' => $product->get_id(), | |
| 308 | - 'parent_product_id' => $product_parent ?: $product->get_id(), | |
| 309 | - 'product_type' => $product_type, | |
| 310 | - 'button_text' => $button_text, | |
| 311 | - 'show_button_text' => false, | |
| 312 | - 'left_text' => apply_filters( 'rtsb/module/wishlist/button_left_text', '' ), | |
| 313 | - 'right_text' => apply_filters( 'rtsb/module/wishlist/button_right_text', '' ), | |
| 314 | - ]; | |
| 315 | - | |
| 316 | - // let third party developer filter options. | |
| 317 | - $atts = apply_filters( 'rtsb/module/wishlist/button_params', $params ); | |
| 318 | - | |
| 319 | - // set fragment options. | |
| 320 | - $atts['icon_html'] = apply_filters( 'rtsb/module/wishlist/icon_html', $icon_html, $atts ); | |
| 321 | - | |
| 322 | - Fns::load_template( 'wishlist/button', $atts ); | |
| 323 | - } | |
| 324 | - | |
| 325 | - | |
| 326 | - /** | |
| 327 | - * Add specific body class when the Wishlist page is opened | |
| 328 | - * | |
| 329 | - * @param array $classes Existing boy classes. | |
| 330 | - * | |
| 331 | - * @return array | |
| 332 | - */ | |
| 333 | - public function add_body_class( $classes ) { | |
| 334 | - $wishlist_page_id = Fns::get_option( 'modules', 'wishlist', 'page', '', 'number' ); | |
| 335 | - if ( ! empty( $wishlist_page_id ) && is_page( $wishlist_page_id ) ) { | |
| 336 | - $classes[] = 'woocommerce-wishlist rtsb-wishlist-page'; | |
| 337 | - $classes[] = 'woocommerce'; | |
| 338 | - $classes[] = 'woocommerce-page'; | |
| 339 | - } | |
| 340 | - | |
| 341 | - return $classes; | |
| 342 | - } | |
| 343 | - | |
| 344 | - | |
| 345 | - /** | |
| 346 | - * Table List Shortcode callable function | |
| 347 | - * | |
| 348 | - * @param array $atts | |
| 349 | - * @param string $content | |
| 350 | - * | |
| 351 | - * @return string [HTML] | |
| 352 | - */ | |
| 353 | - public function list_shortcode( $atts, $content = '' ) { | |
| 354 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 355 | - wp_enqueue_script( 'rtsb-wishlist' ); | |
| 356 | - | |
| 357 | - /* Fetch From option data */ | |
| 358 | - $empty_text = Fns::get_option( 'modules', 'wishlist', 'empty_table_text', esc_html__( 'No product found at your wishlist.', 'shopbuilder' ) ); | |
| 359 | - /* Product and Field */ | |
| 360 | - $products = WishlistFns::instance()->get_products_data(); | |
| 361 | - $field_ids = WishlistFns::instance()->get_field_ids(); | |
| 362 | - | |
| 363 | - $custom_headings = []; | |
| 364 | - | |
| 365 | - $default_atts = [ | |
| 366 | - 'products' => $products, | |
| 367 | - 'field_ids' => $field_ids, | |
| 368 | - 'empty_text' => ! empty( $empty_text ) ? $empty_text : '', | |
| 369 | - ]; | |
| 370 | - | |
| 371 | - $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 372 | - $args = apply_filters( 'rtsb/module/wishlist/list_args', $atts ); | |
| 373 | - | |
| 374 | - return Fns::load_template( 'wishlist/list', $args, true ); | |
| 375 | - } | |
| 376 | - | |
| 377 | - /** | |
| 378 | - * Wishlist button Shortcode callable function | |
| 379 | - * | |
| 380 | - * @param array $atts | |
| 381 | - * @param string $content | |
| 382 | - * | |
| 383 | - * @return string [HTML] | |
| 384 | - */ | |
| 385 | - public function button_shortcode( $atts, $content = '' ) { | |
| 386 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 387 | - wp_enqueue_script( 'rtsb-wishlist' ); | |
| 388 | - | |
| 389 | - ob_start(); | |
| 390 | - global $product; | |
| 391 | - if ( ! $product instanceof WC_Product ) { | |
| 392 | - return ''; | |
| 393 | - } | |
| 394 | - | |
| 395 | - do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() ); | |
| 396 | - | |
| 397 | - return ob_get_clean(); | |
| 398 | - } | |
| 399 | - | |
| 400 | - public function counter_shortcode( $atts, $content = '' ) { | |
| 401 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 402 | - | |
| 403 | - $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(); | |
| 408 | - if ( ! is_user_logged_in() && $enable_login_limit ) { | |
| 409 | - $button_text = esc_html__( 'Please login', 'shopbuilder' ); | |
| 410 | - $page_url = $myaccount_url; | |
| 411 | - } else { | |
| 412 | - $button_text = esc_html__( 'Wishlist', 'shopbuilder' ); | |
| 413 | - $page_url = wc_get_account_endpoint_url( 'wishlist' ); | |
| 414 | - } | |
| 415 | - | |
| 416 | - $default_atts = [ | |
| 417 | - 'products' => $products, | |
| 418 | - 'item_count' => count( $products ), | |
| 419 | - 'page_url' => $page_url, | |
| 420 | - 'button_text' => $button_text, | |
| 421 | - 'enable_login_limit' => $enable_login_limit, | |
| 422 | - 'text' => '', | |
| 423 | - ]; | |
| 424 | - | |
| 425 | - $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 426 | - $count_attr = apply_filters( 'rtsb/module/wishlist/counter_args', $atts ); | |
| 427 | - | |
| 428 | - return Fns::load_template( 'wishlist/counter', $count_attr, true ); | |
| 429 | - } | |
| 430 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace RadiusTheme\SB\Modules\WishList; | |
| 4 | + | |
| 5 | +use RadiusTheme\SB\Helpers\Fns; | |
| 6 | +use RadiusTheme\SB\Traits\SingletonTrait; | |
| 7 | +use WC_Product; | |
| 8 | + | |
| 9 | +// Do not allow directly accessing this file. | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 'This script cannot be accessed directly.' ); | |
| 12 | +} | |
| 13 | + | |
| 14 | +class WishlistFrontEnd { | |
| 15 | + | |
| 16 | + | |
| 17 | + use SingletonTrait; | |
| 18 | + | |
| 19 | + public function __construct() { | |
| 20 | + | |
| 21 | + //Template | |
| 22 | + add_action( 'init', [ $this, 'attach_button' ] ); | |
| 23 | + add_filter( 'body_class', [ $this, 'add_body_class' ] ); | |
| 24 | + | |
| 25 | + // Note:: Add do_action('rtsb/modules/wishlist/frontend/display' ); for display anywhere. | |
| 26 | + add_action( 'rtsb/modules/wishlist/frontend/display', [ $this, 'button_hook' ] ); | |
| 27 | + | |
| 28 | + add_action( 'rtsb/modules/wishlist/print_button', [ $this, 'print_button' ] ); | |
| 29 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 30 | + | |
| 31 | + // ShortCode | |
| 32 | + add_shortcode( 'rtsb_wishlist', [ $this, 'list_shortcode' ] ); | |
| 33 | + add_shortcode( 'rtsb_wishlist_button', [ $this, 'button_shortcode' ] ); | |
| 34 | + add_shortcode( 'rtsb_wishlist_counter', [ $this, 'counter_shortcode' ] ); | |
| 35 | + } | |
| 36 | + | |
| 37 | + | |
| 38 | + public function enqueue() { | |
| 39 | + wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION ); | |
| 40 | + wp_register_script( 'rtsb-wishlist', rtsb()->get_assets_uri( 'modules/wishlist.js' ), [ | |
| 41 | + 'jquery', | |
| 42 | + 'rtsb-public', | |
| 43 | + ], RTSB_VERSION, true ); | |
| 44 | + wp_enqueue_style( 'rtsb-modules' ); | |
| 45 | + wp_enqueue_script( 'rtsb-wishlist' ); | |
| 46 | + if ( is_user_logged_in() && is_account_page() && is_wc_endpoint_url( WishlistFns::WC_MENU_SLUG ) ) { | |
| 47 | + wp_enqueue_script('wc-add-to-cart'); | |
| 48 | + wp_enqueue_script('wc-add-to-cart-variation'); | |
| 49 | + } | |
| 50 | + | |
| 51 | + $params = apply_filters( 'rtsb/module/wishlist/js_params', [ | |
| 52 | + 'product_id' => get_the_ID(), | |
| 53 | + 'resturl' => get_rest_url(), | |
| 54 | + 'isLoggedIn' => is_user_logged_in(), | |
| 55 | + 'pageUrl' => wc_get_account_endpoint_url( 'wishlist' ), | |
| 56 | + 'rest_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 57 | + 'notice' => [ | |
| 58 | + 'add_to_wishlist' => esc_html__( 'Add to wishlist', 'shopbuilder' ), | |
| 59 | + 'remove_wishlist' => esc_html__( 'Remove from list', 'shopbuilder' ), | |
| 60 | + 'added' => Fns::get_option( 'modules', 'wishlist', 'notice_added_text', esc_html__( 'Product added!', 'shopbuilder' ) ), | |
| 61 | + 'removed' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'Product removed!', 'shopbuilder' ) ), | |
| 62 | + 'exist' => Fns::get_option( 'modules', 'wishlist', 'notice_removed_text', esc_html__( 'The product is already in your wishlist!', 'shopbuilder' ) ), | |
| 63 | + 'browse' => Fns::get_option( 'modules', 'wishlist', 'browse_list_text', esc_html__( 'Browse wishlist', 'shopbuilder' ) ), | |
| 64 | + 'error' => esc_html__( 'Something went wrong!.', 'shopbuilder' ), | |
| 65 | + 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ), | |
| 66 | + ], | |
| 67 | + ] ); | |
| 68 | + | |
| 69 | + wp_localize_script( 'rtsb-wishlist', 'rtsbWishlistParams', $params ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Add the "Add to Wishlist" button. Needed to use in wp_head hook. | |
| 74 | + * | |
| 75 | + * @return void | |
| 76 | + * @since 1.0.0 | |
| 77 | + */ | |
| 78 | + public function attach_button() { | |
| 79 | + $positions = apply_filters( | |
| 80 | + 'rtsb/module/wishlist/product_btn_positions', | |
| 81 | + [ | |
| 82 | + 'before_cart_btn' => [ | |
| 83 | + 'hook' => 'woocommerce_before_add_to_cart_button', | |
| 84 | + 'priority' => 20, | |
| 85 | + ], | |
| 86 | + 'after_add_to_cart' => [ | |
| 87 | + 'hook' => 'woocommerce_single_product_summary', | |
| 88 | + 'priority' => 31, | |
| 89 | + ], | |
| 90 | + 'after_thumbnail' => [ | |
| 91 | + 'hook' => 'woocommerce_product_thumbnails', | |
| 92 | + 'priority' => 21, | |
| 93 | + ], | |
| 94 | + 'after_summary' => [ | |
| 95 | + 'hook' => 'woocommerce_after_single_product_summary', | |
| 96 | + 'priority' => 11, | |
| 97 | + ], | |
| 98 | + 'custom' => [ | |
| 99 | + 'hook' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_name', '' ), | |
| 100 | + 'priority' => Fns::get_option( 'modules', 'wishlist', 'product_custom_hook_priority', 10 ), | |
| 101 | + ], | |
| 102 | + ] | |
| 103 | + ); | |
| 104 | + | |
| 105 | + // Add the link "Add to wishlist". | |
| 106 | + $product_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_product_page', true, 'checkbox' ); | |
| 107 | + $product_btn_position = Fns::get_option( 'modules', 'wishlist', 'product_btn_position', 'after_add_to_cart' ); | |
| 108 | + if ( $product_btn_enable && 'shortcode' !== $product_btn_position && isset( $positions[ $product_btn_position ]['hook'] ) ) { | |
| 109 | + add_action( $positions[ $product_btn_position ]['hook'], [ | |
| 110 | + $this, | |
| 111 | + 'button_hook' | |
| 112 | + ], isset( $positions[ $product_btn_position ]['priority'] ) ? absint( $positions[ $product_btn_position ]['priority'] ) : '' ); | |
| 113 | + } | |
| 114 | + // check if Add to wishlist button is enabled for loop. | |
| 115 | + $loop_btn_enable = Fns::get_option( 'modules', 'wishlist', 'show_btn_on_loop', false, 'checkbox' ); | |
| 116 | + if ( ! $loop_btn_enable ) { | |
| 117 | + return; | |
| 118 | + } | |
| 119 | + | |
| 120 | + $positions = apply_filters( | |
| 121 | + 'rtsb/module/wishlist/loop_btn_position', | |
| 122 | + [ | |
| 123 | + 'top_thumbnail' => [ | |
| 124 | + 'hook' => 'woocommerce_before_shop_loop_item', | |
| 125 | + 'priority' => 5, | |
| 126 | + ], | |
| 127 | + 'before_add_to_cart' => [ | |
| 128 | + 'hook' => 'woocommerce_after_shop_loop_item', | |
| 129 | + 'priority' => 7, | |
| 130 | + ], | |
| 131 | + 'after_add_to_cart' => [ | |
| 132 | + 'hook' => 'woocommerce_after_shop_loop_item', | |
| 133 | + 'priority' => 15, | |
| 134 | + ], | |
| 135 | + 'custom' => [ | |
| 136 | + 'hook' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_name', '' ), | |
| 137 | + 'priority' => Fns::get_option( 'modules', 'wishlist', 'loop_custom_hook_priority', 10 ), | |
| 138 | + ], | |
| 139 | + ] | |
| 140 | + ); | |
| 141 | + | |
| 142 | + // Add the link "Add to wishlist" in the loop. | |
| 143 | + $loop_btn_position = Fns::get_option( 'modules', 'wishlist', 'loop_btn_position', 'after_add_to_cart' ); | |
| 144 | + | |
| 145 | + if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) { | |
| 146 | + add_action( $positions[ $loop_btn_position ]['hook'], [ | |
| 147 | + $this, | |
| 148 | + 'button_hook' | |
| 149 | + ], isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + // Add the link "Add to wishlist" for Gutenberg blocks. | |
| 153 | + add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); | |
| 154 | + | |
| 155 | + | |
| 156 | + // rtsb/module/wishlist/show_button | |
| 157 | + } | |
| 158 | + | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Add ATW button to Products block item | |
| 162 | + * | |
| 163 | + * @param string $item_html HTML of the single block item. | |
| 164 | + * @param array $data Data used to render the item. | |
| 165 | + * @param WC_Product $product Current product. | |
| 166 | + * | |
| 167 | + * @return string Filtered HTML. | |
| 168 | + */ | |
| 169 | + public function add_button_for_block( $item_html, $data, $product ) { | |
| 170 | + // Add the link "Add to wishlist" in the loop. | |
| 171 | + $loop_btn_position = Fns::get_option( 'modules', 'wishlist', 'shop_btn_position', 'after_add_to_cart' ); | |
| 172 | + ob_start(); | |
| 173 | + $this->print_button( $product->get_id() ); | |
| 174 | + $button = ob_get_clean(); | |
| 175 | + $parts = []; | |
| 176 | + | |
| 177 | + preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts ); | |
| 178 | + | |
| 179 | + if ( ! $parts || count( $parts ) < 5 ) { | |
| 180 | + return $item_html; | |
| 181 | + } | |
| 182 | + | |
| 183 | + // removes first match (entire match). | |
| 184 | + array_shift( $parts ); | |
| 185 | + | |
| 186 | + // removes empty parts. | |
| 187 | + $parts = array_filter( $parts ); | |
| 188 | + | |
| 189 | + // searches for index to cut parts array. | |
| 190 | + switch ( $loop_btn_position ) { | |
| 191 | + case 'top_thumbnail': | |
| 192 | + $index = 1; | |
| 193 | + break; | |
| 194 | + case 'before_add_to_cart': | |
| 195 | + $index = 2; | |
| 196 | + break; | |
| 197 | + case 'after_add_to_cart': | |
| 198 | + $index = 3; | |
| 199 | + break; | |
| 200 | + default: | |
| 201 | + $index = 0; | |
| 202 | + break; | |
| 203 | + } | |
| 204 | + | |
| 205 | + // if index is found, stitch button in correct position. | |
| 206 | + if ( $index ) { | |
| 207 | + $first_set = array_slice( $parts, 0, $index ); | |
| 208 | + $second_set = array_slice( $parts, $index ); | |
| 209 | + | |
| 210 | + $parts = array_merge( | |
| 211 | + $first_set, | |
| 212 | + (array) $button, | |
| 213 | + $second_set | |
| 214 | + ); | |
| 215 | + | |
| 216 | + // replace li classes. | |
| 217 | + $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 add-to-wishlist-' . $shop_btn_position . '"', $parts[0] ); | |
| 218 | + } | |
| 219 | + | |
| 220 | + // join all parts together and return item. | |
| 221 | + return implode( '', $parts ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Returns HTML for ATW button | |
| 226 | + * | |
| 227 | + * @param int $product_id Optional product id (if empty global product will be used instead by the shortcode). | |
| 228 | + * | |
| 229 | + * @return string HTML for ATW button. | |
| 230 | + */ | |
| 231 | + public function get_button( $product_id = false ) { | |
| 232 | + $shortcode_tag = 'rtsb_wishlist_button'; | |
| 233 | + $options = []; | |
| 234 | + $text_options = ''; | |
| 235 | + | |
| 236 | + if ( $product_id ) { | |
| 237 | + $options['product_id'] = $product_id; | |
| 238 | + } | |
| 239 | + | |
| 240 | + if ( ! empty( $options ) ) { | |
| 241 | + foreach ( $options as $option_key => $option_value ) { | |
| 242 | + $text_options .= " $option_key=\"$option_value\""; | |
| 243 | + } | |
| 244 | + } | |
| 245 | + | |
| 246 | + $shortcode = "[$shortcode_tag $text_options]"; | |
| 247 | + | |
| 248 | + return do_shortcode( $shortcode ); | |
| 249 | + } | |
| 250 | + | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * Print "Add to compare" button | |
| 254 | + * | |
| 255 | + * @return void | |
| 256 | + */ | |
| 257 | + public function button_hook() { | |
| 258 | + | |
| 259 | + if ( ! apply_filters( 'rtsb/module/wishlist/show_button', true ) ) { | |
| 260 | + return; | |
| 261 | + } | |
| 262 | + global $product; | |
| 263 | + | |
| 264 | + do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() ); | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * @return string|void | |
| 269 | + */ | |
| 270 | + public function print_button( $product_id = 0 ) { | |
| 271 | + | |
| 272 | + global $product; | |
| 273 | + | |
| 274 | + // product object. | |
| 275 | + $current_product = $product_id ? wc_get_product( $product_id ) : false; | |
| 276 | + $current_product = $current_product ?: $product; | |
| 277 | + | |
| 278 | + if ( ! $current_product instanceof WC_Product ) { | |
| 279 | + return ''; | |
| 280 | + } | |
| 281 | + | |
| 282 | + // product parent. | |
| 283 | + $current_product_parent = $current_product->get_parent_id(); | |
| 284 | + | |
| 285 | + // button class. | |
| 286 | + $classes = []; | |
| 287 | + // check if product is already in wishlist. | |
| 288 | + $exists = WishlistFns::instance()->is_exists_in_wishlist( $current_product->get_id() ); | |
| 289 | + if ( $exists ) { | |
| 290 | + $classes[] = 'rtsb-wishlist-remove'; | |
| 291 | + $button_text = Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Remove from list', 'shopbuilder' ) ); | |
| 292 | + $button_text = apply_filters( 'rtsb/modules/wishlist/remove_from_list_button_text', $button_text ); | |
| 293 | + $icon_html = '<i class="rtsb-icon rtsb-icon-heart"></i>'; | |
| 294 | + } else { | |
| 295 | + $classes[] = 'rtsb-wishlist-add'; | |
| 296 | + $icon_html = '<i class="rtsb-icon rtsb-icon-heart-empty"></i>'; | |
| 297 | + // labels & icons settings. | |
| 298 | + $button_text = Fns::get_option( 'modules', 'wishlist', 'button_text', esc_html__( 'Add to wishlist', 'shopbuilder' ) ); | |
| 299 | + // button text. | |
| 300 | + $button_text = apply_filters( 'rtsb/module/wishlist/add_to_list_button_txt', $button_text ); | |
| 301 | + } | |
| 302 | + | |
| 303 | + // get product type. | |
| 304 | + $product_type = $current_product->get_type(); | |
| 305 | + $params = [ | |
| 306 | + 'exists' => $exists, | |
| 307 | + 'classes' => $classes, | |
| 308 | + 'product_id' => $current_product->get_id(), | |
| 309 | + 'parent_product_id' => $current_product_parent ?: $current_product->get_id(), | |
| 310 | + 'product_type' => $product_type, | |
| 311 | + 'button_text' => $button_text, | |
| 312 | + 'left_text' => apply_filters( 'rtsb/module/wishlist/button_left_text', '' ), | |
| 313 | + 'right_text' => apply_filters( 'rtsb/module/wishlist/button_right_text', '' ) | |
| 314 | + ]; | |
| 315 | + | |
| 316 | + // let third party developer filter options. | |
| 317 | + $atts = apply_filters( 'rtsb/module/wishlist/button_params', $params ); | |
| 318 | + | |
| 319 | + // set fragment options. | |
| 320 | + $atts['icon_html'] = apply_filters( 'rtsb/module/wishlist/icon_html', $icon_html, $atts ); | |
| 321 | + | |
| 322 | + Fns::load_template( 'wishlist/button', $atts ); | |
| 323 | + } | |
| 324 | + | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * Add specific body class when the Wishlist page is opened | |
| 328 | + * | |
| 329 | + * @param array $classes Existing boy classes. | |
| 330 | + * | |
| 331 | + * @return array | |
| 332 | + */ | |
| 333 | + public function add_body_class( $classes ) { | |
| 334 | + $wishlist_page_id = Fns::get_option( 'modules', 'wishlist', 'page', '', 'number' ); | |
| 335 | + if ( ! empty( $wishlist_page_id ) && is_page( $wishlist_page_id ) ) { | |
| 336 | + $classes[] = 'woocommerce-wishlist rtsb-wishlist-page'; | |
| 337 | + $classes[] = 'woocommerce'; | |
| 338 | + $classes[] = 'woocommerce-page'; | |
| 339 | + } | |
| 340 | + | |
| 341 | + return $classes; | |
| 342 | + } | |
| 343 | + | |
| 344 | + | |
| 345 | + /** | |
| 346 | + * Table List Shortcode callable function | |
| 347 | + * | |
| 348 | + * @param array $atts | |
| 349 | + * @param string $content | |
| 350 | + * | |
| 351 | + * @return string [HTML] | |
| 352 | + */ | |
| 353 | + public function list_shortcode( $atts, $content = '' ) { | |
| 354 | + wp_enqueue_style( 'rtsb-modules' ); | |
| 355 | + wp_enqueue_script( 'rtsb-wishlist' ); | |
| 356 | + | |
| 357 | + /* Fetch From option data */ | |
| 358 | + $empty_text = Fns::get_option( 'modules', 'wishlist', 'empty_table_text', esc_html__( "No product found at your wishlist.", 'shopbuilder' ) ); | |
| 359 | + /* Product and Field */ | |
| 360 | + $products = WishlistFns::instance()->get_products_data(); | |
| 361 | + $field_ids = WishlistFns::instance()->get_field_ids(); | |
| 362 | + | |
| 363 | + $custom_headings = []; | |
| 364 | + | |
| 365 | + $default_atts = [ | |
| 366 | + 'products' => $products, | |
| 367 | + 'field_ids' => $field_ids, | |
| 368 | + 'empty_text' => ! empty( $empty_text ) ? $empty_text : '', | |
| 369 | + ]; | |
| 370 | + | |
| 371 | + $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 372 | + $args = apply_filters( 'rtsb/module/wishlist/list_args', $atts ); | |
| 373 | + | |
| 374 | + return Fns::load_template( 'wishlist/list', $args, true ); | |
| 375 | + } | |
| 376 | + | |
| 377 | + /** | |
| 378 | + * Wishlist button Shortcode callable function | |
| 379 | + * | |
| 380 | + * @param array $atts | |
| 381 | + * @param string $content | |
| 382 | + * | |
| 383 | + * @return string [HTML] | |
| 384 | + */ | |
| 385 | + public function button_shortcode( $atts, $content = '' ) { | |
| 386 | + wp_enqueue_style( 'rtsb-modules' ); | |
| 387 | + wp_enqueue_script( 'rtsb-wishlist' ); | |
| 388 | + | |
| 389 | + ob_start(); | |
| 390 | + global $product; | |
| 391 | + if ( ! $product instanceof WC_Product ) { | |
| 392 | + return ''; | |
| 393 | + } | |
| 394 | + | |
| 395 | + do_action( 'rtsb/modules/wishlist/print_button', $product->get_id() ); | |
| 396 | + | |
| 397 | + return ob_get_clean(); | |
| 398 | + } | |
| 399 | + | |
| 400 | + public function counter_shortcode( $atts, $content = '' ) { | |
| 401 | + wp_enqueue_style( 'rtsb-modules' ); | |
| 402 | + | |
| 403 | + $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(); | |
| 408 | + if ( ! is_user_logged_in() && $enable_login_limit ) { | |
| 409 | + $button_text = esc_html__( 'Please login', 'shopbuilder' ); | |
| 410 | + $page_url = $myaccount_url; | |
| 411 | + } else { | |
| 412 | + $button_text = esc_html__( 'Wishlist', 'shopbuilder' ); | |
| 413 | + $page_url = wc_get_account_endpoint_url( 'wishlist' ); | |
| 414 | + } | |
| 415 | + | |
| 416 | + $default_atts = [ | |
| 417 | + 'products' => $products, | |
| 418 | + 'item_count' => count( $products ), | |
| 419 | + 'page_url' => $page_url, | |
| 420 | + 'button_text' => $button_text, | |
| 421 | + 'enable_login_limit' => $enable_login_limit, | |
| 422 | + 'text' => '', | |
| 423 | + ]; | |
| 424 | + | |
| 425 | + $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 426 | + $count_attr = apply_filters( 'rtsb/module/wishlist/counter_args', $atts ); | |
| 427 | + | |
| 428 | + return Fns::load_template( 'wishlist/counter', $count_attr, true ); | |
| 429 | + } | |
| 430 | + | |
| 431 | +} | |