PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.7
ShopBuilder – WooCommerce Builder For Elementor v2.1.7
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.1.7, at app/Modules/Compare/CompareFns.php

424 lines 11.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\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
113 public function display_field( $field_id, $product ) {
114
115 $type = $field_id;
116 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
117 $type = 'attribute';
118 }
119 switch ( $type ) {
120 case 'primary':
121 ?>
122 <div class="rtsb-compare-primary-content-area">
123 <a href="#" class="rtsb-compare-remove" data-product_id="<?php echo absint( $product['id'] ); ?>">
124 <i class="rtsb-icon rtsb-icon-trash-empty"></i>
125 </a>
126 <a href="<?php echo get_permalink( $product['id'] ); ?>" class="rtsb-compare-product-image">
127 <?php
128 if ( ! empty( $product['primary']['image'] ) ) {
129 echo wp_kses( $product['primary']['image'], Fns::allowedHtml( 'image' ) );
130 }
131 ?>
132 </a>
133 </div>
134 <?php
135 break;
136
137 case 'title':
138 echo '<a href="' . get_permalink( $product['id'] ) . '" class="rtsb-compare-product-title">' . esc_html( $product[ $field_id ] ) . '</a>';
139 break;
140
141 case 'rating':
142 echo '<span class="rtsb-compare-product-rating">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
143 break;
144
145 case 'price':
146 echo '<div class="rtsb-compare-product-price">' . wp_kses_post( $product[ $field_id ] ) . '</div>';
147 break;
148
149 case 'add_to_cart':
150 echo apply_filters( 'rtsb/module/compare/add_to_cart_btn', $product[ $field_id ] );
151 break;
152
153 case 'weight':
154 if ( $product[ $field_id ] ) {
155 $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
156 echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit );
157 }
158 break;
159
160 case 'description':
161 echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] );
162 break;
163
164 default:
165 echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : '';
166 break;
167 }
168 }
169
170 public function get_products_data() {
171 $ids = $this->get_compared_product_ids();
172
173 // For shareable link
174 if ( empty( $ids ) ) {
175 return [];
176 }
177
178 $args = [
179 'include' => $ids,
180 ];
181
182 $products = wc_get_products( $args );
183
184 $products_data = [];
185
186 $field_ids = $this->get_list_field_ids();
187
188 $tax_field_ids = array_filter(
189 $field_ids,
190 function ( $field_id ) {
191 return 'pa_' === substr( $field_id, 0, 3 );
192 },
193 ARRAY_FILTER_USE_KEY
194 );
195
196 $data_none = '-';
197
198 foreach ( $products as $product ) {
199
200 $rating_count = $product->get_rating_count();
201 $average = $product->get_average_rating();
202
203 $products_data[ $product->get_id() ] = [
204 'primary' => [
205 'image' => $product->get_image() ? $product->get_image( 'ever-compare-image' ) : $data_none,
206 ],
207 'id' => $product->get_id(),
208 'title' => $product->get_title() ? $product->get_title() : $data_none,
209 'image_id' => $product->get_image_id(),
210 'permalink' => $product->get_permalink(),
211 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
212 'rating' => wc_get_rating_html( $average, $rating_count ),
213 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none,
214 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
215 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
216 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
217 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
218 'availability' => $this->availability_html( $product ),
219 ];
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 if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
397 $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
398 }
399
400 return $ids;
401 }
402
403 public function reached_max_limit() {
404
405 $product_ids = $this->get_compared_product_ids();
406 $max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) );
407
408 // Remove if product is not exist.
409 foreach ( $product_ids as $id ) {
410 if( 'publish' !== get_post_status( $id )){
411 $this->remove_product( $id );
412 unset( $product_ids[$id] );
413 }
414 }
415
416 if ( $max_limit && count( $product_ids ) >= $max_limit ) {
417 return true;
418 }
419
420 return false;
421 }
422
423 }
424