Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,6 @@ function gutenberg_extend_wp_api_backbone_client() {
wp_json_encode( $schema_response->get_data() )
), 'before' );
}

/*
* For API requests to happen over HTTP/1.0 methods,
* as HTTP/1.1 methods are blocked in a variety of situations.
*/
wp_add_inline_script( 'wp-api', 'Backbone.emulateHTTP = true;', 'before' );
}

/**
Expand Down
31 changes: 31 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,37 @@ function gutenberg_shim_fix_api_request_plain_permalinks( $scripts ) {
}
add_action( 'wp_default_scripts', 'gutenberg_shim_fix_api_request_plain_permalinks' );

/**
* Shims support for emulating HTTP/1.0 requests in wp.apiRequest
*
* @see https://core.trac.wordpress.org/ticket/43605
*
* @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
*/
function gutenberg_shim_api_request_emulate_http( $scripts ) {
$api_request_fix = <<<JS
( function( wp ) {
var oldApiRequest = wp.apiRequest;
wp.apiRequest = function ( options ) {
if ( options.method ) {
if ( [ 'PATCH', 'PUT', 'DELETE' ].indexOf( options.method.toUpperCase() ) >= 0 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, we could inverse the check to better reflect that we support only GET and POST:

if ( [ 'GET', 'POST' ].indexOf( options.method.toUpperCase() ) === -1 ) {

Aside: Curiosity got me wondering: https://jsperf.com/indexof-array-vs-regexp (tl;dr: This is good)

Copy link
Member Author

Choose a reason for hiding this comment

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

OPTIONS shouldn't be converted, either. (Neither should HEAD, CONNECT, or TRACE, even though we don't specifically use them anywhere.) I'm inclined to go with the same conversion as Backbone, as that's the one that is most likely to be supported.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm. I'm not clear what exactly we're doing in this pull request then. Are we targeting HTTP/1.0 ? There are only 3 defined verbs (previously overlooked HEAD):

https://www.w3.org/Protocols/HTTP/1.0/spec.html#Methods

It makes me a bit nervous that we target Backbone, as it's not obvious why we target Backbone (aside from anecdotal evidence that it appears to be well-supported).

if ( ! options.headers ) {
options.headers = {};
}
options.headers['X-HTTP-Method-Override'] = options.method;
options.method = 'POST';
}
}

return oldApiRequest( options );
}
} )( window.wp );
JS;

$scripts->add_inline_script( 'wp-api-request', $api_request_fix, 'after' );
}
add_action( 'wp_default_scripts', 'gutenberg_shim_api_request_emulate_http' );
Copy link
Member

Choose a reason for hiding this comment

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

The previous shim was called inside gutenberg_editor_scripts_and_styles. This code uses wp_default_scripts action. Not sure what is the difference as I can't find what this action does.

Copy link
Member Author

Choose a reason for hiding this comment

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

No practical difference, I just copy/pasta-ed from the gutenberg_shim_fix_api_request_plain_permalinks shim above it.


/**
* Disables wpautop behavior in classic editor when post contains blocks, to
* prevent removep from invalidating paragraph blocks.
Expand Down