Created
October 6, 2023 12:16
-
-
Save chrishow/b885ac5b5a498d0091ca34c6b980eaf9 to your computer and use it in GitHub Desktop.
Simple class to import jobs from Greenhouse.com API into WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GreenhouseJobs { | |
public static function get_most_recent_jobs($how_many, $categories = NULL) { | |
$all_jobs = static::get_jobs(); | |
$jobs = []; | |
foreach($all_jobs->departments as $department) { | |
foreach($department->jobs as $job) { | |
$job->department = $department->name; | |
$job->timestamp = (new DateTimeImmutable($job->updated_at))->getTimestamp(); | |
if($categories) { | |
// Only add this job if the category is selected | |
$index = array_search(trim($job->department), array_column($categories, 'post_title')); | |
if($index !== FALSE) { | |
$jobs[] = $job; | |
} | |
} else { | |
// Add all jobs | |
$jobs[] = $job; | |
} | |
} | |
} | |
// Order by reverse date order | |
usort($jobs, function($a, $b) { | |
return $b->timestamp <=> $a->timestamp; | |
}); | |
$return_categories = []; | |
foreach($jobs as $index => $job) { | |
$existing_department = array_key_exists($job->department, $return_categories); | |
if($existing_department === FALSE) { | |
// New category array and add this job | |
$return_categories[$job->department] = [$job]; | |
} else { | |
// Add to existing array of jobs in this category | |
$return_categories[$job->department][] = $job; | |
} | |
if($index == ($how_many - 1)) { // We have reached the number of jobs requested | |
return $return_categories; | |
} | |
} | |
return $return_categories; | |
} | |
public static function get_jobs() { | |
// URL is public API endpoint, something like | |
// https://boards-api.greenhouse.io/v1/boards/wayve/departments | |
$jobs = get_transient(GREENHOUSE_JOBS_URL); | |
if($jobs === FALSE) { | |
// error_log('no data in cache'); | |
try { | |
$json = static::load_jobs_from_api(); | |
$jobs = json_decode($json); | |
if(is_object($jobs)) { | |
error_log('Loaded jobs directly from API'); | |
set_transient(GREENHOUSE_JOBS_URL, $jobs, 10 * MINUTE_IN_SECONDS ); | |
static::update_job_categories($jobs); | |
} | |
} catch (Exception $e) { | |
error_log($e); | |
} | |
} | |
return $jobs; | |
} | |
private static function load_jobs_from_api() { | |
$jobs = file_get_contents(GREENHOUSE_JOBS_URL); | |
return $jobs; | |
} | |
private static function update_job_categories($jobs) { | |
foreach($jobs->departments as $department) { | |
static::update_job_category($department); | |
} | |
} | |
private static function update_job_category($department) { | |
// Does a department exist with this ID? | |
$category = get_posts(array( | |
'numberposts' => 1, | |
'post_type' => 'job_categories', | |
'meta_key' => 'greenhouse_id', | |
'meta_value' => $department->id, | |
)); | |
if(empty($category)) { | |
// Add new job category | |
$new_category = array( | |
'post_type' => 'job_categories', | |
'post_title' => $department->name, | |
'post_status' => 'publish', | |
'post_author' => 1, | |
); | |
$category_id = wp_insert_post($new_category); | |
update_field('greenhouse_id', $department->id, $category_id); | |
} else { | |
if($category[0]->post_title !== $department->name) { | |
$category[0]->post_title = $department->name; | |
wp_update_post($category[0]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment