This repository has been archived by the owner on Jul 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwp-job-manager-addon-regions.php
124 lines (92 loc) · 3.39 KB
/
wp-job-manager-addon-regions.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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_Job_Manager_Addon_Regions {
static $job_listing_region_tax = 'job_listing_region';
static $last_job_done = 0;
function __construct() {
// run after the geogcode has been set
add_action( 'job_manager_update_job_data', array( $this, 'set_location_tax' ), 30 );
add_action( 'job_manager_job_location_edited', array( $this, 'set_location_tax' ), 30 );
add_action( 'bullhorn_sync_complete', array( $this, 'set_location_tax' ) );
}
public static function set_location_tax( $job_id ) {
if ( self::$last_job_done === $job_id ) {
return;
}
if ( apply_filters( 'job_manager_geolocation_enabled', true ) ) {
$meta = get_post_meta( $job_id );
$cat_ids = array();
$parent_term_id = 1;
$slug = '';
if ( isset( $meta['Country'] ) || isset( $meta['geolocation_country_long'] ) ) {
$geo = isset( $meta['Country'] ) ? $meta['Country'][0] : $meta['geolocation_country_long'][0];
$term = self::get_region_term_by_name_and_parent( $geo, 0 );
if ( false !== $term ) {
$cat_ids[] = $term->term_id;
$parent_term_id = $term->term_id;
$slug = $term->slug;
} else {
$parent_term = wp_insert_term( $geo, self::$job_listing_region_tax );
$term = self::get_region_term_by_name_and_parent( $geo, 0 );
$slug = $term->slug;
$parent_term_id = $parent_term['term_id'];
$cat_ids[] = $parent_term_id;
}
}
if ( isset( $meta['state'] ) || isset( $meta['geolocation_state_long'] ) ) {
$geo = isset( $meta['state'] ) ? $meta['state'][0] : $meta['geolocation_state_long'][0];
$term = self::get_region_term_by_name_and_parent( $geo, $parent_term_id );
if ( false !== $term ) {
$cat_ids[] = $term->term_id;
$parent_term_id = $term->term_id;
$slug = $term->slug;
} else {
$args = array(
'parent' => $parent_term_id,
'slug' => $slug . '-' . $geo,
);
$parent_term = wp_insert_term( $geo, self::$job_listing_region_tax, $args );
$term = self::get_region_term_by_name_and_parent( $geo, $parent_term_id );
$slug = $term->slug;
$parent_term_id = $parent_term['term_id'];
$cat_ids[] = $parent_term_id;
}
}
if ( isset( $meta['city'] ) || isset( $meta['geolocation_city'] ) ) {
$geo = isset( $meta['city'] ) ? $meta['city'][0] : $meta['geolocation_city'][0];
$term = self::get_region_term_by_name_and_parent( $geo, $parent_term_id );
if ( false !== $term ) {
$cat_ids[] = $term->term_id;
$parent_term_id = $term->term_id;
} else {
$args = array(
'parent' => $parent_term_id,
'slug' => $slug . '-' . $geo,
);
$parent_term = wp_insert_term( $geo, self::$job_listing_region_tax, $args );
$parent_term_id = $parent_term['term_id'];
$cat_ids[] = $parent_term_id;
}
}
$cat_ids = array_map( 'intval', $cat_ids );
$cat_ids = array_unique( $cat_ids );
wp_set_object_terms( $job_id, $cat_ids, self::$job_listing_region_tax );
}
}
public static function get_region_term_by_name_and_parent( $name, $parent_id ) {
$args = array(
'name' => $name,
'taxonomy' => self::$job_listing_region_tax,
'hide_empty' => false,
'parent' => $parent_id,
);
$terms = get_terms( $args );
if ( empty( $terms ) ) {
return false;
} else {
return $terms[0];
}
}
}