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