settings = $settings; } /** * @return array|\WP_Error { purged: bool, zone: string, purgedAt: int, message: string }. */ public function purge() { if ( ! $this->settings->enabled( 'agent_writes_maintenance' ) ) { return new \WP_Error( 'agentimus_maintenance_off', __( 'Agents may not purge the cache on this site. The owner can allow it under MCP Server, with the “Refresh data and purge the cache” extra. Nothing was purged.', 'agentimus' ), array( 'status' => 403 ) ); } $cloudflare = new CloudflareSettings(); if ( ! $cloudflare->connected() ) { return new \WP_Error( 'agentimus_refresh_not_connected', __( 'Cloudflare is not connected on this site, so there is no cache to purge. Connecting it is up to the owner.', 'agentimus' ), array( 'status' => 409 ) ); } $count = (int) get_transient( self::COUNT_TRANSIENT ); if ( $count >= self::HOURLY_LIMIT ) { return new \WP_Error( 'agentimus_purge_rate_limited', sprintf( /* translators: %d: purges allowed per hour. */ __( 'Agents have purged this site’s Cloudflare cache %d times in the last hour, which is the limit. Nothing was purged. Try again later.', 'agentimus' ), self::HOURLY_LIMIT ), array( 'status' => 429 ) ); } set_transient( self::COUNT_TRANSIENT, $count + 1, HOUR_IN_SECONDS ); $out = Purge::purge_all( $cloudflare ); if ( empty( $out['ok'] ) ) { return new \WP_Error( 'agentimus_purge_failed', (string) ( isset( $out['error'] ) ? $out['error'] : __( 'Cloudflare did not confirm the purge.', 'agentimus' ) ), array( 'status' => 502 ) ); } $view = $cloudflare->public_view(); return array( 'purged' => true, 'zone' => (string) $view['zoneName'], 'purgedAt' => (int) $view['lastPurgeAt'], 'message' => __( 'Cloudflare’s cache for this site is empty. It rebuilds as visitors return, and the site may be slower until then.', 'agentimus' ), ); } }