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

Components: withAPIData: Don't restrict to /wp/v2, allow custom endpoints #3536

Closed
wants to merge 1 commit into from
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
7 changes: 5 additions & 2 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,14 @@ function gutenberg_extend_wp_api_backbone_client() {
wp_add_inline_script( 'wp-api', $script );

// Localize the wp-api settings and schema.
$schema_response = rest_do_request( new WP_REST_Request( 'GET', '/wp/v2' ) );
$schema_response = rest_do_request( new WP_REST_Request( 'GET', '/' ) );
if ( ! $schema_response->is_error() ) {
wp_add_inline_script( 'wp-api', sprintf(
'wpApiSettings.cacheSchema = true; wpApiSettings.schema = %s;',
wp_json_encode( $schema_response->get_data() )
wp_json_encode( array(
'namespace' => '',
'routes' => $schema_response->get_data()[ 'routes' ],
Copy link
Member

Choose a reason for hiding this comment

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

Per failing test:

Function array dereferencing is not present in PHP version 5.3 or earlier (PHPCompatibility.PHP.NewFunctionArrayDereferencing.Found)

Further, why do we need to do this change at all? Can we not just encode the data as we had done previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can, but the data has a different shape. Instead of getting an object with keys ( namespace, routes ), we have keys ( name, description, url, home, gmt_offset, timezone_string, namespaces, authentication, routes, permalink_structure ). Note the plural namespaces. Hence the sort-of-faking happening here. How do you suggest proceeding? We can indeed pass the entire data and handle it differently on the client.

Copy link
Member

Choose a reason for hiding this comment

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

As far as I can recall, withAPIData really only cares about the routes property, and if we assume the schema response' routes includes all registered routes, all the better. Other keys can simply be ignored.

Copy link
Contributor

@donnapep donnapep Dec 3, 2017

Choose a reason for hiding this comment

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

I've tested this change both as-is and by reverting the encoding changes as per some discussion with @aduth . Works great!

I can submit a separate PR, but thought it might be easier for someone with commit access to make the change in this PR and put the encoding stuff back in. Cheers!

Never mind. I really need this so I've created PR #3778. Thx.

) )
), 'before' );
}
}
Expand Down