Skip to content

Commit

Permalink
fix: serversiderender in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dkjensen committed Apr 4, 2022
1 parent 6f89fdb commit 2de1375
Show file tree
Hide file tree
Showing 10 changed files with 1,288 additions and 26,320 deletions.
5 changes: 2 additions & 3 deletions lib/config/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"apiVersion": 2,
"name": "swc/template-part",
"name": "wpbtp/template-part",
"title": "Template Part",
"category": "design",
"parent": [ "core/post-template" ],
Expand All @@ -20,6 +20,5 @@
"html": false,
"align": true
},
"editorScript": "wp-block-template-part",
"editorStyle": "file:../../assets/css/block.css"
"editorScript": "wp-block-template-part"
}
12 changes: 10 additions & 2 deletions lib/functions/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ function get_template_parts() {
* @return string Returns the template part for the given post.
*/
function render_block_template_part( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
$is_block_editor = defined( 'REST_REQUEST' ) && true === REST_REQUEST && isset( $_GET['context'] ) && 'edit' === sanitize_key( $_GET['context'] );

if ( $is_block_editor ) {
$post_id = absint( $_GET['postId'] ?? null );
} else {
$post_id = absint( $block->context['postId'] ?? null );
}

if ( ! $post_id ) {
return '';
}

Expand All @@ -97,7 +105,7 @@ function render_block_template_part( $attributes, $content, $block ) {
$template_part = apply_filters( 'wp_block_template_part_default', '' );
}

$content = get_template_part( $block->context['postId'], $template_part );
$content = get_template_part( $post_id, $template_part );

\wp_reset_postdata();

Expand Down
4 changes: 2 additions & 2 deletions lib/functions/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function rest_route_template_parts_part( $request ) {
$template_part = $request->get_param( 'template_part' );
$object_id = $request->get_param( 'id' );

$content = '';
$post = \get_post( $object_id );
$content = '';
$GLOBALS['post'] = \get_post( $object_id ); // phpcs:ignore

ob_start();

Expand Down
6 changes: 4 additions & 2 deletions lib/functions/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* @return string
*/
function get_template_part( $post, $template_part = '' ) {
$content = '';
$post = \get_post( $post );
$content = '';
$GLOBALS['post'] = \get_post( $post ); // phpcs:ignore

$template_part = $template_part ?: 'template-parts/content-' . $post->post_type;

Expand All @@ -30,6 +30,8 @@ function get_template_part( $post, $template_part = '' ) {

$has_template_part = ob_get_clean();

\wp_reset_postdata();

if ( $has_template_part ) {
$content = $has_template_part;
}
Expand Down
Loading

0 comments on commit 2de1375

Please sign in to comment.