182 lines
6.0 KiB
PHP
182 lines
6.0 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // If this file is called directly, abort.
|
|
|
|
class Upstudy_Wishlist {
|
|
protected static $instance = null;
|
|
|
|
/**
|
|
* Constructor to hook AJAX actions.
|
|
*/
|
|
public function __construct() {
|
|
add_action( 'wp_ajax_upstudy_wishlist_item', [ $this, 'wishlist_ajax_connect' ] );
|
|
add_action( 'wp_ajax_nopriv_upstudy_wishlist_item', [ $this, 'wishlist_ajax_connect' ] );
|
|
add_action( 'wp_ajax_upstudy_wishlist_item_remove', [ $this, 'wishlist_ajax_remove' ] );
|
|
add_action( 'wp_ajax_nopriv_upstudy_wishlist_item_remove', [ $this, 'wishlist_ajax_remove' ] );
|
|
}
|
|
|
|
/**
|
|
* Get the singleton instance of the class.
|
|
*
|
|
* @return Upstudy_Wishlist The instance of the Upstudy_Wishlist class.
|
|
*/
|
|
public static function instance() {
|
|
if ( null === self::$instance ) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Handle adding an item to the wishlist via AJAX.
|
|
*/
|
|
public function wishlist_ajax_connect() {
|
|
check_ajax_referer( 'upstudy-wishlist-ajax-connect', 'security' );
|
|
|
|
$status = array();
|
|
|
|
// Sanitize and validate post_id
|
|
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
|
|
|
|
if ( $post_id > 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 '<a href="#tpc-wishlist-item-link" class="tpc-wishlist-button with-icon-text ' . esc_attr( $class ) . '" data-id="' . esc_attr( $id ) . '">';
|
|
echo '<i class="' . esc_attr( $icon_class ) . '"></i>';
|
|
echo '<span class="wishlist-text">' . esc_html( $text ) . '</span>';
|
|
echo '</a>';
|
|
} else {
|
|
echo '<a href="#tpc-wishlist-item-link" class="tpc-wishlist-button with-icon ' . esc_attr( $class ) . '" data-id="' . esc_attr( $id ) . '">';
|
|
echo '<i class="upstudy-wishlist-wrapper ' . esc_attr( $icon_class ) . '"></i>';
|
|
echo '</a>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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();
|