__('Not now', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_wizard_dismiss'), 'aipcf_wizard_dismiss'), 'kind' => 'link'];
if (!$connected) {
return Ui::card(__('Set up AIPCF', 'aipcf-ai-provider-for-cloudflare'), Ui::actions([['label' => __('Start setup', 'aipcf-ai-provider-for-cloudflare'), 'url' => Wizard::url(), 'kind' => 'primary']]), [
'text' => __('About five minutes, step by step: connect Cloudflare, choose what you want, set a budget. No credit card.', 'aipcf-ai-provider-for-cloudflare'),
]);
}
if (Wizard::offerReview()) {
return Ui::card(__('Review your setup', 'aipcf-ai-provider-for-cloudflare'), Ui::actions([['label' => __('Review', 'aipcf-ai-provider-for-cloudflare'), 'url' => Wizard::url('goals'), 'kind' => 'primary'], $dismiss]), [
'text' => __('AIPCF 2.0 has a guided setup. Check your features, content index and budget in a few steps; your settings are filled in.', 'aipcf-ai-provider-for-cloudflare'),
]);
}
return '';
}
/**
* Everything that needs the site owner, in one list. Nothing shows when all is well.
*/
private static function needsYou(bool $connected): string
{
$items = [];
if (!$connected) {
$items[] = [__('AIPCF is not connected to Cloudflare yet.', 'aipcf-ai-provider-for-cloudflare'), __('Connect', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('connection')];
} elseif (class_exists('\\WordPress\\AI\\Connector_Approval\\Approvals_Store') && !is_connector_approved()) {
$items[] = [__('The AI plugin blocks AIPCF until you allow it.', 'aipcf-ai-provider-for-cloudflare'), __('Allow now', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::connectorApprovalUrl()];
} else {
// The same check as Help and Site Health: is Cloudflare really answering?
$test = SiteHealth::runTest();
if (($test['status'] ?? 'good') === 'critical') {
$items[] = [(string) $test['label'], __('What to do', 'aipcf-ai-provider-for-cloudflare'), HelpPage::url()];
}
}
if (Gateway::id() !== '' && get_transient(Gateway::DOWN_TRANSIENT)) {
$items[] = [__('The AI Gateway was skipped after an error, for up to an hour.', 'aipcf-ai-provider-for-cloudflare'), __('Check it', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('connection')];
}
$used = (float) (UsageTracker::todayTotals()['neurons'] ?? 0);
$budget = UsageTracker::budgetEnabled() ? UsageTracker::budget() : UsageTracker::FREE_TIER;
if ($connected && $used >= $budget) {
$items[] = [
UsageTracker::budgetEnabled() ? __('Today\'s budget is used up.', 'aipcf-ai-provider-for-cloudflare') : __('Today is past the free allowance, and no budget stops it.', 'aipcf-ai-provider-for-cloudflare'),
__('Budget', 'aipcf-ai-provider-for-cloudflare'),
SettingsPage::settingsUrl('budget'),
];
}
if ($connected && EmbeddingSettings::enabled()) {
$test = SiteHealth::runEmbeddingsTest();
if (($test['status'] ?? 'good') !== 'good') {
$items[] = [(string) $test['label'], __('Content index', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('content-index')];
}
}
if ($items === []) {
return '';
}
$body = '';
foreach ($items as [$text, $label, $url]) {
$body .= '
' . Ui::pill(__('Needs you', 'aipcf-ai-provider-for-cloudflare'), 'warn') . ' ' . esc_html($text) . ' ' . Ui::link($label, $url) . '
';
}
return Ui::card(__('Needs you', 'aipcf-ai-provider-for-cloudflare'), $body);
}
private static function today(): string
{
$today = UsageTracker::todayTotals();
$site = (float) ($today['neurons'] ?? 0);
$account = AccountUsage::today();
$used = UsageTracker::countedNeurons();
$budget = UsageTracker::budgetEnabled() ? UsageTracker::budget() : UsageTracker::FREE_TIER;
$stats = [
/* translators: %s: the daily budget or the free daily allowance, in neurons */
['value' => Ui::num($site), 'label' => sprintf(__('of %s neurons on this site', 'aipcf-ai-provider-for-cloudflare'), Ui::num($budget)), 'state' => $site >= $budget ? 'warn' : ''],
];
if ($account !== null) {
$stats[] = ['value' => Ui::num($account), 'label' => __('on the whole account', 'aipcf-ai-provider-for-cloudflare'), 'state' => $account >= $budget ? 'warn' : ''];
} else {
$stats[] = ['value' => Ui::num((int) ($today['requests'] ?? 0)), 'label' => _n('request', 'requests', (int) ($today['requests'] ?? 0), 'aipcf-ai-provider-for-cloudflare')];
}
$body = Ui::stats($stats);
if (UsageTracker::budgetEnabled() && $used >= $budget) {
$body .= Ui::notice($account !== null && $account > $site
? __('The whole account passed the daily budget. AIPCF follows the budget setting until midnight UTC.', 'aipcf-ai-provider-for-cloudflare')
: __('The daily budget is used up. AIPCF follows the budget setting until midnight UTC.', 'aipcf-ai-provider-for-cloudflare'), 'bad');
} elseif ($account === null && AccountUsage::summary() !== '') {
$body .= Ui::status(AccountUsage::summary());
}
/* translators: %s: what the rest of the day's neurons cover, e.g. "about 4,750 short tasks" */
$body .= Ui::status(sprintf(__('Left today: %s.', 'aipcf-ai-provider-for-cloudflare'), UsageTracker::describe(max(0.0, $budget - $used))));
$tasks = is_array($today['tasks'] ?? null) ? $today['tasks'] : [];
arsort($tasks);
$rows = [];
foreach (array_slice($tasks, 0, 3, true) as $slug => $n) {
/* translators: %s: neurons */
$rows[] = [task_label((string) $slug), sprintf(__('%s neurons', 'aipcf-ai-provider-for-cloudflare'), Ui::num((float) $n))];
}
if ($rows !== []) {
$body .= Ui::facts($rows);
}
return Ui::card(__('Today', 'aipcf-ai-provider-for-cloudflare'), $body . Ui::actions([['label' => __('Budget', 'aipcf-ai-provider-for-cloudflare'), 'url' => SettingsPage::settingsUrl('budget'), 'kind' => 'link']]), [
'text' => __('Resets at midnight UTC.', 'aipcf-ai-provider-for-cloudflare'),
]);
}
private static function models(): string
{
$rows = array_map(static fn(array $r): array => [$r[0], $r[1]], array_slice(ModelMap::rows(), 0, 4));
return Ui::card(__('Models', 'aipcf-ai-provider-for-cloudflare'), Ui::facts($rows) . Ui::actions([['label' => __('Change models', 'aipcf-ai-provider-for-cloudflare'), 'url' => SettingsPage::settingsUrl('models'), 'kind' => 'link']]), [
'text' => __('Who does what, on Cloudflare Workers AI.', 'aipcf-ai-provider-for-cloudflare'),
]);
}
/**
* Every feature: on or off, one line on where it stands, and a way in.
*/
private static function whatIsOn(): string
{
$s = get_plugin_settings();
$f = static fn(string $k): bool => !empty(FeatureSettings::get($k));
$pill = static fn(bool $on): string => $on ? Ui::pill(__('On', 'aipcf-ai-provider-for-cloudflare'), 'ok') : Ui::pill(__('Off', 'aipcf-ai-provider-for-cloudflare'));
$tasks = (array) (UsageTracker::todayTotals()['tasks'] ?? []);
$left = Backfill::remaining();
$counts = EmbeddingSettings::enabled() ? IndexTable::counts(EmbeddingSettings::signature()) : ['indexed' => 0];
$rows = [
[Ui::link(__('Content index', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('content-index')), $pill(EmbeddingSettings::enabled()), EmbeddingSettings::enabled()
/* translators: %s: number of posts */
? esc_html(sprintf(_n('%s post in it', '%s posts in it', (int) $counts['indexed'], 'aipcf-ai-provider-for-cloudflare'), Ui::num((int) $counts['indexed'])))
: esc_html__('Needed by most features', 'aipcf-ai-provider-for-cloudflare')],
/* translators: %s: neurons */
[Ui::link(__('Site assistant', 'aipcf-ai-provider-for-cloudflare'), AssistantPage::pageUrl()), $pill($f('chat_enabled')), $f('chat_enabled') ? esc_html(sprintf(__('%s neurons today', 'aipcf-ai-provider-for-cloudflare'), Ui::num((float) ($tasks['chat'] ?? 0) + (float) ($tasks['chat-public'] ?? 0)))) : ''],
[Ui::link(__('Related posts', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('related')), $pill($f('related_enabled')), ''],
[Ui::link(__('Search', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('search')), $pill($f('search_enabled')), ''],
[Ui::link(__('Link assistant', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('links')), $pill($f('links_enabled')), ''],
[Ui::link(__('Search insights', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('insights')), $pill($f('insights_enabled')), ''],
[Ui::link(__('AI agents', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('agents')), $pill($f('agents_enabled')), ''],
/* translators: %s: number of images */
[Ui::link(__('Backfill', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('backfill')), $pill(Backfill::enabled()), Backfill::enabled() ? esc_html(sprintf(_n('%s image without alt text', '%s images without alt text', (int) $left['alt_text'], 'aipcf-ai-provider-for-cloudflare'), Ui::num((int) $left['alt_text']))) : ''],
[Ui::link(__('Daily budget', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('budget')), $pill(!empty($s['budget_enabled'])), ''],
/* translators: %s: the gateway's name */
[Ui::link(__('AI Gateway', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('connection')), $pill(Gateway::id() !== ''), Gateway::id() !== '' ? esc_html(Gateway::id()) : ''],
];
return Ui::card(__('What is on', 'aipcf-ai-provider-for-cloudflare'), Ui::table([__('Feature', 'aipcf-ai-provider-for-cloudflare'), __('Status', 'aipcf-ai-provider-for-cloudflare'), __('Now', 'aipcf-ai-provider-for-cloudflare')], $rows), [
'text' => __('Switch features on and off on Features.', 'aipcf-ai-provider-for-cloudflare'),
]);
}
/**
* The WordPress AI plugin: suggested while it is missing, and a first thing to try once on.
*/
private static function aiPlugin(bool $connected): string
{
$ai = ai_plugin_status();
if ($ai['status'] !== 'active') {
if (!$connected) {
return ''; // The setup offers it at the end.
}
return Ui::card(__('AI in the block editor', 'aipcf-ai-provider-for-cloudflare'), Ui::actions([['label' => $ai['action_label'], 'url' => $ai['action_url']]]), [
'text' => __('The WordPress AI plugin adds excerpts, titles, meta descriptions, alt text and images to the editor, using AIPCF.', 'aipcf-ai-provider-for-cloudflare'),
]);
}
if (!$connected || (core_connector_is_available() && !is_connector_approved() && class_exists('\\WordPress\\AI\\Connector_Approval\\Approvals_Store'))) {
return '';
}
$hint = FreeTierSettings::tryItHint();
return Ui::card(__('Try it', 'aipcf-ai-provider-for-cloudflare'), Ui::actions([['label' => $hint['label'], 'url' => $hint['url'], 'kind' => 'link']]), ['text' => $hint['text']]);
}
}