class-wp-widget-search.php 2.7 KB

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