PluginProbe
Search Analytics for WP / trunk
Search Analytics for WP vtrunk
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.1.1 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 37 releases
search-analytics / includes / class.process-query.php

class.process-query.php in Search Analytics for WP trunk, at includes/class.process-query.php

281 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( "ABSPATH" ) || exit;
3
4 if ( ! class_exists( 'MWTSA_Process_Query' ) ) {
5
6 class MWTSA_Process_Query {
7
8 public static function process_wpforo_search_term_action( $args, $items_count, $posts, $sql ) {
9 $process = new self();
10
11 if ( apply_filters( 'mwtsa_wpforo_do_not_save_search', false, $args['needle'] ) ) {
12 return;
13 }
14
15 $process->process_search_term( $args['needle'], $items_count );
16 }
17
18 public static function process_rest_api_search_term_action() {
19 $process = new self();
20
21 $custom_search_value = $process->get_custom_search_value();
22
23 if ( apply_filters( 'mwtsa_rest_api_do_not_save_search', $custom_search_value === '', $custom_search_value ) ) {
24 return;
25 }
26
27 $result_count = apply_filters( 'mwtsa_rest_api_result_count', 0, $custom_search_value );
28
29 if ( $result_count === 0 ) {
30 $args = array(
31 'posts_per_page' => - 1,
32 'post_status' => 'publish',
33 'post_type' => 'any',
34 'offset' => 0,
35 'fields' => 'ids',
36 's' => $custom_search_value,
37 );
38
39 $posts = get_posts( apply_filters( 'mwtsa_rest_api_posts_count_query_args', $args ) );
40
41 if ( $posts ) {
42 $result_count = count( $posts );
43 }
44 }
45
46 $process->process_search_term( $custom_search_value, $result_count );
47 }
48
49 public static function process_search_term_action() {
50 global $wp_query;
51
52 $process = new self();
53
54 $custom_search_value = $process->get_custom_search_value();
55
56 if ( apply_filters( 'mwtsa_do_not_save_search', ( ! is_search() && $custom_search_value == '' ) || is_admin(), $custom_search_value ) ) {
57 return;
58 }
59
60 $search_term = $custom_search_value != '' ? $custom_search_value : get_search_query();
61
62 $process->process_search_term( $search_term, apply_filters( 'mwtsa_result_count', $wp_query->found_posts, $search_term ) );
63 }
64
65 public function get_custom_search_value() {
66 $exclude_custom_search_params = MWTSA_Options::get_option( 'mwtsa_custom_search_url_params' );
67
68 if ( empty( $exclude_custom_search_params ) ) {
69 return '';
70 }
71
72 $custom_search_params = array_map( 'trim', explode( ',', $exclude_custom_search_params ) );
73 foreach ( $custom_search_params as $param ) {
74 if ( ! empty( $_REQUEST[ $param ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
75 return sanitize_text_field( wp_unslash( $_REQUEST[ $param ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
76 }
77 }
78
79 return '';
80 }
81
82 public function process_search_term( $search_term, $count ) {
83
84 $exclude_search_for_roles = MWTSA_Options::get_option( 'mwtsa_exclude_search_for_role' );
85 $current_user_roles = mwtsa_get_current_user_roles();
86
87 $exclude_search_for_roles_after_logout = MWTSA_Options::get_option( 'mwtsa_exclude_search_for_role_after_logout' );
88
89 if ( ! empty( $exclude_search_for_roles_after_logout ) ) {
90 $current_user_cookie = MWTSA_Cookies::get_cookie_value();
91
92 if ( isset( $current_user_cookie['is_excluded'] ) && $current_user_cookie['is_excluded'] == 1 ) {
93 return false;
94 }
95 }
96
97 if ( is_array( $exclude_search_for_roles ) ) {
98 $matching_roles = array_intersect( $exclude_search_for_roles, $current_user_roles );
99
100 if ( count( $matching_roles ) > 0 ) {
101 return false;
102 }
103 }
104
105 $exclude_search_for_ips = MWTSA_Options::get_option( 'mwtsa_exclude_searches_from_ip_addresses' );
106
107 $client_ip = mwtsa_get_current_user_ip();
108
109 if ( ! empty( $exclude_search_for_ips ) ) {
110 $ips_list = array();
111 $excluded_ips = explode( ',', $exclude_search_for_ips );
112
113 foreach ( $excluded_ips as $ip ) {
114 $ip = trim( $ip );
115
116 if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) {
117 $ips_list[] = $ip;
118 }
119 }
120
121 if ( in_array( $client_ip, $ips_list ) ) {
122 return false;
123 }
124 }
125
126 $exclude_if_contains = MWTSA_Options::get_option( 'mwtsa_exclude_if_string_contains' );
127
128 if ( ! empty( $exclude_if_contains ) ) {
129 $match_against = array_map( function ($excluded_string) {
130 $excluded_string = trim( $excluded_string );
131
132 return preg_quote( $excluded_string, '/' );
133 }, explode( ',', $exclude_if_contains ) );
134
135 preg_match( '/(' . implode( '|', $match_against ) . ')/i', $search_term, $matches );
136
137 if ( count( $matches ) > 0 ) {
138 return false;
139 }
140 }
141
142 if ( apply_filters( 'mwtsa_extra_exclude_conditions', false, $search_term ) ) {
143 return false;
144 }
145
146 $country = '';
147
148 if ( ! empty( MWTSA_Options::get_option( 'mwtsa_save_search_country' ) ) ) {
149 //http://ip-api.com/json/24.48.0?fields=49154
150 // IP-API integration according to the documentation at http://ip-api.com/docs/api:json
151 // non-pro works with http only!
152 $request = wp_remote_get( 'http://ip-api.com/json/' . $client_ip . '?fields=49155' );
153 $ip_details_get = wp_remote_retrieve_body( $request );
154 if ( ! empty( $ip_details_get ) ) {
155 $ip_details = json_decode( $ip_details_get );
156
157 if ( $ip_details && ! empty( $ip_details->status ) && $ip_details->status !== 'fail' ) {
158 $country = strtolower( $ip_details->countryCode );
159 }
160 }
161 }
162
163 $user_id = 0;
164
165 if ( ! empty( MWTSA_Options::get_option( 'mwtsa_save_search_by_user' ) ) && is_user_logged_in() ) {
166 $user = wp_get_current_user();
167 $user_id = $user->ID;
168 }
169
170 if ( ! empty( $search_term ) ) {
171
172 $minimum_length_term = MWTSA_Options::get_option( 'mwtsa_minimum_characters' );
173
174 if ( ! empty( $minimum_length_term ) && strlen( $search_term ) < $minimum_length_term ) {
175 return false;
176 }
177
178 if ( apply_filters( 'mwtsa_exclude_term', false, $search_term ) ) {
179 return false;
180 }
181
182 $this->save_search_term( $search_term, $count, $country, $user_id );
183 }
184
185 return true;
186 }
187
188 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- MWTSAI()->terms_table_name and MWTSAI()->history_table_name are hardcoded.
189 public function save_search_term( $term, $found_posts, $country = '', $user_id = 0 ) {
190 global $wpdb;
191
192 //make sure db is up to date
193 // TODO: move this away
194 MWTSA_Install::activate_single_site();
195
196 $instance = MWTSAI();
197
198 //1. add/update term string
199 $existing_term = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
200 "SELECT *
201 FROM `$instance->terms_table_name`
202 WHERE term = %s
203 LIMIT 1
204 ", $term
205 ) );
206
207 $exclude_doubled_search_for = MWTSA_Options::get_option( 'mwtsa_exclude_doubled_search_for_interval' );
208
209 $current_user_cookie = MWTSA_Cookies::get_cookie_value();
210
211 $term_id = null;
212
213 if ( empty ( $existing_term ) ) {
214 $success = $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
215 "INSERT INTO `$instance->terms_table_name` (`term`, `total_count`)
216 VALUES (%s, %d)",
217 sanitize_text_field( $term ),
218 1
219 ) );
220
221 if ( $success ) {
222 $term_id = $wpdb->insert_id;
223 }
224 } else {
225
226 if ( ! empty ( $exclude_doubled_search_for ) ) {
227 if ( isset( $current_user_cookie['search'] ) && isset( $current_user_cookie['search'][ $existing_term->id ] ) && ( $current_user_cookie['search'][ $existing_term->id ] + ( 60 * $exclude_doubled_search_for ) ) > time() ) {
228 return false;
229 }
230 }
231
232 $total_count = $existing_term->total_count + 1;
233
234 $success = $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
235 "UPDATE `$instance->terms_table_name`
236 SET total_count = %d
237 WHERE term = %s
238 LIMIT 1
239 ", $total_count, $term
240 ) );
241
242 if ( $success ) {
243 $term_id = $existing_term->id;
244 }
245 }
246
247 do_action( 'mwtsa_after_term_save', $term, $term_id, ! empty( $existing_term ) );
248
249 //2. add term timestamp + posts_count - ON term_id
250 if ( ! empty( $term_id ) ) {
251
252 $history_term_id = null;
253
254 if ( ! empty ( $exclude_doubled_search_for ) ) {
255 $current_user_cookie['search'][ $term_id ] = time();
256 MWTSA_Cookies::set_cookie_value( $current_user_cookie, ( 86400 * 7 ) );
257 }
258
259 $success = $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
260 "INSERT INTO `$instance->history_table_name` (`term_id`, `datetime`, `count_posts`, `country`, `user_id`)
261 VALUES (%d, UTC_TIMESTAMP(), %d, %s, %d)",
262 $term_id,
263 $found_posts,
264 $country,
265 $user_id
266 ) );
267
268 if ( $success ) {
269 $history_term_id = $wpdb->insert_id;
270 }
271
272 do_action( 'mwtsa_after_history_term_save', $history_term_id, $term_id, $found_posts, $country, $user_id );
273
274 wp_cache_set( 'last_changed', microtime(), 'mwtsa' );
275 }
276
277 return $success;
278 }
279 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter
280 }
281 }