| 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 ] ) ); |
| 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 |
|
| 163 |
default: |
| 164 |
echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : ''; |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public function get_products_data() { |
| 170 |
$ids = $this->get_compared_product_ids(); |
| 171 |
|
| 172 |
// For shareable link |
| 173 |
if ( empty( $ids ) ) { |
| 174 |
return []; |
| 175 |
} |
| 176 |
|
| 177 |
$args = [ |
| 178 |
'include' => $ids, |
| 179 |
]; |
| 180 |
|
| 181 |
$products = wc_get_products( $args ); |
| 182 |
|
| 183 |
$products_data = []; |
| 184 |
|
| 185 |
$field_ids = $this->get_list_field_ids(); |
| 186 |
|
| 187 |
$tax_field_ids = array_filter( |
| 188 |
$field_ids, |
| 189 |
function ( $field_id ) { |
| 190 |
return 'pa_' === substr( $field_id, 0, 3 ); |
| 191 |
}, |
| 192 |
ARRAY_FILTER_USE_KEY |
| 193 |
); |
| 194 |
|
| 195 |
$data_none = '-'; |
| 196 |
|
| 197 |
foreach ( $products as $product ) { |
| 198 |
|
| 199 |
$rating_count = $product->get_rating_count(); |
| 200 |
$average = $product->get_average_rating(); |
| 201 |
|
| 202 |
$products_data[ $product->get_id() ] = [ |
| 203 |
'primary' => [ |
| 204 |
'image' => $product->get_image() ? $product->get_image( 'ever-compare-image' ) : $data_none, |
| 205 |
], |
| 206 |
'id' => $product->get_id(), |
| 207 |
'title' => $product->get_title() ? $product->get_title() : $data_none, |
| 208 |
'image_id' => $product->get_image_id(), |
| 209 |
'permalink' => $product->get_permalink(), |
| 210 |
'price' => $product->get_price_html() ? $product->get_price_html() : $data_none, |
| 211 |
'rating' => wc_get_rating_html( $average, $rating_count ), |
| 212 |
'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none, |
| 213 |
'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ), |
| 214 |
'description' => $product->get_short_description() ? $product->get_short_description() : $data_none, |
| 215 |
'weight' => $product->get_weight() ? $product->get_weight() : $data_none, |
| 216 |
'sku' => $product->get_sku() ? $product->get_sku() : $data_none, |
| 217 |
'availability' => $this->availability_html( $product ), |
| 218 |
]; |
| 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 ] ); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return $products_data; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* @param $product |
| 244 |
* |
| 245 |
* @return mixed|void|null |
| 246 |
*/ |
| 247 |
public function add_to_cart_html( $product ) { |
| 248 |
|
| 249 |
$defaults = [ |
| 250 |
'quantity' => 1, |
| 251 |
'class' => implode( |
| 252 |
' ', |
| 253 |
array_filter( |
| 254 |
[ |
| 255 |
'rtsb-compare-cart-button button', |
| 256 |
'product_type_' . $product->get_type(), |
| 257 |
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', |
| 258 |
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', |
| 259 |
] |
| 260 |
) |
| 261 |
), |
| 262 |
'attributes' => [ |
| 263 |
'data-product_id' => $product->get_id(), |
| 264 |
'data-product_sku' => $product->get_sku(), |
| 265 |
'aria-label' => $product->add_to_cart_description(), |
| 266 |
'rel' => 'nofollow', |
| 267 |
], |
| 268 |
]; |
| 269 |
|
| 270 |
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product ); |
| 271 |
|
| 272 |
if ( isset( $args['attributes']['aria-label'] ) ) { |
| 273 |
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); |
| 274 |
} |
| 275 |
|
| 276 |
return apply_filters( |
| 277 |
'woocommerce_loop_add_to_cart_link', |
| 278 |
sprintf( |
| 279 |
'<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>', |
| 280 |
esc_url( $product->add_to_cart_url() ), |
| 281 |
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ), |
| 282 |
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ), |
| 283 |
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '', |
| 284 |
esc_html( $product->add_to_cart_text() ) |
| 285 |
), |
| 286 |
$product, |
| 287 |
$args |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
public function availability_html( $product ) { |
| 292 |
$html = ''; |
| 293 |
$availability = $product->get_availability(); |
| 294 |
|
| 295 |
if ( empty( $availability['availability'] ) ) { |
| 296 |
$availability['availability'] = esc_html__( 'In stock', 'shopbuilder' ); |
| 297 |
} |
| 298 |
|
| 299 |
if ( ! empty( $availability['availability'] ) ) { |
| 300 |
ob_start(); |
| 301 |
|
| 302 |
wc_get_template( |
| 303 |
'single-product/stock.php', |
| 304 |
[ |
| 305 |
'product' => $product, |
| 306 |
'class' => $availability['class'], |
| 307 |
'availability' => $availability['availability'], |
| 308 |
] |
| 309 |
); |
| 310 |
|
| 311 |
$html = ob_get_clean(); |
| 312 |
} |
| 313 |
|
| 314 |
return apply_filters( 'woocommerce_get_stock_html', $html, $product ); |
| 315 |
} |
| 316 |
|
| 317 |
public function add_product( $id ) { |
| 318 |
$cookie_name = $this->get_cookie_name(); |
| 319 |
|
| 320 |
if ( $this->is_exists_in_list( $id ) ) { |
| 321 |
return new \WP_Error( 'rtsb-compare-product-exist', esc_html__( 'Product already in compare list!.', 'shopbuilder' ) ); |
| 322 |
} |
| 323 |
|
| 324 |
$product_ids = $this->get_compared_product_ids(); |
| 325 |
|
| 326 |
// Reached Maximum Limit |
| 327 |
if ( $this->reached_max_limit() ) { |
| 328 |
return new \WP_Error( 'rtsb-compare-reached_max_limit', esc_html__( 'Reached maximum number of compare!.', 'shopbuilder' ) ); |
| 329 |
} |
| 330 |
|
| 331 |
$product_ids[] = $id; |
| 332 |
|
| 333 |
setcookie( $cookie_name, wp_json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 334 |
|
| 335 |
$_COOKIE[ $cookie_name ] = wp_json_encode( $product_ids ); |
| 336 |
|
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Remove product from compare list |
| 342 |
* |
| 343 |
* @param integer $product_id |
| 344 |
* |
| 345 |
* @return bool|WP_Error |
| 346 |
*/ |
| 347 |
public function remove_product( $product_id ) { |
| 348 |
|
| 349 |
if ( ! $this->is_exists_in_list( $product_id ) ) { |
| 350 |
return new WP_Error( 'rtsb_compare_product_not_exist', esc_html__( 'Product not in compare list!.', 'shopbuilder' ) ); |
| 351 |
} |
| 352 |
$delete_status = false; |
| 353 |
$product_ids = $this->get_compared_product_ids(); |
| 354 |
if ( is_array( $product_ids ) && ! empty( $product_ids ) ) { |
| 355 |
foreach ( $product_ids as $prod_key => $productId ) { |
| 356 |
if ( $productId == $product_id ) { |
| 357 |
unset( $product_ids[ $prod_key ] ); |
| 358 |
$delete_status = true; |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
$cookie_name = $this->get_cookie_name(); |
| 365 |
|
| 366 |
setcookie( $cookie_name, wp_json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 367 |
$_COOKIE[ $cookie_name ] = wp_json_encode( $product_ids ); |
| 368 |
|
| 369 |
return $delete_status; |
| 370 |
} |
| 371 |
|
| 372 |
public function get_cookie_name() { |
| 373 |
$name = 'rtsb_compare_list'; |
| 374 |
if ( is_multisite() ) { |
| 375 |
$name .= '_' . get_current_blog_id(); |
| 376 |
} |
| 377 |
|
| 378 |
return $name; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* @param $id |
| 383 |
* |
| 384 |
* @return bool |
| 385 |
*/ |
| 386 |
public function is_exists_in_list( $id ) { |
| 387 |
$list = $this->get_compared_product_ids(); |
| 388 |
|
| 389 |
return in_array( $id, $list ); |
| 390 |
} |
| 391 |
|
| 392 |
public function get_compared_product_ids() { |
| 393 |
$cookie_name = $this->get_cookie_name(); |
| 394 |
$ids = []; |
| 395 |
if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) { |
| 396 |
$ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ); |
| 397 |
} |
| 398 |
|
| 399 |
return $ids; |
| 400 |
} |
| 401 |
|
| 402 |
public function reached_max_limit() { |
| 403 |
|
| 404 |
$product_ids = $this->get_compared_product_ids(); |
| 405 |
$max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) ); |
| 406 |
|
| 407 |
// Remove if product is not exist. |
| 408 |
foreach ( $product_ids as $id ) { |
| 409 |
if ( 'publish' !== get_post_status( $id ) ) { |
| 410 |
$this->remove_product( $id ); |
| 411 |
unset( $product_ids[ $id ] ); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
if ( $max_limit && count( $product_ids ) >= $max_limit ) { |
| 416 |
return true; |
| 417 |
} |
| 418 |
|
| 419 |
return false; |
| 420 |
} |
| 421 |
} |
| 422 |
|