/
lift-search-form.php
234 lines (195 loc) · 6.44 KB
/
lift-search-form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
require_once __DIR__ . '/form-controls.php';
// Make sure class name doesn't exist
if ( !class_exists( 'Lift_Search_Form' ) ) {
/**
* Lift_Search_Form is a class for building the fields and html used in the Lift Search plugin.
* This class uses the singleton design pattern, and shouldn't be called more than once on a document.
*
* There are three filters within the class that can be used to modify the output:
*
* 'lift_filters_default_fields' can be used to remove default search fields on the form.
*
* 'lift_filters_form_field_objects' can be used to add or remove Voce Search Field objects.
*
* 'lift_search_form' can be used to modify the form html output
*/
class Lift_Search_Form {
private static $instances;
/**
* Returns an instance of the search form based on the given WP_Query instance
* @global WP_Query $wp_query
* @param WP_Query $a_wp_query
* @return Lift_Search_Form
*/
public static function GetInstance( $a_wp_query = null ) {
global $wp_query;
if ( is_null( $a_wp_query ) ) {
$a_wp_query = $wp_query;
}
$query_id = spl_object_hash( $a_wp_query );
if ( !isset( self::$instances ) ) {
self::$instances = array( );
}
if ( !isset( self::$instances[$query_id] ) ) {
self::$instances[$query_id] = new Lift_Search_Form( $a_wp_query );
}
return self::$instances[$query_id];
}
private $fields = array( );
/**
* WP_Query instance reference for search
* @var Lift_WP_Query
*/
public $lift_query;
/**
* Lift_Search_Form constructor.
*/
private function __construct( $wp_query ) {
$this->lift_query = Lift_WP_Query::GetInstance( $wp_query );
$this->fields = apply_filters( 'lift_form_filters', array( ), $this );
}
/**
* Returns the base url for the search.
*
* @todo allow the instance to be detached from the global site search
* @return string
*/
public function getSearchBaseURL() {
return user_trailingslashit( site_url() );
}
/**
* Returns the current state of the search form.
*
* @todo allow instances to be detached from the global request vars
* @return array
*/
public function getStateVars() {
return array_merge( $_GET, $_POST );
}
/**
* Builds the sort by dropdown/select field.
*/
public function add_sort_field() {
if ( !$selected = $this->lift_query->wp_query->get( 'orderby' ) ) {
$selected = 'relevancy';
}
$options = array(
'label' => ($selected) ? ucwords( $selected ) : 'Sort By',
'value' => array(
'Date' => 'date',
'Relevancy' => 'relevancy'
),
'selected' => $selected,
);
$this->add_field( 'orderby', 'select', $options );
}
/**
* Get custom query var from the wp_query
* @param string $var query variable
* @return array|boolean query variable if it exists, else false
*/
public function get_query_var( $var ) {
return ( $val = get_query_var( $var ) ) ? $val : false;
}
/**
* Builds the html form using all fields in $this->fields.
* @return string search form
*/
public function form() {
$search_term = (is_search()) ? get_search_query( false ) : "";
$html = '<form role="search" class="lift-search no-js" id="searchform" action="' . esc_url( $this->getSearchBaseURL() ) . '"><div>';
$html .= sprintf( "<input type='text' name='s' id='s' value='%s' />", esc_attr( $search_term ) );
$html .= ' <input type="submit" id="searchsubmit" value="' . esc_attr__( 'Search' ) . '" />';
$html .= '<fieldset class="lift-search-form-filters"><ul>';
if ( count( $this->getStateVars() ) > 1 ) {
$html .= sprintf( '<li class="reset"><a href="%s">Reset</a></li>', esc_url( add_query_arg( array( 's' => $search_term ), $this->getSearchBaseURL() ) ) );
}
$html .= $this->form_filters();
$html .= "</ul></fieldset>";
$html .= "</div></form>";
return apply_filters( 'lift_search_form', $html );
}
public function loop() {
$path = dirname( __DIR__ ) . '/templates/lift-loop.php';
$path = apply_filters( 'lift_search_get_template_loop', $path );
include_once $path;
}
/**
* Renders the filters
* @return string, The HTML output for the rendered filters
*/
public function form_filters() {
$html = '';
$args = array( 'before_field' => '<li>', 'after_field' => '</li>' );
foreach ( $this->fields as $field ) {
$html .= apply_filters( 'lift_form_field_' . $field, '', $this, $args );
}
return apply_filters( 'lift_form_fields_html', $html, $this, $args );
}
}
}
/**
* Template Tags
*/
if ( !function_exists( 'lift_search_form' ) ) {
function lift_search_form( $a_wp_query = null ) {
echo Lift_Search_Form::GetInstance( $a_wp_query )->form();
}
}
if ( !function_exists( 'lift_search_filters' ) ) {
function lift_search_filters( $a_wp_query = null ) {
echo Lift_Search_Form::GetInstance( $a_wp_query )->form_filters();
}
}
if ( !function_exists( 'lift_loop' ) ) {
function lift_loop() {
echo Lift_Search_Form::GetInstance()->loop();
}
}
/**
* Embed the Lift Search form in a sidebar
* @class Lift_Form_Widget
*/
class Lift_Form_Widget extends WP_Widget {
/**
* @constructor
*/
public function __construct() {
parent::__construct(
'lift_form_widget', "Lift Search Form", array( 'description' => "Add a Lift search form" )
);
}
/**
* Output the widget
*
* @method widget
*/
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $before_widget;
if ( $title )
echo $before_title . esc_html( $title ) . $after_title;
if ( class_exists( 'Lift_Search_Form' ) ) {
echo Lift_Search_Form::GetInstance()->form();
}
echo $after_widget;
}
function form( $instance ) {
$instance = wp_parse_args( ( array ) $instance, array( 'title' => '' ) );
$title = $instance['title'];
?>
<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>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args( ( array ) $new_instance, array( 'title' => '' ) );
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
}
add_action( 'widgets_init', function() {
register_widget( 'Lift_Form_Widget' );
} );