-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-job-manager-functions.php
347 lines (289 loc) · 9.1 KB
/
wp-job-manager-functions.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
<?php
if ( ! function_exists( 'get_job_listings' ) ) :
/**
* Queries job listings with certain criteria and returns them
*
* @access public
* @return void
*/
function get_job_listings( $args = array() ) {
global $wpdb;
$args = wp_parse_args( $args, array(
'search_location' => '',
'search_keywords' => '',
'search_categories' => array(),
'job_types' => array(),
'offset' => '',
'posts_per_page' => '-1',
'orderby' => 'date',
'order' => 'DESC'
) );
$query_args = array(
'post_type' => 'job_listing',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'offset' => absint( $args['offset'] ),
'posts_per_page' => intval( $args['posts_per_page'] ),
'orderby' => $args['orderby'],
'order' => $args['order'],
'tax_query' => array(),
'meta_query' => array()
);
if ( ! empty( $args['job_types'] ) )
$query_args['tax_query'][] = array(
'taxonomy' => 'job_listing_type',
'field' => 'slug',
'terms' => $args['job_types']
);
if ( ! empty( $args['search_categories'] ) ) {
$field = is_numeric( $args['search_categories'][0] ) ? 'term_id' : 'slug';
$query_args['tax_query'][] = array(
'taxonomy' => 'job_listing_category',
'field' => $field,
'terms' => $args['search_categories']
);
}
if ( get_option( 'job_manager_hide_filled_positions' ) == 1 )
$query_args['meta_query'][] = array(
'key' => '_filled',
'value' => '1',
'compare' => '!='
);
if ( $args['search_location'] )
$query_args['meta_query'][] = array(
'key' => '_job_location',
'value' => $args['search_location'],
'compare' => 'LIKE'
);
// Keyword search - search meta as well as post content
if ( $args['search_keywords'] ) {
$post_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT post_id FROM {$wpdb->postmeta}
WHERE meta_value LIKE '%%%s%%'
", $args['search_keywords'] ) );
$post_ids = array_merge( $post_ids, $wpdb->get_col( $wpdb->prepare( "
SELECT ID FROM {$wpdb->posts}
WHERE post_title LIKE '%%%s%%'
OR post_content LIKE '%%%s%%'
AND post_type = 'job_listing'
AND post_status = 'publish'
", $args['search_keywords'], $args['search_keywords'] ) ) );
$query_args['post__in'] = $post_ids + array( 0 );
}
$query_args = apply_filters( 'job_manager_get_listings', $query_args );
if ( empty( $query_args['meta_query'] ) )
unset( $query_args['meta_query'] );
if ( empty( $query_args['tax_query'] ) )
unset( $query_args['tax_query'] );
if ( $args['orderby'] == 'featured' ) {
$query_args['orderby'] = 'meta_key';
$query_args['meta_key'] = '_featured';
add_filter( 'posts_clauses', 'order_featured_job_listing' );
}
// Filter args
$query_args = apply_filters( 'get_job_listings_query_args', $query_args );
do_action( 'before_get_job_listings', $query_args );
$result = new WP_Query( $query_args );
do_action( 'after_get_job_listings', $query_args );
remove_filter( 'posts_clauses', 'order_featured_job_listing' );
return $result;
}
endif;
if ( ! function_exists( 'order_featured_job_listing' ) ) :
/**
* WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065
*
* @access public
* @param array $args
* @return array
*/
function order_featured_job_listing( $args ) {
global $wpdb;
$args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
return $args;
}
endif;
if ( ! function_exists( 'get_featured_job_ids' ) ) :
/**
* Gets the ids of featured jobs.
*
* @access public
* @return array
*/
function get_featured_job_ids() {
return get_posts( array(
'posts_per_page' => -1,
'post_type' => 'job_listing',
'post_status' => 'publish',
'meta_key' => '_featured',
'meta_value' => '1',
'fields' => 'ids'
) );
}
endif;
if ( ! function_exists( 'get_job_listing_types' ) ) :
/**
* Outputs a form to submit a new job to the site from the frontend.
*
* @access public
* @return array
*/
function get_job_listing_types() {
return get_terms( "job_listing_type", array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
) );
}
endif;
if ( ! function_exists( 'get_job_listing_categories' ) ) :
/**
* Outputs a form to submit a new job to the site from the frontend.
*
* @access public
* @return array
*/
function get_job_listing_categories() {
if ( ! get_option( 'job_manager_enable_categories' ) )
return array();
return get_terms( "job_listing_category", array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
) );
}
endif;
if ( ! function_exists( 'job_manager_get_filtered_links' ) ) :
/**
* Shows links after filtering jobs
*/
function job_manager_get_filtered_links( $args = array() ) {
$links = apply_filters( 'job_manager_job_filters_showing_jobs_links', array(
'reset' => array(
'name' => __( 'Reset', 'job_manager' ),
'url' => '#'
),
'rss_link' => array(
'name' => __( 'RSS', 'job_manager' ),
'url' => get_job_listing_rss_link( apply_filters( 'job_manager_get_listings_custom_filter_rss_args', array(
'type' => isset( $args['filter_job_types'] ) ? implode( ',', $args['filter_job_types'] ) : '',
'location' => $args['search_location'],
'job_categories' => implode( ',', $args['search_categories'] ),
's' => $args['search_keywords'],
) ) )
)
), $args );
$return = '';
foreach ( $links as $key => $link ) {
$return .= '<a href="' . esc_url( $link['url'] ) . '" class="' . esc_attr( $key ) . '">' . $link['name'] . '</a>';
}
return $return;
}
endif;
if ( ! function_exists( 'get_job_listing_rss_link' ) ) :
/**
* Get the Job Listing RSS link
*
* @return string
*/
function get_job_listing_rss_link( $args = array() ) {
$rss_link = add_query_arg( array_merge( array( 'feed' => 'job_feed' ), $args ), home_url() );
return $rss_link;
}
endif;
if ( ! function_exists( 'job_manager_create_account' ) ) :
/**
* Handle account creation.
*
* @param string $account_email
* @param string $role
* @return WP_error | bool was an account created?
*/
function wp_job_manager_create_account( $account_email, $role = '' ) {
global $current_user;
$user_email = apply_filters( 'user_registration_email', sanitize_email( $account_email ) );
if ( empty( $user_email ) )
return false;
if ( ! is_email( $user_email ) )
return new WP_Error( 'validation-error', __( 'Your email address isn’t correct.', 'job_manager' ) );
if ( email_exists( $user_email ) )
return new WP_Error( 'validation-error', __( 'This email is already registered, please choose another one.', 'job_manager' ) );
// Email is good to go - use it to create a user name
$username = sanitize_user( current( explode( '@', $user_email ) ) );
$password = wp_generate_password();
// Ensure username is unique
$append = 1;
$o_username = $username;
while( username_exists( $username ) ) {
$username = $o_username . $append;
$append ++;
}
// Final error check
$reg_errors = new WP_Error();
do_action( 'register_post', $username, $user_email, $reg_errors );
$reg_errors = apply_filters( 'registration_errors', $reg_errors, $username, $user_email );
if ( $reg_errors->get_error_code() )
return $reg_errors;
// Create account
$new_user = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $user_email,
'role' => $role
);
$user_id = wp_insert_user( apply_filters( 'job_manager_create_account_data', $new_user ) );
if ( is_wp_error( $user_id ) )
return $user_id;
// Notify
wp_new_user_notification( $user_id, $password );
// Login
wp_set_auth_cookie( $user_id, true, is_ssl() );
$current_user = get_user_by( 'id', $user_id );
return true;
}
endif;
/**
* True if an the user can post a job. If accounts are required, and reg is enabled, users can post (they signup at the same time).
*
* @return bool
*/
function job_manager_user_can_post_job() {
$can_post = true;
if ( ! is_user_logged_in() ) {
if ( job_manager_user_requires_account() && ! job_manager_enable_registration() ) {
$can_post = false;
}
}
return apply_filters( 'job_manager_user_can_post_job', $can_post );
}
/**
* True if an the user can edit a job.
*
* @return bool
*/
function job_manager_user_can_edit_job( $job_id ) {
$can_edit = true;
$job = get_post( $job_id );
if ( ! is_user_logged_in() ) {
$can_edit = false;
} elseif ( $job->post_author != get_current_user_id() ) {
$can_edit = false;
}
return apply_filters( 'job_manager_user_can_edit_job', $can_edit, $job_id );
}
/**
* True if registration is enabled.
*
* @return bool
*/
function job_manager_enable_registration() {
return apply_filters( 'job_manager_enable_registration', get_option( 'job_manager_enable_registration' ) == 1 ? true : false );
}
/**
* True if an account is required to post a job.
*
* @return bool
*/
function job_manager_user_requires_account() {
return apply_filters( 'job_manager_user_requires_account', get_option( 'job_manager_user_requires_account' ) == 1 ? true : false );
}