forked from raideus/wp-advanced-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpas.php
264 lines (222 loc) · 6.97 KB
/
wpas.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/*
Plugin Name: WP Advanced Search Framework
Plugin URI: http://wpadvancedsearch.com
Version: 1.4.3
Author: Sean Butze
Author URI: http://seanbutze.com
License: GPLv2 or later
*/
$WPAS_FORMS = array();
require_once('lib.php');
require_once('init.php');
require_once('config/form.default.php');
class WP_Advanced_Search {
private $factory;
private $errors;
private $args;
private $request;
private $debug;
private $ajax;
private $query;
public $debug_level;
function __construct($id = '', $request = null) {
$this->errors = array();
$this->request = array();
$this->args = $this->get_form_args($id);
$this->args = $this->process_args($this->args);
$this->ajax = $this->args['form']['ajax'];
$this->debug = $this->args['debug'];
$this->debug_level = $this->args['debug_level'];
$this->factory = new WPAS\Factory($this->args, $request);
$this->query = $this->factory->buildQueryObject();
}
/**
* Get arguments for a form based on its registered ID
*
* @param $id
* @return array|mixed
*/
private function get_form_args($id) {
global $WPAS_FORMS;
if (empty($WPAS_FORMS)) {
$this->errors[] = "No forms have been configured.";
return array();
}
if (empty($id)) {
if (!empty($WPAS_FORMS['default'])) return $WPAS_FORMS['default'];
else return reset($WPAS_FORMS);
} else if (empty($WPAS_FORMS[$id])) {
$this->errors[] = "WPAS form with ID \"".$id."\" is not registered.";
return array();
}
return $WPAS_FORMS[$id];
}
/**
* Print HTML content of the search form
*/
public function the_form() {
$form = $this->factory->getForm();
if ($this->debug) $form->addClass('wpas-debug-enabled');
echo $form->toHTML();
}
/**
* Create and return WP_Query object for the search instance
*
* @return WP_Query
*/
public function query() {
if (!$this->ajax_enabled()) $this->print_debug();
return $this->query;
}
/**
* Displays range of results displayed on the current page.
*
* @return string
*/
function results_range( $args = array() ) {
return WPAS\Helper\ResultsRange::make($this->query, $args);
}
/**
* Displays pagination links
*/
public function pagination( $args = array() ) {
echo WPAS\Helper\Pagination::make($this->query, $args, $this->ajax_enabled());
}
/**
* Create string of debug information
*
* For use when WPAS_DEBUG is enabled, or when calling the
* print_debug() method.
*
* When $log is set to 'verbose', the output will contain a full var dump
* of the generated WP_Query object.
*
* @param string $level
* @return string
*/
public function create_debug_output() {
$level = $this->debug_level;
$errors = $this->get_errors();
$wp_query_obj = $this->factory->getWPQuery();
$output = "WPAS DEBUG\n";
$output .= "------------------------------------\n";
$output .= "|| Errors\n";
$output .= "------------------------------------\n";
if (empty($errors)) {
$output .= "No errors detected.\n";
} else {
$output .= count($errors) . " errors detected:\n";
$output .= print_r($errors, true) . "\n";
}
$output .= "------------------------------------\n";
$output .= "|| WP_Query Arguments\n";
$output .= "------------------------------------\n";
$output .= print_r($wp_query_obj->query, true) . "\n";
$output .= "------------------------------------\n";
$output .= "|| MySQL Query \n";
$output .= "------------------------------------\n";
$output .= print_r($wp_query_obj->request, true) . "\n";
$output .= "------------------------------------\n";
$output .= "|| Request Data \n";
$output .= "------------------------------------\n";
$output .= print_r($this->get_request(), true) . "\n";
if ($level == 'verbose') {
$output .= "------------------------------------\n";
$output .= "|| WP_Query Object Dump\n";
$output .= "------------------------------------\n";
$output .= print_r($wp_query_obj, true);
}
return $output;
}
/**
* Print debug information
*/
public function print_debug() {
if ($this->debug === false) return;
$output = $this->create_debug_output();
echo '<pre>' . $output . '</pre>';
}
/**
* @return bool
*/
public function debug_enabled() {
return $this->debug;
}
/**
* Get array of errors generated during setup/configuration of search
* instance
*
* @return array
*/
public function get_errors() {
$errors = $this->errors;
if (is_object($this->factory)) {
$errors = array_merge($this->errors, $this->factory->getErrors());
}
return $errors;
}
public function set_error($err_msg) {
$this->errors[] = $err_msg;
}
/**
* Get Ajax configuration
*
* @return mixed
*/
public function get_ajax() {
return $this->ajax;
}
/**
* Returns true if ajax is enabled for the current search instance
*
* @return bool
*/
public function ajax_enabled() {
return $this->ajax->isEnabled();
}
/**
* Pre process arguments, translate argument blocks into config objects
*
* @param $args
* @return mixed
*/
private function process_args($args) {
// Establish AJAX configuration
$args = $this->set_ajax_config($args);
// Set debug mode and debug level
$args = $this->set_debug_args($args);
return $args;
}
private function set_ajax_config($args) {
$ajax_args = array();
if (!isset($args['form'])) $args['form'] = array();
if (isset($args['form']['ajax'])) {
$ajax_args = $args['form']['ajax'];
}
$args['form']['ajax'] = new WPAS\AjaxConfig($ajax_args);
return $args;
}
private function set_debug_args($args)
{
$debug = false;
if (defined('WPAS_DEBUG') && WPAS_DEBUG) {
$debug = true;
} else if (!empty($args['debug']) && $args['debug']) {
$debug = true;
}
$debug_level = 'log';
if (defined('WPAS_DEBUG_LEVEL') && WPAS_DEBUG_LEVEL) {
$debug_level = WPAS_DEBUG_LEVEL;
} else if (!empty($args['debug_level']) && $args['debug_level']) {
$debug_level = $args['debug_level'];
}
$args['debug'] = $debug;
$args['debug_level'] = $debug_level;
return $args;
}
private function get_request() {
$request = $this->factory->getRequest();
return $request->all();
}
}