Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created November 27, 2024 21:02
Show Gist options
  • Save bhubbard/69418a624f9c0e55d97324de71cfb906 to your computer and use it in GitHub Desktop.
Save bhubbard/69418a624f9c0e55d97324de71cfb906 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Jira API
* Plugin URI: https://example.com/wp-jira-api
* Description: A WordPress plugin that provides an API wrapper for Jira.
* 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-jira-api
* Domain Path: /languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Jira_API' ) ) :
/**
* Main WP_Jira_API Class.
*
* @since 1.0.0
*/
final class WP_Jira_API {
/**
* Version of the plugin.
*
* @var string
*/
public $version = '1.0.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* The Jira API base URL.
*
* @var string
*/
public $api_base_url = 'https://your-domain.atlassian.net/rest/api/3/'; // Replace with your Jira instance URL
/**
* Jira 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 Jira API credentials.
$this->credentials = array(
'username' => 'YOUR_USERNAME', // Replace with your Jira username
'api_token' => 'YOUR_API_TOKEN', // Replace with your Jira 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-jira-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 Jira 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['username'] . ':' . $this->credentials['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( 'Jira 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( 'Jira API Error (' . $status_code . '): ' . $error_message );
return new WP_Error( 'jira_api_error', $error_message, array( 'status_code' => $status_code ) );
}
return json_decode( wp_remote_retrieve_body( $response ) );
}
/**
* Get a list of projects.
*
* @param array $params Optional parameters (e.g., startAt, maxResults).
* @return array The list of projects.
*/
public function get_projects( $params = array() ) {
return $this->make_request( 'project', 'GET', $params );
}
/**
* Get a single project by key.
*
* @param string $project_key The key of the project.
* @return array The project data.
*/
public function get_project( $project_key ) {
return $this->make_request( 'project/' . $project_key );
}
/**
* Get issues in a project.
*
* @param string $project_key The key of the project.
* @param array $params Optional parameters (e.g., jql, startAt, maxResults).
* @return array The project issues.
*/
public function get_project_issues( $project_key, $params = array() ) {
// Example: Get issues with the "Bug" issue type in the project.
$jql = 'project = "' . $project_key . '" AND issuetype = Bug';
$params['jql'] = $jql;
return $this->make_request( 'search', 'GET', $params );
}
// Add other helper methods for specific Jira API functionalities, e.g.,:
// public function create_issue( $project_key, $summary, $description, $issue_type ) { ... }
// public function get_issue( $issue_key ) { ... }
// public function add_comment_to_issue( $issue_key, $comment ) { ... }
// ... and so on
}
endif;
/**
* Returns the main instance of WP_Jira_API.
*
* @return WP_Jira_API The main instance.
*/
function WP_Jira_API() {
return WP_Jira_API::get_instance();
}
// Get WP_Jira_API Running.
WP_Jira_API();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment