Merge pull request #20686 from woocommerce/fix/20673

Replaced query attribute placeholder with actual value for header link.
This commit is contained in:
Claudio Sanches 2018-07-10 15:22:30 -03:00 committed by GitHub
commit 9b7fbff544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -370,7 +370,21 @@ abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller {
$response->header( 'X-WP-Total', $query_results['total'] ); $response->header( 'X-WP-Total', $query_results['total'] );
$response->header( 'X-WP-TotalPages', (int) $max_pages ); $response->header( 'X-WP-TotalPages', (int) $max_pages );
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) ); $base = $this->rest_base;
$attrib_prefix = '(?P<';
if ( strpos( $base, $attrib_prefix ) !== false ) {
$attrib_names = array();
preg_match( '/\(\?P<[^>]+>.*\)/', $base, $attrib_names, PREG_OFFSET_CAPTURE );
foreach ( $attrib_names as $attrib_name_match ) {
$beginning_offset = strlen( $attrib_prefix );
$attrib_name_end = strpos( $attrib_name_match[0], '>', $attrib_name_match[1] );
$attrib_name = substr( $attrib_name_match[0], $beginning_offset, $attrib_name_end - $beginning_offset );
if ( isset( $request[ $attrib_name ] ) ) {
$base = str_replace( "(?P<$attrib_name>[\d]+)", $request[ $attrib_name ], $base );
}
}
}
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ) );
if ( $page > 1 ) { if ( $page > 1 ) {
$prev_page = $page - 1; $prev_page = $page - 1;

View File

@ -344,7 +344,8 @@ abstract class WC_REST_Terms_Controller extends WC_REST_Controller {
$max_pages = ceil( $total_terms / $per_page ); $max_pages = ceil( $total_terms / $per_page );
$response->header( 'X-WP-TotalPages', (int) $max_pages ); $response->header( 'X-WP-TotalPages', (int) $max_pages );
$base = add_query_arg( $request->get_query_params(), rest_url( '/' . $this->namespace . '/' . $this->rest_base ) ); $base = str_replace( '(?P<attribute_id>[\d]+)', $request['attribute_id'], $this->rest_base );
$base = add_query_arg( $request->get_query_params(), rest_url( '/' . $this->namespace . '/' . $base ) );
if ( $page > 1 ) { if ( $page > 1 ) {
$prev_page = $page - 1; $prev_page = $page - 1;
if ( $prev_page > $max_pages ) { if ( $prev_page > $max_pages ) {