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 | 'show_instance_in_rest' => true, |
---|
30 | ); |
---|
31 | parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops ); |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Outputs the content for the current Search widget instance. |
---|
36 | * |
---|
37 | * @since 2.8.0 |
---|
38 | * |
---|
39 | * @param array $args Display arguments including 'before_title', 'after_title', |
---|
40 | * 'before_widget', and 'after_widget'. |
---|
41 | * @param array $instance Settings for the current Search widget instance. |
---|
42 | */ |
---|
43 | public function widget( $args, $instance ) { |
---|
44 | $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; |
---|
45 | |
---|
46 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
---|
47 | $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
---|
48 | |
---|
49 | echo $args['before_widget']; |
---|
50 | if ( $title ) { |
---|
51 | echo $args['before_title'] . $title . $args['after_title']; |
---|
52 | } |
---|
53 | |
---|
54 | // Use current theme search form if it exists. |
---|
55 | get_search_form(); |
---|
56 | |
---|
57 | echo $args['after_widget']; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Outputs the settings form for the Search widget. |
---|
62 | * |
---|
63 | * @since 2.8.0 |
---|
64 | * |
---|
65 | * @param array $instance Current settings. |
---|
66 | */ |
---|
67 | public function form( $instance ) { |
---|
68 | $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); |
---|
69 | $title = $instance['title']; |
---|
70 | ?> |
---|
71 | <p> |
---|
72 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
---|
73 | <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 ); ?>" /> |
---|
74 | </p> |
---|
75 | <?php |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Handles updating settings for the current Search widget instance. |
---|
80 | * |
---|
81 | * @since 2.8.0 |
---|
82 | * |
---|
83 | * @param array $new_instance New settings for this instance as input by the user via |
---|
84 | * WP_Widget::form(). |
---|
85 | * @param array $old_instance Old settings for this instance. |
---|
86 | * @return array Updated settings. |
---|
87 | */ |
---|
88 | public function update( $new_instance, $old_instance ) { |
---|
89 | $instance = $old_instance; |
---|
90 | $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '' ) ); |
---|
91 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
---|
92 | return $instance; |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|