Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created November 27, 2024 22:05
Show Gist options
  • Save bhubbard/826ea31d1f70308a334d9986d5555f4e to your computer and use it in GitHub Desktop.
Save bhubbard/826ea31d1f70308a334d9986d5555f4e to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Cherwell API
* Plugin URI: https://example.com/wp-cherwell-api
* Description: A WordPress plugin that provides an API wrapper for Cherwell.
* 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-cherwell-api
* Domain Path: /languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Cherwell_API' ) ) :
/**
* Main WP_Cherwell_API Class.
*
* @since 1.0.0
*/
final class WP_Cherwell_API {
/**
* Version of the plugin.
*
* @var string
*/
public $version = '1.0.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* The Cherwell API base URL.
*
* @var string
*/
public $api_base_url = 'YOUR_CHERWELL_API_BASE_URL'; // Replace with your Cherwell API base URL
/**
* Cherwell API credentials.
*
* @var array
*/
private $credentials;
/**
* Cherwell API token.
*
* @var string
*/
private $api_token;
/**
* Initialize the plugin public actions.
*/
private function __construct() {
// Load plugin text domain.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Set the Cherwell API credentials.
$this->credentials = array(
'username' => 'YOUR_CHERWELL_USERNAME', // Replace with your Cherwell username
'password' => 'YOUR_CHERWELL_PASSWORD', // Replace with your Cherwell password
);
// Get the API token on initialization.
$this->get_api_token();
// 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-cherwell-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;
}
/**
* Get the Cherwell API token.
*
* @return string|WP_Error The API token or WP_Error on failure.
*/
private function get_api_token() {
// If the token is already set, return it.
if ( $this->api_token ) {
return $this->api_token;
}
$url = $this->api_base_url . '/token';
$args = array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
'body' => array(
'grant_type' => 'password',
'client_id' => 'Cherwell Client', // This might need to be adjusted based on your Cherwell setup
'username' => $this->credentials['username'],
'password' => $this->credentials['password'],
),
);
$response = wp_remote_post( $url, $args );
if ( is_wp_error( $response ) ) {
// Handle WP_Error.
error_log( 'Cherwell API Error: ' . $response->get_error_message() );
return $response;
}
$status_code = wp_remote_retrieve_response_code( $response );
if ( $status_code !== 200 ) {
// Handle API errors.
$error_message = wp_remote_retrieve_body( $response );
error_log( 'Cherwell API Error (' . $status_code . '): ' . $error_message );
return new WP_Error( 'cherwell_api_error', $error_message, array( 'status_code' => $status_code ) );
}
$response_body = json_decode( wp_remote_retrieve_body( $response ) );
$this->api_token = $response_body->access_token;
return $this->api_token;
}
/**
* Make a request to the Cherwell 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' => 'Bearer ' . $this->get_api_token(),
'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( 'Cherwell 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( 'Cherwell API Error (' . $status_code . '): ' . $error_message );
return new WP_Error( 'cherwell_api_error', $error_message, array( 'status_code' => $status_code ) );
}
return json_decode( wp_remote_retrieve_body( $response ) );
}
/**
* Get a list of business objects.
*
* @return array The list of business objects.
*/
public function get_business_objects() {
return $this->make_request( '/api/V1/getbusinessobjects' );
}
/**
* Get a business object by name.
*
* @param string $busObName The name of the business object.
* @return array The business object data.
*/
public function get_business_object( $busObName ) {
return $this->make_request( '/api/V1/getbusinessobject/' . $busObName );
}
/**
* Get a list of incidents.
*
* @param array $params Optional parameters (e.g., pageSize, pageNumber, sortBy, filters).
* @return array The list of incidents.
*/
public function get_incidents( $params = array() ) {
return $this->make_request( '/api/V1/getsearchresults', 'POST', array(
'busObId' => 'YOUR_INCIDENT_BUSOB_ID', // Replace with the ID of your Incident business object
'filters' => array(
// Add any desired filters here
),
'includeAllProperties' => true,
'pageSize' => 10,
'pageNumber' => 1,
'sortBy' => 'CreatedDateTime',
'sortOrder' => 'descending',
) );
}
// Add other helper methods for specific Cherwell API functionalities, e.g.,:
// public function create_incident( $data ) { ... }
// public function get_incident_by_public_id( $public_id ) { ... }
// public function update_incident( $incident_id, $data ) { ... }
// ... and so on
}
endif;
/**
* Returns the main instance of WP_Cherwell_API.
*
* @return WP_Cherwell_API The main instance.
*/
function WP_Cherwell_API() {
return WP_Cherwell_API::get_instance();
}
// Get WP_Cherwell_API Running.
WP_Cherwell_API();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment