-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathclass-wp-job-manager-promoted-jobs-api.php
More file actions
346 lines (315 loc) · 9.38 KB
/
class-wp-job-manager-promoted-jobs-api.php
File metadata and controls
346 lines (315 loc) · 9.38 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
<?php
/**
* File containing the class WP_Job_Manager_Promoted_Jobs_API.
*
* @package wp-job-manager
*/
/**
* Handles functionality related to the Promoted Jobs REST API.
*
* @since 1.42.0
*/
class WP_Job_Manager_Promoted_Jobs_API {
/**
* The namespace.
*
* @var string
*/
private const NAMESPACE = 'wpjm-internal/v1';
/**
* Rest base for the current object.
*
* @var string
*/
private const REST_BASE = '/promoted-jobs';
/**
* The status handler.
*
* @var WP_Job_Manager_Promoted_Jobs_Status_Handler
*/
private WP_Job_Manager_Promoted_Jobs_Status_Handler $status_handler;
/**
* Constructor.
*
* @param WP_Job_Manager_Promoted_Jobs_Status_Handler $status_handler The status handler.
*/
public function __construct( WP_Job_Manager_Promoted_Jobs_Status_Handler $status_handler ) {
$this->status_handler = $status_handler;
add_filter( 'rest_post_dispatch', [ $this, 'add_nocache_headers' ], 10, 3 );
}
/**
* Initializes the REST API.
*
* @return void
*/
public function init() {
add_action( 'rest_api_init', [ $this, 'register_routes' ] );
}
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
register_rest_route(
self::NAMESPACE,
self::REST_BASE,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => '__return_true',
],
]
);
register_rest_route(
self::NAMESPACE,
self::REST_BASE . '/(?P<id>[\d]+)',
[
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_job_status' ],
'permission_callback' => '__return_true',
'args' => [
'id' => [
'type' => 'integer',
'required' => true,
],
'status' => [
'type' => 'boolean',
'required' => true,
],
],
],
]
);
register_rest_route(
self::NAMESPACE,
self::REST_BASE . '/(?P<job_id>[\d]+)',
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_job_data' ],
'permission_callback' => '__return_true',
'args' => [
'job_id' => [
'required' => true,
'type' => 'integer',
],
],
],
]
);
register_rest_route(
self::NAMESPACE,
self::REST_BASE . '/verify-token',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'verify_token' ],
'permission_callback' => '__return_true',
'args' => [
'user_id' => [
'required' => true,
'type' => 'integer',
],
'token' => [
'required' => true,
'type' => 'string',
],
'job_id' => [
'required' => true,
'type' => 'integer',
],
],
],
]
);
register_rest_route(
self::NAMESPACE,
self::REST_BASE . '/refresh-status',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'refresh_status' ],
'permission_callback' => '__return_true',
],
]
);
}
/**
* Adds no-cache headers to the REST response if they're in the Promoted Jobs API namespace.
*
* @param WP_REST_Response $response The response data.
* @param WP_REST_Server $server The REST server instance.
* @param WP_REST_Request $request The request used to generate the response.
*
* @return WP_REST_Response The response data.
*/
public function add_nocache_headers( $response, $server, $request ) {
// Check if the request belongs to the specified namespace and the response is successful.
if ( str_starts_with( $request->get_route(), '/' . self::NAMESPACE . self::REST_BASE ) && $response->get_status() >= 200 && $response->get_status() < 300 ) {
// Get the no-cache headers array.
$nocache_headers = wp_get_nocache_headers();
// Add nocache headers to the response.
foreach ( $nocache_headers as $header => $header_value ) {
if ( empty( $header_value ) ) {
$server->remove_header( $header );
} else {
$server->send_header( $header, $header_value );
}
}
}
return $response;
}
/**
* Get all promoted jobs.
*
* @return WP_Error|WP_REST_Response The response, or WP_Error on failure.
*/
public function get_items() {
global $wpdb;
$args = [
'post_type' => 'job_listing',
'post_status' => array_merge( array_keys( get_job_listing_post_statuses() ), [ 'trash' ] ),
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'posts_per_page' => -1,
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Returns promoted jobs only which should be a small number.
[
'key' => WP_Job_Manager_Promoted_Jobs::PROMOTED_META_KEY,
'compare' => 'EXISTS',
],
],
];
$items = get_posts( $args );
if ( ! empty( $wpdb->last_errors ) ) {
return new WP_Error(
'wpjm_error_getting_jobs',
$wpdb->last_errors,
[ 'status' => 500 ]
);
}
$data = array_map( [ $this, 'prepare_item_for_response' ], $items );
foreach ( $data as $job ) {
if ( is_wp_error( $job ) ) {
return $job;
}
}
return new WP_REST_Response( [ 'jobs' => $data ], 200 );
}
/**
* Prepare the item for the REST response
*
* @param WP_Post $item WordPress representation of the item.
*
* @return array|\WP_Error The response, or WP_Error on failure.
*/
private function prepare_item_for_response( WP_Post $item ) {
$terms = wpjm_get_the_job_types( $item );
if ( false === $terms ) {
$terms = [];
}
$terms_array = wp_list_pluck( $terms, 'slug' );
return [
'id' => (string) $item->ID,
'status' => $item->post_status,
'promoted' => WP_Job_Manager_Promoted_Jobs::is_promoted( $item->ID ),
'title' => $item->post_title,
'description' => $item->post_content,
'permalink' => get_permalink( $item ),
'location' => get_post_meta( $item->ID, '_job_location', true ),
'company_name' => get_post_meta( $item->ID, '_company_name', true ),
'is_remote' => (bool) get_post_meta( $item->ID, '_remote_position', true ),
'job_type' => $terms_array,
'salary' => [
'amount' => get_post_meta( $item->ID, '_job_salary', true ),
'amountmax' => get_post_meta( $item->ID, '_job_salary_max', true ),
'currency' => get_the_job_salary_currency( $item ),
'unit' => get_the_job_salary_unit_display_text( $item ),
],
];
}
/**
* Update the promoted job status.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_REST_Response The response, or WP_Error on failure.
*/
public function update_job_status( $request ) {
$post_id = $request->get_param( 'id' );
$status = $request->get_param( 'status' );
$post = get_post( $post_id );
if ( empty( $post ) ) {
return new WP_Error( 'not_found', __( 'The promoted job was not found', 'wp-job-manager' ), [ 'status' => 404 ] );
}
$result = WP_Job_Manager_Promoted_Jobs::update_promotion( $post_id, $status );
return new WP_REST_Response(
[
'data' => $result,
'message' => 'Promoted job status updated',
],
200
);
}
/**
* Get the job data or error if the job is not found.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_REST_Response|WP_Error The response, or WP_Error on failure.
*/
public function get_job_data( $request ) {
$job_id = $request->get_param( 'job_id' );
$post = get_post( $job_id );
if ( 'job_listing' !== get_post_type( $post ) ) {
return new WP_Error( 'not_found', __( 'The promoted job was not found', 'wp-job-manager' ), [ 'status' => 404 ] );
}
$controller = get_post_type_object( 'job_listing' )->get_rest_controller();
if ( ! ( $controller instanceof WP_REST_Posts_Controller ) || ! $controller->check_read_permission( $post ) ) {
return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to view this job.', 'wp-job-manager' ), [ 'status' => rest_authorization_required_code() ] );
}
$job_data = $this->prepare_item_for_response( get_post( $job_id ) );
if ( is_wp_error( $job_data ) ) {
return $job_data;
}
return rest_ensure_response(
[
'job_data' => $job_data,
]
);
}
/**
* Verify if the token is valid or not. Checks that the job exists and the user has access to it.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_REST_Response The result of the validation.
*/
public function verify_token( $request ) {
$token = $request->get_param( 'token' );
$user_id = $request->get_param( 'user_id' );
$job_id = $request->get_param( 'job_id' );
$verified = false;
// We only verify the token if the job_id exists and user has access to it.
if ( 'job_listing' === get_post_type( $job_id ) ) {
if ( user_can( $user_id, 'manage_job_listings', $job_id ) ) {
$verified = WP_Job_Manager_Site_Trust_Token::instance()->validate( 'user', $user_id, $token );
}
}
return rest_ensure_response(
[
'verified' => $verified,
]
);
}
/**
* Refreshes the status of the promoted jobs.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_REST_Response The response.
*/
public function refresh_status( $request ) {
$this->status_handler->fetch_updates();
return new WP_REST_Response( [ 'success' => true ] );
}
}