| @@ -96,17 +96,8 @@ | ||
| 96 | 96 | * @return string Complete API URL |
| 97 | 97 | */ |
| 98 | 98 | public static function get_api_url($endpoint): string { |
| 99 | 99 | $base_url = self::is_dev_api() ? 'https://app.templately.dev' : 'https://app.templately.com'; |
| 100 | - | |
| 101 | - /** | |
| 102 | - * Filter the base URL for development API | |
| 103 | - * | |
| 104 | - * @since 3.5.0 | |
| 105 | - * @param string $base_url The default base URL | |
| 106 | - */ | |
| 107 | - $base_url = apply_filters('templately_dev_api_base_url', $base_url); | |
| 108 | - | |
| 109 | 100 | return "{$base_url}/api/{$endpoint}"; |
| 110 | 101 | } |
| 111 | 102 | |
| 112 | 103 | /** |
| @@ -133,15 +124,8 @@ | ||
| 133 | 124 | if (strtoupper($method) === 'POST') { |
| 134 | 125 | $headers['Content-Type'] = 'application/json'; |
| 135 | 126 | } |
| 136 | 127 | |
| 137 | - // Resolve requested platform: $_REQUEST wins (frontend-supplied), then caller's extra_headers, then default. | |
| 138 | - if ( isset( $_REQUEST['requested_platform'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 139 | - $extra_headers['x-templately-requested-platform'] = sanitize_text_field( wp_unslash( $_REQUEST['requested_platform'] ) ); | |
| 140 | - } elseif ( ! isset( $extra_headers['x-templately-requested-platform'] ) ) { | |
| 141 | - $extra_headers['x-templately-requested-platform'] = 'templately'; | |
| 142 | - } | |
| 143 | - | |
| 144 | 128 | // Merge additional headers |
| 145 | 129 | $headers = array_merge($headers, $extra_headers); |
| 146 | 130 | |
| 147 | 131 | $args = [ |
| @@ -166,12 +150,8 @@ | ||
| 166 | 150 | |
| 167 | 151 | // Check for verification header in the response |
| 168 | 152 | self::check_verification_header($response); |
| 169 | 153 | |
| 170 | - | |
| 171 | - // Check for site disconnection in response body | |
| 172 | - self::check_site_disconnection($response); | |
| 173 | - | |
| 174 | 154 | return $response; |
| 175 | 155 | } |
| 176 | 156 | |
| 177 | 157 | /** |
| @@ -229,46 +209,8 @@ | ||
| 229 | 209 | return $sanitized_value; |
| 230 | 210 | } |
| 231 | 211 | |
| 232 | 212 | /** |
| 233 | - * Escape a string for safe embedding inside a GraphQL or JSON string literal. | |
| 234 | - * | |
| 235 | - * GraphQL string escaping rules are identical to JSON string escaping (per the | |
| 236 | - * GraphQL spec), so wp_json_encode() is the authoritative escaper. We strip the | |
| 237 | - * outer quotes it adds and return only the escaped inner content, ready to be | |
| 238 | - * wrapped in your own quote pair. | |
| 239 | - * | |
| 240 | - * Handles pre-encoded JSON: when the caller has already run json_encode() + | |
| 241 | - * wp_slash() on a value (e.g. categories, dependencies in Items.php), the | |
| 242 | - * quotes are already escaped as \" and the string is ready to embed. Calling | |
| 243 | - * wp_json_encode() again would double-escape those backslashes. We detect this | |
| 244 | - * case by checking whether wp_unslash() produces valid JSON, and if so, return | |
| 245 | - * the value directly without further encoding. | |
| 246 | - * | |
| 247 | - * @param string $value Raw string or wp_slash(json_encode()) output. | |
| 248 | - * @return string Escaped string, safe to place between double quotes in GraphQL/JSON. | |
| 249 | - */ | |
| 250 | - public static function esc_json_string( $value ) { | |
| 251 | - $value = (string) $value; | |
| 252 | - | |
| 253 | - // If wp_slash() was applied to a JSON string upstream, the quotes are | |
| 254 | - // already escaped (e.g. {\"key\":\"val\"}). Detect this by unslashing and | |
| 255 | - // checking for valid JSON — if it matches, the value is already suitable | |
| 256 | - // for embedding in a string literal; return it as-is to avoid doubling backslashes. | |
| 257 | - $unslashed = wp_unslash( $value ); | |
| 258 | - if ( $unslashed !== $value ) { | |
| 259 | - $decoded = json_decode( $unslashed, true ); | |
| 260 | - if ( json_last_error() === JSON_ERROR_NONE && null !== $decoded ) { | |
| 261 | - return $value; | |
| 262 | - } | |
| 263 | - } | |
| 264 | - | |
| 265 | - $encoded = wp_json_encode( $value ); | |
| 266 | - // wp_json_encode wraps the value in "...", strip those outer quotes. | |
| 267 | - return substr( $encoded, 1, -1 ); | |
| 268 | - } | |
| 269 | - | |
| 270 | - /** | |
| 271 | 213 | * Check for X-Templately-Verified header and update user verification status |
| 272 | 214 | * |
| 273 | 215 | * @param array|WP_Error $response The HTTP response array from wp_remote_get/wp_remote_post |
| 274 | 216 | * @return void |
| @@ -317,103 +259,8 @@ | ||
| 317 | 259 | } |
| 318 | 260 | } |
| 319 | 261 | |
| 320 | 262 | return false; |
| 321 | - } | |
| 322 | - | |
| 323 | - /** | |
| 324 | - * Check for site disconnection status in API response body | |
| 325 | - * | |
| 326 | - * Detects SiteNotConnected errors and updates user disconnection status. | |
| 327 | - * Sends X-Templately-Disconnected header for frontend detection. | |
| 328 | - * | |
| 329 | - * | |
| 330 | - * @param array|WP_Error|mixed $response The response object or body array | |
| 331 | - * @return bool True if site is disconnected, false otherwise | |
| 332 | - */ | |
| 333 | - public static function check_site_disconnection($response) { | |
| 334 | - if (is_wp_error($response)) { | |
| 335 | - return false; | |
| 336 | - } | |
| 337 | - | |
| 338 | - $response_body = $response; | |
| 339 | - | |
| 340 | - // If it's a raw WP response array with body, decode it | |
| 341 | - if (is_array($response) && isset($response['body']) && is_string($response['body'])) { | |
| 342 | - $response_body = json_decode(wp_remote_retrieve_body($response), true); | |
| 343 | - } | |
| 344 | - | |
| 345 | - // Check if response body indicates site disconnection | |
| 346 | - if (!is_array($response_body)) { | |
| 347 | - return false; | |
| 348 | - } | |
| 349 | - | |
| 350 | - $status = $response_body['status'] ?? null; | |
| 351 | - $status_text = $response_body['statusText'] ?? null; | |
| 352 | - | |
| 353 | - // Check for SiteNotConnected error | |
| 354 | - if ($status === 'error' && $status_text === 'SiteNotConnected') { | |
| 355 | - try { | |
| 356 | - // Get current user data | |
| 357 | - $options = Options::get_instance(); | |
| 358 | - $user = $options->get('user'); | |
| 359 | - | |
| 360 | - // Only update if user data exists | |
| 361 | - if (!empty($user) && is_array($user)) { | |
| 362 | - // Set disconnection flag | |
| 363 | - $user['is_disconnected'] = true; | |
| 364 | - | |
| 365 | - // Save updated user data | |
| 366 | - $options->set('user', $user); | |
| 367 | - | |
| 368 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 369 | - self::log('Site disconnection detected: SiteNotConnected status'); | |
| 370 | - } | |
| 371 | - } | |
| 372 | - | |
| 373 | - // Send header for frontend detection | |
| 374 | - if (!headers_sent()) { | |
| 375 | - header('X-Templately-Disconnected: true'); | |
| 376 | - } | |
| 377 | - | |
| 378 | - return true; | |
| 379 | - } catch (\Exception $e) { | |
| 380 | - // Log error if debug logging is enabled | |
| 381 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 382 | - self::log('Error updating site disconnection status: ' . $e->getMessage()); | |
| 383 | - } | |
| 384 | - } | |
| 385 | - } | |
| 386 | - | |
| 387 | - return false; | |
| 388 | - } | |
| 389 | - | |
| 390 | - /** | |
| 391 | - * Clear site disconnection status | |
| 392 | - * | |
| 393 | - * Called after successful site migration to reset the disconnection flag. | |
| 394 | - * | |
| 395 | - * @return void | |
| 396 | - */ | |
| 397 | - public static function clear_site_disconnection() { | |
| 398 | - try { | |
| 399 | - $options = Options::get_instance(); | |
| 400 | - $user = $options->get('user'); | |
| 401 | - | |
| 402 | - if (!empty($user) && is_array($user)) { | |
| 403 | - $user['site_url'] = base64_encode( home_url('/') ); | |
| 404 | - $user['is_disconnected'] = false; | |
| 405 | - $options->set('user', $user); | |
| 406 | - | |
| 407 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 408 | - self::log('Site disconnection status cleared and URL updated.'); | |
| 409 | - } | |
| 410 | - } | |
| 411 | - } catch (\Exception $e) { | |
| 412 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 413 | - self::log('Error clearing site disconnection status: ' . $e->getMessage()); | |
| 414 | - } | |
| 415 | - } | |
| 416 | 263 | } |
| 417 | 264 | |
| 418 | 265 | /** |
| 419 | 266 | * API Error Formatter |