PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.0
ShopBuilder – WooCommerce Builder For Elementor v3.2.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 / Compare / CompareFns.php

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

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