class-wp-widget-search.php 2.6 KB

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