-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Localize schema for wp api client #1498
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 |
---|---|---|
|
@@ -317,6 +317,52 @@ function gutenberg_extend_wp_api_backbone_client() { | |
} ) ); | ||
}; | ||
JS; | ||
|
||
/** | ||
* @var WP_REST_Server $wp_rest_server | ||
*/ | ||
global $wp_rest_server; | ||
|
||
// Ensure the rest server is initialized. | ||
if ( empty( $wp_rest_server ) ) { | ||
/** This filter is documented in wp-includes/rest-api.php */ | ||
$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); | ||
$wp_rest_server = new $wp_rest_server_class(); | ||
|
||
/** This filter is documented in wp-includes/rest-api.php */ | ||
do_action( 'rest_api_init', $wp_rest_server ); | ||
} | ||
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. You can use |
||
|
||
// Load the schema. | ||
$schema_request = new WP_REST_Request( 'GET', '/wp/v2' ); | ||
$schema_response = $wp_rest_server->dispatch( $schema_request ); | ||
$schema = null; | ||
if ( ! $schema_response->is_error() ) { | ||
$schema = $schema_response->get_data(); | ||
} | ||
|
||
// Localize the wp-api settings and schema. | ||
$settings = array( | ||
'root' => esc_url_raw( get_rest_url() ), | ||
'nonce' => wp_create_nonce( 'wp_rest' ), | ||
'versionString' => 'wp/v2/', | ||
'schema' => $schema, | ||
'cacheSchema' => true, | ||
); | ||
|
||
/** | ||
* Filter the JavaScript Client settings before localizing. | ||
* | ||
* Enables modifying the settings values sent to the JS client. | ||
* | ||
* @param array $settings The WP-API JS Client settings. | ||
*/ | ||
$settings = apply_filters( 'wp_api_js_client_settings', $settings ); | ||
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 should probably be a Gutenberg-specific filter name, no? |
||
|
||
wp_deregister_script( 'wp-api' ); | ||
wp_register_script( 'wp-api', "/wp-includes/js/wp-api$suffix.js", array( 'jquery', 'backbone', 'underscore' ), false, 1 ); | ||
wp_localize_script( 'wp-api', 'wpApiSettings', $settings ); | ||
|
||
wp_add_inline_script( 'wp-api', $script ); | ||
} | ||
|
||
|
@@ -433,7 +479,7 @@ function gutenberg_scripts_and_styles( $hook ) { | |
); | ||
|
||
// Initialize the editor. | ||
wp_add_inline_script( 'wp-editor', 'wp.api.init().done( function() { wp.editor.createEditorInstance( \'editor\', window._wpGutenbergPost ); } );' ); | ||
wp_add_inline_script( 'wp-editor', 'wp.editor.createEditorInstance( \'editor\', window._wpGutenbergPost );' ); | ||
|
||
/** | ||
* Styles | ||
|
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.
Should this block not be replaced with
$wp_rest_server = rest_get_server()
, and then eliminate theglobal
?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.
Even better:
rest_do_request
as noted above