-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.php
126 lines (116 loc) · 3.71 KB
/
frontend.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
<?php
/**
* FIXME:
* Situations to consider...
* posts.all datatype
* with multiple words (e.g. if I type author name and title)
* or joins
* checkbox inputs
* custom fields
*
*/
class WPCustomFieldsSearchFrontEnd {
function __construct(){
/** Front End Search */
add_filter('posts_join',array($this,'posts_join'));
add_filter('posts_where',array($this,'posts_where'));
add_filter('posts_groupby',array($this,'posts_groupby'));
add_filter('posts_orderby',array($this,'posts_orderby'));
add_filter('home_template',array($this,'show_search_template_if_posted'));
add_filter('page_template',array($this,'show_search_template_if_posted'));
/* Relevanssi compatibility - Over-rides relevanssi changes when the search form has been used*/
add_filter('posts_request',array(&$this,'store_search'),9);
add_filter('posts_request',array(&$this,'restore_search'),11);
add_filter('the_posts',array(&$this,'store_search'),9);
add_filter('the_posts',array(&$this,'restore_search'),11);
}
function _is_search_request(){
return $this->_get_posted_form();
}
function _load_keyed($key){
$array = apply_filters($key,array());
$keyed = array();
foreach($array as $item){
$keyed[$item->getId()] = $item;
}
return $keyed;
}
function _get_posted_form(){
static $posted_form;
$source_id = $_REQUEST['wpcfs-search-source'];
if($source_id && !$posted_form){
list($widget_type,$index)=explode('-',$source_id,2);
if($widget_type!='wp_custom_fields_search_1_0')
throw new Exception("Should this ever be different?");//TODO:Get rid of this
$widgets = get_option('widget_'.$widget_type);
$posted_form = json_decode($widgets[$index]['data'],true);
$inputs = $this->_load_keyed("wp_custom_fields_search_inputs");
$datatypes = $this->_load_keyed("wp_custom_fields_search_datatypes");
$comparisons = $this->_load_keyed("wp_custom_fields_search_comparisons");
$index = 0;
foreach($posted_form['inputs'] as &$field){
$field['index'] = ++$index;
$field['input'] = $inputs[$field['input']];
$field['datatype'] = $datatypes[$field['datatype']];
$field['comparison'] = $comparisons[$field['comparison']];
}
}
return $posted_form;
}
function posts_join($join){
if($this->_is_search_request()){
throw new Exception("Unimplemented");
}
return $join;
}
function posts_where($where){
if($this->_is_search_request()){
$form = $this->_get_posted_form();
$extra_where = array();
foreach($form['inputs'] as $params){
if(!$params['input']->is_populated($params,$_REQUEST)){
continue;
}
$field_names=$params['datatype']->get_sql_fields($params,$_REQUEST);
$value=$params['input']->get_value($params,$_REQUEST);
$sub_where = array();
foreach($field_names as $field_name){
$sub_where[] = $params['comparison']->get_sql_where_clause($params,$field_name,$value);
}
$extra_where[] = join(" OR",$sub_where);
}
$where = " (".join(" AND ",$extra_where).")";
}
return $where;
}
function posts_groupby($groupby){
if($this->_is_search_request()){
global $wpdb;
$groupby = $wpdb->posts.".ID";
}
return $groupby;
}
function posts_orderby($orderby){
if($this->_is_search_request()){
throw new Exception("Unimplemented");
}
return $orderby;
}
function show_search_template_if_posted($template){
if($this->_is_search_request()){
$template = get_query_template("search");
}
return $template;
}
function store_search($sql){
$this->sql = $sql;
return $sql;
}
function restore_search($sql){
if($this->_is_search_request()){
$sql = $this->sql;
}
return $sql;
}
}
new WPCustomFieldsSearchFrontEnd();