-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.php
More file actions
314 lines (289 loc) · 13 KB
/
search.php
File metadata and controls
314 lines (289 loc) · 13 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
<?php
/**
* CloudScale Analytics: Search Widget
*
* Replaces Relevanssi on the frontend with a LIKE '%term%' wildcard search.
* Renders a search form widget (Appearance > Widgets > "CloudScale Analytics: Search").
* Results are capped at 50 posts, ordered newest-first.
* Thumbnails are forced to 'thumbnail' size on search-result pages.
*
* @package CloudScale_Free_Analytics
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// -------------------------------------------------------------------------
// 1. Register widget
// -------------------------------------------------------------------------
add_action( 'widgets_init', function () {
register_widget( 'CSPV_Search_Widget' );
} );
// Enqueue widget CSS only on pages where the widget is active.
add_action( 'wp_enqueue_scripts', 'cspv_search_widget_enqueue' );
/**
* Enqueue inline CSS for the Search widget.
*
* Only fires when the widget is active to avoid injecting styles on every page.
*
* @since 2.9.94
* @return void
*/
function cspv_search_widget_enqueue() {
if ( ! is_active_widget( false, false, 'cspv_search_widget' ) ) {
return;
}
wp_register_style( 'cspv-search-widget', false, array(), CSPV_VERSION ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- virtual handle
wp_enqueue_style( 'cspv-search-widget' );
wp_add_inline_style( 'cspv-search-widget', cspv_search_widget_css() );
}
// -------------------------------------------------------------------------
// 2. Widget class
// -------------------------------------------------------------------------
/**
* Search widget that replaces Relevanssi with a LIKE-based wildcard search.
*
* @since 2.9.94 */
class CSPV_Search_Widget extends WP_Widget {
/**
* Register the widget with WordPress.
*
* @since 2.9.94 */
public function __construct() {
parent::__construct(
'cspv_search_widget',
__( 'CloudScale Analytics: Search', 'cloudscale-wordpress-free-analytics' ),
array(
'description' => __( 'Wildcard search form. Finds posts by LIKE match on title, content and excerpt — short terms like "AI" work correctly. Results capped at 50.', 'cloudscale-wordpress-free-analytics' ),
'show_instance_in_rest' => true,
)
);
}
/**
* Output the widget HTML on the frontend.
*
* @since 2.9.94
* @param array $args Widget display arguments (before/after widget/title).
* @param array $instance Saved widget settings.
* @return void
*/
public function widget( $args, $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Search', 'cloudscale-wordpress-free-analytics' );
$placeholder = ! empty( $instance['placeholder'] ) ? $instance['placeholder'] : __( 'Search posts…', 'cloudscale-wordpress-free-analytics' );
$btn_color = ! empty( $instance['btn_color'] ) ? sanitize_hex_color( $instance['btn_color'] ) : '#e8491d';
$btn_hover = ! empty( $instance['btn_hover'] ) ? sanitize_hex_color( $instance['btn_hover'] ) : '#f27c1a';
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- standard WP widget output
echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- standard WP widget output
?>
<form role="search" method="get" class="cspv-search-form"
style="--cspv-btn-color:<?php echo esc_attr( $btn_color ); ?>;--cspv-btn-hover:<?php echo esc_attr( $btn_hover ); ?>;"
action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label class="screen-reader-text" for="cspv-search-<?php echo esc_attr( $args['widget_id'] ?? '' ); ?>">
<?php echo esc_html( $title ); ?>
</label>
<input type="search"
id="cspv-search-<?php echo esc_attr( $args['widget_id'] ?? '' ); ?>"
class="cspv-search-input"
name="s"
placeholder="<?php echo esc_attr( $placeholder ); ?>"
value="<?php echo esc_attr( get_search_query() ); ?>">
<button type="submit" class="cspv-search-btn">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" width="16" height="16" aria-hidden="true">
<path fill-rule="evenodd" d="M9 3a6 6 0 1 0 3.72 10.72l3.28 3.28a1 1 0 0 0 1.42-1.42l-3.28-3.28A6 6 0 0 0 9 3Zm-4 6a4 4 0 1 1 8 0 4 4 0 0 1-8 0Z" clip-rule="evenodd"/>
</svg>
<span class="screen-reader-text"><?php esc_html_e( 'Search', 'cloudscale-wordpress-free-analytics' ); ?></span>
</button>
</form>
<?php
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- standard WP widget output
}
/**
* Render the widget settings form in the Widgets admin screen.
*
* @since 2.9.94
* @param array $instance Current saved widget settings.
* @return void
*/
public function form( $instance ) {
$title = $instance['title'] ?? __( 'Search', 'cloudscale-wordpress-free-analytics' );
$placeholder = $instance['placeholder'] ?? __( 'Search posts…', 'cloudscale-wordpress-free-analytics' );
$btn_color = $instance['btn_color'] ?? '#e8491d';
$btn_hover = $instance['btn_hover'] ?? '#f27c1a';
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'cloudscale-wordpress-free-analytics' ); ?></label>
<input class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
type="text"
value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>"><?php esc_html_e( 'Input placeholder:', 'cloudscale-wordpress-free-analytics' ); ?></label>
<input class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'placeholder' ) ); ?>"
type="text"
value="<?php echo esc_attr( $placeholder ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'btn_color' ) ); ?>"><?php esc_html_e( 'Button colour:', 'cloudscale-wordpress-free-analytics' ); ?></label><br>
<input id="<?php echo esc_attr( $this->get_field_id( 'btn_color' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'btn_color' ) ); ?>"
type="color"
value="<?php echo esc_attr( $btn_color ); ?>"
style="width:50px;height:30px;padding:0;border:1px solid #ccc;border-radius:3px;cursor:pointer;vertical-align:middle;">
<code style="font-size:11px;color:#666;"><?php echo esc_html( $btn_color ); ?></code>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'btn_hover' ) ); ?>"><?php esc_html_e( 'Button hover colour:', 'cloudscale-wordpress-free-analytics' ); ?></label><br>
<input id="<?php echo esc_attr( $this->get_field_id( 'btn_hover' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'btn_hover' ) ); ?>"
type="color"
value="<?php echo esc_attr( $btn_hover ); ?>"
style="width:50px;height:30px;padding:0;border:1px solid #ccc;border-radius:3px;cursor:pointer;vertical-align:middle;">
<code style="font-size:11px;color:#666;"><?php echo esc_html( $btn_hover ); ?></code>
</p>
<?php
}
/**
* Sanitise and save widget settings submitted from the form.
*
* @since 2.9.94
* @param array $new_instance New settings submitted by the user.
* @param array $old_instance Previous saved settings.
* @return array Sanitised settings to persist.
*/
public function update( $new_instance, $old_instance ) {
return array(
'title' => sanitize_text_field( $new_instance['title'] ),
'placeholder' => sanitize_text_field( $new_instance['placeholder'] ),
'btn_color' => sanitize_hex_color( $new_instance['btn_color'] ?? '#e8491d' ) ?: '#e8491d',
'btn_hover' => sanitize_hex_color( $new_instance['btn_hover'] ?? '#f27c1a' ) ?: '#f27c1a',
);
}
}
// -------------------------------------------------------------------------
// 3. Widget CSS
// -------------------------------------------------------------------------
/**
* Return the inline CSS for the search widget form.
*
* @since 2.9.94
* @return string CSS string.
*/
function cspv_search_widget_css() {
return '
.cspv-search-form{display:flex;gap:0;width:100%;}
.cspv-search-input{flex:1;min-width:0;padding:8px 12px;font-size:14px;border:2px solid #ddd;border-right:none;border-radius:4px 0 0 4px;outline:none;transition:border-color .15s;}
.cspv-search-input:focus{border-color:var(--cspv-btn-color,#e8491d);}
.cspv-search-btn{display:inline-flex;align-items:center;justify-content:center;padding:0 14px;background:var(--cspv-btn-color,#e8491d);border:2px solid var(--cspv-btn-color,#e8491d);border-radius:0 4px 4px 0;color:#fff;cursor:pointer;transition:background .15s,border-color .15s;line-height:1;}
.cspv-search-btn:hover{background:var(--cspv-btn-hover,#f27c1a);border-color:var(--cspv-btn-hover,#f27c1a);}
';
}
// -------------------------------------------------------------------------
// 4. Wildcard search — bypasses Relevanssi entirely via posts_pre_query
// -------------------------------------------------------------------------
/**
* Run our own LIKE SQL and return results directly, before WordPress (and
* Relevanssi) execute their queries. Priority 999 ensures we run last so
* we always win regardless of what priority Relevanssi registered at.
*
* Ranking: title matches first, then content/excerpt matches, newest-first
* within each group. Results capped at 50.
*
* @since 2.9.94
* @param array|null $posts Existing pre-query result (null = not yet handled).
* @param WP_Query $query
* @return array|null
*/
add_filter( 'posts_pre_query', 'cspv_wildcard_posts_pre_query', 999, 2 );
function cspv_wildcard_posts_pre_query( $posts, $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
return $posts;
}
$term = trim( $query->get( 's' ) );
if ( $term === '' ) {
return $posts;
}
// Remove Relevanssi from the_posts at every priority it is known to use.
remove_filter( 'the_posts', 'relevanssi_filter_the_posts', 10 );
remove_filter( 'the_posts', 'relevanssi_filter_the_posts', 99 );
global $wpdb;
$esc = $wpdb->esc_like( $term );
$like = '%' . $esc . '%';
// Tier-0 patterns: exact-case term as a standalone word in the title.
// Covers space-bounded, start/end of title, and common punctuation after term.
$t0_word = '% ' . $esc . ' %';
$t0_start = $esc . ' %';
$t0_end = '% ' . $esc;
$t0_colon = '% ' . $esc . ':%';
$t0_dot = '% ' . $esc . '.%';
$t0_comma = '% ' . $esc . ',%';
$t0_exact = $esc;
// Standard WP pattern: get_results( prepare( ... ) ) — prepare() returns safe SQL string, get_results() executes it.
$results = $wpdb->get_results( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- trusted internal table name/expression
$wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $wpdb->posts is a trusted core table name
"SELECT {$wpdb->posts}.*
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_type NOT IN (
'revision','nav_menu_item','attachment','custom_css',
'customize_changeset','oembed_cache','user_request',
'wp_block','wp_template','wp_template_part',
'wp_global_styles','wp_navigation'
)
AND (
{$wpdb->posts}.post_title LIKE %s
OR {$wpdb->posts}.post_content LIKE %s
OR {$wpdb->posts}.post_excerpt LIKE %s
)
ORDER BY
CASE
-- Tier 0: exact-case term as a standalone word in the title
WHEN ( BINARY {$wpdb->posts}.post_title LIKE %s
OR BINARY {$wpdb->posts}.post_title LIKE %s
OR BINARY {$wpdb->posts}.post_title LIKE %s
OR BINARY {$wpdb->posts}.post_title LIKE %s
OR BINARY {$wpdb->posts}.post_title LIKE %s
OR BINARY {$wpdb->posts}.post_title LIKE %s
OR BINARY {$wpdb->posts}.post_title = %s
) THEN 0
-- Tier 1: exact-case term anywhere in the title
WHEN BINARY {$wpdb->posts}.post_title LIKE %s THEN 1
-- Tier 2: case-insensitive match in title (e.g. 'ai' inside 'waiting')
WHEN {$wpdb->posts}.post_title LIKE %s THEN 2
-- Tier 3: match is only in content/excerpt, not in title
ELSE 3
END,
{$wpdb->posts}.post_date DESC
LIMIT 50",
// WHERE (3)
$like, $like, $like,
// Tier 0 (7)
$t0_word, $t0_start, $t0_end, $t0_colon, $t0_dot, $t0_comma, $t0_exact,
// Tier 1 (1)
$like,
// Tier 2 (1)
$like
)
);
$results = is_array( $results ) ? $results : array();
$query->found_posts = count( $results );
$query->max_num_pages = 1;
return $results;
}
/**
* Force 'thumbnail' image size on search-result pages so full-size
* infographics are not rendered in the results list.
*
* @since 2.9.94
* @param string|int[] $size
* @return string|int[]
*/
add_filter( 'post_thumbnail_size', 'cspv_search_thumbnail_size' );
function cspv_search_thumbnail_size( $size ) {
if ( is_search() ) {
return 'thumbnail';
}
return $size;
}