| @@ -144,8 +144,98 @@ | ||
| 144 | 144 | */ |
| 145 | 145 | public const REGENERATION_ERROR_OPTION = 'thinkrank_sitemap_regeneration_error'; |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | + * Delivery modes accepted by the `delivery_mode` setting. | |
| 149 | + * | |
| 150 | + * Mirrors LLMs_Txt_Manager::DELIVERY_MODES, which solved the same problem | |
| 151 | + * for llms.txt. `auto` is the only value a site should normally need. | |
| 152 | + * | |
| 153 | + * @since 2.9.0 | |
| 154 | + * @var string[] | |
| 155 | + */ | |
| 156 | + public const DELIVERY_MODES = ['auto', 'static', 'dynamic']; | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Cache group for documents rendered on the dynamic path. | |
| 160 | + * | |
| 161 | + * @since 2.9.0 | |
| 162 | + * @var string | |
| 163 | + */ | |
| 164 | + private const DYNAMIC_CACHE_PREFIX = 'thinkrank_sitemap_doc_'; | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * How long a dynamically rendered document is cached. | |
| 168 | + * | |
| 169 | + * Invalidated by content and settings changes through | |
| 170 | + * {@see self::flush_dynamic_cache()}, so this is only the backstop for a | |
| 171 | + * change nothing hooked. | |
| 172 | + * | |
| 173 | + * @since 2.9.0 | |
| 174 | + * @var int | |
| 175 | + */ | |
| 176 | + private const DYNAMIC_CACHE_TTL = 12 * HOUR_IN_SECONDS; | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * How long the render lock is held before it is assumed abandoned. | |
| 180 | + * | |
| 181 | + * Long enough for a large site's full build, short enough that a request | |
| 182 | + * killed mid-build does not lock the endpoint out for meaningfully long. | |
| 183 | + * | |
| 184 | + * @since 2.9.0 | |
| 185 | + * @var int | |
| 186 | + */ | |
| 187 | + private const RENDER_LOCK_TTL = 60; | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Cached stand-in for "this site does not publish that name". | |
| 191 | + * | |
| 192 | + * published_document_names() lists what the configuration *could* produce, | |
| 193 | + * but a child whose type is excluded produces nothing. Without a negative | |
| 194 | + * entry those names miss the cache forever, so every request for one | |
| 195 | + * rebuilt the entire sitemap — the same cost the positive cache exists to | |
| 196 | + * avoid, on a public endpoint (#754 review). | |
| 197 | + * | |
| 198 | + * @since 2.9.0 | |
| 199 | + * @var string | |
| 200 | + */ | |
| 201 | + private const ABSENT_MARKER = "\0thinkrank-absent"; | |
| 202 | + | |
| 203 | + /** | |
| 204 | + * How many times, and how long, a losing request waits for the winner. | |
| 205 | + * | |
| 206 | + * Bounded at roughly a second in total: past that, building a second copy | |
| 207 | + * costs less than making a crawler wait. | |
| 208 | + * | |
| 209 | + * @since 2.9.0 | |
| 210 | + * @var int | |
| 211 | + */ | |
| 212 | + private const RENDER_LOCK_WAIT_ATTEMPTS = 4; | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * @since 2.9.0 | |
| 216 | + * @var int | |
| 217 | + */ | |
| 218 | + private const RENDER_LOCK_WAIT_MICROSECONDS = 250000; | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Where generated documents go instead of disk, when set. | |
| 222 | + * | |
| 223 | + * Every sitemap document this class produces — segments, the index, the | |
| 224 | + * single flat file and local-sitemap.xml — is published through the one | |
| 225 | + * writer, {@see self::save_sitemap_to_file()}. Swapping that writer for a | |
| 226 | + * collector is therefore all it takes to render the same bytes without a | |
| 227 | + * filesystem, which is what dynamic delivery needs (#752). Doing it here | |
| 228 | + * rather than duplicating the build pipeline is deliberate: a second | |
| 229 | + * pipeline would drift from this one, and the index in particular is | |
| 230 | + * assembled from whatever the children actually produced. | |
| 231 | + * | |
| 232 | + * @since 2.9.0 | |
| 233 | + * @var callable|null | |
| 234 | + */ | |
| 235 | + private $document_sink = null; | |
| 236 | + | |
| 237 | + /** | |
| 148 | 238 | * Transient guarding against two generations running at once. Shared with |
| 149 | 239 | * Sitemap_Endpoint's manual generate route so an automatic rebuild and a |
| 150 | 240 | * manual one cannot write the same files concurrently. |
| 151 | 241 | * |
| @@ -452,8 +542,15 @@ | ||
| 452 | 542 | ] |
| 453 | 543 | ], |
| 454 | 544 | 'use_sitemap_index' => false, |
| 455 | 545 | |
| 546 | + // How the sitemap reaches crawlers. 'auto' keeps the historical | |
| 547 | + // behaviour wherever the web root is writable, and only falls back | |
| 548 | + // to serving the sitemap from PHP where writing a file is | |
| 549 | + // impossible — previously a hard failure with nothing served | |
| 550 | + // (#752). | |
| 551 | + 'delivery_mode' => 'auto', | |
| 552 | + | |
| 456 | 553 | // General Settings |
| 457 | 554 | 'links_per_sitemap' => 1000, |
| 458 | 555 | 'include_images' => true, |
| 459 | 556 | 'include_featured_images' => false, |
| @@ -519,8 +616,16 @@ | ||
| 519 | 616 | if (array_key_exists('styling_logo_url', $sanitized)) { |
| 520 | 617 | $sanitized['styling_logo_url'] = esc_url_raw((string) $sanitized['styling_logo_url']); |
| 521 | 618 | } |
| 522 | 619 | |
| 620 | + // A mode this build cannot act on has to be stored as the fallback | |
| 621 | + // rather than kept verbatim, or get-sitemap-settings reports a delivery | |
| 622 | + // mode the site does not actually apply. | |
| 623 | + if (array_key_exists('delivery_mode', $sanitized)) { | |
| 624 | + $mode = sanitize_key((string) $sanitized['delivery_mode']); | |
| 625 | + $sanitized['delivery_mode'] = in_array($mode, self::DELIVERY_MODES, true) ? $mode : 'auto'; | |
| 626 | + } | |
| 627 | + | |
| 523 | 628 | return $sanitized; |
| 524 | 629 | } |
| 525 | 630 | |
| 526 | 631 | /** |
| @@ -538,8 +643,15 @@ | ||
| 538 | 643 | 'title' => 'Enable Sitemap', |
| 539 | 644 | 'description' => 'Generate XML sitemap for search engines', |
| 540 | 645 | 'default' => true |
| 541 | 646 | ], |
| 647 | + 'delivery_mode' => [ | |
| 648 | + 'type' => 'string', | |
| 649 | + 'title' => 'Sitemap Delivery', | |
| 650 | + 'description' => 'How the sitemap is served: auto picks static when the WordPress root is writable and dynamic when it is not, static writes files to the web root, dynamic serves the sitemap from WordPress with no files written', | |
| 651 | + 'enum' => self::DELIVERY_MODES, | |
| 652 | + 'default' => 'auto' | |
| 653 | + ], | |
| 542 | 654 | 'include_posts' => [ |
| 543 | 655 | 'type' => 'boolean', |
| 544 | 656 | 'title' => 'Include Posts', |
| 545 | 657 | 'description' => 'Include blog posts in sitemap', |
| @@ -1729,8 +1841,14 @@ | ||
| 1729 | 1841 | * @param string $source Either 'content' or 'settings'. |
| 1730 | 1842 | * @return void |
| 1731 | 1843 | */ |
| 1732 | 1844 | private function mark_regeneration_pending(string $source): void { |
| 1845 | + // Whatever made the static files stale made the rendered ones stale | |
| 1846 | + // too. Invalidating here rather than only on the rebuild keeps the two | |
| 1847 | + // delivery modes reacting to exactly the same triggers, which is the | |
| 1848 | + // only way a dynamic site stays as fresh as a static one (#752). | |
| 1849 | + $this->flush_dynamic_cache(); | |
| 1850 | + | |
| 1733 | 1851 | $pending = get_option(self::REGENERATION_PENDING_OPTION, []); |
| 1734 | 1852 | $pending = is_array($pending) ? $pending : []; |
| 1735 | 1853 | |
| 1736 | 1854 | $since = !empty($pending['since']) ? (int) $pending['since'] : time(); |
| @@ -2067,13 +2185,18 @@ | ||
| 2067 | 2185 | } |
| 2068 | 2186 | |
| 2069 | 2187 | $revision = $this->current_regeneration_revision(); |
| 2070 | 2188 | |
| 2189 | + if ('dynamic' === $this->resolve_delivery_mode($settings)) { | |
| 2190 | + $this->switch_to_dynamic_delivery($settings, $revision, 'settings'); | |
| 2191 | + return; | |
| 2192 | + } | |
| 2193 | + | |
| 2071 | 2194 | if ($this->generate_and_save($settings)) { |
| 2072 | 2195 | $this->mark_regeneration_complete($revision); |
| 2073 | 2196 | } else { |
| 2074 | 2197 | $this->record_regeneration_failure( |
| 2075 | - __('The sitemap files could not be written to the site root.', 'thinkrank'), | |
| 2198 | + $this->write_failure_message(), | |
| 2076 | 2199 | 'settings' |
| 2077 | 2200 | ); |
| 2078 | 2201 | } |
| 2079 | 2202 | } catch (\Throwable $e) { |
| @@ -2183,10 +2306,17 @@ | ||
| 2183 | 2306 | return; |
| 2184 | 2307 | } |
| 2185 | 2308 | |
| 2186 | 2309 | $revision = $this->current_regeneration_revision(); |
| 2310 | + $settings = $this->get_settings('site'); | |
| 2187 | 2311 | |
| 2188 | - if ($this->generate_and_save($this->get_settings('site'))) { | |
| 2312 | + // See regenerate_sitemap_from_settings(): nothing to write. | |
| 2313 | + if ('dynamic' === $this->resolve_delivery_mode($settings)) { | |
| 2314 | + $this->switch_to_dynamic_delivery($settings, $revision, 'content'); | |
| 2315 | + return; | |
| 2316 | + } | |
| 2317 | + | |
| 2318 | + if ($this->generate_and_save($settings)) { | |
| 2189 | 2319 | $this->mark_regeneration_complete($revision); |
| 2190 | 2320 | } else { |
| 2191 | 2321 | // Previously this returned quietly and last_generated simply |
| 2192 | 2322 | // stopped advancing, leaving the site owner with no way to learn |
| @@ -2191,9 +2321,9 @@ | ||
| 2191 | 2321 | // Previously this returned quietly and last_generated simply |
| 2192 | 2322 | // stopped advancing, leaving the site owner with no way to learn |
| 2193 | 2323 | // the sitemap had stopped updating (#629). |
| 2194 | 2324 | $this->record_regeneration_failure( |
| 2195 | - __('The sitemap files could not be written to the site root.', 'thinkrank'), | |
| 2325 | + $this->write_failure_message(), | |
| 2196 | 2326 | 'content' |
| 2197 | 2327 | ); |
| 2198 | 2328 | } |
| 2199 | 2329 | } catch (\Throwable $e) { |
| @@ -2215,8 +2345,26 @@ | ||
| 2215 | 2345 | * @param array $settings Sitemap settings. |
| 2216 | 2346 | * @return bool True when the sitemap files were written. |
| 2217 | 2347 | */ |
| 2218 | 2348 | public function generate_and_save(array $settings): bool { |
| 2349 | + // Dynamic delivery publishes no files, so writing them here would put a | |
| 2350 | + // static copy back in the web root for the server to serve in place of | |
| 2351 | + // the dynamic route. Guarding at each call site left gaps — the | |
| 2352 | + // snapshot migrator's post-import regeneration had none — so the rule | |
| 2353 | + // lives with the writing instead. | |
| 2354 | + // | |
| 2355 | + // `is_collecting()` is the exception that makes dynamic delivery work | |
| 2356 | + // at all: render_document() and collect_documents() reach this same | |
| 2357 | + // method with the writer swapped for a collector, and that is precisely | |
| 2358 | + // the dynamic build. Only a real write is skipped. | |
| 2359 | + if (!$this->is_collecting() && 'dynamic' === $this->resolve_delivery_mode($settings)) { | |
| 2360 | + // Whatever prompted this call changed the sitemap's content, so the | |
| 2361 | + // rendered copies must not outlive it. | |
| 2362 | + $this->flush_dynamic_cache(); | |
| 2363 | + | |
| 2364 | + return true; | |
| 2365 | + } | |
| 2366 | + | |
| 2219 | 2367 | // Index mode is driven by the use_sitemap_index toggle (not merely by how |
| 2220 | 2368 | // many sitemap_urls happen to be configured). When the toggle is on but |
| 2221 | 2369 | // no child sitemaps are set up yet, synthesize the per-type segmented set |
| 2222 | 2370 | // so we emit a real <sitemapindex> with paginated children instead of a |
| @@ -2243,9 +2391,13 @@ | ||
| 2243 | 2391 | // names is never touched (#515). |
| 2244 | 2392 | $this->prune_orphaned_segments($settings, [['filename' => basename($primary)]]); |
| 2245 | 2393 | } |
| 2246 | 2394 | |
| 2247 | - if ($written) { | |
| 2395 | + // last_generated describes what is on disk. A dynamic render publishes | |
| 2396 | + // nothing, so advancing it would report a static publication that never | |
| 2397 | + // happened and would let primary_sitemap_file_exists() callers believe | |
| 2398 | + // there is a file to serve. | |
| 2399 | + if ($written && !$this->is_collecting()) { | |
| 2248 | 2400 | $settings['last_generated'] = gmdate('c'); |
| 2249 | 2401 | $this->save_settings('site', null, $settings); |
| 2250 | 2402 | } |
| 2251 | 2403 | |
| @@ -2252,8 +2404,398 @@ | ||
| 2252 | 2404 | return $written; |
| 2253 | 2405 | } |
| 2254 | 2406 | |
| 2255 | 2407 | /** |
| 2408 | + * Whether this instance is rendering documents rather than publishing them. | |
| 2409 | + * | |
| 2410 | + * @since 2.9.0 | |
| 2411 | + * | |
| 2412 | + * @return bool | |
| 2413 | + */ | |
| 2414 | + private function is_collecting(): bool { | |
| 2415 | + return $this->document_sink !== null; | |
| 2416 | + } | |
| 2417 | + | |
| 2418 | + /** | |
| 2419 | + * Complete a regeneration that delivers dynamically, retiring stale files. | |
| 2420 | + * | |
| 2421 | + * Dynamic delivery renders nothing to disk, but that is only half the job. | |
| 2422 | + * A web server hands back an existing `/sitemap.xml` without ever loading | |
| 2423 | + * WordPress, so any file left over from a previous static generation goes on | |
| 2424 | + * being served forever and {@see \ThinkRank\Frontend\SEO_Manager | |
| 2425 | + * ::maybe_serve_sitemap()} is never reached. Switching to dynamic while | |
| 2426 | + * leaving those files in place would therefore appear to do nothing at all. | |
| 2427 | + * | |
| 2428 | + * Both transitions matter and they differ: | |
| 2429 | + * | |
| 2430 | + * - An explicit switch to `dynamic` happens on a site whose root is usually | |
| 2431 | + * still writable, so the files can simply be removed. | |
| 2432 | + * - An `auto` site that becomes read-only cannot remove them, because | |
| 2433 | + * deleting an entry needs write permission on the directory that holds | |
| 2434 | + * it. There the stale sitemap really is stuck in front of us, and the | |
| 2435 | + * honest outcome is a recorded failure naming it rather than a rebuild | |
| 2436 | + * reported as complete (#754 review). | |
| 2437 | + * | |
| 2438 | + * Ownership is tested per file by the shared helper, so another plugin's | |
| 2439 | + * sitemap at one of our names is never deleted (#515). | |
| 2440 | + * | |
| 2441 | + * @since 2.9.0 | |
| 2442 | + * | |
| 2443 | + * @param array $settings Sitemap settings. | |
| 2444 | + * @param int $revision Revision this rebuild is completing. | |
| 2445 | + * @param string $source 'settings' or 'content', for the failure record. | |
| 2446 | + * @return void | |
| 2447 | + */ | |
| 2448 | + private function switch_to_dynamic_delivery(array $settings, int $revision, string $source): void { | |
| 2449 | + $this->flush_dynamic_cache(); | |
| 2450 | + | |
| 2451 | + $removal = $this->delete_published_sitemaps($settings); | |
| 2452 | + $stuck = is_array($removal['failed'] ?? null) ? $removal['failed'] : []; | |
| 2453 | + | |
| 2454 | + if (!empty($stuck)) { | |
| 2455 | + $this->record_regeneration_failure( | |
| 2456 | + sprintf( | |
| 2457 | + /* translators: 1: comma-separated file names, 2: absolute path to the WordPress root. */ | |
| 2458 | + __('The sitemap is being served from WordPress, but these files are still in the site root and your web server will keep serving them instead: %1$s. They could not be removed because %2$s is not writable. Delete them, or ask your host to make the WordPress root writable.', 'thinkrank'), | |
| 2459 | + implode(', ', $stuck), | |
| 2460 | + untrailingslashit(ABSPATH) | |
| 2461 | + ), | |
| 2462 | + $source | |
| 2463 | + ); | |
| 2464 | + | |
| 2465 | + return; | |
| 2466 | + } | |
| 2467 | + | |
| 2468 | + $this->mark_regeneration_complete($revision); | |
| 2469 | + } | |
| 2470 | + | |
| 2471 | + /** | |
| 2472 | + * What to tell the site owner when publishing the files failed. | |
| 2473 | + * | |
| 2474 | + * The old wording stated the symptom and stopped there, so the reported | |
| 2475 | + * cause was a guess and this reached support as a plugin fault rather than | |
| 2476 | + * a folder permission (#752, #753). When the root is demonstrably | |
| 2477 | + * unwritable, say that, and say what to do about it. | |
| 2478 | + * | |
| 2479 | + * @since 2.9.0 | |
| 2480 | + * | |
| 2481 | + * @return string | |
| 2482 | + */ | |
| 2483 | + private function write_failure_message(): string { | |
| 2484 | + if (!wp_is_writable(ABSPATH)) { | |
| 2485 | + return sprintf( | |
| 2486 | + /* translators: %s: absolute path to the WordPress root. */ | |
| 2487 | + __('The sitemap could not be written because the folder %s is not writable by PHP. Ask your host to make the WordPress root writable, or set Sitemap Delivery to Dynamic to serve the sitemap without writing files.', 'thinkrank'), | |
| 2488 | + untrailingslashit(ABSPATH) | |
| 2489 | + ); | |
| 2490 | + } | |
| 2491 | + | |
| 2492 | + return __('The sitemap files could not be written to the site root.', 'thinkrank'); | |
| 2493 | + } | |
| 2494 | + | |
| 2495 | + /** | |
| 2496 | + * How this site delivers its sitemap. | |
| 2497 | + * | |
| 2498 | + * `auto` is resolved on whether the web root can be written. That is the | |
| 2499 | + * right signal here (unlike llms.txt, where the question is whether the | |
| 2500 | + * server applies the .htaccess charset block): a site whose root is | |
| 2501 | + * read-only cannot publish a sitemap file at all, and before this existed | |
| 2502 | + * the feature simply failed with "The sitemap files could not be written to | |
| 2503 | + * the site root." and served nothing (#752). | |
| 2504 | + * | |
| 2505 | + * @since 2.9.0 | |
| 2506 | + * | |
| 2507 | + * @param array|null $settings Sitemap settings (falls back to saved ones). | |
| 2508 | + * @return string One of 'static' or 'dynamic'. Never 'auto'. | |
| 2509 | + */ | |
| 2510 | + public function resolve_delivery_mode(?array $settings = null): string { | |
| 2511 | + $settings = $settings ?? $this->get_settings('site'); | |
| 2512 | + $mode = (string) ($settings['delivery_mode'] ?? 'auto'); | |
| 2513 | + | |
| 2514 | + if ('static' === $mode || 'dynamic' === $mode) { | |
| 2515 | + return $mode; | |
| 2516 | + } | |
| 2517 | + | |
| 2518 | + return wp_is_writable(ABSPATH) ? 'static' : 'dynamic'; | |
| 2519 | + } | |
| 2520 | + | |
| 2521 | + /** | |
| 2522 | + * Render one published sitemap document without touching the filesystem. | |
| 2523 | + * | |
| 2524 | + * Runs the ordinary build pipeline with the writer swapped for a collector, | |
| 2525 | + * so the bytes returned here are the bytes the static path would have | |
| 2526 | + * written. `SitemapDeliveryParityTest` asserts that equivalence rather than | |
| 2527 | + * trusting it. | |
| 2528 | + * | |
| 2529 | + * The whole set is built to answer for one file, because the index can only | |
| 2530 | + * be assembled from the children that were actually produced. The result is | |
| 2531 | + * cached per document, so that cost is paid once per change and not once | |
| 2532 | + * per crawler request. | |
| 2533 | + * | |
| 2534 | + * @since 2.9.0 | |
| 2535 | + * | |
| 2536 | + * @param string $filename Published file name, e.g. 'sitemap.xml'. | |
| 2537 | + * @param array|null $settings Sitemap settings (falls back to saved ones). | |
| 2538 | + * @return string|null XML, or null when this site does not publish that name. | |
| 2539 | + */ | |
| 2540 | + public function render_document(string $filename, ?array $settings = null): ?string { | |
| 2541 | + $filename = basename($filename); | |
| 2542 | + $settings = $settings ?? $this->get_settings('site'); | |
| 2543 | + | |
| 2544 | + if (empty($settings['enabled'])) { | |
| 2545 | + return null; | |
| 2546 | + } | |
| 2547 | + | |
| 2548 | + $cached = get_transient($this->dynamic_cache_key($filename)); | |
| 2549 | + if (self::ABSENT_MARKER === $cached) { | |
| 2550 | + return null; | |
| 2551 | + } | |
| 2552 | + if (is_string($cached) && '' !== $cached) { | |
| 2553 | + return $cached; | |
| 2554 | + } | |
| 2555 | + | |
| 2556 | + // A miss builds the whole set, because the index can only be assembled | |
| 2557 | + // from the children that were actually produced. Caching only the | |
| 2558 | + // requested document therefore made a crawler walking the index and its | |
| 2559 | + // children rebuild the entire site's sitemap once per file — every post | |
| 2560 | + // and taxonomy query repeated N times on a public endpoint (#754 | |
| 2561 | + // review). The set is built once and stored in full. | |
| 2562 | + return $this->stream_documents($settings, $filename); | |
| 2563 | + } | |
| 2564 | + | |
| 2565 | + /** | |
| 2566 | + * Build every document, caching each as it is produced, keeping one. | |
| 2567 | + * | |
| 2568 | + * A miss has to build the whole set, because the index can only be | |
| 2569 | + * assembled from the children that were actually produced. It does not have | |
| 2570 | + * to *hold* the whole set: the static path never keeps more than one page | |
| 2571 | + * in memory, writing each to disk as it goes, and buffering every | |
| 2572 | + * document's XML to return one of them undid that on the request path, | |
| 2573 | + * where a large site's entire sitemap corpus would sit in a single PHP | |
| 2574 | + * process (#754 review). | |
| 2575 | + * | |
| 2576 | + * So the sink writes each document straight to its cache entry and lets it | |
| 2577 | + * go, retaining only the one this request is answering. Peak retention is | |
| 2578 | + * one document, whatever the site's size. | |
| 2579 | + * | |
| 2580 | + * Concurrency: the first request through takes a short lock and does the | |
| 2581 | + * work. One that finds the lock held waits a bounded moment for the winner | |
| 2582 | + * to publish, then builds anyway, because serving a correct sitemap late | |
| 2583 | + * beats serving none. | |
| 2584 | + * | |
| 2585 | + * @since 2.9.0 | |
| 2586 | + * | |
| 2587 | + * @param array $settings Sitemap settings. | |
| 2588 | + * @param string $wanted Document this request is answering. | |
| 2589 | + * @return string|null XML for $wanted, or null when the site does not publish it. | |
| 2590 | + */ | |
| 2591 | + private function stream_documents(array $settings, string $wanted): ?string { | |
| 2592 | + $lock = self::DYNAMIC_CACHE_PREFIX . 'lock'; | |
| 2593 | + | |
| 2594 | + if (!$this->acquire_render_lock($lock)) { | |
| 2595 | + for ($attempt = 0; $attempt < self::RENDER_LOCK_WAIT_ATTEMPTS; $attempt++) { | |
| 2596 | + usleep(self::RENDER_LOCK_WAIT_MICROSECONDS); | |
| 2597 | + | |
| 2598 | + $cached = get_transient($this->dynamic_cache_key($wanted)); | |
| 2599 | + if (self::ABSENT_MARKER === $cached) { | |
| 2600 | + return null; | |
| 2601 | + } | |
| 2602 | + if (is_string($cached) && '' !== $cached) { | |
| 2603 | + return $cached; | |
| 2604 | + } | |
| 2605 | + } | |
| 2606 | + } | |
| 2607 | + | |
| 2608 | + $kept = null; | |
| 2609 | + // Names only. Keeping the bodies here would be the very retention this | |
| 2610 | + // method exists to avoid. | |
| 2611 | + $produced = []; | |
| 2612 | + | |
| 2613 | + $previous = $this->document_sink; | |
| 2614 | + $this->document_sink = function (string $name, string $xml) use (&$kept, &$produced, $wanted): void { | |
| 2615 | + $produced[$name] = true; | |
| 2616 | + set_transient($this->dynamic_cache_key($name), $xml, self::DYNAMIC_CACHE_TTL); | |
| 2617 | + | |
| 2618 | + if ($name === $wanted) { | |
| 2619 | + $kept = $xml; | |
| 2620 | + } | |
| 2621 | + }; | |
| 2622 | + | |
| 2623 | + try { | |
| 2624 | + $this->generate_and_save($settings); | |
| 2625 | + | |
| 2626 | + // Names the configuration lists but this build did not produce get | |
| 2627 | + // a negative entry, so asking for one again is a cache hit rather | |
| 2628 | + // than another full rebuild. | |
| 2629 | + $absent = $this->published_document_names($settings); | |
| 2630 | + | |
| 2631 | + // Also the exact name this request asked for: a paginated page past | |
| 2632 | + // the end of a stem is a legitimate request shape that the base | |
| 2633 | + // list cannot enumerate, and without an entry it would rebuild on | |
| 2634 | + // every hit. | |
| 2635 | + $absent[] = $wanted; | |
| 2636 | + | |
| 2637 | + foreach (array_unique($absent) as $name) { | |
| 2638 | + if (!isset($produced[$name])) { | |
| 2639 | + set_transient($this->dynamic_cache_key($name), self::ABSENT_MARKER, self::DYNAMIC_CACHE_TTL); | |
| 2640 | + } | |
| 2641 | + } | |
| 2642 | + } finally { | |
| 2643 | + $this->document_sink = $previous; | |
| 2644 | + delete_transient($lock); | |
| 2645 | + } | |
| 2646 | + | |
| 2647 | + return $kept; | |
| 2648 | + } | |
| 2649 | + | |
| 2650 | + /** | |
| 2651 | + * Take the render lock, if it is free. | |
| 2652 | + * | |
| 2653 | + * Not atomic across processes, and deliberately so: the fallback for losing | |
| 2654 | + * a race is duplicated work, never a wrong or missing sitemap, so a | |
| 2655 | + * heavier primitive would buy nothing here. | |
| 2656 | + * | |
| 2657 | + * @since 2.9.0 | |
| 2658 | + * | |
| 2659 | + * @param string $lock Lock transient name. | |
| 2660 | + * @return bool True when this request holds the lock. | |
| 2661 | + */ | |
| 2662 | + private function acquire_render_lock(string $lock): bool { | |
| 2663 | + if (false !== get_transient($lock)) { | |
| 2664 | + return false; | |
| 2665 | + } | |
| 2666 | + | |
| 2667 | + set_transient($lock, time(), self::RENDER_LOCK_TTL); | |
| 2668 | + | |
| 2669 | + return true; | |
| 2670 | + } | |
| 2671 | + | |
| 2672 | + /** | |
| 2673 | + * Build every document this site publishes and return them all. | |
| 2674 | + * | |
| 2675 | + * Verification and tooling only. This retains the whole set in memory, so | |
| 2676 | + * it must never be used to answer a request: {@see self::stream_documents()} | |
| 2677 | + * is the serving path and keeps one document at a time regardless of site | |
| 2678 | + * size (#754 review). `SitemapDeliveryParityTest` enforces that separation | |
| 2679 | + * by failing if the request path routes back through here. | |
| 2680 | + * | |
| 2681 | + * @since 2.9.0 | |
| 2682 | + * | |
| 2683 | + * @param array $settings Sitemap settings. | |
| 2684 | + * @return array<string,string> Filename => XML. | |
| 2685 | + */ | |
| 2686 | + public function collect_documents(array $settings): array { | |
| 2687 | + $documents = []; | |
| 2688 | + | |
| 2689 | + $previous = $this->document_sink; | |
| 2690 | + $this->document_sink = static function (string $name, string $xml) use (&$documents): void { | |
| 2691 | + $documents[$name] = $xml; | |
| 2692 | + }; | |
| 2693 | + | |
| 2694 | + try { | |
| 2695 | + $this->generate_and_save($settings); | |
| 2696 | + } finally { | |
| 2697 | + $this->document_sink = $previous; | |
| 2698 | + } | |
| 2699 | + | |
| 2700 | + return $documents; | |
| 2701 | + } | |
| 2702 | + | |
| 2703 | + /** | |
| 2704 | + * The file names this site publishes, without building their contents. | |
| 2705 | + * | |
| 2706 | + * Used by the request router to decide whether a URL is ours before doing | |
| 2707 | + * any work. Cheap: it reads the configured child list rather than querying | |
| 2708 | + * for entries. | |
| 2709 | + * | |
| 2710 | + * @since 2.9.0 | |
| 2711 | + * | |
| 2712 | + * @param array|null $settings Sitemap settings (falls back to saved ones). | |
| 2713 | + * @return string[] File names, including paginated pages that may exist. | |
| 2714 | + */ | |
| 2715 | + public function published_document_names(?array $settings = null): array { | |
| 2716 | + $settings = $settings ?? $this->get_settings('site'); | |
| 2717 | + $resolved = $this->maybe_promote_to_index($settings); | |
| 2718 | + | |
| 2719 | + $names = [$this->get_primary_sitemap_filename($settings), 'local-sitemap.xml']; | |
| 2720 | + | |
| 2721 | + foreach ((array) ($resolved['sitemap_urls'] ?? []) as $child) { | |
| 2722 | + if (!is_array($child) || empty($child['enabled'])) { | |
| 2723 | + continue; | |
| 2724 | + } | |
| 2725 | + | |
| 2726 | + $path = (string) wp_parse_url((string) ($child['url'] ?? ''), PHP_URL_PATH); | |
| 2727 | + if ('' !== $path) { | |
| 2728 | + $names[] = basename($path); | |
| 2729 | + } | |
| 2730 | + } | |
| 2731 | + | |
| 2732 | + return array_values(array_unique(array_filter($names))); | |
| 2733 | + } | |
| 2734 | + | |
| 2735 | + /** | |
| 2736 | + * Does this site publish a document under that name? | |
| 2737 | + * | |
| 2738 | + * Not a plain membership test against {@see self::published_document_names()}: | |
| 2739 | + * that lists the configured children, and a child over the per-file URL cap | |
| 2740 | + * is split into `<stem>-2.xml`, `<stem>-3.xml` and so on, with every page | |
| 2741 | + * listed in the index. Gating the request router on the base list alone | |
| 2742 | + * therefore 404'd exactly the pages the index points at, which is worse than | |
| 2743 | + * not serving them at all. | |
| 2744 | + * | |
| 2745 | + * Page counts are not knowable without building, so the stem is what is | |
| 2746 | + * matched; a page that does not exist is answered by the build finding | |
| 2747 | + * nothing for it, and is then cached as absent. | |
| 2748 | + * | |
| 2749 | + * @since 2.9.0 | |
| 2750 | + * | |
| 2751 | + * @param string $name Requested file name. | |
| 2752 | + * @param array|null $settings Sitemap settings (falls back to saved ones). | |
| 2753 | + * @return bool | |
| 2754 | + */ | |
| 2755 | + public function publishes_document_name(string $name, ?array $settings = null): bool { | |
| 2756 | + $names = $this->published_document_names($settings); | |
| 2757 | + | |
| 2758 | + if (in_array($name, $names, true)) { | |
| 2759 | + return true; | |
| 2760 | + } | |
| 2761 | + | |
| 2762 | + if (!preg_match('/^(.*)-\d+\.xml$/i', $name, $m)) { | |
| 2763 | + return false; | |
| 2764 | + } | |
| 2765 | + | |
| 2766 | + return in_array($m[1] . '.xml', $names, true); | |
| 2767 | + } | |
| 2768 | + | |
| 2769 | + /** | |
| 2770 | + * Transient key for a rendered document. | |
| 2771 | + * | |
| 2772 | + * @since 2.9.0 | |
| 2773 | + * | |
| 2774 | + * @param string $filename Published file name. | |
| 2775 | + * @return string | |
| 2776 | + */ | |
| 2777 | + private function dynamic_cache_key(string $filename): string { | |
| 2778 | + return self::DYNAMIC_CACHE_PREFIX . md5($filename); | |
| 2779 | + } | |
| 2780 | + | |
| 2781 | + /** | |
| 2782 | + * Drop every cached dynamic document. | |
| 2783 | + * | |
| 2784 | + * Called from the same places that mark the static files stale, so the two | |
| 2785 | + * delivery modes invalidate on identical triggers. | |
| 2786 | + * | |
| 2787 | + * @since 2.9.0 | |
| 2788 | + * | |
| 2789 | + * @return void | |
| 2790 | + */ | |
| 2791 | + public function flush_dynamic_cache(): void { | |
| 2792 | + foreach ($this->published_document_names() as $name) { | |
| 2793 | + delete_transient($this->dynamic_cache_key($name)); | |
| 2794 | + } | |
| 2795 | + } | |
| 2796 | + | |
| 2797 | + /** | |
| 2256 | 2798 | * Resolve index-vs-single mode, synthesizing child sitemaps when needed. |
| 2257 | 2799 | * |
| 2258 | 2800 | * - When use_sitemap_index is on but no child sitemaps are configured, build |
| 2259 | 2801 | * the per-type segmented set so a real <sitemapindex> is produced (#127). |
| @@ -2448,8 +2990,18 @@ | ||
| 2448 | 2990 | // File validation failed - error details available in exception |
| 2449 | 2991 | return false; |
| 2450 | 2992 | } |
| 2451 | 2993 | |
| 2994 | + // Dynamic delivery: hand the document to the collector instead of the | |
| 2995 | + // filesystem. Reported as published, because for this run it is — the | |
| 2996 | + // caller's success/failure bookkeeping and the index assembly both key | |
| 2997 | + // off this return value. | |
| 2998 | + if ($this->document_sink !== null) { | |
| 2999 | + ($this->document_sink)($filename, $sitemap_xml); | |
| 3000 | + | |
| 3001 | + return true; | |
| 3002 | + } | |
| 3003 | + | |
| 2452 | 3004 | $sitemap_path = ABSPATH . $filename; |
| 2453 | 3005 | |
| 2454 | 3006 | // Use WordPress filesystem API for better security |
| 2455 | 3007 | global $wp_filesystem; |
| @@ -2863,8 +3415,15 @@ | ||
| 2863 | 3415 | * @param array $generated Entries from $results['sitemaps_generated']. |
| 2864 | 3416 | * @return string[] Basenames removed. |
| 2865 | 3417 | */ |
| 2866 | 3418 | private function prune_orphaned_segments(array $settings, array $generated): array { |
| 3419 | + // Rendering for a request, not publishing: there is nothing on disk | |
| 3420 | + // this run owns, and a dynamic render must never delete the files a | |
| 3421 | + // site's previous static mode left behind. | |
| 3422 | + if ($this->is_collecting()) { | |
| 3423 | + return []; | |
| 3424 | + } | |
| 3425 | + | |
| 2867 | 3426 | $kept = []; |
| 2868 | 3427 | foreach ($generated as $entry) { |
| 2869 | 3428 | if (!empty($entry['filename'])) { |
| 2870 | 3429 | $kept[strtolower((string) $entry['filename'])] = true; |
| @@ -2966,9 +3525,9 @@ | ||
| 2966 | 3525 | // publishes under too (this method mirrors it deliberately), so on |
| 2967 | 3526 | // a migrated site the file at that path may never have been ours |
| 2968 | 3527 | // to delete (#515). |
| 2969 | 3528 | $path = ABSPATH . 'local-sitemap.xml'; |
| 2970 | - if (file_exists($path) && $this->webroot_sitemap_is_ours($path, $settings)) { | |
| 3529 | + if (!$this->is_collecting() && file_exists($path) && $this->webroot_sitemap_is_ours($path, $settings)) { | |
| 2971 | 3530 | wp_delete_file($path); |
| 2972 | 3531 | } |
| 2973 | 3532 | return false; |
| 2974 | 3533 | } |
| @@ -2990,8 +3549,25 @@ | ||
| 2990 | 3549 | * |
| 2991 | 3550 | * @since 1.15.x |
| 2992 | 3551 | * @return array Zero or one URL entry |
| 2993 | 3552 | */ |
| 3553 | + /** | |
| 3554 | + * Does this site publish a local business sitemap right now? | |
| 3555 | + * | |
| 3556 | + * The same gate {@see self::regenerate_local_sitemap()} applies, asked | |
| 3557 | + * without writing anything. Callers that need to know whether the document | |
| 3558 | + * exists must not test the filesystem: under dynamic delivery it is served | |
| 3559 | + * from PHP and there is no file, which is how `local-sitemap.xml` came to be | |
| 3560 | + * dropped from robots.txt on exactly those sites (#752). | |
| 3561 | + * | |
| 3562 | + * @since 2.9.0 | |
| 3563 | + * | |
| 3564 | + * @return bool True when the local sitemap has content to publish. | |
| 3565 | + */ | |
| 3566 | + public function publishes_local_sitemap(): bool { | |
| 3567 | + return !empty($this->collect_local_entries()); | |
| 3568 | + } | |
| 3569 | + | |
| 2994 | 3570 | private function collect_local_entries(): array { |
| 2995 | 3571 | if (!class_exists('ThinkRank\\SEO\\Site_Identity_Manager')) { |
| 2996 | 3572 | require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-site-identity-manager.php'; |
| 2997 | 3573 | } |
| @@ -3152,8 +3728,13 @@ | ||
| 3152 | 3728 | * @param array $settings Sitemap settings, for the ownership test. |
| 3153 | 3729 | * @return void |
| 3154 | 3730 | */ |
| 3155 | 3731 | private function cleanup_stale_pages(string $base_url, int $current_pages, array $settings): void { |
| 3732 | + // See prune_orphaned_segments(): a dynamic render deletes nothing. | |
| 3733 | + if ($this->is_collecting()) { | |
| 3734 | + return; | |
| 3735 | + } | |
| 3736 | + | |
| 3156 | 3737 | $filename = basename(wp_parse_url($base_url, PHP_URL_PATH)); |
| 3157 | 3738 | if (!preg_match('/^(.*)\.xml$/i', $filename, $m)) { |
| 3158 | 3739 | return; |
| 3159 | 3740 | } |