Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SaculRennorb opened this issue Jan 17, 2023 · 1 comment

Comments

@SaculRennorb
Copy link

This

public static function getUserPatronage( $user = false ) {
if ( self::$current_user_pledge_amount != -1 ) {
return self::$current_user_pledge_amount;
}

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant