| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Backoff REST API Endpoints |
| 4 |
* |
| 5 |
* Provides REST API endpoints for managing and monitoring API backoff states. |
| 6 |
* |
| 7 |
* @package Metasync |
| 8 |
* @subpackage Metasync/includes |
| 9 |
* @since 2.5.15 |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Metasync_API_Backoff_REST |
| 18 |
* |
| 19 |
* Handles REST API endpoints for backoff management. |
| 20 |
*/ |
| 21 |
class Metasync_API_Backoff_REST { |
| 22 |
|
| 23 |
/** |
| 24 |
* REST API namespace |
| 25 |
*/ |
| 26 |
private const NAMESPACE = 'metasync/v1'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Backoff manager instance |
| 30 |
* |
| 31 |
* @var Metasync_API_Backoff_Manager |
| 32 |
*/ |
| 33 |
private $backoff_manager; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
$this->backoff_manager = Metasync_API_Backoff_Manager::get_instance(); |
| 40 |
$this->register_routes(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register REST API routes |
| 45 |
*/ |
| 46 |
public function register_routes() { |
| 47 |
// Get all active backoffs |
| 48 |
register_rest_route(self::NAMESPACE, '/backoff/status', [ |
| 49 |
'methods' => 'GET', |
| 50 |
'callback' => [$this, 'get_backoff_status'], |
| 51 |
'permission_callback' => [$this, 'check_admin_permission'], |
| 52 |
]); |
| 53 |
|
| 54 |
// Get specific endpoint backoff state |
| 55 |
register_rest_route(self::NAMESPACE, '/backoff/(?P<hash>[a-f0-9]+)', [ |
| 56 |
'methods' => 'GET', |
| 57 |
'callback' => [$this, 'get_endpoint_backoff'], |
| 58 |
'permission_callback' => [$this, 'check_admin_permission'], |
| 59 |
'args' => [ |
| 60 |
'hash' => [ |
| 61 |
'description' => 'Endpoint hash identifier', |
| 62 |
'type' => 'string', |
| 63 |
'pattern' => '^[a-f0-9]+$', |
| 64 |
], |
| 65 |
], |
| 66 |
]); |
| 67 |
|
| 68 |
// Clear specific endpoint backoff |
| 69 |
register_rest_route(self::NAMESPACE, '/backoff/(?P<hash>[a-f0-9]+)', [ |
| 70 |
'methods' => 'DELETE', |
| 71 |
'callback' => [$this, 'clear_endpoint_backoff'], |
| 72 |
'permission_callback' => [$this, 'check_admin_permission'], |
| 73 |
'args' => [ |
| 74 |
'hash' => [ |
| 75 |
'description' => 'Endpoint hash identifier', |
| 76 |
'type' => 'string', |
| 77 |
'pattern' => '^[a-f0-9]+$', |
| 78 |
], |
| 79 |
], |
| 80 |
]); |
| 81 |
|
| 82 |
// Clear all backoffs |
| 83 |
register_rest_route(self::NAMESPACE, '/backoff/clear-all', [ |
| 84 |
'methods' => 'POST', |
| 85 |
'callback' => [$this, 'clear_all_backoffs'], |
| 86 |
'permission_callback' => [$this, 'check_admin_permission'], |
| 87 |
]); |
| 88 |
|
| 89 |
// Get backoff statistics |
| 90 |
register_rest_route(self::NAMESPACE, '/backoff/statistics', [ |
| 91 |
'methods' => 'GET', |
| 92 |
'callback' => [$this, 'get_statistics'], |
| 93 |
'permission_callback' => [$this, 'check_admin_permission'], |
| 94 |
]); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if user has admin permission |
| 99 |
* |
| 100 |
* @param WP_REST_Request $request Request object. |
| 101 |
* @return bool True if user has permission. |
| 102 |
*/ |
| 103 |
public function check_admin_permission($request) { |
| 104 |
return current_user_can('manage_options'); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get all active backoffs |
| 109 |
* |
| 110 |
* @param WP_REST_Request $request Request object. |
| 111 |
* @return WP_REST_Response |
| 112 |
*/ |
| 113 |
public function get_backoff_status($request) { |
| 114 |
$active_backoffs = $this->backoff_manager->get_all_active_backoffs(); |
| 115 |
|
| 116 |
return new WP_REST_Response([ |
| 117 |
'success' => true, |
| 118 |
'data' => [ |
| 119 |
'active_backoffs' => $active_backoffs, |
| 120 |
'count' => count($active_backoffs), |
| 121 |
'timestamp' => current_time('timestamp'), |
| 122 |
], |
| 123 |
], 200); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get specific endpoint backoff state |
| 128 |
* |
| 129 |
* @param WP_REST_Request $request Request object. |
| 130 |
* @return WP_REST_Response |
| 131 |
*/ |
| 132 |
public function get_endpoint_backoff($request) { |
| 133 |
$endpoint_hash = $request->get_param('hash'); |
| 134 |
$backoff_state = $this->backoff_manager->get_backoff_state($endpoint_hash); |
| 135 |
|
| 136 |
if ($backoff_state === false) { |
| 137 |
return new WP_REST_Response([ |
| 138 |
'success' => false, |
| 139 |
'message' => 'No active backoff for this endpoint', |
| 140 |
'data' => [ |
| 141 |
'endpoint_hash' => $endpoint_hash, |
| 142 |
'is_active' => false, |
| 143 |
], |
| 144 |
], 404); |
| 145 |
} |
| 146 |
|
| 147 |
return new WP_REST_Response([ |
| 148 |
'success' => true, |
| 149 |
'data' => [ |
| 150 |
'endpoint_hash' => $endpoint_hash, |
| 151 |
'is_active' => true, |
| 152 |
'backoff_state' => $backoff_state, |
| 153 |
], |
| 154 |
], 200); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Clear specific endpoint backoff |
| 159 |
* |
| 160 |
* @param WP_REST_Request $request Request object. |
| 161 |
* @return WP_REST_Response |
| 162 |
*/ |
| 163 |
public function clear_endpoint_backoff($request) { |
| 164 |
$endpoint_hash = $request->get_param('hash'); |
| 165 |
$cleared = $this->backoff_manager->clear_backoff($endpoint_hash); |
| 166 |
|
| 167 |
if (!$cleared) { |
| 168 |
return new WP_REST_Response([ |
| 169 |
'success' => false, |
| 170 |
'message' => 'Failed to clear backoff or no backoff exists', |
| 171 |
'data' => [ |
| 172 |
'endpoint_hash' => $endpoint_hash, |
| 173 |
], |
| 174 |
], 404); |
| 175 |
} |
| 176 |
|
| 177 |
return new WP_REST_Response([ |
| 178 |
'success' => true, |
| 179 |
'message' => 'Backoff cleared successfully', |
| 180 |
'data' => [ |
| 181 |
'endpoint_hash' => $endpoint_hash, |
| 182 |
'cleared_by' => get_current_user_id(), |
| 183 |
'cleared_at' => current_time('timestamp'), |
| 184 |
], |
| 185 |
], 200); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Clear all backoffs |
| 190 |
* |
| 191 |
* @param WP_REST_Request $request Request object. |
| 192 |
* @return WP_REST_Response |
| 193 |
*/ |
| 194 |
public function clear_all_backoffs($request) { |
| 195 |
$cleared_count = $this->backoff_manager->clear_all_backoffs(); |
| 196 |
|
| 197 |
return new WP_REST_Response([ |
| 198 |
'success' => true, |
| 199 |
'message' => sprintf('Successfully cleared %d backoff(s)', $cleared_count), |
| 200 |
'data' => [ |
| 201 |
'cleared_count' => $cleared_count, |
| 202 |
'cleared_by' => get_current_user_id(), |
| 203 |
'cleared_at' => current_time('timestamp'), |
| 204 |
], |
| 205 |
], 200); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get backoff statistics |
| 210 |
* |
| 211 |
* @param WP_REST_Request $request Request object. |
| 212 |
* @return WP_REST_Response |
| 213 |
*/ |
| 214 |
public function get_statistics($request) { |
| 215 |
$statistics = $this->backoff_manager->get_statistics(); |
| 216 |
|
| 217 |
return new WP_REST_Response([ |
| 218 |
'success' => true, |
| 219 |
'data' => $statistics, |
| 220 |
], 200); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Initialize REST API endpoints |
| 226 |
*/ |
| 227 |
function metasync_api_backoff_rest_init() { |
| 228 |
new Metasync_API_Backoff_REST(); |
| 229 |
} |
| 230 |
add_action('rest_api_init', 'metasync_api_backoff_rest_init'); |
| 231 |
|