PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/classes/widget-search.php

https://bitbucket.org/lgorence/quickpress
PHP | 171 lines | 88 code | 28 blank | 55 comment | 6 complexity | c75d81165916d191cb380d3f6d13f1e1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * The Search widget replaces the default WordPress Search widget. This version gives total
  4. * control over the output to the user by allowing the input of various arguments that typically
  5. * represent a search form. It also gives the user the option of using the theme's search form
  6. * through the use of the get_search_form() function.
  7. *
  8. * @package Hybrid
  9. * @subpackage Classes
  10. * @author Justin Tadlock <justin@justintadlock.com>
  11. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  12. * @link http://themehybrid.com/hybrid-core
  13. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  14. */
  15. /**
  16. * Search Widget Class
  17. *
  18. * @since 0.6.0
  19. */
  20. class Hybrid_Widget_Search extends WP_Widget {
  21. /**
  22. * Set up the widget's unique name, ID, class, description, and other options.
  23. *
  24. * @since 1.2.0
  25. */
  26. function __construct() {
  27. /* Set up the widget options. */
  28. $widget_options = array(
  29. 'classname' => 'search',
  30. 'description' => esc_html__( 'An advanced widget that gives you total control over the output of your search form.', 'hybrid-core' )
  31. );
  32. /* Set up the widget control options. */
  33. $control_options = array(
  34. 'width' => 525,
  35. 'height' => 350
  36. );
  37. /* Create the widget. */
  38. $this->WP_Widget(
  39. 'hybrid-search', // $this->id_base
  40. __( 'Search', 'hybrid-core' ), // $this->name
  41. $widget_options, // $this->widget_options
  42. $control_options // $this->control_options
  43. );
  44. }
  45. /**
  46. * Outputs the widget based on the arguments input through the widget controls.
  47. *
  48. * @since 0.6.0
  49. */
  50. function widget( $sidebar, $instance ) {
  51. extract( $sidebar );
  52. /* Output the theme's $before_widget wrapper. */
  53. echo $before_widget;
  54. /* If a title was input by the user, display it. */
  55. if ( !empty( $instance['title'] ) )
  56. echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
  57. /* If the user chose to use the theme's search form, load it. */
  58. if ( !empty( $instance['theme_search'] ) ) {
  59. get_search_form();
  60. }
  61. /* Else, create the form based on the user-selected arguments. */
  62. else {
  63. /* Set up some variables for the search form. */
  64. if ( empty( $instance['search_text'] ) )
  65. $instance['search_text'] = '';
  66. $search_text = ( ( is_search() ) ? esc_attr( get_search_query() ) : esc_attr( $instance['search_text'] ) );
  67. /* Open the form. */
  68. $search = '<form method="get" class="search-form" id="search-form' . esc_attr( $this->id_base ) . '" action="' . home_url() . '/"><div>';
  69. /* If a search label was set, add it. */
  70. if ( !empty( $instance['search_label'] ) )
  71. $search .= '<label for="search-text' . esc_attr( $this->id_base ) . '">' . $instance['search_label'] . '</label>';
  72. /* Search form text input. */
  73. $search .= '<input class="search-text" type="text" name="s" id="search-text' . esc_attr( $this->id_base ) . '" value="' . $search_text . '" onfocus="if(this.value==this.defaultValue)this.value=\'\';" onblur="if(this.value==\'\')this.value=this.defaultValue;" />';
  74. /* Search form submit button. */
  75. if ( $instance['search_submit'] )
  76. $search .= '<input class="search-submit button" name="submit" type="submit" id="search-submit' . esc_attr( $this->id_base ). '" value="' . esc_attr( $instance['search_submit'] ) . '" />';
  77. /* Close the form. */
  78. $search .= '</div></form>';
  79. /* Display the form. */
  80. echo $search;
  81. }
  82. /* Close the theme's widget wrapper. */
  83. echo $after_widget;
  84. }
  85. /**
  86. * Updates the widget control options for the particular instance of the widget.
  87. *
  88. * @since 0.6.0
  89. */
  90. function update( $new_instance, $old_instance ) {
  91. $instance = $new_instance;
  92. $instance['title'] = strip_tags( $new_instance['title'] );
  93. $instance['search_label'] = strip_tags( $new_instance['search_label'] );
  94. $instance['search_text'] = strip_tags( $new_instance['search_text'] );
  95. $instance['search_submit'] = strip_tags( $new_instance['search_submit'] );
  96. $instance['theme_search'] = ( isset( $new_instance['theme_search'] ) ? 1 : 0 );
  97. return $instance;
  98. }
  99. /**
  100. * Displays the widget control options in the Widgets admin screen.
  101. *
  102. * @since 0.6.0
  103. */
  104. function form( $instance ) {
  105. /* Set up the default form values. */
  106. $defaults = array(
  107. 'title' => esc_attr__( 'Search', 'hybrid-core' ),
  108. 'theme_search' => false,
  109. 'search_label' => '',
  110. 'search_text' => '',
  111. 'search_submit' => ''
  112. );
  113. /* Merge the user-selected arguments with the defaults. */
  114. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  115. <div class="hybrid-widget-controls columns-2">
  116. <p>
  117. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'hybrid-core' ); ?></label>
  118. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  119. </p>
  120. <p>
  121. <label for="<?php echo $this->get_field_id( 'search_label' ); ?>"><?php _e( 'Search Label:', 'hybrid-core' ); ?></label>
  122. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'search_label' ); ?>" name="<?php echo $this->get_field_name( 'search_label' ); ?>" value="<?php echo esc_attr( $instance['search_label'] ); ?>" />
  123. </p>
  124. <p>
  125. <label for="<?php echo $this->get_field_id( 'search_text' ); ?>"><?php _e( 'Search Text:', 'hybrid-core' ); ?></label>
  126. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'search_text' ); ?>" name="<?php echo $this->get_field_name( 'search_text' ); ?>" value="<?php echo esc_attr( $instance['search_text'] ); ?>" />
  127. </p>
  128. </div>
  129. <div class="hybrid-widget-controls columns-2 column-last">
  130. <p>
  131. <label for="<?php echo $this->get_field_id( 'search_submit' ); ?>"><?php _e( 'Search Submit:', 'hybrid-core' ); ?></label>
  132. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'search_submit' ); ?>" name="<?php echo $this->get_field_name( 'search_submit' ); ?>" value="<?php echo esc_attr( $instance['search_submit'] ); ?>" />
  133. </p>
  134. <p>
  135. <label for="<?php echo $this->get_field_id( 'theme_search' ); ?>">
  136. <input class="checkbox" type="checkbox" <?php checked( $instance['theme_search'], true ); ?> id="<?php echo $this->get_field_id( 'theme_search' ); ?>" name="<?php echo $this->get_field_name( 'theme_search' ); ?>" /> <?php _e( 'Use theme\'s <code>searchform.php</code>?', 'hybrid-core' ); ?></label>
  137. </p>
  138. </div>
  139. <div style="clear:both;">&nbsp;</div>
  140. <?php
  141. }
  142. }
  143. ?>