Make WordPress Core

Changeset 60073


Ignore:
Timestamp:
03/24/2025 11:57:08 PM (2 months ago)
Author:
audrasjb
Message:

REST API: exclude rest_route from get_params() if pretty permalinks are disabled.

This changeset introduces a modification to the get_params() method within the WordPress REST API. The change ensures that the rest_route parameter is excluded from the parameters returned when pretty permalinks are not enabled. This update enhances the developer experience by ensuring that the parameters returned by get_params() are relevant and do not include unnecessary values, thereby reducing potential confusion and errors.

Props westonruter, TimothyBlynJacobs, audrasjb, debarghyabanerjee, dilip2615, shanemuir, peterwilsoncc.
Fixes #62163.

Location:
trunk
Files:
2 edited

Legend:

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

    r59899 r60073  
    495495        }
    496496
     497        // Exclude rest_route if pretty permalinks are not enabled.
     498        if ( ! get_option( 'permalink_structure' ) ) {
     499            unset( $params['rest_route'] );
     500        }   
     501
    497502        return $params;
    498503    }
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r59899 r60073  
    11271127        );
    11281128    }
     1129
     1130    /**
     1131     * @ticket 62163
     1132     */
     1133    public function test_get_params_without_pretty_permalink() {
     1134        update_option( 'permalink_structure', '' );
     1135
     1136        $request = new WP_REST_Request();
     1137        $request->set_param( 'rest_route', '/wp/v2/posts' );
     1138        $request->set_param( 'some_param', 'foobar' );
     1139
     1140        $params = $request->get_params();
     1141
     1142        $this->assertArrayNotHasKey( 'rest_route', $params );
     1143        $this->assertArrayHasKey( 'some_param', $params );
     1144    }
     1145
     1146    /**
     1147     * @ticket 62163
     1148     */
     1149    public function test_get_params_with_pretty_permalinks() {
     1150        update_option( 'permalink_structure', '/%postname%/' );
     1151
     1152        $request = new WP_REST_Request();
     1153
     1154        $request->set_param( 'rest_route', '/wp/v2/posts' );
     1155        $request->set_param( 'some_param', 'foobar' );
     1156
     1157        $params = $request->get_params();
     1158
     1159        $this->assertArrayHasKey( 'rest_route', $params );
     1160        $this->assertArrayHasKey( 'some_param', $params );
     1161    }
    11291162}
Note: See TracChangeset for help on using the changeset viewer.