init_hooks(); } public function init_hooks() { // if ( ! session_id() ) { // session_start(); // } add_action( 'woocommerce_before_shop_loop_item', array( __CLASS__, 'render_floating_section' ), 9 ); add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'render_add_to_wishlist' ), 41 ); add_action( 'wp_ajax_brandy_add_to_wishlist', array( $this, 'ajax_atw' ) ); add_action( 'wp_ajax_brandy_remove_wishlist_item', array( $this, 'ajax_remove_wishlist_item' ) ); add_action( 'wp_ajax_nopriv_brandy_add_to_wishlist', array( $this, 'ajax_atw' ) ); add_action( 'wp_ajax_nopriv_brandy_remove_wishlist_item', array( $this, 'ajax_remove_wishlist_item' ) ); //Shortcode removed } public static function get_user_wishlist_items() { $list = self::get_wishlist_items(); $new_list = array_filter( $list, function( $id ) { return ! empty( \wc_get_product( $id ) ); } ); if ( ! empty( array_diff( $list, $new_list ) ) ) { self::update_wishlist_items( $new_list ); } return $new_list; } public static function render_floating_section() { global $product; if ( empty( $product ) ) { return; } self::render_floating_fragment( $product ); } public static function render_floating_fragment( $product ) { $is_added = in_array( $product->get_id(), self::get_user_wishlist_items() ); ?>
>
>
get_id(), self::get_user_wishlist_items() ); ?>
>
>
$input_product->get_id(), 'data-nonce' => wp_create_nonce( 'brandy_add_to_wishlist' ), ); echo esc_attr( brandy_print_dom_attributes( $attributes ) ); } public function ajax_atw() { try { $nonce = ''; if ( isset( $_GET['nonce'] ) ) { $nonce = sanitize_text_field( $_GET['nonce'] ); } elseif ( isset( $_POST['nonce'] ) ) { $nonce = sanitize_text_field( $_POST['nonce'] ); } if ( ! wp_verify_nonce( $nonce, 'brandy_add_to_wishlist' ) ) { wp_send_json_error( array( 'message' => __( 'Verify nonce failed', 'brandy' ), ) ); } if ( ! function_exists( 'wc_get_product' ) ) { throw new \Error( __( 'WooCommerce uninstalled', 'brandy' ) ); } $product_id = isset( $_GET['product_id'] ) ? sanitize_text_field( $_GET['product_id'] ) : ''; $product = \wc_get_product( $product_id ); if ( empty( $product ) ) { throw __( 'Product not found', 'brandy' ); } $current_wishlist = self::get_wishlist_items(); if ( ! in_array( $product_id, $current_wishlist ) ) { $current_wishlist[] = $product_id; } self::update_wishlist_items( $current_wishlist ); wp_send_json_success( array( 'product_id' => $product_id, 'message' => __( 'Success', 'brandy' ), //PHPCS:ignore 'fragments' => self::get_fragments( $product, true ), ) ); } catch ( \Error $err ) { wp_send_json_error( array( 'message' => $err->getMessage(), ) ); } } public function ajax_remove_wishlist_item() { try { $nonce = ''; if ( isset( $_GET['nonce'] ) ) { $nonce = sanitize_text_field( $_GET['nonce'] ); } elseif ( isset( $_POST['nonce'] ) ) { $nonce = sanitize_text_field( $_POST['nonce'] ); } if ( ! wp_verify_nonce( $nonce, 'brandy_remove_wishlist_item' ) ) { wp_send_json_error( array( 'message' => __( 'Verify nonce failed', 'brandy' ), ) ); } $product_id = isset( $_GET['product_id'] ) ? sanitize_text_field( $_GET['product_id'] ) : ''; $current_wishlist = self::get_wishlist_items(); $pos = array_search( $product_id, $current_wishlist ); if ( false !== $pos ) { array_splice( $current_wishlist, $pos, 1 ); } self::update_wishlist_items( $current_wishlist ); wp_send_json_success( array( 'item_id' => $product_id, 'message' => __( 'Removed', 'brandy' ), 'fragments' => self::get_fragments( \wc_get_product( $product_id ) ), ) ); } catch ( \Error $err ) { wp_send_json_error( array( 'message' => $err->getMessage(), ) ); } } public static function get_fragments( $product, $just_added = false ) { ob_start(); self::render_floating_fragment( $product ); $floating_button_fragment = ob_get_contents(); ob_end_clean(); self::render_add_to_wishlist_fragment( $product, $just_added ); $atw_fragment = ob_get_contents(); ob_end_clean(); return array( 'wishlist_shortcode' => self::wishlist_shortcode(), 'wishlist_floating_fragment' => $floating_button_fragment, 'wishlist_atw_fragment' => $atw_fragment, 'count' => self::count(), ); } public static function render_add_to_wishlist_icon( $wishlisted = false ) { if ( $wishlisted ) : ?> self::get_user_wishlist_items(), ) ); $html = ob_get_contents(); ob_end_clean(); return $html; } public static function render_loading() { echo ""; } public static function get_wishlist_url() { $page_id = WishlistPanel::get_wishlist_page(); if ( empty( $page_id ) ) { $page_url = '#'; } else { $page_url = get_page_link( $page_id ); } return $page_url; } public static function render_view_wishlist_link() { $text = WishlistPanel::get_view_wishlist_text(); ?>