Add fictioneer_patreon_tiers_valid() function
And a filter.
This commit is contained in:
parent
4dec87b117
commit
2fa25630f8
10
FILTERS.md
10
FILTERS.md
@ -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 user’s bookmarks.
|
||||
* $discord (string|null) – Optional. List item with link to the site’s 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 user’s 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 user’s Patreon tiers. Can be empty.
|
||||
|
@ -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 ) {
|
||||
|
@ -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
|
||||
// =============================================================================
|
||||
|
@ -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'],
|
||||
|
Loading…
x
Reference in New Issue
Block a user