User_Favourites
extends Collection
in package
Provides post or recipe ids for favourite recipes by user.
- A user is someone who has login credentials.
- This class does NOT use transients, unlike the other Collection sub-classes. This is done to provide immediate response to users updating favourites.
- User favourites are stored in user-meta.
- Provide functionality to add and manage these favourites.
Tags
Table of Contents
Constants
- ACTION_ADD = 'add_favourite'
- The AJAX action to add a favourite.
- ACTION_DEL = 'del_favourite'
- The AJAX action to delete a favourite.
- AJAX_VARS = array('action' => '', 'recipe' => '', 'token' => '', 'user' => '')
- The AJAX post vars defaults.
- FAV_BUTTON_CONTAINER = 'button-favourite.php'
- The favourite button partial filename.
- FAVOURITES_KEY = 'pfic_favourites'
- The user meta key for the favourites.
- TRANSIENT_EXPIRATION_DEBUG = MINUTE_IN_SECONDS * 5
- The transient expiration for debugging.
- TRANSIENT_EXPIRATION_PRODUCTION = WEEK_IN_SECONDS
- The transient expiration for production
- TRANSIENT_KEY = ''
- Collection members transient key.
Properties
- $fav_button_container : string
- The fully qualified path to the add favourite button container.
- $instances : array<string|int, object>
- The array of instances of this class and sub-classes.
- $recipe : Recipe
- The instance of the recipe class.
- $transient_expiration : int
- The transient expiration.
Methods
- get_instance() : object
- Creates and/or returns the instance of this class.
- get_post_ids() : array<string|int, int>
- Return the array of user favourite post ids.
- get_recipe_ids() : array<string|int, int>
- Return the array of user favourite recipe ids.
- handle_ajax_request() : void
- The AJAX handler for the favourite action.
- rebuild_collection() : array<string|int, int>|bool
- Rebuild the array of collection post ids and set as transient.
- render_buttons() : void
- Render the favourite buttons.
- __construct() : void
- Initialise the class and set its properties.
- add() : int
- Add a favurite to user meta.
- delete() : int
- Delete a favourite from user meta.
Constants
ACTION_ADD
The AJAX action to add a favourite.
protected
string
ACTION_ADD
= 'add_favourite'
Tags
ACTION_DEL
The AJAX action to delete a favourite.
protected
string
ACTION_DEL
= 'del_favourite'
Tags
AJAX_VARS
The AJAX post vars defaults.
protected
array<string|int, string>
AJAX_VARS
= array('action' => '', 'recipe' => '', 'token' => '', 'user' => '')
{ The fields returned by the AJAX favourites request. @type string $action The AJAX action. @type string $recipe The recipe id. @type string $token The AJAX token. @type string $user The user id. }
Tags
FAV_BUTTON_CONTAINER
The favourite button partial filename.
protected
string
FAV_BUTTON_CONTAINER
= 'button-favourite.php'
Tags
FAVOURITES_KEY
The user meta key for the favourites.
protected
string
FAVOURITES_KEY
= 'pfic_favourites'
Tags
TRANSIENT_EXPIRATION_DEBUG
The transient expiration for debugging.
protected
int
TRANSIENT_EXPIRATION_DEBUG
= MINUTE_IN_SECONDS * 5
Tags
TRANSIENT_EXPIRATION_PRODUCTION
The transient expiration for production
protected
int
TRANSIENT_EXPIRATION_PRODUCTION
= WEEK_IN_SECONDS
Tags
TRANSIENT_KEY
Collection members transient key.
protected
string
TRANSIENT_KEY
= ''
- MUST override this in sub-classes.
Tags
Properties
$fav_button_container
The fully qualified path to the add favourite button container.
protected
string
$fav_button_container
Tags
$instances
The array of instances of this class and sub-classes.
protected
static array<string|int, object>
$instances
= array()
Tags
$recipe
The instance of the recipe class.
protected
Recipe
$recipe
Tags
$transient_expiration
The transient expiration.
protected
int
$transient_expiration
Tags
Methods
get_instance()
Creates and/or returns the instance of this class.
public
static get_instance() : object
Tags
Return values
object —The instance of this class.
get_post_ids()
Return the array of user favourite post ids.
public
get_post_ids() : array<string|int, int>
Tags
Return values
array<string|int, int> —Array of post ids.
get_recipe_ids()
Return the array of user favourite recipe ids.
public
get_recipe_ids() : array<string|int, int>
Tags
Return values
array<string|int, int> —Array of recipe ids.
handle_ajax_request()
The AJAX handler for the favourite action.
public
handle_ajax_request() : void
Tags
rebuild_collection()
Rebuild the array of collection post ids and set as transient.
public
rebuild_collection() : array<string|int, int>|bool
- MUST override this in child classes.
- Return the array of post_ids.
Tags
Return values
array<string|int, int>|bool —Array of post ids or false if no data
render_buttons()
Render the favourite buttons.
public
render_buttons() : void
- Provide a button for the user to toggle adding or removing a recipe from their favourites.
Tags
__construct()
Initialise the class and set its properties.
protected
__construct() : void
Tags
add()
Add a favurite to user meta.
protected
add(int $recipe_id, int $user_id) : int
Parameters
- $recipe_id : int
-
The recipe id.
- $user_id : int
-
The user id.
Tags
Return values
int —The result of the update_user_meta() call. 0 on no update.
delete()
Delete a favourite from user meta.
protected
delete(int $recipe_id, int $user_id) : int
Parameters
- $recipe_id : int
-
The recipe id.
- $user_id : int
-
The user id.
Tags
Return values
int —The result of the update_user_meta() call. 0 on no update.