Make WordPress Core

Changeset 36673


Ignore:
Timestamp:
02/24/2016 04:00:12 AM (8 years ago)
Author:
rmccue
Message:

REST API: Add WP_REST_Request::from_url()

Allows converting a REST URL into a Request object.

Props danielbachhuber.
Fixes #35803.

Location:
trunk
Files:
2 edited

Legend:

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

    r36636 r36673  
    935935        }
    936936    }
     937
     938    /**
     939     * Gets a WP_REST_Request object from a full URL.
     940     *
     941     * @since 4.5.0
     942     *
     943     * @param string $url URL with protocol, domain, path and query args.
     944     * @return WP_REST_Request|false WP_REST_Request object on success, false on failure.
     945     */
     946    public static function from_url( $url ) {
     947        $bits = parse_url( $url );
     948        $query_params = array();
     949
     950        if ( ! empty( $bits['query'] ) ) {
     951            wp_parse_str( $bits['query'], $query_params );
     952        }
     953
     954        $api_root = rest_url();
     955        if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) {
     956            // Pretty permalinks on, and URL is under the API root
     957            $api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) );
     958            $route = parse_url( $api_url_part, PHP_URL_PATH );
     959        } elseif ( ! empty( $query_params['rest_route'] ) ) {
     960            // ?rest_route=... set directly
     961            $route = $query_params['rest_route'];
     962            unset( $query_params['rest_route'] );
     963        }
     964
     965        $request = false;
     966        if ( ! empty( $route ) ) {
     967            $request = new WP_REST_Request( 'GET', $route );
     968            $request->set_query_params( $query_params );
     969        }
     970
     971        /**
     972         * Filter the request generated from a URL.
     973         *
     974         * @since 4.5.0
     975         *
     976         * @param WP_REST_Request|false $request Generated request object, or false if URL could not be parsed.
     977         * @param string $url URL the request was generated from.
     978         */
     979        return apply_filters( 'rest_request_from_url', $request, $url );
     980    }
    937981}
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r35890 r36673  
    421421        return new WP_Error( 'some-error', 'This is not valid!' );
    422422    }
     423
     424    public function data_from_url() {
     425        return array(
     426            array(
     427                'permalink_structure' => '/%post_name%/',
     428                'original_url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-json/wp/v2/posts/1?foo=bar',
     429            ),
     430            array(
     431                'permalink_structure' => '',
     432                'original_url'        => 'http://' . WP_TESTS_DOMAIN . '/?rest_route=%2Fwp%2Fv2%2Fposts%2F1&foo=bar',
     433            ),
     434        );
     435    }
     436
     437    /**
     438     * @dataProvider data_from_url
     439     */
     440    public function test_from_url( $permalink_structure, $original_url ) {
     441        update_option( 'permalink_structure', $permalink_structure );
     442        $url = add_query_arg( 'foo', 'bar', rest_url( '/wp/v2/posts/1' ) );
     443        $this->assertEquals( $original_url, $url );
     444        $request = WP_REST_Request::from_url( $url );
     445        $this->assertInstanceOf( 'WP_REST_Request', $request );
     446        $this->assertEquals( '/wp/v2/posts/1', $request->get_route() );
     447        $this->assertEqualSets( array(
     448            'foo' => 'bar',
     449        ), $request->get_query_params() );
     450    }
     451
     452    /**
     453     * @dataProvider data_from_url
     454     */
     455    public function test_from_url_invalid( $permalink_structure ) {
     456        update_option( 'permalink_structure', $permalink_structure );
     457        $using_site = site_url( '/wp/v2/posts/1' );
     458        $request = WP_REST_Request::from_url( $using_site );
     459        $this->assertFalse( $request );
     460
     461        $using_home = home_url( '/wp/v2/posts/1' ) ;
     462        $request = WP_REST_Request::from_url( $using_home );
     463        $this->assertFalse( $request );
     464    }
    423465}
Note: See TracChangeset for help on using the changeset viewer.