Skip to content

Preload REST API responses for plugins on Connectors screen and add support for preloading non-OK responses#11790

Open
westonruter wants to merge 7 commits into
WordPress:trunkfrom
westonruter:trac-65215
Open

Preload REST API responses for plugins on Connectors screen and add support for preloading non-OK responses#11790
westonruter wants to merge 7 commits into
WordPress:trunkfrom
westonruter:trac-65215

Conversation

@westonruter
Copy link
Copy Markdown
Member

@westonruter westonruter commented May 11, 2026

Trac ticket: https://core.trac.wordpress.org/ticket/65215

This backports the logic in this Gutenberg PR, which also must be applied to core to see the preloading take effect on the Connectors screen:

Additionally, improves REST API preloading in a few ways ways:

First of all, support is added to allow a non-OK response for a preloaded request to be sent to the preloading middleware. Without this, all of the plugins on the Connectors screen would cause unconditional 404 REST API responses when accessing the page:

image

The data format for a preloaded request was previously two possibilities:

  1. string: The REST API path to preload.
  2. array{ 0: string, 1?: 'OPTIONS'|'GET' }. A tuple with the first item being the REST API path, and the second item being the method.

This PR modifies the tuple to add a third item for the allowed_statuses:

array{ 0: string, 1?: 'OPTIONS'|'GET', 2?: int<100, 599>|int<100, 599>[] }

This allows us now to do the following:

add_filter(
	'options-connectors-wp-admin_preload_paths',
	static function ( array $preload_paths ): array {
		$preload_paths[] = array( '/wp/v2/plugins/ai/ai?context=edit', 'GET', array( 200, 404 ) );
		return $preload_paths;
	}
);

This allows both 200 OK responses and 404 Not Found responses to be served to the preload middleware. If this 3rd item of the tuple is omitted, then it retains the previous behavior of only allowing 200 OK.

To further support this enhancement, full PHPStan typing was added to the rest_preload_api_request() function, and any PHPStan rule level 10 errors were addressed:

  2937   Function rest_preload_api_request() has parameter $memo with no value type specified in iterable type array.
         🪪  missingType.iterableValue
         💡  See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         at src/wp-includes/rest-api.php:2937
  2937   Function rest_preload_api_request() return type has no value type specified in iterable type array.
         🪪  missingType.iterableValue
         💡  See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         at src/wp-includes/rest-api.php:2937
  2961   Parameter #1 $value of function untrailingslashit expects string, string|false given.
         🪪  argument.type
         at src/wp-includes/rest-api.php:2961
  2979   Offset 'path' might not exist on array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?:
         string}.
         🪪  offsetAccess.notFound
         at src/wp-includes/rest-api.php:2979
  2979   Parameter #1 $method of class WP_REST_Request constructor expects string, string|false given.
         🪪  argument.type
         at src/wp-includes/rest-api.php:2979
  2990   Parameter #1 $embed of function rest_parse_embed_param expects array|string, mixed given.
         🪪  argument.type
         at src/wp-includes/rest-api.php:2990
  2991   Parameter #1 $response of method WP_REST_Server::response_to_data() expects WP_REST_Response, mixed given.
         🪪  argument.type
         at src/wp-includes/rest-api.php:2991
  2994   Cannot access offset string on mixed.
         🪪  offsetAccess.nonOffsetAccessible
         at src/wp-includes/rest-api.php:2994
  2996   Cannot access property $headers on mixed.
         🪪  property.nonObject
         at src/wp-includes/rest-api.php:2996
  3001   Cannot access property $headers on mixed.
         🪪  property.nonObject
         at src/wp-includes/rest-api.php:3001

Use of AI Tools

AI assistance: Yes
Tool(s): Clause Code
Model(s): Claude Opus 4.7
Used for: Research for PHPStan types.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment thread src/wp-includes/build/pages/options-connectors/page-wp-admin.php Outdated
@westonruter
Copy link
Copy Markdown
Member Author

@pento As you've been around lately and you introduced rest_preload_api_request() in r44123 and r44172, maybe you'd have review input as well.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/rest-api.php
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

Successfully merging this pull request may close these issues.

2 participants