Skip to content
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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Copy link
Member

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 the global?

Copy link
Member

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

/** 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 );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use rest_do_request instead of $wp_rest_server->dispatch, as done elsewhere in the codebase.


// 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 );
Copy link
Member

Choose a reason for hiding this comment

The 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 );
}

Expand Down Expand Up @@ -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
Expand Down