Skip to content

Commit

Permalink
Use static cache so the REST user authenticated can be test-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Aug 28, 2024
1 parent c7d542d commit 5d26a32
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 11 additions & 11 deletions classes/PodsRESTFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ class PodsRESTFields {
*/
protected $pod = null;

/**
* The user ID for the authenticated user.
*
* @var int
*/
private static $rest_user_id;

/**
* Constructor for class
*
Expand Down Expand Up @@ -115,13 +108,20 @@ public function set_pod( $pod ) {
* @return bool
*/
public static function is_rest_authenticated(): bool {
if ( isset( self::$rest_user_id ) ) {
return ! empty( self::$rest_user_id );
$is_rest_authenticated = (bool) pods_static_cache_get( __FUNCTION__, __CLASS__ );

if ( $is_rest_authenticated ) {
return true;
}

self::$rest_user_id = wp_validate_application_password( get_current_user_id() );
$is_rest_authenticated = (
is_user_logged_in()
|| wp_validate_application_password( get_current_user_id() )
);

pods_static_cache_set( __FUNCTION__, (int) $is_rest_authenticated, __CLASS__ );

return ! empty( self::$rest_user_id );
return $is_rest_authenticated;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/codeception/wpunit/Pods/PodsRESTFieldsTest.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d26a32

Please sign in to comment.