patreon-wordpress icon indicating copy to clipboard operation
patreon-wordpress copied to clipboard

`getUserPatronage` only works correctly for the first user you call it with

Open SaculRennorb opened this issue 2 years ago • 1 comments

This https://github.com/Patreon/patreon-wordpress/blob/d7f28ff5f3bf0c9c55ed7b5087ea11a93b54be70/classes/patreon_wordpress.php#L564-L568

does not make sense. You accept a user argument but only use one static cache variable. Any call to this function will first check that cache which gets filled by the first valid user. You then return this cached value, regardless of the actual user argument.

This is my proposed solution if you want to keep using the simple cache:

   public static function getUserPatronage( $user = false ) {
- 	if ( self::$current_user_pledge_amount != -1 ) {
-		return self::$current_user_pledge_amount;
-	}
-
  	// If user is not given, try to get the current user attribute ID will be 0 if there is no logged in user
  	if ( $user == false ) {
+		if ( self::$current_user_pledge_amount != -1 ) {
+ 			return self::$current_user_pledge_amount;
+ 		}
  
  		$user = wp_get_current_user();
  	}

It only uses the cache if you actually request the current user. Ofcourse this isnt perfect, and also not the only place that this would have to be fixed, but i hope you get the general idea.

SaculRennorb avatar Jan 17 '23 13:01 SaculRennorb

Also i should add that this is not the only function that has this issue. getUserPatronageDuration, getUserPatronageLevel and some others are affected.

SaculRennorb avatar Jan 17 '23 13:01 SaculRennorb