-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-daves-wordpress-live-search.php
460 lines (369 loc) · 17.2 KB
/
class-daves-wordpress-live-search.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'Cannot access files directly.' );
}
class DavesWordPressLiveSearch {
///////////////////
// Initialization
///////////////////
/**
* Initialize the live search object & enqueuing scripts
*
* @return void
*/
public static function advanced_search_init() {
load_plugin_textdomain( 'daves-wordpress-live-search', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
if ( self::isSearchablePage() ) {
wp_enqueue_script( 'underscore' );
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
wp_enqueue_script( 'daves-wordpress-live-search', plugin_dir_url( __FILE__ ) . 'js/daves-wordpress-live-search.js', array(
'jquery',
'underscore'
), false, true );
wp_enqueue_script( 'excanvas', plugin_dir_url( __FILE__ ) . 'js/excanvas.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'spinners', plugin_dir_url( __FILE__ ) . 'js/spinners.js', array( 'excanvas' ), false, true );
} else {
wp_enqueue_script( 'daves-wordpress-live-search', plugin_dir_url( __FILE__ ) . 'js/daves-wordpress-live-search.min.js', array(
'jquery',
'underscore'
), false, true );
wp_enqueue_script( 'excanvas', plugin_dir_url( __FILE__ ) . 'js/excanvas.compiled.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'spinners', plugin_dir_url( __FILE__ ) . 'js/spinners.min.js', array( 'excanvas' ), false, true );
}
self::inlineSettings();
}
}
public static function head() {
$cssOption = get_option( 'daves-wordpress-live-search_css_option' );
$themeDir = get_bloginfo( "stylesheet_directory" );
if ( is_admin() ) {
$style = plugin_dir_url( __FILE__ ) . 'css/daves-wordpress-live-search_custom.css';
if ( $style ) {
wp_register_style( 'daves-wordpress-live-search', $style );
wp_enqueue_style( 'daves-wordpress-live-search' );
}
} elseif ( self::isSearchablePage() ) {
switch ( $cssOption ) {
case 'theme':
$style = $themeDir . '/daves-wordpress-live-search.css';
break;
case 'default_red':
case 'default_blue':
case 'default_gray':
case 'custom':
$style = plugin_dir_url( __FILE__ ) . 'css/daves-wordpress-live-search_' . $cssOption . '.css';
break;
case 'notheme':
default:
$style = false;
}
if ( $style ) {
wp_register_style( 'daves-wordpress-live-search', $style );
wp_enqueue_style( 'daves-wordpress-live-search' );
wp_print_styles();
}
if ( $cssOption === 'custom' && ! is_admin() ) {
$customOptions = get_option( 'daves-wordpress-live-search_custom_options' );
// Default width if none was provided
if ( ! isset( $customOptions['width'] ) || empty( $customOptions['width'] ) ) {
$customOptions['width'] = 250;
}
$styleTag = <<<STYLE
ul.dwls_search_results {
width: {$customOptions['width']}px;
}
ul.dwls_search_results li {
color: {$customOptions['fg']};
background-color: {$customOptions['bg']};
}
.search_footer {
background-color: {$customOptions['footbg']};
}
.search_footer a,
.search_footer a:visited {
color: {$customOptions['footfg']};
}
ul.dwls_search_results li a, ul.dwls_search_results li a:visited {
color: {$customOptions['title']};
}
ul.dwls_search_results li:hover
{
background-color: {$customOptions['hoverbg']};
}
ul.dwls_search_results li {
border-bottom: 1px solid {$customOptions['divider']};
}
STYLE;
if ( ! empty( $customOptions['shadow'] ) ) {
$styleTag .= <<<STYLE
ul.dwls_search_results {
-moz-box-shadow: 5px 5px 3px #222;
-webkit-box-shadow: 5px 5px 3px #222;
box-shadow: 5px 5px 3px #222;
}
STYLE;
}
echo '<style type="text/css">' . $styleTag . '</style>';
}
}
}
/**
* Working around the fact either the $_POST parameters are unreliable (may be true or "true")
* or I just can't keep them straight. Probably the latter, but best to be safe.
*
* @param mixed $value value to be tested for truthiness
*
* @return boolean truthiness
*/
private static function isTruthy( $value ) {
if ( true === $value ) {
return true;
} // Check for boolean true
if ( is_numeric( $value ) && 0 !== $value ) {
return true;
} // Check for nonzero
if ( 'true' === $value ) {
return true;
} // Check for the word 'true'
return false;
}
private static function inlineSettings() {
$resultsDirection = stripslashes( get_option( 'daves-wordpress-live-search_results_direction' ) );
$showThumbs = ( true === self::isTruthy( get_option( 'daves-wordpress-live-search_thumbs' ) ) );
$showExcerpt = ( true === self::isTruthy( get_option( 'daves-wordpress-live-search_excerpt' ) ) );
$showMoreResultsLink = ( true === self::isTruthy( get_option( 'daves-wordpress-live-search_more_results', true ) ) );
$minCharsToSearch = intval( get_option( 'daves-wordpress-live-search_minchars' ) );
$xOffset = intval( get_option( 'daves-wordpress-live-search_xoffset' ) );
$yOffset = intval( get_option( 'daves-wordpress-live-search_yoffset' ) );
$resultTemplate = apply_filters( 'dwls_alter_result_template', file_get_contents( dirname( __FILE__ ) . "/js/dwls-results.tpl" ) );
// Translations
$moreResultsText = __( 'View more results', 'daves-wordpress-live-search' );
$outdatedJQueryText = __( "Dave's WordPress Live Search requires jQuery 1.2.6 or higher. WordPress ships with current jQuery versions. But if you are seeing this message, it's likely that another plugin is including an earlier version.", 'daves-wordpress-live-search' );
// Neat trick: use wp_localize_script to generate the config object
// "This way, you won’t have to use PHP to print out JavaScript code,
// which is both ugly and non-cacheable."
// @see http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global
wp_localize_script( 'daves-wordpress-live-search', 'DavesWordPressLiveSearchConfig', array(
'resultsDirection' => $resultsDirection,
'showThumbs' => ( $showThumbs ) ? 'true' : 'false',
'showExcerpt' => ( $showExcerpt == 1 ) ? 'true' : 'false',
'showMoreResultsLink' => ( $showMoreResultsLink ) ? 'true' : 'false',
'minCharsToSearch' => $minCharsToSearch,
'xOffset' => $xOffset,
'yOffset' => $yOffset,
'blogURL' => get_bloginfo( 'url' ),
'ajaxURL' => admin_url( 'admin-ajax.php' ),
'viewMoreText' => $moreResultsText,
'outdatedJQuery' => $outdatedJQueryText,
'resultTemplate' => $resultTemplate,
) );
}
///////////////
// Admin Pages
///////////////
/**
* Include the Live Search options page in the admin menu
*
* @return void
*/
public static function admin_menu() {
add_options_page( "Dave's WordPress Live Search Options", __( 'Live Search', 'daves-wordpress-live-search' ), 'manage_options', __FILE__, array(
'DavesWordPressLiveSearch',
'plugin_options'
) );
}
public static function admin_enqueue_scripts() {
global $wp_version;
// Color picker was introduced in WP 3.5
if ( floatval( $wp_version ) >= 3.5 ) {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'my-script-handle', plugins_url( 'admin/color_picker.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}
}
/**
* Display & process the Live Search admin options
*
* @return void
*/
public static function plugin_options() {
$tab = isset( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : '';
switch ( $tab ) {
case 'advanced':
return self::plugin_options_advanced();
case 'appearance':
return self::plugin_options_design();
case 'settings':
default:
return self::plugin_options_settings();
}
}
private static function plugin_options_settings() {
$thisPluginsDirectory = dirname( __FILE__ );
if ( array_key_exists( 'daves-wordpress-live-search_submit', $_POST ) && current_user_can( 'manage_options' ) ) {
check_admin_referer( 'daves-wordpress-live-search-config' );
// Read their posted value
$maxResults = max( intval( $_POST['daves-wordpress-live-search_max_results'] ), 0 );
$resultsDirection = $_POST['daves-wordpress-live-search_results_direction'];
$minCharsToSearch = intval( $_POST['daves-wordpress-live-search_minchars'] );
$searchSource = intval( $_POST['daves-wordpress-live-search_source'] );
// Save the posted value in the database
update_option( 'daves-wordpress-live-search_max_results', $maxResults );
update_option( 'daves-wordpress-live-search_results_direction', $resultsDirection );
update_option( 'daves-wordpress-live-search_minchars', $minCharsToSearch );
update_option( 'daves-wordpress-live-search_source', $searchSource );
$updateMessage = __( 'Options saved.', 'daves-wordpress-live-search' );
echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>";
} else {
$maxResults = intval( get_option( 'daves-wordpress-live-search_max_results' ) );
$resultsDirection = stripslashes( get_option( 'daves-wordpress-live-search_results_direction' ) );
$minCharsToSearch = intval( get_option( 'daves-wordpress-live-search_minchars' ) );
$searchSource = intval( get_option( 'daves-wordpress-live-search_source' ) );
}
$wp_pre_4_3 = version_compare( $GLOBALS['wp_version'], '4.3', 'lt' );
include "$thisPluginsDirectory/admin/daves-wordpress-live-search-admin.tpl.php";
}
private static function plugin_options_design() {
$thisPluginsDirectory = dirname( __FILE__ );
if ( array_key_exists( 'daves-wordpress-live-search_submit', $_POST ) && current_user_can( 'manage_options' ) ) {
check_admin_referer( 'daves-wordpress-live-search-config' );
// Read their posted value
$displayPostMeta = ( 'true' === $_POST['daves-wordpress-live-search_display_post_meta'] );
$showThumbs = ( 'true' === $_POST['daves-wordpress-live-search_thumbs'] );
$showExcerpt = ( 'true' === $_POST['daves-wordpress-live-search_excerpt'] );
$excerptLength = $_POST['daves-wordpress-live-search_excerpt_length'];
$showMoreResultsLink = ( 'true' === $_POST['daves-wordpress-live-search_more_results'] );
$cssOption = $_POST['daves-wordpress-live-search_css'];
$customOptions = $_POST['daves-wordpress-live-search_custom'];
// Force the width to be something we can work with
$customOptions['width'] = max( abs( intval( $customOptions['width'] ) ), 1 );
// Save the posted value in the database
update_option( 'daves-wordpress-live-search_display_post_meta', (string) $displayPostMeta );
update_option( 'daves-wordpress-live-search_thumbs', $showThumbs );
update_option( 'daves-wordpress-live-search_excerpt', $showExcerpt );
update_option( 'daves-wordpress-live-search_excerpt_length', $excerptLength );
update_option( 'daves-wordpress-live-search_more_results', $showMoreResultsLink );
update_option( 'daves-wordpress-live-search_css_option', $cssOption );
update_option( 'daves-wordpress-live-search_custom_options', $customOptions );
// Translate the "Options saved" message...just in case.
// You know...the code I was copying for this does it, thought it might be a good idea to leave it
$updateMessage = __( 'Options saved.', 'daves-wordpress-live-search' );
echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>";
} else {
$displayPostMeta = (bool) get_option( 'daves-wordpress-live-search_display_post_meta' );
$showThumbs = (bool) get_option( 'daves-wordpress-live-search_thumbs' );
$showExcerpt = (bool) get_option( 'daves-wordpress-live-search_excerpt' );
$excerptLength = intval( get_option( 'daves-wordpress-live-search_excerpt_length' ) );
$showMoreResultsLink = (bool) get_option( 'daves-wordpress-live-search_more_results' );
$cssOption = get_option( 'daves-wordpress-live-search_css_option' );
$customOptions = get_option( 'daves-wordpress-live-search_custom_options' );
}
$wp_pre_4_3 = version_compare( $GLOBALS['wp_version'], '4.3', 'lt' );
include "$thisPluginsDirectory/admin/daves-wordpress-live-search-admin-appearance.tpl.php";
}
private static function plugin_options_advanced() {
$thisPluginsDirectory = dirname( __FILE__ );
if ( array_key_exists( 'daves-wordpress-live-search_submit', $_POST ) && current_user_can( 'manage_options' ) ) {
check_admin_referer( 'daves-wordpress-live-search-config' );
// Read their posted value
$xOffset = intval( $_POST['daves-wordpress-live-search_xoffset'] );
$yOffset = intval( $_POST['daves-wordpress-live-search_yoffset'] );
$exceptions = $_POST['daves-wordpress-live-search_exceptions'];
$applyContentFilter = ( isset( $_POST['daves-wordpress-live-search_apply_content_filter'] ) && "true" == $_POST['daves-wordpress-live-search_apply_content_filter'] );
update_option( 'daves-wordpress-live-search_exceptions', $exceptions );
update_option( 'daves-wordpress-live-search_xoffset', intval( $xOffset ) );
update_option( 'daves-wordpress-live-search_yoffset', intval( $yOffset ) );
update_option( 'daves-wordpress-live-search_apply_content_filter', $applyContentFilter );
// Translate the "Options saved" message...just in case.
// You know...the code I was copying for this does it, thought it might be a good idea to leave it
$updateMessage = __( 'Options saved.', 'daves-wordpress-live-search' );
echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>";
} else {
$exceptions = get_option( 'daves-wordpress-live-search_exceptions' );
$xOffset = intval( get_option( 'daves-wordpress-live-search_xoffset' ) );
$yOffset = intval( get_option( 'daves-wordpress-live-search_yoffset' ) );
$applyContentFilter = (bool) get_option( 'daves-wordpress-live-search_apply_content_filter' );
}
$wp_pre_4_3 = version_compare( $GLOBALS['wp_version'], '4.3', 'lt' );
include "$thisPluginsDirectory/admin/daves-wordpress-live-search-admin-advanced.tpl.php";
}
public static function admin_notices() {
$cssOption = get_option( 'daves-wordpress-live-search_css_option' );
if ( 'theme' == $cssOption ) {
$themeDir = get_theme_root() . '/' . get_stylesheet();
// Make sure there's a daves-wordpress-live-search.css file in the theme
if ( ! file_exists( $themeDir . "/daves-wordpress-live-search.css" ) ) {
$alertMessage = sprintf( __( "The %sDave's WordPress Live Search%s plugin is configured to use a theme-specific CSS file, but the current theme does not contain a daves-wordpress-live-search.css file." ), '<em>', '</em>' );
echo "<div class=\"updated fade\"><p><strong>$alertMessage</strong></p></div>";
}
}
}
/**
* Check if the current page is the login or registration page
*
* @return bool
*/
private static function is_login_page() {
return in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) );
}
private static function isSearchablePage() {
if ( is_admin() || self::is_login_page() ) {
return false;
}
$searchable = true;
$exceptions = explode( "\n", get_option( 'daves-wordpress-live-search_exceptions' ) );
foreach ( $exceptions as $exception ) {
$regexp = trim( $exception );
// Blank paths were slipping through. Ignore them.
if ( empty( $regexp ) ) {
continue;
}
if ( '<front>' == $regexp ) {
$regexp = '';
}
$regexp = str_replace( '?', '[?]', $regexp );
$regexp = str_replace( '|', '[|]', $regexp );
// These checks can probably be turned into regexps themselves,
// but it's too early in the morning to be writing regexps
if ( '*' == substr( $regexp, 0, 1 ) ) {
$regexp = substr( $regexp, 1 );
} else {
$regexp = '^' . $regexp;
}
if ( '*' == substr( $regexp, - 1 ) ) {
$regexp = substr( $regexp, 0, - 1 );
} else {
$regexp = $regexp . '$';
}
$regexp = '|' . $regexp . '|';
if ( preg_match( $regexp, substr( $_SERVER['REQUEST_URI'], 1 ) ) > 0 ) {
return false;
}
}
// Fall-through, search everything by default
return true;
}
/**
* Set some decent defaults
*/
public static function activate() {
add_option( 'daves-wordpress-live-search_max_results', 10 );
add_option( 'daves-wordpress-live-search_results_direction', 'down' );
add_option( 'daves-wordpress-live-search_display_post_meta', 'true' );
add_option( 'daves-wordpress-live-search_css_option', 'default_gray' );
add_option( 'daves-wordpress-live-search_thumbs', 'true' );
add_option( 'daves-wordpress-live-search_excerpt', 'true' );
add_option( 'daves-wordpress-live-search_excerpt_length', 100 );
add_option( 'daves-wordpress-live-search_more_results', 'true' );
add_option( 'daves-wordpress-live-search_minchars', 3 );
add_option( 'daves-wordpress-live-search_source', DavesWordPressLiveSearchResults::SEARCH_CONTENT );
add_option( 'daves-wordpress-live-search_custom_options', array(
'fg' => '#000',
'bg' => '#ddd',
'hoverbg' => '#fff',
'title' => '#000',
'footbg' => '#888',
'footfg' => '#fff',
'width' => '250',
) );
}
}