connected() && (bool) ( new \Agentimus\Settings() )->get( 'cf_purge_on_change', true ) && ! $settings->purge_denied(); } /** * Drop specific URLs from the edge cache — the AUTOMATIC path. Quiet no-op * unless {@see armed()}; otherwise the outcome — clean or failed — is * recorded on the connection so the panel's rail can tell the owner without * a nag: a permission refusal (401/403) records ONCE and stands the * automatic path down until a reconnect or a clean manual purge re-arms it. * * @param string[] $urls Absolute URLs. * @param Settings|null $settings Injectable for tests. * @param Client|null $client Injectable for tests. * @return void */ public static function purge_urls( array $urls, ?Settings $settings = null, ?Client $client = null ) { $settings = $settings ? $settings : new Settings(); if ( ! self::armed( $settings ) || empty( $urls ) ) { return; } $client = $client ? $client : new Client(); $out = $client->purge_urls( $settings->token(), (string) $settings->get( 'zone_id' ), $urls, self::AUTO_TIMEOUT ); // The count the client already worked out and nobody was reading: it // stops at the first failing batch and reports how many landed before // it. Recorded, because "30 of 47 cleared" and "nothing cleared" are // different situations for the owner, and only one of them means the // page they are looking at is fresh. $settings->record_purge( isset( $out['error'] ) ? (string) $out['error'] : '', self::is_refusal( $out ), count( $urls ), (int) ( isset( $out['purged'] ) ? $out['purged'] : 0 ) ); } /** * A permission refusal, as opposed to a transient fault: 401/403 means the * token cannot purge and every retry would fail identically; anything else * (a timeout, a 5xx) deserves a fresh attempt on the next occasion. * * @param array $out A Client purge result. * @return bool */ private static function is_refusal( array $out ) { $status = (int) ( isset( $out['status'] ) ? $out['status'] : 0 ); return isset( $out['error'] ) && in_array( $status, array( 401, 403 ), true ); } /** * Drop everything the edge holds for the zone — the owner's button, for the * "this page keeps serving stale" moment. Outcome recorded the same way. * * @param Settings|null $settings Injectable for tests. * @param Client|null $client Injectable for tests. * @return array { ok: bool, error?: string } */ public static function purge_all( ?Settings $settings = null, ?Client $client = null ) { $settings = $settings ? $settings : new Settings(); if ( ! $settings->connected() ) { return array( 'ok' => false, 'error' => __( 'Connect Cloudflare first.', 'agentimus' ) ); } $client = $client ? $client : new Client(); $out = $client->purge_all( $settings->token(), (string) $settings->get( 'zone_id' ) ); $error = isset( $out['error'] ) ? (string) $out['error'] : ''; // A clean manual purge re-arms the automatic path (record_purge clears // the denial); a refused one stands it down — same verdict either way. $settings->record_purge( $error, self::is_refusal( $out ) ); return '' === $error ? array( 'ok' => true ) : array( 'ok' => false, 'error' => $error ); } }