| @@ -19,13 +19,22 @@ | ||
| 19 | 19 | class Helper extends Base { |
| 20 | 20 | /** |
| 21 | 21 | * Check if development API should be used |
| 22 | 22 | * |
| 23 | + * This method maintains backward compatibility by checking both TEMPLATELY_DEV_API | |
| 24 | + * and falling back to TEMPLATELY_DEV if needed. This fallback logic should NOT be | |
| 25 | + * removed as it ensures existing setups continue to work. | |
| 26 | + * | |
| 23 | 27 | * @return bool True if development API should be used |
| 24 | 28 | */ |
| 25 | 29 | public static function is_dev_api(){ |
| 26 | - // Only check TEMPLATELY_DEV_API constant - no fallback mechanisms | |
| 27 | - return defined( 'TEMPLATELY_DEV_API' ) && constant( 'TEMPLATELY_DEV_API' ); | |
| 30 | + // Primary check: TEMPLATELY_DEV_API constant | |
| 31 | + if ( defined( 'TEMPLATELY_DEV_API' ) ) { | |
| 32 | + return constant( 'TEMPLATELY_DEV_API' ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + // Fallback: Legacy TEMPLATELY_DEV constant for backward compatibility | |
| 36 | + return defined( 'TEMPLATELY_DEV' ) && constant( 'TEMPLATELY_DEV' ); | |
| 28 | 37 | } |
| 29 | 38 | |
| 30 | 39 | /** |
| 31 | 40 | * Get installed WordPress Plugin List |
| @@ -96,17 +105,8 @@ | ||
| 96 | 105 | * @return string Complete API URL |
| 97 | 106 | */ |
| 98 | 107 | public static function get_api_url($endpoint): string { |
| 99 | 108 | $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 | 109 | return "{$base_url}/api/{$endpoint}"; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| @@ -133,15 +133,8 @@ | ||
| 133 | 133 | if (strtoupper($method) === 'POST') { |
| 134 | 134 | $headers['Content-Type'] = 'application/json'; |
| 135 | 135 | } |
| 136 | 136 | |
| 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 | 137 | // Merge additional headers |
| 145 | 138 | $headers = array_merge($headers, $extra_headers); |
| 146 | 139 | |
| 147 | 140 | $args = [ |
| @@ -158,21 +151,12 @@ | ||
| 158 | 151 | } |
| 159 | 152 | |
| 160 | 153 | // Make the appropriate request |
| 161 | 154 | if (strtoupper($method) === 'POST') { |
| 162 | - $response = wp_remote_post($api_url, $args); | |
| 155 | + return wp_remote_post($api_url, $args); | |
| 163 | 156 | } else { |
| 164 | - $response = wp_remote_get($api_url, $args); | |
| 157 | + return wp_remote_get($api_url, $args); | |
| 165 | 158 | } |
| 166 | - | |
| 167 | - // Check for verification header in the response | |
| 168 | - self::check_verification_header($response); | |
| 169 | - | |
| 170 | - | |
| 171 | - // Check for site disconnection in response body | |
| 172 | - self::check_site_disconnection($response); | |
| 173 | - | |
| 174 | - return $response; | |
| 175 | 159 | } |
| 176 | 160 | |
| 177 | 161 | /** |
| 178 | 162 | * Make a GET request to Templately API |
| @@ -229,194 +213,8 @@ | ||
| 229 | 213 | return $sanitized_value; |
| 230 | 214 | } |
| 231 | 215 | |
| 232 | 216 | /** |
| 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 | - * Check for X-Templately-Verified header and update user verification status | |
| 272 | - * | |
| 273 | - * @param array|WP_Error $response The HTTP response array from wp_remote_get/wp_remote_post | |
| 274 | - * @return void | |
| 275 | - */ | |
| 276 | - public static function check_verification_header($response) { | |
| 277 | - // Only process if response is not a WP_Error and contains headers | |
| 278 | - if (is_wp_error($response)) { | |
| 279 | - return; | |
| 280 | - } | |
| 281 | - | |
| 282 | - // Retrieve the X-Templately-Verified header | |
| 283 | - $verification_header = wp_remote_retrieve_header($response, 'X-Templately-Verified'); | |
| 284 | - | |
| 285 | - // Check if header exists and has a truthy value | |
| 286 | - if (!empty($verification_header) && filter_var($verification_header, FILTER_VALIDATE_BOOLEAN)) { | |
| 287 | - try { | |
| 288 | - // Get current user data | |
| 289 | - $options = Options::get_instance(); | |
| 290 | - $user = $options->get('user'); | |
| 291 | - | |
| 292 | - // Only update if user data exists and is not already verified | |
| 293 | - if (!empty($user) && is_array($user) && empty($user['is_verified'])) { | |
| 294 | - // Set verification flag | |
| 295 | - $user['is_verified'] = true; | |
| 296 | - | |
| 297 | - // Save updated user data | |
| 298 | - $options->set('user', $user); | |
| 299 | - | |
| 300 | - } | |
| 301 | - | |
| 302 | - if (!empty($user['is_verified'])){ | |
| 303 | - if(!headers_sent()){ | |
| 304 | - header( 'X-Templately-Verified: true' ); | |
| 305 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 306 | - self::log('User verification status already updated via X-Templately-Verified header'); | |
| 307 | - } | |
| 308 | - } | |
| 309 | - | |
| 310 | - return true; | |
| 311 | - } | |
| 312 | - } catch (\Exception $e) { | |
| 313 | - // Log error if debug logging is enabled | |
| 314 | - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { | |
| 315 | - self::log('Error updating user verification status: ' . $e->getMessage()); | |
| 316 | - } | |
| 317 | - } | |
| 318 | - } | |
| 319 | - | |
| 320 | - 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 | - } | |
| 417 | - | |
| 418 | - /** | |
| 419 | 217 | * API Error Formatter |
| 420 | 218 | * |
| 421 | 219 | * @param int $error_code |
| 422 | 220 | * @param mixed $error_message |
| @@ -510,59 +308,21 @@ | ||
| 510 | 308 | |
| 511 | 309 | /** |
| 512 | 310 | * Printing Error Logs in debug.log file. |
| 513 | 311 | * |
| 514 | - * @param mixed $log The data to log | |
| 515 | - * @param string $context Optional context for categorizing log entries | |
| 516 | - * @param string $level Optional log level (debug, info, warning, error) | |
| 312 | + * @param mixed $log | |
| 517 | 313 | * @return void |
| 518 | 314 | */ |
| 519 | - public static function log($log, $context = '', $level = 'info') { | |
| 520 | - // Allow complete override of logging behavior | |
| 521 | - $override_result = apply_filters('templately_log_override', null, $log, $context, $level); | |
| 522 | - if ($override_result !== null) { | |
| 523 | - return; | |
| 315 | + public static function log($log) { | |
| 316 | + if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) { | |
| 317 | + if (is_array($log) || is_object($log)) { | |
| 318 | + error_log(print_r($log, true)); | |
| 319 | + } else { | |
| 320 | + error_log($log ?: ''); | |
| 321 | + } | |
| 524 | 322 | } |
| 525 | - | |
| 526 | - // Only log if WP_DEBUG_LOG is enabled | |
| 527 | - if (!defined('WP_DEBUG_LOG') || !WP_DEBUG_LOG) { | |
| 528 | - return; | |
| 529 | - } | |
| 530 | - | |
| 531 | - // Format the log message | |
| 532 | - $formatted_message = self::format_log_message($log, $context, $level); | |
| 533 | - | |
| 534 | - // Write to error log | |
| 535 | - error_log($formatted_message); | |
| 536 | 323 | } |
| 537 | 324 | |
| 538 | - /** | |
| 539 | - * Format log message with context and level | |
| 540 | - * | |
| 541 | - * @param mixed $log The data to log | |
| 542 | - * @param string $context Context for categorizing log entries | |
| 543 | - * @param string $level Log level | |
| 544 | - * @return string Formatted log message | |
| 545 | - */ | |
| 546 | - private static function format_log_message($log, $context = '', $level = 'info') { | |
| 547 | - // Convert arrays and objects to readable format | |
| 548 | - if (is_array($log) || is_object($log)) { | |
| 549 | - $log_content = print_r($log, true); | |
| 550 | - } else { | |
| 551 | - $log_content = (string) ($log ?: ''); | |
| 552 | - } | |
| 553 | - | |
| 554 | - // Build the formatted message | |
| 555 | - $timestamp = current_time('Y-m-d H:i:s'); | |
| 556 | - $level_upper = strtoupper($level); | |
| 557 | - | |
| 558 | - if (!empty($context)) { | |
| 559 | - return "[{$timestamp}] [{$level_upper}] [{$context}] {$log_content}"; | |
| 560 | - } else { | |
| 561 | - return "[{$timestamp}] [{$level_upper}] {$log_content}"; | |
| 562 | - } | |
| 563 | - } | |
| 564 | - | |
| 565 | 325 | public static function should_flush() { |
| 566 | 326 | if (isset($_REQUEST['is_lightspeed']) && $_REQUEST['is_lightspeed'] === 'true') { |
| 567 | 327 | return false; |
| 568 | 328 | } |
| @@ -677,9 +437,9 @@ | ||
| 677 | 437 | */ |
| 678 | 438 | public static function enable_elementor_container() { |
| 679 | 439 | if (class_exists('Elementor\Plugin')) { |
| 680 | 440 | $control_name = Plugin::instance()->experiments->get_feature_option_key('container'); |
| 681 | - if (get_option($control_name) !== 'active') { | |
| 441 | + if (get_option($control_name) === 'inactive') { | |
| 682 | 442 | update_option($control_name, 'active'); |
| 683 | 443 | return true; |
| 684 | 444 | } |
| 685 | 445 | } |