forked from mrpharderwijk/wp-job-manager-regions-dropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wp-job-manager-ajax.php
155 lines (121 loc) · 5.52 KB
/
class-wp-job-manager-ajax.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
<?php
/*
* this file is located in wp-content/plugins/wp-job-manager/includes/class-wp-job-manager-ajax.php
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* WP_Job_Manager_Ajax class.
*/
class WP_Job_Manager_Ajax {
/**
* Constructor
*/
public function __construct() {
add_action( 'wp_ajax_nopriv_job_manager_get_listings', array( $this, 'get_listings' ) );
add_action( 'wp_ajax_job_manager_get_listings', array( $this, 'get_listings' ) );
}
/**
* Get listings via ajax
*/
public function get_listings() {
$result = array();
$search_location = sanitize_text_field( stripslashes( $_POST['search_location'] ) );
$search_region = sanitize_text_field( stripslashes( $_POST['search_region'] ) );
$search_keywords = sanitize_text_field( stripslashes( $_POST['search_keywords'] ) );
$search_categories = isset( $_POST['search_categories'] ) ? $_POST['search_categories'] : '';
$filter_job_types = isset( $_POST['filter_job_type'] ) ? array_filter( array_map( 'sanitize_title', (array) $_POST['filter_job_type'] ) ) : null;
if ( is_array( $search_categories ) ) {
$search_categories = array_filter( array_map( 'sanitize_text_field', array_map( 'stripslashes', $search_categories ) ) );
} else {
$search_categories = array_filter( array( sanitize_text_field( stripslashes( $search_categories ) ) ) );
}
$args = array(
'search_location' => $search_location,
'search_region' => $search_region,
'search_keywords' => $search_keywords,
'search_categories' => $search_categories,
'job_types' => is_null( $filter_job_types ) ? '' : $filter_job_types + array( 0 ),
'orderby' => sanitize_text_field( $_POST['orderby'] ),
'order' => sanitize_text_field( $_POST['order'] ),
'offset' => ( absint( $_POST['page'] ) - 1 ) * absint( $_POST['per_page'] ),
'posts_per_page' => absint( $_POST['per_page'] )
);
if ( isset( $_POST['featured'] ) && ( $_POST['featured'] === 'true' || $_POST['featured'] === 'false' ) ) {
$args['featured'] = $_POST['featured'] === 'true' ? true : false;
}
ob_start();
$jobs = get_job_listings( apply_filters( 'job_manager_get_listings_args', $args ) );
$result['found_jobs'] = false;
if ( $jobs->have_posts() ) : $result['found_jobs'] = true; ?>
<?php while ( $jobs->have_posts() ) : $jobs->the_post(); ?>
<?php get_job_manager_template_part( 'content', 'job_listing' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_job_manager_template_part( 'content', 'no-jobs-found' ); ?>
<?php endif;
$result['html'] = ob_get_clean();
// Generate 'showing' text
$types = get_job_listing_types();
if ( sizeof( $filter_job_types ) > 0 && ( sizeof( $filter_job_types ) !== sizeof( $types ) || $search_keywords || $search_location || $search_region || $search_categories || apply_filters( 'job_manager_get_listings_custom_filter', false ) ) ) {
$showing_types = array();
$unmatched = false;
foreach ( $types as $type ) {
if ( in_array( $type->slug, $filter_job_types ) )
$showing_types[] = $type->name;
else
$unmatched = true;
}
if ( ! $unmatched )
$showing_types = '';
elseif ( sizeof( $showing_types ) == 1 ) {
$showing_types = implode( ', ', $showing_types ) . ' ';
} else {
$last = array_pop( $showing_types );
$showing_types = implode( ', ', $showing_types );
$showing_types .= " & $last ";
}
$showing_categories = array();
if ( $search_categories ) {
foreach ( $search_categories as $category ) {
if ( ! is_numeric( $category ) ) {
$category_object = get_term_by( 'slug', $category, 'job_listing_category' );
}
if ( is_numeric( $category ) || is_wp_error( $category_object ) || ! $category_object ) {
$category_object = get_term_by( 'id', $category, 'job_listing_category' );
}
if ( ! is_wp_error( $category_object ) ) {
$showing_categories[] = $category_object->name;
}
}
}
if ( $search_keywords ) {
$showing_jobs = sprintf( __( 'Showing %s“%s” %sjobs', 'wp-job-manager' ), $showing_types, $search_keywords, implode( ', ', $showing_categories ) );
} else {
$showing_jobs = sprintf( __( 'Showing all %s%sjobs', 'wp-job-manager' ), $showing_types, implode( ', ', $showing_categories ) . ' ' );
}
$showing_location = $search_location ? sprintf( ' ' . __( 'located in “%s”', 'wp-job-manager' ), $search_location ) : '';
$showing_region = $search_region ? sprintf( ' ' . __( 'located in “%s”', 'wp-job-manager' ), $search_region ) : '';
$result['showing'] = apply_filters( 'job_manager_get_listings_custom_filter_text', $showing_jobs . $showing_location . $showing_region);
} else {
$result['showing'] = '';
}
// Generate RSS link
$result['showing_links'] = job_manager_get_filtered_links( array(
'filter_job_types' => $filter_job_types,
'search_location' => $search_location,
'search_region' => $search_region,
'search_categories' => $search_categories,
'search_keywords' => $search_keywords
) );
// Generate pagination
if ( isset( $_POST['show_pagination'] ) && $_POST['show_pagination'] === 'true' ) {
$result['pagination'] = get_job_listing_pagination( $jobs->max_num_pages, absint( $_POST['page'] ) );
}
$result['max_num_pages'] = $jobs->max_num_pages;
echo '<!--WPJM-->';
echo json_encode( apply_filters( 'job_manager_get_listings_result', $result ) );
echo '<!--WPJM_END-->';
die();
}
}
new WP_Job_Manager_Ajax();