PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.6.1
ShopBuilder – WooCommerce Builder For Elementor v2.6.1
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 / Compare / CompareFns.php

CompareFns.php in ShopBuilder – WooCommerce Builder For Elementor 2.6.1, at app/Modules/Compare/CompareFns.php

426 lines 11.8 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\Compare;
4
5 use RadiusTheme\SB\Helpers\Fns;
6 use RadiusTheme\SB\Traits\SingletonTrait;
7 use WP_Error;
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 CompareFns {
15
16 use SingletonTrait;
17
18 /**
19 * @return false|string
20 */
21 public function get_page_url() {
22 $page_id = Fns::get_option( 'modules', 'compare', 'page' );
23 return get_permalink( $page_id );
24 }
25
26 /**
27 * Get Fields List
28 *
29 * @return array
30 */
31 static function get_available_list_fields() {
32 $attribute_list = [];
33
34 if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
35 $attribute_list = wc_get_attribute_taxonomies();
36 }
37
38 $fields = self::get_list_default_fields();
39
40 if ( count( $attribute_list ) > 0 ) {
41 foreach ( $attribute_list as $attribute ) {
42 $fields[ 'pa_' . $attribute->attribute_name ] = $attribute->attribute_label;
43 }
44 }
45
46 return $fields;
47 }
48
49
50 /**
51 * Get default fields List
52 * return array
53 */
54 static function get_list_default_fields() {
55 $fields = [
56 'title' => esc_html__( 'Title', 'shopbuilder' ),
57 'rating' => esc_html__( 'Rating', 'shopbuilder' ),
58 'price' => esc_html__( 'Price', 'shopbuilder' ),
59 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ),
60 'description' => esc_html__( 'Description', 'shopbuilder' ),
61 'availability' => esc_html__( 'Availability', 'shopbuilder' ),
62 'sku' => esc_html__( 'Sku', 'shopbuilder' ),
63 'weight' => esc_html__( 'Weight', 'shopbuilder' ),
64 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ),
65 ];
66
67 return apply_filters( 'rtsb/module/compare/table_default_fields', $fields );
68 }
69
70
71 /**
72 * Table field list
73 *
74 * @return array Table Field list
75 */
76 static function get_list_field_ids() {
77
78 $default_show = array_keys( self::get_list_default_fields() );
79 $fields_settings = Fns::get_option( 'modules', 'compare', 'page_show_fields', [] );
80 if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) {
81 $fields = $fields_settings;
82 } else {
83 $fields = $default_show;
84 }
85
86 return $fields;
87 }
88
89 /**
90 * @param $field
91 *
92 * @return mixed|string|void
93 */
94 public function field_name( $field ) {
95
96 if ( empty( $field ) ) {
97 return;
98 }
99
100 $default = self::get_list_default_fields();
101
102 $str = substr( $field, 0, 3 );
103 if ( 'pa_' === $str ) {
104 $field_name = wc_attribute_label( $field );
105 } else {
106 $field_name = isset( $default[ $field ] ) ? $default[ $field ] : '';
107 }
108
109 return $field_name;
110 }
111
112 public function display_field( $field_id, $product ) {
113
114 $type = $field_id;
115 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
116 $type = 'attribute';
117 }
118 switch ( $type ) {
119 case 'primary':
120 ?>
121 <div class="rtsb-compare-primary-content-area">
122 <a href="#" class="rtsb-compare-remove" data-product_id="<?php echo absint( $product['id'] ); ?>">
123 <i class="rtsb-icon rtsb-icon-trash-empty"></i>
124 </a>
125 <a href="<?php echo esc_url( get_permalink( $product['id'] ) ); ?>" class="rtsb-compare-product-image">
126 <?php
127 if ( ! empty( $product['primary']['image'] ) ) {
128 echo wp_kses( $product['primary']['image'], Fns::allowedHtml( 'image' ) );
129 }
130 ?>
131 </a>
132 </div>
133 <?php
134 break;
135
136 case 'title':
137 echo '<a href="' . esc_url( get_permalink( $product['id'] ) ) . '" class="rtsb-compare-product-title">' . esc_html( $product[ $field_id ] ) . '</a>';
138 break;
139
140 case 'rating':
141 echo '<span class="rtsb-compare-product-rating">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
142 break;
143
144 case 'price':
145 echo '<div class="rtsb-compare-product-price">' . wp_kses_post( $product[ $field_id ] ) . '</div>';
146 break;
147
148 case 'add_to_cart':
149 Fns::print_html( apply_filters( 'rtsb/module/compare/add_to_cart_btn', $product[ $field_id ] ), true );
150 break;
151
152 case 'weight':
153 if ( $product[ $field_id ] ) {
154 $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
155 Fns::print_html( wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ) );
156 }
157 break;
158
159 case 'description':
160 Fns::print_html( apply_filters( 'woocommerce_short_description', $product[ $field_id ] ) );
161 break;
162 case 'attribute':
163 if ( isset( $product[ $field_id ] ) ) {
164 Fns::print_html( $product[ $field_id ] );
165 }
166 break;
167 default:
168 echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : '';
169 break;
170 }
171 }
172
173 public function get_products_data() {
174 $ids = $this->get_compared_product_ids();
175
176 // For shareable link
177 if ( empty( $ids ) ) {
178 return [];
179 }
180
181 $args = [
182 'include' => $ids,
183 ];
184
185 $products = wc_get_products( $args );
186
187 $products_data = [];
188
189 $field_ids = $this->get_list_field_ids();
190 $tax_field_ids = array_filter(
191 $field_ids,
192 function ( $field_id ) {
193 return str_starts_with( $field_id, 'pa_' );
194 }
195 );
196
197 $data_none = '-';
198
199 foreach ( $products as $product ) {
200
201 $rating_count = $product->get_rating_count();
202 $average = $product->get_average_rating();
203
204 $products_data[ $product->get_id() ] = [
205 'primary' => [
206 'image' => $product->get_image() ? $product->get_image( 'ever-compare-image' ) : $data_none,
207 ],
208 'id' => $product->get_id(),
209 'title' => $product->get_title() ? $product->get_title() : $data_none,
210 'image_id' => $product->get_image_id(),
211 'permalink' => $product->get_permalink(),
212 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
213 'rating' => wc_get_rating_html( $average, $rating_count ),
214 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none,
215 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
216 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
217 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
218 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
219 'availability' => $this->availability_html( $product ),
220 ];
221 if ( ! empty( $tax_field_ids ) ) {
222 foreach ( $tax_field_ids as $field_id ) {
223 if ( taxonomy_exists( $field_id ) ) {
224 $products_data[ $product->get_id() ][ $field_id ] = [];
225 $terms = get_the_terms( $product->get_id(), $field_id );
226 if ( ! empty( $terms ) ) {
227 foreach ( $terms as $term ) {
228 $term = sanitize_term( $term, $field_id );
229 $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
230 }
231 } else {
232 $products_data[ $product->get_id() ][ $field_id ][] = '-';
233 }
234 $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
235 }
236 }
237 }
238 }
239
240 return $products_data;
241 }
242
243 /**
244 * @param $product
245 *
246 * @return mixed|void|null
247 */
248 public function add_to_cart_html( $product ) {
249
250 $defaults = [
251 'quantity' => 1,
252 'class' => implode(
253 ' ',
254 array_filter(
255 [
256 'rtsb-compare-cart-button button',
257 'product_type_' . $product->get_type(),
258 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
259 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
260 ]
261 )
262 ),
263 'attributes' => [
264 'data-product_id' => $product->get_id(),
265 'data-product_sku' => $product->get_sku(),
266 'aria-label' => $product->add_to_cart_description(),
267 'rel' => 'nofollow',
268 ],
269 ];
270
271 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
272
273 if ( isset( $args['attributes']['aria-label'] ) ) {
274 $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
275 }
276
277 return apply_filters(
278 'woocommerce_loop_add_to_cart_link',
279 sprintf(
280 '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
281 esc_url( $product->add_to_cart_url() ),
282 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
283 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
284 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
285 esc_html( $product->add_to_cart_text() )
286 ),
287 $product,
288 $args
289 );
290 }
291
292 public function availability_html( $product ) {
293 $html = '';
294 $availability = $product->get_availability();
295
296 if ( empty( $availability['availability'] ) ) {
297 $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' );
298 }
299
300 if ( ! empty( $availability['availability'] ) ) {
301 ob_start();
302
303 wc_get_template(
304 'single-product/stock.php',
305 [
306 'product' => $product,
307 'class' => $availability['class'],
308 'availability' => $availability['availability'],
309 ]
310 );
311
312 $html = ob_get_clean();
313 }
314
315 return apply_filters( 'woocommerce_get_stock_html', $html, $product );
316 }
317
318 public function add_product( $id ) {
319 $cookie_name = $this->get_cookie_name();
320
321 if ( $this->is_exists_in_list( $id ) ) {
322 return new \WP_Error( 'rtsb-compare-product-exist', esc_html__( 'Product already in compare list!.', 'shopbuilder' ) );
323 }
324
325 $product_ids = $this->get_compared_product_ids();
326
327 // Reached Maximum Limit
328 if ( $this->reached_max_limit() ) {
329 return new \WP_Error( 'rtsb-compare-reached_max_limit', esc_html__( 'Reached maximum number of compare!.', 'shopbuilder' ) );
330 }
331
332 $product_ids[] = $id;
333
334 setcookie( $cookie_name, wp_json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
335
336 $_COOKIE[ $cookie_name ] = wp_json_encode( $product_ids );
337
338 return true;
339 }
340
341 /**
342 * Remove product from compare list
343 *
344 * @param integer $product_id
345 *
346 * @return bool|WP_Error
347 */
348 public function remove_product( $product_id ) {
349
350 if ( ! $this->is_exists_in_list( $product_id ) ) {
351 return new WP_Error( 'rtsb_compare_product_not_exist', esc_html__( 'Product not in compare list!.', 'shopbuilder' ) );
352 }
353 $delete_status = false;
354 $product_ids = $this->get_compared_product_ids();
355 if ( is_array( $product_ids ) && ! empty( $product_ids ) ) {
356 foreach ( $product_ids as $prod_key => $productId ) {
357 if ( $productId == $product_id ) {
358 unset( $product_ids[ $prod_key ] );
359 $delete_status = true;
360 break;
361 }
362 }
363 }
364
365 $cookie_name = $this->get_cookie_name();
366
367 setcookie( $cookie_name, wp_json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
368 $_COOKIE[ $cookie_name ] = wp_json_encode( $product_ids );
369
370 return $delete_status;
371 }
372
373 public function get_cookie_name() {
374 $name = 'rtsb_compare_list';
375 if ( is_multisite() ) {
376 $name .= '_' . get_current_blog_id();
377 }
378
379 return $name;
380 }
381
382 /**
383 * @param $id
384 *
385 * @return bool
386 */
387 public function is_exists_in_list( $id ) {
388 $list = $this->get_compared_product_ids();
389
390 return in_array( $id, $list );
391 }
392
393 public function get_compared_product_ids() {
394 $cookie_name = $this->get_cookie_name();
395 $ids = [];
396
397 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
398 if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
399 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
400 $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
401 }
402
403 return $ids;
404 }
405
406 public function reached_max_limit() {
407
408 $product_ids = $this->get_compared_product_ids();
409 $max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) );
410
411 // Remove if product is not exist.
412 foreach ( $product_ids as $id ) {
413 if ( 'publish' !== get_post_status( $id ) ) {
414 $this->remove_product( $id );
415 unset( $product_ids[ $id ] );
416 }
417 }
418
419 if ( $max_limit && count( $product_ids ) >= $max_limit ) {
420 return true;
421 }
422
423 return false;
424 }
425 }
426