From ef189f6eb1fdec30f2c17c6987be3062a9319995 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 31 Mar 2021 12:01:41 +0300 Subject: [PATCH 1/6] Use a normal WordPress loop similar to themes --- .../block-library/src/query-loop/index.php | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 293ad681ff0f06..54836909d6c12b 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -18,26 +18,23 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); - $query = construct_wp_query_args( $block, $page ); + $query_args = construct_wp_query_args( $block, $page ); // Override the custom query with the global query if needed. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); if ( $use_global_query ) { global $wp_query; if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination. - unset( $query['offset'] ); - $query = wp_parse_args( $wp_query->query_vars, $query ); + unset( $query_args['offset'] ); + $query_args = wp_parse_args( $wp_query->query_vars, $query_args ); - if ( empty( $query['post_type'] ) && is_singular() ) { - $query['post_type'] = get_post_type( get_the_ID() ); + if ( empty( $query_args['post_type'] ) && is_singular() ) { + $query_args['post_type'] = get_post_type( get_the_ID() ); } } } - $posts = get_posts( $query ); - if ( empty( $posts ) ) { - return ''; - } + $query = new WP_Query( $query_args ); $classnames = ''; if ( isset( $block->context['layout'] ) && isset( $block->context['query'] ) ) { @@ -46,27 +43,33 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } } - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); + if ( $query->have_posts() ) { + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); - $content = ''; - foreach ( $posts as $post ) { - $block_content = ( - new WP_Block( - $block->parsed_block, - array( - 'postType' => $post->post_type, - 'postId' => $post->ID, + $content = ''; + while ( $query->have_posts() ) : $query->the_post(); + $block_content = ( + new WP_Block( + $block->parsed_block, + array( + 'postType' => get_post_type(), + 'postId' => get_the_ID(), + ) ) - ) - )->render( array( 'dynamic' => false ) ); - $content .= "
  • {$block_content}
  • "; + )->render( array( 'dynamic' => false ) ); + $content .= "
  • {$block_content}
  • "; + endwhile; + + return sprintf( + '', + $wrapper_attributes, + $content + ); } - return sprintf( - '', - $wrapper_attributes, - $content - ); + wp_reset_postdata(); + + return ''; } /** From 6384f9dccc253ee9f1f0ea00fb0e0c26799399a9 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 31 Mar 2021 12:06:37 +0300 Subject: [PATCH 2/6] post-content: Run the_post if not in a loop --- packages/block-library/src/post-content/index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 7ee8b4dd1b7055..4690e285b5ac06 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -14,6 +14,10 @@ * @return string Returns the filtered post content of the current post. */ function render_block_core_post_content( $attributes, $content, $block ) { + if ( ! in_the_loop() ) { + the_post(); + } + if ( ! isset( $block->context['postId'] ) ) { return ''; } From f75dc63d7cf091ed0f117eadfe4377092fd068eb Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 31 Mar 2021 13:16:19 +0300 Subject: [PATCH 3/6] phpcs fix --- packages/block-library/src/query-loop/index.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 54836909d6c12b..4c5c4b06eae9fd 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -47,7 +47,8 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); $content = ''; - while ( $query->have_posts() ) : $query->the_post(); + while ( $query->have_posts() ) : + $query->the_post(); $block_content = ( new WP_Block( $block->parsed_block, From c78b81c4534e35c63cbdea347361e6dda27410d3 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 31 Mar 2021 15:19:58 +0300 Subject: [PATCH 4/6] Address feedback --- .../block-library/src/post-content/index.php | 7 +-- .../block-library/src/query-loop/index.php | 44 +++++++++---------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 4690e285b5ac06..6c61a866331cac 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -14,14 +14,15 @@ * @return string Returns the filtered post content of the current post. */ function render_block_core_post_content( $attributes, $content, $block ) { - if ( ! in_the_loop() ) { - the_post(); - } if ( ! isset( $block->context['postId'] ) ) { return ''; } + if ( ! in_the_loop() ) { + the_post(); + } + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); return ( diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 4c5c4b06eae9fd..3fac9f2feb9a41 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -43,34 +43,34 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } } - if ( $query->have_posts() ) { - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); + if ( ! $query->have_posts() ) { + return ''; + } - $content = ''; - while ( $query->have_posts() ) : - $query->the_post(); - $block_content = ( - new WP_Block( - $block->parsed_block, - array( - 'postType' => get_post_type(), - 'postId' => get_the_ID(), - ) - ) - )->render( array( 'dynamic' => false ) ); - $content .= "
  • {$block_content}
  • "; - endwhile; + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); - return sprintf( - '
      %2$s
    ', - $wrapper_attributes, - $content - ); + $content = ''; + while ( $query->have_posts() ) { + $query->the_post(); + $block_content = ( + new WP_Block( + $block->parsed_block, + array( + 'postType' => get_post_type(), + 'postId' => get_the_ID(), + ) + ) + )->render( array( 'dynamic' => false ) ); + $content .= "
  • {$block_content}
  • "; } wp_reset_postdata(); - return ''; + return sprintf( + '
      %2$s
    ', + $wrapper_attributes, + $content + ); } /** From ff157f03931fcfcbd50e7771f943c76ad4cf8ece Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 31 Mar 2021 15:23:57 +0300 Subject: [PATCH 5/6] early exit earlier --- packages/block-library/src/query-loop/index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 3fac9f2feb9a41..30bc6ca75de08d 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -36,6 +36,10 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $query = new WP_Query( $query_args ); + if ( ! $query->have_posts() ) { + return ''; + } + $classnames = ''; if ( isset( $block->context['layout'] ) && isset( $block->context['query'] ) ) { if ( isset( $block->context['layout']['type'] ) && 'flex' === $block->context['layout']['type'] ) { @@ -43,10 +47,6 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } } - if ( ! $query->have_posts() ) { - return ''; - } - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); $content = ''; From b6bd2b297d877c7a475ca0bdd3e8e6f33f469ca9 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 31 Mar 2021 16:20:58 +0300 Subject: [PATCH 6/6] Update packages/block-library/src/post-content/index.php Co-authored-by: Nik Tsekouras --- packages/block-library/src/post-content/index.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 6c61a866331cac..251947582c920a 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -14,7 +14,6 @@ * @return string Returns the filtered post content of the current post. */ function render_block_core_post_content( $attributes, $content, $block ) { - if ( ! isset( $block->context['postId'] ) ) { return ''; }