Nenhuma Descrição

post-template.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $query_args = build_query_vars_from_query_block( $block, $page );
  44. // Override the custom query with the global query if needed.
  45. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
  46. if ( $use_global_query ) {
  47. global $wp_query;
  48. if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
  49. // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination.
  50. unset( $query_args['offset'] );
  51. $query_args = wp_parse_args( $wp_query->query_vars, $query_args );
  52. if ( empty( $query_args['post_type'] ) && is_singular() ) {
  53. $query_args['post_type'] = get_post_type( get_the_ID() );
  54. }
  55. }
  56. }
  57. $query = new WP_Query( $query_args );
  58. if ( ! $query->have_posts() ) {
  59. return '';
  60. }
  61. if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
  62. update_post_thumbnail_cache( $query );
  63. }
  64. $classnames = '';
  65. if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
  66. if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
  67. $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
  68. }
  69. }
  70. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  71. $content = '';
  72. while ( $query->have_posts() ) {
  73. $query->the_post();
  74. $block_content = (
  75. new WP_Block(
  76. $block->parsed_block,
  77. array(
  78. 'postType' => get_post_type(),
  79. 'postId' => get_the_ID(),
  80. )
  81. )
  82. )->render( array( 'dynamic' => false ) );
  83. $post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
  84. $content .= '<li class="' . esc_attr( $post_classes ) . '">' . $block_content . '</li>';
  85. }
  86. wp_reset_postdata();
  87. return sprintf(
  88. '<ul %1$s>%2$s</ul>',
  89. $wrapper_attributes,
  90. $content
  91. );
  92. }
  93. /**
  94. * Registers the `core/post-template` block on the server.
  95. */
  96. function register_block_core_post_template() {
  97. register_block_type_from_metadata(
  98. __DIR__ . '/post-template',
  99. array(
  100. 'render_callback' => 'render_block_core_post_template',
  101. 'skip_inner_blocks' => true,
  102. )
  103. );
  104. }
  105. add_action( 'init', 'register_block_core_post_template' );