Skip to content

Wordpress 5.9 templates endpoint regex problem #503

Open
@paussus

Description

@paussus

Hi,

since updating to wordpress 5.9 (or 5.9.1, can't be sure) the api is not able to bootstrap with the endpoints from the json due to a regex problem. The stack trace is

SyntaxError: Invalid regular expression: /^([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?$/: Unterminated group at new RegExp (<anonymous>) 
at reduceRouteComponents (...\node_modules\wpapi\lib\route-tree.js:84:3) 
at Array.reduce (<anonymous>) 
at reduceRouteTree (...\node_modules\wpapi\lib\route-tree.js:185:18) 
at ...\node_modules\wpapi\lib\util\object-reduce.js:25:20 at Array.reduce (<anonymous>) 
at module.exports (...\node_modules\wpapi\lib\util\object-reduce.js:24:3) 
at buildRouteTree (...\node_modules\wpapi\lib\route-tree.js:203:9) at WPAPI.bootstrap (...\node_modules\wpapi\wpapi.js:349:23) 
at new WPAPI (...\node_modules\wpapi\wpapi.js:88:4) 
at ...\node_modules\wpapi\wpapi.js:452:11 
...

The result is that the wpapi only contains the default endpoints.

The problem seems to reside in the new templates endpoint regex /wp/v2/templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w-]+) that it is not correctly captured by the named group regex at reduceRouteComponents in route-tree.js . It does not capture one closing ) in namedGroup[2] and so the groupPattern is invalid and fails when building the regular expression. It can be seen in this demo https://regex101.com/r/zGr26D/1. From my understanding the problem seems to arise from the templates regular expression having 3 levels of named patterns.

I guess it happens too with the template-patterns endpoint too.

In case anyone is interested, to solve it temporarely, since I don't need these endpoints, I remove them in wordpress:

function remove_template_endpoints( $endpoints ) {
    foreach ( $endpoints as $endpoint => $details ) {
        if ( fnmatch( '/wp/v2/template*/*', $endpoint ) ) {
            unset( $endpoints[$endpoint] );
        }
    }

    return $endpoints;
}
add_filter( 'rest_endpoints', 'remove_template_endpoints' );

Thanks for the work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions