Add fictioneer_patreon_tiers_valid() function

And a filter.
This commit is contained in:
Tetrakern 2024-04-27 13:22:01 +02:00
parent 4dec87b117
commit 2fa25630f8
4 changed files with 56 additions and 16 deletions

View File

@ -1446,3 +1446,13 @@ Filters the intermediate output array of the `fictioneer_user_menu_items()` func
* $bookmarks (string|null) Optional. List item with link to the users bookmarks.
* $discord (string|null) Optional. List item with link to the sites Discord server.
* $logout (string) List item with link to log out of the site.
---
### `apply_filters( 'fictioneer_filter_user_patreon_validation', $valid, $user, $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`).
**Parameters:**
* $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.
* $patreon_tiers (array) The users Patreon tiers. Can be empty.

View File

@ -474,22 +474,25 @@ function fictioneer_bypass_password( $required, $post ) {
$user = wp_get_current_user();
if ( $user && $required && get_option( 'fictioneer_enable_patreon_locks' ) ) {
$patreon_lock_tiers = get_post_meta( $post->ID, 'fictioneer_patreon_lock_tiers', true );
$patreon_lock_tiers = is_array( $patreon_lock_tiers ) ? $patreon_lock_tiers : [];
$patreon_lock_amount = absint( get_post_meta( $post->ID, 'fictioneer_patreon_lock_amount', true ) );
$patreon_post_tiers = get_post_meta( $post->ID, 'fictioneer_patreon_lock_tiers', true );
$patreon_post_tiers = is_array( $patreon_post_tiers ) ? $patreon_post_tiers : [];
$patreon_post_amount_cents = absint( get_post_meta( $post->ID, 'fictioneer_patreon_lock_amount', true ) );
if ( $patreon_lock_tiers || $patreon_lock_amount ) {
$patreon_tiers = get_user_meta( $user->ID, 'fictioneer_patreon_tiers', true );
$patreon_tiers = is_array( $patreon_tiers ) ? $patreon_tiers : [];
if (
( $patreon_post_tiers || $patreon_post_amount_cents ) &&
fictioneer_patreon_tiers_valid( $user )
) {
$patreon_user_tiers = get_user_meta( $user->ID, 'fictioneer_patreon_tiers', true );
$patreon_user_tiers = is_array( $patreon_user_tiers ) ? $patreon_user_tiers : [];
foreach ( $patreon_tiers as $tier ) {
foreach ( $patreon_user_tiers as $tier ) {
if ( ! $tier['published'] ?? 0 ) {
continue;
}
$required = ! (
in_array( $tier['id'], $patreon_lock_tiers ) ||
( $patreon_lock_amount > 0 && ( $tier['amount_cents'] ?? 0 ) >= $patreon_lock_amount )
in_array( $tier['id'], $patreon_post_tiers ) ||
( $patreon_post_amount_cents > 0 && ( $tier['amount_cents'] ?? 0 ) >= $patreon_post_amount_cents )
);
if ( ! $required ) {

View File

@ -709,13 +709,10 @@ if ( ! function_exists( 'fictioneer_get_patreon_badge' ) ) {
return $default;
}
// Setup
$patreon_tiers = get_user_meta( $user->ID, 'fictioneer_patreon_tiers', true );
$last_updated = is_array( $patreon_tiers ) ? ( $patreon_tiers[0]['timestamp'] ?? 0 ) : 0;
// Check if still valid if not empty
if ( time() <= $last_updated + FICTIONEER_PATREON_EXPIRATION_TIME ) {
if ( fictioneer_patreon_tiers_valid( $user ) ) {
$label = get_option( 'fictioneer_patreon_label' );
return empty( $label ) ? _x( 'Patron', 'Default Patreon supporter badge label.', 'fictioneer' ) : $label;
}
@ -723,6 +720,37 @@ if ( ! function_exists( 'fictioneer_get_patreon_badge' ) ) {
}
}
/**
* Checks whether the user's Patreon data still valid
*
* Note: Patreon data expires after a set amount of time, one week
* by default defined as FICTIONEER_PATREON_EXPIRATION_TIME.
*
* @since 5.15.0
*
* @param WP_User $user The user.
*
* @return boolean True if still valid, false if expired.
*/
function fictioneer_patreon_tiers_valid( $user ) {
// Abort conditions...
if ( ! $user ) {
return apply_filters( 'fictioneer_filter_user_patreon_validation', false, $user, [] );
}
// Setup
$patreon_tiers = get_user_meta( $user->ID, 'fictioneer_patreon_tiers', true );
$patreon_tiers = is_array( $patreon_tiers ) ? $patreon_tiers : [];
$last_updated = empty( $patreon_tiers ) ? 0 : ( $patreon_tiers[0]['timestamp'] ?? 0 );
// Check
$valid = time() <= $last_updated + FICTIONEER_PATREON_EXPIRATION_TIME;
// Filter and return
return apply_filters( 'fictioneer_filter_user_patreon_validation', $valid, $user, $patreon_tiers );
}
// =============================================================================
// GET UNIQUE USER FINGERPRINT
// =============================================================================

View File

@ -27,8 +27,7 @@ if ( ! get_option( 'fictioneer_enable_oauth' ) ) {
// Setup
$current_user = $args['user'];
$patreon_tiers = get_user_meta( $current_user->ID, 'fictioneer_patreon_tiers', true );
$patreon_timestamp = is_array( $patreon_tiers ) ? ( $patreon_tiers[0]['timestamp'] ?? 0 ) : 0;
$patreon_expired = $patreon_timestamp + FICTIONEER_PATREON_EXPIRATION_TIME < time();
$patreon_expired = ! fictioneer_patreon_tiers_valid( $current_user );
$oauth_providers = [
['discord', 'Discord'],
['twitch', 'Twitch'],