| @@ -1,15 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | namespace Templately\Utils; |
| 4 | 3 | |
| 5 | -use Elementor\Plugin; | |
| 6 | -use Templately\Core\Importer\Utils\Utils; | |
| 7 | 4 | use WP_Error; |
| 8 | 5 | use WP_REST_Response; |
| 9 | 6 | use function get_plugins; |
| 10 | 7 | use function is_plugin_active; |
| 11 | - | |
| 12 | 8 | /** |
| 13 | 9 | * Utility Helper for Templately |
| 14 | 10 | * |
| 15 | 11 | * This class contains some helper functions for easy access. |
| @@ -16,225 +12,66 @@ | ||
| 16 | 12 | * |
| 17 | 13 | * @since 1.0.0 |
| 18 | 14 | */ |
| 19 | 15 | class Helper extends Base { |
| 20 | - /** | |
| 21 | - * Check if development API should be used | |
| 22 | - * | |
| 23 | - * @return bool True if development API should be used | |
| 24 | - */ | |
| 25 | - 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' ); | |
| 28 | - } | |
| 16 | + /** | |
| 17 | + * Get installed WordPress Plugin List | |
| 18 | + * @return array | |
| 19 | + */ | |
| 20 | + public static function get_plugins(){ | |
| 21 | + if( ! function_exists( 'get_plugins' ) ) { | |
| 22 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 23 | + } | |
| 24 | + return get_plugins(); | |
| 25 | + } | |
| 29 | 26 | |
| 30 | - /** | |
| 31 | - * Get installed WordPress Plugin List | |
| 32 | - * @return array | |
| 33 | - */ | |
| 34 | - public static function get_plugins() { | |
| 35 | - if (! function_exists('get_plugins')) { | |
| 36 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 37 | - } | |
| 38 | - return get_plugins(); | |
| 39 | - } | |
| 40 | - public static function is_plugins_installed($plugin_file) { | |
| 41 | - $_plugins = self::get_plugins(); | |
| 42 | - $is_installed = isset($_plugins[$plugin_file]); | |
| 43 | - return $is_installed; | |
| 44 | - } | |
| 27 | + /** | |
| 28 | + * Get installed WordPress Plugin List | |
| 29 | + * @return boolean | |
| 30 | + */ | |
| 31 | + public static function is_plugin_active( $plugin ){ | |
| 32 | + if( ! function_exists( 'is_plugin_active' ) ) { | |
| 33 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 34 | + } | |
| 35 | + return is_plugin_active( $plugin ); | |
| 36 | + } | |
| 45 | 37 | |
| 46 | - /** | |
| 47 | - * Get installed WordPress Plugin List | |
| 48 | - * @return boolean | |
| 49 | - */ | |
| 50 | - public static function is_plugin_active($plugin) { | |
| 51 | - if (! function_exists('is_plugin_active')) { | |
| 52 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 53 | - } | |
| 54 | - return is_plugin_active($plugin); | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 38 | + /** | |
| 58 | 39 | * Collect IP from request. |
| 59 | 40 | * |
| 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 | 41 | * @return string |
| 66 | 42 | */ |
| 67 | 43 | 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 | - } | |
| 73 | - | |
| 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'; | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 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 | - * Get views for front-end display | |
| 102 | - * | |
| 103 | - * @param string $name it will be file name only from the view's folder. | |
| 104 | - * @param array $data | |
| 105 | - * @return void | |
| 106 | - */ | |
| 107 | - public static function views($name, $data = []) { | |
| 108 | - extract($data); | |
| 109 | - $helper = self::class; | |
| 110 | - $file = TEMPLATELY_PATH . 'views/' . $name . '.php'; | |
| 111 | - | |
| 112 | - if (is_readable($file)) { | |
| 113 | - include_once $file; | |
| 114 | - } | |
| 115 | - } | |
| 116 | - | |
| 117 | - /** | |
| 118 | - * Get API URL for Templately endpoints | |
| 119 | - * | |
| 120 | - * @param string $endpoint API endpoint path (e.g., 'v2/import/pack/123') | |
| 121 | - * @return string Complete API URL | |
| 122 | - */ | |
| 123 | - public static function get_api_url($endpoint): string { | |
| 124 | - $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 | - return "{$base_url}/api/{$endpoint}"; | |
| 135 | - } | |
| 136 | - | |
| 137 | - /** | |
| 138 | - * Make a unified API request to Templately API | |
| 139 | - * | |
| 140 | - * @param string $method HTTP method (GET or POST) | |
| 141 | - * @param string $api_url Complete API URL | |
| 142 | - * @param array $body Request body data (for POST requests) | |
| 143 | - * @param array $extra_headers Additional headers beyond the standard ones | |
| 144 | - * @param int $timeout Request timeout in seconds (default: 30) | |
| 145 | - * @return array|WP_Error Response array or WP_Error on failure | |
| 146 | - */ | |
| 147 | - private static function make_api_request($method, $api_url, $body = [], $extra_headers = [], $timeout = 30) { | |
| 148 | - $api_key = Options::get_instance()->get('api_key'); | |
| 149 | - | |
| 150 | - $headers = [ | |
| 151 | - 'Authorization' => 'Bearer ' . $api_key, | |
| 152 | - 'x-templately-ip' => self::get_ip(), | |
| 153 | - 'x-templately-url' => home_url('/'), | |
| 154 | - 'x-templately-version' => defined( 'TEMPLATELY_VERSION' ) ? constant( 'TEMPLATELY_VERSION' ) : '1.0.0', | |
| 155 | - ]; | |
| 156 | - | |
| 157 | - // Add Content-Type for POST requests | |
| 158 | - if (strtoupper($method) === 'POST') { | |
| 159 | - $headers['Content-Type'] = 'application/json'; | |
| 160 | - } | |
| 161 | - | |
| 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 | - // Merge additional headers | |
| 170 | - $headers = array_merge($headers, $extra_headers); | |
| 171 | - | |
| 172 | - $args = [ | |
| 173 | - 'timeout' => $timeout, | |
| 174 | - 'headers' => $headers, | |
| 175 | - ]; | |
| 176 | - | |
| 177 | - // Apply filter to allow network admin or other functionality to modify request args | |
| 178 | - $args = apply_filters( 'templately_api_request_params', $args, $method, $api_url ); | |
| 179 | - | |
| 180 | - // Add body for POST requests | |
| 181 | - if (strtoupper($method) === 'POST') { | |
| 182 | - $args['body'] = is_array($body) ? json_encode($body) : $body; | |
| 183 | - } | |
| 184 | - | |
| 185 | - // Make the appropriate request | |
| 186 | - if (strtoupper($method) === 'POST') { | |
| 187 | - $response = wp_remote_post($api_url, $args); | |
| 44 | + $ip = '127.0.0.1'; // Local IP | |
| 45 | + if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { | |
| 46 | + $ip = $_SERVER['HTTP_CLIENT_IP']; | |
| 47 | + } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { | |
| 48 | + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
| 188 | 49 | } else { |
| 189 | - $response = wp_remote_get($api_url, $args); | |
| 50 | + $ip = ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : $ip; | |
| 190 | 51 | } |
| 191 | 52 | |
| 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; | |
| 53 | + return sanitize_text_field( $ip ); | |
| 200 | 54 | } |
| 201 | 55 | |
| 202 | - /** | |
| 203 | - * Make a GET request to Templately API | |
| 204 | - * | |
| 205 | - * @param string $endpoint API endpoint path (e.g., 'v2/import/pack/123') | |
| 206 | - * @param array $query_params Query parameters as key-value pairs | |
| 207 | - * @param array $extra_headers Additional headers beyond the standard ones | |
| 208 | - * @param int $timeout Request timeout in seconds (default: 30) | |
| 209 | - * @return array|WP_Error Response array or WP_Error on failure | |
| 210 | - */ | |
| 211 | - public static function make_api_get_request($endpoint, $query_params = [], $extra_headers = [], $timeout = 30) { | |
| 212 | - $api_url = self::get_api_url($endpoint); | |
| 56 | + /** | |
| 57 | + * Get views for front-end display | |
| 58 | + * | |
| 59 | + * @param string $name it will be file name only from the view's folder. | |
| 60 | + * @param array $data | |
| 61 | + * @return void | |
| 62 | + */ | |
| 63 | + public static function views( $name, $data = [] ){ | |
| 64 | + extract( $data ); | |
| 65 | + $helper = self::class; | |
| 66 | + $file = TEMPLATELY_PATH . 'views/' . $name . '.php'; | |
| 213 | 67 | |
| 214 | - // Add query parameters if provided | |
| 215 | - if (!empty($query_params)) { | |
| 216 | - $api_url = add_query_arg($query_params, $api_url); | |
| 217 | - } | |
| 68 | + if( is_readable( $file ) ) { | |
| 69 | + include_once $file; | |
| 70 | + } | |
| 71 | + } | |
| 218 | 72 | |
| 219 | - return self::make_api_request('GET', $api_url, [], $extra_headers, $timeout); | |
| 220 | - } | |
| 221 | - | |
| 222 | 73 | /** |
| 223 | - * Make a POST request to Templately API | |
| 224 | - * | |
| 225 | - * @param string $endpoint API endpoint path (e.g., 'v2/feedback/store') | |
| 226 | - * @param array $body Request body data | |
| 227 | - * @param array $extra_headers Additional headers beyond the standard ones | |
| 228 | - * @param int $timeout Request timeout in seconds (default: 30) | |
| 229 | - * @return array|WP_Error Response array or WP_Error on failure | |
| 230 | - */ | |
| 231 | - public static function make_api_post_request($endpoint, $body = [], $extra_headers = [], $timeout = 30) { | |
| 232 | - $api_url = self::get_api_url($endpoint); | |
| 233 | - return self::make_api_request('POST', $api_url, $body, $extra_headers, $timeout); | |
| 234 | - } | |
| 235 | - | |
| 236 | - /** | |
| 237 | 74 | * Sanitize Helper |
| 238 | 75 | * |
| 239 | 76 | * @param mixed $value |
| 240 | 77 | * @param string $type |
| @@ -240,15 +77,15 @@ | ||
| 240 | 77 | * @param string $type |
| 241 | 78 | * |
| 242 | 79 | * @return bool|string |
| 243 | 80 | */ |
| 244 | - public static function sanitize($value, $type = 'text') { | |
| 245 | - switch ($type) { | |
| 81 | + public static function sanitize( $value, $type = 'text' ){ | |
| 82 | + switch ( $type ) { | |
| 246 | 83 | case 'boolean': |
| 247 | - $sanitized_value = rest_sanitize_boolean($value); | |
| 84 | + $sanitized_value = rest_sanitize_boolean( $value ); | |
| 248 | 85 | break; |
| 249 | 86 | default: |
| 250 | - $sanitized_value = sanitize_text_field($value); | |
| 87 | + $sanitized_value = sanitize_text_field( $value ); | |
| 251 | 88 | break; |
| 252 | 89 | } |
| 253 | 90 | |
| 254 | 91 | return $sanitized_value; |
| @@ -254,194 +91,8 @@ | ||
| 254 | 91 | return $sanitized_value; |
| 255 | 92 | } |
| 256 | 93 | |
| 257 | 94 | /** |
| 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 | 95 | * API Error Formatter |
| 445 | 96 | * |
| 446 | 97 | * @param int $error_code |
| 447 | 98 | * @param mixed $error_message |
| @@ -449,18 +100,15 @@ | ||
| 449 | 100 | * @param integer $status |
| 450 | 101 | * @param array $additional_data |
| 451 | 102 | * @return WP_Error |
| 452 | 103 | */ |
| 453 | - public static function error($error_code, $error_message, $endpoint = '', $status = 500, $additional_data = []) { | |
| 104 | + public static function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) { | |
| 454 | 105 | $additional_data['status'] = $status; |
| 455 | - if (! empty($endpoint)) { | |
| 106 | + if ( ! empty( $endpoint ) ) { | |
| 456 | 107 | $additional_data['endpoint'] = $endpoint; |
| 457 | 108 | } |
| 458 | - // Add browser padding to avoid browsers not serving small JSON responses | |
| 459 | - $padding_length = 512; | |
| 460 | - $additional_data['browser_padding'] = str_repeat(' ', $padding_length); | |
| 461 | 109 | |
| 462 | - return new WP_Error($error_code, $error_message, $additional_data); | |
| 110 | + return new WP_Error( $error_code, $error_message, $additional_data ); | |
| 463 | 111 | } |
| 464 | 112 | |
| 465 | 113 | /** |
| 466 | 114 | * API Response Formatter |
| @@ -467,10 +115,10 @@ | ||
| 467 | 115 | * |
| 468 | 116 | * @param mixed $data |
| 469 | 117 | * @return WP_REST_Response |
| 470 | 118 | */ |
| 471 | - public static function success($data) { | |
| 472 | - return new WP_REST_Response($data, 200); | |
| 119 | + public static function success( $data ) { | |
| 120 | + return new WP_REST_Response( $data, 200 ); | |
| 473 | 121 | } |
| 474 | 122 | |
| 475 | 123 | /** |
| 476 | 124 | * Normalize Favourites Data |
| @@ -480,46 +128,31 @@ | ||
| 480 | 128 | * @param boolean $undo |
| 481 | 129 | * |
| 482 | 130 | * @return array |
| 483 | 131 | */ |
| 484 | - public function normalizeFavourites($favourites, $_favourites = [], $undo = false) { | |
| 485 | - if ($undo) { | |
| 486 | - $_favourites[$favourites['type']] = array_values(array_filter($_favourites[$favourites['type']], function ($item) use ($favourites) { | |
| 132 | + public function normalizeFavourites( $favourites, $_favourites = [], $undo = false ){ | |
| 133 | + if( $undo ) { | |
| 134 | + $_favourites[ $favourites['type'] ] = array_values(array_filter( $_favourites[ $favourites['type'] ], function( $item ) use( $favourites ) { | |
| 487 | 135 | return $item != $favourites['id']; |
| 488 | - })); | |
| 136 | + } )); | |
| 489 | 137 | return $_favourites; |
| 490 | 138 | } |
| 491 | 139 | |
| 492 | - array_map(function ($item) use (&$_favourites) { | |
| 493 | - if (! is_null($item)) { | |
| 140 | + array_map( function( $item ) use ( &$_favourites) { | |
| 141 | + if( ! is_null( $item ) ) { | |
| 494 | 142 | $item = (array) $item; |
| 495 | - if (isset($_favourites[$item['type']])) { | |
| 496 | - $_favourites[$item['type']][] = $item['id']; | |
| 143 | + if ( isset( $_favourites[ $item['type'] ] ) ){ | |
| 144 | + $_favourites[ $item['type'] ][] = $item['id']; | |
| 497 | 145 | } else { |
| 498 | - $_favourites[$item['type']] = [$item['id']]; | |
| 146 | + $_favourites[ $item['type'] ] = [ $item['id'] ]; | |
| 499 | 147 | } |
| 500 | 148 | } |
| 501 | 149 | return $_favourites; |
| 502 | - }, $favourites); | |
| 150 | + }, $favourites ); | |
| 503 | 151 | |
| 504 | 152 | return $_favourites; |
| 505 | 153 | } |
| 506 | 154 | |
| 507 | - public function normalizeReviews($favourites, $_favourites = [], $undo = false) { | |
| 508 | - array_map(function ($item) use (&$_favourites) { | |
| 509 | - if (! is_null($item)) { | |
| 510 | - $item = (array) $item; | |
| 511 | - if (!isset($_favourites[$item['type']])) { | |
| 512 | - $_favourites[$item['type']] = []; | |
| 513 | - } | |
| 514 | - $_favourites[$item['type']][$item['type_id']] = $item['rating']; | |
| 515 | - } | |
| 516 | - return $_favourites; | |
| 517 | - }, $favourites); | |
| 518 | - | |
| 519 | - return $_favourites; | |
| 520 | - } | |
| 521 | - | |
| 522 | 155 | /** |
| 523 | 156 | * Trigger Error |
| 524 | 157 | * |
| 525 | 158 | * @param object $triggered_by |
| @@ -524,10 +157,10 @@ | ||
| 524 | 157 | * |
| 525 | 158 | * @param object $triggered_by |
| 526 | 159 | * @return void |
| 527 | 160 | */ |
| 528 | - public static function trigger_error($triggered_by, $method = 'get_instance') { | |
| 529 | - $class = get_class($triggered_by); | |
| 161 | + public static function trigger_error( $triggered_by, $method = 'get_instance' ){ | |
| 162 | + $class = get_class( $triggered_by ); | |
| 530 | 163 | $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
| 531 | 164 | $file = $trace[0]['file']; |
| 532 | 165 | $line = $trace[0]['line']; |
| 533 | 166 | trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR); |
| @@ -535,214 +168,17 @@ | ||
| 535 | 168 | |
| 536 | 169 | /** |
| 537 | 170 | * Printing Error Logs in debug.log file. |
| 538 | 171 | * |
| 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) | |
| 172 | + * @param mixed $log | |
| 542 | 173 | * @return void |
| 543 | 174 | */ |
| 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; | |
| 549 | - } | |
| 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 | - } | |
| 562 | - | |
| 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 | - public static function should_flush() { | |
| 591 | - if (isset($_REQUEST['is_lightspeed']) && $_REQUEST['is_lightspeed'] === 'true') { | |
| 592 | - return false; | |
| 593 | - } | |
| 594 | - return (!defined('TEMPLATELY_IGNORE_FLUSH_ALL') || !TEMPLATELY_IGNORE_FLUSH_ALL) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') === false; | |
| 595 | - } | |
| 596 | - | |
| 597 | - public static function get_block_by_name($blocks, $search) { | |
| 598 | - $queue = $blocks; | |
| 599 | - | |
| 600 | - while (!empty($queue)) { | |
| 601 | - $current_block = array_shift($queue); | |
| 602 | - | |
| 603 | - if ($search === $current_block['blockName']) { | |
| 604 | - return $current_block; | |
| 605 | - } | |
| 606 | - | |
| 607 | - if (isset($current_block['innerBlocks'])) { | |
| 608 | - // Add nested blocks to the end of the queue for processing | |
| 609 | - $queue = array_merge($queue, $current_block['innerBlocks']); | |
| 610 | - } | |
| 611 | - } | |
| 612 | - | |
| 613 | - return false; | |
| 614 | - } | |
| 615 | - | |
| 616 | - /** | |
| 617 | - * Only checks if user can install/activate plugins | |
| 618 | - * | |
| 619 | - * @param [type] $cap | |
| 620 | - * @param [type] ...$args | |
| 621 | - * @return void | |
| 622 | - */ | |
| 623 | - public static function current_user_can($cap, ...$args) { | |
| 624 | - $user = wp_get_current_user(); | |
| 625 | - | |
| 626 | - // Multisite super admin has all caps by definition, Unless specifically denied. | |
| 627 | - if (is_multisite() && is_super_admin($user->ID)) { | |
| 628 | - return true; | |
| 629 | - } | |
| 630 | - | |
| 631 | - $caps = map_meta_cap($cap, $user->ID, ...$args); | |
| 632 | - | |
| 633 | - switch ($cap) { | |
| 634 | - case 'install_plugins': | |
| 635 | - case 'upload_plugins': | |
| 636 | - $caps = ['install_plugins']; | |
| 637 | - break; | |
| 638 | - case 'install_themes': | |
| 639 | - case 'upload_themes': | |
| 640 | - $caps = ['install_themes']; | |
| 641 | - break; | |
| 642 | - case 'activate_plugins': | |
| 643 | - case 'deactivate_plugins': | |
| 644 | - case 'activate_plugin': | |
| 645 | - case 'deactivate_plugin': | |
| 646 | - $caps = ['activate_plugins']; | |
| 647 | - break; | |
| 648 | - default: | |
| 649 | - break; | |
| 650 | - } | |
| 651 | - | |
| 652 | - // Maintain BC for the argument passed to the "user_has_cap" filter. | |
| 653 | - $args = array_merge(array($cap, $user->ID), $args); | |
| 654 | - | |
| 655 | - /** | |
| 656 | - * See WP_User::has_cap() for description. | |
| 657 | - */ | |
| 658 | - $capabilities = apply_filters('user_has_cap', $user->allcaps, $caps, $args, $user); | |
| 659 | - | |
| 660 | - // Everyone is allowed to exist. | |
| 661 | - $capabilities['exist'] = true; | |
| 662 | - | |
| 663 | - // Nobody is allowed to do things they are not allowed to do. | |
| 664 | - unset($capabilities['do_not_allow']); | |
| 665 | - | |
| 666 | - // Must have ALL requested caps. | |
| 667 | - foreach ((array) $caps as $cap) { | |
| 668 | - if (empty($capabilities[$cap])) { | |
| 669 | - return false; | |
| 670 | - } | |
| 671 | - } | |
| 672 | - | |
| 673 | - return true; | |
| 674 | - } | |
| 675 | - | |
| 676 | - /** | |
| 677 | - * Calculates the elapsed time and checks if it is close to the maximum execution time. | |
| 678 | - * Returns true if the script should exit to avoid exceeding the limit. | |
| 679 | - * | |
| 680 | - * @return bool True if the script should exit, false otherwise. | |
| 681 | - */ | |
| 682 | - public static function fsi_should_exit() { | |
| 683 | - if (defined('TEMPLATELY_START_TIME') && ini_get('max_execution_time')) { | |
| 684 | - $max_time = ini_get('max_execution_time'); | |
| 685 | - $elapsed = microtime(true) - TEMPLATELY_START_TIME; | |
| 686 | - $delay = max(5, $max_time * 20 / 100); | |
| 687 | - | |
| 688 | - // Check if elapsed time is close to max execution time | |
| 689 | - if ($max_time - $elapsed <= $delay) { | |
| 690 | - return ['max_time' => $max_time, 'elapsed' => $elapsed, 'delay' => $delay]; | |
| 691 | - } | |
| 692 | - } | |
| 693 | - return false; | |
| 694 | - } | |
| 695 | - | |
| 696 | - /** | |
| 697 | - * Enable Elementor Container | |
| 698 | - * This function will enable the Elementor Container feature. | |
| 699 | - * Without this feature, some of the templates may not work properly. | |
| 700 | - * | |
| 701 | - * @return boolean | |
| 702 | - */ | |
| 703 | - public static function enable_elementor_container() { | |
| 704 | - if (class_exists('Elementor\Plugin')) { | |
| 705 | - $control_name = Plugin::instance()->experiments->get_feature_option_key('container'); | |
| 706 | - if (get_option($control_name) !== 'active') { | |
| 707 | - update_option($control_name, 'active'); | |
| 708 | - return true; | |
| 709 | - } | |
| 710 | - } | |
| 711 | - return false; | |
| 712 | - } | |
| 713 | - | |
| 714 | - /** | |
| 715 | - * Undocumented function | |
| 716 | - * | |
| 717 | - * @param [type] $args | |
| 718 | - * @param [type] $defaults | |
| 719 | - * @return array | |
| 720 | - */ | |
| 721 | - public static function recursive_wp_parse_args($args, $defaults) { | |
| 722 | - $args = (array) $args; | |
| 723 | - $defaults = (array) $defaults; | |
| 724 | - $r = $defaults; | |
| 725 | - foreach ($args as $key => $value) { | |
| 726 | - if (is_array($value) && isset($r[$key])) { | |
| 727 | - // also handle numeric array. if both $value and $r[ $key ] are numeric array. wp_is_numeric_array() | |
| 728 | - if (wp_is_numeric_array($value) && wp_is_numeric_array($r[$key])) { | |
| 729 | - foreach ($value as $k => $v) { | |
| 730 | - if (!in_array($v, $r[$key])) { | |
| 731 | - if (!isset($r[$key][$k])) { | |
| 732 | - $r[$key][$k] = $v; | |
| 733 | - } else { | |
| 734 | - $r[$key][] = $v; | |
| 735 | - } | |
| 736 | - } | |
| 737 | - } | |
| 738 | - } else { | |
| 739 | - $r[$key] = self::recursive_wp_parse_args($value, $r[$key]); | |
| 740 | - } | |
| 175 | + public static function log( $log ){ | |
| 176 | + if ( WP_DEBUG_LOG === true ) { | |
| 177 | + if ( is_array( $log ) || is_object( $log ) ) { | |
| 178 | + error_log( print_r( $log, true ) ); | |
| 741 | 179 | } else { |
| 742 | - $r[$key] = $value; | |
| 180 | + error_log( $log ); | |
| 743 | 181 | } |
| 744 | 182 | } |
| 745 | - return $r; | |
| 746 | 183 | } |
| 747 | - | |
| 748 | -} | |
| 184 | +} | |