'POST', 'callback' => [$this, 'handle'], 'permission_callback' => static fn (): bool => current_user_can('manage_options'), ]); } public static function url(): string { return rest_url(self::NAMESPACE . self::ROUTE); } public function handle(): WP_REST_Response { $notice = 'divi_check_running'; if (self::lock()) { try { $report = ($this->check)()->run(get_current_user_id()); ($this->reports ?? new CheckReportStore())->save($report); ReleaseGate::invalidate(); $notice = 'divi_checked'; } catch (Throwable $e) { Log::failure('divi check', $e); $notice = 'divi_check_failed'; } finally { delete_option(self::LOCK); } } return new WP_REST_Response( null, 303, ['Location' => SettingsPage::url(DiviTab::SLUG, ['lit_mcp_notice' => $notice])] ); } private static function lock(): bool { global $wpdb; if (self::insertLock($wpdb, time())) { return true; } // get_var(), not get_option(): the row was written with raw SQL, bypassing the options cache. $since = $wpdb->get_var($wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", self::LOCK )); if ($since === null) { // The lock was released between our failed insert and this read; try once more. return self::insertLock($wpdb, time()); } if (time() - (int) $since < self::LOCK_TTL) { return false; } $taken = $wpdb->query($wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND option_value = %s", (string) time(), self::LOCK, $since )); return (int) $taken === 1; } /** INSERT IGNORE: 1 row affected = the lock did not exist and is now ours; 0 = someone else holds it. */ private static function insertLock(\wpdb $wpdb, int $now): bool { $inserted = $wpdb->query($wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", self::LOCK, (string) $now )); return (int) $inserted === 1; } }