$target ) { add_rewrite_rule( $pattern, $target, 'top' ); } } /** * The five rules, pattern => target, in registration order. * * One source, so registration and `missing_rules()` cannot drift: a rule * added here is both registered AND checked for, and the presence check * can never silently pass because it was comparing against a stale copy * of the list. * * Order is load-bearing — see the path-insertion note on * `register_rewrite_rules()`. An ordered map preserves it. * * @return array */ private static function rules(): array { return array( '^\.well-known/oauth-protected-resource/(.+?)/?$' => 'index.php?' . self::QUERY_VAR . '=pr-metadata&' . self::RESOURCE_VAR . '=$matches[1]', '^\.well-known/oauth-authorization-server/?$' => 'index.php?' . self::QUERY_VAR . '=as-metadata', '^\.well-known/oauth-protected-resource/?$' => 'index.php?' . self::QUERY_VAR . '=pr-metadata', '^authorize/?$' => 'index.php?' . self::QUERY_VAR . '=authorize', '^token/?$' => 'index.php?' . self::QUERY_VAR . '=token', ); } /** * Which of our rules are absent from the stored `rewrite_rules` option. * * Read-only and cheap: `rewrite_rules` is an autoloaded option, so this * costs an array lookup, not a query. Public because the troubleshooter * reports the same list the flush guard acts on. * * Returns an empty array when permalinks are plain — there is no rewrite * table at all then, so "missing" is not the right description of the * problem and flushing would not fix it. `rewrites_are_possible()` tells * the two cases apart. * * @return array Absent patterns, in registration order. */ public static function missing_rules(): array { if ( ! self::rewrites_are_possible() ) { return array(); } $stored = get_option( 'rewrite_rules' ); if ( ! is_array( $stored ) ) { // No table yet, but permalinks ARE pretty: every rule is missing. return array_keys( self::rules() ); } $missing = array(); foreach ( array_keys( self::rules() ) as $pattern ) { if ( ! isset( $stored[ $pattern ] ) ) { $missing[] = $pattern; } } return $missing; } /** * Whether this site can serve rewrite-based routes at all. * * With plain permalinks WordPress keeps no rewrite table, so * `.well-known/*`, `/authorize` and `/token` can never resolve however * often they are flushed. The operator has to choose a permalink * structure; that is a different sentence from "your rules went missing", * and the two must not be reported as the same failure. * * @return bool */ public static function rewrites_are_possible(): bool { return '' !== (string) get_option( 'permalink_structure', '' ); } /** * Whitelist the query var. Wired by Main.php on `query_vars` filter. * * @param array $vars Existing query vars. * @return array */ public function add_query_var( array $vars ): array { $vars[] = self::QUERY_VAR; $vars[] = self::RESOURCE_VAR; return $vars; } /** * Dispatcher for the WP `parse_request` action. * * Wired by Main.php on `parse_request`. Reads the OAuth query var and * delegates to the appropriate controller. `exit`s on match. * * @param \WP $wp WP object holding query_vars. * @return void */ public function parse_request( \WP $wp ): void { if ( ! isset( $wp->query_vars[ self::QUERY_VAR ] ) ) { return; } $route = (string) $wp->query_vars[ self::QUERY_VAR ]; switch ( $route ) { case 'as-metadata': DiscoveryController::instance()->render_authorization_server_metadata(); break; case 'pr-metadata': $suffix = isset( $wp->query_vars[ self::RESOURCE_VAR ] ) ? (string) $wp->query_vars[ self::RESOURCE_VAR ] : ''; DiscoveryController::instance()->render_protected_resource_metadata( $suffix ); break; case 'authorize': if ( 'POST' === self::request_method() ) { AuthorizationController::instance()->handle_post(); } else { AuthorizationController::instance()->handle_get(); } break; case 'token': TokenController::instance()->handle(); break; default: // Unknown route via our query var: styled 404 (was a blank body). MessagePage::render( 404, __( 'Page not found', 'acrossai-mcp-manager' ), array( __( 'This address is not a valid OAuth endpoint on this site.', 'acrossai-mcp-manager' ), ) ); } } /** * @return string HTTP method (uppercased). */ private static function request_method(): string { return isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( (string) $_SERVER['REQUEST_METHOD'] ) : 'GET'; } }