PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
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
← All changes | app/Modules/Compare/CompareFns.php +91 -57 2.1.73.2.6 View file →
@@ -10,10 +10,15 @@
10 10 if ( ! defined( 'ABSPATH' ) ) {
11 11 exit( 'This script cannot be accessed directly.' );
12 12 }
13 13
14 +/**
15 + * Class CompareFns
16 + */
14 17 class CompareFns {
15 -
18 + /**
19 + * @var singleton
20 + */
16 21 use SingletonTrait;
17 22
18 23 /**
19 24 * @return false|string
@@ -18,10 +23,10 @@
18 23 /**
19 24 * @return false|string
20 25 */
21 26 public function get_page_url() {
22 - $page_id = Fns::get_option( 'modules', 'compare', 'page' );
23 - return get_permalink( $page_id );
27 + $page_id = Fns::get_option( 'modules', 'compare', 'page' );
28 + return $page_id && absint( $page_id ) ? get_permalink( $page_id ) : '#';
24 29 }
25 30
26 31 /**
27 32 * Get Fields List
@@ -27,9 +32,9 @@
27 32 * Get Fields List
28 33 *
29 34 * @return array
30 35 */
31 - static function get_available_list_fields() {
36 + public static function get_available_list_fields() {
32 37 $attribute_list = [];
33 38
34 39 if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
35 40 $attribute_list = wc_get_attribute_taxonomies();
@@ -50,9 +55,9 @@
50 55 /**
51 56 * Get default fields List
52 57 * return array
53 58 */
54 - static function get_list_default_fields() {
59 + public static function get_list_default_fields() {
55 60 $fields = [
56 61 'title' => esc_html__( 'Title', 'shopbuilder' ),
57 62 'rating' => esc_html__( 'Rating', 'shopbuilder' ),
58 63 'price' => esc_html__( 'Price', 'shopbuilder' ),
@@ -72,12 +77,12 @@
72 77 * Table field list
73 78 *
74 79 * @return array Table Field list
75 80 */
76 - static function get_list_field_ids() {
81 + public static function get_list_field_ids() {
77 82
78 - $default_show = array_keys( self::get_list_default_fields() );
79 - $fields_settings = Fns::get_option( 'modules', 'compare', 'page_show_fields', [] );
83 + $default_show = array_keys( self::get_list_default_fields() );
84 + $fields_settings = Fns::get_option( 'modules', 'compare', 'page_show_fields', [] );
80 85 if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) {
81 86 $fields = $fields_settings;
82 87 } else {
83 88 $fields = $default_show;
@@ -86,9 +91,9 @@
86 91 return $fields;
87 92 }
88 93
89 94 /**
90 - * @param $field
95 + * @param string $field field.
91 96 *
92 97 * @return mixed|string|void
93 98 */
94 99 public function field_name( $field ) {
@@ -106,11 +111,16 @@
106 111 $field_name = isset( $default[ $field ] ) ? $default[ $field ] : '';
107 112 }
108 113
109 114 return $field_name;
110 -
111 115 }
112 116
117 + /**
118 + * @param string $field_id firld id.
119 + * @param object $product product object.
120 + *
121 + * @return void
122 + */
113 123 public function display_field( $field_id, $product ) {
114 124
115 125 $type = $field_id;
116 126 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
@@ -118,25 +128,25 @@
118 128 }
119 129 switch ( $type ) {
120 130 case 'primary':
121 131 ?>
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">
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">
127 137 <?php
128 138 if ( ! empty( $product['primary']['image'] ) ) {
129 139 echo wp_kses( $product['primary']['image'], Fns::allowedHtml( 'image' ) );
130 140 }
131 141 ?>
132 - </a>
133 - </div>
142 + </a>
143 + </div>
134 144 <?php
135 145 break;
136 146
137 147 case 'title':
138 - echo '<a href="' . get_permalink( $product['id'] ) . '" class="rtsb-compare-product-title">' . esc_html( $product[ $field_id ] ) . '</a>';
148 + echo '<a href="' . esc_url( get_permalink( $product['id'] ) ) . '" class="rtsb-compare-product-title">' . esc_html( $product[ $field_id ] ) . '</a>';
139 149 break;
140 150
141 151 case 'rating':
142 152 echo '<span class="rtsb-compare-product-rating">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
@@ -146,22 +156,26 @@
146 156 echo '<div class="rtsb-compare-product-price">' . wp_kses_post( $product[ $field_id ] ) . '</div>';
147 157 break;
148 158
149 159 case 'add_to_cart':
150 - echo apply_filters( 'rtsb/module/compare/add_to_cart_btn', $product[ $field_id ] );
160 + Fns::print_html( apply_filters( 'rtsb/module/compare/add_to_cart_btn', $product[ $field_id ] ), true );
151 161 break;
152 162
153 163 case 'weight':
154 164 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 );
165 + $unit = '-' !== $product[ $field_id ] ? get_option( 'woocommerce_weight_unit' ) : '';
166 + Fns::print_html( wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ) );
157 167 }
158 168 break;
159 169
160 170 case 'description':
161 - echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] );
171 + Fns::print_html( apply_filters( 'woocommerce_short_description', $product[ $field_id ] ) );
162 172 break;
163 -
173 + case 'attribute':
174 + if ( isset( $product[ $field_id ] ) ) {
175 + Fns::print_html( $product[ $field_id ] );
176 + }
177 + break;
164 178 default:
165 179 echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : '';
166 180 break;
167 181 }
@@ -166,12 +180,15 @@
166 180 break;
167 181 }
168 182 }
169 183
184 + /**
185 + * @return array
186 + */
170 187 public function get_products_data() {
171 188 $ids = $this->get_compared_product_ids();
172 189
173 - // For shareable link
190 + // For shareable link.
174 191 if ( empty( $ids ) ) {
175 192 return [];
176 193 }
177 194
@@ -182,16 +199,14 @@
182 199 $products = wc_get_products( $args );
183 200
184 201 $products_data = [];
185 202
186 - $field_ids = $this->get_list_field_ids();
187 -
203 + $field_ids = $this->get_list_field_ids();
188 204 $tax_field_ids = array_filter(
189 205 $field_ids,
190 206 function ( $field_id ) {
191 - return 'pa_' === substr( $field_id, 0, 3 );
192 - },
193 - ARRAY_FILTER_USE_KEY
207 + return str_starts_with( $field_id, 'pa_' );
208 + }
194 209 );
195 210
196 211 $data_none = '-';
197 212
@@ -216,25 +231,27 @@
216 231 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
217 232 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
218 233 'availability' => $this->availability_html( $product ),
219 234 ];
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 + 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;
235 248 }
249 + } else {
250 + $products_data[ $product->get_id() ][ $field_id ][] = '-';
236 251 }
252 + $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
253 +
237 254 }
238 255 }
239 256
240 257 return $products_data;
@@ -240,9 +257,9 @@
240 257 return $products_data;
241 258 }
242 259
243 260 /**
244 - * @param $product
261 + * @param object $product product object.
245 262 *
246 263 * @return mixed|void|null
247 264 */
248 265 public function add_to_cart_html( $product ) {
@@ -263,9 +280,8 @@
263 280 'attributes' => [
264 281 'data-product_id' => $product->get_id(),
265 282 'data-product_sku' => $product->get_sku(),
266 283 'aria-label' => $product->add_to_cart_description(),
267 - 'rel' => 'nofollow',
268 284 ],
269 285 ];
270 286
271 287 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
@@ -287,9 +303,13 @@
287 303 $product,
288 304 $args
289 305 );
290 306 }
291 -
307 + /**
308 + * @param object $product product object.
309 + *
310 + * @return mixed|void|null
311 + */
292 312 public function availability_html( $product ) {
293 313 $html = '';
294 314 $availability = $product->get_availability();
295 315
@@ -314,8 +334,12 @@
314 334
315 335 return apply_filters( 'woocommerce_get_stock_html', $html, $product );
316 336 }
317 337
338 + /**
339 + * @param int $id product id.
340 + * @return true|WP_Error
341 + */
318 342 public function add_product( $id ) {
319 343 $cookie_name = $this->get_cookie_name();
320 344
321 345 if ( $this->is_exists_in_list( $id ) ) {
@@ -323,9 +347,9 @@
323 347 }
324 348
325 349 $product_ids = $this->get_compared_product_ids();
326 350
327 - // Reached Maximum Limit
351 + // Reached Maximum Limit.
328 352 if ( $this->reached_max_limit() ) {
329 353 return new \WP_Error( 'rtsb-compare-reached_max_limit', esc_html__( 'Reached maximum number of compare!.', 'shopbuilder' ) );
330 354 }
331 355
@@ -340,9 +364,9 @@
340 364
341 365 /**
342 366 * Remove product from compare list
343 367 *
344 - * @param integer $product_id
368 + * @param integer $product_id product id.
345 369 *
346 370 * @return bool|WP_Error
347 371 */
348 372 public function remove_product( $product_id ) {
@@ -369,8 +393,11 @@
369 393
370 394 return $delete_status;
371 395 }
372 396
397 + /**
398 + * @return string
399 + */
373 400 public function get_cookie_name() {
374 401 $name = 'rtsb_compare_list';
375 402 if ( is_multisite() ) {
376 403 $name .= '_' . get_current_blog_id();
@@ -379,22 +406,27 @@
379 406 return $name;
380 407 }
381 408
382 409 /**
383 - * @param $id
410 + * @param int $id product id.
384 411 *
385 412 * @return bool
386 413 */
387 414 public function is_exists_in_list( $id ) {
388 415 $list = $this->get_compared_product_ids();
389 -
390 - return in_array( $id, $list );
416 + return in_array( absint( $id ), $list, true );
391 417 }
392 418
419 + /**
420 + * @return array
421 + */
393 422 public function get_compared_product_ids() {
394 423 $cookie_name = $this->get_cookie_name();
395 424 $ids = [];
425 +
426 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
396 427 if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
428 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
397 429 $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
398 430 }
399 431
400 432 return $ids;
@@ -399,18 +431,21 @@
399 431
400 432 return $ids;
401 433 }
402 434
435 + /**
436 + * @return bool
437 + */
403 438 public function reached_max_limit() {
404 439
405 440 $product_ids = $this->get_compared_product_ids();
406 - $max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) );
441 + $max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) );
407 442
408 - // Remove if product is not exist.
443 + // Remove if product is not exist.
409 444 foreach ( $product_ids as $id ) {
410 - if( 'publish' !== get_post_status( $id )){
445 + if ( 'publish' !== get_post_status( $id ) ) {
411 446 $this->remove_product( $id );
412 - unset( $product_ids[$id] );
447 + unset( $product_ids[ $id ] );
413 448 }
414 449 }
415 450
416 451 if ( $max_limit && count( $product_ids ) >= $max_limit ) {
@@ -418,6 +453,5 @@
418 453 }
419 454
420 455 return false;
421 456 }
422 -
423 457 }