PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.7.2
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.7.2
2.7.1 2.7.2 2.7.0 2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 All 141 releases
metasync / includes / class-metasync-endpoint-manager.php

class-metasync-endpoint-manager.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.7.2, at includes/class-metasync-endpoint-manager.php

147 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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" ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets
85 return false;
86 }
87
88 $result = update_option( self::MODE_OPTION, $mode );
89
90 // WordPress returns false when the requested value is already stored.
91 // Read it back so selecting the current mode remains an idempotent success,
92 // while a failed write that leaves another value still reports failure.
93 return $result || self::get_mode() === $mode;
94 }
95
96 /**
97 * Get a specific endpoint URL by key.
98 *
99 * @param string $endpoint_key The endpoint key (e.g., 'OTTO_URL_DETAILS').
100 * @return string The endpoint URL, or empty string if not found.
101 */
102 public static function get_endpoint( $endpoint_key ) {
103 $mode = self::get_mode();
104
105 if ( ! isset( self::$endpoints[ $mode ][ $endpoint_key ] ) ) {
106 error_log( "MetaSync Endpoint Manager: Unknown endpoint key '{$endpoint_key}'" ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets
107
108 // Fallback to production if key exists there
109 if ( isset( self::$endpoints[ self::MODE_PRODUCTION ][ $endpoint_key ] ) ) {
110 return self::$endpoints[ self::MODE_PRODUCTION ][ $endpoint_key ];
111 }
112
113 return '';
114 }
115
116 return self::$endpoints[ $mode ][ $endpoint_key ];
117 }
118
119 /**
120 * Get all endpoints for the current mode.
121 *
122 * @return array Associative array of endpoint keys and URLs.
123 */
124 public static function get_all_endpoints() {
125 $mode = self::get_mode();
126 return self::$endpoints[ $mode ];
127 }
128
129 /**
130 * Check if staging mode is currently active.
131 *
132 * @return bool True if staging mode is active, false otherwise.
133 */
134 public static function is_staging_mode() {
135 return self::get_mode() === self::MODE_STAGING;
136 }
137
138 /**
139 * Get all available modes.
140 *
141 * @return array Array of available modes.
142 */
143 public static function get_available_modes() {
144 return array( self::MODE_PRODUCTION, self::MODE_STAGING );
145 }
146 }
147