| @@ -2,9 +2,10 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Templately\Utils; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Plugin; |
| 6 | -use Templately\Core\Importer\Utils\Utils; | |
| 6 | +use Templately\Modules\FullSiteImport\Utils\Utils; | |
| 7 | +use Templately\Utils\Response\ResponseNormalizer; | |
| 7 | 8 | use WP_Error; |
| 8 | 9 | use WP_REST_Response; |
| 9 | 10 | use function get_plugins; |
| 10 | 11 | use function is_plugin_active; |
| @@ -22,13 +23,58 @@ | ||
| 22 | 23 | * |
| 23 | 24 | * @return bool True if development API should be used |
| 24 | 25 | */ |
| 25 | 26 | public static function is_dev_api(){ |
| 26 | - // Only check TEMPLATELY_DEV_API constant - no fallback mechanisms | |
| 27 | + // Developer OVERRIDE: a PHP constant can't be redefined at runtime, so when the | |
| 28 | + // override is explicitly unlocked (`templately_developer_override` option), the | |
| 29 | + // saved developer setting takes priority over the wp-config constant. Read | |
| 30 | + // directly (no Developer-class dependency — that module is dev-only) and OFF by | |
| 31 | + // default, so production behaviour is unchanged (constant-only). | |
| 32 | + if ( get_option( 'templately_developer_override', false ) ) { | |
| 33 | + $settings = get_option( 'templately_developer_settings', [] ); | |
| 34 | + if ( is_array( $settings ) && array_key_exists( 'TEMPLATELY_DEV_API', $settings ) ) { | |
| 35 | + return (bool) $settings['TEMPLATELY_DEV_API']; | |
| 36 | + } | |
| 37 | + } | |
| 27 | 38 | return defined( 'TEMPLATELY_DEV_API' ) && constant( 'TEMPLATELY_DEV_API' ); |
| 28 | 39 | } |
| 29 | 40 | |
| 30 | 41 | /** |
| 42 | + * The singular post-type label for the content currently being viewed — i.e. what a | |
| 43 | + * conditioned header/footer/single/archive template is decorating on this request | |
| 44 | + * ("Post" on a single post, "Page" on a page, "Product" on a shop/product). Null when | |
| 45 | + * there's no meaningful post type (e.g. 404). Shared so BOTH theme-builder admin bars | |
| 46 | + * (Elementor `DocumentManager::filter_admin_bar_labels` + Gutenberg `AdminBar`) badge | |
| 47 | + * the same way — the pill reads the post type, not the template type. | |
| 48 | + * | |
| 49 | + * @return string|null | |
| 50 | + */ | |
| 51 | + public static function current_context_post_type_label(): ?string { | |
| 52 | + $post_type = ''; | |
| 53 | + | |
| 54 | + if ( is_singular() ) { | |
| 55 | + $post_type = get_post_type( get_queried_object_id() ); | |
| 56 | + } elseif ( function_exists( 'is_shop' ) && is_shop() ) { | |
| 57 | + $post_type = 'product'; | |
| 58 | + } elseif ( is_post_type_archive() ) { | |
| 59 | + $post_type = get_query_var( 'post_type' ); | |
| 60 | + if ( is_array( $post_type ) ) { | |
| 61 | + $post_type = reset( $post_type ); | |
| 62 | + } | |
| 63 | + } elseif ( is_home() ) { | |
| 64 | + $post_type = 'post'; | |
| 65 | + } | |
| 66 | + | |
| 67 | + if ( empty( $post_type ) ) { | |
| 68 | + return null; | |
| 69 | + } | |
| 70 | + | |
| 71 | + $object = get_post_type_object( $post_type ); | |
| 72 | + | |
| 73 | + return $object ? $object->labels->singular_name : null; | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 31 | 77 | * Get installed WordPress Plugin List |
| 32 | 78 | * @return array |
| 33 | 79 | */ |
| 34 | 80 | public static function get_plugins() { |
| @@ -116,9 +162,9 @@ | ||
| 116 | 162 | |
| 117 | 163 | /** |
| 118 | 164 | * A URL on the public Templately website, honouring the dev domain. |
| 119 | 165 | * |
| 120 | - * The PHP counterpart of `react-src/utils/helper.js#webURL`. A hard-coded | |
| 166 | + * The PHP counterpart of `react-src/utils/helper.ts#webURL`. A hard-coded | |
| 121 | 167 | * `https://templately.com/...` sends a site running against the dev API to |
| 122 | 168 | * the live site, where its account does not exist — so build every out-link |
| 123 | 169 | * through this instead. |
| 124 | 170 | * |
| @@ -155,8 +201,68 @@ | ||
| 155 | 201 | return "{$base_url}/api/{$endpoint}"; |
| 156 | 202 | } |
| 157 | 203 | |
| 158 | 204 | /** |
| 205 | + * Resolve the requesting host platform ('templately' | 'ai-builder' | ...), | |
| 206 | + * forwarded to the cloud as the `x-templately-requested-platform` header so the | |
| 207 | + * cloud can scope behaviour per host (e.g. waive the pack-purchase gate for the | |
| 208 | + * AI Builder onboarding). Resolution order: | |
| 209 | + * 1. the incoming `X-Templately-Requested-Platform` HTTP header — a host (e.g. | |
| 210 | + * ai-builder) sets it ONCE via an apiFetch middleware / fetch header, so no | |
| 211 | + * per-call body param is needed; | |
| 212 | + * 2. the `requested_platform` request param (back-compat with explicit callers); | |
| 213 | + * 3. the `templately_requested_platform` filter (programmatic override); | |
| 214 | + * 4. the fallback (default 'templately'). | |
| 215 | + * | |
| 216 | + * @param string $fallback Value when nothing else resolves. | |
| 217 | + * @return string | |
| 218 | + */ | |
| 219 | + public static function get_requested_platform( $fallback = 'templately' ) { | |
| 220 | + $platform = ''; | |
| 221 | + if ( ! empty( $_SERVER['HTTP_X_TEMPLATELY_REQUESTED_PLATFORM'] ) ) { | |
| 222 | + $platform = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_TEMPLATELY_REQUESTED_PLATFORM'] ) ); | |
| 223 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended -- reading a host marker, not acting on form data. | |
| 224 | + } elseif ( isset( $_REQUEST['requested_platform'] ) ) { | |
| 225 | + $platform = sanitize_text_field( wp_unslash( $_REQUEST['requested_platform'] ) ); | |
| 226 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended | |
| 227 | + } | |
| 228 | + | |
| 229 | + if ( '' === $platform ) { | |
| 230 | + $platform = $fallback; | |
| 231 | + } | |
| 232 | + | |
| 233 | + return apply_filters( 'templately_requested_platform', $platform ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Resolve the entry-point/source marker for the current request — which surface | |
| 238 | + * initiated the flow (admin SPA, editor toolbar, editor add-section, …). Mirrors | |
| 239 | + * get_requested_platform(): incoming `X-Templately-Source` header → | |
| 240 | + * `$_REQUEST['templately_source']` → filter. Empty string means "unknown" and is | |
| 241 | + * NOT forwarded to the cloud. Values are engagement telemetry only — never branch | |
| 242 | + * behaviour on them. | |
| 243 | + * | |
| 244 | + * @param string $fallback Value when nothing else resolves. | |
| 245 | + * @return string | |
| 246 | + */ | |
| 247 | + public static function get_request_source( $fallback = '' ) { | |
| 248 | + $source = ''; | |
| 249 | + if ( ! empty( $_SERVER['HTTP_X_TEMPLATELY_SOURCE'] ) ) { | |
| 250 | + $source = sanitize_key( wp_unslash( $_SERVER['HTTP_X_TEMPLATELY_SOURCE'] ) ); | |
| 251 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended -- reading a telemetry marker, not acting on form data. | |
| 252 | + } elseif ( isset( $_REQUEST['templately_source'] ) ) { | |
| 253 | + $source = sanitize_key( wp_unslash( $_REQUEST['templately_source'] ) ); | |
| 254 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended | |
| 255 | + } | |
| 256 | + | |
| 257 | + if ( '' === $source ) { | |
| 258 | + $source = $fallback; | |
| 259 | + } | |
| 260 | + | |
| 261 | + return apply_filters( 'templately_request_source', $source ); | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 159 | 265 | * Make a unified API request to Templately API |
| 160 | 266 | * |
| 161 | 267 | * @param string $method HTTP method (GET or POST) |
| 162 | 268 | * @param string $api_url Complete API URL |
| @@ -184,18 +290,35 @@ | ||
| 184 | 290 | if (strtoupper($method) === 'POST') { |
| 185 | 291 | $headers['Content-Type'] = 'application/json'; |
| 186 | 292 | } |
| 187 | 293 | |
| 188 | - // Resolve requested platform: $_REQUEST wins (frontend-supplied), then caller's extra_headers, then default. | |
| 189 | - if ( isset( $_REQUEST['requested_platform'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 190 | - $extra_headers['x-templately-requested-platform'] = sanitize_text_field( wp_unslash( $_REQUEST['requested_platform'] ) ); | |
| 191 | - } elseif ( ! isset( $extra_headers['x-templately-requested-platform'] ) ) { | |
| 192 | - $extra_headers['x-templately-requested-platform'] = 'templately'; | |
| 294 | + // Resolve the requesting host platform centrally. A caller-supplied extra_headers | |
| 295 | + // value still wins (explicit override); otherwise resolve from the incoming | |
| 296 | + // header / request param / filter (see Helper::get_requested_platform). | |
| 297 | + if ( ! isset( $extra_headers['x-templately-requested-platform'] ) ) { | |
| 298 | + $extra_headers['x-templately-requested-platform'] = self::get_requested_platform(); | |
| 193 | 299 | } |
| 194 | 300 | |
| 301 | + // Entry-point marker (engagement telemetry). A URL QUERY PARAM, not a header: | |
| 302 | + // the cloud's ordinary access logs capture the request URL, so the marker is | |
| 303 | + // countable with zero cloud-side code. No separate tracking calls are ever | |
| 304 | + // made (wp.org policy) — this only annotates requests that happen anyway. | |
| 305 | + // Only appended when known; absent means "unknown", not an error. | |
| 306 | + if ( '' !== self::get_request_source() ) { | |
| 307 | + $api_url = add_query_arg( 'tl_source', self::get_request_source(), $api_url ); | |
| 308 | + } | |
| 309 | + | |
| 195 | 310 | // Merge additional headers |
| 196 | 311 | $headers = array_merge($headers, $extra_headers); |
| 197 | 312 | |
| 313 | + // A request the HTTP layer is allowed to spend $timeout seconds on can still | |
| 314 | + // be killed by PHP's OWN max_execution_time, which is 30s on a great many | |
| 315 | + // hosts. When that happens there is no WP_Error and no response to inspect: | |
| 316 | + // PHP fatals mid-request, WordPress answers with its HTML "critical error" | |
| 317 | + // page, and a JSON client renders that markup as if it were content. Give | |
| 318 | + // PHP enough runway for the timeout we are about to ask for. | |
| 319 | + self::extend_time_limit( $timeout ); | |
| 320 | + | |
| 198 | 321 | $args = [ |
| 199 | 322 | 'timeout' => $timeout, |
| 200 | 323 | 'headers' => $headers, |
| 201 | 324 | ]; |
| @@ -225,8 +348,51 @@ | ||
| 225 | 348 | return $response; |
| 226 | 349 | } |
| 227 | 350 | |
| 228 | 351 | /** |
| 352 | + * GET a Templately API endpoint and return the NORMALIZED result (spec 043). | |
| 353 | + * | |
| 354 | + * This is the REST twin of `Http::post()`. Prefer it over | |
| 355 | + * `make_api_get_request()`: that one hands back the raw `wp_remote_*` array and | |
| 356 | + * leaves every caller to invent its own "did this fail?" check — which is how | |
| 357 | + * the plugin ended up reading one server ten different ways. | |
| 358 | + * | |
| 359 | + * Side-effects (verification / disconnection) are already applied by | |
| 360 | + * `make_api_request()`, so the normalizer is told to skip them rather than | |
| 361 | + * repeat the work. | |
| 362 | + * | |
| 363 | + * @param string $endpoint API endpoint path (e.g. 'v2/import/info/pack/123'). | |
| 364 | + * @param array $query_params Query parameters. | |
| 365 | + * @param array $extra_headers Additional headers. | |
| 366 | + * @param int $timeout Seconds. | |
| 367 | + * @param array $options Normalizer options (e.g. [ 'raw' => true ] for binary). | |
| 368 | + * @return \Templately\Utils\Response\RemoteResponse | |
| 369 | + */ | |
| 370 | + public static function api_get( $endpoint, $query_params = [], $extra_headers = [], $timeout = 30, $options = [] ) { | |
| 371 | + return ResponseNormalizer::normalize( | |
| 372 | + self::make_api_get_request( $endpoint, $query_params, $extra_headers, $timeout ), | |
| 373 | + array_merge( [ 'side_effects' => false ], $options ) | |
| 374 | + ); | |
| 375 | + } | |
| 376 | + | |
| 377 | + /** | |
| 378 | + * POST to a Templately API endpoint and return the NORMALIZED result (spec 043). | |
| 379 | + * | |
| 380 | + * @param string $endpoint API endpoint path. | |
| 381 | + * @param array $body Request body. | |
| 382 | + * @param array $extra_headers Additional headers. | |
| 383 | + * @param int $timeout Seconds. | |
| 384 | + * @param array $options Normalizer options. | |
| 385 | + * @return \Templately\Utils\Response\RemoteResponse | |
| 386 | + */ | |
| 387 | + public static function api_post( $endpoint, $body = [], $extra_headers = [], $timeout = 30, $options = [] ) { | |
| 388 | + return ResponseNormalizer::normalize( | |
| 389 | + self::make_api_post_request( $endpoint, $body, $extra_headers, $timeout ), | |
| 390 | + array_merge( [ 'side_effects' => false ], $options ) | |
| 391 | + ); | |
| 392 | + } | |
| 393 | + | |
| 394 | + /** | |
| 229 | 395 | * Make a GET request to Templately API |
| 230 | 396 | * |
| 231 | 397 | * @param string $endpoint API endpoint path (e.g., 'v2/import/pack/123') |
| 232 | 398 | * @param array $query_params Query parameters as key-value pairs |
| @@ -236,11 +402,20 @@ | ||
| 236 | 402 | */ |
| 237 | 403 | public static function make_api_get_request($endpoint, $query_params = [], $extra_headers = [], $timeout = 30) { |
| 238 | 404 | $api_url = self::get_api_url($endpoint); |
| 239 | 405 | |
| 240 | - // Add query parameters if provided | |
| 406 | + // Add query parameters if provided. | |
| 407 | + // | |
| 408 | + // `http_build_query()`, NOT `add_query_arg()`: the latter does not encode the values | |
| 409 | + // it is given (only the ones already on the URL, via its own `urlencode_deep`), so a | |
| 410 | + // value carrying `&` silently became two parameters and a value carrying `#` truncated | |
| 411 | + // the rest of the query into a fragment the server never sees. Every param here comes | |
| 412 | + // from somewhere a user can type — an image search term, a business description, a hex | |
| 413 | + // colour — so that is not a theoretical shape. It also flattens arrays as `k[0]=…`, | |
| 414 | + // which is what PHP on the other end parses back into an array. | |
| 241 | 415 | if (!empty($query_params)) { |
| 242 | - $api_url = add_query_arg($query_params, $api_url); | |
| 416 | + $separator = false === strpos($api_url, '?') ? '?' : '&'; | |
| 417 | + $api_url .= $separator . http_build_query($query_params, '', '&', PHP_QUERY_RFC3986); | |
| 243 | 418 | } |
| 244 | 419 | |
| 245 | 420 | return self::make_api_request('GET', $api_url, [], $extra_headers, $timeout); |
| 246 | 421 | } |
| @@ -259,8 +434,37 @@ | ||
| 259 | 434 | return self::make_api_request('POST', $api_url, $body, $extra_headers, $timeout); |
| 260 | 435 | } |
| 261 | 436 | |
| 262 | 437 | /** |
| 438 | + * Ensure PHP will not cut the process short before an HTTP call of $timeout | |
| 439 | + * seconds can finish. | |
| 440 | + * | |
| 441 | + * Only ever RAISES the limit, and only when the configured one is too small; | |
| 442 | + * a host that has already granted more keeps it. `max_execution_time` of 0 | |
| 443 | + * means "no limit" (CLI, WP-Cron on some setups) and needs nothing. When the | |
| 444 | + * host has disabled `set_time_limit()`, `function_exists()` is false and we | |
| 445 | + * leave the request to take its chances rather than emit a warning. | |
| 446 | + * | |
| 447 | + * @param int $timeout Seconds the pending HTTP request may take. | |
| 448 | + * @return void | |
| 449 | + */ | |
| 450 | + private static function extend_time_limit( $timeout ) { | |
| 451 | + $limit = (int) ini_get( 'max_execution_time' ); | |
| 452 | + | |
| 453 | + if ( $limit <= 0 || ! function_exists( 'set_time_limit' ) ) { | |
| 454 | + return; | |
| 455 | + } | |
| 456 | + | |
| 457 | + // The margin covers everything around the call that also runs on this | |
| 458 | + // clock: building the payload, and the response handling afterwards. | |
| 459 | + $needed = (int) $timeout + 30; | |
| 460 | + | |
| 461 | + if ( $needed > $limit ) { | |
| 462 | + @set_time_limit( $needed ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- host may refuse; the request still proceeds. | |
| 463 | + } | |
| 464 | + } | |
| 465 | + | |
| 466 | + /** | |
| 263 | 467 | * Sanitize Helper |
| 264 | 468 | * |
| 265 | 469 | * @param mixed $value |
| 266 | 470 | * @param string $type |
| @@ -318,8 +522,33 @@ | ||
| 318 | 522 | return substr( $encoded, 1, -1 ); |
| 319 | 523 | } |
| 320 | 524 | |
| 321 | 525 | /** |
| 526 | + * Flip the stored user's `is_verified` flag on. | |
| 527 | + * | |
| 528 | + * Extracted for spec 043 so the normalizer can apply the same side-effect | |
| 529 | + * when verification arrives in the response BODY (the connect mutation) | |
| 530 | + * rather than in the `X-Templately-Verified` header. | |
| 531 | + * | |
| 532 | + * @return array the (possibly updated) user option; empty when no user is stored. | |
| 533 | + */ | |
| 534 | + public static function mark_user_verified() { | |
| 535 | + $options = Options::get_instance(); | |
| 536 | + $user = $options->get('user'); | |
| 537 | + | |
| 538 | + if (empty($user) || !is_array($user)) { | |
| 539 | + return []; | |
| 540 | + } | |
| 541 | + | |
| 542 | + if (empty($user['is_verified'])) { | |
| 543 | + $user['is_verified'] = true; | |
| 544 | + $options->set('user', $user); | |
| 545 | + } | |
| 546 | + | |
| 547 | + return $user; | |
| 548 | + } | |
| 549 | + | |
| 550 | + /** | |
| 322 | 551 | * Check for X-Templately-Verified header and update user verification status |
| 323 | 552 | * |
| 324 | 553 | * @param array|WP_Error $response The HTTP response array from wp_remote_get/wp_remote_post |
| 325 | 554 | * @return void |
| @@ -332,25 +561,14 @@ | ||
| 332 | 561 | |
| 333 | 562 | // Retrieve the X-Templately-Verified header |
| 334 | 563 | $verification_header = wp_remote_retrieve_header($response, 'X-Templately-Verified'); |
| 335 | 564 | |
| 336 | - // Check if header exists and has a truthy value | |
| 565 | + // Check if header exists and has a truthy value. | |
| 566 | + // An empty/absent header means "no change" — never "unverified" (043 D6). | |
| 337 | 567 | if (!empty($verification_header) && filter_var($verification_header, FILTER_VALIDATE_BOOLEAN)) { |
| 338 | 568 | try { |
| 339 | - // Get current user data | |
| 340 | - $options = Options::get_instance(); | |
| 341 | - $user = $options->get('user'); | |
| 569 | + $user = self::mark_user_verified(); | |
| 342 | 570 | |
| 343 | - // Only update if user data exists and is not already verified | |
| 344 | - if (!empty($user) && is_array($user) && empty($user['is_verified'])) { | |
| 345 | - // Set verification flag | |
| 346 | - $user['is_verified'] = true; | |
| 347 | - | |
| 348 | - // Save updated user data | |
| 349 | - $options->set('user', $user); | |
| 350 | - | |
| 351 | - } | |
| 352 | - | |
| 353 | 571 | if (!empty($user['is_verified'])){ |
| 354 | 572 | if(!headers_sent()){ |
| 355 | 573 | header( 'X-Templately-Verified: true' ); |
| 356 | 574 | if (defined('TEMPLATELY_DEBUG_LOG') && constant('TEMPLATELY_DEBUG_LOG')) { |
| @@ -476,16 +694,32 @@ | ||
| 476 | 694 | * @param array $additional_data |
| 477 | 695 | * @return WP_Error |
| 478 | 696 | */ |
| 479 | 697 | public static function error($error_code, $error_message, $endpoint = '', $status = 500, $additional_data = []) { |
| 480 | - $additional_data['status'] = $status; | |
| 698 | + // The status reaches WP's REST server as `data.status` and decides the HTTP | |
| 699 | + // code. A STRING there ("404") makes WP fall back to 500, so a caller that | |
| 700 | + // passed a numeric string got the wrong status on the wire. | |
| 701 | + $additional_data['status'] = (int) $status; | |
| 702 | + | |
| 703 | + // An array message is field-level validation detail, not a sentence. Left in | |
| 704 | + // `message` it renders as "Array" to the user; routed to `fields` it is | |
| 705 | + // structured data the client can attach to the right input (FR-007). | |
| 706 | + if (is_array($error_message)) { | |
| 707 | + $additional_data['fields'] = array_merge( | |
| 708 | + isset($additional_data['fields']) && is_array($additional_data['fields']) ? $additional_data['fields'] : [], | |
| 709 | + $error_message | |
| 710 | + ); | |
| 711 | + $error_message = __('Please correct the highlighted fields.', 'templately'); | |
| 712 | + } | |
| 481 | 713 | if (! empty($endpoint)) { |
| 482 | 714 | $additional_data['endpoint'] = $endpoint; |
| 483 | 715 | } |
| 484 | - // Add browser padding to avoid browsers not serving small JSON responses | |
| 485 | - $padding_length = 512; | |
| 486 | - $additional_data['browser_padding'] = str_repeat(' ', $padding_length); | |
| 487 | - | |
| 716 | + // Small-response browser padding is NOT added here any more (043 FR-008a). | |
| 717 | + // It used to staple 512 bytes into every error's data bag unconditionally, | |
| 718 | + // which put a junk field inside the response contract and padded bodies | |
| 719 | + // that were never small enough to need it. `RestEnvelope` now applies it | |
| 720 | + // once, centrally, only below the size threshold, and as a header — so the | |
| 721 | + // body stays exactly the shape the schema describes. | |
| 488 | 722 | return new WP_Error($error_code, $error_message, $additional_data); |
| 489 | 723 | } |
| 490 | 724 | |
| 491 | 725 | /** |
| @@ -555,13 +789,14 @@ | ||
| 555 | 789 | $class = get_class($triggered_by); |
| 556 | 790 | $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
| 557 | 791 | $file = $trace[0]['file']; |
| 558 | 792 | $line = $trace[0]['line']; |
| 559 | - trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR); | |
| 793 | + trigger_error( esc_html( "Call to undefined method $class::$method() in $file on line $line" ), E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error | |
| 560 | 794 | } |
| 561 | 795 | |
| 562 | 796 | /** |
| 563 | - * Printing Error Logs in debug.log file. | |
| 797 | + * Write a line to Templately's dedicated log file (uploads/templately/log/), | |
| 798 | + * NOT the site-wide debug.log — see {@see \Templately\Utils\Log\LogFile}. | |
| 564 | 799 | * |
| 565 | 800 | * @param mixed $log The data to log |
| 566 | 801 | * @param string $context Optional context for categorizing log entries |
| 567 | 802 | * @param string $level Optional log level (debug, info, warning, error) |
| @@ -573,10 +808,14 @@ | ||
| 573 | 808 | if ($override_result !== null) { |
| 574 | 809 | return; |
| 575 | 810 | } |
| 576 | 811 | |
| 577 | - // Only log if WP_DEBUG_LOG is enabled | |
| 578 | - if (!defined('WP_DEBUG_LOG') || !WP_DEBUG_LOG) { | |
| 812 | + // The default sink is WP_DEBUG_LOG-gated; additional sinks subscribed to | |
| 813 | + // the `templately_log` action see entries regardless (a capture module | |
| 814 | + // must not depend on the file gate). Skip the formatting work entirely | |
| 815 | + // when nobody would receive the entry. | |
| 816 | + $gate_open = defined('WP_DEBUG_LOG') && WP_DEBUG_LOG; | |
| 817 | + if (!$gate_open && !has_action('templately_log')) { | |
| 579 | 818 | return; |
| 580 | 819 | } |
| 581 | 820 | |
| 582 | 821 | // Format the log message |
| @@ -581,10 +820,40 @@ | ||
| 581 | 820 | |
| 582 | 821 | // Format the log message |
| 583 | 822 | $formatted_message = self::format_log_message($log, $context, $level); |
| 584 | 823 | |
| 585 | - // Write to error log | |
| 586 | - error_log($formatted_message); | |
| 824 | + /** | |
| 825 | + * Fan-out: every subscribed module receives every entry (http-inspector | |
| 826 | + * capture, dev console, telemetry…). Listeners are independent sinks — | |
| 827 | + * this is how MULTIPLE loggers coexist without replacing each other. | |
| 828 | + * | |
| 829 | + * @param string $formatted_message The full formatted line. | |
| 830 | + * @param mixed $log The raw payload passed to log(). | |
| 831 | + * @param string $context Context label. | |
| 832 | + * @param string $level debug|info|warning|error. | |
| 833 | + */ | |
| 834 | + do_action('templately_log', $formatted_message, $log, $context, $level); | |
| 835 | + | |
| 836 | + if (!$gate_open) { | |
| 837 | + return; | |
| 838 | + } | |
| 839 | + | |
| 840 | + /** | |
| 841 | + * The DEFAULT sink, replaceable/decoratable: a filter receives the | |
| 842 | + * current sink callable and may return a wrapper around it (decorator — | |
| 843 | + * filter priority defines wrap order) or a substitute. Core's default is | |
| 844 | + * the dedicated uploads log file (error_log fallback inside). Note: | |
| 845 | + * entries logged before a module boots (e.g. Modules_Manager discovery | |
| 846 | + * notices) necessarily use the core default. | |
| 847 | + * | |
| 848 | + * @param callable $sink function( string $formatted_line ): void | |
| 849 | + */ | |
| 850 | + $sink = apply_filters('templately_log_sink', [Log\LogFile::class, 'write']); | |
| 851 | + if (is_callable($sink)) { | |
| 852 | + $sink($formatted_message); | |
| 853 | + } else { | |
| 854 | + Log\LogFile::write($formatted_message); | |
| 855 | + } | |
| 587 | 856 | } |
| 588 | 857 | |
| 589 | 858 | /** |
| 590 | 859 | * Format log message with context and level |
| @@ -613,12 +882,19 @@ | ||
| 613 | 882 | } |
| 614 | 883 | } |
| 615 | 884 | |
| 616 | 885 | public static function should_flush() { |
| 617 | - if (isset($_REQUEST['is_lightspeed']) && $_REQUEST['is_lightspeed'] === 'true') { | |
| 886 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- a read-only capability probe, not a state change. | |
| 887 | + $is_lightspeed = isset($_REQUEST['is_lightspeed']) ? sanitize_text_field(wp_unslash($_REQUEST['is_lightspeed'])) : ''; | |
| 888 | + if ('true' === $is_lightspeed) { | |
| 618 | 889 | return false; |
| 619 | 890 | } |
| 620 | - return (!defined('TEMPLATELY_IGNORE_FLUSH_ALL') || !TEMPLATELY_IGNORE_FLUSH_ALL) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') === false; | |
| 891 | + | |
| 892 | + // SERVER_SOFTWARE is not guaranteed to be set — some SAPIs (and CLI) omit | |
| 893 | + // it entirely, and passing null to strpos() is a fatal on PHP 8.1+. | |
| 894 | + $server_software = isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : ''; | |
| 895 | + | |
| 896 | + return (!defined('TEMPLATELY_IGNORE_FLUSH_ALL') || !TEMPLATELY_IGNORE_FLUSH_ALL) && strpos($server_software, 'LiteSpeed') === false; | |
| 621 | 897 | } |
| 622 | 898 | |
| 623 | 899 | public static function get_block_by_name($blocks, $search) { |
| 624 | 900 | $queue = $blocks; |
| @@ -705,18 +981,38 @@ | ||
| 705 | 981 | * |
| 706 | 982 | * @return bool True if the script should exit, false otherwise. |
| 707 | 983 | */ |
| 708 | 984 | public static function fsi_should_exit() { |
| 709 | - if (defined('TEMPLATELY_START_TIME') && ini_get('max_execution_time')) { | |
| 710 | - $max_time = ini_get('max_execution_time'); | |
| 711 | - $elapsed = microtime(true) - TEMPLATELY_START_TIME; | |
| 712 | - $delay = max(5, $max_time * 20 / 100); | |
| 985 | + if (!defined('TEMPLATELY_START_TIME')) { | |
| 986 | + return false; | |
| 987 | + } | |
| 713 | 988 | |
| 714 | - // Check if elapsed time is close to max execution time | |
| 715 | - if ($max_time - $elapsed <= $delay) { | |
| 716 | - return ['max_time' => $max_time, 'elapsed' => $elapsed, 'delay' => $delay]; | |
| 989 | + $max_time = (int) ini_get('max_execution_time'); | |
| 990 | + $elapsed = microtime(true) - TEMPLATELY_START_TIME; | |
| 991 | + | |
| 992 | + if ($max_time <= 0) { | |
| 993 | + // The import request calls set_time_limit(0), which zeroes | |
| 994 | + // max_execution_time — but gateways (FPM, LiteSpeed, nginx proxies) | |
| 995 | + // still kill the request on THEIR clock, typically at 60s. With no | |
| 996 | + // PHP limit to lean on, budget against a wall-clock ceiling instead | |
| 997 | + // so runners keep chunking gracefully (and persisting their | |
| 998 | + // backup_attributes) instead of dying mid-loop to a 504. | |
| 999 | + $budget = (int) apply_filters('templately_fsi_request_time_budget', 25); | |
| 1000 | + | |
| 1001 | + if ($budget > 0 && $elapsed >= $budget) { | |
| 1002 | + return ['max_time' => $budget, 'elapsed' => $elapsed, 'delay' => 0]; | |
| 717 | 1003 | } |
| 1004 | + | |
| 1005 | + return false; | |
| 718 | 1006 | } |
| 1007 | + | |
| 1008 | + $delay = max(5, $max_time * 20 / 100); | |
| 1009 | + | |
| 1010 | + // Check if elapsed time is close to max execution time | |
| 1011 | + if ($max_time - $elapsed <= $delay) { | |
| 1012 | + return ['max_time' => $max_time, 'elapsed' => $elapsed, 'delay' => $delay]; | |
| 1013 | + } | |
| 1014 | + | |
| 719 | 1015 | return false; |
| 720 | 1016 | } |
| 721 | 1017 | |
| 722 | 1018 | /** |
| @@ -743,8 +1039,42 @@ | ||
| 743 | 1039 | * @param [type] $args |
| 744 | 1040 | * @param [type] $defaults |
| 745 | 1041 | * @return array |
| 746 | 1042 | */ |
| 1043 | + /** | |
| 1044 | + * Returns true when the current WordPress site is running on a private/loopback | |
| 1045 | + * host where an external API server cannot push callbacks (localhost, *.local, | |
| 1046 | + * *.test, RFC-1918 addresses). Used as a fallback when the remote API does not | |
| 1047 | + * return an explicit is_local_site flag. | |
| 1048 | + */ | |
| 1049 | + public static function is_local_site(): bool { | |
| 1050 | + $host = (string) parse_url( get_option( 'siteurl' ), PHP_URL_HOST ); | |
| 1051 | + // Strip port if present (e.g. "localhost:8888") | |
| 1052 | + $host = (string) preg_replace( '/:\d+$/', '', $host ); | |
| 1053 | + | |
| 1054 | + if ( in_array( $host, [ 'localhost', '127.0.0.1', '::1' ], true ) ) { | |
| 1055 | + return true; | |
| 1056 | + } | |
| 1057 | + // Common local/dev TLDs that no public DNS resolves (an external API server | |
| 1058 | + // cannot push callbacks to them): .local/.test (mDNS/RFC-6761), .tst (the | |
| 1059 | + // WPDeveloper sandbox), .localhost, .dev. | |
| 1060 | + // `substr()` rather than `str_ends_with()`: the plugin advertises WordPress 5.0 / | |
| 1061 | + // PHP 7.2, `str_ends_with()` is PHP 8.0+, and core only polyfills it from WP 5.9 — | |
| 1062 | + // so the call fatals on a host inside our own advertised floor. | |
| 1063 | + foreach ( [ '.local', '.test', '.tst', '.localhost' ] as $suffix ) { | |
| 1064 | + if ( substr( $host, - strlen( $suffix ) ) === $suffix ) { | |
| 1065 | + return true; | |
| 1066 | + } | |
| 1067 | + } | |
| 1068 | + // RFC-1918 private ranges | |
| 1069 | + if ( preg_match( '/^192\.168\./', $host ) | |
| 1070 | + || preg_match( '/^10\./', $host ) | |
| 1071 | + || preg_match( '/^172\.(1[6-9]|2[0-9]|3[01])\./', $host ) ) { | |
| 1072 | + return true; | |
| 1073 | + } | |
| 1074 | + return false; | |
| 1075 | + } | |
| 1076 | + | |
| 747 | 1077 | public static function recursive_wp_parse_args($args, $defaults) { |
| 748 | 1078 | $args = (array) $args; |
| 749 | 1079 | $defaults = (array) $defaults; |
| 750 | 1080 | $r = $defaults; |