__('Star Ratings settings saved.', 'ai-eshop-optimizer'))); } public function ajax_save_appearance() { AIEO_DMM::verify_ajax_request(); $cur = AIEO_Ratings::get_settings(); foreach (array('color_empty', 'color_filled', 'color_hover', 'color_pre_rated') as $k) { if (isset($_POST[$k])) { $v = sanitize_hex_color(wp_unslash((string) $_POST[$k])); if ($v !== null && $v !== '') { $cur[$k] = $v; } } } if (isset($_POST['size'])) { $cur['size'] = max(8, min(96, (int) wp_unslash($_POST['size']))); } foreach (array('size_mobile', 'size_single') as $k) { if (isset($_POST[$k])) { $v = (int) wp_unslash($_POST[$k]); if ($v <= 0) { $cur[$k] = 0; } else { $cur[$k] = max(8, min(96, $v)); } } } if (isset($_POST['gap'])) { $cur['gap'] = max(0, min(20, (int) wp_unslash($_POST['gap']))); } if (isset($_POST['gap_mobile'])) { $v = (int) wp_unslash($_POST['gap_mobile']); $cur['gap_mobile'] = $v <= 0 ? 0 : max(0, min(20, $v)); } if (isset($_POST['size_archive'])) { $v = (int) wp_unslash($_POST['size_archive']); $cur['size_archive'] = $v <= 0 ? 0 : max(8, min(96, $v)); } if (isset($_POST['gap_archive'])) { $cur['gap_archive'] = max(0, min(20, (int) wp_unslash($_POST['gap_archive']))); } if (isset($_POST['gap_single'])) { $cur['gap_single'] = max(0, min(20, (int) wp_unslash($_POST['gap_single']))); } foreach (array('caption_size', 'caption_size_mobile', 'caption_size_archive', 'caption_size_archive_mobile') as $k) { if (isset($_POST[$k])) { $cur[$k] = max(8, min(48, (int) wp_unslash($_POST[$k]))); } } update_option(AIEO_Ratings::SETTINGS_OPTION, $cur, false); wp_send_json_success(array('message' => __('Star Ratings appearance saved.', 'ai-eshop-optimizer'))); } public function render_subtab() { global $wpdb; $events = $wpdb->prefix . 'aieo_ratings'; $stats = $wpdb->prefix . 'aieo_rating_stats'; $events_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $events)) === $events; $stats_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $stats)) === $stats; $moved = wp_kses_post(sprintf( __('Looking for the Star Ratings display settings (behavior, captions, colors, sizes)? They now live under %1$sVisual Display → Star Ratings Display%2$s.', 'ai-eshop-optimizer'), '', '' )); if (!$events_exists || !$stats_exists) { $close = $this->card_open(array( 'anchor' => 'ratings-overview', 'icon' => 'layout-dashboard', 'title' => __('Overview', 'ai-eshop-optimizer'), 'description' => __('Totals of the star ratings shoppers gave your products: how many ratings, rated products and voters.', 'ai-eshop-optimizer'), )); $this->state('not-set-up', __('No rating tables yet — they install lazily on the next frontend hit. Visit any frontend page once and the analytics will appear here.', 'ai-eshop-optimizer')); $this->note($moved, 'mono'); echo $close; return; } $total_events = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$events}"); $unique_voters_u = (int) $wpdb->get_var("SELECT COUNT(DISTINCT user_id) FROM {$events} WHERE user_id > 0"); $unique_voters_s = (int) $wpdb->get_var("SELECT COUNT(DISTINCT session_token) FROM {$events} WHERE user_id = 0 AND session_token <> ''"); $rated_products = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$stats} WHERE count_total > 0"); $row_global = $wpdb->get_row("SELECT SUM(sum_total) AS s, SUM(count_total) AS c FROM {$stats}", ARRAY_A); $global_avg = ($row_global && (int) $row_global['c'] > 0) ? round((float) $row_global['s'] / (int) $row_global['c'], 2) : 0; $top = $wpdb->get_results("SELECT product_id, count_total, sum_total FROM {$stats} WHERE count_total >= 3 ORDER BY (sum_total / count_total) DESC, count_total DESC LIMIT 20"); $bottom = $wpdb->get_results("SELECT product_id, count_total, sum_total FROM {$stats} WHERE count_total >= 3 ORDER BY (sum_total / count_total) ASC, count_total DESC LIMIT 20"); $most = $wpdb->get_results("SELECT product_id, count_total, sum_total FROM {$stats} ORDER BY count_total DESC LIMIT 20"); $by_source = $wpdb->get_results("SELECT source, COUNT(*) AS c FROM {$events} WHERE source <> '' GROUP BY source ORDER BY c DESC LIMIT 10"); $row_dist = $wpdb->get_row("SELECT SUM(n1) AS n1, SUM(n2) AS n2, SUM(n3) AS n3, SUM(n4) AS n4, SUM(n5) AS n5 FROM {$stats}", ARRAY_A); $this->band_open(array('cols' => 1)); $close = $this->card_open(array( 'anchor' => 'ratings-overview', 'icon' => 'layout-dashboard', 'title' => __('Overview', 'ai-eshop-optimizer'), 'description' => __('Totals of the star ratings shoppers gave your products: how many ratings, rated products and voters.', 'ai-eshop-optimizer'), )); echo '
'; $this->tile(__('Total rating events', 'ai-eshop-optimizer'), number_format_i18n($total_events)); $this->tile(__('Rated products', 'ai-eshop-optimizer'), number_format_i18n($rated_products)); $this->tile(__('Voters — logged in', 'ai-eshop-optimizer'), number_format_i18n($unique_voters_u)); $this->tile(__('Voters — anonymous', 'ai-eshop-optimizer'), number_format_i18n($unique_voters_s)); $this->tile(__('Site-wide average', 'ai-eshop-optimizer'), $global_avg > 0 ? number_format_i18n($global_avg, 2) . ' / 5' : '—'); $total_votes_now = (int) $wpdb->get_var("SELECT SUM(count_total) FROM {$stats}"); $this->tile(__('Active votes', 'ai-eshop-optimizer'), number_format_i18n((int) $total_votes_now)); echo '
'; $this->note($moved, 'mono'); echo $close; $this->render_sparsity_card($rated_products, $by_source); $this->render_needs_ratings_card(); $this->band_close(); $this->band_open(array('cols' => 1)); $this->render_distribution_card($row_dist); $this->render_source_card($by_source, $total_events); $this->band_close(); $this->band_open(array('cols' => 1)); $this->render_top_card(__('Top rated', 'ai-eshop-optimizer'), $top, true, 'ratings-top-rated', 'trending-up', __('The products with the highest average rating, counting only those with at least three votes.', 'ai-eshop-optimizer')); $this->render_top_card(__('Lowest rated', 'ai-eshop-optimizer'), $bottom, true, 'ratings-lowest-rated', 'trending-down', __('The products with the lowest average rating, worth a look at their description, photos or quality.', 'ai-eshop-optimizer')); $this->render_top_card(__('Most votes', 'ai-eshop-optimizer'), $most, false, 'ratings-most-votes', 'award', __('The products shoppers rated most often, whatever their average.', 'ai-eshop-optimizer')); $this->band_close(); } public function render_settings_card() { $s = AIEO_Ratings::get_settings(); $id_of = static function ($name) { return 'aieo-rat-' . str_replace('_', '-', $name); }; $row = static function ($name, $label, $control, $help = '') use ($id_of) { $field = array('id' => $id_of($name), 'label' => $label, 'control' => $control); if ('' !== $help) { $field['help'] = $help; } return \Rooster\Admin\Ui::field_row($field); }; $switch = static function ($name, $text) use ($s, $id_of) { return \Rooster\Admin\Ui::toggle($name, !empty($s[$name]), array('id' => $id_of($name), 'hidden' => false, 'label' => $text)); }; $px = static function ($name, $value, $min, $max) use ($id_of) { return \Rooster\Admin\Ui::input($name, (string) (int) $value, array('id' => $id_of($name), 'type' => 'number', 'xs' => true, 'unit' => 'px', 'min' => $min, 'max' => $max)); }; $colour = static function ($name, $value, $fallback) use ($id_of) { return \Rooster\Admin\Fields::color($name, (string) $value, array('id' => $id_of($name), 'default' => $fallback)); }; $card = static function ($c, $rows, $after = '') { $form_id = 'aieo-card-' . $c['anchor'] . '-form'; list($open, $close) = AIEO_DMM_UI::card($c + array('footer' => \Rooster\Admin\States::save_button($form_id))); echo $open; echo AIEO_DMM_UI::card_form('aieo_dmm_ratings_save_settings', 'ratings_display', $form_id, '
' . $rows . '
' . $after); echo $close; }; $band = function_exists('rooster_admin_band_open'); if ($band) { rooster_admin_band_open(array('cols' => 2)); } $rows = $row('enabled', __('Enable Star Ratings', 'ai-eshop-optimizer'), $switch('enabled', __('Master switch for the module.', 'ai-eshop-optimizer'))); $rows .= $row('require_login', __('Require login to vote', 'ai-eshop-optimizer'), $switch('require_login', __('Anonymous shoppers see the widget but clicks are gated.', 'ai-eshop-optimizer'))); $rows .= $row('login_redirect_mode', __('Login gate behavior', 'ai-eshop-optimizer'), \Rooster\Admin\Ui::select('login_redirect_mode', array( 'redirect' => __('Redirect to login page', 'ai-eshop-optimizer'), 'event' => __('Emit aieo:rating:login_required event (theme-handled)', 'ai-eshop-optimizer'), ), (string) $s['login_redirect_mode'], array('id' => $id_of('login_redirect_mode')))); $rows .= $row('allow_clear', __('Allow clear', 'ai-eshop-optimizer'), $switch('allow_clear', __('Re-clicking the active star clears the rating.', 'ai-eshop-optimizer'))); $card(array( 'anchor' => 'ratings-behavior', 'icon' => 'activity', 'title' => __('Behavior', 'ai-eshop-optimizer'), 'description' => __('Turn star ratings on, decide whether shoppers must log in to vote and whether clicking their star again clears it.', 'ai-eshop-optimizer'), 'col' => 1, ), $rows); $rows = $row('display_archive', __('Display on archive cards', 'ai-eshop-optimizer'), $switch('display_archive', __('Render on shop / category pages.', 'ai-eshop-optimizer'))); $rows .= $row('display_single', __('Display on single product page', 'ai-eshop-optimizer'), $switch('display_single', __('Render between the title and the price.', 'ai-eshop-optimizer'))); $rows .= $row('aggregate_only_archive', __('Aggregate only on archive', 'ai-eshop-optimizer'), $switch('aggregate_only_archive', __('Archive cards show the read-only aggregate (no interactive widget).', 'ai-eshop-optimizer'))); $rows .= $row('display_full_until_rated', __('Show 5 stars until rated', 'ai-eshop-optimizer'), $switch('display_full_until_rated', __('Display all 5 stars filled (and hide the avg/count caption) until the viewer casts their own rating. After they vote, the widget switches to the true aggregate.', 'ai-eshop-optimizer'))); $card(array( 'anchor' => 'ratings-where-it-appears', 'icon' => 'pin', 'title' => __('Star placement', 'ai-eshop-optimizer'), 'description' => __('Turns the stars on for shop and category cards and for the product page between title and price, and can limit archive cards to the read-only average.', 'ai-eshop-optimizer'), 'col' => 1, ), $rows); $prompt = method_exists('AIEO_Ratings', 'default_caption_before_rated') ? AIEO_Ratings::default_caption_before_rated() : ''; $rows = $row('caption_before_rated', __('Prompt before viewer rates', 'ai-eshop-optimizer'), \Rooster\Admin\Ui::input('caption_before_rated', (string) $s['caption_before_rated'], array('id' => $id_of('caption_before_rated'), 'placeholder' => (string) $prompt)), __('Shown next to the stars until the viewer casts their own rating. Plain text — no tokens. Leave empty to use the built-in wording, which follows each reader\'s language.', 'ai-eshop-optimizer')); $read_only = method_exists('AIEO_Ratings', 'default_caption_read_only') ? AIEO_Ratings::default_caption_read_only() : ''; $rows .= $row('caption_read_only', __('Prompt on read-only stars', 'ai-eshop-optimizer'), \Rooster\Admin\Ui::input('caption_read_only', (string) ($s['caption_read_only'] ?? ''), array('id' => $id_of('caption_read_only'), 'placeholder' => (string) $read_only)), __('A read-only rating, such as an archive card\'s under "Aggregate only on archive", cannot take a vote: it shows one star, the average and the number of votes, and leads to the product page\'s stars. While "Show 5 stars until rated" hides the average from a visitor who has not voted, this text says where to vote, beside an empty star. Plain text. Leave empty to use the built-in wording, which follows each reader\'s language.', 'ai-eshop-optimizer')); $tokens = __('Tokens: %avg, %count, %avg5. Empty = hide caption entirely.', 'ai-eshop-optimizer'); $rows .= $row('caption_format', __('Caption after viewer rates', 'ai-eshop-optimizer'), \Rooster\Admin\Ui::input('caption_format', (string) $s['caption_format'], array('id' => $id_of('caption_format'), 'medium' => true, 'code' => true)), $tokens); $rows .= $row('min_votes_for_display', __('Minimum votes to show aggregate', 'ai-eshop-optimizer'), \Rooster\Admin\Ui::input('min_votes_for_display', (string) (int) $s['min_votes_for_display'], array('id' => $id_of('min_votes_for_display'), 'type' => 'number', 'xs' => true, 'min' => 0)), __('Hide the aggregate until N votes — avoids "5.0 (1)" being misleading.', 'ai-eshop-optimizer')); $card(array( 'anchor' => 'ratings-caption-thresholds', 'icon' => 'text-cursor-input', 'title' => __('Caption and thresholds', 'ai-eshop-optimizer'), 'description' => __('Write the text beside the stars before and after a shopper votes, and set how many votes an average needs before it shows.', 'ai-eshop-optimizer'), 'col' => 1, ), $rows); $rows = $row('color_empty', __('Empty (outline) color', 'ai-eshop-optimizer'), $colour('color_empty', $s['color_empty'], '#d4d4d8')); $rows .= $row('color_pre_rated', __('Pre-rated color', 'ai-eshop-optimizer'), $colour('color_pre_rated', $s['color_pre_rated'] ?? '#F5B301', '#F5B301'), __('Color of the 5 placeholder stars before the viewer has rated.', 'ai-eshop-optimizer')); $rows .= $row('color_filled', __('Rated (filled) color', 'ai-eshop-optimizer'), $colour('color_filled', $s['color_filled'], '#F5B301'), html_entity_decode(__('Color of the stars representing the viewer’s own rating, after they vote.', 'ai-eshop-optimizer'), ENT_QUOTES, 'UTF-8')); $rows .= $row('color_hover', __('Hover color', 'ai-eshop-optimizer'), $colour('color_hover', $s['color_hover'], '#FFC93C')); $rows .= $row('size', __('Star size on desktop', 'ai-eshop-optimizer'), $px('size', $s['size'], 8, 96), __('Default size used everywhere. The two overrides below kick in on top of this.', 'ai-eshop-optimizer')); $rows .= $row('size_mobile', __('Star size on mobile', 'ai-eshop-optimizer'), $px('size_mobile', $s['size_mobile'] ?? 0, 0, 96), __('Applied on viewports ≤ 768px wide. Set 0 to inherit the desktop size.', 'ai-eshop-optimizer')); $rows .= $row('size_single', __('Star size on the single-product page', 'ai-eshop-optimizer'), $px('size_single', $s['size_single'] ?? 0, 0, 96), __('Applied only on the single-product page, at every width. Set 0 to inherit the desktop size.', 'ai-eshop-optimizer')); $rows .= $row('gap', __('Star gap on desktop', 'ai-eshop-optimizer'), $px('gap', $s['gap'] ?? 2, 0, 20), __('Horizontal distance between adjacent stars in the row. 0 = stars touching · 2 = the dev theme reference · 8 = airy. Range 0–20px.', 'ai-eshop-optimizer')); $rows .= $row('gap_mobile', __('Star gap on mobile', 'ai-eshop-optimizer'), $px('gap_mobile', $s['gap_mobile'] ?? 0, 0, 20), __('Applied on viewports ≤ 768px wide. Set 0 to inherit the desktop gap. Tighter values (1–2px) prevent the row inflating to fill narrow archive cards.', 'ai-eshop-optimizer')); $rows .= $row('gap_single', __('Star gap on the single-product page', 'ai-eshop-optimizer'), $px('gap_single', $s['gap_single'] ?? 4, 0, 20), __('Horizontal distance between the stars on the single-product page, at every width.', 'ai-eshop-optimizer')); $rows .= $row('size_archive', __('Star size on archive cards', 'ai-eshop-optimizer'), $px('size_archive', $s['size_archive'] ?? 16, 0, 96), __('Shop and category cards and other product lists, at every width: smaller than on the product page. Set 0 to use the desktop size.', 'ai-eshop-optimizer')); $rows .= $row('gap_archive', __('Star gap on archive cards', 'ai-eshop-optimizer'), $px('gap_archive', $s['gap_archive'] ?? 4, 0, 20), __('Horizontal distance between the stars on archive cards. Size plus gap under 24px makes them smaller touch targets than the accessibility guideline asks.', 'ai-eshop-optimizer')); $captions = $row('caption_size', __('Caption text size, product page, desktop', 'ai-eshop-optimizer'), $px('caption_size', $s['caption_size'] ?? 13, 8, 48), __('Font size of the rating caption (e.g. “4.6 (128)” / the prompt) on product / single pages.', 'ai-eshop-optimizer')); $captions .= $row('caption_size_mobile', __('Caption text size, product page, mobile', 'ai-eshop-optimizer'), $px('caption_size_mobile', $s['caption_size_mobile'] ?? 13, 8, 48), __('Applied on viewports ≤ 768px wide.', 'ai-eshop-optimizer')); $captions .= $row('caption_size_archive', __('Caption text size, archive, desktop', 'ai-eshop-optimizer'), $px('caption_size_archive', $s['caption_size_archive'] ?? 12, 8, 48), __('Font size of the caption on shop / category archive cards.', 'ai-eshop-optimizer')); $captions .= $row('caption_size_archive_mobile', __('Caption text size, archive, mobile', 'ai-eshop-optimizer'), $px('caption_size_archive_mobile', $s['caption_size_archive_mobile'] ?? 11, 8, 48), __('Applied on archive cards on viewports ≤ 768px wide.', 'ai-eshop-optimizer')); $card(array( 'anchor' => 'ratings-appearance', 'icon' => 'palette', 'title' => __('Star colours and size', 'ai-eshop-optimizer'), 'description' => __('Choose the star colours before and after a vote, and the size and spacing of the stars on desktop, mobile and product pages.', 'ai-eshop-optimizer'), 'col' => 2, ), $rows, \Rooster\Admin\Ui::fold(__('Advanced', 'ai-eshop-optimizer'), '
' . $captions . '
')); $card(array( 'anchor' => 'ratings-seo-schema', 'icon' => 'braces', 'title' => __('SEO / Schema', 'ai-eshop-optimizer'), 'description' => __('Google reads JSON-LD on product pages to render the star rich-snippet under each search result. When enabled, the Rooster One rating aggregate (count + average from wp_aieo_rating_stats) is injected into the Product schema emitted by AIOSEO, Yoast SEO, or Rank Math — whichever you have active. Falls back to its own minimal JSON-LD if none is detected.', 'ai-eshop-optimizer'), 'col' => 1, ), $row('seo_schema_enabled', __('Feed schema with Rooster One ratings', 'ai-eshop-optimizer'), $switch('seo_schema_enabled', __('Inject aggregateRating into Product JSON-LD on single-product pages.', 'ai-eshop-optimizer')), __('Reads from the materialized stats table — no per-vote computation, no comment-system dependency.', 'ai-eshop-optimizer'))); if ($band) { rooster_admin_band_close(); } } private function render_sparsity_card($rated_rows_all, $by_source = array()) { global $wpdb; $stats = $wpdb->prefix . 'aieo_rating_stats'; $counts = wp_count_posts('product'); $total_published = ($counts && isset($counts->publish)) ? (int) $counts->publish : 0; $prior_votes = defined('AIEO_Boost_Candidates::RATING_PRIOR_VOTES') ? (int) constant('AIEO_Boost_Candidates::RATING_PRIOR_VOTES') : 5; $veto_votes = defined('AIEO_Boost_Candidates::RATING_VETO_MIN_VOTES') ? (int) constant('AIEO_Boost_Candidates::RATING_VETO_MIN_VOTES') : 5; if ($prior_votes < 1) { $prior_votes = 5; } if ($veto_votes < 1) { $veto_votes = 5; } $rungs = array(1, 3, $prior_votes, 10); $rungs = array_values(array_unique(array_filter(array_map('intval', $rungs), function ($n) { return $n > 0; }))); sort($rungs); $type_clause = $this->stats_has_object_type() ? " AND s.object_type = 'product'" : ''; $select = array(); foreach ($rungs as $i => $n) { $select[] = 'SUM(CASE WHEN s.count_total >= ' . (int) $n . ' THEN 1 ELSE 0 END) AS r' . (int) $i; } $row = $wpdb->get_row( 'SELECT ' . implode(', ', $select) . ", SUM(s.count_total) AS votes FROM {$stats} s INNER JOIN {$wpdb->posts} p ON p.ID = s.product_id WHERE p.post_type = 'product' AND p.post_status = 'publish' AND s.count_total > 0{$type_clause}", ARRAY_A ); $at = array(); foreach ($rungs as $i => $n) { $at[$n] = ($row && isset($row['r' . $i])) ? (int) $row['r' . $i] : 0; } $votes_total = ($row && isset($row['votes'])) ? (int) $row['votes'] : 0; $covered = isset($at[1]) ? (int) $at[1] : 0; $deep = isset($at[$prior_votes]) ? (int) $at[$prior_votes] : 0; $deep_pct = $total_published > 0 ? (100 * $deep / $total_published) : 0.0; if ($total_published <= 0) { $verdict = 'empty'; } elseif ($deep_pct >= 20.0) { $verdict = 'healthy'; } elseif ($deep_pct >= 5.0) { $verdict = 'building'; } else { $verdict = 'thin'; } $close = $this->card_open(array( 'anchor' => 'ratings-rating-coverage-can-ratings-rank-your-catalo', 'icon' => 'gauge', 'title' => __('Catalogue rating coverage', 'ai-eshop-optimizer'), 'description' => __('Ratings only sort your catalogue once enough products carry enough votes. This is where you stand.', 'ai-eshop-optimizer'), )); if ($verdict === 'empty') { $this->state('first-use', __('No published products yet — nothing to measure coverage against.', 'ai-eshop-optimizer')); echo $close; return; } echo '

'; echo wp_kses_post(sprintf( __('%1$s of %2$s published products carry at least one rating — %3$s of the catalogue.', 'ai-eshop-optimizer'), '' . esc_html(number_format_i18n($covered)) . '', '' . esc_html(number_format_i18n($total_published)) . '', esc_html($this->share_label($covered, $total_published)) )); echo ' '; if ($verdict === 'healthy') { $this->echo_pill(__('Healthy coverage', 'ai-eshop-optimizer'), true); } elseif ($verdict === 'building') { $this->echo_pill(__('Building', 'ai-eshop-optimizer'), false); } else { $this->echo_pill(__('Too thin to rank on', 'ai-eshop-optimizer'), false); } echo '

'; $unlocks = array( 1 => __('A star average appears on the product for shoppers.', 'ai-eshop-optimizer'), 3 => __('Enough votes to show up in the Top rated / Lowest rated tables below.', 'ai-eshop-optimizer'), $prior_votes => __('The product\'s own rating starts to outweigh the shop average when we rank it — and the low-rating safety check can apply.', 'ai-eshop-optimizer'), 10 => __('A steady signal: one unhappy customer no longer moves the average much.', 'ai-eshop-optimizer'), ); $ladder = array(); foreach ($rungs as $n) { $cnt = isset($at[$n]) ? (int) $at[$n] : 0; $ladder[] = array( 'depth' => sprintf( _n('%s vote or more', '%s votes or more', $n, 'ai-eshop-optimizer'), number_format_i18n($n) ), 'products' => $cnt, 'share' => $this->share_label($cnt, $total_published), 'share_pct' => $total_published > 0 ? round(100 * $cnt / $total_published, 1) : 0.0, 'unlocks' => isset($unlocks[$n]) ? $unlocks[$n] : '', ); } AIEO_Table::render(array( 'caption' => __('Catalogue rating coverage', 'ai-eshop-optimizer'), 'page_id' => $this->get_id(), 'row_key' => 'depth', 'sortable' => false, 'density' => 'compact', 'rows' => $ladder, 'empty' => __('No published products to measure.', 'ai-eshop-optimizer'), 'columns' => array( array('key' => 'depth', 'label' => __('Vote depth', 'ai-eshop-optimizer'), 'type' => 'text', 'width' => '180px'), array('key' => 'products', 'label' => __('Products', 'ai-eshop-optimizer'), 'type' => 'integer', 'width' => '110px'), array( 'key' => 'share', 'label' => __('Of catalogue', 'ai-eshop-optimizer'), 'type' => 'text', 'width' => '150px', 'sort_key' => 'share_pct', ), array('key' => 'unlocks', 'label' => __('What that unlocks', 'ai-eshop-optimizer'), 'type' => 'text', 'tone' => 'muted'), ), )); $orphans = (int) $rated_rows_all - $covered; if ($orphans > 0) { $this->note(esc_html(sprintf( _n( '%s rating record points at something that is not a published product (a draft, a deleted product, or a brand) and is left out of the figures above.', '%s rating records point at something that is not a published product (drafts, deleted products, or brands) and are left out of the figures above.', $orphans, 'ai-eshop-optimizer' ), number_format_i18n($orphans) )), 'mono'); } ob_start(); if ($verdict === 'healthy') { echo '

' . esc_html__('This is working.', 'ai-eshop-optimizer') . ' ' . esc_html(sprintf( _n( '%1$s product has %2$s votes or more, which is enough for ratings to genuinely tell your catalogue apart when we rank it. Nothing to fix here — just keep the votes coming in.', '%1$s products have %2$s votes or more, which is enough for ratings to genuinely tell your catalogue apart when we rank it. Nothing to fix here — just keep the votes coming in.', $deep, 'ai-eshop-optimizer' ), number_format_i18n($deep), number_format_i18n($prior_votes) )) . '

'; } else { echo '

' . esc_html__('What this means for ranking.', 'ai-eshop-optimizer') . ' ' . esc_html(sprintf( __('Until a product has about %s votes we treat its rating as "probably average" and lean on the shop-wide average instead. That is deliberate: one 5-star click should not outrank a product a hundred people liked.', 'ai-eshop-optimizer'), number_format_i18n($prior_votes) )) . '

'; echo '

' . esc_html(sprintf( _n( 'Right now %1$s product out of %3$s clears that bar, so ratings have almost nothing to sort — they are still doing their job on the page as social proof, they just are not ordering your catalogue yet.', 'Right now %1$s products out of %3$s clear that bar, so ratings have almost nothing to sort — they are still doing their job on the page as social proof, they just are not ordering your catalogue yet.', $deep, 'ai-eshop-optimizer' ), number_format_i18n($deep), number_format_i18n($prior_votes), number_format_i18n($total_published) )) . '

'; echo '

' . esc_html(sprintf( _n( 'The safety check that stops us promoting a badly-rated product also needs %1$s votes — today it can act on %2$s product.', 'The safety check that stops us promoting a badly-rated product also needs %1$s votes — today it can act on %2$s products.', isset($at[$veto_votes]) ? (int) $at[$veto_votes] : 0, 'ai-eshop-optimizer' ), number_format_i18n($veto_votes), number_format_i18n(isset($at[$veto_votes]) ? (int) $at[$veto_votes] : 0) )) . '

'; echo '

' . esc_html__('None of this is broken.', 'ai-eshop-optimizer') . ' ' . esc_html(sprintf( _n( 'Thin ratings are where every shop starts — you have %s vote so far. The fix is not a setting, it is asking more customers to rate.', 'Thin ratings are where every shop starts — you have %s votes so far. The fix is not a setting, it is asking more customers to rate.', $votes_total, 'ai-eshop-optimizer' ), number_format_i18n($votes_total) )) . '

'; } $consequence = (string) ob_get_clean(); $first = strpos($consequence, '

'); $head = substr($consequence, 3, $first - 3); $rest = substr($consequence, $first + 4); echo AIEO_DMM_UI::notice($head, 'healthy' === $verdict ? 'success' : 'info', array('detail' => $rest)); $wc_slug = defined('AIEO_Ratings::SOURCE_WC_REVIEW') ? (string) constant('AIEO_Ratings::SOURCE_WC_REVIEW') : 'wc_review'; $wc_mirrored = 0; foreach ((array) $by_source as $s) { $slug = is_object($s) ? (string) $s->source : (is_array($s) && isset($s['source']) ? (string) $s['source'] : ''); if ($slug === $wc_slug) { $wc_mirrored = is_object($s) ? (int) $s->c : (int) $s['c']; break; } } if (class_exists('WooCommerce') && get_option('woocommerce_enable_reviews') !== 'yes') { if ($wc_mirrored > 0) { $wc_off = esc_html(sprintf( _n( 'Note: WooCommerce product reviews are switched off on this shop (WooCommerce → Settings → Products → Enable product reviews), so no new review can come in through that route. The %s review already approved here is still counted in the figures above.', 'Note: WooCommerce product reviews are switched off on this shop (WooCommerce → Settings → Products → Enable product reviews), so no new review can come in through that route. The %s reviews already approved here are still counted in the figures above.', $wc_mirrored, 'ai-eshop-optimizer' ), number_format_i18n($wc_mirrored) )); } else { $wc_off = esc_html__('Note: WooCommerce product reviews are switched off on this shop (WooCommerce → Settings → Products → Enable product reviews), so no new ratings can be collected through the native review route — the default rating source on most shops.', 'ai-eshop-optimizer'); } $this->note($wc_off, 'warning'); } echo $close; } private function render_needs_ratings_card() { $result = $this->fetch_products_needing_ratings(15); $rows = is_array($result) && empty($result['note']) && isset($result['rows']) && is_array($result['rows']) ? $result['rows'] : array(); $close = $this->card_open(array( 'anchor' => 'ratings-where-to-ask-first', 'icon' => 'target', 'title' => __('Where to ask first', 'ai-eshop-optimizer'), 'description' => __('Products shoppers are already adding to the basket that carry no rating (or just one). Asking these buyers is the cheapest way to turn a flat rating signal into a useful one — the same shortlist the AI assistant builds from aieo/find-products-needing-ratings.', 'ai-eshop-optimizer'), 'flush' => !empty($rows), )); if ($result === null) { $this->state('not-set-up', __('The shortlist could not be built on this install — the ability that defines it is not available here.', 'ai-eshop-optimizer')); echo $close; return; } if (!empty($result['note'])) { $this->note(esc_html((string) $result['note'])); echo $close; return; } if (empty($rows)) { $this->state('first-use', __('No cart-adds recorded in the last 90 days, so there is no targeted shortlist to show. Ask on your best sellers instead.', 'ai-eshop-optimizer')); echo $close; return; } $shortlist = array(); $i = 0; foreach ($rows as $r) { $i++; $pid = isset($r['product_id']) ? (int) $r['product_id'] : 0; $title = isset($r['title']) ? (string) $r['title'] : ''; if ($title === '') { $title = '#' . $pid . ' ' . __('(deleted)', 'ai-eshop-optimizer'); } $votes = isset($r['votes']) ? (int) $r['votes'] : 0; $shortlist[] = array( 'n' => $i, 'product_id' => $pid, 'title' => $this->shorten($title), 'title_full' => $title, 'id_line' => 'ID: ' . $pid, 'cart_adds' => isset($r['cart_adds']) ? (int) $r['cart_adds'] : 0, 'votes' => $votes, 'votes_label' => $votes > 0 ? sprintf( _n('%s vote', '%s votes', $votes, 'ai-eshop-optimizer'), number_format_i18n($votes) ) : __('no ratings', 'ai-eshop-optimizer'), 'edit_url' => $pid > 0 ? admin_url('post.php?post=' . $pid . '&action=edit') : '', 'permalink' => isset($r['permalink']) ? (string) $r['permalink'] : '', ); } AIEO_Table::render(array( 'caption' => __('Where to ask first', 'ai-eshop-optimizer'), 'page_id' => $this->get_id(), 'row_key' => 'product_id', 'density' => 'compact', 'rows' => $shortlist, 'caps' => array('search' => true), 'filters' => array(array('key' => 'votes_label', 'label' => __('Votes', 'ai-eshop-optimizer'), 'kind' => 'values', 'control' => 'select')), 'per_page' => 0, 'empty' => __('No cart-adds recorded in the last 90 days.', 'ai-eshop-optimizer'), 'columns' => array( array('key' => 'n', 'label' => '#', 'type' => 'integer', 'width' => '50px'), array( 'key' => 'title', 'label' => __('Product', 'ai-eshop-optimizer'), 'type' => 'link', 'format' => array('href' => 'edit_url'), 'subline' => 'id_line', 'sort_key' => 'title_full', 'tooltip' => 'title_full', ), array('key' => 'title_full', 'label' => __('Product', 'ai-eshop-optimizer'), 'type' => 'text', 'hidden' => true), array('key' => 'cart_adds', 'label' => __('Cart adds (90 days)', 'ai-eshop-optimizer'), 'type' => 'integer'), array( 'key' => 'votes_label', 'label' => __('Votes', 'ai-eshop-optimizer'), 'type' => 'badge', 'sort_key' => 'votes', ), array('key' => 'votes', 'label' => __('Votes', 'ai-eshop-optimizer'), 'type' => 'integer', 'hidden' => true), ), 'actions' => array( array( 'id' => 'view', 'label' => __('View', 'ai-eshop-optimizer'), 'description' => __('Opens the product on the shop.', 'ai-eshop-optimizer'), 'href' => static function ($row) { return isset($row['permalink']) ? (string) $row['permalink'] : ''; }, 'visible' => static function ($row) { return ! empty($row['permalink']); }, ), ), )); echo $close; } private function fetch_products_needing_ratings($limit) { $cls = 'AIEO\\Claude\\Abilities\\FindProductsNeedingRatingsAbility'; if (!class_exists($cls)) { $base = AIEO_DIR . 'includes/claude/abilities/'; foreach (array('class-period-helper.php', 'class-find-products-needing-ratings-ability.php') as $file) { if (file_exists($base . $file)) { require_once $base . $file; } } } if (!class_exists($cls) || !is_callable(array($cls, 'execute'))) { return null; } $out = call_user_func(array($cls, 'execute'), array( 'period' => 'last_90_days', 'min_cart_adds' => 1, 'max_votes' => 1, 'limit' => max(1, (int) $limit), )); return is_array($out) ? $out : null; } private function stats_has_object_type() { static $has = null; if ($has !== null) { return $has; } global $wpdb; $has = ((int) $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s AND COLUMN_NAME = 'object_type'", $wpdb->prefix . 'aieo_rating_stats' ))) > 0; return $has; } private function share_label($count, $total) { $count = (int) $count; $total = (int) $total; if ($total <= 0) { return number_format_i18n(0, 1) . '%'; } $pct = 100 * $count / $total; if ($count > 0 && $pct < 0.05) { return '< ' . number_format_i18n(0.1, 1) . '%'; } return number_format_i18n(round($pct, 1), 1) . '%'; } private function echo_pill($text, $good = false) { if (class_exists('\Rooster\Admin\Ui')) { echo \Rooster\Admin\Ui::badge((string) $text, $good ? 'success' : 'mono'); return; } echo '' . esc_html((string) $text) . ''; } private function shorten($text, $len = 70) { $text = (string) $text; if (function_exists('mb_strlen') && function_exists('mb_substr')) { return mb_strlen($text, 'UTF-8') > $len ? mb_substr($text, 0, $len, 'UTF-8') . '…' : $text; } return strlen($text) > $len ? substr($text, 0, $len) . '…' : $text; } private function render_distribution_card($row) { $row = is_array($row) ? $row : array(); $n = array( 1 => (int) ($row['n1'] ?? 0), 2 => (int) ($row['n2'] ?? 0), 3 => (int) ($row['n3'] ?? 0), 4 => (int) ($row['n4'] ?? 0), 5 => (int) ($row['n5'] ?? 0), ); $total = array_sum($n); $close = $this->card_open(array( 'anchor' => 'ratings-site-wide-rating-distribution', 'icon' => 'chart-column', 'title' => __('Vote distribution per star', 'ai-eshop-optimizer'), 'description' => __('How votes break down across the 5 stars. Bigger 4–5 buckets vs 1–2 = healthy product mix.', 'ai-eshop-optimizer'), 'flush' => true, )); $dist = array(); for ($i = 5; $i >= 1; $i--) { $dist[] = array( 'rating' => $i, 'stars' => \Rooster\Admin\Ui::stars($i, array('sm' => true)), 'share' => $total > 0 ? round(100 * $n[$i] / $total, 1) : 0, 'votes' => $n[$i], ); } AIEO_Table::render(array( 'caption' => __('Vote distribution per star', 'ai-eshop-optimizer'), 'page_id' => $this->get_id(), 'row_key' => 'rating', 'sortable' => false, 'density' => 'compact', 'rows' => $dist, 'empty' => __('No votes yet.', 'ai-eshop-optimizer'), 'columns' => array( array('key' => 'stars', 'label' => __('Rating', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'xs', 'nowrap' => true), array( 'key' => 'share', 'label' => __('Distribution', 'ai-eshop-optimizer'), 'type' => 'bar', 'format' => array('max' => 100, 'precision' => 1), 'suffix' => '%', ), array('key' => 'votes', 'label' => __('Votes', 'ai-eshop-optimizer'), 'type' => 'integer', 'width' => '120px'), ), )); echo $close; } private function render_source_card($rows, $total_events) { $close = $this->card_open(array( 'anchor' => 'ratings-where-ratings-come-from', 'icon' => 'door-open', 'title' => __('Where ratings come from', 'ai-eshop-optimizer'), 'description' => __('Source breakdown: which surface the shopper was on when they rated.', 'ai-eshop-optimizer'), 'flush' => true, )); $sources = array(); foreach ((array) $rows as $r) { $sources[] = array( 'source' => (string) $r->source, 'events' => (int) $r->c, 'share' => $total_events > 0 ? round(100 * ((int) $r->c) / $total_events, 1) : 0, ); } AIEO_Table::render(array( 'caption' => __('Where ratings come from', 'ai-eshop-optimizer'), 'page_id' => $this->get_id(), 'row_key' => 'source', 'density' => 'compact', 'rows' => $sources, 'empty' => __('No rating events yet.', 'ai-eshop-optimizer'), 'columns' => array( array('key' => 'source', 'label' => __('Source', 'ai-eshop-optimizer'), 'type' => 'code'), array('key' => 'events', 'label' => __('Events', 'ai-eshop-optimizer'), 'type' => 'integer'), array( 'key' => 'share', 'label' => __('Percentage', 'ai-eshop-optimizer'), 'type' => 'bar', 'format' => array('max' => 100, 'precision' => 1), 'suffix' => '%', ), ), )); echo $close; } private function render_top_card($title, $rows, $show_avg, $anchor = '', $icon = '', $description = '') { $close = $this->card_open(array( 'anchor' => (string) $anchor, 'icon' => (string) $icon, 'title' => (string) $title, 'description' => (string) $description, 'flush' => !empty($rows), )); if (empty($rows)) { $this->state('first-use', __('Not enough rating data yet (each product needs at least 3 votes to appear here).', 'ai-eshop-optimizer')); echo $close; return; } $list = array(); foreach ($rows as $i => $r) { $pid = (int) $r->product_id; $count = (int) $r->count_total; $sum = (int) $r->sum_total; $title = $pid > 0 ? get_the_title($pid) : ''; if ($title === '' || $title === null) { $title = '#' . $pid . ' ' . __('(deleted)', 'ai-eshop-optimizer'); } $list[] = array( 'n' => $i + 1, 'product_id' => $pid, 'title' => $title, 'id_line' => 'ID: ' . $pid, 'avg' => $count > 0 ? round($sum / $count, 2) : 0, 'votes' => $count, 'edit_url' => $pid > 0 ? admin_url('post.php?post=' . $pid . '&action=edit') : '', 'permalink' => $pid > 0 ? get_permalink($pid) : '', ); } AIEO_Table::render(array( 'caption' => (string) $title, 'page_id' => $this->get_id(), 'row_key' => 'product_id', 'density' => 'compact', 'rows' => $list, 'caps' => array('search' => true), 'per_page' => 0, 'empty' => __('No rated products in this list yet.', 'ai-eshop-optimizer'), 'columns' => array( array('key' => 'n', 'label' => '#', 'type' => 'integer', 'width' => '50px'), array( 'key' => 'title', 'label' => __('Product', 'ai-eshop-optimizer'), 'type' => 'link', 'format' => array('href' => 'edit_url'), 'subline' => 'id_line', 'truncate' => 70, 'tooltip' => true, ), array('key' => 'avg', 'label' => __('Avg', 'ai-eshop-optimizer'), 'type' => 'decimal', 'format' => array('precision' => 2)), array('key' => 'votes', 'label' => __('Votes', 'ai-eshop-optimizer'), 'type' => 'integer'), ), 'actions' => array( array( 'id' => 'view', 'label' => __('View', 'ai-eshop-optimizer'), 'description' => __('Opens the product on the shop.', 'ai-eshop-optimizer'), 'href' => static function ($row) { return isset($row['permalink']) ? (string) $row['permalink'] : ''; }, 'visible' => static function ($row) { return ! empty($row['permalink']); }, ), ), )); echo $close; unset($show_avg); } private function card_open(array $c) { list($open, $close) = AIEO_DMM_UI::card($c); echo $open; return $close; } private function band_open(array $opts) { if (function_exists('rooster_admin_band_open')) { rooster_admin_band_open($opts); } } private function band_close() { if (function_exists('rooster_admin_band_close')) { rooster_admin_band_close(); } } private function note($html, $tone = 'info') { if (class_exists('\Rooster\Admin\Ui')) { echo \Rooster\Admin\Ui::notice($html, $tone, array('inline' => true)); return; } echo '

' . $html . '

'; } private function state($kind, $title) { if (class_exists('\Rooster\Admin\States')) { echo \Rooster\Admin\States::state((string) $kind, array('title' => (string) $title, 'compact' => true)); return; } echo '

' . esc_html((string) $title) . '

'; } private function tile($label, $value) { if (class_exists('\Rooster\Admin\Ui')) { echo \Rooster\Admin\Ui::stat((string) $label, (string) $value); return; } echo '
'; echo '
' . esc_html($label) . '
'; echo '
' . esc_html((string) $value) . '
'; echo '
'; } }