| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimateStoreKit; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
|
| 11 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 12 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 13 |
} |
| 14 |
|
| 15 |
final class WishlistCompare { |
| 16 |
public function __construct() { |
| 17 |
add_action( 'wp_ajax_usk_add_to_wishlist', [ $this, 'usk_add_to_wishlist' ] ); |
| 18 |
add_action( 'wp_ajax_nopriv_usk_add_to_wishlist', [ $this, 'usk_add_to_wishlist' ] ); |
| 19 |
|
| 20 |
add_action( 'wp_ajax_usk_add_to_compare_products', [ $this, 'usk_add_to_compare_products' ] ); |
| 21 |
add_action( 'wp_ajax_nopriv_usk_add_to_compare_products', [ $this, 'usk_add_to_compare_products' ] ); |
| 22 |
|
| 23 |
add_action( 'wp_ajax_usk_remove_from_compare_products', [ $this, 'usk_remove_from_compare_products' ] ); |
| 24 |
add_action( 'wp_ajax_nopriv_usk_remove_from_compare_products', [ $this, 'usk_remove_from_compare_products' ] ); |
| 25 |
|
| 26 |
//wishlist |
| 27 |
// add_action('woocommerce_account_wishlist_endpoint', [$this, 'usk_wishlist_content']); |
| 28 |
// add_filter('woocommerce_account_menu_items', [$this, 'usk_wishlist_link_my_account']); |
| 29 |
// add_filter('query_vars', [$this, 'usk_wishlist_query_vars'], 0); |
| 30 |
// add_action('init', [$this, 'usk_add_rewrite_flash_rules_endpoint']); |
| 31 |
} |
| 32 |
|
| 33 |
public function usk_add_to_wishlist() { |
| 34 |
$response = [ |
| 35 |
'status' => 0, |
| 36 |
'message' => __( 'Unauthorized!', 'ultimate-store-kit' ), |
| 37 |
]; |
| 38 |
|
| 39 |
if ( ! isset( $_POST['product_id'] ) ) { |
| 40 |
$response['message'] = __( 'No product selected!', 'ultimate-store-kit' ); |
| 41 |
wp_send_json( $response ); |
| 42 |
} |
| 43 |
|
| 44 |
$product_id = isset( $_POST['product_id'] ) ? sanitize_text_field( $_POST['product_id'] ) : ''; |
| 45 |
|
| 46 |
$user_id = get_current_user_id(); |
| 47 |
$wishlist = ultimate_store_kit_get_wishlist( $user_id ); |
| 48 |
|
| 49 |
$wishlistCounter = count( $wishlist ); |
| 50 |
|
| 51 |
if ( ( $key = array_search( $product_id, $wishlist ) ) !== false ) { |
| 52 |
$response['action'] = 'removed'; |
| 53 |
$response['count'] = --$wishlistCounter; |
| 54 |
unset( $wishlist[ $key ] ); |
| 55 |
} else { |
| 56 |
$response['action'] = 'added'; |
| 57 |
$response['count'] = ++$wishlistCounter; |
| 58 |
$wishlist[] = $product_id; |
| 59 |
} |
| 60 |
|
| 61 |
$wishlist = array_unique( $wishlist ); |
| 62 |
|
| 63 |
// update wishlist |
| 64 |
$this->ultimate_store_kit_set_wishlist( $wishlist, $user_id ); |
| 65 |
|
| 66 |
// send response |
| 67 |
$response['status'] = 1; |
| 68 |
|
| 69 |
if ( $response['action'] == 'added' ) { |
| 70 |
$response['message'] = __( "Wishlist item Added!", "ultimate-store-kit" ); |
| 71 |
wp_send_json( $response ); |
| 72 |
} else { |
| 73 |
$response['message'] = __( "Add To Wishlist", "ultimate-store-kit" ); |
| 74 |
wp_send_json( $response ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public function ultimate_store_kit_set_wishlist( $wishlist, $user_id = 0 ) { |
| 79 |
$_wishlist_key = '_ultimate_store_kit_wishlist'; |
| 80 |
$user_id = get_current_user_id(); |
| 81 |
$existing_wishlist = ultimate_store_kit_get_wishlist( $user_id ); |
| 82 |
|
| 83 |
$wishlist = array_unique( array_merge( $existing_wishlist, $wishlist ) ); |
| 84 |
|
| 85 |
setcookie( $_wishlist_key, json_encode( $wishlist ), time() + MONTH_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
| 86 |
} |
| 87 |
|
| 88 |
public function get_compare_product_page_id() { |
| 89 |
if ( $comparePage = ultimate_store_kit_compare_product_page() ) { |
| 90 |
return $comparePage->ID; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
//====================================== |
| 96 |
//=========COMPARE PRODUCTS============= |
| 97 |
//====================================== |
| 98 |
public function usk_add_to_compare_products() { |
| 99 |
$response = [ |
| 100 |
'status' => 0, |
| 101 |
'message' => __( 'Unauthorized!', 'ultimate-store-kit' ), |
| 102 |
]; |
| 103 |
|
| 104 |
if ( ! isset( $_POST['product_id'] ) ) { |
| 105 |
$response['message'] = __( 'No product selected!', 'ultimate-store-kit' ); |
| 106 |
wp_send_json( $response ); |
| 107 |
} |
| 108 |
|
| 109 |
$user_id = get_current_user_id(); |
| 110 |
$compare_products = usk_get_compare_products( $user_id ); |
| 111 |
|
| 112 |
// count compare products |
| 113 |
if ( is_array( $compare_products ) ) { |
| 114 |
$response['count'] = count( $compare_products ) + 1; |
| 115 |
} |
| 116 |
|
| 117 |
//add to compare products |
| 118 |
$response['action'] = 'added'; |
| 119 |
$compare_products[] = $_POST['product_id']; |
| 120 |
|
| 121 |
$compare_products = array_unique( $compare_products ); |
| 122 |
|
| 123 |
// update compare_productsusk_add_to_compare_products |
| 124 |
$this->ultimate_store_kit_set_compare_products( $compare_products, $user_id ); |
| 125 |
|
| 126 |
// send response |
| 127 |
$response['status'] = 1; |
| 128 |
if ( $response['action'] == 'added' ) { |
| 129 |
$response['message'] = __( "Added", "ultimate-store-kit" ); |
| 130 |
$response['url'] = ''; |
| 131 |
if ( $pageId = $this->get_compare_product_page_id() ) { |
| 132 |
$response['url'] = get_permalink( $pageId ); |
| 133 |
} |
| 134 |
|
| 135 |
wp_send_json( $response ); |
| 136 |
} else { |
| 137 |
$response['message'] = __( "Compare", "ultimate-store-kit" ); |
| 138 |
wp_send_json( $response ); |
| 139 |
} |
| 140 |
} |
| 141 |
public function usk_remove_from_compare_products() { |
| 142 |
$response = [ |
| 143 |
'status' => 0, |
| 144 |
'message' => __( 'Unauthorized!', 'ultimate-store-kit' ), |
| 145 |
]; |
| 146 |
if ( ! isset( $_POST['product_id'] ) ) { |
| 147 |
$response['message'] = __( 'No product selected!', 'ultimate-store-kit' ); |
| 148 |
wp_send_json( $response ); |
| 149 |
} |
| 150 |
$user_id = get_current_user_id(); |
| 151 |
$compare_products = usk_get_compare_products( $user_id ); |
| 152 |
|
| 153 |
//add remove from compare products |
| 154 |
if ( ( $key = array_search( $_POST['product_id'], $compare_products ) ) !== false ) { |
| 155 |
$response['action'] = 'removed'; |
| 156 |
unset( $compare_products[ $key ] ); |
| 157 |
} |
| 158 |
$compare_products = array_unique( $compare_products ); |
| 159 |
|
| 160 |
// update compare_products |
| 161 |
$this->ultimate_store_kit_set_compare_products( $compare_products, $user_id ); |
| 162 |
|
| 163 |
// send response |
| 164 |
$response['status'] = 1; |
| 165 |
/* translators: %s is the action performed on the compared product */ |
| 166 |
$response['message'] = sprintf( __( 'Compared product item: %s.', 'ultimate-store-kit' ), $response['action'] ); |
| 167 |
|
| 168 |
wp_send_json( $response ); |
| 169 |
} |
| 170 |
public function ultimate_store_kit_set_compare_products( $compare_products, $user_id = 0 ) { |
| 171 |
$_compare_products_key = '_ultimate_store_kit_compare_products'; |
| 172 |
$_compare_products = []; |
| 173 |
|
| 174 |
if ( $user_id != 0 ) { |
| 175 |
update_user_meta( $user_id, $_compare_products_key, $compare_products ); |
| 176 |
} else { |
| 177 |
setcookie( $_compare_products_key, json_encode( $compare_products ), time() + MONTH_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
new WishlistCompare(); |
| 183 |
|