From 5b9221d789dfcd452b4e557ba1ee471962a17fcc Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Sun, 8 Nov 2020 20:31:30 +0000 Subject: [PATCH] Check postId returns a published wp_template_part (#26734) --- .../block-library/src/template-part/index.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 79acd655bb659..6b957d9e4476a 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -15,10 +15,17 @@ function render_block_core_template_part( $attributes ) { $content = null; - if ( ! empty( $attributes['postId'] ) && get_post_status( $attributes['postId'] ) ) { - // If we have a post ID and the post exists, which means this template part - // is user-customized, render the corresponding post content. - $content = get_post( $attributes['postId'] )->post_content; + if ( ! empty( $attributes['postId'] ) ) { + // If we have a post ID, and it's a published template part, + // then render the corresponding post content. + // Else treat this as an error. + $post = get_post( $attributes['postId'] ); + if ( $post && ( 'wp_template_part' === $post->post_type ) ) { + $post_status = get_post_status( $attributes['postId'] ); + if ( 'publish' === $post_status ) { + $content = $post->post_content; + } + } } elseif ( isset( $attributes['theme'] ) && basename( wp_get_theme()->get_stylesheet() ) === $attributes['theme'] ) { $template_part_query = new WP_Query( array( @@ -47,7 +54,7 @@ function render_block_core_template_part( $attributes ) { } if ( is_null( $content ) ) { - return 'Template Part Not Found'; + return 'Template Part Not Found: ' . $attributes['slug']; } // Run through the actions that are typically taken on the_content.