Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wordpress 5.9 templates endpoint regex problem #503

Open
paussus opened this issue Mar 1, 2022 · 2 comments
Open

Wordpress 5.9 templates endpoint regex problem #503

paussus opened this issue Mar 1, 2022 · 2 comments

Comments

@paussus
Copy link

paussus commented Mar 1, 2022

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!

@samfreedman
Copy link

I'm experiencing the same issue, but I do need these endpoints for a custom integration. If I can find time, I'll see if I can update the regex pattern

@lucrus73
Copy link

Is it possible to work around this by providing the routes to wpapi instead of using autodiscovery? I've never tried that and, even assuming it could work, I don't know how to do it, so I'm just asking to know if it would make sense to spend time on this kind of workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants