Created
November 27, 2024 21:02
-
-
Save bhubbard/fbb6a8b181550c80ea38d4beaf3fed6e to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Plugin Name: WP Workday API | |
* Plugin URI: https://example.com/wp-workday-api | |
* Description: A WordPress plugin that provides an API wrapper for Workday. | |
* Version: 1.0.0 | |
* Author: Your Name | |
* Author URI: https://example.com | |
* License: GPL2 | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
* Text Domain: wp-workday-api | |
* Domain Path: /languages | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WP_Workday_API' ) ) : | |
/** | |
* Main WP_Workday_API Class. | |
* | |
* @since 1.0.0 | |
*/ | |
final class WP_Workday_API { | |
/** | |
* Version of the plugin. | |
* | |
* @var string | |
*/ | |
public $version = '1.0.0'; | |
/** | |
* Instance of this class. | |
* | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* The Workday API base URL. | |
* | |
* @var string | |
*/ | |
public $api_base_url = 'https://wd3-impl-services1.workday.com/ccx/service/your_tenant/Human_Resources/v34.0'; // Replace with your Workday API endpoint | |
/** | |
* Workday API credentials. | |
* | |
* @var array | |
*/ | |
private $credentials; | |
/** | |
* Initialize the plugin public actions. | |
*/ | |
private function __construct() { | |
// Load plugin text domain. | |
add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); | |
// Set the Workday API credentials. | |
$this->credentials = array( | |
'username' => 'YOUR_USERNAME', // Replace with your Workday username | |
'password' => 'YOUR_PASSWORD', // Replace with your Workday password | |
'client_id' => 'YOUR_CLIENT_ID', // Replace with your Workday client ID | |
'client_secret' => 'YOUR_CLIENT_SECRET', // Replace with your Workday client secret | |
'tenant_id' => 'YOUR_TENANT_ID', // Replace with your Workday tenant ID | |
); | |
// Register any necessary hooks or actions. | |
} | |
/** | |
* Return an instance of this class. | |
* | |
* @return object A single instance of this class. | |
*/ | |
public static function get_instance() { | |
// If the single instance hasn't been set, set it now. | |
if ( null === self::$instance ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Load the plugin text domain for translation. | |
*/ | |
public function load_plugin_textdomain() { | |
load_plugin_textdomain( 'wp-workday-api', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | |
} | |
/** | |
* Magic getter for our object. Allows getting but not setting. | |
* | |
* @param string $field | |
* @return mixed | |
*/ | |
public function __get( $field ) { | |
return $this->$field; | |
} | |
/** | |
* Make a request to the Workday API. | |
* | |
* @param string $endpoint The API endpoint. | |
* @param string $method The HTTP method (GET, POST, PUT, DELETE). | |
* @param array $params The request parameters. | |
* @return array The API response. | |
*/ | |
public function make_request( $endpoint, $method = 'GET', $params = array() ) { | |
$url = $this->api_base_url . $endpoint; | |
$args = array( | |
'method' => $method, | |
'headers' => array( | |
'Authorization' => 'Basic ' . base64_encode( $this->credentials['client_id'] . ':' . $this->credentials['client_secret'] ), | |
'Content-Type' => 'application/json', | |
), | |
); | |
if ( 'GET' === $method ) { | |
$url = add_query_arg( $params, $url ); | |
} else { | |
$args['body'] = json_encode( $params ); | |
} | |
$response = wp_remote_request( $url, $args ); | |
if ( is_wp_error( $response ) ) { | |
// Handle WP_Error. | |
error_log( 'Workday API Error: ' . $response->get_error_message() ); | |
return $response; | |
} | |
$status_code = wp_remote_retrieve_response_code( $response ); | |
if ( $status_code < 200 || $status_code >= 300 ) { | |
// Handle API errors. | |
$error_message = wp_remote_retrieve_body( $response ); | |
error_log( 'Workday API Error (' . $status_code . '): ' . $error_message ); | |
return new WP_Error( 'workday_api_error', $error_message, array( 'status_code' => $status_code ) ); | |
} | |
return json_decode( wp_remote_retrieve_body( $response ) ); | |
} | |
/** | |
* Get a list of workers. | |
* | |
* @param array $params Optional parameters (e.g., page, count). | |
* @return array The list of workers. | |
*/ | |
public function get_workers( $params = array() ) { | |
return $this->make_request( '/Workers', 'GET', $params ); | |
} | |
/** | |
* Get a single worker by ID. | |
* | |
* @param string $worker_id The ID of the worker. | |
* @return array The worker data. | |
*/ | |
public function get_worker( $worker_id ) { | |
return $this->make_request( '/Workers/' . $worker_id ); | |
} | |
// Add other helper methods for specific Workday API functionalities, e.g.,: | |
// public function get_organizations() { ... } | |
// public function get_positions() { ... } | |
// public function submit_job_application( $data ) { ... } | |
// ... and so on | |
} | |
endif; | |
/** | |
* Returns the main instance of WP_Workday_API. | |
* | |
* @return WP_Workday_API The main instance. | |
*/ | |
function WP_Workday_API() { | |
return WP_Workday_API::get_instance(); | |
} | |
// Get WP_Workday_API Running. | |
WP_Workday_API(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment