From 93f88321fa4cfe137969a5fb387d6007cda71a66 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Fri, 17 Jan 2025 13:05:40 +0100 Subject: [PATCH] Update Patreon API --- ACTIONS.md | 31 +++++++++++++++++++-------- includes/functions/_helpers-users.php | 2 +- includes/functions/_module-oauth.php | 11 +++++----- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/ACTIONS.md b/ACTIONS.md index 6c01ee7a..f3387d70 100644 --- a/ACTIONS.md +++ b/ACTIONS.md @@ -119,15 +119,28 @@ Fires after an user has been successfully created or logged-in via the OAuth 2.0 * $user (WP_User) – The user object. **$args:** -* $channel (string) – Either `discord`, `patreon`, `twitch`, or `google`. -* $uid (string) – External unique user ID from the linked account. Unsanitized. -* $username (string) – The external username. Unsanitized. -* $nickname (string) – The external nickname (or same as username). Unsanitized. -* $email (string) – The external email address. Unsanitized. -* $avatar_url (string) – The external avatar URL. Unsanitized. -* $patreon_tiers (array) – The relevant Patreon tiers or an empty array. Unsanitized. -* $new (boolean) – Whether this is a newly created user. -* $merged (boolean) – Whether the account has been newly linked to an existing user. +* 'channel' (string) – Either `discord`, `patreon`, `twitch`, or `google`. +* 'uid' (string) – External unique user ID from the linked account. Unsanitized. +* 'username' (string) – The external username. Unsanitized. +* 'nickname' (string) – The external nickname (or same as username). Unsanitized. +* 'email' (string) – The external email address. Unsanitized. +* 'avatar_url' (string) – The external avatar URL. Unsanitized. +* 'patreon_tiers' (array) – Associative array (Tier ID => Array) with the relevant Patreon tiers or an empty array. Unsanitized. + * 'tier' (string) – Tier title. + * 'title' (string) – Tier title (again, yes). + * 'description' (string) – Tier description or empty. + * 'published' (boolean) – Whether the tier is published. + * 'amount_cents' (int) – Tier cent amount. + * 'timestamp' (int) – Unix timestamp (GMT) of the authentication in seconds. + * 'id' (int) – Tier ID (also used as array key). +* 'patreon_membership' (array) – Array with the Patreon membership data or an empty array. Unsanitized. + * 'lifetime_support_cents' (int) – The total amount that the member has ever paid to the campaign in the campaign's currency. `0` if never paid. + * 'last_charge_date' (string|null) – Datetime (UTC ISO) of last attempted charge. `null` if never charged. + * 'last_charge_status' (string|null) – The result of the last attempted charge. The only successful status is `'Paid'`. `null` if never charged. One of `'Paid'`, `'Declined'`, `'Deleted'`, `'Pending'`, `'Refunded'`, `'Fraud'`, `'Refunded by Patreon'`, `'Other'`, `'Partially Refunded'`, `'Free Trial'`. + * 'next_charge_date' (string|null) – Datetime (UTC ISO) of next charge. `null` if annual pledge downgrade. + * 'patron_status' (string|null) – One of `'active_patron'`, `'declined_patron'`, `'former_patron'`. A `null` value indicates the member has never pledged. +* 'new' (boolean) – Whether this is a newly created user. +* 'merged' (boolean) – Whether the account has been newly linked to an existing user. --- diff --git a/includes/functions/_helpers-users.php b/includes/functions/_helpers-users.php index 9862c5d5..d3abc605 100644 --- a/includes/functions/_helpers-users.php +++ b/includes/functions/_helpers-users.php @@ -345,7 +345,7 @@ function fictioneer_patreon_tiers_valid( $user = null ) { * @param int|WP_User|null $user The user object or user ID. Defaults to current user. * * @return array Empty array if not a patron, associative array otherwise. Includes the - * keys 'valid', 'is_follower', 'lifetime_support_cents', 'last_charge_date', + * keys 'valid', 'lifetime_support_cents', 'last_charge_date', * 'last_charge_status', 'next_charge_date', 'patron_status', and 'tiers'. * Tiers is an array of tiers with the keys 'id', 'title', 'description', * 'published', 'amount_cents', and 'timestamp'. diff --git a/includes/functions/_module-oauth.php b/includes/functions/_module-oauth.php index 9d71ab57..a8a06199 100644 --- a/includes/functions/_module-oauth.php +++ b/includes/functions/_module-oauth.php @@ -491,6 +491,7 @@ function fictioneer_oauth2_make_user( $user_data, $cookie ) { 'email' => $user_data['email'], 'avatar_url' => $user_data['avatar'], 'patreon_tiers' => $user_data['tiers'] ?? [], + 'patreon_membership' => $user_data['membership'] ?? [], 'new' => $new, 'merged' => $merged ) @@ -790,7 +791,7 @@ function fictioneer_oauth2_patreon( $token_response, $cookie ) { // Build params $params = '?fields' . urlencode( '[user]' ) . '=email,first_name,image_url,is_email_verified'; $params .= '&fields' . urlencode( '[tier]' ) . '=title,amount_cents,published,description'; - $params .= '&fields' . urlencode( '[member]' ) . '=lifetime_support_cents,is_follower,last_charge_date,last_charge_status,next_charge_date,patron_status'; + $params .= '&fields' . urlencode( '[member]' ) . '=lifetime_support_cents,campaign_lifetime_support_cents,last_charge_date,last_charge_status,next_charge_date,patron_status'; $params .= '&include=memberships.currently_entitled_tiers'; // Retrieve user data from Patreon @@ -827,13 +828,13 @@ function fictioneer_oauth2_patreon( $token_response, $cookie ) { // Tiers data foreach ( $user->included as $node ) { if ( isset( $node->type ) && $node->type === 'tier' ) { - $tiers[] = array( + $tiers[ $node->id ] = array( 'tier' => sanitize_text_field( $node->attributes->title ), 'title' => sanitize_text_field( $node->attributes->title ), 'description' => wp_kses_post( $node->attributes->description ?? '' ), 'published' => filter_var( $node->attributes->published ?? 0, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ), 'amount_cents' => absint( $node->attributes->amount_cents ?? 0 ), - 'timestamp' => time(), + 'timestamp' => current_time( 'U', true ), 'id' => $node->id ); $tier_ids[] = $node->id; @@ -846,12 +847,10 @@ function fictioneer_oauth2_patreon( $token_response, $cookie ) { isset( $node->type ) && $node->type === 'member' && isset( $node->attributes ) && - isset( $node->attributes->lifetime_support_cents ) && isset( $node->relationships->currently_entitled_tiers->data ) && in_array( $node->relationships->currently_entitled_tiers->data[0]->id, $tier_ids ) ) { - $membership['is_follower'] = $node->attributes->is_follower ?? 0; - $membership['lifetime_support_cents'] = $node->attributes->lifetime_support_cents ?? 0; + $membership['lifetime_support_cents'] = $node->attributes->lifetime_support_cents ?? $node->attributes->campaign_lifetime_support_cents ?? 0; $membership['last_charge_date'] = $node->attributes->last_charge_date ?? null; $membership['last_charge_status'] = $node->attributes->last_charge_status ?? null; $membership['next_charge_date'] = $node->attributes->next_charge_date ?? null;