home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / widgets / class-wp-widget-search.php < prev    next >
Encoding:
PHP Script  |  2017-10-15  |  2.5 KB  |  92 lines

  1. <?php
  2. /**
  3.  * Widget API: WP_Widget_Search class
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Widgets
  7.  * @since 4.4.0
  8.  */
  9.  
  10. /**
  11.  * Core class used to implement a Search widget.
  12.  *
  13.  * @since 2.8.0
  14.  *
  15.  * @see WP_Widget
  16.  */
  17. class WP_Widget_Search extends WP_Widget {
  18.  
  19.     /**
  20.      * Sets up a new Search widget instance.
  21.      *
  22.      * @since 2.8.0
  23.      */
  24.     public function __construct() {
  25.         $widget_ops = array(
  26.             'classname' => 'widget_search',
  27.             'description' => __( 'A search form for your site.' ),
  28.             'customize_selective_refresh' => true,
  29.         );
  30.         parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
  31.     }
  32.  
  33.     /**
  34.      * Outputs the content for the current Search widget instance.
  35.      *
  36.      * @since 2.8.0
  37.      *
  38.      * @param array $args     Display arguments including 'before_title', 'after_title',
  39.      *                        'before_widget', and 'after_widget'.
  40.      * @param array $instance Settings for the current Search widget instance.
  41.      */
  42.     public function widget( $args, $instance ) {
  43.         $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
  44.  
  45.         /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  46.         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  47.  
  48.         echo $args['before_widget'];
  49.         if ( $title ) {
  50.             echo $args['before_title'] . $title . $args['after_title'];
  51.         }
  52.  
  53.         // Use current theme search form if it exists
  54.         get_search_form();
  55.  
  56.         echo $args['after_widget'];
  57.     }
  58.  
  59.     /**
  60.      * Outputs the settings form for the Search widget.
  61.      *
  62.      * @since 2.8.0
  63.      *
  64.      * @param array $instance Current settings.
  65.      */
  66.     public function form( $instance ) {
  67.         $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
  68.         $title = $instance['title'];
  69.         ?>
  70.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
  71.         <?php
  72.     }
  73.  
  74.     /**
  75.      * Handles updating settings for the current Search widget instance.
  76.      *
  77.      * @since 2.8.0
  78.      *
  79.      * @param array $new_instance New settings for this instance as input by the user via
  80.      *                            WP_Widget::form().
  81.      * @param array $old_instance Old settings for this instance.
  82.      * @return array Updated settings.
  83.      */
  84.     public function update( $new_instance, $old_instance ) {
  85.         $instance = $old_instance;
  86.         $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
  87.         $instance['title'] = sanitize_text_field( $new_instance['title'] );
  88.         return $instance;
  89.     }
  90.  
  91. }
  92.