0 ) {
self::cache_in_wishlist( $post_id );
$status['progress'] = 'done';
$status['text'] = __( 'Item Added to Wishlist', 'upstudy' );
} else {
$status['progress'] = 'undone';
$status['text'] = __( 'Invalid item ID.', 'upstudy' );
}
wp_send_json( $status );
}
/**
* Handle removing an item from the wishlist via AJAX.
*/
public function wishlist_ajax_remove() {
check_ajax_referer( 'upstudy-wishlist-ajax-connect', 'security' );
$status = array();
// Sanitize the post_id
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
if ( $post_id > 0 ) {
$new_wishlist = array();
if ( isset( $_COOKIE['tpc_course_wishlist'] ) ) {
$items = explode( ',', sanitize_text_field( $_COOKIE['tpc_course_wishlist'] ) );
// Filter out the item to remove
foreach ( $items as $item ) {
if ( intval( $item ) !== $post_id ) {
$new_wishlist[] = intval( $item );
}
}
}
$wishlist_string = implode( ',', $new_wishlist );
// Set the updated cookie
setcookie( 'tpc_course_wishlist', $wishlist_string, time() + ( 60 * 60 * 24 * 15 ), '/' );
$_COOKIE['tpc_course_wishlist'] = $wishlist_string;
$status['progress'] = 'done';
$status['text'] = __( 'Item removed from wishlist', 'upstudy' );
} else {
$status['progress'] = 'error';
$status['text'] = __( 'Invalid item ID.', 'upstudy' );
}
wp_send_json( $status );
}
/**
* Cache an item in the wishlist.
*
* @param int $id The ID of the item to cache in the wishlist.
*/
public static function cache_in_wishlist( $id ) {
$items = array();
if ( isset( $_COOKIE['tpc_course_wishlist'] ) ) {
$items = explode( ',', $_COOKIE['tpc_course_wishlist'] );
if ( ! self::verify_wishlisted_items( $id, $items ) ) {
$items[] = $id;
}
} else {
$items = array( $id );
}
setcookie( 'tpc_course_wishlist', implode( ',', $items ), time() + 60 * 60 * 24 * 15, '/' );
$_COOKIE['tpc_course_wishlist'] = implode( ',', $items );
}
/**
* Verify if an item is in the wishlist.
*
* @param int $id The ID of the item to verify.
* @return bool True if the item is in the wishlist, false otherwise.
*/
public static function verify_wishlisted_items( $id ) {
if ( empty( $id ) ) {
return false;
}
if ( isset( $_COOKIE['tpc_course_wishlist'] ) && ! empty( $_COOKIE['tpc_course_wishlist'] ) ) {
$added_items = explode( ',', $_COOKIE['tpc_course_wishlist'] );
if ( in_array( $id, $added_items ) ) {
return true;
}
}
return false;
}
/**
* Display the wishlist button.
*
* @param WP_Post $post The post object.
* @param string $type The type of button display ('icon' or 'text').
*/
public static function content( $post, $type = 'icon' ) {
$id = $post->ID;
$class = '';
$icon_class = 'dt-icon-heart';
$text = __( 'Add Item to wishlist', 'upstudy' );
$added = self::verify_wishlisted_items( $id );
if ( $added ) {
$text = __( 'Item Added to Wishlist', 'upstudy' );
$icon_class = 'dt-icon-heart';
$class = 'tpc-wishlisted';
} else {
$class = 'tpc-wishlist-non-added';
}
if ( 'text' === $type ) {
echo '';
echo '';
echo '' . esc_html( $text ) . '';
echo '';
} else {
echo '';
echo '';
echo '';
}
}
/**
* Fetch all items in the wishlist.
*
* @return array The list of wishlist item IDs.
*/
public static function fetch_wishlist() {
if ( isset( $_COOKIE['tpc_course_wishlist'] ) && ! empty( $_COOKIE['tpc_course_wishlist'] ) ) {
return explode( ',', $_COOKIE['tpc_course_wishlist'] );
}
return array();
}
}
Upstudy_Wishlist::instance();