[ 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      $input_id      = 'wp-block-search__input-' . ++$instance_id;
  19      $label_markup  = '';
  20      $button_markup = '';
  21  
  22      if ( ! empty( $attributes['label'] ) ) {
  23          $label_markup = sprintf(
  24              '<label for="%s" class="wp-block-search__label">%s</label>',
  25              $input_id,
  26              $attributes['label']
  27          );
  28      }
  29  
  30      $input_markup = sprintf(
  31          '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" />',
  32          $input_id,
  33          esc_attr( get_search_query() ),
  34          esc_attr( $attributes['placeholder'] )
  35      );
  36  
  37      if ( ! empty( $attributes['buttonText'] ) ) {
  38          $button_markup = sprintf(
  39              '<button type="submit" class="wp-block-search__button">%s</button>',
  40              $attributes['buttonText']
  41          );
  42      }
  43  
  44      $class = 'wp-block-search';
  45      if ( isset( $attributes['className'] ) ) {
  46          $class .= ' ' . $attributes['className'];
  47      }
  48  
  49      if ( isset( $attributes['align'] ) ) {
  50          $class .= ' align' . $attributes['align'];
  51      }
  52  
  53      return sprintf(
  54          '<form class="%s" role="search" method="get" action="%s">%s</form>',
  55          $class,
  56          esc_url( home_url( '/' ) ),
  57          $label_markup . $input_markup . $button_markup
  58      );
  59  }
  60  
  61  /**
  62   * Registers the `core/search` block on the server.
  63   */
  64  function register_block_core_search() {
  65      register_block_type(
  66          'core/search',
  67          array(
  68              'attributes'      => array(
  69                  'align'       => array(
  70                      'type' => 'string',
  71                      'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  72                  ),
  73                  'className'   => array(
  74                      'type' => 'string',
  75                  ),
  76                  'label'       => array(
  77                      'type'    => 'string',
  78                      'default' => __( 'Search' ),
  79                  ),
  80                  'placeholder' => array(
  81                      'type'    => 'string',
  82                      'default' => '',
  83                  ),
  84                  'buttonText'  => array(
  85                      'type'    => 'string',
  86                      'default' => __( 'Search' ),
  87                  ),
  88              ),
  89              'render_callback' => 'render_block_core_search',
  90          )
  91      );
  92  }
  93  add_action( 'init', 'register_block_core_search' );


Generated: Mon Oct 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1