Files
igny8/igny8-wp-plugin/includes/class-igny8-api.php
IGNY8 VPS (Salman) a0d9bccb05 Refactor IGNY8 Bridge to use API key authentication exclusively
- Removed email/password authentication and related settings from the plugin.
- Updated API connection logic to utilize only the API key for authentication.
- Simplified the admin interface by removing webhook-related settings and messages.
- Enhanced the settings page with improved UI and status indicators for API connection.
- Added a new REST API endpoint to check plugin status and connection health.
- Updated styles for a modernized look and feel across the admin interface.
2025-11-22 10:31:07 +00:00

280 lines
7.5 KiB
PHP

<?php
/**
* IGNY8 API Client Class
*
* Handles all communication with IGNY8 API v1.0
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Igny8API Class
*/
class Igny8API {
/**
* API base URL
*
* @var string
*/
private $base_url = 'https://api.igny8.com/api/v1';
/**
* API key (used as access token)
*
* @var string|null
*/
private $access_token = null;
/**
* Whether authentication is via API key (always true now)
*
* @var bool
*/
private $api_key_auth = true;
/**
* Constructor
* Only uses API key for authentication
*/
public function __construct() {
if (function_exists('igny8_get_secure_option')) {
$api_key = igny8_get_secure_option('igny8_api_key');
} else {
$api_key = get_option('igny8_api_key');
}
// API key is the only authentication method
if (!empty($api_key)) {
$this->access_token = $api_key;
$this->api_key_auth = true;
}
}
/**
* Connect using API key
*
* @param string $api_key API key from IGNY8 app
* @return bool True on success, false on failure
*/
public function connect($api_key) {
if (empty($api_key)) {
return false;
}
// Store API key
if (function_exists('igny8_store_secure_option')) {
igny8_store_secure_option('igny8_api_key', $api_key);
} else {
update_option('igny8_api_key', $api_key);
}
$this->access_token = $api_key;
$this->api_key_auth = true;
// Test connection by making a simple API call
$test_response = $this->get('/auth/sites/');
if ($test_response['success']) {
$timestamp = current_time('timestamp');
update_option('igny8_last_api_health_check', $timestamp);
return true;
}
return false;
}
/**
* Parse unified API response
*
* @param array|WP_Error $response HTTP response
* @return array Parsed response
*/
private function parse_response($response) {
if (is_wp_error($response)) {
return array(
'success' => false,
'error' => $response->get_error_message(),
'http_status' => 0
);
}
$status_code = wp_remote_retrieve_response_code($response);
$raw_body = wp_remote_retrieve_body($response);
$body = json_decode($raw_body, true);
// Handle non-JSON responses
if (!$body) {
return array(
'success' => false,
'error' => 'Invalid response format: ' . substr($raw_body, 0, 100),
'http_status' => $status_code
);
}
// Check if response follows unified format
if (isset($body['success'])) {
$body['http_status'] = $status_code;
return $body;
}
// Legacy format - wrap in unified format
if ($status_code >= 200 && $status_code < 300) {
return array(
'success' => true,
'data' => $body,
'http_status' => $status_code
);
} else {
return array(
'success' => false,
'error' => $body['detail'] ?? 'HTTP ' . $status_code . ' error',
'http_status' => $status_code,
'raw_error' => $body
);
}
}
/**
* Get headers with authentication
*
* @return array Headers array
* @throws Exception If not authenticated
*/
private function get_headers() {
if (!$this->access_token) {
throw new Exception('Not authenticated');
}
return array(
'Authorization' => 'Bearer ' . $this->access_token,
'Content-Type' => 'application/json'
);
}
/**
* Make GET request
*
* @param string $endpoint API endpoint
* @return array Response data
*/
public function get($endpoint) {
$url = $this->base_url . $endpoint;
$headers = $this->get_headers();
// Debug logging (enable with WP_DEBUG or IGNY8_DEBUG constant)
$debug_enabled = (defined('WP_DEBUG') && WP_DEBUG) || (defined('IGNY8_DEBUG') && IGNY8_DEBUG);
if ($debug_enabled) {
error_log(sprintf(
'IGNY8 DEBUG GET: %s | Headers: %s',
$url,
json_encode(array_merge($headers, array('Authorization' => 'Bearer ***')))
));
}
$response = wp_remote_get($url, array(
'headers' => $headers,
'timeout' => 30
));
// Debug response
$debug_enabled = (defined('WP_DEBUG') && WP_DEBUG) || (defined('IGNY8_DEBUG') && IGNY8_DEBUG);
if ($debug_enabled) {
$status_code = wp_remote_retrieve_response_code($response);
$response_body = wp_remote_retrieve_body($response);
error_log(sprintf(
'IGNY8 DEBUG RESPONSE: Status=%s | Body=%s',
$status_code,
substr($response_body, 0, 500)
));
}
$body = $this->parse_response($response);
// API keys don't expire, so no refresh logic needed
// If 401, the API key is invalid or revoked
return $body;
}
/**
* Make POST request
*
* @param string $endpoint API endpoint
* @param array $data Request data
* @return array Response data
*/
public function post($endpoint, $data) {
$response = wp_remote_post($this->base_url . $endpoint, array(
'headers' => $this->get_headers(),
'body' => json_encode($data),
'timeout' => 60
));
$body = $this->parse_response($response);
// API keys don't expire, so no refresh logic needed
return $body;
}
/**
* Make PUT request
*
* @param string $endpoint API endpoint
* @param array $data Request data
* @return array Response data
*/
public function put($endpoint, $data) {
$response = wp_remote_request($this->base_url . $endpoint, array(
'method' => 'PUT',
'headers' => $this->get_headers(),
'body' => json_encode($data),
'timeout' => 60
));
return $this->parse_response($response);
}
/**
* Make DELETE request
*
* @param string $endpoint API endpoint
* @return array Response data
*/
public function delete($endpoint) {
$response = wp_remote_request($this->base_url . $endpoint, array(
'method' => 'DELETE',
'headers' => $this->get_headers(),
'timeout' => 30
));
return $this->parse_response($response);
}
/**
* Check if authenticated
*
* @return bool True if authenticated
*/
public function is_authenticated() {
return !empty($this->access_token);
}
/**
* Get access token
*
* @return string|null Access token
*/
public function get_access_token() {
return $this->access_token;
}
}