WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Авторские Темы для WordPress
класс не описан

Automattic\WooCommerce\Admin\Features\Navigation

Favorites{} WC 1.0

Contains logic for the WooCommerce Navigation menu.

Хуков нет.

Возвращает

null. Ничего.

Использование

$Favorites = new Favorites();
// use class methods

Методы

  1. add_item( $item_id, $user_id = null )
  2. get_all( $user_id = null )
  3. instance()
  4. remove_item( $item_id, $user_id = null )
  5. set_meta_value( $user_id, $favorites )

Код Favorites{} WC 5.4.1

<?php
class Favorites {

	/**
	 * Array index of menu capability.
	 *
	 * @var int
	 */
	const META_NAME = 'navigation_favorites';

	/**
	 * Get class instance.
	 */
	final public static function instance() {
		if ( ! static::$instance ) {
			static::$instance = new static();
		}
		return static::$instance;
	}

	/**
	 * Set given favorites string to the user meta data.
	 *
	 * @param string|number $user_id User id.
	 * @param array         $favorites Array of favorite values to set.
	 */
	private static function set_meta_value( $user_id, $favorites ) {
		Loader::update_user_data_field( $user_id, self::META_NAME, wp_json_encode( (array) $favorites ) );
	}

	/**
	 * Add item to favorites
	 *
	 * @param string        $item_id Identifier of item to add.
	 * @param string|number $user_id Identifier of user to add to.
	 * @return WP_Error|Boolean   Throws exception if item already exists.
	 */
	public static function add_item( $item_id, $user_id = null ) {
		$user = $user_id ?? get_current_user_id();

		if ( ! $user || ! $item_id ) {
			return new \WP_Error(
				'woocommerce_favorites_invalid_request',
				__( 'Sorry, invalid request', 'woocommerce' )
			);
		}

		$all_favorites = self::get_all( $user );

		if ( in_array( $item_id, $all_favorites, true ) ) {
			return new \WP_Error(
				'woocommerce_favorites_already_exists',
				__( 'Favorite already exists', 'woocommerce' )
			);
		}

		$all_favorites[] = $item_id;

		self::set_meta_value( $user, $all_favorites );

		return true;
	}

	/**
	 * Remove item from favorites
	 *
	 * @param string        $item_id Identifier of item to remove.
	 * @param string|number $user_id Identifier of user to remove from.
	 * @return \WP_Error|Boolean   Throws exception if item does not exist.
	 */
	public static function remove_item( $item_id, $user_id = null ) {
		$user = $user_id ?? get_current_user_id();

		if ( ! $user || ! $item_id ) {
			return new \WP_Error(
				'woocommerce_favorites_invalid_request',
				__( 'Sorry, invalid request', 'woocommerce' )
			);
		}

		$all_favorites = self::get_all( $user );

		if ( ! in_array( $item_id, $all_favorites, true ) ) {
			return new \WP_Error(
				'woocommerce_favorites_does_not_exist',
				__( 'Favorite item not found', 'woocommerce' )
			);
		}

		$remaining = array_values( array_diff( $all_favorites, [ $item_id ] ) );

		self::set_meta_value( $user, $remaining );

		return true;
	}

	/**
	 * Get all registered favorites.
	 *
	 * @param string|number $user_id Identifier of user to query.
	 * @return WP_Error|Array
	 */
	public static function get_all( $user_id = null ) {
		$user = $user_id ?? get_current_user_id();

		if ( ! $user ) {
			return new \WP_Error(
				'woocommerce_favorites_invalid_request',
				__( 'Sorry, invalid request', 'woocommerce' )
			);
		}

		$response = Loader::get_user_data_field( $user, self::META_NAME );

		return $response ? json_decode( $response, true ) : array();
	}

}