-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathclass-wp-job-manager-post-types.php
More file actions
2033 lines (1813 loc) · 66.7 KB
/
class-wp-job-manager-post-types.php
File metadata and controls
2033 lines (1813 loc) · 66.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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File containing the class WP_Job_Manager_Post_Types.
*
* @package wp-job-manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles displays and hooks for the Job Listing custom post type.
*
* @since 1.0.0
*/
class WP_Job_Manager_Post_Types {
const PERMALINK_OPTION_NAME = 'job_manager_permalinks';
/**
* The single instance of the class.
*
* @var self
* @since 1.26.0
*/
private static $instance = null;
/**
* Job IDs that were submitted in this request.
*
* @var array
*/
private $submitted_job_ids = [];
/**
* 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', [ $this, 'register_post_types' ], 0 );
add_action( 'init', [ $this, 'prepare_block_editor' ] );
add_action( 'init', [ $this, 'register_meta_fields' ] );
add_filter( 'admin_head', [ $this, 'admin_head' ] );
add_action( 'job_manager_check_for_expired_jobs', [ $this, 'check_for_expired_jobs' ] );
add_action( 'job_manager_delete_old_previews', [ $this, 'delete_old_previews' ] );
add_action( 'transition_post_status', [ $this, 'transition_post_status' ], 10, 3 );
add_action( 'wp_head', [ $this, 'noindex_expired_filled_job_listings' ], 0 );
add_action( 'wp_footer', [ $this, 'output_structured_data' ] );
add_filter( 'wp_sitemaps_posts_query_args', [ $this, 'sitemaps_maybe_hide_filled' ], 10, 2 );
add_filter( 'the_job_description', 'wptexturize' );
add_filter( 'the_job_description', 'convert_smilies' );
add_filter( 'the_job_description', 'convert_chars' );
add_filter( 'the_job_description', 'wpautop' );
add_filter( 'the_job_description', 'shortcode_unautop' );
add_filter( 'the_job_description', 'prepend_attachment' );
if ( ! empty( $GLOBALS['wp_embed'] ) ) {
add_filter( 'the_job_description', [ $GLOBALS['wp_embed'], 'run_shortcode' ], 8 );
add_filter( 'the_job_description', [ $GLOBALS['wp_embed'], 'autoembed' ], 8 );
}
add_action( 'job_manager_application_details_email', [ $this, 'application_details_email' ] );
add_action( 'job_manager_application_details_url', [ $this, 'application_details_url' ] );
add_filter( 'wp_insert_post_data', [ $this, 'fix_post_name' ], 10, 2 );
add_action( 'add_post_meta', [ $this, 'maybe_add_geolocation_data' ], 10, 3 );
add_action( 'update_post_meta', [ $this, 'update_post_meta' ], 10, 4 );
add_action( 'updated_post_meta', [ $this, 'delete_filled_job_listing_transient' ], 10, 4 );
add_action( 'wp_insert_post', [ $this, 'maybe_add_default_meta_data' ], 10, 2 );
add_filter( 'post_types_to_delete_with_user', [ $this, 'delete_user_add_job_listings_post_type' ] );
add_action( 'transition_post_status', [ $this, 'track_job_submission' ], 10, 3 );
add_action( 'parse_query', [ $this, 'add_feed_query_args' ] );
add_action( 'pre_get_posts', [ $this, 'maybe_hide_filled_job_listings' ] );
add_action( 'pre_get_posts', [ $this, 'maybe_hide_expired_job_listings' ] );
// Single job content.
$this->job_content_filter( true );
}
/**
* Prepare CPTs for special block editor situations.
*/
public function prepare_block_editor() {
if ( false === job_manager_multi_job_type() ) {
add_filter( 'rest_prepare_taxonomy', [ $this, 'hide_job_type_block_editor_selector' ], 10, 3 );
}
}
/**
* Forces job listings to just have the classic block. This is necessary with the use of the classic editor on
* the frontend.
*
* @deprecated 1.35.2
*
* @param array $allowed_block_types
* @param WP_Post $post
* @return array
*/
public function force_classic_block( $allowed_block_types, $post ) {
_deprecated_function( __METHOD__, '1.35.2' );
if ( 'job_listing' === $post->post_type ) {
return [ 'core/freeform' ];
}
return $allowed_block_types;
}
/**
* Filters a taxonomy returned from the REST API.
*
* Allows modification of the taxonomy data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param object $taxonomy The original taxonomy object.
* @param WP_REST_Request $request Request used to generate the response.
*
* @return WP_REST_Response
*/
public function hide_job_type_block_editor_selector( $response, $taxonomy, $request ) {
if (
'job_listing_type' === $taxonomy->name
&& 'edit' === $request->get_param( 'context' )
) {
$response->data['visibility']['show_ui'] = false;
}
return $response;
}
/**
* Registers the custom post type and taxonomies.
*/
public function register_post_types() {
if ( post_type_exists( 'job_listing' ) ) {
return;
}
$admin_capability = 'manage_job_listings';
$permalink_structure = self::get_permalink_structure();
/**
* Taxonomies
*/
if ( get_option( 'job_manager_enable_categories' ) ) {
$singular = __( 'Job category', 'wp-job-manager' );
$plural = __( 'Job categories', 'wp-job-manager' );
if ( current_theme_supports( 'job-manager-templates' ) ) {
$rewrite = [
'slug' => $permalink_structure['category_rewrite_slug'],
'with_front' => false,
'hierarchical' => false,
];
$public = true;
} else {
$rewrite = false;
$public = false;
}
register_taxonomy(
'job_listing_category',
apply_filters( 'register_taxonomy_job_listing_category_object_type', [ 'job_listing' ] ),
apply_filters(
'register_taxonomy_job_listing_category_args',
[
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => $plural,
'labels' => [
'name' => $plural,
'singular_name' => $singular,
'menu_name' => ucwords( $plural ),
// translators: Placeholder %s is the plural label of the job listing category taxonomy type.
'search_items' => sprintf( __( 'Search %s', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the plural label of the job listing category taxonomy type.
'all_items' => sprintf( __( 'All %s', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the singular label of the job listing category taxonomy type.
'parent_item' => sprintf( __( 'Parent %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing category taxonomy type.
'parent_item_colon' => sprintf( __( 'Parent %s:', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing category taxonomy type.
'edit_item' => sprintf( __( 'Edit %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing category taxonomy type.
'update_item' => sprintf( __( 'Update %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing category taxonomy type.
'add_new_item' => sprintf( __( 'Add New %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing category taxonomy type.
'new_item_name' => sprintf( __( 'New %s Name', 'wp-job-manager' ), $singular ),
],
'show_ui' => true,
'show_tagcloud' => false,
'public' => $public,
'capabilities' => [
'manage_terms' => $admin_capability,
'edit_terms' => $admin_capability,
'delete_terms' => $admin_capability,
'assign_terms' => $admin_capability,
],
'rewrite' => $rewrite,
'show_in_rest' => true,
'rest_base' => 'job-categories',
]
)
);
}
if ( get_option( 'job_manager_enable_types' ) ) {
$singular = __( 'Job type', 'wp-job-manager' );
$plural = __( 'Job types', 'wp-job-manager' );
if ( current_theme_supports( 'job-manager-templates' ) ) {
$rewrite = [
'slug' => $permalink_structure['type_rewrite_slug'],
'with_front' => false,
'hierarchical' => false,
];
$public = true;
} else {
$rewrite = false;
$public = false;
}
register_taxonomy(
'job_listing_type',
apply_filters( 'register_taxonomy_job_listing_type_object_type', [ 'job_listing' ] ),
apply_filters(
'register_taxonomy_job_listing_type_args',
[
'hierarchical' => true,
'label' => $plural,
'labels' => [
'name' => $plural,
'singular_name' => $singular,
'menu_name' => ucwords( $plural ),
// translators: Placeholder %s is the plural label of the job listing job type taxonomy type.
'search_items' => sprintf( __( 'Search %s', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the plural label of the job listing job type taxonomy type.
'all_items' => sprintf( __( 'All %s', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the singular label of the job listing job type taxonomy type.
'parent_item' => sprintf( __( 'Parent %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing job type taxonomy type.
'parent_item_colon' => sprintf( __( 'Parent %s:', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing job type taxonomy type.
'edit_item' => sprintf( __( 'Edit %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing job type taxonomy type.
'update_item' => sprintf( __( 'Update %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing job type taxonomy type.
'add_new_item' => sprintf( __( 'Add New %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing job type taxonomy type.
'new_item_name' => sprintf( __( 'New %s Name', 'wp-job-manager' ), $singular ),
],
'show_ui' => true,
'show_tagcloud' => false,
'public' => $public,
'capabilities' => [
'manage_terms' => $admin_capability,
'edit_terms' => $admin_capability,
'delete_terms' => $admin_capability,
'assign_terms' => $admin_capability,
],
'rewrite' => $rewrite,
'show_in_rest' => true,
'rest_base' => 'job-types',
'meta_box_sanitize_cb' => [ $this, 'sanitize_job_type_meta_box_input' ],
]
)
);
if ( function_exists( 'wpjm_job_listing_employment_type_enabled' ) && wpjm_job_listing_employment_type_enabled() ) {
register_meta(
'term',
'employment_type',
[
'object_subtype' => 'job_listing_type',
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'description' => esc_html__( 'Employment Type', 'wp-job-manager' ),
'sanitize_callback' => [ $this, 'sanitize_employment_type' ],
]
);
}
}
/**
* Post types
*/
$singular = __( 'Job', 'wp-job-manager' );
$plural = __( 'Jobs', 'wp-job-manager' );
/**
* Set whether to add archive page support when registering the job listing post type.
*
* @since 1.30.0
*
* @param bool $enable_job_archive_page
*/
if ( apply_filters( 'job_manager_enable_job_archive_page', current_theme_supports( 'job-manager-templates' ) ) ) {
$has_archive = $permalink_structure['jobs_archive_rewrite_slug'];
} else {
$has_archive = false;
}
$rewrite = [
'slug' => $permalink_structure['job_rewrite_slug'],
'with_front' => false,
'feeds' => true,
'pages' => false,
];
register_post_type(
'job_listing',
apply_filters(
'register_post_type_job_listing',
[
'labels' => [
'name' => $plural,
'singular_name' => $singular,
'menu_name' => __( 'Job Listings', 'wp-job-manager' ),
// translators: Placeholder %s is the plural label of the job listing post type.
'all_items' => sprintf( __( 'All %s', 'wp-job-manager' ), $plural ),
'add_new' => __( 'Add New', 'wp-job-manager' ),
// translators: Placeholder %s is the singular label of the job listing post type.
'add_new_item' => sprintf( __( 'Add %s', 'wp-job-manager' ), $singular ),
'edit' => __( 'Edit', 'wp-job-manager' ),
// translators: Placeholder %s is the singular label of the job listing post type.
'edit_item' => sprintf( __( 'Edit %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing post type.
'new_item' => sprintf( __( 'New %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing post type.
'view' => sprintf( __( 'View %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing post type.
'view_item' => sprintf( __( 'View %s', 'wp-job-manager' ), $singular ),
// translators: Placeholder %s is the singular label of the job listing post type.
'search_items' => sprintf( __( 'Search %s', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the singular label of the job listing post type.
'not_found' => sprintf( __( 'No %s found', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the plural label of the job listing post type.
'not_found_in_trash' => sprintf( __( 'No %s found in trash', 'wp-job-manager' ), $plural ),
// translators: Placeholder %s is the singular label of the job listing post type.
'parent' => sprintf( __( 'Parent %s', 'wp-job-manager' ), $singular ),
'featured_image' => __( 'Company Logo', 'wp-job-manager' ),
'set_featured_image' => __( 'Set company logo', 'wp-job-manager' ),
'remove_featured_image' => __( 'Remove company logo', 'wp-job-manager' ),
'use_featured_image' => __( 'Use as company logo', 'wp-job-manager' ),
],
// translators: Placeholder %s is the plural label of the job listing post type.
'description' => sprintf( __( 'This is where you can create and manage %s.', 'wp-job-manager' ), $plural ),
'public' => true,
'show_ui' => true,
'capability_type' => 'job_listing',
'map_meta_cap' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => false,
'rewrite' => $rewrite,
'query_var' => true,
'supports' => [ 'title', 'editor', 'custom-fields', 'publicize', 'thumbnail', 'author' ],
'has_archive' => $has_archive,
'show_in_nav_menus' => false,
'delete_with_user' => true,
'show_in_rest' => true,
'rest_base' => 'job-listings',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'template' => [ [ 'core/freeform' ] ],
'template_lock' => 'all',
'menu_position' => 30,
]
)
);
/**
* Feeds
*/
add_feed( self::get_job_feed_name(), [ $this, 'job_feed' ] );
/**
* Post status
*/
register_post_status(
'expired',
[
'label' => _x( 'Expired', 'post status', 'wp-job-manager' ),
'public' => true,
'protected' => true,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
// translators: Placeholder %s is the number of expired posts of this type.
'label_count' => _n_noop( 'Expired <span class="count">(%s)</span>', 'Expired <span class="count">(%s)</span>', 'wp-job-manager' ),
]
);
register_post_status(
'preview',
[
'label' => _x( 'Preview', 'post status', 'wp-job-manager' ),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => true,
// translators: Placeholder %s is the number of posts in a preview state.
'label_count' => _n_noop( 'Preview <span class="count">(%s)</span>', 'Preview <span class="count">(%s)</span>', 'wp-job-manager' ),
]
);
}
/**
* Change label for admin menu item to show number of Job Listing items pending approval.
*/
public function admin_head() {
global $menu;
$pending_jobs = WP_Job_Manager_Cache_Helper::get_listings_count();
// No need to go further if no pending jobs, menu is not set, or is not an array.
if ( empty( $pending_jobs ) || empty( $menu ) || ! is_array( $menu ) ) {
return;
}
// Try to pull menu_name from post type object to support themes/plugins that change the menu string.
$post_type = get_post_type_object( 'job_listing' );
$plural = isset( $post_type->labels, $post_type->labels->menu_name ) ? $post_type->labels->menu_name : __( 'Job Listings', 'wp-job-manager' );
foreach ( $menu as $key => $menu_item ) {
if ( strpos( $menu_item[0], $plural ) === 0 ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Only way to add pending listing count.
$menu[ $key ][0] .= " <span class='awaiting-mod update-plugins count-" . esc_attr( $pending_jobs ) . "'><span class='pending-count'>" . absint( number_format_i18n( $pending_jobs ) ) . '</span></span>';
break;
}
}
}
/**
* Filter the post content of job listings.
*
* @since 1.33.0
* @param string $post_content Post content to filter.
*/
public static function output_kses_post( $post_content ) {
echo wp_kses( $post_content, self::kses_allowed_html() );
}
/**
* Returns the expanded set of tags allowed in job listing content.
*
* @since 1.33.0
* @return string
*/
private static function kses_allowed_html() {
/**
* Change the allowed tags in job listing content.
*
* @since 1.33.0
*
* @param array $allowed_html Tags allowed in job listing posts.
*/
return apply_filters(
'job_manager_kses_allowed_html',
array_replace_recursive( // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_replace_recursiveFound
wp_kses_allowed_html( 'post' ),
[
'iframe' => [
'src' => true,
'width' => true,
'height' => true,
'frameborder' => true,
'marginwidth' => true,
'marginheight' => true,
'scrolling' => true,
'title' => true,
'allow' => true,
'allowfullscreen' => true,
],
]
)
);
}
/**
* Sanitize job type meta box input data from WP admin.
*
* @param WP_Taxonomy $taxonomy Taxonomy being sterilized.
* @param mixed $input Raw term data from the 'tax_input' field.
* @return int[]|int
*/
public function sanitize_job_type_meta_box_input( $taxonomy, $input ) {
if ( is_array( $input ) ) {
return array_map( 'intval', $input );
}
return intval( $input );
}
/**
* Toggles content filter on and off.
*
* @param bool $enable
*/
private function job_content_filter( $enable ) {
if ( ! $enable ) {
remove_filter( 'the_content', [ $this, 'job_content' ] );
} else {
add_filter( 'the_content', [ $this, 'job_content' ] );
}
}
/**
* Adds extra content before/after the post for single job listings.
*
* @param string $content
* @return string
*/
public function job_content( $content ) {
global $post;
if (
! is_singular( 'job_listing' ) ||
! in_the_loop() ||
'job_listing' !== $post->post_type ||
( post_password_required() && ! is_super_admin() )
) {
return $content;
}
ob_start();
$this->job_content_filter( false );
do_action( 'job_content_start' );
get_job_manager_template_part( 'content-single', 'job_listing' );
do_action( 'job_content_end' );
$this->job_content_filter( true );
return apply_filters( 'job_manager_single_job_content', ob_get_clean(), $post );
}
/**
* Generates the RSS feed for Job Listings.
*/
public function job_feed() {
global $job_manager_keyword;
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Input used to filter public data in feed.
$input_posts_per_page = isset( $_GET['posts_per_page'] ) ? absint( $_GET['posts_per_page'] ) : 10;
$input_search_location = isset( $_GET['search_location'] ) ? sanitize_text_field( wp_unslash( $_GET['search_location'] ) ) : false;
if ( isset( $_GET['job_types'] ) ) {
$sanitized_job_types = sanitize_text_field( wp_unslash( $_GET['job_types'] ) );
$input_job_types = empty( $sanitized_job_types ) ? false : explode( ',', $sanitized_job_types );
} else {
$input_job_types = false;
}
if ( isset( $_GET['job_categories'] ) ) {
$sanitized_job_categories = sanitize_text_field( wp_unslash( $_GET['job_categories'] ) );
$input_job_categories = empty( $sanitized_job_categories ) ? false : explode( ',', $sanitized_job_categories );
} else {
$input_job_categories = false;
}
$job_manager_keyword = isset( $_GET['search_keywords'] ) ? sanitize_text_field( wp_unslash( $_GET['search_keywords'] ) ) : '';
// phpcs:enable WordPress.Security.NonceVerification.Recommended
$query_args = [
'post_type' => 'job_listing',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $input_posts_per_page,
'paged' => absint( get_query_var( 'paged', 1 ) ),
'tax_query' => [],
'meta_query' => [],
];
if ( ! empty( $input_search_location ) ) {
$location_meta_keys = [ 'geolocation_formatted_address', '_job_location', 'geolocation_state_long' ];
$location_search = [ 'relation' => 'OR' ];
$locations = explode( ';', $input_search_location );
foreach ( $locations as $location ) {
$location = trim( $location );
if ( ! empty( $location ) ) {
$location_subquery = [ 'relation' => 'OR' ];
foreach ( $location_meta_keys as $meta_key ) {
$location_subquery[] = [
'key' => $meta_key,
'value' => $location,
'compare' => 'like',
];
}
$location_search[] = $location_subquery;
}
}
$query_args['meta_query'][] = $location_search;
}
// Hide filled positions from the job feed.
if ( 1 === absint( get_option( 'job_manager_hide_filled_positions' ) ) ) {
$query_args['meta_query'][] = [
'key' => '_filled',
'value' => '0',
];
}
if ( ! empty( $input_job_types ) ) {
$query_args['tax_query'][] = [
'taxonomy' => 'job_listing_type',
'field' => 'slug',
'terms' => $input_job_types + [ 0 ],
];
}
if ( ! empty( $input_job_categories ) ) {
$cats = $input_job_categories + [ 0 ];
$field = is_numeric( $cats ) ? 'term_id' : 'slug';
$operator = 'all' === get_option( 'job_manager_category_filter_type', 'all' ) && count( $cats ) > 1 ? 'AND' : 'IN';
$query_args['tax_query'][] = [
'taxonomy' => 'job_listing_category',
'field' => $field,
'terms' => $cats,
'include_children' => 'AND' !== $operator,
'operator' => $operator,
];
}
if ( ! empty( $job_manager_keyword ) ) {
$query_args['s'] = $job_manager_keyword;
add_filter( 'posts_search', 'get_job_listings_keyword_search' );
}
if ( empty( $query_args['meta_query'] ) ) {
unset( $query_args['meta_query'] );
}
if ( empty( $query_args['tax_query'] ) ) {
unset( $query_args['tax_query'] );
}
// phpcs:ignore WordPress.WP.DiscouragedFunctions
query_posts( apply_filters( 'job_feed_args', $query_args ) );
add_action( 'rss2_ns', [ $this, 'job_feed_namespace' ] );
add_action( 'rss2_item', [ $this, 'job_feed_item' ] );
do_feed_rss2( false );
remove_filter( 'posts_search', 'get_job_listings_keyword_search' );
}
/**
* Retrieve and return the post IDs of any job listings marked as filled.
*
* @return array Array of filled job listing post IDs.
*/
public function get_filled_job_listings(): array {
$filled_jobs_transient = get_transient( 'hide_filled_jobs_transient' );
if ( false === $filled_jobs_transient ) {
$filled_jobs_transient = get_posts(
[
'post_status' => 'publish',
'post_type' => 'job_listing',
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => '_filled',
'value' => '1',
'compare' => '=',
],
],
]
);
set_transient( 'hide_filled_jobs_transient', $filled_jobs_transient, DAY_IN_SECONDS );
}
return $filled_jobs_transient;
}
/**
* Maybe exclude filled job listings from search and archive pages.
*
* @param $query WP_Query $query
*
* @return void
*/
public function maybe_hide_filled_job_listings( WP_Query $query ): void {
$hide_filled_positions = get_option( 'job_manager_hide_filled_positions' );
if ( ! $hide_filled_positions ) {
return;
}
if (
! is_admin()
&& $query->is_main_query()
&& ( $query->is_search() || $query->is_archive() )
) {
$query->set( 'post__not_in', $this->get_filled_job_listings() );
}
}
/**
* Maybe exclude expired job listings from search and archive pages.
*
* @param $query WP_Query $query
*
* @return void
*/
public function maybe_hide_expired_job_listings( WP_Query $query ): void {
$hide_expired = get_option( 'job_manager_hide_expired' );
if ( ! $hide_expired ) {
return;
}
if (
! is_admin()
&& $query->is_main_query()
&& ( $query->is_search() || $query->is_archive() )
) {
$this->make_expired_private();
add_action( 'posts_selection', [ $this, 'make_expired_public' ] );
}
}
/**
* Make the expired post status public.
*
* @return void
* @internal
*/
public function make_expired_public(): void {
global $wp_post_statuses;
if ( isset( $wp_post_statuses['expired'] ) ) {
$wp_post_statuses['expired']->public = true;
}
}
/**
* Make the expired post status private.
*
* @return void
* @internal
*/
public function make_expired_private() {
global $wp_post_statuses;
if ( isset( $wp_post_statuses['expired'] ) ) {
$wp_post_statuses['expired']->public = false;
}
}
/**
* Adds query arguments in order to make sure that the feed properly queries the 'job_listing' type.
*
* @param WP_Query $wp
*/
public function add_feed_query_args( $wp ) {
// Let's leave if not the job feed.
if ( ! isset( $wp->query_vars['feed'] ) || self::get_job_feed_name() !== $wp->query_vars['feed'] ) {
return;
}
// Leave if not a feed.
if ( false === $wp->is_feed ) {
return;
}
// If the post_type was already set, let's get out of here.
if ( isset( $wp->query_vars['post_type'] ) && ! empty( $wp->query_vars['post_type'] ) ) {
return;
}
$wp->query_vars['post_type'] = 'job_listing';
}
/**
* Adds a custom namespace to the job feed.
*/
public function job_feed_namespace() {
echo 'xmlns:job_listing="' . esc_url( site_url() ) . '"' . "\n";
}
/**
* Adds custom data to the job feed.
*/
public function job_feed_item() {
$post_id = get_the_ID();
$location = get_the_job_location( $post_id );
$company = get_the_company_name( $post_id );
$job_types = wpjm_get_the_job_types( $post_id );
$salary = get_the_job_salary( $post_id );
if ( $location ) {
echo '<job_listing:location><![CDATA[' . esc_html( $location ) . "]]></job_listing:location>\n";
}
if ( ! empty( $job_types ) ) {
$job_types_names = implode( ', ', wp_list_pluck( $job_types, 'name' ) );
echo '<job_listing:job_type><![CDATA[' . esc_html( $job_types_names ) . "]]></job_listing:job_type>\n";
}
if ( $company ) {
echo '<job_listing:company><![CDATA[' . esc_html( $company ) . "]]></job_listing:company>\n";
}
if ( $salary ) {
echo '<job_listing:salary><![CDATA[' . esc_html( $salary ) . "]]></job_listing:salary>\n";
}
/**
* Fires at the end of each job RSS feed item.
*
* @param int $post_id The post ID of the job.
*/
do_action( 'job_feed_item', $post_id );
}
/**
* Maintenance task to expire jobs.
*/
public function check_for_expired_jobs() {
$expired_date_comparison = current_datetime();
if ( ! $this->jobs_expires_end_of_day() ) {
$expired_date_comparison = $expired_date_comparison->add( new DateInterval( 'P1D' ) );
}
// Change status to expired.
$job_ids = get_posts(
[
'post_type' => 'job_listing',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => [
'relation' => 'AND',
[
'key' => '_job_expires',
'value' => 0,
'compare' => '>',
],
[
'key' => '_job_expires',
'value' => $expired_date_comparison->format( 'Y-m-d' ),
'compare' => '<',
],
],
]
);
if ( $job_ids ) {
foreach ( $job_ids as $job_id ) {
$job_data = [];
$job_data['ID'] = $job_id;
$job_data['post_status'] = 'expired';
wp_update_post( $job_data );
}
}
// Delete old expired jobs.
/**
* Set whether or not we should delete expired jobs after a certain amount of time.
*
* @since 1.0.0
*
* @param bool $delete_expired_jobs Whether we should delete expired jobs after a certain amount of time. Defaults to false.
*/
if ( apply_filters( 'job_manager_delete_expired_jobs', false ) ) {
/**
* Days to preserve expired job listings before deleting them.
*
* @since 1.0.0
*
* @param int $delete_expired_jobs_days Number of days to preserve expired job listings before deleting them.
*/
$delete_expired_jobs_days = apply_filters( 'job_manager_delete_expired_jobs_days', 30 );
$date_cutoff = current_datetime()->sub( new DateInterval( 'P' . $delete_expired_jobs_days . 'D' ) );
$job_ids = get_posts(
[
'post_type' => 'job_listing',
'post_status' => 'expired',
'fields' => 'ids',
'date_query' => [
[
'column' => 'post_modified',
'before' => $date_cutoff->format( 'Y-m-d' ),
],
],
'posts_per_page' => -1,
]
);
if ( $job_ids ) {
foreach ( $job_ids as $job_id ) {
wp_trash_post( $job_id );
}
}
}
}
/**
* Deletes old previewed jobs after 30 days to keep the DB clean.
*/
public function delete_old_previews() {
// Delete old jobs stuck in preview.
$date_cutoff = current_datetime()->sub( new DateInterval( 'P30D' ) );
$job_ids = get_posts(
[
'post_type' => 'job_listing',
'post_status' => 'preview',
'fields' => 'ids',
'date_query' => [
[
'column' => 'post_modified',
'before' => $date_cutoff->format( 'Y-m-d' ),
],
],
'posts_per_page' => -1,
]
);
if ( $job_ids ) {
foreach ( $job_ids as $job_id ) {
wp_delete_post( $job_id, true );
}
}
}
/**
* Typo wrapper for `set_expiry` method.
*
* @param WP_Post $post
* @since 1.0.0
* @deprecated 1.0.1
*/
public function set_expirey( $post ) {
_deprecated_function( __METHOD__, '1.0.1', 'WP_Job_Manager_Post_Types::set_expiry' );
$this->set_expiry( $post );
}
/**
* Handle tasks related to transition job listing post statuses.
*
* @access private
*
* @param string $new_status The new post status.
* @param string $old_status The old post status.
* @param WP_Post $post The post object.
*/
public function transition_post_status( $new_status, $old_status, $post ) {
if ( 'job_listing' !== $post->post_type ) {
return;
}
$published_post_statuses = [ 'publish' ];
$submitted_post_statuses = [ 'publish', 'pending' ];
// If we're coming to a published post status from a non-published post status, set the expiry.
if (
'new' !== $old_status
&& in_array( $new_status, $published_post_statuses, true )
&& ! in_array( $old_status, $published_post_statuses, true )
) {
$this->set_expiry( $post );
}
// If we're transitioning to a submitted post status on a public submission, fire submitted hook.
if (
get_post_meta( $post->ID, '_public_submission', true )
&& in_array( $new_status, $submitted_post_statuses, true )
&& ! in_array( $old_status, $submitted_post_statuses, true )
) {
$this->submitted_job_ids[] = $post->ID;
add_action( 'shutdown', [ $this, 'ensure_job_submission_action_triggered' ], 1 );
}
}