[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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  
  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          $query = $wp_query;
  30      } else {
  31          $query_args = build_query_vars_from_query_block( $block, $page );
  32          $query      = new WP_Query( $query_args );
  33      }
  34  
  35      if ( $query->post_count > 0 ) {
  36          return '';
  37      }
  38  
  39      $classes            = ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) ? 'has-link-color' : '';
  40      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  41      return sprintf(
  42          '<div %1$s>%2$s</div>',
  43          $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 : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref