PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.1
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
← All changes | includes/Utils/Helper.php +30 -295 3.6.63.4.1 View file →
@@ -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
@@ -56,49 +65,24 @@
56 65
57 66 /**
58 67 * Collect IP from request.
59 68 *
60 - * Prefers REMOTE_ADDR since it cannot be spoofed by the client. When it is
61 - * a private/reserved address (reverse proxy, Docker bridge gateway like
62 - * 192.168.65.1, local dev), the forwarded headers are scanned for the first
63 - * public IP. If nothing public is found, the request is local: 127.0.0.1.
64 - *
65 69 * @return string
66 70 */
67 71 public static function get_ip() {
68 - $remote_addr = ! empty($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : '';
69 -
70 - if (self::is_public_ip($remote_addr)) {
71 - return $remote_addr;
72 + $ip = '127.0.0.1'; // Local IP
73 + if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
74 + $ip = $_SERVER['HTTP_CLIENT_IP'];
75 + } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
76 + $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
77 + } else {
78 + $ip = ! empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $ip;
72 79 }
73 80
74 - foreach (['HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP'] as $header) {
75 - if (empty($_SERVER[$header])) {
76 - continue;
77 - }
78 - $candidates = explode(',', sanitize_text_field($_SERVER[$header]));
79 - foreach ($candidates as $candidate) {
80 - $candidate = trim($candidate);
81 - if (self::is_public_ip($candidate)) {
82 - return $candidate;
83 - }
84 - }
85 - }
86 -
87 - return '127.0.0.1';
81 + return sanitize_text_field($ip);
88 82 }
89 83
90 84 /**
91 - * Check whether a string is a valid public (non-private, non-reserved) IP.
92 - *
93 - * @param string $ip
94 - * @return bool
95 - */
96 - private static function is_public_ip($ip): bool {
97 - return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
98 - }
99 -
100 - /**
101 85 * Get views for front-end display
102 86 *
103 87 * @param string $name it will be file name only from the view's folder.
104 88 * @param array $data
@@ -121,17 +105,8 @@
121 105 * @return string Complete API URL
122 106 */
123 107 public static function get_api_url($endpoint): string {
124 108 $base_url = self::is_dev_api() ? 'https://app.templately.dev' : 'https://app.templately.com';
125 -
126 - /**
127 - * Filter the base URL for development API
128 - *
129 - * @since 3.5.0
130 - * @param string $base_url The default base URL
131 - */
132 - $base_url = apply_filters('templately_dev_api_base_url', $base_url);
133 -
134 109 return "{$base_url}/api/{$endpoint}";
135 110 }
136 111
137 112 /**
@@ -158,15 +133,8 @@
158 133 if (strtoupper($method) === 'POST') {
159 134 $headers['Content-Type'] = 'application/json';
160 135 }
161 136
162 - // Resolve requested platform: $_REQUEST wins (frontend-supplied), then caller's extra_headers, then default.
163 - if ( isset( $_REQUEST['requested_platform'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
164 - $extra_headers['x-templately-requested-platform'] = sanitize_text_field( wp_unslash( $_REQUEST['requested_platform'] ) );
165 - } elseif ( ! isset( $extra_headers['x-templately-requested-platform'] ) ) {
166 - $extra_headers['x-templately-requested-platform'] = 'templately';
167 - }
168 -
169 137 // Merge additional headers
170 138 $headers = array_merge($headers, $extra_headers);
171 139
172 140 $args = [
@@ -183,21 +151,12 @@
183 151 }
184 152
185 153 // Make the appropriate request
186 154 if (strtoupper($method) === 'POST') {
187 - $response = wp_remote_post($api_url, $args);
155 + return wp_remote_post($api_url, $args);
188 156 } else {
189 - $response = wp_remote_get($api_url, $args);
157 + return wp_remote_get($api_url, $args);
190 158 }
191 -
192 - // Check for verification header in the response
193 - self::check_verification_header($response);
194 -
195 -
196 - // Check for site disconnection in response body
197 - self::check_site_disconnection($response);
198 -
199 - return $response;
200 159 }
201 160
202 161 /**
203 162 * Make a GET request to Templately API
@@ -254,194 +213,8 @@
254 213 return $sanitized_value;
255 214 }
256 215
257 216 /**
258 - * Escape a string for safe embedding inside a GraphQL or JSON string literal.
259 - *
260 - * GraphQL string escaping rules are identical to JSON string escaping (per the
261 - * GraphQL spec), so wp_json_encode() is the authoritative escaper. We strip the
262 - * outer quotes it adds and return only the escaped inner content, ready to be
263 - * wrapped in your own quote pair.
264 - *
265 - * Handles pre-encoded JSON: when the caller has already run json_encode() +
266 - * wp_slash() on a value (e.g. categories, dependencies in Items.php), the
267 - * quotes are already escaped as \" and the string is ready to embed. Calling
268 - * wp_json_encode() again would double-escape those backslashes. We detect this
269 - * case by checking whether wp_unslash() produces valid JSON, and if so, return
270 - * the value directly without further encoding.
271 - *
272 - * @param string $value Raw string or wp_slash(json_encode()) output.
273 - * @return string Escaped string, safe to place between double quotes in GraphQL/JSON.
274 - */
275 - public static function esc_json_string( $value ) {
276 - $value = (string) $value;
277 -
278 - // If wp_slash() was applied to a JSON string upstream, the quotes are
279 - // already escaped (e.g. {\"key\":\"val\"}). Detect this by unslashing and
280 - // checking for valid JSON — if it matches, the value is already suitable
281 - // for embedding in a string literal; return it as-is to avoid doubling backslashes.
282 - $unslashed = wp_unslash( $value );
283 - if ( $unslashed !== $value ) {
284 - $decoded = json_decode( $unslashed, true );
285 - if ( json_last_error() === JSON_ERROR_NONE && null !== $decoded ) {
286 - return $value;
287 - }
288 - }
289 -
290 - $encoded = wp_json_encode( $value );
291 - // wp_json_encode wraps the value in "...", strip those outer quotes.
292 - return substr( $encoded, 1, -1 );
293 - }
294 -
295 - /**
296 - * Check for X-Templately-Verified header and update user verification status
297 - *
298 - * @param array|WP_Error $response The HTTP response array from wp_remote_get/wp_remote_post
299 - * @return void
300 - */
301 - public static function check_verification_header($response) {
302 - // Only process if response is not a WP_Error and contains headers
303 - if (is_wp_error($response)) {
304 - return;
305 - }
306 -
307 - // Retrieve the X-Templately-Verified header
308 - $verification_header = wp_remote_retrieve_header($response, 'X-Templately-Verified');
309 -
310 - // Check if header exists and has a truthy value
311 - if (!empty($verification_header) && filter_var($verification_header, FILTER_VALIDATE_BOOLEAN)) {
312 - try {
313 - // Get current user data
314 - $options = Options::get_instance();
315 - $user = $options->get('user');
316 -
317 - // Only update if user data exists and is not already verified
318 - if (!empty($user) && is_array($user) && empty($user['is_verified'])) {
319 - // Set verification flag
320 - $user['is_verified'] = true;
321 -
322 - // Save updated user data
323 - $options->set('user', $user);
324 -
325 - }
326 -
327 - if (!empty($user['is_verified'])){
328 - if(!headers_sent()){
329 - header( 'X-Templately-Verified: true' );
330 - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) {
331 - self::log('User verification status already updated via X-Templately-Verified header');
332 - }
333 - }
334 -
335 - return true;
336 - }
337 - } catch (\Exception $e) {
338 - // Log error if debug logging is enabled
339 - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) {
340 - self::log('Error updating user verification status: ' . $e->getMessage());
341 - }
342 - }
343 - }
344 -
345 - return false;
346 - }
347 -
348 - /**
349 - * Check for site disconnection status in API response body
350 - *
351 - * Detects SiteNotConnected errors and updates user disconnection status.
352 - * Sends X-Templately-Disconnected header for frontend detection.
353 - *
354 - *
355 - * @param array|WP_Error|mixed $response The response object or body array
356 - * @return bool True if site is disconnected, false otherwise
357 - */
358 - public static function check_site_disconnection($response) {
359 - if (is_wp_error($response)) {
360 - return false;
361 - }
362 -
363 - $response_body = $response;
364 -
365 - // If it's a raw WP response array with body, decode it
366 - if (is_array($response) && isset($response['body']) && is_string($response['body'])) {
367 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
368 - }
369 -
370 - // Check if response body indicates site disconnection
371 - if (!is_array($response_body)) {
372 - return false;
373 - }
374 -
375 - $status = $response_body['status'] ?? null;
376 - $status_text = $response_body['statusText'] ?? null;
377 -
378 - // Check for SiteNotConnected error
379 - if ($status === 'error' && $status_text === 'SiteNotConnected') {
380 - try {
381 - // Get current user data
382 - $options = Options::get_instance();
383 - $user = $options->get('user');
384 -
385 - // Only update if user data exists
386 - if (!empty($user) && is_array($user)) {
387 - // Set disconnection flag
388 - $user['is_disconnected'] = true;
389 -
390 - // Save updated user data
391 - $options->set('user', $user);
392 -
393 - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) {
394 - self::log('Site disconnection detected: SiteNotConnected status');
395 - }
396 - }
397 -
398 - // Send header for frontend detection
399 - if (!headers_sent()) {
400 - header('X-Templately-Disconnected: true');
401 - }
402 -
403 - return true;
404 - } catch (\Exception $e) {
405 - // Log error if debug logging is enabled
406 - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) {
407 - self::log('Error updating site disconnection status: ' . $e->getMessage());
408 - }
409 - }
410 - }
411 -
412 - return false;
413 - }
414 -
415 - /**
416 - * Clear site disconnection status
417 - *
418 - * Called after successful site migration to reset the disconnection flag.
419 - *
420 - * @return void
421 - */
422 - public static function clear_site_disconnection() {
423 - try {
424 - $options = Options::get_instance();
425 - $user = $options->get('user');
426 -
427 - if (!empty($user) && is_array($user)) {
428 - $user['site_url'] = base64_encode( home_url('/') );
429 - $user['is_disconnected'] = false;
430 - $options->set('user', $user);
431 -
432 - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) {
433 - self::log('Site disconnection status cleared and URL updated.');
434 - }
435 - }
436 - } catch (\Exception $e) {
437 - if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) {
438 - self::log('Error clearing site disconnection status: ' . $e->getMessage());
439 - }
440 - }
441 - }
442 -
443 - /**
444 217 * API Error Formatter
445 218 *
446 219 * @param int $error_code
447 220 * @param mixed $error_message
@@ -535,59 +308,21 @@
535 308
536 309 /**
537 310 * Printing Error Logs in debug.log file.
538 311 *
539 - * @param mixed $log The data to log
540 - * @param string $context Optional context for categorizing log entries
541 - * @param string $level Optional log level (debug, info, warning, error)
312 + * @param mixed $log
542 313 * @return void
543 314 */
544 - public static function log($log, $context = '', $level = 'info') {
545 - // Allow complete override of logging behavior
546 - $override_result = apply_filters('templately_log_override', null, $log, $context, $level);
547 - if ($override_result !== null) {
548 - 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 + }
549 322 }
550 -
551 - // Only log if WP_DEBUG_LOG is enabled
552 - if (!defined('WP_DEBUG_LOG') || !WP_DEBUG_LOG) {
553 - return;
554 - }
555 -
556 - // Format the log message
557 - $formatted_message = self::format_log_message($log, $context, $level);
558 -
559 - // Write to error log
560 - error_log($formatted_message);
561 323 }
562 324
563 - /**
564 - * Format log message with context and level
565 - *
566 - * @param mixed $log The data to log
567 - * @param string $context Context for categorizing log entries
568 - * @param string $level Log level
569 - * @return string Formatted log message
570 - */
571 - private static function format_log_message($log, $context = '', $level = 'info') {
572 - // Convert arrays and objects to readable format
573 - if (is_array($log) || is_object($log)) {
574 - $log_content = print_r($log, true);
575 - } else {
576 - $log_content = (string) ($log ?: '');
577 - }
578 -
579 - // Build the formatted message
580 - $timestamp = current_time('Y-m-d H:i:s');
581 - $level_upper = strtoupper($level);
582 -
583 - if (!empty($context)) {
584 - return "[{$timestamp}] [{$level_upper}] [{$context}] {$log_content}";
585 - } else {
586 - return "[{$timestamp}] [{$level_upper}] {$log_content}";
587 - }
588 - }
589 -
590 325 public static function should_flush() {
591 326 if (isset($_REQUEST['is_lightspeed']) && $_REQUEST['is_lightspeed'] === 'true') {
592 327 return false;
593 328 }
@@ -702,9 +437,9 @@
702 437 */
703 438 public static function enable_elementor_container() {
704 439 if (class_exists('Elementor\Plugin')) {
705 440 $control_name = Plugin::instance()->experiments->get_feature_option_key('container');
706 - if (get_option($control_name) !== 'active') {
441 + if (get_option($control_name) === 'inactive') {
707 442 update_option($control_name, 'active');
708 443 return true;
709 444 }
710 445 }