Settings.php
9.52 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
395
396
397
398
399
400
<?php
/**
* SearchWP Settings.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
/**
* Class Settings is responsible for handling project settings.
*
* @since 4.0
*/
class Settings {
/**
* Capability requirement for managing Settings.
*
* @since 4.0
* @since 4.2.6 Visibility changed from public to private.
*
* @var string
*/
private static $capability = 'manage_options';
/**
* Cache key.
*
* @since 4.0
*
* @var string
*/
public static $engines_cache_key = SEARCHWP_PREFIX . 'engines_settings';
/**
* Comprehensive list of (unprefixed) Settings keys.
*
* @since 4.0
* @var string[]
*/
private static $keys = [
'engines',
'stopwords',
'synonyms',
'forms',
'results_page',
'migrated',
'upgraded_from',
'ignored_queries',
'dismissed_notices',
'trim_stats_logs_after',
'document_content_reset',
'document_content_reset_dismissed',
];
/**
* Comprehensive list of (unprefixed) Settings keys that should be autoloaded.
*
* @since 4.0
* @var string[]
*/
private static $autoload_keys = [
'debug',
'version',
'index_outdated',
'partial_matches',
'highlighting',
'parse_shortcodes',
'do_suggestions',
'quoted_search_support',
'tokenize_pattern_matches',
'remove_min_word_length',
'reduced_indexer_aggressiveness',
'hide_announcements',
'nuke_on_delete',
'indexer_paused',
'license',
'new_activation',
];
/**
* Getter for capability tag.
*
* @since 4.0.12
*
* @return string
*/
public static function get_capability() {
return (string) apply_filters( 'searchwp\settings\capability', self::$capability );
}
/**
* Getter for all settings.
*
* @since 4.0
* @return array Settings.
*/
public static function get( string $setting = '', $type = null ) {
if ( ! empty( $setting ) ) {
return self::get_single( $setting, $type );
}
$index = \SearchWP::$index;
$engines = self::get_engines();
return [
'sources' => $index->get_sources(),
'engines' => $engines,
'weights' => Utils::get_weight_definitions(),
'misc' => [
'colors' => self::get_colors(),
'prefix' => SEARCHWP_PREFIX,
'hasInitialSave' => ! empty( $engines ),
'docContentResetAsk' => empty( self::get_single( 'document_content_reset', 'boolean' ) )
&& empty( self::get_single( 'document_content_reset_dismissed', 'boolean' ) ),
],
];
}
/**
* Getter for settings keys.
*
* @since 4.0
* @return string[]
*/
public static function get_keys() {
return array_merge( self::$keys, self::$autoload_keys );
}
/**
* Getter for autoloaded settings keys.
*
* @since 4.0
* @return string[]
*/
public static function get_autoload_keys() {
return self::$autoload_keys;
}
/**
* Retrieves a single Setting value.
*
* @since 4.0
* @param string $setting The setting key.
* @return mixed
*/
public static function get_single( string $setting, $type = null ) {
if ( ! in_array( $setting, self::get_keys() ) ) {
return null;
}
$cache = wp_cache_get( SEARCHWP_PREFIX . 'settings_' . $setting, '' );
if ( ! empty( $cache ) && is_array( $cache ) && array_key_exists( $setting, $cache ) ) {
return self::normalize_value( $cache[ $setting ], $type );
}
$setting_value = get_option( SEARCHWP_PREFIX . $setting );
// Because some values will be FALSE we're going to cache an array so as to flag the cache.
wp_cache_set( SEARCHWP_PREFIX . 'settings_' . $setting, [ $setting => $setting_value ], '', 1 );
return self::normalize_value( $setting_value, $type );
}
/**
* Noramlizes a value based on the passed type.
*
* @since 4.1
* @param mixed $setting_value The incoming value.
* @param mixed|null $type The type.
* @return mixed The normalized value.
*/
public static function normalize_value( $setting_value, $type = null ) {
if ( 'boolean' === $type || 'bool' === $type ) {
$setting_value = '1' == $setting_value ? true : false;
}
if ( 'array' === $type ) {
$setting_value = is_array( $setting_value ) ? $setting_value : [];
}
if ( 'int' === $type || 'integer' === $type ) {
$setting_value = intval( $setting_value );
}
return $setting_value;
}
/**
* Setter for setting.
*
* @since 4.0
* @param string $setting The setting key.
* @param mixed $value The setting value.
* @return mixed
*/
public static function update( string $setting = '', $value = null ) {
if ( ! in_array( $setting, self::get_keys() ) ) {
return null;
}
$autoload = in_array( $setting, self::$autoload_keys ) ? 'yes' : 'no';
wp_cache_delete( SEARCHWP_PREFIX . 'settings_' . $setting );
update_option( SEARCHWP_PREFIX . $setting, $value, $autoload );
// By default WP_Cache will return `false` if the key doesn't exist, but sometimes
// our intended value is `false` so we're going to wrap this in an array.
wp_cache_set( SEARCHWP_PREFIX . 'settings_' . $setting, [ $setting => $value ], '', 1 );
do_action( "searchwp\settings\update\\" . $setting, $value );
return $value;
}
/**
* Setup for colors given the current admin color scheme.
*
* @since 4.0
* @return array Hex codes for colors to use in the WP Admin.
*/
public static function get_colors() {
global $_wp_admin_css_colors;
$scheme_id = get_user_option( 'admin_color' );
// It's possible to completely remove all color schemes, so we have to be a bit verbose here.
$colors = isset( $_wp_admin_css_colors['fresh'] ) ? $_wp_admin_css_colors['fresh'] : (object) [
'name' => 'Default',
'url' => '',
'colors' => [ '#222', '#333', '#0073aa', '#00a0d2', ],
'icon_colors' => [ 'base' => '#a0a5aa', 'focus' => '#00a0d2', 'current' => '#fff', ],
];
$colors = isset( $_wp_admin_css_colors[ $scheme_id ] ) ? $_wp_admin_css_colors[ $scheme_id ] : $colors;
$current = isset( $colors->colors[2] ) ? $colors->colors[2] : null;
return [
'text' => '#444',
'heading' => '#23282d',
'border' => '#ccd0d4',
'input' => [ 'color' => '#32373c', 'border' => '#7e8993', ],
'link' => [ 'base' => '#0073aa', 'hover' => '#0096dd', ],
'admin' => $colors,
'highlight' => isset( $colors->colors[0] ) ? $colors->colors[0] : null,
'base' => isset( $colors->colors[1] ) ? $colors->colors[1] : null,
'current' => $current,
'hover' => isset( $colors->colors[3] ) ? $colors->colors[3] : null,
];
}
/**
* Getter for engines as name => label pairs.
*
* @since 4.0
* @return array Engine labels.
*/
public static function get_engines_as_name_label() {
return array_map( function( $engine ) {
return $engine->get_label();
}, self::get_engines() );
}
/**
* Deletes setting.
*
* @since 4.0
* @param string $setting The setting key.
* @return mixed
*/
public static function delete( string $setting = '' ) {
if ( ! in_array( $setting, self::get_keys() ) ) {
return null;
}
delete_option( SEARCHWP_PREFIX . $setting );
}
/**
* Getter for engines as Engine objects.
*
* @since 4.0
* @return array Engine models.
*/
public static function get_engines( $skip_cache = false ) {
// Statically cache engines to improve runtime performance.
static $_engines = null;
static $_engines_settings = null;
$engines_settings = self::_get_engines_settings( $skip_cache );
// Make sure we return statically cached $engines only if $engines_settings didn't change in the runtime.
if ( $_engines !== null && $_engines_settings === $engines_settings ) {
return $_engines;
}
$engines = [];
foreach ( $engines_settings as $engine => $engine_settings ) {
$engine_model = new Engine( $engine, $engine_settings );
if ( ! empty( $engine_model->get_name() ) ) {
$engines[ $engine ] = $engine_model;
}
}
// Save the static cache.
$_engines = $engines;
$_engines_settings = $engines_settings;
return $engines;
}
/**
* Getter for defined Admin Engine.
*
* @since 4.0
* @return string|false Name of Engine (or false).
*/
public static function get_admin_engine() {
$admin_engine = array_filter( self::_get_engines_settings(), function( $engine ) {
return ! empty( $engine['settings']['adminengine'] );
} );
if ( empty( $admin_engine ) ) {
$admin_engine = false;
} else {
reset( $admin_engine );
$admin_engine = key( $admin_engine );
}
return $admin_engine;
}
/**
* Getter for single Engine settings.
*
* @since 4.0
*
* @param string|null $name Engine name.
*
* @return mixed|false
*/
public static function get_engine_settings( ?string $name ) {
if ( empty( $name ) ) {
return false;
}
$engines = self::_get_engines_settings();
return array_key_exists( $name, $engines ) ? $engines[ $name ] : false;
}
/**
* Getter for saved Engines settings stored in the database.
*
* @since 4.0
*
* @param bool $skip_cache Skip caching.
*
* @return array Raw Engine settings.
*/
public static function _get_engines_settings( $skip_cache = false ) {
if ( ! $skip_cache ) {
$cache = wp_cache_get( self::$engines_cache_key, '' );
}
if ( empty( $cache ) || $skip_cache ) {
$engines_settings = get_option( SEARCHWP_PREFIX . 'engines' );
} else {
$engines_settings = $cache;
}
if ( ! $skip_cache ) {
wp_cache_set( self::$engines_cache_key, $engines_settings, '', 1 );
}
return is_array( $engines_settings ) ? $engines_settings : [];
}
/**
* Setter for Engines configs.
*
* @since 4.0
* @return void
*/
public static function update_engines_config( $config ) {
update_option( SEARCHWP_PREFIX . 'engines', $config, 'no' );
wp_cache_delete( self::$engines_cache_key );
}
}