diff --git a/classes/PodsRESTFields.php b/classes/PodsRESTFields.php index 3873d21ea9..66bad2ccd6 100644 --- a/classes/PodsRESTFields.php +++ b/classes/PodsRESTFields.php @@ -102,6 +102,28 @@ public function set_pod( $pod ) { $this->pod = $pod; } + /** + * Validates if a current user or application is logged in. + * + * @return bool + */ + public static function is_rest_authenticated(): bool { + $is_rest_authenticated = (bool) pods_static_cache_get( __FUNCTION__, __CLASS__ ); + + if ( $is_rest_authenticated ) { + return true; + } + + $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 $is_rest_authenticated; + } + /** * Add fields, based on options to REST read/write requests * @@ -237,7 +259,7 @@ public static function field_allowed_to_extend( $field, $pod, $mode ) { // Check if user must be logged in to access all fields and override whether they can use it. if ( $all_fields_can_use_mode && $all_fields_access ) { - $all_fields_can_use_mode = is_user_logged_in(); + $all_fields_can_use_mode = self::is_rest_authenticated(); } // Maybe get the Field object from the Pod. @@ -267,7 +289,7 @@ public static function field_allowed_to_extend( $field, $pod, $mode ) { // Check if user must be logged in to access field and override whether they can use it. if ( $can_use_mode && $access ) { - $can_use_mode = is_user_logged_in(); + $can_use_mode = self::is_rest_authenticated(); } return $can_use_mode; diff --git a/tests/codeception/wpunit/Pods/PodsRESTFieldsTest.php b/tests/codeception/wpunit/Pods/PodsRESTFieldsTest.php index 5ec67e28ab..dd63231ce1 100644 --- a/tests/codeception/wpunit/Pods/PodsRESTFieldsTest.php +++ b/tests/codeception/wpunit/Pods/PodsRESTFieldsTest.php @@ -160,6 +160,8 @@ public function tearDown(): void { $this->full_write_pod_id = null; $this->full_write_pod = null; + pods_static_cache_clear(); + // Reset current user. global $current_user;