| @@ -14,8 +14,13 @@ | ||
| 14 | 14 | declare(strict_types=1); |
| 15 | 15 | |
| 16 | 16 | namespace ThinkRank\API; |
| 17 | 17 | |
| 18 | +// Prevent direct access | |
| 19 | +if (!defined('ABSPATH')) { | |
| 20 | + exit; | |
| 21 | +} | |
| 22 | + | |
| 18 | 23 | use ThinkRank\SEO\Sitemap_Generator; |
| 19 | 24 | use ThinkRank\API\Traits\CSRF_Protection; |
| 20 | 25 | use ThinkRank\API\Traits\Context_Authorization; |
| 21 | 26 | use WP_REST_Controller; |
| @@ -22,8 +27,13 @@ | ||
| 22 | 27 | use WP_REST_Request; |
| 23 | 28 | use WP_REST_Response; |
| 24 | 29 | use WP_Error; |
| 25 | 30 | |
| 31 | +// Prevent direct access | |
| 32 | +if (!defined('ABSPATH')) { | |
| 33 | + exit; | |
| 34 | +} | |
| 35 | + | |
| 26 | 36 | // Load CSRF Protection trait |
| 27 | 37 | require_once THINKRANK_PLUGIN_DIR . 'includes/api/traits/trait-csrf-protection.php'; |
| 28 | 38 | require_once THINKRANK_PLUGIN_DIR . 'includes/api/traits/trait-context-authorization.php'; |
| 29 | 39 | |
| @@ -254,8 +264,14 @@ | ||
| 254 | 264 | $timestamp = gmdate('c'); |
| 255 | 265 | $settings = $this->sitemap_generator->get_settings('site'); |
| 256 | 266 | $settings['last_generated'] = $timestamp; |
| 257 | 267 | $this->sitemap_generator->save_settings('site', null, $settings); |
| 268 | + | |
| 269 | + // This generation wrote the same files the outstanding automatic rebuild | |
| 270 | + // was queued to write, so clear its marker (and any recorded failure) | |
| 271 | + // instead of leaving a request-time takeover to repeat the work. | |
| 272 | + $this->sitemap_generator->mark_regeneration_complete(); | |
| 273 | + | |
| 258 | 274 | return $timestamp; |
| 259 | 275 | } |
| 260 | 276 | |
| 261 | 277 | /** |
| @@ -456,9 +472,21 @@ | ||
| 456 | 472 | $filename = 'sitemap.xml'; |
| 457 | 473 | if (!empty($options['sitemap_urls'][0]['url'])) { |
| 458 | 474 | $filename = basename(wp_parse_url($options['sitemap_urls'][0]['url'], PHP_URL_PATH)); |
| 459 | 475 | } |
| 460 | - $this->save_sitemap_file($sitemap_xml, $filename); | |
| 476 | + // A failed write has to surface here the way the index | |
| 477 | + // branch surfaces one. Discarding it let record_generation() | |
| 478 | + // advance last_generated and clear the pending marker and | |
| 479 | + // the recorded failure, so an unwritable site root — the | |
| 480 | + // exact case this endpoint reports health for — came back | |
| 481 | + // as a healthy "Generated successfully". | |
| 482 | + if (!$this->save_sitemap_file($sitemap_xml, $filename)) { | |
| 483 | + return new WP_Error( | |
| 484 | + 'sitemap_generation_failed', | |
| 485 | + 'Failed to save sitemap: ' . $filename, | |
| 486 | + ['status' => 500] | |
| 487 | + ); | |
| 488 | + } | |
| 461 | 489 | |
| 462 | 490 | // Regenerate the standalone local business sitemap on the |
| 463 | 491 | // single-sitemap path too (parity with Rank Math). |
| 464 | 492 | $this->sitemap_generator->regenerate_local_sitemap($options); |
| @@ -902,9 +930,14 @@ | ||
| 902 | 930 | 'success' => true, |
| 903 | 931 | 'data' => [ |
| 904 | 932 | 'settings' => $settings, |
| 905 | 933 | 'context_type' => $context_type, |
| 906 | - 'context_id' => $context_id | |
| 934 | + 'context_id' => $context_id, | |
| 935 | + // Kept out of `settings` on purpose: this is generator state, | |
| 936 | + // not something the settings POST round-trips. | |
| 937 | + 'health' => $context_type === 'site' | |
| 938 | + ? $this->sitemap_generator->get_regeneration_health() | |
| 939 | + : null | |
| 907 | 940 | ], |
| 908 | 941 | 'message' => 'Sitemap settings retrieved successfully' |
| 909 | 942 | ], 200); |
| 910 | 943 | |
| @@ -1294,9 +1327,9 @@ | ||
| 1294 | 1327 | * @since 1.0.0 |
| 1295 | 1328 | * @return bool True if lock acquired |
| 1296 | 1329 | */ |
| 1297 | 1330 | private function acquire_generation_lock(): bool { |
| 1298 | - $lock_key = 'thinkrank_sitemap_generation_lock'; | |
| 1331 | + $lock_key = Sitemap_Generator::GENERATION_LOCK_TRANSIENT; | |
| 1299 | 1332 | |
| 1300 | 1333 | if (get_transient($lock_key)) { |
| 1301 | 1334 | return false; // Generation already in progress |
| 1302 | 1335 | } |
| @@ -1311,7 +1344,7 @@ | ||
| 1311 | 1344 | * @since 1.0.0 |
| 1312 | 1345 | * @return void |
| 1313 | 1346 | */ |
| 1314 | 1347 | private function release_generation_lock(): void { |
| 1315 | - delete_transient('thinkrank_sitemap_generation_lock'); | |
| 1348 | + delete_transient(Sitemap_Generator::GENERATION_LOCK_TRANSIENT); | |
| 1316 | 1349 | } |
| 1317 | 1350 | } |