WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

#42382 closed defect (bug) (fixed)

REST API: Handle api-request query parameters with plain permalinks

Reported by: aduth Owned by: adamsilverstein
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.9
Component: REST API Keywords: commit has-unit-tests
Focuses: javascript Cc:

Description

Related: #40919
See: https://github.com/WordPress/gutenberg/issues/3215

When a site is configured to use plain permalinks, passing an endpoint or path argument containing query parameters prefixed by ? will fail:

Example:

wp.apiRequest( {
	namespace: '/wp/v2/',
	endpoint: '/posts?orderby=title'
} )

This is because we perform a simple concatenation on the API root which in the case of plain permalinks results in a URL like:

/index.php?rest_route=/wp/v2/posts?orderby=title

(Note the two ?)

Attached is a patch which resolves this issue by checking whether the API root already contains ? and, if so, converting the path of the endpoint request to replace ? with &.

QUnit tests have been updated accordingly.

Attachments (1)

42382.diff (1.6 KB) - added by aduth 3 years ago.

Download all attachments as: .zip

Change History (11)

@aduth
3 years ago

#1 @adamsilverstein
3 years ago

  • Owner set to adamsilverstein
  • Status changed from new to assigned

#2 @rachelbaker
3 years ago

@adamsilverstein still want to pick this up? Handling the edge cases of permalinks is a frustrating game of whack-a-mole.

#3 @adamsilverstein
3 years ago

sure, i've got it on my list :)

#4 @aduth
3 years ago

Sample workaround fix for plugins encountering this bug: https://github.com/WordPress/gutenberg/pull/4877

#5 @aduth
3 years ago

Alternatively, since it's a common use-case, maybe apiRequest could accept a query object, which is normalized internally. Doesn't help existing usage though.

#6 @adamsilverstein
3 years ago

  • Keywords needs-testing added
  • Milestone changed from Awaiting Review to 5.0

#7 @adamsilverstein
3 years ago

  • Keywords commit has-unit-tests added; needs-testing removed

Verified this works as expected, marking for commit.

#8 @adamsilverstein
3 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 42965:

REST API: Handle api-request query parameters with plain permalinks.

When constructing the request URL, ensure that ? is replaced with & when the API root already contains a ?. Fixes an issue where requests were broken when sites had permalinks set to plain.

Props aduth.
Fixes #42382.

#9 @SergeyBiryukov
2 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

[42965] should be backported to the 5.0 branch.

#10 @danielbachhuber
2 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 43771:

REST API: Handle api-request query parameters with plain permalinks.

When constructing the request URL, ensure that ? is replaced with & when the API root already contains a ?. Fixes an issue where requests were broken when sites had permalinks set to plain.

Props aduth.
Merges [42965] to the 5.0 branch.
Fixes #42382.

Note: See TracTickets for help on using tickets.