From a94f468051a40cb30fccce76f6f340b0563a6a37 Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Fri, 14 Oct 2016 19:29:08 +0000 Subject: [PATCH] REST API: Support sites with index-style permalinks in `get_rest_url()`. Support the index-style permalinks (http://example.com/index.php/postName) when registering the REST API rewrite rules and within the `get_rest_url()` function. This allows sites that do not have mod_rewrite support to have almost pretty urls and have access to their REST API endpoints. Props kraftbj. Fixes #38182. git-svn-id: https://develop.svn.wordpress.org/trunk@38790 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api.php | 15 ++++++++++++++- tests/phpunit/tests/rest-api.php | 8 ++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 0eee75c772..620ca925b9 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -91,10 +91,15 @@ function rest_api_init() { * @since 4.4.0 * * @see add_rewrite_rule() + * @global WP_Rewrite $wp_rewrite */ function rest_api_register_rewrites() { + global $wp_rewrite; + add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$','index.php?rest_route=/','top' ); add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?','index.php?rest_route=/$matches[1]','top' ); + add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/?$','index.php?rest_route=/','top' ); + add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/(.*)?','index.php?rest_route=/$matches[1]','top' ); } /** @@ -176,6 +181,7 @@ function rest_get_url_prefix() { * @since 4.4.0 * * @todo Check if this is even necessary + * @global WP_Rewrite $wp_rewrite * * @param int $blog_id Optional. Blog ID. Default of null returns URL for current blog. * @param string $path Optional. REST route. Default '/'. @@ -188,7 +194,14 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { } if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) { - $url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme ); + global $wp_rewrite; + + if ( $wp_rewrite->using_index_permalinks() ) { + $url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme ); + } else { + $url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme ); + } + $url .= '/' . ltrim( $path, '/' ); } else { $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) ); diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index 1fe1b49540..b364a8af72 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -275,11 +275,15 @@ class Tests_REST_API extends WP_UnitTestCase { */ public function test_rest_url_generation() { // In pretty permalinks case, we expect a path of wp-json/ with no query. - update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() ); - update_option( 'permalink_structure', '' ); + // In index permalinks case, we expect a path of index.php/wp-json/ with no query. + $this->set_permalink_structure( '/index.php/%year%/%monthnum%/%day%/%postname%/' ); + $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() ); + // In non-pretty case, we get a query string to invoke the rest router. + $this->set_permalink_structure( '' ); $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/?rest_route=/', get_rest_url() ); }