From ff989eccce3b9a712b908c2166147aed99374ae9 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Sat, 24 Oct 2020 14:10:26 +0000 Subject: [PATCH] REST API: Make template handling resilient against plugins setting the global post. Plugins shouldn't be setting the global post object during a REST API request, but if they did this could cause unexpected errors when creating a post with a template. Props Kipperlenny, TimothyBlynJacobs. Fixes #49695. git-svn-id: https://develop.svn.wordpress.org/trunk@49301 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/endpoints/class-wp-rest-posts-controller.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 06db540af2..e4a8bcba86 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1344,8 +1344,10 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } if ( $request['id'] ) { + $post = get_post( $request['id'] ); $current_template = get_page_template_slug( $request['id'] ); } else { + $post = null; $current_template = ''; } @@ -1355,7 +1357,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } // If this is a create request, get_post() will return null and wp theme will fallback to the passed post type. - $allowed_templates = wp_get_theme()->get_page_templates( get_post( $request['id'] ), $this->post_type ); + $allowed_templates = wp_get_theme()->get_page_templates( $post, $this->post_type ); if ( isset( $allowed_templates[ $template ] ) ) { return true;