Created
November 27, 2024 21:10
-
-
Save bhubbard/ac7d975a334eedd1257fd4a7f9c9acf9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Microsoft Outlook API | |
* Plugin URI: https://example.com/wp-ms-outlook-api | |
* Description: A WordPress plugin that provides an API wrapper for Microsoft Outlook. | |
* 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-ms-outlook-api | |
* Domain Path: /languages | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WP_MS_Outlook_API' ) ) : | |
/** | |
* Main WP_MS_Outlook_API Class. | |
* | |
* @since 1.0.0 | |
*/ | |
final class WP_MS_Outlook_API { | |
/** | |
* Version of the plugin. | |
* | |
* @var string | |
*/ | |
public $version = '1.0.0'; | |
/** | |
* Instance of this class. | |
* | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* The Microsoft Graph API base URL. | |
* | |
* @var string | |
*/ | |
public $api_base_url = 'https://graph.microsoft.com/v1.0/'; | |
/** | |
* Microsoft Outlook API access token. | |
* | |
* @var string | |
*/ | |
private $access_token; | |
/** | |
* Initialize the plugin public actions. | |
*/ | |
private function __construct() { | |
// Load plugin text domain. | |
add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); | |
// Set the Microsoft Outlook API access token. | |
// NOTE: This is a simplified example and assumes you have a way to obtain and refresh the access token. | |
$this->access_token = 'YOUR_MS_OUTLOOK_ACCESS_TOKEN'; // Replace with your actual access 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-ms-outlook-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 Microsoft Graph 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->access_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( 'Microsoft Outlook 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( 'Microsoft Outlook API Error (' . $status_code . '): ' . $error_message ); | |
return new WP_Error( 'ms_outlook_api_error', $error_message, array( 'status_code' => $status_code ) ); | |
} | |
return json_decode( wp_remote_retrieve_body( $response ) ); | |
} | |
/** | |
* Get a list of emails from the user's inbox. | |
* | |
* @param array $params Optional parameters (e.g., $top, $skip, $filter). | |
* @return array The list of emails. | |
*/ | |
public function get_emails( $params = array() ) { | |
return $this->make_request( 'me/mailFolders/inbox/messages', 'GET', $params ); | |
} | |
/** | |
* Get a single email by ID. | |
* | |
* @param string $email_id The ID of the email. | |
* @return array The email data. | |
*/ | |
public function get_email( $email_id ) { | |
return $this->make_request( 'me/messages/' . $email_id ); | |
} | |
/** | |
* Send an email. | |
* | |
* @param string $to The recipient's email address. | |
* @param string $subject The email subject. | |
* @param string $body The email body. | |
* @return array The API response. | |
*/ | |
public function send_email( $to, $subject, $body ) { | |
$endpoint = 'me/sendMail'; | |
$params = array( | |
'message' => array( | |
'subject' => $subject, | |
'body' => array( | |
'contentType' => 'Text', | |
'content' => $body, | |
), | |
'toRecipients' => array( | |
array( | |
'emailAddress' => array( | |
'address' => $to, | |
), | |
), | |
), | |
), | |
); | |
return $this->make_request( $endpoint, 'POST', $params ); | |
} | |
// Add other helper methods for specific Microsoft Outlook API functionalities, e.g.,: | |
// public function create_event( $subject, $start, $end, $attendees ) { ... } | |
// public function get_calendar_events( $params = array() ) { ... } | |
// public function get_contacts( $params = array() ) { ... } | |
// ... and so on | |
} | |
endif; | |
/** | |
* Returns the main instance of WP_MS_Outlook_API. | |
* | |
* @return WP_MS_Outlook_API The main instance. | |
*/ | |
function WP_MS_Outlook_API() { | |
return WP_MS_Outlook_API::get_instance(); | |
} | |
// Get WP_MS_Outlook_API Running. | |
WP_MS_Outlook_API(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment