post-template.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-template` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Determines whether a block list contains a block that uses the featured image.
  9. *
  10. * @param WP_Block_List $inner_blocks Inner block instance.
  11. *
  12. * @return bool Whether the block list contains a block that uses the featured image.
  13. */
  14. function block_core_post_template_uses_featured_image( $inner_blocks ) {
  15. foreach ( $inner_blocks as $block ) {
  16. if ( 'core/post-featured-image' === $block->name ) {
  17. return true;
  18. }
  19. if (
  20. 'core/cover' === $block->name &&
  21. ! empty( $block->attributes['useFeaturedImage'] )
  22. ) {
  23. return true;
  24. }
  25. if ( $block->inner_blocks && block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. /**
  32. * Renders the `core/post-template` block on the server.
  33. *
  34. * @param array $attributes Block attributes.
  35. * @param string $content Block default content.
  36. * @param WP_Block $block Block instance.
  37. *
  38. * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
  39. */
  40. function render_block_core_post_template( $attributes, $content, $block ) {
  41. $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  42. $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  43. // Use global query if needed.
  44. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
  45. if ( $use_global_query ) {
  46. global $wp_query;
  47. $query = clone $wp_query;
  48. } else {
  49. $query_args = build_query_vars_from_query_block( $block, $page );
  50. $query = new WP_Query( $query_args );
  51. }
  52. if ( ! $query->have_posts() ) {
  53. return '';
  54. }
  55. if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
  56. update_post_thumbnail_cache( $query );
  57. }
  58. $classnames = '';
  59. if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
  60. if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
  61. $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
  62. }
  63. }
  64. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  65. $content = '';
  66. while ( $query->have_posts() ) {
  67. $query->the_post();
  68. // Get an instance of the current Post Template block.
  69. $block_instance = $block->parsed_block;
  70. // Set the block name to one that does not correspond to an existing registered block.
  71. // This ensures that for the inner instances of the Post Template block, we do not render any block supports.
  72. $block_instance['blockName'] = 'core/null';
  73. // Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
  74. // `render_callback` and ensure that no wrapper markup is included.
  75. $block_content = (
  76. new WP_Block(
  77. $block_instance,
  78. array(
  79. 'postType' => get_post_type(),
  80. 'postId' => get_the_ID(),
  81. )
  82. )
  83. )->render( array( 'dynamic' => false ) );
  84. // Wrap the render inner blocks in a `li` element with the appropriate post classes.
  85. $post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
  86. $content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
  87. }
  88. /*
  89. * Use this function to restore the context of the template tags
  90. * from a secondary query loop back to the main query loop.
  91. * Since we use two custom loops, it's safest to always restore.
  92. */
  93. wp_reset_postdata();
  94. return sprintf(
  95. '<ul %1$s>%2$s</ul>',
  96. $wrapper_attributes,
  97. $content
  98. );
  99. }
  100. /**
  101. * Registers the `core/post-template` block on the server.
  102. */
  103. function register_block_core_post_template() {
  104. register_block_type_from_metadata(
  105. __DIR__ . '/post-template',
  106. array(
  107. 'render_callback' => 'render_block_core_post_template',
  108. 'skip_inner_blocks' => true,
  109. )
  110. );
  111. }
  112. add_action( 'init', 'register_block_core_post_template' );