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();