PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.0.0
ShopBuilder – WooCommerce Builder For Elementor v2.0.0
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 2.0.0, at app/Modules/QuickView/QuickViewFrontEnd.php

328 lines 9.7 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 // Template.
21
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.
24
25 add_action( 'rtsb/modules/quick_view/print_button', [ $this, 'print_button' ] );
26
27 // Load action for product template.
28 $this->quick_view_action_template();
29
30 // ShortCode.
31 add_shortcode( 'rtsb_quick_view_button', [ $this, 'button_shortcode' ] );
32
33 // Quick view AJAX.
34 add_action( 'wp_ajax_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] );
35
36 add_action( 'wp_ajax_nopriv_rtsb_load_product_quick_view', [ $this, 'product_quick_view_ajax' ] );
37
38 $this->attach_button();
39
40 }
41
42
43 /**
44 * Load wc action for quick view product template
45 *
46 * @access public
47 * @return void
48 * @since 1.0.0
49 */
50 public function quick_view_action_template() {
51
52 // Image.
53 add_action( 'rtsb/wcqv/product/image', 'woocommerce_show_product_sale_flash', 10 );
54 add_action( 'rtsb/wcqv/product/image', 'woocommerce_show_product_images', 20 );
55
56 // Summary.
57 add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_title', 5 );
58 add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_rating', 10 );
59 add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_price', 15 );
60 add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_excerpt', 20 );
61 add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_add_to_cart', 25 );
62 add_action( 'rtsb/wcqv/product/summary', 'woocommerce_template_single_meta', 30 );
63
64 }
65
66 public function product_quick_view_ajax() {
67 $product_id = absint( $_REQUEST['product_id'] );
68
69 if ( empty( $product_id ) ) {
70 wp_send_json_error( esc_html__( 'Product Id not found', 'shopbuilder' ) );
71 }
72
73 global $sitepress;
74
75 $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['lang'] ) ) : '';
76 if ( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress ) ) {
77 $sitepress->switch_lang( $lang, true );
78 }
79
80 // Set the main wp query for the product.
81 wp( 'p=' . $product_id . '&post_type=product' );
82
83 // Remove product thumbnails gallery.
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 // }
92 ob_start();
93 Fns::load_template( 'quick-view/content' );
94 $html = ob_get_contents(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
95 ob_end_clean();
96
97 wp_send_json_success( [ 'html' => $html ] );
98 }
99
100 /**
101 * Add the "Add to Wishlist" button. Needed to use in wp_head hook.
102 *
103 * @return void
104 * @since 1.0.0
105 */
106 public function attach_button() {
107
108 $positions = apply_filters(
109 'rtsb/module/quick_view/loop_btn_position',
110 [
111
112 'before_add_to_cart' => [
113 'hook' => 'woocommerce_after_shop_loop_item',
114 'priority' => 7,
115 ],
116 'after_add_to_cart' => [
117 'hook' => 'woocommerce_after_shop_loop_item',
118 'priority' => 15,
119 ],
120 'custom' => [
121 'hook' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_name', '' ),
122 'priority' => Fns::get_option( 'modules', 'quick_view', 'loop_custom_hook_priority', '' ),
123 ],
124 ]
125 );
126
127 // Add the link "Add to wishlist" in the loop.
128
129 $loop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' );
130
131 if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) {
132 add_action(
133 $positions[ $loop_btn_position ]['hook'],
134 [
135 $this,
136 'button_hook',
137 ],
138 isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : ''
139 );
140 }
141 // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility.
142 // Add the link "Quick view" for Gutenberg blocks.
143 add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 );
144 }
145
146 /**
147 * Print "Add to compare" button
148 *
149 * @return void
150 */
151 public function button_hook() {
152 if ( ! apply_filters( 'rtsb/module/quick_view/show_button', true ) ) {
153 return;
154 }
155 global $product;
156 if( $product instanceof WC_Product ) {
157 do_action('rtsb/modules/quick_view/print_button', $product->get_id());
158 }
159 }
160
161
162 /**
163 * @return string|void
164 */
165 public function print_button( $product_id = 0 ) {
166 global $product;
167
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 '';
174 }
175
176 $classes = []; // button class.
177 $button_text = Fns::get_option( 'modules', 'quick_view', 'button_text', esc_html__( 'Quick View', 'shopbuilder' ) );
178
179 $icon_html = '<i class="rtsb-icon rtsb-icon-eye"></i>';
180 // get product type.
181 $product_type = $current_product->get_type();
182 $params = [
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', '' ),
189 ];
190 // let third party developer filter options.
191 $atts = apply_filters( 'rtsb/module/quick_view/button_params', $params );
192 // set fragment options.
193 $atts['icon_html'] = apply_filters( 'rtsb/module/quick_view/icon_html', $icon_html, $atts );
194
195 Fns::load_template( 'quick-view/button', $atts );
196 }
197
198
199 /**
200 * Wishlist button Shortcode callable function
201 *
202 * @param array $atts
203 * @param string $content
204 *
205 * @return string [HTML]
206 */
207 public function button_shortcode( $atts, $content = '' ) {
208 $this->enqueue();
209 global $product;
210 if ( ! $product instanceof WC_Product ) {
211 return '';
212 }
213 ob_start();
214
215 do_action( 'rtsb/modules/quick_view/print_button', $product->get_id() );
216
217 return ob_get_clean();
218 }
219
220 public function enqueue() {
221 wp_enqueue_script( 'wc-add-to-cart-variation' );
222 if ( version_compare( WC()->version, '3.0.0', '>=' ) ) {
223 if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
224 wp_enqueue_script( 'zoom' );
225 }
226 if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
227 wp_enqueue_script( 'photoswipe-ui-default' );
228 wp_enqueue_style( 'photoswipe-default-skin' );
229 if ( has_action( 'wp_footer', 'woocommerce_photoswipe' ) === false ) {
230 add_action( 'wp_footer', 'woocommerce_photoswipe', 15 );
231 }
232 }
233 wp_enqueue_script( 'wc-single-product' );
234 }
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
250 // Allow user to load custom style and scripts!
251 do_action( 'rtsb/modules/quick_view/custom_scripts' );
252
253 $params = apply_filters(
254 'rtsb/module/quick_view/js_params',
255 [
256 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
257 'lang' => defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '',
258 'isLoggedIn' => is_user_logged_in(),
259 ]
260 );
261
262 wp_localize_script( 'rtsb-quick-view', 'rtsbQvParams', $params );
263 }
264
265
266 /**
267 * Add ATW button to Products block item
268 *
269 * @param string $item_html HTML of the single block item.
270 * @param array $data Data used to render the item.
271 * @param WC_Product $product Current product.
272 *
273 * @return string Filtered HTML.
274 */
275 public function add_button_for_block( $item_html, $data, $product ) {
276 // Add the link "Add to wishlist" in the loop.
277 $shop_btn_position = Fns::get_option( 'modules', 'quick_view', 'loop_btn_position', 'after_add_to_cart' );
278 ob_start();
279 $this->print_button( $product->get_id() );
280 $button = ob_get_clean();
281 $parts = [];
282
283 preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts );
284
285 if ( ! $parts || count( $parts ) < 5 ) {
286 return $item_html;
287 }
288
289 // removes first match (entire match).
290 array_shift( $parts );
291
292 // removes empty parts.
293 $parts = array_filter( $parts );
294
295 // searches for index to cut parts array.
296 switch ( $shop_btn_position ) {
297
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 }
328