@@ -21,6 +21,7 @@ public function load() {
2121
2222 add_filter ( 'indieauth_scopes ' , array ( $ this , 'get_indieauth_scopes ' ), 9 );
2323 add_filter ( 'indieauth_response ' , array ( $ this , 'get_indieauth_response ' ), 9 );
24+ add_filter ( 'wp_rest_server_class ' , array ( $ this , 'wp_rest_server_class ' ) );
2425
2526 }
2627
@@ -38,6 +39,29 @@ abstract public static function get_authorization_endpoint();
3839 **/
3940 abstract public static function get_token_endpoint ();
4041
42+
43+ /**
44+ * Prevent caching of unauthenticated status. See comment below.
45+ *
46+ * We don't actually care about the `wp_rest_server_class` filter, it just
47+ * happens right after the constant we do care about is defined. This is taken from the Application Passwords plugin.
48+ *
49+ */
50+ public static function wp_rest_server_class ( $ class ) {
51+ global $ current_user ;
52+ if ( defined ( 'REST_REQUEST ' ) && REST_REQUEST && $ current_user instanceof WP_User && 0 === $ current_user ->ID ) {
53+ /*
54+ * For our authentication to work, we need to remove the cached lack
55+ * of a current user, so the next time it checks, we can detect that
56+ * this is a rest api request and allow our override to happen. This
57+ * is because the constant is defined later than the first get current
58+ * user call may run.
59+ */
60+ $ current_user = null ; // phpcs:ignore
61+ }
62+ return $ class ;
63+ }
64+
4165 /**
4266 * Add authentication information into the REST API Index
4367 *
0 commit comments