-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Connectors: Stream connection status checks to avoid blocking page render #76546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -427,8 +427,6 @@ function _gutenberg_get_connector_script_module_data( array $data ): array { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| return $data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $registry = \WordPress\AiClient\AiClient::defaultRegistry(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Build a slug-to-file map for plugin installation status. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! function_exists( 'get_plugins' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -448,11 +446,6 @@ function _gutenberg_get_connector_script_module_data( array $data ): array { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| $auth_out['settingName'] = $auth['setting_name'] ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $auth_out['credentialsUrl'] = $auth['credentials_url'] ?? null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $auth_out['keySource'] = _gutenberg_get_api_key_source( $connector_id, $auth['setting_name'] ?? '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $auth_out['isConnected'] = $registry->hasProvider( $connector_id ) && $registry->isProviderConfigured( $connector_id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch ( Exception $e ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $auth_out['isConnected'] = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $connector_out = array( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -485,3 +478,59 @@ function _gutenberg_get_connector_script_module_data( array $data ): array { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| remove_filter( 'script_module_data_options-connectors-wp-admin', '_wp_connectors_get_connector_script_module_data' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'script_module_data_options-connectors-wp-admin', '_gutenberg_get_connector_script_module_data' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Streams connector connection-status checks to the browser after the page | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * has been flushed, so the initial render is not blocked by slow HTTP | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * validation requests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Each resolved status is delivered as an inline <script> that writes to | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * window.__connectorStatuses and dispatches a CustomEvent so the React UI | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * can update progressively. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @access private | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function _gutenberg_stream_connector_statuses(): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! class_exists( '\WordPress\AiClient\AiClient' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Flush all output buffers so the browser receives the full page | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // (including React bootstrap scripts) immediately. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| while ( ob_get_level() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ob_end_flush(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| flush(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $registry = \WordPress\AiClient\AiClient::defaultRegistry(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ( wp_get_connectors() as $connector_id => $connector_data ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $auth = $connector_data['authentication']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( 'api_key' !== $auth['method'] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sleep( 3 ); // Simulate slow validation for demo purposes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just for demo to simulate a slow check. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $is_connected = $registry->hasProvider( $connector_id ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| && $registry->isProviderConfigured( $connector_id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch ( Exception $e ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $is_connected = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| printf( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| '<script>' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| . '(window.__connectorStatuses=window.__connectorStatuses||{})[%s]=%s;' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| . 'document.dispatchEvent(new CustomEvent("connector-status",{detail:{id:%s,connected:%s}}));' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| . '</script>', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wp_json_encode( $connector_id ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $is_connected ? 'true' : 'false', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wp_json_encode( $connector_id ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $is_connected ? 'true' : 'false' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+521
to
+530
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple important suggestions here:
Suggested change
For reference for where this was recently done in core similarly: In PhpStorm: Before:
After:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Flush each result so the browser processes it immediately. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| flush(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_action( 'admin_footer-settings_page_options-connectors-wp-admin', '_gutenberg_stream_connector_statuses' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could conflict with the output buffer used for client-side media processing, which is now slated for 7.1.
Specifically, see WordPress/wordpress-develop#11324 and how
wp_start_cross_origin_isolation_output_buffer()starts an output buffer viawp_set_up_cross_origin_isolation(). That said, it currently only added currently on these screens:So while it doesn't seem it will be a problem in 7.0, and maybe it won't even be a problem in 7.1, I'm concerned it could cause compatibility problems elsewhere for perhaps other extensions that are output buffering the admin screens.
One possibility is to run this at the
shutdownaction. There is already thewp_ob_end_flush_all()function which runs atshutdownpriority 1. So you could run the streaming of these scripts atshutdownpriority 2 (or higher).