Нет описания

query-no-results.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/query-no-results` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/query-no-results` 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 wrapper for the no results block.
  15. */
  16. function render_block_core_query_no_results( $attributes, $content, $block ) {
  17. if ( empty( trim( $content ) ) ) {
  18. return '';
  19. }
  20. $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  21. $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  22. $query_args = build_query_vars_from_query_block( $block, $page );
  23. // Override the custom query with the global query if needed.
  24. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
  25. if ( $use_global_query ) {
  26. global $wp_query;
  27. if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
  28. $query_args = wp_parse_args( $wp_query->query_vars, $query_args );
  29. }
  30. }
  31. $query = new WP_Query( $query_args );
  32. if ( $query->have_posts() ) {
  33. return '';
  34. }
  35. wp_reset_postdata();
  36. return sprintf(
  37. '<div %1$s>%2$s</div>',
  38. get_block_wrapper_attributes(),
  39. $content
  40. );
  41. }
  42. /**
  43. * Registers the `core/query-no-results` block on the server.
  44. */
  45. function register_block_core_query_no_results() {
  46. register_block_type_from_metadata(
  47. __DIR__ . '/query-no-results',
  48. array(
  49. 'render_callback' => 'render_block_core_query_no_results',
  50. )
  51. );
  52. }
  53. add_action( 'init', 'register_block_core_query_no_results' );