| @@ -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 |
| @@ -19,9 +24,9 @@ | ||
| 19 | 24 | * @return false|string |
| 20 | 25 | */ |
| 21 | 26 | public function get_page_url() { |
| 22 | 27 | $page_id = Fns::get_option( 'modules', 'compare', 'page' ); |
| 23 | - return get_permalink( $page_id ); | |
| 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,9 +77,9 @@ | ||
| 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 | 83 | $default_show = array_keys( self::get_list_default_fields() ); |
| 79 | 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 ) { |
| @@ -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 ) { |
| @@ -108,8 +113,14 @@ | ||
| 108 | 113 | |
| 109 | 114 | return $field_name; |
| 110 | 115 | } |
| 111 | 116 | |
| 117 | + /** | |
| 118 | + * @param string $field_id firld id. | |
| 119 | + * @param object $product product object. | |
| 120 | + * | |
| 121 | + * @return void | |
| 122 | + */ | |
| 112 | 123 | public function display_field( $field_id, $product ) { |
| 113 | 124 | |
| 114 | 125 | $type = $field_id; |
| 115 | 126 | if ( 'pa_' === substr( $field_id, 0, 3 ) ) { |
| @@ -145,14 +156,14 @@ | ||
| 145 | 156 | echo '<div class="rtsb-compare-product-price">' . wp_kses_post( $product[ $field_id ] ) . '</div>'; |
| 146 | 157 | break; |
| 147 | 158 | |
| 148 | 159 | case 'add_to_cart': |
| 149 | - Fns::print_html( 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 ); | |
| 150 | 161 | break; |
| 151 | 162 | |
| 152 | 163 | case 'weight': |
| 153 | 164 | if ( $product[ $field_id ] ) { |
| 154 | - $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : ''; | |
| 165 | + $unit = '-' !== $product[ $field_id ] ? get_option( 'woocommerce_weight_unit' ) : ''; | |
| 155 | 166 | Fns::print_html( wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ) ); |
| 156 | 167 | } |
| 157 | 168 | break; |
| 158 | 169 | |
| @@ -158,9 +169,13 @@ | ||
| 158 | 169 | |
| 159 | 170 | case 'description': |
| 160 | 171 | Fns::print_html( apply_filters( 'woocommerce_short_description', $product[ $field_id ] ) ); |
| 161 | 172 | break; |
| 162 | - | |
| 173 | + case 'attribute': | |
| 174 | + if ( isset( $product[ $field_id ] ) ) { | |
| 175 | + Fns::print_html( $product[ $field_id ] ); | |
| 176 | + } | |
| 177 | + break; | |
| 163 | 178 | default: |
| 164 | 179 | echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : ''; |
| 165 | 180 | break; |
| 166 | 181 | } |
| @@ -165,12 +180,15 @@ | ||
| 165 | 180 | break; |
| 166 | 181 | } |
| 167 | 182 | } |
| 168 | 183 | |
| 184 | + /** | |
| 185 | + * @return array | |
| 186 | + */ | |
| 169 | 187 | public function get_products_data() { |
| 170 | 188 | $ids = $this->get_compared_product_ids(); |
| 171 | 189 | |
| 172 | - // For shareable link | |
| 190 | + // For shareable link. | |
| 173 | 191 | if ( empty( $ids ) ) { |
| 174 | 192 | return []; |
| 175 | 193 | } |
| 176 | 194 | |
| @@ -181,16 +199,14 @@ | ||
| 181 | 199 | $products = wc_get_products( $args ); |
| 182 | 200 | |
| 183 | 201 | $products_data = []; |
| 184 | 202 | |
| 185 | - $field_ids = $this->get_list_field_ids(); | |
| 186 | - | |
| 203 | + $field_ids = $this->get_list_field_ids(); | |
| 187 | 204 | $tax_field_ids = array_filter( |
| 188 | 205 | $field_ids, |
| 189 | 206 | function ( $field_id ) { |
| 190 | - return 'pa_' === substr( $field_id, 0, 3 ); | |
| 191 | - }, | |
| 192 | - ARRAY_FILTER_USE_KEY | |
| 207 | + return str_starts_with( $field_id, 'pa_' ); | |
| 208 | + } | |
| 193 | 209 | ); |
| 194 | 210 | |
| 195 | 211 | $data_none = '-'; |
| 196 | 212 | |
| @@ -215,25 +231,27 @@ | ||
| 215 | 231 | 'weight' => $product->get_weight() ? $product->get_weight() : $data_none, |
| 216 | 232 | 'sku' => $product->get_sku() ? $product->get_sku() : $data_none, |
| 217 | 233 | 'availability' => $this->availability_html( $product ), |
| 218 | 234 | ]; |
| 219 | - | |
| 220 | - if ( ! empty( $tax_field_ids ) ) { | |
| 221 | - foreach ( $tax_field_ids as $field_id ) { | |
| 222 | - if ( taxonomy_exists( $field_id ) ) { | |
| 223 | - $products_data[ $product->get_id() ][ $field_id ] = []; | |
| 224 | - $terms = get_the_terms( $product->get_id(), $field_id ); | |
| 225 | - if ( ! empty( $terms ) ) { | |
| 226 | - foreach ( $terms as $term ) { | |
| 227 | - $term = sanitize_term( $term, $field_id ); | |
| 228 | - $products_data[ $product->get_id() ][ $field_id ][] = $term->name; | |
| 229 | - } | |
| 230 | - } else { | |
| 231 | - $products_data[ $product->get_id() ][ $field_id ][] = '-'; | |
| 232 | - } | |
| 233 | - $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; | |
| 234 | 248 | } |
| 249 | + } else { | |
| 250 | + $products_data[ $product->get_id() ][ $field_id ][] = '-'; | |
| 235 | 251 | } |
| 252 | + $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] ); | |
| 253 | + | |
| 236 | 254 | } |
| 237 | 255 | } |
| 238 | 256 | |
| 239 | 257 | return $products_data; |
| @@ -239,9 +257,9 @@ | ||
| 239 | 257 | return $products_data; |
| 240 | 258 | } |
| 241 | 259 | |
| 242 | 260 | /** |
| 243 | - * @param $product | |
| 261 | + * @param object $product product object. | |
| 244 | 262 | * |
| 245 | 263 | * @return mixed|void|null |
| 246 | 264 | */ |
| 247 | 265 | public function add_to_cart_html( $product ) { |
| @@ -262,9 +280,8 @@ | ||
| 262 | 280 | 'attributes' => [ |
| 263 | 281 | 'data-product_id' => $product->get_id(), |
| 264 | 282 | 'data-product_sku' => $product->get_sku(), |
| 265 | 283 | 'aria-label' => $product->add_to_cart_description(), |
| 266 | - 'rel' => 'nofollow', | |
| 267 | 284 | ], |
| 268 | 285 | ]; |
| 269 | 286 | |
| 270 | 287 | $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product ); |
| @@ -286,9 +303,13 @@ | ||
| 286 | 303 | $product, |
| 287 | 304 | $args |
| 288 | 305 | ); |
| 289 | 306 | } |
| 290 | - | |
| 307 | + /** | |
| 308 | + * @param object $product product object. | |
| 309 | + * | |
| 310 | + * @return mixed|void|null | |
| 311 | + */ | |
| 291 | 312 | public function availability_html( $product ) { |
| 292 | 313 | $html = ''; |
| 293 | 314 | $availability = $product->get_availability(); |
| 294 | 315 | |
| @@ -313,8 +334,12 @@ | ||
| 313 | 334 | |
| 314 | 335 | return apply_filters( 'woocommerce_get_stock_html', $html, $product ); |
| 315 | 336 | } |
| 316 | 337 | |
| 338 | + /** | |
| 339 | + * @param int $id product id. | |
| 340 | + * @return true|WP_Error | |
| 341 | + */ | |
| 317 | 342 | public function add_product( $id ) { |
| 318 | 343 | $cookie_name = $this->get_cookie_name(); |
| 319 | 344 | |
| 320 | 345 | if ( $this->is_exists_in_list( $id ) ) { |
| @@ -322,9 +347,9 @@ | ||
| 322 | 347 | } |
| 323 | 348 | |
| 324 | 349 | $product_ids = $this->get_compared_product_ids(); |
| 325 | 350 | |
| 326 | - // Reached Maximum Limit | |
| 351 | + // Reached Maximum Limit. | |
| 327 | 352 | if ( $this->reached_max_limit() ) { |
| 328 | 353 | return new \WP_Error( 'rtsb-compare-reached_max_limit', esc_html__( 'Reached maximum number of compare!.', 'shopbuilder' ) ); |
| 329 | 354 | } |
| 330 | 355 | |
| @@ -339,9 +364,9 @@ | ||
| 339 | 364 | |
| 340 | 365 | /** |
| 341 | 366 | * Remove product from compare list |
| 342 | 367 | * |
| 343 | - * @param integer $product_id | |
| 368 | + * @param integer $product_id product id. | |
| 344 | 369 | * |
| 345 | 370 | * @return bool|WP_Error |
| 346 | 371 | */ |
| 347 | 372 | public function remove_product( $product_id ) { |
| @@ -368,8 +393,11 @@ | ||
| 368 | 393 | |
| 369 | 394 | return $delete_status; |
| 370 | 395 | } |
| 371 | 396 | |
| 397 | + /** | |
| 398 | + * @return string | |
| 399 | + */ | |
| 372 | 400 | public function get_cookie_name() { |
| 373 | 401 | $name = 'rtsb_compare_list'; |
| 374 | 402 | if ( is_multisite() ) { |
| 375 | 403 | $name .= '_' . get_current_blog_id(); |
| @@ -378,22 +406,27 @@ | ||
| 378 | 406 | return $name; |
| 379 | 407 | } |
| 380 | 408 | |
| 381 | 409 | /** |
| 382 | - * @param $id | |
| 410 | + * @param int $id product id. | |
| 383 | 411 | * |
| 384 | 412 | * @return bool |
| 385 | 413 | */ |
| 386 | 414 | public function is_exists_in_list( $id ) { |
| 387 | 415 | $list = $this->get_compared_product_ids(); |
| 388 | - | |
| 389 | - return in_array( $id, $list ); | |
| 416 | + return in_array( absint( $id ), $list, true ); | |
| 390 | 417 | } |
| 391 | 418 | |
| 419 | + /** | |
| 420 | + * @return array | |
| 421 | + */ | |
| 392 | 422 | public function get_compared_product_ids() { |
| 393 | 423 | $cookie_name = $this->get_cookie_name(); |
| 394 | 424 | $ids = []; |
| 425 | + | |
| 426 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 395 | 427 | if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) { |
| 428 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 396 | 429 | $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ); |
| 397 | 430 | } |
| 398 | 431 | |
| 399 | 432 | return $ids; |
| @@ -398,8 +431,11 @@ | ||
| 398 | 431 | |
| 399 | 432 | return $ids; |
| 400 | 433 | } |
| 401 | 434 | |
| 435 | + /** | |
| 436 | + * @return bool | |
| 437 | + */ | |
| 402 | 438 | public function reached_max_limit() { |
| 403 | 439 | |
| 404 | 440 | $product_ids = $this->get_compared_product_ids(); |
| 405 | 441 | $max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) ); |