Improve fictioneer_patreon_tiers_valid()

This commit is contained in:
Tetrakern 2024-05-10 16:35:07 +02:00
parent f2e731475a
commit 42a0aa087e
2 changed files with 13 additions and 9 deletions

View File

@ -1477,10 +1477,10 @@ Filters the intermediate output array of the `fictioneer_user_menu_items()` func
--- ---
### `apply_filters( 'fictioneer_filter_user_patreon_validation', $valid, $user, $patreon_tiers )` ### `apply_filters( 'fictioneer_filter_user_patreon_validation', $valid, $user_id, $patreon_tiers )`
Filters the check result of whether the users Patreon data is still valid. Because there is no continuous connection to Patreon, the data expires after a set amount of time, one week in seconds by default (defined as `FICTIONEER_PATREON_EXPIRATION_TIME`). Filters the check result of whether the users Patreon data is still valid. Because there is no continuous connection to Patreon, the data expires after a set amount of time, one week in seconds by default (defined as `FICTIONEER_PATREON_EXPIRATION_TIME`).
**Parameters:** **Parameters:**
* $valid (boolean) Result of the check. True if valid, false if expired. * $valid (boolean) Result of the check. True if valid, false if expired.
* $user (WP_User) The user the check is for. Invalid if not logged in. * $user_id (int) The user the check is for. 0 if not logged in.
* $patreon_tiers (array) The users Patreon tiers. Can be empty. * $patreon_tiers (array) The users Patreon tiers. Can be empty.

View File

@ -846,7 +846,7 @@ if ( ! function_exists( 'fictioneer_get_override_badge' ) ) {
} }
// ============================================================================= // =============================================================================
// GET PATREON BADGE // PATREON
// ============================================================================= // =============================================================================
if ( ! function_exists( 'fictioneer_get_patreon_badge' ) ) { if ( ! function_exists( 'fictioneer_get_patreon_badge' ) ) {
@ -886,19 +886,23 @@ if ( ! function_exists( 'fictioneer_get_patreon_badge' ) ) {
* *
* @since 5.15.0 * @since 5.15.0
* *
* @param WP_User $user The user. * @param int|WP_User|null $user The user object or user ID. Defaults to current user.
* *
* @return boolean True if still valid, false if expired. * @return boolean True if still valid, false if expired.
*/ */
function fictioneer_patreon_tiers_valid( $user ) { function fictioneer_patreon_tiers_valid( $user = null ) {
// Setup
$user = $user ?? wp_get_current_user();
$user_id = is_numeric( $user ) ? $user : $user->ID;
// Abort conditions... // Abort conditions...
if ( ! $user ) { if ( ! $user_id ) {
return apply_filters( 'fictioneer_filter_user_patreon_validation', false, $user, [] ); return apply_filters( 'fictioneer_filter_user_patreon_validation', false, $user_id, [] );
} }
// Setup // Setup
$patreon_tiers = get_user_meta( $user->ID, 'fictioneer_patreon_tiers', true ); $patreon_tiers = get_user_meta( $user_id, 'fictioneer_patreon_tiers', true );
$patreon_tiers = is_array( $patreon_tiers ) ? $patreon_tiers : []; $patreon_tiers = is_array( $patreon_tiers ) ? $patreon_tiers : [];
$last_updated = empty( $patreon_tiers ) ? 0 : ( $patreon_tiers[0]['timestamp'] ?? 0 ); $last_updated = empty( $patreon_tiers ) ? 0 : ( $patreon_tiers[0]['timestamp'] ?? 0 );
@ -906,7 +910,7 @@ function fictioneer_patreon_tiers_valid( $user ) {
$valid = time() <= $last_updated + FICTIONEER_PATREON_EXPIRATION_TIME; $valid = time() <= $last_updated + FICTIONEER_PATREON_EXPIRATION_TIME;
// Filter and return // Filter and return
return apply_filters( 'fictioneer_filter_user_patreon_validation', $valid, $user, $patreon_tiers ); return apply_filters( 'fictioneer_filter_user_patreon_validation', $valid, $user_id, $patreon_tiers );
} }
// ============================================================================= // =============================================================================