Related posts. */ final class RelatedScreen { public static function render(): string { $on = !empty(Settings::get('related_enabled')); $body = ''; if ($on) { $c = IndexTable::relatedCounts(); $queries = (int) (UsageTracker::todayTotals()['queries'][Related::TASK] ?? 0); $cap = (int) Settings::get('related_daily_queries'); $body .= Ui::stats([ ['value' => Ui::num($c['done']), 'label' => _n('post with a list', 'posts with a list', (int) $c['done'], 'aipcf-ai-provider-for-cloudflare')], ['value' => Ui::num($c['waiting']), 'label' => __('waiting', 'aipcf-ai-provider-for-cloudflare'), 'state' => $c['waiting'] === 0 ? 'done' : ''], [ 'value' => Ui::num($queries), 'label' => $cap > 0 /* translators: %s: searches of the content index a day */ ? sprintf(__('of %s index searches today', 'aipcf-ai-provider-for-cloudflare'), Ui::num($cap)) : __('index searches today', 'aipcf-ai-provider-for-cloudflare'), 'state' => $cap > 0 && $queries >= $cap ? 'warn' : '', ], ]); if ($cap > 0 && $queries >= $cap && $c['waiting'] > 0) { $body .= Ui::status(__('Today\'s limit is reached. The rest are built after midnight UTC.', 'aipcf-ai-provider-for-cloudflare')); } if ($c['waiting'] > 0) { $body .= Ui::actions([[ 'label' => __('Build lists now', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_related_build'), 'aipcf_related_build'), 'busy' => __('Building...', 'aipcf-ai-provider-for-cloudflare'), ]]); } } $body .= Ui::row(__('Related posts', 'aipcf-ai-provider-for-cloudflare'), Ui::toggle('related_enabled', $on, __('Build related lists', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-related-enabled'), [ 'for' => 'aipcf-related-enabled', 'more' => '
' . esc_html__('Put [aipcf_related] in a post, or let AIPCF add the list for you below. The shortcode can set its own count, title, heading, excerpt, thumbnail and why.', 'aipcf-ai-provider-for-cloudflare') . '
', ]); $body .= Ui::row(__('Add automatically', 'aipcf-ai-provider-for-cloudflare'), Ui::toggle('related_auto', !empty(Settings::get('related_auto')), __('Add the list to posts', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-related-auto'), [ 'for' => 'aipcf-related-auto', 'help' => __('A post with the shortcode keeps its own list.', 'aipcf-ai-provider-for-cloudflare'), ]); $body .= self::placement(); $body .= Ui::row(__('How many', 'aipcf-ai-provider-for-cloudflare'), Ui::number('related_count', (int) Settings::get('related_count'), 'aipcf-related-count', ['min' => 1, 'max' => 12, 'unit' => __('posts', 'aipcf-ai-provider-for-cloudflare')]), ['for' => 'aipcf-related-count']); $body .= self::look(); $body .= Ui::moreSettings( Ui::row(__('Minimum match', 'aipcf-ai-provider-for-cloudflare'), Ui::number('related_min_score', (float) Settings::get('related_min_score'), 'aipcf-related-min-score', ['min' => 0, 'max' => 1, 'step' => 0.05]), [ 'for' => 'aipcf-related-min-score', 'help' => __('0.55 means a 55% match. Weaker matches are not listed.', 'aipcf-ai-provider-for-cloudflare'), ]) . Ui::row(__('Index searches', 'aipcf-ai-provider-for-cloudflare'), Ui::number('related_daily_queries', (int) Settings::get('related_daily_queries'), 'aipcf-related-daily-queries', ['min' => 0, 'max' => 1000000, 'unit' => __('a day', 'aipcf-ai-provider-for-cloudflare')]), [ 'for' => 'aipcf-related-daily-queries', 'help' => __('One post\'s list is one search. 0 means no limit of its own.', 'aipcf-ai-provider-for-cloudflare'), ]) ); return Ui::card(__('Related posts', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'id' => 'aipcf-related-status', 'text' => __('Built in the background from the content index, so it uses no neurons and nothing runs while a page loads.', 'aipcf-ai-provider-for-cloudflare'), ]); } /** * Where "Add automatically" puts the list, and on which post types: shown only while it is on. */ private static function placement(): string { $types = array_values(array_filter((array) (get_plugin_settings()['embed_post_types'] ?? []), 'post_type_exists')); $skip = (array) Settings::get('related_skip_types'); $where = Ui::select('related_position', [ 'after' => __('After the content', 'aipcf-ai-provider-for-cloudflare'), 'before' => __('Before the content', 'aipcf-ai-provider-for-cloudflare'), 'paragraph' => __('After paragraph', 'aipcf-ai-provider-for-cloudflare'), ], (string) Settings::get('related_position'), 'aipcf-related-position') . sprintf( ' ', esc_attr(Ui::field('related_paragraph')), esc_attr((string) Settings::get('related_paragraph')), esc_attr__('Paragraph number', 'aipcf-ai-provider-for-cloudflare') ); $out = Ui::row(__('Where', 'aipcf-ai-provider-for-cloudflare'), $where, [ 'for' => 'aipcf-related-position', 'help' => __('Paragraphs in quotes, lists, tables and groups are not counted.', 'aipcf-ai-provider-for-cloudflare'), 'class' => 'aipcf-related-auto-row', ]); if ($types === []) { return $out; } $boxes = ''; return $out . Ui::row(__('On', 'aipcf-ai-provider-for-cloudflare'), $boxes, [ 'for' => 'aipcf-related-types', 'help' => __('A post type you embed later gets the list too, until you untick it.', 'aipcf-ai-provider-for-cloudflare'), 'class' => 'aipcf-related-auto-row', ]); } /** * The list's title and what each entry shows. */ private static function look(): string { $title = Ui::text('related_title', (string) Settings::get('related_title'), 'aipcf-related-title', ['placeholder' => __('Related posts', 'aipcf-ai-provider-for-cloudflare')]) . ' ' . Ui::select('related_heading', [ 'h2' => __('Heading 2', 'aipcf-ai-provider-for-cloudflare'), 'h3' => __('Heading 3', 'aipcf-ai-provider-for-cloudflare'), 'h4' => __('Heading 4', 'aipcf-ai-provider-for-cloudflare'), 'p' => __('Plain text', 'aipcf-ai-provider-for-cloudflare'), ], (string) Settings::get('related_heading'), 'aipcf-related-heading'); $show = []; foreach (['related_why', 'related_excerpt', 'related_thumbnail'] as $key) { if (!empty(Settings::get($key))) { $show[] = $key; } } // Three separate settings that read as one list: each is its own 0/1 field. $checks = ''; return Ui::row(__('Title', 'aipcf-ai-provider-for-cloudflare'), $title, ['for' => 'aipcf-related-title', 'help' => __('Empty means "Related posts", in the reader\'s language.', 'aipcf-ai-provider-for-cloudflare')]) . Ui::row(__('With each post', 'aipcf-ai-provider-for-cloudflare'), $checks, ['for' => 'aipcf-related-why']); } }