|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Main class for search module. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Eoxia <[email protected]> |
|
6
|
|
|
* @copyright (c) 2015-2018 Eoxia <[email protected]>. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GPLv3 <https://spdx.org/licenses/GPL-3.0-or-later.html> |
|
9
|
|
|
* |
|
10
|
|
|
* @package EO_Framework\EO_Search\Class |
|
11
|
|
|
* |
|
12
|
|
|
* @since 1.1.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace eoxia; |
|
16
|
|
|
|
|
17
|
|
|
defined( 'ABSPATH' ) || exit; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Search Class. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Search_Class extends Singleton_Util { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* List of registered search. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.1.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $registered_search = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @since 1.1.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function construct() {} |
|
39
|
|
|
|
|
40
|
|
|
public function register_search( $slug, $atts ) { |
|
41
|
|
|
$this->registered_search[ $slug ] = $this->construct_atts( $slug, $atts ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function get_registered_search( $slug ) { |
|
45
|
|
|
return $this->registered_search[ $slug ]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function construct_atts( $slug, $atts ) { |
|
49
|
|
|
$default = array( |
|
50
|
|
|
'value' => '', |
|
51
|
|
|
'hidden_value' => '', |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$atts['slug'] = $slug; |
|
55
|
|
|
$atts['id'] = ''; |
|
56
|
|
|
|
|
57
|
|
|
if ( ! empty( $atts['label'] ) ) { |
|
58
|
|
|
$atts['id'] = sanitize_title( $atts['label'] ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$atts = wp_parse_args( $atts, $default ); |
|
62
|
|
|
|
|
63
|
|
|
return $atts; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* This method get a registered search in the $registed_search array |
|
68
|
|
|
* by the $slug. |
|
69
|
|
|
* |
|
70
|
|
|
* This method call the "main" view of search module. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 1.1.0 |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $atts [description] |
|
75
|
|
|
*/ |
|
76
|
|
|
public function display( $slug, $visible_value = '', $hidden_value = '' ) { |
|
77
|
|
|
$atts = $this->get_registered_search( $slug ); |
|
78
|
|
|
|
|
79
|
|
|
\eoxia\View_Util::exec( 'eo-framework', 'wpeo_search', 'main', array( |
|
80
|
|
|
'atts' => $atts, |
|
81
|
|
|
) ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* This method switch by type for call another method. |
|
86
|
|
|
* |
|
87
|
|
|
* @since 1.1.0 |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $term Term of search. |
|
90
|
|
|
* @param string $type Type of search. |
|
91
|
|
|
* @param array $args Other parameters. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array Array of results. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function search( $term, $type, $args ) { |
|
96
|
|
|
switch ( $type ) { |
|
97
|
|
|
case "user": |
|
98
|
|
|
$results = $this->search_user( $term, $args ); |
|
99
|
|
|
break; |
|
100
|
|
|
case "post": |
|
101
|
|
|
$results = $this->search_post( $term, $args ); |
|
102
|
|
|
break; |
|
103
|
|
|
default: |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $results; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function search_user( $term, $data ) { |
|
111
|
|
|
if ( ! empty( $term ) ) { |
|
112
|
|
|
$args = array( |
|
113
|
|
|
'search' => '*' . $term . '*', |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
if ( ! empty( $data['args'] ) ) { |
|
117
|
|
|
$args = array_merge( $args, $data['args'] ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$results = User_Class::g()->get( $args ); |
|
121
|
|
|
} else { |
|
122
|
|
|
$results = User_Class::g()->get( array( |
|
123
|
|
|
'exclude' => array( 1 ), |
|
124
|
|
|
) ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $results; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function search_post( $term, $data ) { |
|
131
|
|
|
$results = array(); |
|
132
|
|
|
|
|
133
|
|
|
$get_args = array( 'meta_or_title' => $term ); |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
if ( ! empty( $data['args']['meta_query'] ) ) { |
|
137
|
|
|
$get_args['meta_query'] = $this->construct_meta_query( $term, $data['args']['meta_query'] ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$get_args = array_merge( $get_args, $data['args'] ); |
|
141
|
|
|
|
|
142
|
|
|
if ( ! empty( $data['args']['model_name'] ) ) { |
|
143
|
|
|
foreach ( $data['args']['model_name'] as $model_name ) { |
|
144
|
|
|
$results = array_merge( $results, $model_name::g()->get( $get_args ) ); |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
$get_args['posts_per_page'] = -1; |
|
148
|
|
|
$results = get_posts( $get_args ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $results; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function construct_meta_query( $term, $args_meta_query ) { |
|
155
|
|
|
if ( ! empty( $args_meta_query ) ) { |
|
156
|
|
|
foreach ( $args_meta_query as &$meta_data ) { |
|
157
|
|
|
if ( ! empty( $meta_data ) && is_array( $meta_data ) ) { |
|
158
|
|
|
$meta_data['value'] = $term; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $args_meta_query; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
global $eo_search; |
|
168
|
|
|
$eo_search = Search_Class::g(); |
|
169
|
|
|
|