'Yoast SEO', 'rank_math_description' => 'Rank Math', '_aioseo_description' => 'All in One SEO', '_seopress_titles_desc' => 'SEOPress', ]; public static function render(): string { $s = get_plugin_settings(); $on = Backfill::enabled(); $tasks = is_array($s['backfill_tasks'] ?? null) ? array_map('strval', $s['backfill_tasks']) : ['meta_description']; $limit = (int) ($s['backfill_daily_neurons'] ?? 3000); $used = UsageTracker::todayBackfillNeurons(); $left = Backfill::remaining(); $metaKey = Backfill::metaDescriptionKey(); $seo = $metaKey === '' ? '' : (self::SEO_PLUGINS[$metaKey] ?? $metaKey); $body = Ui::stats(self::figures($tasks, $left, $seo, $used, $limit)); $body .= self::lastRun(); $body .= self::actions(); $body .= Ui::row(__('Backfill', 'aipcf-ai-provider-for-cloudflare'), Ui::toggle('backfill_enabled', $on, __('Run every hour', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-backfill-enabled'), ['for' => 'aipcf-backfill-enabled']); $body .= Ui::row( __('Fill in', 'aipcf-ai-provider-for-cloudflare'), Ui::checks('backfill_tasks', [ 'meta_description' => [ 'label' => __('Meta descriptions', 'aipcf-ai-provider-for-cloudflare'), 'note' => $seo !== '' /* translators: %s: an SEO plugin's name, e.g. Yoast SEO */ ? sprintf(__('for %s, about 2 neurons each', 'aipcf-ai-provider-for-cloudflare'), $seo) : __('needs Yoast SEO, Rank Math, All in One SEO or SEOPress', 'aipcf-ai-provider-for-cloudflare'), ], 'alt_text' => [ 'label' => __('Image alt text', 'aipcf-ai-provider-for-cloudflare'), 'note' => __('about 17 neurons per image', 'aipcf-ai-provider-for-cloudflare'), ], ], $tasks, __('Fill in', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-backfill-tasks'), ['for' => 'aipcf-backfill-tasks', 'more' => self::models()] ); $body .= Ui::row( __('Daily limit', 'aipcf-ai-provider-for-cloudflare'), Ui::number('backfill_daily_neurons', $limit, 'aipcf-backfill-limit', ['min' => 0, 'max' => 10000000, 'unit' => __('neurons a day', 'aipcf-ai-provider-for-cloudflare')]), ['for' => 'aipcf-backfill-limit', 'help' => __('Counts toward the daily budget. 0 means no limit of its own.', 'aipcf-ai-provider-for-cloudflare')] ); $body .= Ui::moreSettings(Ui::row( __('Items per run', 'aipcf-ai-provider-for-cloudflare'), Ui::number('backfill_batch', (int) ($s['backfill_batch'] ?? 10), 'aipcf-backfill-batch', ['min' => 1, 'max' => 200]), ['for' => 'aipcf-backfill-batch', 'help' => __('How many posts or images each run handles.', 'aipcf-ai-provider-for-cloudflare')] )); return Ui::card(__('Backfill', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'id' => 'aipcf-backfill-status', 'text' => __('Writes missing meta descriptions and image alt text in the background, a few at a time.', 'aipcf-ai-provider-for-cloudflare'), 'pill' => $on ? ['ok', __('On', 'aipcf-ai-provider-for-cloudflare')] : ['', __('Off', 'aipcf-ai-provider-for-cloudflare')], ]); } /** * What is left to fill, and today's use against the limit. * * @param list $tasks * @param array{meta_description: int, alt_text: int} $left * @return list */ private static function figures(array $tasks, array $left, string $seo, float $used, int $limit): array { $out = []; if (in_array('meta_description', $tasks, true)) { $out[] = $seo === '' ? ['value' => __('No SEO plugin', 'aipcf-ai-provider-for-cloudflare'), 'label' => __('so no meta descriptions yet', 'aipcf-ai-provider-for-cloudflare'), 'state' => 'warn'] : ['value' => Ui::num($left['meta_description']), 'label' => _n('post without a meta description', 'posts without a meta description', (int) $left['meta_description'], 'aipcf-ai-provider-for-cloudflare'), 'state' => $left['meta_description'] === 0 ? 'done' : '']; } if (in_array('alt_text', $tasks, true)) { $out[] = ['value' => Ui::num($left['alt_text']), 'label' => _n('image without alt text', 'images without alt text', (int) $left['alt_text'], 'aipcf-ai-provider-for-cloudflare'), 'state' => $left['alt_text'] === 0 ? 'done' : '']; } $out[] = $limit > 0 ? [ 'value' => Ui::num($used), /* translators: %s: the daily limit in neurons, e.g. 3,000 */ 'label' => sprintf(__('of %s neurons today', 'aipcf-ai-provider-for-cloudflare'), Ui::num($limit)), 'state' => $used >= $limit ? 'warn' : '', ] : ['value' => Ui::num($used), 'label' => __('neurons today', 'aipcf-ai-provider-for-cloudflare')]; return $out; } private static function lastRun(): string { $state = Backfill::state(); $filled = (int) array_sum((array) ($state['done'] ?? [])); $skipped = (int) array_sum((array) ($state['failed'] ?? [])); if (empty($state['last_run'])) { return Ui::status(__('Not run yet.', 'aipcf-ai-provider-for-cloudflare')); } $out = Ui::status(sprintf( /* translators: 1: time since the last run, e.g. "5 mins", 2: items filled so far, 3: items skipped */ __('Last run %1$s ago. Filled so far: %2$s. Skipped: %3$s.', 'aipcf-ai-provider-for-cloudflare'), human_time_diff((int) $state['last_run']), Ui::num($filled), Ui::num($skipped) )); if (!empty($state['last_error'])) { $out .= Ui::more(__('Why the last item was skipped', 'aipcf-ai-provider-for-cloudflare'), '

' . esc_html((string) $state['last_error']) . '

'); } return $out; } private static function actions(): string { $state = Backfill::state(); $skipped = (int) array_sum((array) ($state['failed'] ?? [])); $items = [[ 'label' => __('Run one batch now', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_backfill_run'), 'aipcf_backfill_run'), 'busy' => __('Running...', 'aipcf-ai-provider-for-cloudflare'), ]]; $items[] = [ 'label' => __('See what changed', 'aipcf-ai-provider-for-cloudflare'), 'url' => ActivityPage::pageUrl(['tool' => 'backfill']), 'kind' => 'link', ]; if ($skipped > 0) { $items[] = [ 'label' => __('Retry skipped items', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_backfill_reset'), 'aipcf_backfill_reset'), 'kind' => 'link', 'confirm' => __('Try the skipped items again on the next runs?', 'aipcf-ai-provider-for-cloudflare'), 'busy' => __('Queuing...', 'aipcf-ai-provider-for-cloudflare'), ]; } return Ui::actions($items); } /** * Which models write what, behind "Learn more". */ private static function models(): string { return '

' . esc_html(sprintf( /* translators: 1: the light model's name, 2: the vision model's name */ __('Meta descriptions are written by %1$s, the light model. Alt text is written by %2$s, the vision model, because text models cannot see images.', 'aipcf-ai-provider-for-cloudflare'), pretty_model_name(ModelMap::textModelFor('backfill-meta-description')), pretty_model_name(vision_model_id()) )) . '

'; } }