[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> query-no-results.php (source)

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


Generated: Sat Jun 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1