<?php
/*
Plugin Name: Jobvite Job Fetcher
Description: Fetches job data from Jobvite API, saves it as a JSON file in wp-content/jobs directory, and sends an email notification once done.
Version: 1.0
Author: ABCD
*/
// Ensure the code is not executed outside of WordPress
defined('ABSPATH') or die('No script kiddies please!');
// Schedule the custom cron event on plugin activation
register_activation_hook(__FILE__, 'jobvite_job_fetcher_activation');
function jobvite_job_fetcher_activation() {
if (!wp_next_scheduled('jobvite_job_fetcher_cron_event')) {
// Schedule the event to run daily at midnight
wp_schedule_event(strtotime('midnight'), 'daily', 'jobvite_job_fetcher_cron_event');
}
}
// Clear the custom cron event on plugin deactivation
register_deactivation_hook(__FILE__, 'jobvite_job_fetcher_deactivation');
function jobvite_job_fetcher_deactivation() {
wp_clear_scheduled_hook('jobvite_job_fetcher_cron_event');
}
// Hook the custom function to the custom cron event
add_action('jobvite_job_fetcher_cron_event', 'fetch_job_data_from_jobvite');
function fetch_job_data_from_jobvite() {
// URL of the API endpoint
$apiUrl = "https://api.jobvite.com/api/v2/job?api=abcd_jobfeedapi_key&sc=abcdefjhijklmnopq";
// Directory where the JSON file will be saved
$outputDirectory = WP_CONTENT_DIR . '/jobs';
// Check if the directory exists, if not create it
if (!is_dir($outputDirectory)) {
mkdir($outputDirectory, 0755, true);
}
// Path to the old JSON file
$oldFilePath = $outputDirectory . '/jobs.json';
// Delete the old JSON file if it exists
if (file_exists($oldFilePath)) {
unlink($oldFilePath);
}
// Create a new file name with the current date
$date = date('Y-m-d_H-i-s');
$newFilePath = $outputDirectory . '/jobs.json';
// Fetch data from the API
$apiResponse = file_get_contents($apiUrl);
if ($apiResponse === FALSE) {
error_log('Error occurred while fetching data from the API');
return;
}
// Convert API response to JSON format
$jsonData = json_encode(json_decode($apiResponse), JSON_PRETTY_PRINT);
// Save the JSON data to the new file
file_put_contents($newFilePath, $jsonData);
}