| @@ -1,361 +1,327 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace RadiusTheme\SB\Modules\QuickView; | |
| 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 QuickViewFrontEnd { | |
| 15 | - use SingletonTrait; | |
| 16 | - | |
| 17 | - public function __construct() { | |
| 18 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 19 | - | |
| 20 | - // Gallery Plugin Support rtwpvg_disable_enqueue_scripts. | |
| 21 | - add_filter( 'rtwpvg_disable_enqueue_scripts', '__return_false', 11 ); | |
| 22 | - | |
| 23 | - // Template. | |
| 24 | - | |
| 25 | - add_action( 'rtsb/modules/quick_view/frontend/display', [ $this, 'button_hook' ] ); | |
| 26 | - // Add do_action( 'rtsb/modules/quick_view/frontend/display' ); for display anywhere. | |
| 27 | - | |
| 28 | - add_action( 'rtsb/modules/quick_view/print_button', [ $this, 'print_button' ] ); | |
| 29 | - | |
| 30 | - // Load action for product template. | |
| 31 | - $this->quick_view_action_template(); | |
| 32 | - | |
| 33 | - // ShortCode. | |
| 34 | - add_shortcode( 'rtsb_quick_view_button', [ $this, 'button_shortcode' ] ); | |
| 35 | - | |
| 36 | - // Quick view AJAX. | |
| 37 | - add_action( 'wp_ajax_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] ); | |
| 38 | - | |
| 39 | - add_action( 'wp_ajax_nopriv_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] ); | |
| 40 | - | |
| 41 | - $this->attach_button(); | |
| 42 | - } | |
| 43 | - | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Load wc action for quick view product template | |
| 47 | - * | |
| 48 | - * @access public | |
| 49 | - * @return void | |
| 50 | - * @since 1.0.0 | |
| 51 | - */ | |
| 52 | - public function quick_view_action_template() { | |
| 53 | - | |
| 54 | - // Image. | |
| 55 | - add_action( 'rtsb/wcqv/product/image', 'woocommerce_show_product_sale_flash', 10 ); | |
| 56 | - add_action( 'rtsb/wcqv/product/image', 'woocommerce_show_product_images', 20 ); | |
| 57 | - | |
| 58 | - // Summary. | |
| 59 | - add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_title', 5 ); | |
| 60 | - add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_rating', 10 ); | |
| 61 | - add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_price', 15 ); | |
| 62 | - add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_excerpt', 20 ); | |
| 63 | - add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_add_to_cart', 25 ); | |
| 64 | - add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_meta', 30 ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - public function product_quick_view_ajax() { | |
| 68 | - $product_id = isset( $_REQUEST['product_id'] ) ? absint( $_REQUEST['product_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 69 | - | |
| 70 | - if ( empty( $product_id ) || 'publish' !== get_post_status( $product_id ) ) { | |
| 71 | - wp_send_json_error( esc_html__( 'Product Id not found', 'shopbuilder' ) ); | |
| 72 | - } | |
| 73 | - | |
| 74 | - global $sitepress; | |
| 75 | - | |
| 76 | - $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['lang'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 77 | - if ( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress ) ) { | |
| 78 | - $sitepress->switch_lang( $lang, true ); | |
| 79 | - } | |
| 80 | - | |
| 81 | - // Set the main wp query for the product. | |
| 82 | - wp( 'p=' . $product_id . '&post_type=product' ); | |
| 83 | - | |
| 84 | - // Remove product thumbnails gallery. | |
| 85 | - if ( defined( 'RTWPVG_VERSION' ) ) { | |
| 86 | - remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 ); | |
| 87 | - } | |
| 88 | - if ( ! defined( 'RTWPVG_VERSION' ) ) { | |
| 89 | - //add_action( 'woocommerce_product_thumbnails', [ $this, 'woocommerce_product_thumbnails_before' ], 1 ); | |
| 90 | - //add_action( 'woocommerce_product_thumbnails', [ $this, 'woocommerce_product_thumbnails_after' ], 25 ); | |
| 91 | - } | |
| 92 | - | |
| 93 | - ob_start(); | |
| 94 | - Fns::load_template( 'quick-view/content' ); | |
| 95 | - $html = ob_get_contents(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 96 | - ob_end_clean(); | |
| 97 | - | |
| 98 | - wp_send_json_success( [ 'html' => $html ] ); | |
| 99 | - } | |
| 100 | - | |
| 101 | - /** | |
| 102 | - * Add the "Add to Wishlist" button. Needed to use in wp_head hook. | |
| 103 | - * | |
| 104 | - * @return void | |
| 105 | - * @since 1.0.0 | |
| 106 | - */ | |
| 107 | - public function attach_button() { | |
| 108 | - | |
| 109 | - $positions = apply_filters( | |
| 110 | - 'rtsb/module/quick_view/loop_btn_position', | |
| 111 | - [ | |
| 112 | - | |
| 113 | - 'before_add_to_cart' => [ | |
| 114 | - 'hook' => 'woocommerce_after_shop_loop_item', | |
| 115 | - 'priority' => 7, | |
| 116 | - ], | |
| 117 | - 'after_add_to_cart' => [ | |
| 118 | - 'hook' => 'woocommerce_after_shop_loop_item', | |
| 119 | - 'priority' => 15, | |
| 120 | - ], | |
| 121 | - 'custom' => [ | |
| 122 | - 'hook' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_name', '' ), | |
| 123 | - 'priority' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_priority', '' ), | |
| 124 | - ], | |
| 125 | - ] | |
| 126 | - ); | |
| 127 | - | |
| 128 | - // Add the link "Add to wishlist" in the loop. | |
| 129 | - | |
| 130 | - $loop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); | |
| 131 | - | |
| 132 | - if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) { | |
| 133 | - add_action( | |
| 134 | - $positions[ $loop_btn_position ]['hook'], | |
| 135 | - [ | |
| 136 | - $this, | |
| 137 | - 'button_hook', | |
| 138 | - ], | |
| 139 | - isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' | |
| 140 | - ); | |
| 141 | - } | |
| 142 | - // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility. | |
| 143 | - // Add the link "Quick view" for Gutenberg blocks. | |
| 144 | - add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); | |
| 145 | - } | |
| 146 | - | |
| 147 | - /** | |
| 148 | - * Print "Add to compare" button | |
| 149 | - * | |
| 150 | - * @return void | |
| 151 | - */ | |
| 152 | - public function woocommerce_product_thumbnails_before() { | |
| 153 | - ?> | |
| 154 | - <div class="rtsb-gallery-images-wrapper"> | |
| 155 | - <?php | |
| 156 | - } | |
| 157 | - /** | |
| 158 | - * Print "Add to compare" button | |
| 159 | - * | |
| 160 | - * @return void | |
| 161 | - */ | |
| 162 | - public function woocommerce_product_thumbnails_after() { | |
| 163 | - | |
| 164 | - ?> | |
| 165 | - </div> | |
| 166 | - <?php | |
| 167 | - } | |
| 168 | - | |
| 169 | - /** | |
| 170 | - * Print "Add to compare" button | |
| 171 | - * | |
| 172 | - * @return void | |
| 173 | - */ | |
| 174 | - public function button_hook() { | |
| 175 | - if ( ! apply_filters( 'rtsb/module/quick_view/show_button', true ) ) { | |
| 176 | - return; | |
| 177 | - } | |
| 178 | - global $product; | |
| 179 | - if ( $product instanceof WC_Product ) { | |
| 180 | - do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() ); | |
| 181 | - } | |
| 182 | - | |
| 183 | - } | |
| 184 | - | |
| 185 | - | |
| 186 | - /** | |
| 187 | - * @return string|void | |
| 188 | - */ | |
| 189 | - public function print_button( $product_id = 0 ) { | |
| 190 | - global $product; | |
| 191 | - | |
| 192 | - if ( ! $product instanceof WC_Product && $product_id ) { | |
| 193 | - $product = wc_get_product( $product_id ); | |
| 194 | - } | |
| 195 | - | |
| 196 | - // if ( ! $current_product instanceof WC_Product ) { | |
| 197 | - // return ''; | |
| 198 | - // } | |
| 199 | - | |
| 200 | - $classes = []; // button class. | |
| 201 | - $button_text = Fns::get_option( 'modules', 'quick_view', 'button_text', esc_html__( 'Quick View', 'shopbuilder' ) ); | |
| 202 | - | |
| 203 | - $icon_html = '<i class="rtsb-icon rtsb-icon-eye"></i>'; | |
| 204 | - // get product type. | |
| 205 | - $product_type = $product->get_type(); | |
| 206 | - $params = [ | |
| 207 | - 'classes' => $classes, | |
| 208 | - 'product_id' => $product->get_id(), | |
| 209 | - 'product_type' => $product_type, | |
| 210 | - 'button_text' => $button_text, | |
| 211 | - 'left_text' => apply_filters( 'rtsb/module/quick_view/button_left_text', '' ), | |
| 212 | - 'right_text' => apply_filters( 'rtsb/module/quick_view/button_right_text', '' ), | |
| 213 | - ]; | |
| 214 | - // let third party developer filter options. | |
| 215 | - $atts = apply_filters( 'rtsb/module/quick_view/button_params', $params ); | |
| 216 | - // set fragment options. | |
| 217 | - $atts['icon_html'] = apply_filters( 'rtsb/module/quick_view/icon_html', $icon_html, $atts ); | |
| 218 | - | |
| 219 | - Fns::load_template( 'quick-view/button', $atts ); | |
| 220 | - } | |
| 221 | - | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Wishlist button Shortcode callable function | |
| 225 | - * | |
| 226 | - * @param array $atts | |
| 227 | - * @param string $content | |
| 228 | - * | |
| 229 | - * @return string [HTML] | |
| 230 | - */ | |
| 231 | - public function button_shortcode( $atts, $content = '' ) { | |
| 232 | - global $product; | |
| 233 | - if ( ! $product instanceof WC_Product ) { | |
| 234 | - return ''; | |
| 235 | - } | |
| 236 | - ob_start(); | |
| 237 | - | |
| 238 | - do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() ); | |
| 239 | - | |
| 240 | - return ob_get_clean(); | |
| 241 | - } | |
| 242 | - | |
| 243 | - public function enqueue() { | |
| 244 | - | |
| 245 | - wp_enqueue_style( 'elementor-icons-fa-regular' ); | |
| 246 | - | |
| 247 | - wp_enqueue_style( 'elementor-icons-fa-solid' ); | |
| 248 | - wp_enqueue_style( 'elementor-icons-shared-0' ); | |
| 249 | - // wp_enqueue_style( 'elementor-frontend' ); | |
| 250 | - | |
| 251 | - if ( ! class_exists('WooProductVariationGallery') && current_theme_supports( 'wc-product-gallery-slider' ) ) { | |
| 252 | - wp_enqueue_script( 'flexslider' ); | |
| 253 | - } | |
| 254 | - | |
| 255 | - wp_enqueue_script( 'wc-add-to-cart-variation' ); | |
| 256 | - if ( version_compare( WC()->version, '3.0.0', '>=' ) ) { | |
| 257 | - if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { | |
| 258 | - wp_enqueue_script( 'zoom' ); | |
| 259 | - } | |
| 260 | - if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) { | |
| 261 | - wp_enqueue_script( 'photoswipe-ui-default' ); | |
| 262 | - wp_enqueue_style( 'photoswipe-default-skin' ); | |
| 263 | - if ( has_action( 'wp_footer', 'woocommerce_photoswipe' ) === false ) { | |
| 264 | - add_action( 'wp_footer', 'woocommerce_photoswipe', 15 ); | |
| 265 | - } | |
| 266 | - } | |
| 267 | - wp_enqueue_script( 'wc-single-product' ); | |
| 268 | - } | |
| 269 | - | |
| 270 | - wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION ); | |
| 271 | - wp_register_script( | |
| 272 | - 'rtsb-quick-view', | |
| 273 | - rtsb()->get_assets_uri( 'modules/quick-view.js' ), | |
| 274 | - [ | |
| 275 | - 'jquery', | |
| 276 | - 'rtsb-public', | |
| 277 | - ], | |
| 278 | - RTSB_VERSION, | |
| 279 | - true | |
| 280 | - ); | |
| 281 | - wp_enqueue_style( 'rtsb-modules' ); | |
| 282 | - wp_enqueue_script( 'rtsb-quick-view' ); | |
| 283 | - | |
| 284 | - // Allow user to load custom style and scripts! | |
| 285 | - do_action( 'rtsb/modules/quick_view/custom_scripts' ); | |
| 286 | - | |
| 287 | - $params = apply_filters( | |
| 288 | - 'rtsb/module/quick_view/js_params', | |
| 289 | - [ | |
| 290 | - 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ), | |
| 291 | - 'lang' => defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '', | |
| 292 | - 'isLoggedIn' => is_user_logged_in(), | |
| 293 | - ] | |
| 294 | - ); | |
| 295 | - | |
| 296 | - wp_localize_script( 'rtsb-quick-view', 'rtsbQvParams', $params ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - | |
| 300 | - /** | |
| 301 | - * Add ATW button to Products block item | |
| 302 | - * | |
| 303 | - * @param string $item_html HTML of the single block item. | |
| 304 | - * @param array $data Data used to render the item. | |
| 305 | - * @param WC_Product $product Current product. | |
| 306 | - * | |
| 307 | - * @return string Filtered HTML. | |
| 308 | - */ | |
| 309 | - public function add_button_for_block( $item_html, $data, $product ) { | |
| 310 | - // Add the link "Add to wishlist" in the loop. | |
| 311 | - $shop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); | |
| 312 | - ob_start(); | |
| 313 | - $this->print_button( $product->get_id() ); | |
| 314 | - $button = ob_get_clean(); | |
| 315 | - $parts = []; | |
| 316 | - | |
| 317 | - preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts ); | |
| 318 | - | |
| 319 | - if ( ! $parts || count( $parts ) < 5 ) { | |
| 320 | - return $item_html; | |
| 321 | - } | |
| 322 | - | |
| 323 | - // removes first match (entire match). | |
| 324 | - array_shift( $parts ); | |
| 325 | - | |
| 326 | - // removes empty parts. | |
| 327 | - $parts = array_filter( $parts ); | |
| 328 | - | |
| 329 | - // searches for index to cut parts array. | |
| 330 | - switch ( $shop_btn_position ) { | |
| 331 | - | |
| 332 | - case 'before_add_to_cart': | |
| 333 | - $index = 2; | |
| 334 | - break; | |
| 335 | - case 'after_add_to_cart': | |
| 336 | - $index = 3; | |
| 337 | - break; | |
| 338 | - default: | |
| 339 | - $index = 0; | |
| 340 | - break; | |
| 341 | - } | |
| 342 | - | |
| 343 | - // if index is found, stitch button in correct position. | |
| 344 | - if ( $index ) { | |
| 345 | - $first_set = array_slice( $parts, 0, $index ); | |
| 346 | - $second_set = array_slice( $parts, $index ); | |
| 347 | - | |
| 348 | - $parts = array_merge( | |
| 349 | - $first_set, | |
| 350 | - (array) $button, | |
| 351 | - $second_set | |
| 352 | - ); | |
| 353 | - | |
| 354 | - // replace li classes. | |
| 355 | - $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 rtsb-quick-view-btn-' . $shop_btn_position . '"', $parts[0] ); | |
| 356 | - } | |
| 357 | - | |
| 358 | - // join all parts together and return item. | |
| 359 | - return implode( '', $parts ); | |
| 360 | - } | |
| 361 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace RadiusTheme\SB\Modules\QuickView; | |
| 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 QuickViewFrontEnd { | |
| 15 | + use SingletonTrait; | |
| 16 | + | |
| 17 | + public function __construct() { | |
| 18 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 19 | + | |
| 20 | + // Template. | |
| 21 | + add_action( 'init', [ $this, 'attach_button' ] ); | |
| 22 | + | |
| 23 | + add_action( 'rtsb/modules/quick_view/frontend/display', [ $this, 'button_hook' ] ); | |
| 24 | + // Add do_action( 'rtsb/modules/quick_view/frontend/display' ); for display anywhere. | |
| 25 | + | |
| 26 | + add_action( 'rtsb/modules/quick_view/print_button', [ $this, 'print_button' ] ); | |
| 27 | + | |
| 28 | + // Load action for product template. | |
| 29 | + $this->quick_view_action_template(); | |
| 30 | + | |
| 31 | + // ShortCode. | |
| 32 | + add_shortcode( 'rtsb_quick_view_button', [ $this, 'button_shortcode' ] ); | |
| 33 | + | |
| 34 | + // Quick view AJAX. | |
| 35 | + add_action( 'wp_ajax_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] ); | |
| 36 | + add_action( 'wp_ajax_nopriv_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] ); | |
| 37 | + | |
| 38 | + } | |
| 39 | + | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Load wc action for quick view product template | |
| 43 | + * | |
| 44 | + * @access public | |
| 45 | + * @return void | |
| 46 | + * @since 1.0.0 | |
| 47 | + */ | |
| 48 | + public function quick_view_action_template() { | |
| 49 | + | |
| 50 | + // Image. | |
| 51 | + add_action( 'rtsb_wcqv_product_image', 'woocommerce_show_product_sale_flash', 10 ); | |
| 52 | + add_action( 'rtsb_wcqv_product_image', 'woocommerce_show_product_images', 20 ); | |
| 53 | + | |
| 54 | + // Summary. | |
| 55 | + add_action( 'rtsb_wcqv_product_summary', 'woocommerce_template_single_title', 5 ); | |
| 56 | + add_action( 'rtsb_wcqv_product_summary', 'woocommerce_template_single_rating', 10 ); | |
| 57 | + add_action( 'rtsb_wcqv_product_summary', 'woocommerce_template_single_price', 15 ); | |
| 58 | + add_action( 'rtsb_wcqv_product_summary', 'woocommerce_template_single_excerpt', 20 ); | |
| 59 | + add_action( 'rtsb_wcqv_product_summary', 'woocommerce_template_single_add_to_cart', 25 ); | |
| 60 | + add_action( 'rtsb_wcqv_product_summary', 'woocommerce_template_single_meta', 30 ); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | + public function product_quick_view_ajax() { | |
| 65 | + $product_id = absint( $_REQUEST['product_id'] ); | |
| 66 | + | |
| 67 | + if ( empty( $product_id ) ) { | |
| 68 | + wp_send_json_error( esc_html__( 'Product Id not found', 'shopbuilder' ) ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + global $sitepress; | |
| 72 | + | |
| 73 | + $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['lang'] ) ) : ''; | |
| 74 | + if ( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress ) ) { | |
| 75 | + $sitepress->switch_lang( $lang, true ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + // Set the main wp query for the product. | |
| 79 | + wp( 'p=' . $product_id . '&post_type=product' ); | |
| 80 | + | |
| 81 | + // Remove product thumbnails gallery. | |
| 82 | + remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 ); | |
| 83 | + // Change template for variable products. | |
| 84 | + // if ( isset( $GLOBALS['yith_wccl'] ) ) { | |
| 85 | + // $GLOBALS['yith_wccl']->obj = new YITH_WCCL_Frontend(); | |
| 86 | + // $GLOBALS['yith_wccl']->obj->override(); | |
| 87 | + // } elseif ( defined( 'YITH_WCCL_PREMIUM' ) && YITH_WCCL_PREMIUM && class_exists( 'YITH_WCCL_Frontend' ) ) { | |
| 88 | + // $attributes = YITH_WCCL_Frontend()->create_attributes_json( $product_id, true ); | |
| 89 | + // } | |
| 90 | + ob_start(); | |
| 91 | + Fns::load_template( 'quick-view/content' ); | |
| 92 | + $html = ob_get_contents(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 93 | + ob_end_clean(); | |
| 94 | + | |
| 95 | + wp_send_json_success( [ 'html' => $html ] ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Add the "Add to Wishlist" button. Needed to use in wp_head hook. | |
| 100 | + * | |
| 101 | + * @return void | |
| 102 | + * @since 1.0.0 | |
| 103 | + */ | |
| 104 | + public function attach_button() { | |
| 105 | + | |
| 106 | + $positions = apply_filters( | |
| 107 | + 'rtsb/module/quick_view/loop_btn_position', | |
| 108 | + [ | |
| 109 | + 'top_thumbnail' => [ | |
| 110 | + 'hook' => 'woocommerce_before_shop_loop_item', | |
| 111 | + 'priority' => 5, | |
| 112 | + ], | |
| 113 | + 'before_add_to_cart' => [ | |
| 114 | + 'hook' => 'woocommerce_after_shop_loop_item', | |
| 115 | + 'priority' => 7, | |
| 116 | + ], | |
| 117 | + 'after_add_to_cart' => [ | |
| 118 | + 'hook' => 'woocommerce_after_shop_loop_item', | |
| 119 | + 'priority' => 15, | |
| 120 | + ], | |
| 121 | + 'custom' => [ | |
| 122 | + 'hook' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_name', '' ), | |
| 123 | + 'priority' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_priority', '' ), | |
| 124 | + ], | |
| 125 | + ] | |
| 126 | + ); | |
| 127 | + | |
| 128 | + // Add the link "Add to wishlist" in the loop. | |
| 129 | + | |
| 130 | + $loop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); | |
| 131 | + | |
| 132 | + if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) { | |
| 133 | + add_action( | |
| 134 | + $positions[ $loop_btn_position ]['hook'], | |
| 135 | + [ | |
| 136 | + $this, | |
| 137 | + 'button_hook', | |
| 138 | + ], | |
| 139 | + isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' | |
| 140 | + ); | |
| 141 | + } | |
| 142 | + | |
| 143 | + // Add the link "Quick view" for Gutenberg blocks. | |
| 144 | + add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Print "Add to compare" button | |
| 149 | + * | |
| 150 | + * @return void | |
| 151 | + */ | |
| 152 | + public function button_hook() { | |
| 153 | + if ( ! apply_filters( 'rtsb/module/quick_view/show_button', true ) ) { | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + global $product; | |
| 157 | + if( $product instanceof WC_Product ) { | |
| 158 | + do_action('rtsb/modules/quick_view/print_button', $product->get_id()); | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * @return string|void | |
| 165 | + */ | |
| 166 | + public function print_button( $product_id = 0 ) { | |
| 167 | + global $product; | |
| 168 | + | |
| 169 | + // product object. | |
| 170 | + $current_product = $product_id ? wc_get_product( $product_id ) : false; | |
| 171 | + $current_product = $current_product ?: $product; | |
| 172 | + | |
| 173 | + if ( ! $current_product instanceof WC_Product ) { | |
| 174 | + return ''; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $classes = []; // button class. | |
| 178 | + $button_text = Fns::get_option( 'modules', 'quick_view', 'button_text', esc_html__( 'Quick View', 'shopbuilder' ) ); | |
| 179 | + | |
| 180 | + $icon_html = '<i class="rtsb-icon rtsb-icon-eye"></i>'; | |
| 181 | + // get product type. | |
| 182 | + $product_type = $current_product->get_type(); | |
| 183 | + $params = [ | |
| 184 | + 'classes' => $classes, | |
| 185 | + 'product_id' => $current_product->get_id(), | |
| 186 | + 'product_type' => $product_type, | |
| 187 | + 'button_text' => $button_text, | |
| 188 | + 'left_text' => apply_filters( 'rtsb/module/quick_view/button_left_text', '' ), | |
| 189 | + 'right_text' => apply_filters( 'rtsb/module/quick_view/button_right_text', '' ), | |
| 190 | + ]; | |
| 191 | + // let third party developer filter options. | |
| 192 | + $atts = apply_filters( 'rtsb/module/quick_view/button_params', $params ); | |
| 193 | + // set fragment options. | |
| 194 | + $atts['icon_html'] = apply_filters( 'rtsb/module/quick_view/icon_html', $icon_html, $atts ); | |
| 195 | + | |
| 196 | + Fns::load_template( 'quick-view/button', $atts ); | |
| 197 | + } | |
| 198 | + | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * Wishlist button Shortcode callable function | |
| 202 | + * | |
| 203 | + * @param array $atts | |
| 204 | + * @param string $content | |
| 205 | + * | |
| 206 | + * @return string [HTML] | |
| 207 | + */ | |
| 208 | + public function button_shortcode( $atts, $content = '' ) { | |
| 209 | + $this->enqueue(); | |
| 210 | + global $product; | |
| 211 | + ob_start(); | |
| 212 | + | |
| 213 | + do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() ); | |
| 214 | + | |
| 215 | + return ob_get_clean(); | |
| 216 | + } | |
| 217 | + | |
| 218 | + public function enqueue() { | |
| 219 | + wp_enqueue_script( 'wc-add-to-cart-variation' ); | |
| 220 | + if ( version_compare( WC()->version, '3.0.0', '>=' ) ) { | |
| 221 | + if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { | |
| 222 | + wp_enqueue_script( 'zoom' ); | |
| 223 | + } | |
| 224 | + if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) { | |
| 225 | + wp_enqueue_script( 'photoswipe-ui-default' ); | |
| 226 | + wp_enqueue_style( 'photoswipe-default-skin' ); | |
| 227 | + if ( has_action( 'wp_footer', 'woocommerce_photoswipe' ) === false ) { | |
| 228 | + add_action( 'wp_footer', 'woocommerce_photoswipe', 15 ); | |
| 229 | + } | |
| 230 | + } | |
| 231 | + wp_enqueue_script( 'wc-single-product' ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION ); | |
| 235 | + wp_register_script( | |
| 236 | + 'rtsb-quick-view', | |
| 237 | + rtsb()->get_assets_uri( 'modules/quick-view.js' ), | |
| 238 | + [ | |
| 239 | + 'jquery', | |
| 240 | + 'rtsb-public', | |
| 241 | + ], | |
| 242 | + RTSB_VERSION, | |
| 243 | + true | |
| 244 | + ); | |
| 245 | + wp_enqueue_style( 'rtsb-modules' ); | |
| 246 | + wp_enqueue_script( 'rtsb-quick-view' ); | |
| 247 | + | |
| 248 | + // Allow user to load custom style and scripts! | |
| 249 | + do_action( 'rtsb/modules/quick_view/custom_scripts' ); | |
| 250 | + | |
| 251 | + $params = apply_filters( | |
| 252 | + 'rtsb/module/quick_view/js_params', | |
| 253 | + [ | |
| 254 | + 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ), | |
| 255 | + 'lang' => defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '', | |
| 256 | + 'isLoggedIn' => is_user_logged_in(), | |
| 257 | + ] | |
| 258 | + ); | |
| 259 | + | |
| 260 | + wp_localize_script( 'rtsb-quick-view', 'rtsbQvParams', $params ); | |
| 261 | + } | |
| 262 | + | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Add ATW button to Products block item | |
| 266 | + * | |
| 267 | + * @param string $item_html HTML of the single block item. | |
| 268 | + * @param array $data Data used to render the item. | |
| 269 | + * @param WC_Product $product Current product. | |
| 270 | + * | |
| 271 | + * @return string Filtered HTML. | |
| 272 | + */ | |
| 273 | + public function add_button_for_block( $item_html, $data, $product ) { | |
| 274 | + // Add the link "Add to wishlist" in the loop. | |
| 275 | + $shop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); | |
| 276 | + ob_start(); | |
| 277 | + $this->print_button( $product->get_id() ); | |
| 278 | + $button = ob_get_clean(); | |
| 279 | + $parts = []; | |
| 280 | + | |
| 281 | + preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts ); | |
| 282 | + | |
| 283 | + if ( ! $parts || count( $parts ) < 5 ) { | |
| 284 | + return $item_html; | |
| 285 | + } | |
| 286 | + | |
| 287 | + // removes first match (entire match). | |
| 288 | + array_shift( $parts ); | |
| 289 | + | |
| 290 | + // removes empty parts. | |
| 291 | + $parts = array_filter( $parts ); | |
| 292 | + | |
| 293 | + // searches for index to cut parts array. | |
| 294 | + switch ( $shop_btn_position ) { | |
| 295 | + case 'top_thumbnail': | |
| 296 | + $index = 1; | |
| 297 | + break; | |
| 298 | + case 'before_add_to_cart': | |
| 299 | + $index = 2; | |
| 300 | + break; | |
| 301 | + case 'after_add_to_cart': | |
| 302 | + $index = 3; | |
| 303 | + break; | |
| 304 | + default: | |
| 305 | + $index = 0; | |
| 306 | + break; | |
| 307 | + } | |
| 308 | + | |
| 309 | + // if index is found, stitch button in correct position. | |
| 310 | + if ( $index ) { | |
| 311 | + $first_set = array_slice( $parts, 0, $index ); | |
| 312 | + $second_set = array_slice( $parts, $index ); | |
| 313 | + | |
| 314 | + $parts = array_merge( | |
| 315 | + $first_set, | |
| 316 | + (array) $button, | |
| 317 | + $second_set | |
| 318 | + ); | |
| 319 | + | |
| 320 | + // replace li classes. | |
| 321 | + $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 rtsb-quick-view-btn-' . $shop_btn_position . '"', $parts[0] ); | |
| 322 | + } | |
| 323 | + | |
| 324 | + // join all parts together and return item. | |
| 325 | + return implode( '', $parts ); | |
| 326 | + } | |
| 327 | +} | |