[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> search.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/search` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Dynamically renders the `core/search` block.
  10   *
  11   * @param array $attributes The block attributes.
  12   *
  13   * @return string The search block markup.
  14   */
  15  function render_block_core_search( $attributes ) {
  16      static $instance_id = 0;
  17  
  18      // Older versions of the Search block defaulted the label and buttonText
  19      // attributes to `__( 'Search' )` meaning that many posts contain `<!--
  20      // wp:search /-->`. Support these by defaulting an undefined label and
  21      // buttonText to `__( 'Search' )`.
  22      $attributes = wp_parse_args(
  23          $attributes,
  24          array(
  25              'label'      => __( 'Search' ),
  26              'buttonText' => __( 'Search' ),
  27          )
  28      );
  29  
  30      $input_id      = 'wp-block-search__input-' . ++$instance_id;
  31      $label_markup  = '';
  32      $button_markup = '';
  33  
  34      if ( ! empty( $attributes['label'] ) ) {
  35          $label_markup = sprintf(
  36              '<label for="%s" class="wp-block-search__label">%s</label>',
  37              $input_id,
  38              $attributes['label']
  39          );
  40      } else {
  41          $label_markup = sprintf(
  42              '<label for="%s" class="wp-block-search__label screen-reader-text">%s</label>',
  43              $input_id,
  44              __( 'Search' )
  45          );
  46      }
  47  
  48      $input_markup = sprintf(
  49          '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" required />',
  50          $input_id,
  51          esc_attr( get_search_query() ),
  52          esc_attr( $attributes['placeholder'] )
  53      );
  54  
  55      if ( ! empty( $attributes['buttonText'] ) ) {
  56          $button_markup = sprintf(
  57              '<button type="submit" class="wp-block-search__button">%s</button>',
  58              $attributes['buttonText']
  59          );
  60      }
  61  
  62      $class = 'wp-block-search';
  63      if ( isset( $attributes['className'] ) ) {
  64          $class .= ' ' . $attributes['className'];
  65      }
  66      if ( isset( $attributes['align'] ) ) {
  67          $class .= ' align' . $attributes['align'];
  68      }
  69  
  70      return sprintf(
  71          '<form class="%s" role="search" method="get" action="%s">%s</form>',
  72          esc_attr( $class ),
  73          esc_url( home_url( '/' ) ),
  74          $label_markup . $input_markup . $button_markup
  75      );
  76  }
  77  
  78  /**
  79   * Registers the `core/search` block on the server.
  80   */
  81  function register_block_core_search() {
  82      register_block_type_from_metadata(
  83          __DIR__ . '/search',
  84          array(
  85              'render_callback' => 'render_block_core_search',
  86          )
  87      );
  88  }
  89  add_action( 'init', 'register_block_core_search' );


Generated: Thu Sep 24 01:02:16 2020 Cross-referenced by PHPXref 0.7.1