-
Notifications
You must be signed in to change notification settings - Fork 366
/
class-wp-job-manager-writepanels.php
706 lines (640 loc) · 25.3 KB
/
class-wp-job-manager-writepanels.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Handles the management of Job Listing meta fields.
*
* @package wp-job-manager
* @since 1.0.0
*/
class WP_Job_Manager_Writepanels {
/**
* 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( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ), 1, 2 );
add_action( 'job_manager_save_job_listing', array( $this, 'save_job_listing_data' ), 20, 2 );
}
/**
* Returns configuration for custom fields on Job Listing posts.
*
* @return array
*/
public function job_listing_fields() {
global $post;
$current_user = wp_get_current_user();
$fields = array(
'_job_location' => array(
'label' => __( 'Location', 'wp-job-manager' ),
'placeholder' => __( 'e.g. "London"', 'wp-job-manager' ),
'description' => __( 'Leave this blank if the location is not important.', 'wp-job-manager' ),
'priority' => 1
),
'_application' => array(
'label' => __( 'Application Email or URL', 'wp-job-manager' ),
'placeholder' => __( 'URL or email which applicants use to apply', 'wp-job-manager' ),
'description' => __( 'This field is required for the "application" area to appear beneath the listing.', 'wp-job-manager' ),
'value' => metadata_exists( 'post', $post->ID, '_application' ) ? get_post_meta( $post->ID, '_application', true ) : $current_user->user_email,
'priority' => 2
),
'_company_name' => array(
'label' => __( 'Company Name', 'wp-job-manager' ),
'placeholder' => '',
'priority' => 3
),
'_company_website' => array(
'label' => __( 'Company Website', 'wp-job-manager' ),
'placeholder' => '',
'priority' => 4
),
'_company_tagline' => array(
'label' => __( 'Company Tagline', 'wp-job-manager' ),
'placeholder' => __( 'Brief description about the company', 'wp-job-manager' ),
'priority' => 5
),
'_company_twitter' => array(
'label' => __( 'Company Twitter', 'wp-job-manager' ),
'placeholder' => '@yourcompany',
'priority' => 6
),
'_company_video' => array(
'label' => __( 'Company Video', 'wp-job-manager' ),
'placeholder' => __( 'URL to the company video', 'wp-job-manager' ),
'type' => 'file',
'priority' => 8
),
'_filled' => array(
'label' => __( 'Position Filled', 'wp-job-manager' ),
'type' => 'checkbox',
'priority' => 9,
'description' => __( 'Filled listings will no longer accept applications.', 'wp-job-manager' ),
)
);
if ( $current_user->has_cap( 'manage_job_listings' ) ) {
$fields['_featured'] = array(
'label' => __( 'Featured Listing', 'wp-job-manager' ),
'type' => 'checkbox',
'description' => __( 'Featured listings will be sticky during searches, and can be styled differently.', 'wp-job-manager' ),
'priority' => 10
);
$job_expires = get_post_meta( $post->ID, '_job_expires', true );
$fields['_job_expires'] = array(
'label' => __( 'Listing Expiry Date', 'wp-job-manager' ),
'priority' => 11,
'classes' => array( 'job-manager-datepicker' ),
'placeholder' => ! empty( $job_expires ) ? null : date_i18n( get_option( 'date_format' ), strtotime( calculate_job_expiry( $post->ID ) ) ),
'value' => ! empty( $job_expires ) ? date( 'Y-m-d', strtotime( $job_expires ) ) : '',
);
}
if ( $current_user->has_cap( 'edit_others_job_listings' ) ) {
$fields['_job_author'] = array(
'label' => __( 'Posted by', 'wp-job-manager' ),
'type' => 'author',
'priority' => 12
);
}
/**
* Filters job listing data fields for WP Admin post editor.
*
* @since 1.0.0
* @since 1.27.0 $post_id was added
*
* @param array $fields
* @param int $post_id
*/
$fields = apply_filters( 'job_manager_job_listing_data_fields', $fields, $post->ID );
uasort( $fields, array( $this, 'sort_by_priority' ) );
return $fields;
}
/**
* Sorts array of custom fields by priority value.
*
* @param array $a
* @param array $b
* @return int
*/
protected function sort_by_priority( $a, $b ) {
if ( ! isset( $a['priority'] ) || ! isset( $b['priority'] ) || $a['priority'] === $b['priority'] ) {
return 0;
}
return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
}
/**
* Handles the hooks to add custom field meta boxes.
*/
public function add_meta_boxes() {
global $wp_post_types;
add_meta_box(
'job_listing_data',
sprintf( __( '%s Data', 'wp-job-manager' ), $wp_post_types['job_listing']->labels->singular_name ),
array( $this, 'job_listing_data' ),
'job_listing',
'normal',
'high',
array(
'__back_compat_meta_box' => true,
'__block_editor_compatible_meta_box' => true,
)
);
if ( ! get_option( 'job_manager_enable_types' ) || wp_count_terms( 'job_listing_type' ) == 0 ) {
remove_meta_box( 'job_listing_typediv', 'job_listing', 'side');
} elseif ( false == job_manager_multi_job_type() ) {
remove_meta_box( 'job_listing_typediv', 'job_listing', 'side');
$job_listing_type = get_taxonomy( 'job_listing_type' );
add_meta_box( 'job_listing_type', $job_listing_type->labels->menu_name, array( $this, 'job_listing_metabox' ),'job_listing' ,'side','core');
}
}
/**
* Displays job listing metabox.
*
* @param int|WP_Post $post
*/
public function job_listing_metabox( $post ) {
// Set up the taxonomy object and get terms
$taxonomy = 'job_listing_type';
$tax = get_taxonomy( $taxonomy );// This is the taxonomy object
// The name of the form
$name = 'tax_input[' . $taxonomy . ']';
// Get all the terms for this taxonomy
$terms = get_terms( $taxonomy, array( 'hide_empty' => 0 ) );
$postterms = get_the_terms( $post->ID, $taxonomy );
$current = ( $postterms ? array_pop( $postterms ) : false );
$current = ( $current ? $current->term_id : 0 );
// Get current and popular terms
$popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
$postterms = get_the_terms( $post->ID,$taxonomy );
$current = ($postterms ? array_pop($postterms) : false);
$current = ($current ? $current->term_id : 0);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
<!-- Display tabs-->
<ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
<li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a></li>
<li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used', 'wp-job-manager' ); ?></a></li>
</ul>
<!-- Display taxonomy terms -->
<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
<?php foreach($terms as $term){
$id = $taxonomy.'-'.$term->term_id;
echo "<li id='$id'><label class='selectit'>";
echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
echo "</label></li>";
}?>
</ul>
</div>
<!-- Display popular taxonomy terms -->
<div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
<ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
<?php foreach($popular as $term){
$id = 'popular-'.$taxonomy.'-'.$term->term_id;
echo "<li id='$id'><label class='selectit'>";
echo "<input type='radio' id='in-$id'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
echo "</label></li>";
}?>
</ul>
</div>
</div>
<?php
}
/**
* Displays label and file input field.
*
* @param string $key
* @param array $field
*/
public static function input_file( $key, $field ) {
global $thepostid;
if ( ! isset( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( empty( $field['placeholder'] ) ) {
$field['placeholder'] = 'http://';
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<?php
if ( ! empty( $field['multiple'] ) ) {
foreach ( (array) $field['value'] as $value ) {
?><span class="file_url"><input type="text" name="<?php echo esc_attr( $name ); ?>[]" placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" value="<?php echo esc_attr( $value ); ?>" /><button class="button button-small wp_job_manager_upload_file_button" data-uploader_button_text="<?php _e( 'Use file', 'wp-job-manager' ); ?>"><?php _e( 'Upload', 'wp-job-manager' ); ?></button><button class="button button-small wp_job_manager_view_file_button"><?php _e( 'View', 'wp-job-manager' ); ?></button></span><?php
}
} else {
?><span class="file_url"><input type="text" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $key ); ?>" placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" value="<?php echo esc_attr( $field['value'] ); ?>" /><button class="button button-small wp_job_manager_upload_file_button" data-uploader_button_text="<?php _e( 'Use file', 'wp-job-manager' ); ?>"><?php _e( 'Upload', 'wp-job-manager' ); ?></button><button class="button button-small wp_job_manager_view_file_button"><?php _e( 'View', 'wp-job-manager' ); ?></button></span><?php
}
if ( ! empty( $field['multiple'] ) ) {
?><button class="button button-small wp_job_manager_add_another_file_button" data-field_name="<?php echo esc_attr( $key ); ?>" data-field_placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" data-uploader_button_text="<?php _e( 'Use file', 'wp-job-manager' ); ?>" data-uploader_button="<?php _e( 'Upload', 'wp-job-manager' ); ?>" data-view_button="<?php _e( 'View', 'wp-job-manager' ); ?>"><?php _e( 'Add file', 'wp-job-manager' ); ?></button><?php
}
?>
</p>
<?php
}
/**
* Displays label and text input field.
*
* @param string $key
* @param array $field
*/
public static function input_text( $key, $field ) {
global $thepostid;
if ( ! isset( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
if ( ! empty( $field['classes'] ) ) {
$classes = implode( ' ', is_array( $field['classes'] ) ? $field['classes'] : array( $field['classes'] ) );
} else {
$classes = '';
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<input type="text" autocomplete="off" name="<?php echo esc_attr( $name ); ?>" class="<?php echo esc_attr( $classes ); ?>" id="<?php echo esc_attr( $key ); ?>" placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" value="<?php echo esc_attr( $field['value'] ); ?>" />
</p>
<?php
}
/**
* Just displays information.
*
* @since 1.27.0
*
* @param string $key
* @param array $field
*/
public static function input_info( $key, $field ) {
self::input_hidden( $key, $field );
}
/**
* Displays information and/or hidden input.
*
* @since 1.27.0
*
* @param string $key
* @param array $field
*/
public static function input_hidden( $key, $field ) {
global $thepostid;
if ( 'hidden' === $field['type'] && ! isset( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
if ( ! empty( $field['classes'] ) ) {
$classes = implode( ' ', is_array( $field['classes'] ) ? $field['classes'] : array( $field['classes'] ) );
} else {
$classes = '';
}
$hidden_input = '';
if ( 'hidden' === $field['type'] ) {
$hidden_input = '<input type="hidden" name="' . esc_attr( $name ) . '" class="' . esc_attr( $classes ) . '" id="' . esc_attr( $key ) . '" value="' . esc_attr( $field['value'] ) . '" />';
if ( empty( $field['label'] ) ) {
echo $hidden_input;
return;
}
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<?php if ( ! empty( $field['information'] ) ) : ?><span class="information"><?php echo wp_kses( $field['information'], array( 'a' => array( 'href' => array() ) ) ); ?></span><?php endif; ?>
<?php echo $hidden_input; ?>
</p>
<?php
}
/**
* Displays label and textarea input field.
*
* @param string $key
* @param array $field
*/
public static function input_textarea( $key, $field ) {
global $thepostid;
if ( ! isset( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<textarea name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $key ); ?>" placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>"><?php echo esc_html( $field['value'] ); ?></textarea>
</p>
<?php
}
/**
* Displays label and select input field.
*
* @param string $key
* @param array $field
*/
public static function input_select( $key, $field ) {
global $thepostid;
if ( ! isset( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<select name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $key ); ?>">
<?php foreach ( $field['options'] as $key => $value ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php if ( isset( $field['value'] ) ) selected( $field['value'], $key ); ?>><?php echo esc_html( $value ); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
/**
* Displays label and multi-select input field.
*
* @param string $key
* @param array $field
*/
public static function input_multiselect( $key, $field ) {
global $thepostid;
if ( ! isset( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<select multiple="multiple" name="<?php echo esc_attr( $name ); ?>[]" id="<?php echo esc_attr( $key ); ?>">
<?php foreach ( $field['options'] as $key => $value ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php if ( ! empty( $field['value'] ) && is_array( $field['value'] ) ) selected( in_array( $key, $field['value'] ), true ); ?>><?php echo esc_html( $value ); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
/**
* Displays label and checkbox input field.
*
* @param string $key
* @param array $field
*/
public static function input_checkbox( $key, $field ) {
global $thepostid;
if ( empty( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
?>
<p class="form-field form-field-checkbox">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?></label>
<input type="checkbox" class="checkbox" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" <?php checked( $field['value'], 1 ); ?> />
<?php if ( ! empty( $field['description'] ) ) : ?><span class="description"><?php echo $field['description']; ?></span><?php endif; ?>
</p>
<?php
}
/**
* Displays label and author select field.
*
* @param string $key
* @param array $field
*/
public static function input_author( $key, $field ) {
global $thepostid, $post;
if ( ! $post || $thepostid !== $post->ID ) {
$the_post = get_post( $thepostid );
$author_id = $the_post->post_author;
} else {
$author_id = $post->post_author;
}
$posted_by = get_user_by( 'id', $author_id );
$field['value'] = ! isset( $field['value'] ) ? get_post_meta( $thepostid, $key, true ) : $field['value'];
$name = ! empty( $field['name'] ) ? $field['name'] : $key;
?>
<p class="form-field form-field-author">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo wp_strip_all_tags( $field['label'] ) ; ?>:</label>
<span class="current-author">
<?php
if ( $posted_by ) {
echo '<a href="' . admin_url( 'user-edit.php?user_id=' . absint( $author_id ) ) . '">#' . absint( $author_id ) . ' – ' . $posted_by->user_login . '</a>';
} else {
_e( 'Guest User', 'wp-job-manager' );
}
?> <a href="#" class="change-author button button-small"><?php _e( 'Change', 'wp-job-manager' ); ?></a>
</span>
<span class="hidden change-author">
<input type="number" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $key ); ?>" step="1" value="<?php echo esc_attr( $author_id ); ?>" style="width: 4em;" />
<span class="description"><?php _e( 'Enter the ID of the user, or leave blank if submitted by a guest.', 'wp-job-manager' ) ?></span>
</span>
</p>
<?php
}
/**
* Displays label and radio input field.
*
* @param string $key
* @param array $field
*/
public static function input_radio( $key, $field ) {
global $thepostid;
if ( empty( $field['value'] ) ) {
$field['value'] = get_post_meta( $thepostid, $key, true );
}
if ( ! empty( $field['name'] ) ) {
$name = $field['name'];
} else {
$name = $key;
}
?>
<p class="form-field form-field-checkbox">
<label><?php echo wp_strip_all_tags( $field['label'] ) ; ?></label>
<?php foreach ( $field['options'] as $option_key => $value ) : ?>
<label><input type="radio" class="radio" name="<?php echo esc_attr( isset( $field['name'] ) ? $field['name'] : $key ); ?>" value="<?php echo esc_attr( $option_key ); ?>" <?php checked( $field['value'], $option_key ); ?> /> <?php echo esc_html( $value ); ?></label>
<?php endforeach; ?>
<?php if ( ! empty( $field['description'] ) ) : ?><span class="description"><?php echo $field['description']; ?></span><?php endif; ?>
</p>
<?php
}
/**
* Displays metadata fields for Job Listings.
*
* @param int|WP_Post $post
*/
public function job_listing_data( $post ) {
global $post, $thepostid, $wp_post_types;
$thepostid = $post->ID;
echo '<div class="wp_job_manager_meta_data">';
wp_nonce_field( 'save_meta_data', 'job_manager_nonce' );
do_action( 'job_manager_job_listing_data_start', $thepostid );
foreach ( $this->job_listing_fields() as $key => $field ) {
$type = ! empty( $field['type'] ) ? $field['type'] : 'text';
if ( has_action( 'job_manager_input_' . $type ) ) {
do_action( 'job_manager_input_' . $type, $key, $field );
} elseif ( method_exists( $this, 'input_' . $type ) ) {
call_user_func( array( $this, 'input_' . $type ), $key, $field );
}
}
$user_edited_date = get_post_meta( $post->ID, '_job_edited', true );
if ( $user_edited_date ) {
echo '<p class="form-field">';
echo '<em>' . sprintf( __( '%s was last modified by the user on %s.', 'wp-job-manager' ), $wp_post_types['job_listing']->labels->singular_name, date_i18n( get_option( 'date_format' ), $user_edited_date ) ) . '</em>';
echo '</p>';
}
do_action( 'job_manager_job_listing_data_end', $thepostid );
echo '</div>';
}
/**
* Handles `save_post` action.
*
* @param int $post_id
* @param WP_Post $post
*/
public function save_post( $post_id, $post ) {
if ( empty( $post_id ) || empty( $post ) || empty( $_POST ) ) return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( is_int( wp_is_post_revision( $post ) ) ) return;
if ( is_int( wp_is_post_autosave( $post ) ) ) return;
if ( empty($_POST['job_manager_nonce']) || ! wp_verify_nonce( $_POST['job_manager_nonce'], 'save_meta_data' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( $post->post_type != 'job_listing' ) return;
do_action( 'job_manager_save_job_listing', $post_id, $post );
}
/**
* Handles the actual saving of job listing data fields.
*
* @param int $post_id
* @param WP_Post $post (Unused)
*/
public function save_job_listing_data( $post_id, $post ) {
global $wpdb;
// These need to exist
add_post_meta( $post_id, '_filled', 0, true );
add_post_meta( $post_id, '_featured', 0, true );
// Save fields
foreach ( $this->job_listing_fields() as $key => $field ) {
if ( isset( $field['type'] ) && 'info' === $field['type'] ) {
continue;
}
// Expirey date
if ( '_job_expires' === $key ) {
if ( empty( $_POST[ $key ] ) ) {
if ( get_option( 'job_manager_submission_duration' ) ) {
update_post_meta( $post_id, $key, calculate_job_expiry( $post_id ) );
} else {
delete_post_meta( $post_id, $key );
}
} else {
update_post_meta( $post_id, $key, date( 'Y-m-d', strtotime( sanitize_text_field( $_POST[ $key ] ) ) ) );
}
}
// Locations
elseif ( '_job_location' === $key ) {
if ( update_post_meta( $post_id, $key, sanitize_text_field( $_POST[ $key ] ) ) ) {
// Location data will be updated by hooked in methods
} elseif ( apply_filters( 'job_manager_geolocation_enabled', true ) && ! WP_Job_Manager_Geocode::has_location_data( $post_id ) ) {
WP_Job_Manager_Geocode::generate_location_data( $post_id, sanitize_text_field( $_POST[ $key ] ) );
}
}
elseif ( '_job_author' === $key ) {
$wpdb->update( $wpdb->posts, array( 'post_author' => $_POST[ $key ] > 0 ? absint( $_POST[ $key ] ) : 0 ), array( 'ID' => $post_id ) );
}
elseif ( '_application' === $key ) {
update_post_meta( $post_id, $key, sanitize_text_field( is_email( $_POST[ $key ] ) ? $_POST[ $key ] : urldecode( $_POST[ $key ] ) ) );
}
// Everything else
else {
$type = ! empty( $field['type'] ) ? $field['type'] : '';
switch ( $type ) {
case 'textarea' :
update_post_meta( $post_id, $key, wp_kses_post( stripslashes( $_POST[ $key ] ) ) );
break;
case 'checkbox' :
if ( isset( $_POST[ $key ] ) ) {
update_post_meta( $post_id, $key, 1 );
} else {
update_post_meta( $post_id, $key, 0 );
}
break;
default :
if ( ! isset( $_POST[ $key ] ) ) {
continue;
} elseif ( is_array( $_POST[ $key ] ) ) {
update_post_meta( $post_id, $key, array_filter( array_map( 'sanitize_text_field', $_POST[ $key ] ) ) );
} else {
update_post_meta( $post_id, $key, sanitize_text_field( $_POST[ $key ] ) );
}
break;
}
}
}
/* Set Post Status To Expired If Already Expired */
$expiry_date = get_post_meta( $post_id, '_job_expires', true );
$today_date = date( 'Y-m-d', current_time( 'timestamp' ) );
$is_job_listing_expired = $expiry_date && $today_date > $expiry_date;
if( $is_job_listing_expired ) {
remove_action( 'job_manager_save_job_listing', array( $this, 'save_job_listing_data' ), 20, 2 );
if ( $this->is_job_listing_being_reactivated() ) {
update_post_meta( $post_id, '_job_expires', calculate_job_expiry( $post_id ) );
} else {
$job_data = array(
'ID' => $post_id,
'post_status' => 'expired',
);
wp_update_post( $job_data );
}
add_action( 'job_manager_save_job_listing', array( $this, 'save_job_listing_data' ), 20, 2 );
}
}
/**
* Checks if the job listing is being reactivated from an expired state.
*
* @return bool True if being reactivated.
*/
protected function is_job_listing_being_reactivated() {
return isset( $_POST['post_status'] )
&& isset( $_POST['original_post_status'] )
&& 'expired' === $_POST['original_post_status']
&& 'publish' === $_POST['post_status'];
}
}
WP_Job_Manager_Writepanels::instance();