PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.4
ShopBuilder – WooCommerce Builder For Elementor v3.2.4
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / QuickView / QuickViewFrontEnd.php

QuickViewFrontEnd.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.4, at app/Modules/QuickView/QuickViewFrontEnd.php

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