Нет описания

post-template.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-template` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-template` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. *
  14. * @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
  15. */
  16. function render_block_core_post_template( $attributes, $content, $block ) {
  17. $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  18. $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  19. $query_args = build_query_vars_from_query_block( $block, $page );
  20. // Override the custom query with the global query if needed.
  21. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
  22. if ( $use_global_query ) {
  23. global $wp_query;
  24. if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
  25. // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination.
  26. unset( $query_args['offset'] );
  27. $query_args = wp_parse_args( $wp_query->query_vars, $query_args );
  28. if ( empty( $query_args['post_type'] ) && is_singular() ) {
  29. $query_args['post_type'] = get_post_type( get_the_ID() );
  30. }
  31. }
  32. }
  33. $query = new WP_Query( $query_args );
  34. if ( ! $query->have_posts() ) {
  35. return '';
  36. }
  37. $classnames = '';
  38. if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
  39. if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
  40. $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
  41. }
  42. }
  43. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  44. $content = '';
  45. while ( $query->have_posts() ) {
  46. $query->the_post();
  47. $block_content = (
  48. new WP_Block(
  49. $block->parsed_block,
  50. array(
  51. 'postType' => get_post_type(),
  52. 'postId' => get_the_ID(),
  53. )
  54. )
  55. )->render( array( 'dynamic' => false ) );
  56. $content .= "<li>{$block_content}</li>";
  57. }
  58. wp_reset_postdata();
  59. return sprintf(
  60. '<ul %1$s>%2$s</ul>',
  61. $wrapper_attributes,
  62. $content
  63. );
  64. }
  65. /**
  66. * Registers the `core/post-template` block on the server.
  67. */
  68. function register_block_core_post_template() {
  69. register_block_type_from_metadata(
  70. __DIR__ . '/post-template',
  71. array(
  72. 'render_callback' => 'render_block_core_post_template',
  73. 'skip_inner_blocks' => true,
  74. )
  75. );
  76. }
  77. add_action( 'init', 'register_block_core_post_template' );