| @@ -1,10 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Quick View Module Class. | |
| 4 | - * | |
| 5 | - * @package RadiusTheme\SB | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | namespace RadiusTheme\SB\Modules\QuickView; |
| 9 | 4 | |
| 10 | 5 | use RadiusTheme\SB\Helpers\Fns; |
| @@ -15,34 +10,20 @@ | ||
| 15 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | 11 | exit( 'This script cannot be accessed directly.' ); |
| 17 | 12 | } |
| 18 | 13 | |
| 19 | -/** | |
| 20 | - * Quick View Module Class. | |
| 21 | - */ | |
| 22 | 14 | class QuickViewFrontEnd { |
| 23 | 15 | use SingletonTrait; |
| 24 | 16 | |
| 25 | - /** | |
| 26 | - * Asset Handle | |
| 27 | - * | |
| 28 | - * @var string | |
| 29 | - */ | |
| 30 | - private $handle = 'rtsb-quick-view'; | |
| 31 | - | |
| 32 | - /** | |
| 33 | - * Class constructor. | |
| 34 | - */ | |
| 35 | 17 | public function __construct() { |
| 36 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ], 99 ); | |
| 18 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 37 | 19 | |
| 38 | - // Gallery Plugin Support rtwpvg_disable_enqueue_scripts. | |
| 39 | - add_filter( 'rtwpvg_disable_enqueue_scripts', '__return_false', 11 ); | |
| 20 | + // Template. | |
| 40 | 21 | |
| 41 | - // Template. | |
| 42 | - add_action( 'rtsb/modules/quick_view/frontend/display', [ $this, 'button_hook' ] ); | |
| 22 | + add_action( 'rtsb/modules/quick_view/frontend/display', [ $this, 'button_hook' ] ); | |
| 23 | + // Add do_action( 'rtsb/modules/quick_view/frontend/display' ); for display anywhere. | |
| 43 | 24 | |
| 44 | - add_action( 'rtsb/modules/quick_view/print_button', [ $this, 'print_button' ] ); | |
| 25 | + add_action( 'rtsb/modules/quick_view/print_button', [ $this, 'print_button' ] ); | |
| 45 | 26 | |
| 46 | 27 | // Load action for product template. |
| 47 | 28 | $this->quick_view_action_template(); |
| 48 | 29 | |
| @@ -54,8 +35,9 @@ | ||
| 54 | 35 | |
| 55 | 36 | add_action( 'wp_ajax_nopriv_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] ); |
| 56 | 37 | |
| 57 | 38 | $this->attach_button(); |
| 39 | + | |
| 58 | 40 | } |
| 59 | 41 | |
| 60 | 42 | |
| 61 | 43 | /** |
| @@ -65,9 +47,9 @@ | ||
| 65 | 47 | * @return void |
| 66 | 48 | * @since 1.0.0 |
| 67 | 49 | */ |
| 68 | 50 | public function quick_view_action_template() { |
| 69 | - wp_enqueue_style( 'elementor-frontend' ); | |
| 51 | + | |
| 70 | 52 | // Image. |
| 71 | 53 | add_action( 'rtsb/wcqv/product/image', 'woocommerce_show_product_sale_flash', 10 ); |
| 72 | 54 | add_action( 'rtsb/wcqv/product/image', 'woocommerce_show_product_images', 20 ); |
| 73 | 55 | |
| @@ -77,25 +59,21 @@ | ||
| 77 | 59 | add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_price', 15 ); |
| 78 | 60 | add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_excerpt', 20 ); |
| 79 | 61 | add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_add_to_cart', 25 ); |
| 80 | 62 | add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_meta', 30 ); |
| 63 | + | |
| 81 | 64 | } |
| 82 | 65 | |
| 83 | - /** | |
| 84 | - * Quick View AJAX. | |
| 85 | - * | |
| 86 | - * @return void | |
| 87 | - */ | |
| 88 | 66 | public function product_quick_view_ajax() { |
| 89 | - $product_id = isset( $_REQUEST['product_id'] ) ? absint( $_REQUEST['product_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 67 | + $product_id = absint( $_REQUEST['product_id'] ); | |
| 90 | 68 | |
| 91 | - if ( empty( $product_id ) || 'publish' !== get_post_status( $product_id ) ) { | |
| 69 | + if ( empty( $product_id ) ) { | |
| 92 | 70 | wp_send_json_error( esc_html__( 'Product Id not found', 'shopbuilder' ) ); |
| 93 | 71 | } |
| 94 | 72 | |
| 95 | 73 | global $sitepress; |
| 96 | 74 | |
| 97 | - $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['lang'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 75 | + $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['lang'] ) ) : ''; | |
| 98 | 76 | if ( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress ) ) { |
| 99 | 77 | $sitepress->switch_lang( $lang, true ); |
| 100 | 78 | } |
| 101 | 79 | |
| @@ -102,12 +80,16 @@ | ||
| 102 | 80 | // Set the main wp query for the product. |
| 103 | 81 | wp( 'p=' . $product_id . '&post_type=product' ); |
| 104 | 82 | |
| 105 | 83 | // Remove product thumbnails gallery. |
| 106 | - if ( defined( 'RTWPVG_VERSION' ) ) { | |
| 107 | - remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 ); | |
| 108 | - } | |
| 109 | - | |
| 84 | + remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 ); | |
| 85 | + // Change template for variable products. | |
| 86 | + // if ( isset( $GLOBALS['yith_wccl'] ) ) { | |
| 87 | + // $GLOBALS['yith_wccl']->obj = new YITH_WCCL_Frontend(); | |
| 88 | + // $GLOBALS['yith_wccl']->obj->override(); | |
| 89 | + // } elseif ( defined( 'YITH_WCCL_PREMIUM' ) && YITH_WCCL_PREMIUM && class_exists( 'YITH_WCCL_Frontend' ) ) { | |
| 90 | + // $attributes = YITH_WCCL_Frontend()->create_attributes_json( $product_id, true ); | |
| 91 | + // } | |
| 110 | 92 | ob_start(); |
| 111 | 93 | Fns::load_template( 'quick-view/content' ); |
| 112 | 94 | $html = ob_get_contents(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 113 | 95 | ob_end_clean(); |
| @@ -121,8 +103,9 @@ | ||
| 121 | 103 | * @return void |
| 122 | 104 | * @since 1.0.0 |
| 123 | 105 | */ |
| 124 | 106 | public function attach_button() { |
| 107 | + | |
| 125 | 108 | $positions = apply_filters( |
| 126 | 109 | 'rtsb/module/quick_view/loop_btn_position', |
| 127 | 110 | [ |
| 128 | 111 | |
| @@ -134,9 +117,9 @@ | ||
| 134 | 117 | 'hook' => 'woocommerce_after_shop_loop_item', |
| 135 | 118 | 'priority' => 15, |
| 136 | 119 | ], |
| 137 | 120 | 'custom' => [ |
| 138 | - 'hook' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_name', '' ), | |
| 121 | + 'hook' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_name', '' ), | |
| 139 | 122 | 'priority' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_priority', '' ), |
| 140 | 123 | ], |
| 141 | 124 | ] |
| 142 | 125 | ); |
| @@ -141,8 +124,9 @@ | ||
| 141 | 124 | ] |
| 142 | 125 | ); |
| 143 | 126 | |
| 144 | 127 | // Add the link "Add to wishlist" in the loop. |
| 128 | + | |
| 145 | 129 | $loop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); |
| 146 | 130 | |
| 147 | 131 | if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) { |
| 148 | 132 | add_action( |
| @@ -153,9 +137,9 @@ | ||
| 153 | 137 | ], |
| 154 | 138 | isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : '' |
| 155 | 139 | ); |
| 156 | 140 | } |
| 157 | - | |
| 141 | + // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility. | |
| 158 | 142 | // Add the link "Quick view" for Gutenberg blocks. |
| 159 | 143 | add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 ); |
| 160 | 144 | } |
| 161 | 145 | |
| @@ -167,48 +151,42 @@ | ||
| 167 | 151 | public function button_hook() { |
| 168 | 152 | if ( ! apply_filters( 'rtsb/module/quick_view/show_button', true ) ) { |
| 169 | 153 | return; |
| 170 | 154 | } |
| 171 | - global $product; | |
| 172 | - if ( $product instanceof WC_Product ) { | |
| 173 | - do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() ); | |
| 174 | - } | |
| 155 | + global $product; | |
| 156 | + if( $product instanceof WC_Product ) { | |
| 157 | + do_action('rtsb/modules/quick_view/print_button', $product->get_id()); | |
| 158 | + } | |
| 175 | 159 | } |
| 176 | 160 | |
| 177 | 161 | |
| 178 | 162 | /** |
| 179 | - * Print "Add to compare" button. | |
| 180 | - * | |
| 181 | - * @param int $product_id Product ID. | |
| 182 | - * | |
| 183 | - * @return void | |
| 163 | + * @return string|void | |
| 184 | 164 | */ |
| 185 | 165 | public function print_button( $product_id = 0 ) { |
| 186 | 166 | global $product; |
| 187 | 167 | |
| 188 | - if ( ! $product instanceof WC_Product && $product_id ) { | |
| 189 | - $product = wc_get_product( $product_id ); | |
| 168 | + // product object. | |
| 169 | + $current_product = $product_id ? wc_get_product( $product_id ) : false; | |
| 170 | + $current_product = $current_product ?: $product; | |
| 171 | + | |
| 172 | + if ( ! $current_product instanceof WC_Product ) { | |
| 173 | + return ''; | |
| 190 | 174 | } |
| 191 | 175 | |
| 192 | - $classes = []; | |
| 193 | - $button_text = Fns::get_option( 'modules', 'quick_view', 'button_text', esc_html__( 'Quick View', 'shopbuilder' ) ); | |
| 194 | - $has_button_text = ! empty( Fns::get_option( 'modules', 'quick_view', 'button_text', '' ) ); | |
| 176 | + $classes = []; // button class. | |
| 177 | + $button_text = Fns::get_option( 'modules', 'quick_view', 'button_text', esc_html__( 'Quick View', 'shopbuilder' ) ); | |
| 195 | 178 | |
| 196 | - if ( $has_button_text ) { | |
| 197 | - $classes[] = 'has-text'; | |
| 198 | - } | |
| 199 | - | |
| 200 | - $icon_html = '<i class="rtsb-icon rtsb-icon-eye"></i>'; | |
| 179 | + $icon_html = '<i class="rtsb-icon rtsb-icon-eye"></i>'; | |
| 201 | 180 | // get product type. |
| 202 | - $product_type = $product->get_type(); | |
| 181 | + $product_type = $current_product->get_type(); | |
| 203 | 182 | $params = [ |
| 204 | - 'classes' => $classes, | |
| 205 | - 'product_id' => $product->get_id(), | |
| 206 | - 'product_type' => $product_type, | |
| 207 | - 'button_text' => $button_text, | |
| 208 | - 'has_button_text' => $has_button_text, | |
| 209 | - 'left_text' => apply_filters( 'rtsb/module/quick_view/button_left_text', '' ), | |
| 210 | - 'right_text' => apply_filters( 'rtsb/module/quick_view/button_right_text', '' ), | |
| 183 | + 'classes' => $classes, | |
| 184 | + 'product_id' => $current_product->get_id(), | |
| 185 | + 'product_type' => $product_type, | |
| 186 | + 'button_text' => $button_text, | |
| 187 | + 'left_text' => apply_filters( 'rtsb/module/quick_view/button_left_text', '' ), | |
| 188 | + 'right_text' => apply_filters( 'rtsb/module/quick_view/button_right_text', '' ), | |
| 211 | 189 | ]; |
| 212 | 190 | // let third party developer filter options. |
| 213 | 191 | $atts = apply_filters( 'rtsb/module/quick_view/button_params', $params ); |
| 214 | 192 | // set fragment options. |
| @@ -216,51 +194,39 @@ | ||
| 216 | 194 | |
| 217 | 195 | Fns::load_template( 'quick-view/button', $atts ); |
| 218 | 196 | } |
| 219 | 197 | |
| 198 | + | |
| 220 | 199 | /** |
| 221 | 200 | * Wishlist button Shortcode callable function |
| 222 | 201 | * |
| 223 | - * @return string | |
| 202 | + * @param array $atts | |
| 203 | + * @param string $content | |
| 204 | + * | |
| 205 | + * @return string [HTML] | |
| 224 | 206 | */ |
| 225 | - public function button_shortcode() { | |
| 226 | - global $product; | |
| 227 | - | |
| 207 | + public function button_shortcode( $atts, $content = '' ) { | |
| 208 | + $this->enqueue(); | |
| 209 | + global $product; | |
| 228 | 210 | if ( ! $product instanceof WC_Product ) { |
| 229 | 211 | return ''; |
| 230 | 212 | } |
| 213 | + ob_start(); | |
| 231 | 214 | |
| 232 | - ob_start(); | |
| 233 | - do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() ); | |
| 215 | + do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() ); | |
| 234 | 216 | |
| 235 | - return ob_get_clean(); | |
| 217 | + return ob_get_clean(); | |
| 236 | 218 | } |
| 237 | 219 | |
| 238 | - /** | |
| 239 | - * Enqueue assets. | |
| 240 | - * | |
| 241 | - * @return void | |
| 242 | - */ | |
| 243 | 220 | public function enqueue() { |
| 244 | - wp_enqueue_style( 'elementor-icons-fa-regular' ); | |
| 245 | - wp_enqueue_style( 'elementor-icons-fa-solid' ); | |
| 246 | - wp_enqueue_style( 'elementor-icons-shared-0' ); | |
| 247 | - | |
| 248 | - if ( ! class_exists( 'WooProductVariationGallery' ) && current_theme_supports( 'wc-product-gallery-slider' ) ) { | |
| 249 | - wp_enqueue_script( 'wc-flexslider' ); | |
| 250 | - } | |
| 251 | - | |
| 252 | 221 | wp_enqueue_script( 'wc-add-to-cart-variation' ); |
| 253 | - | |
| 254 | 222 | if ( version_compare( WC()->version, '3.0.0', '>=' ) ) { |
| 255 | 223 | if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { |
| 256 | - wp_enqueue_script( 'wc-zoom' ); | |
| 224 | + wp_enqueue_script( 'zoom' ); | |
| 257 | 225 | } |
| 258 | - | |
| 259 | 226 | if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) { |
| 260 | - wp_enqueue_script( 'wc-photoswipe-ui-default' ); | |
| 227 | + wp_enqueue_script( 'photoswipe-ui-default' ); | |
| 261 | 228 | wp_enqueue_style( 'photoswipe-default-skin' ); |
| 262 | - | |
| 263 | 229 | if ( has_action( 'wp_footer', 'woocommerce_photoswipe' ) === false ) { |
| 264 | 230 | add_action( 'wp_footer', 'woocommerce_photoswipe', 15 ); |
| 265 | 231 | } |
| 266 | 232 | } |
| @@ -265,10 +231,23 @@ | ||
| 265 | 231 | } |
| 266 | 232 | } |
| 267 | 233 | wp_enqueue_script( 'wc-single-product' ); |
| 268 | 234 | } |
| 269 | - // Enqueue assets. | |
| 270 | - $this->handle = Fns::enqueue_module_assets( $this->handle, 'quick-view' ); | |
| 235 | + | |
| 236 | + wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION ); | |
| 237 | + wp_register_script( | |
| 238 | + 'rtsb-quick-view', | |
| 239 | + rtsb()->get_assets_uri( 'modules/quick-view.js' ), | |
| 240 | + [ | |
| 241 | + 'jquery', | |
| 242 | + 'rtsb-public', | |
| 243 | + ], | |
| 244 | + RTSB_VERSION, | |
| 245 | + true | |
| 246 | + ); | |
| 247 | + wp_enqueue_style( 'rtsb-modules' ); | |
| 248 | + wp_enqueue_script( 'rtsb-quick-view' ); | |
| 249 | + | |
| 271 | 250 | // Allow user to load custom style and scripts! |
| 272 | 251 | do_action( 'rtsb/modules/quick_view/custom_scripts' ); |
| 273 | 252 | |
| 274 | 253 | $params = apply_filters( |
| @@ -279,9 +258,9 @@ | ||
| 279 | 258 | 'isLoggedIn' => is_user_logged_in(), |
| 280 | 259 | ] |
| 281 | 260 | ); |
| 282 | 261 | |
| 283 | - wp_localize_script( $this->handle, 'rtsbQvParams', $params ); | |
| 262 | + wp_localize_script( 'rtsb-quick-view', 'rtsbQvParams', $params ); | |
| 284 | 263 | } |
| 285 | 264 | |
| 286 | 265 | |
| 287 | 266 | /** |
| @@ -294,9 +273,9 @@ | ||
| 294 | 273 | * @return string Filtered HTML. |
| 295 | 274 | */ |
| 296 | 275 | public function add_button_for_block( $item_html, $data, $product ) { |
| 297 | 276 | // Add the link "Add to wishlist" in the loop. |
| 298 | - $shop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); | |
| 277 | + $shop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' ); | |
| 299 | 278 | ob_start(); |
| 300 | 279 | $this->print_button( $product->get_id() ); |
| 301 | 280 | $button = ob_get_clean(); |
| 302 | 281 | $parts = []; |