-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathclass-wp-job-manager-ajax.php
More file actions
464 lines (405 loc) · 15.7 KB
/
Copy pathclass-wp-job-manager-ajax.php
File metadata and controls
464 lines (405 loc) · 15.7 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
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
461
462
463
464
<?php
/**
* File containing the class WP_Job_Manager_Ajax.
*
* @package wp-job-manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles Job Manager's Ajax endpoints.
*
* @since 1.0.0
*/
class WP_Job_Manager_Ajax {
/**
* The single instance of the class.
*
* @var self
* @since 1.26.0
*/
private static $instance = null;
/**
* Allows for accessing single instance of class. Class should only be constructed once per call.
*
* @since 1.26.0
* @static
* @return self Main instance.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', [ __CLASS__, 'add_endpoint' ] );
add_action( 'template_redirect', [ __CLASS__, 'do_jm_ajax' ], 0 );
// JM Ajax endpoints.
add_action( 'job_manager_ajax_get_listings', [ $this, 'get_listings' ] );
add_action( 'job_manager_ajax_upload_file', [ $this, 'upload_file' ] );
// BW compatible handlers.
add_action( 'wp_ajax_nopriv_job_manager_get_listings', [ $this, 'get_listings' ] );
add_action( 'wp_ajax_job_manager_get_listings', [ $this, 'get_listings' ] );
add_action( 'wp_ajax_nopriv_job_manager_upload_file', [ $this, 'upload_file' ] );
add_action( 'wp_ajax_job_manager_upload_file', [ $this, 'upload_file' ] );
add_action( 'wp_ajax_job_manager_search_users', [ $this, 'ajax_search_users' ] );
}
/**
* Adds endpoint for frontend Ajax requests.
*/
public static function add_endpoint() {
add_rewrite_tag( '%jm-ajax%', '([^/]*)' );
add_rewrite_rule( 'jm-ajax/([^/]*)/?', 'index.php?jm-ajax=$matches[1]', 'top' );
add_rewrite_rule( 'index.php/jm-ajax/([^/]*)/?', 'index.php?jm-ajax=$matches[1]', 'top' );
}
/**
* Gets Job Manager's Ajax Endpoint.
*
* @param string $request Optional.
* @return string
*/
public static function get_endpoint( $request = '%%endpoint%%' ) {
if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
$endpoint = trailingslashit( home_url( '/index.php/jm-ajax/' . $request . '/', 'relative' ) );
} elseif ( get_option( 'permalink_structure' ) ) {
$endpoint = trailingslashit( home_url( '/jm-ajax/' . $request . '/', 'relative' ) );
} else {
$endpoint = add_query_arg( 'jm-ajax', $request, home_url( '/', 'relative' ) );
}
return esc_url_raw( $endpoint );
}
/**
* Performs Job Manager's Ajax actions.
*/
public static function do_jm_ajax() {
global $wp_query;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Input is used safely.
if ( ! empty( $_GET['jm-ajax'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Input is used safely.
$wp_query->set( 'jm-ajax', sanitize_text_field( wp_unslash( $_GET['jm-ajax'] ) ) );
}
$action = $wp_query->get( 'jm-ajax' );
if ( $action ) {
if ( ! defined( 'DOING_AJAX' ) ) {
define( 'DOING_AJAX', true );
}
// Not home - this is an ajax endpoint.
$wp_query->is_home = false;
/**
* Performs an Ajax action.
* The dynamic part of the action, $action, is the predefined Ajax action to be performed.
*
* @since 1.23.0
*/
do_action( 'job_manager_ajax_' . sanitize_text_field( $action ) );
wp_die();
}
}
/**
* Returns Job Listings for Ajax endpoint.
*/
public function get_listings() {
// Get input variables.
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Fetching data only; often for logged out visitors.
$search_location = isset( $_REQUEST['search_location'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['search_location'] ) ) : '';
$search_keywords = isset( $_REQUEST['search_keywords'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['search_keywords'] ) ) : '';
$search_categories = isset( $_REQUEST['search_categories'] ) ? wp_unslash( $_REQUEST['search_categories'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Input is sanitized below.
$filter_job_types = isset( $_REQUEST['filter_job_type'] ) ? array_filter( array_map( 'sanitize_title', wp_unslash( (array) $_REQUEST['filter_job_type'] ) ) ) : null;
$filter_post_status = isset( $_REQUEST['filter_post_status'] ) ? array_filter( array_map( 'sanitize_title', wp_unslash( (array) $_REQUEST['filter_post_status'] ) ) ) : null;
$order = isset( $_REQUEST['order'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) : 'DESC';
$orderby = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'featured';
$page = isset( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : 1;
$per_page = isset( $_REQUEST['per_page'] ) ? absint( $_REQUEST['per_page'] ) : absint( get_option( 'job_manager_per_page' ) );
$filled = isset( $_REQUEST['filled'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['filled'] ) ) : null;
$featured = isset( $_REQUEST['featured'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['featured'] ) ) : null;
$remote_position = isset( $_REQUEST['remote_position'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_position'] ) ) : null;
$show_pagination = isset( $_REQUEST['show_pagination'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['show_pagination'] ) ) : null;
$featured_first = isset( $_REQUEST['featured_first'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['featured_first'] ) ) : null;
$author = isset( $_REQUEST['author'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['author'] ) ) : '';
// phpcs:enable WordPress.Security.NonceVerification.Recommended
if ( is_array( $search_categories ) ) {
$search_categories = array_filter( array_map( 'sanitize_text_field', array_map( 'stripslashes', $search_categories ) ) );
} else {
$search_categories = array_filter( [ sanitize_text_field( wp_unslash( $search_categories ) ) ] );
}
// Ensure the current user can filter by post_status.
if ( is_array( $filter_post_status ) && ! current_user_can( \WP_Job_Manager_Post_Types::CAP_EDIT_LISTINGS ) ) {
$filter_post_status = null;
}
$types = get_job_listing_types();
$job_types_filtered = ! is_null( $filter_job_types ) && count( $types ) !== count( $filter_job_types );
$args = [
'search_location' => $search_location,
'search_keywords' => $search_keywords,
'search_categories' => $search_categories,
'job_types' => is_null( $filter_job_types ) || count( $types ) === count( $filter_job_types ) ? '' : $filter_job_types + [ 0 ],
'post_status' => $filter_post_status,
'orderby' => $orderby,
'order' => $order,
'featured_first' => $featured_first,
'author' => $author,
'offset' => ( $page - 1 ) * $per_page,
'posts_per_page' => max( 1, $per_page ), // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- Known slow query.
];
if ( 'true' === $filled || 'false' === $filled ) {
$args['filled'] = 'true' === $filled;
}
if ( 'true' === $remote_position || 'false' === $remote_position ) {
$args['remote_position'] = 'true' === $remote_position;
}
if ( 'true' === $featured || 'false' === $featured ) {
$args['featured'] = 'true' === $featured;
$args['orderby'] = 'featured' === $orderby ? 'date' : $orderby;
}
/**
* Get the arguments to use when building the Job Listing WP Query.
*
* @since 1.0.0
*
* @param array $args Arguments used for generating Job Listing query (see `get_job_listings()`).
*/
$jobs = get_job_listings( apply_filters( 'job_manager_get_listings_args', $args ) );
$result = [
'found_jobs' => $jobs->have_posts(),
'showing' => '',
'max_num_pages' => $jobs->max_num_pages,
];
if ( ( $search_location || $search_keywords || $search_categories || $job_types_filtered ) ) {
// translators: Placeholder %d is the number of found search results.
$message = sprintf( _n( 'Search completed. Found %d matching record.', 'Search completed. Found %d matching records.', $jobs->found_posts, 'wp-job-manager' ), $jobs->found_posts );
$result['showing_all'] = true;
} else {
$message = '';
}
$search_values = [
'location' => $search_location,
'keywords' => $search_keywords,
'categories' => $search_categories,
];
/**
* Filter the message that describes the results of the search query.
*
* @since 1.0.0
*
* @param string $message Default message that is generated when posts are found.
* @param array $search_values {
* Helpful values often used in the generation of this message.
*
* @type string $location Query used to filter by job listing location.
* @type string $keywords Query used to filter by general keywords.
* @type array $categories List of the categories to filter by.
* }
*/
$result['showing'] = apply_filters( 'job_manager_get_listings_custom_filter_text', $message, $search_values );
// Generate RSS link.
$result['showing_links'] = job_manager_get_filtered_links(
[
'filter_job_types' => $filter_job_types,
'search_location' => $search_location,
'search_categories' => $search_categories,
'search_keywords' => $search_keywords,
]
);
/**
* Send back a response to the AJAX request without creating HTML.
*
* @since 1.26.0
*
* @param array $result
* @param WP_Query $jobs
* @return bool True by default. Change to false to halt further response.
*/
if ( true !== apply_filters( 'job_manager_ajax_get_jobs_html_results', true, $result, $jobs ) ) {
/**
* Filters the results of the job listing Ajax query to be sent back to the client.
*
* @since 1.0.0
*
* @param array $result {
* Package of the query results along with meta information.
*
* @type bool $found_jobs Whether or not jobs were found in the query.
* @type string $showing Description of the search query and results.
* @type int $max_num_pages Number of pages in the search result.
* @type string $html HTML representation of the search results (only if filter
* `job_manager_ajax_get_jobs_html_results` returns true).
* @type array $pagination Pagination links to use for stepping through filter results.
* }
*/
wp_send_json( apply_filters( 'job_manager_get_listings_result', $result, $jobs ) );
return;
}
ob_start();
if ( $result['found_jobs'] ) {
while ( $jobs->have_posts() ) {
$jobs->the_post();
get_job_manager_template_part( 'content', \WP_Job_Manager_Post_Types::PT_LISTING );
}
} else {
get_job_manager_template_part( 'content', 'no-jobs-found' );
}
$result['html'] = ob_get_clean();
// Generate pagination.
if ( 'true' === $show_pagination ) {
$result['pagination'] = get_job_listing_pagination( $jobs->max_num_pages, $page );
}
/** This filter is documented in includes/class-wp-job-manager-ajax.php (above) */
wp_send_json( apply_filters( 'job_manager_get_listings_result', $result, $jobs ) );
}
/**
* Uploads file from an Ajax request.
*
* No nonce field since the form may be statically cached.
*/
public function upload_file() {
if ( ! job_manager_user_can_upload_file_via_ajax() ) {
wp_send_json_error( __( 'You must be logged in to upload files using this method.', 'wp-job-manager' ) );
return;
}
$data = [
'files' => [],
];
if ( ! empty( $_FILES ) ) {
foreach ( $_FILES as $file_key => $file ) {
$files_to_upload = job_manager_prepare_uploaded_files( $file );
foreach ( $files_to_upload as $file_to_upload ) {
$uploaded_file = job_manager_upload_file(
$file_to_upload,
[
'file_key' => $file_key,
]
);
if ( is_wp_error( $uploaded_file ) ) {
$data['files'][] = [
'error' => $uploaded_file->get_error_message(),
];
} else {
$data['files'][] = $uploaded_file;
}
}
}
}
wp_send_json( $data );
}
/**
* Checks if user can search for other users in ajax call.
*
* @return bool
*/
private static function user_can_search_users() {
$user_can_search_users = false;
/**
* Filter the capabilities that are allowed to search for users in ajax call.
*
* @since 1.32.0
*
* @param array $user_caps Array of capabilities/roles that are allowed to search for users.
*/
$allowed_capabilities = apply_filters( 'job_manager_caps_can_search_users', [ \WP_Job_Manager_Post_Types::CAP_EDIT_LISTINGS ] );
foreach ( $allowed_capabilities as $cap ) {
if ( current_user_can( $cap ) ) {
$user_can_search_users = true;
break;
}
}
/**
* Filters whether the current user can search for users in ajax call.
*
* @since 1.32.0
*
* @param bool $user_can_search_users True if they are allowed, false if not.
*/
return apply_filters( 'job_manager_user_can_search_users', $user_can_search_users );
}
/**
* Search for users and return json.
*/
public static function ajax_search_users() {
check_ajax_referer( 'search-users', 'security' );
if ( ! self::user_can_search_users() ) {
wp_die( -1 );
}
$term = isset( $_GET['term'] ) ? sanitize_text_field( wp_unslash( $_GET['term'] ) ) : '';
$page = isset( $_GET['page'] ) ? intval( $_GET['page'] ) : 1;
$per_page = 20;
$exclude = [];
if ( ! empty( $_GET['exclude'] ) ) {
$exclude = array_map( 'intval', $_GET['exclude'] );
}
if ( empty( $term ) ) {
wp_die();
}
$more_exist = false;
$users = [];
// Search by ID.
if ( is_numeric( $term ) && ! in_array( intval( $term ), $exclude, true ) ) {
$user = get_user_by( 'ID', intval( $term ) );
if ( $user instanceof WP_User ) {
$users[ $user->ID ] = $user;
}
}
if ( empty( $users ) ) {
$search_args = [
'exclude' => $exclude,
'search' => '*' . esc_attr( $term ) . '*',
'search_columns' => [ 'user_login', 'user_email', 'user_nicename', 'display_name' ],
'number' => $per_page,
'paged' => $page,
'orderby' => 'display_name',
'order' => 'ASC',
];
/**
* Modify the arguments used for `WP_User_Query` constructor.
*
* @since 1.32.0
*
* @see https://codex.wordpress.org/Class_Reference/WP_User_Query
*
* @param array $search_args Argument array used in `WP_User_Query` constructor.
* @param string $term Search term.
* @param int[] $exclude Array of IDs to exclude.
* @param int $page Current page.
*/
$search_args = apply_filters( 'job_manager_search_users_args', $search_args, $term, $exclude, $page );
$user_query = new WP_User_Query( $search_args );
$users = $user_query->get_results();
$total_pages = ceil( $user_query->get_total() / $per_page );
$more_exist = $total_pages > $page;
}
$found_users = [];
foreach ( $users as $user ) {
$found_users[ $user->ID ] = sprintf(
// translators: Used in user select. %1$s is the user's display name; #%2$s is the user ID; %3$s is the user email.
esc_html__( '%1$s (#%2$s – %3$s)', 'wp-job-manager' ),
htmlentities( $user->display_name ),
absint( $user->ID ),
$user->user_email
);
}
$response = [
'results' => $found_users,
'more' => $more_exist,
];
/**
* Modify the search results response for users in ajax call.
*
* @since 1.32.0
*
* @param array $response {
* @type array $results Array of all found users; id => string descriptor
* @type boolean $more True if there is an additional page.
* }
* @param string $term Search term.
* @param int[] $exclude Array of IDs to exclude.
* @param int $page Current page.
*/
$response = apply_filters( 'job_manager_search_users_response', $response, $term, $exclude, $page );
wp_send_json( $response );
}
}
WP_Job_Manager_Ajax::instance();