| 1 |
<?php |
| 2 |
/** |
| 3 |
* Endpoint Manager for MetaSync Plugin |
| 4 |
* |
| 5 |
* Manages switching between production and staging endpoints for development and testing. |
| 6 |
* |
| 7 |
* @package Metasync |
| 8 |
* @subpackage Metasync/includes |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* The endpoint manager class. |
| 13 |
* |
| 14 |
* Provides centralized management of all SearchAtlas API endpoints with support |
| 15 |
* for switching between production and staging environments. |
| 16 |
* |
| 17 |
* @package Metasync |
| 18 |
* @subpackage Metasync/includes |
| 19 |
*/ |
| 20 |
class Metasync_Endpoint_Manager { |
| 21 |
|
| 22 |
/** |
| 23 |
* Option name for storing the current endpoint mode. |
| 24 |
*/ |
| 25 |
const MODE_OPTION = 'metasync_dev_endpoint_mode'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Valid endpoint modes. |
| 29 |
*/ |
| 30 |
const MODE_PRODUCTION = 'production'; |
| 31 |
const MODE_STAGING = 'staging'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Endpoint mapping for production and staging environments. |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
private static $endpoints = array( |
| 39 |
'production' => array( |
| 40 |
'DASHBOARD_DOMAIN' => 'https://dashboard.searchatlas.com', |
| 41 |
'API_DOMAIN' => 'https://api.searchatlas.com', |
| 42 |
'CA_API_DOMAIN' => 'https://ca.searchatlas.com', |
| 43 |
'OTTO_API_DOMAIN' => 'https://sa.searchatlas.com', |
| 44 |
'OTTO_URL_DETAILS' => 'https://sa.searchatlas.com/api/v2/otto-url-details', |
| 45 |
'OTTO_CRAWL_LOGS' => 'https://sa.searchatlas.com/api/v2/otto-page-crawl-logs', |
| 46 |
'OTTO_PROJECTS' => 'https://sa.searchatlas.com/api/v2/otto-projects', |
| 47 |
), |
| 48 |
'staging' => array( |
| 49 |
'DASHBOARD_DOMAIN' => 'https://dashboard.staging.searchatlas.com', |
| 50 |
'API_DOMAIN' => 'https://api.staging.searchatlas.com', |
| 51 |
'CA_API_DOMAIN' => 'https://ca.staging.searchatlas.com', |
| 52 |
'OTTO_API_DOMAIN' => 'https://sa.staging.searchatlas.com', |
| 53 |
'OTTO_URL_DETAILS' => 'https://sa.staging.searchatlas.com/api/v2/otto-url-details', |
| 54 |
'OTTO_CRAWL_LOGS' => 'https://sa.staging.searchatlas.com/api/v2/otto-page-crawl-logs', |
| 55 |
'OTTO_PROJECTS' => 'https://sa.staging.searchatlas.com/api/v2/otto-projects', |
| 56 |
), |
| 57 |
); |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the current endpoint mode. |
| 61 |
* |
| 62 |
* @return string Either 'production' or 'staging'. |
| 63 |
*/ |
| 64 |
public static function get_mode() { |
| 65 |
$mode = get_option( self::MODE_OPTION, self::MODE_PRODUCTION ); |
| 66 |
|
| 67 |
// Validate mode |
| 68 |
if ( ! in_array( $mode, array( self::MODE_PRODUCTION, self::MODE_STAGING ), true ) ) { |
| 69 |
$mode = self::MODE_PRODUCTION; |
| 70 |
} |
| 71 |
|
| 72 |
return $mode; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Set the endpoint mode. |
| 77 |
* |
| 78 |
* @param string $mode The mode to set ('production' or 'staging'). |
| 79 |
* @return bool True on success, false on failure. |
| 80 |
*/ |
| 81 |
public static function set_mode( $mode ) { |
| 82 |
// Validate mode |
| 83 |
if ( ! in_array( $mode, array( self::MODE_PRODUCTION, self::MODE_STAGING ), true ) ) { |
| 84 |
error_log( "MetaSync Endpoint Manager: Invalid mode '{$mode}' provided" ); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
$result = update_option( self::MODE_OPTION, $mode ); |
| 89 |
|
| 90 |
if ( $result ) { |
| 91 |
error_log( "MetaSync Endpoint Manager: Endpoints switched to {$mode} mode by user " . get_current_user_id() ); |
| 92 |
} |
| 93 |
|
| 94 |
return $result; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get a specific endpoint URL by key. |
| 99 |
* |
| 100 |
* @param string $endpoint_key The endpoint key (e.g., 'OTTO_URL_DETAILS'). |
| 101 |
* @return string The endpoint URL, or empty string if not found. |
| 102 |
*/ |
| 103 |
public static function get_endpoint( $endpoint_key ) { |
| 104 |
$mode = self::get_mode(); |
| 105 |
|
| 106 |
if ( ! isset( self::$endpoints[ $mode ][ $endpoint_key ] ) ) { |
| 107 |
error_log( "MetaSync Endpoint Manager: Unknown endpoint key '{$endpoint_key}'" ); |
| 108 |
|
| 109 |
// Fallback to production if key exists there |
| 110 |
if ( isset( self::$endpoints[ self::MODE_PRODUCTION ][ $endpoint_key ] ) ) { |
| 111 |
return self::$endpoints[ self::MODE_PRODUCTION ][ $endpoint_key ]; |
| 112 |
} |
| 113 |
|
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
return self::$endpoints[ $mode ][ $endpoint_key ]; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get all endpoints for the current mode. |
| 122 |
* |
| 123 |
* @return array Associative array of endpoint keys and URLs. |
| 124 |
*/ |
| 125 |
public static function get_all_endpoints() { |
| 126 |
$mode = self::get_mode(); |
| 127 |
return self::$endpoints[ $mode ]; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Check if staging mode is currently active. |
| 132 |
* |
| 133 |
* @return bool True if staging mode is active, false otherwise. |
| 134 |
*/ |
| 135 |
public static function is_staging_mode() { |
| 136 |
return self::get_mode() === self::MODE_STAGING; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get all available modes. |
| 141 |
* |
| 142 |
* @return array Array of available modes. |
| 143 |
*/ |
| 144 |
public static function get_available_modes() { |
| 145 |
return array( self::MODE_PRODUCTION, self::MODE_STAGING ); |
| 146 |
} |
| 147 |
} |
| 148 |
|