Created
November 27, 2024 21:09
-
-
Save bhubbard/2c316471f914e613014b81b4c83e0964 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 Microsoft Teams API | |
* Plugin URI: https://example.com/wp-ms-teams-api | |
* Description: A WordPress plugin that provides an API wrapper for Microsoft Teams. | |
* 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-teams-api | |
* Domain Path: /languages | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WP_MS_Teams_API' ) ) : | |
/** | |
* Main WP_MS_Teams_API Class. | |
* | |
* @since 1.0.0 | |
*/ | |
final class WP_MS_Teams_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 Teams 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 Teams 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_TEAMS_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-teams-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 Teams 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 Teams API Error (' . $status_code . '): ' . $error_message ); | |
return new WP_Error( 'ms_teams_api_error', $error_message, array( 'status_code' => $status_code ) ); | |
} | |
return json_decode( wp_remote_retrieve_body( $response ) ); | |
} | |
/** | |
* Get a list of teams. | |
* | |
* @param array $params Optional parameters (e.g., $top, $skip). | |
* @return array The list of teams. | |
*/ | |
public function get_teams( $params = array() ) { | |
return $this->make_request( 'groups?$filter=resourceProvisioningOptions/Any(x:x eq \'Team\')', 'GET', $params ); | |
} | |
/** | |
* Get a single team by ID. | |
* | |
* @param string $team_id The ID of the team. | |
* @return array The team data. | |
*/ | |
public function get_team( $team_id ) { | |
return $this->make_request( 'groups/' . $team_id ); | |
} | |
/** | |
* Send a message to a channel. | |
* | |
* @param string $team_id The ID of the team. | |
* @param string $channel_id The ID of the channel. | |
* @param string $message The message to send. | |
* @return array The API response. | |
*/ | |
public function send_channel_message( $team_id, $channel_id, $message ) { | |
$endpoint = 'teams/' . $team_id . '/channels/' . $channel_id . '/messages'; | |
$params = array( | |
'body' => array( | |
'content' => $message, | |
), | |
); | |
return $this->make_request( $endpoint, 'POST', $params ); | |
} | |
// Add other helper methods for specific Microsoft Teams API functionalities, e.g.,: | |
// public function get_channels( $team_id ) { ... } | |
// public function create_channel( $team_id, $channel_name ) { ... } | |
// public function get_team_members( $team_id ) { ... } | |
// ... and so on | |
} | |
endif; | |
/** | |
* Returns the main instance of WP_MS_Teams_API. | |
* | |
* @return WP_MS_Teams_API The main instance. | |
*/ | |
function WP_MS_Teams_API() { | |
return WP_MS_Teams_API::get_instance(); | |
} | |
// Get WP_MS_Teams_API Running. | |
WP_MS_Teams_API(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment