WordPress.org

Make WordPress Core

Changeset 38790


Ignore:
Timestamp:
10/14/2016 07:29:08 PM (20 months ago)
Author:
rachelbaker
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r38310 r38790  
    9292 *
    9393 * @see add_rewrite_rule()
     94 * @global WP_Rewrite $wp_rewrite
    9495 */
    9596function rest_api_register_rewrites() {
     97    global $wp_rewrite;
     98
    9699    add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$','index.php?rest_route=/','top' );
    97100    add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?','index.php?rest_route=/$matches[1]','top' );
     101    add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/?$','index.php?rest_route=/','top' );
     102    add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/(.*)?','index.php?rest_route=/$matches[1]','top' );
    98103}
    99104
     
    177182 *
    178183 * @todo Check if this is even necessary
     184 * @global WP_Rewrite $wp_rewrite
    179185 *
    180186 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
     
    189195
    190196    if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
    191         $url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
     197        global $wp_rewrite;
     198
     199        if ( $wp_rewrite->using_index_permalinks() ) {
     200            $url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme );
     201        } else {
     202            $url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
     203        }
     204
    192205        $url .= '/' . ltrim( $path, '/' );
    193206    } else {
  • trunk/tests/phpunit/tests/rest-api.php

    r38771 r38790  
    276276    public function test_rest_url_generation() {
    277277        // In pretty permalinks case, we expect a path of wp-json/ with no query.
    278         update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
     278        $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    279279        $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
    280280
    281         update_option( 'permalink_structure', '' );
     281        // In index permalinks case, we expect a path of index.php/wp-json/ with no query.
     282        $this->set_permalink_structure( '/index.php/%year%/%monthnum%/%day%/%postname%/' );
     283        $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() );
     284
    282285        // In non-pretty case, we get a query string to invoke the rest router.
     286        $this->set_permalink_structure( '' );
    283287        $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/?rest_route=/', get_rest_url() );
    284288
Note: See TracChangeset for help on using the changeset viewer.