PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.6
ShopBuilder – WooCommerce Builder For Elementor v2.1.6
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 2.3.0 All 62 releases
shopbuilder / app / Modules / QuickView / QuickViewFrontEnd.php

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

351 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ) ) {
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 * @return string|void
187 */
188 public function print_button( $product_id = 0 ) {
189 global $product;
190
191 if ( ! $product instanceof WC_Product && $product_id ) {
192 $product = wc_get_product( $product_id );
193 }
194
195 // if ( ! $current_product instanceof WC_Product ) {
196 // return '';
197 // }
198
199 $classes = []; // button class.
200 $button_text = Fns::get_option( 'modules', 'quick_view', 'button_text', esc_html__( 'Quick View', 'shopbuilder' ) );
201
202 $icon_html = '<i class="rtsb-icon rtsb-icon-eye"></i>';
203 // get product type.
204 $product_type = $product->get_type();
205 $params = [
206 'classes' => $classes,
207 'product_id' => $product->get_id(),
208 'product_type' => $product_type,
209 'button_text' => $button_text,
210 'left_text' => apply_filters( 'rtsb/module/quick_view/button_left_text', '' ),
211 'right_text' => apply_filters( 'rtsb/module/quick_view/button_right_text', '' ),
212 ];
213 // let third party developer filter options.
214 $atts = apply_filters( 'rtsb/module/quick_view/button_params', $params );
215 // set fragment options.
216 $atts['icon_html'] = apply_filters( 'rtsb/module/quick_view/icon_html', $icon_html, $atts );
217
218 Fns::load_template( 'quick-view/button', $atts );
219 }
220
221
222 /**
223 * Wishlist button Shortcode callable function
224 *
225 * @param array $atts
226 * @param string $content
227 *
228 * @return string [HTML]
229 */
230 public function button_shortcode( $atts, $content = '' ) {
231 $this->enqueue();
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 wp_enqueue_script( 'wc-add-to-cart-variation' );
245 if ( version_compare( WC()->version, '3.0.0', '>=' ) ) {
246 if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
247 wp_enqueue_script( 'zoom' );
248 }
249 if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
250 wp_enqueue_script( 'photoswipe-ui-default' );
251 wp_enqueue_style( 'photoswipe-default-skin' );
252 if ( has_action( 'wp_footer', 'woocommerce_photoswipe' ) === false ) {
253 add_action( 'wp_footer', 'woocommerce_photoswipe', 15 );
254 }
255 }
256 wp_enqueue_script( 'wc-single-product' );
257 }
258
259 wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION );
260 wp_register_script(
261 'rtsb-quick-view',
262 rtsb()->get_assets_uri( 'modules/quick-view.js' ),
263 [
264 'jquery',
265 'rtsb-public',
266 ],
267 RTSB_VERSION,
268 true
269 );
270 wp_enqueue_style( 'rtsb-modules' );
271 wp_enqueue_script( 'rtsb-quick-view' );
272
273 // Allow user to load custom style and scripts!
274 do_action( 'rtsb/modules/quick_view/custom_scripts' );
275
276 $params = apply_filters(
277 'rtsb/module/quick_view/js_params',
278 [
279 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
280 'lang' => defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '',
281 'isLoggedIn' => is_user_logged_in(),
282 ]
283 );
284
285 wp_localize_script( 'rtsb-quick-view', 'rtsbQvParams', $params );
286 }
287
288
289 /**
290 * Add ATW button to Products block item
291 *
292 * @param string $item_html HTML of the single block item.
293 * @param array $data Data used to render the item.
294 * @param WC_Product $product Current product.
295 *
296 * @return string Filtered HTML.
297 */
298 public function add_button_for_block( $item_html, $data, $product ) {
299 // Add the link "Add to wishlist" in the loop.
300 $shop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' );
301 ob_start();
302 $this->print_button( $product->get_id() );
303 $button = ob_get_clean();
304 $parts = [];
305
306 preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts );
307
308 if ( ! $parts || count( $parts ) < 5 ) {
309 return $item_html;
310 }
311
312 // removes first match (entire match).
313 array_shift( $parts );
314
315 // removes empty parts.
316 $parts = array_filter( $parts );
317
318 // searches for index to cut parts array.
319 switch ( $shop_btn_position ) {
320
321 case 'before_add_to_cart':
322 $index = 2;
323 break;
324 case 'after_add_to_cart':
325 $index = 3;
326 break;
327 default:
328 $index = 0;
329 break;
330 }
331
332 // if index is found, stitch button in correct position.
333 if ( $index ) {
334 $first_set = array_slice( $parts, 0, $index );
335 $second_set = array_slice( $parts, $index );
336
337 $parts = array_merge(
338 $first_set,
339 (array) $button,
340 $second_set
341 );
342
343 // replace li classes.
344 $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 rtsb-quick-view-btn-' . $shop_btn_position . '"', $parts[0] );
345 }
346
347 // join all parts together and return item.
348 return implode( '', $parts );
349 }
350 }
351