Engine.php
9.94 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* SearchWP Engine.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
use SearchWP\Utils;
use SearchWP\Source;
/**
* Class Engine is responsible for modeling an engine from a settings array.
*
* @since 4.0
*/
class Engine implements \JsonSerializable {
/**
* Engine name.
*
* @since 4.0
* @var string
*/
private $name;
/**
* Engine label.
*
* @since 4.0
* @var string
*/
private $label;
/**
* Engine Sources.
*
* @since 4.0
* @var array
*/
private $sources = [];
/**
* Engine Settings.
*
* @since 4.0
* @var array
*/
private $settings = [
'stemming' => true,
'adminengine' => false,
];
/**
* Error collection.
*
* @since 4.0
* @var array
*/
private $errors = [];
/**
* Engine constructor. Builds Engine model from saved Engine settings.
*
* @since 4.0
* @param string $name The engine name.
*/
function __construct( string $name = 'default', array $settings = [] ) {
$name = sanitize_key( trim( $name ) );
if ( empty( $name ) || ( is_string( $name ) && strlen( $name ) > 191 ) ) {
$this->errors[] = new \WP_Error( 'name', __( 'Invalid SearchWP engine name', 'searchwp' ), $name );
} else {
$this->name = sanitize_key( $name );
// If this engine name exists, we can load the settings.
if ( empty( $settings ) && $existing_settings = Settings::get_engine_settings( $this->name ) ) {
$settings = $existing_settings;
}
// If there were no settings, we are going to assume a default set of Sources.
if ( empty( $settings ) ) {
// We're going to normalize anyway, but we needed to know the incoming settings were empty.
$settings = $this->normalize_settings( $settings );
$index = \SearchWP::$index;
$settings['sources'] = array_filter( array_map( function( $source ) {
// A Source is only default if it represents WP_Post.
if ( 0 !== strpos( $source->get_name(), 'post' . SEARCHWP_SEPARATOR ) ) {
return false;
}
return json_decode( json_encode( $source ), true );
}, $index->get_default_sources( true ) ) );
$settings = Utils::normalize_engine_config( $settings );
}
$this->label = ! empty( $settings['label'] ) ? $settings['label'] : ucfirst( $this->name );
$settings = $this->normalize_settings( $settings );
$this->apply_settings( $settings );
}
}
/**
* Applies saved settings array to properties.
*
* @since 4.0
* @param array $dirty_settings Settings as array.
* @return void
*/
private function apply_settings( array $settings ) {
$errors = [];
$models = \SearchWP::$index->get_sources();
$sources = array_filter( array_map( function( $source_name, $source_settings ) use ( $models, $errors ) {
if (
! array_key_exists( $source_name, $models )
|| ( apply_filters( 'searchwp\source\check_db', false ) && ! $models[ $source_name ]->is_valid() )
) {
$errors[] = $source_name;
return false;
}
$settings = $this->normalize_source_settings( $source_settings );
$source = clone $models[ $source_name ];
$this->apply_attributes_settings( $settings['attributes'], $source );
$this->apply_rules_settings( $settings['rules'], $source );
$this->apply_options_settings( $settings['options'], $source );
return $source;
}, array_keys( $settings['sources'] ), array_values( $settings['sources'] ) ) );
foreach ( $sources as $source ) {
$source->init();
$this->sources[ $source->get_name() ] = $source;
}
if ( ! empty( $errors ) ) {
$this->errors[] = new \WP_Error(
'source',
__( 'Invalid SearchWP engine Source(s)', 'searchwp' ),
$errors
);
}
if (
! isset( $settings['settings']['stemming'] )
|| empty( $settings['settings']['stemming'] )
) {
$this->settings['stemming'] = false;
}
if (
isset( $settings['settings']['adminengine'] )
&& ! empty( $settings['settings']['adminengine'] )
&& 'false' !== $settings['settings']['adminengine']
) {
$this->settings['adminengine'] = true;
}
}
/**
* Applies saved Options settings to this Source.
*
* @since 4.0
* @param array $settings Settings to apply.
* @return void
*/
private function apply_options_settings( array $settings, Source $source ) {
// Options are quite restricted. At this time there's only Weight Transfer handling.
foreach ( $settings as $option => $config ) {
$source->set_option_config( $option, $config );
}
}
/**
* Applies saved Attribute settings to this Source's Attributes.
*
* @since 4.0
* @param array $settings Settings to apply.
* @return void
*/
private function apply_attributes_settings( array $settings, Source $source ) {
$settings = array_filter( $settings, function( $setting ) use ( $source ) {
return array_key_exists( $setting, $source->get_attributes() );
}, ARRAY_FILTER_USE_KEY );
foreach( $settings as $attribute_name => $attribute_settings ) {
$source->get_attributes()[ $attribute_name ]->set_settings( $attribute_settings );
}
}
/**
* Applies saved Rules settings to this Source.
*
* @since 4.0
* @param array $settings Settings to apply.
* @return void
*/
private function apply_rules_settings( array $settings, Source $source ) {
// Rules are stored in groups, as each group controls the logic of the Rule combination.
$rule_group_index = 0;
foreach ( $settings as $rule_group ) {
$type = isset( $rule_group['type'] ) && 'NOT IN' === $rule_group['type'] ? 'NOT IN' : 'IN';
$rules = isset( $rule_group['rules'] ) && is_array( $rule_group['rules'] ) ? $rule_group['rules'] : [];
foreach ( $rules as $rule_settings ) {
$source->get_rules()[ $rule_settings['rule'] ]->implement( $rule_group_index, $type, $rule_settings );
}
$rule_group_index++;
}
}
/**
* Removes a Source from the Engine Sources.
*
* @since 4.0
* @param string $source_name Source to remove.
* @return bool Whether the Source was removed.
*/
public function remove_source( string $source_name ) {
foreach ( $this->sources as $index => $source ) {
if ( $source_name === $source->get_name() ) {
unset( $this->sources[ $index ] );
return true;
}
}
return false;
}
/**
* Getter for Engine settings.
*
* @since 4.0
* @return array
*/
public function get_settings() {
return $this->settings;
}
/**
* Retrieves Source Attribute (with Options) settings.
*
* @since 4.0
* @param Source $source Source to consider.
* @param null|string $attribute Attribute to consider.
* @return array Attribute settings for the Source.
*/
public function get_source_attribute_options_settings( Source $source, string $attribute_name = '' ) {
$settings = array_values( array_filter( array_map( function( $source ) use ( $attribute_name ) {
if ( $attribute_name && $source->get_attribute( $attribute_name ) ) {
return $source->get_attribute( $attribute_name )->get_settings();
} else {
return array_map( function( $attribute ) {
return $attribute->get_settings();
}, $source->get_attributes() );
}
}, array_filter( $this->sources, function( $this_source ) use ( $source ) {
return $source->get_name() === $this_source->get_name();
} ) ) ) );
// $settings is either a one dimensional array, or an array of arrays.
if ( empty( $settings ) ) {
return $settings;
}
// If this Attribute has Options, we have a multidimensional array that needs to be merged.
if ( isset( $settings[0] ) && is_array( $settings[0] ) ) {
$settings = call_user_func_array( 'array_merge', $settings );
}
return $settings;
}
/**
* Ensure that expected array keys exist when retrieving Source settings.
*
* @since 4.0
* @param array $settings The settings array to normalize.
* @return array The normalized settings array.
*/
public function normalize_source_settings( array $settings ) {
return [
'options' => empty( $settings['options'] ) ? [] : $settings['options'],
'rules' => empty( $settings['rules'] ) ? [] : $settings['rules'],
'attributes' => empty( $settings['attributes'] ) ? [] : $settings['attributes'],
];
}
/**
* Ensure that expected array keys exist when retrieving settings.
*
* @since 4.0
* @param array $settings The settings array to normalize.
* @return array The normalized settings array.
*/
public function normalize_settings( array $settings ) {
return [
'label' => empty( $settings['label'] ) ? '' : sanitize_text_field( $settings['label'] ),
'settings' => empty( $settings['settings'] ) ? [] : $settings['settings'],
'sources' => empty( $settings['sources'] ) ? [] : $settings['sources'],
];
}
/**
* Getter for Engine Sources.
*
* @since 4.0
* @return array Sources.
*/
public function get_sources() {
return $this->sources;
}
/**
* Getter for a single Source of this Engine.
*
* @since 4.0
* @param string $source_name Name of the Source
* @return mixed|false Source (or false when Source is invalid)
*/
public function get_source( string $source_name ) {
foreach ( $this->sources as $index => $source ) {
if ( $source_name === $source->get_name() ) {
return $source;
}
}
return false;
}
/**
* Getter for label.
*
* @since 4.0
* @return string The label.
*/
public function get_label() {
return $this->label;
}
/**
* Getter for name.
*
* @since 4.0
* @return mixed The engine name.
*/
public function get_name() {
return $this->name;
}
/**
* Getter for errors.
*
* @since 4.2.6
*
* @return array Error collection.
*/
public function get_errors() {
return $this->errors;
}
/**
* Provides the model to use when representing this Engine as JSON.
*
* @since 4.0
* @return array
*/
public function jsonSerialize(): array {
$sources = array_map( function ( $source ) {
// We want to trigger $source->jsonSerialize().
return json_decode( json_encode( $source ) );
}, $this->sources );
$model = [
'name' => $this->name,
'label' => $this->label,
'settings' => $this->settings,
'sources' => $sources,
];
return $model;
}
}