'chatgpt', 'Google AI Mode' => 'size', 'SEO + AI' => 'alt', 'Pinterest' => 'pinterest', ]; public static function boot() { add_action('wp_ajax_acpimg_audit_product', [__CLASS__, 'ajax_audit']); add_action('wp_ajax_acpimg_audit_scan', [__CLASS__, 'ajax_scan']); add_action('wp_ajax_acpimg_audit_explain', [__CLASS__, 'ajax_explain']); } public static function ajax_audit() { check_ajax_referer('acpimg_action', '_wpnonce'); if (!current_user_can('edit_products')) wp_send_json_error(['error' => 'forbidden'], 403); $product_id = isset($_POST['product_id']) ? absint(wp_unslash($_POST['product_id'])) : 0; if (!$product_id) wp_send_json_error(['error' => 'missing_product_id']); wp_send_json_success(self::audit_product($product_id)); } /** * Progressive store scan: audits one page of published products and returns * card-ready rows. The JS pages through until done, updating the hero score * and the "Auditing X/Y" counter. All local, no network, no account. * * @return void */ public static function ajax_scan() { check_ajax_referer('acpimg_action', '_wpnonce'); if (!current_user_can('edit_products')) wp_send_json_error(['error' => 'forbidden'], 403); $offset = isset($_POST['offset']) ? absint(wp_unslash($_POST['offset'])) : 0; $limit = isset($_POST['limit']) ? min(24, max(1, absint(wp_unslash($_POST['limit'])))) : 12; $q = new WP_Query([ 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $limit, 'offset' => $offset, 'orderby' => 'date', 'order' => 'DESC', 'fields' => 'ids', 'no_found_rows' => false, ]); $total = (int) $q->found_posts; $rows = []; foreach ($q->posts as $pid) { $rows[] = self::card_for($pid); } wp_send_json_success([ 'rows' => $rows, 'total' => $total, 'offset' => $offset, 'done' => ($offset + $limit) >= $total, ]); } /** * Build a card-ready audit row for one product: title, thumbnail, score, * and the distinct failing channels (deduped) for the chips. * * @param int $pid Product id. * @return array */ private static function card_for($pid) { $a = self::audit_product($pid); $issues = []; if (!empty($a['findings'])) { foreach ($a['findings'] as $f) { $ch = isset($f['channel']) ? $f['channel'] : ''; $key = isset(self::CHANNEL_KEY[$ch]) ? self::CHANNEL_KEY[$ch] : null; if ($key) $issues[$key] = 1; } } $thumb = get_the_post_thumbnail_url($pid, 'woocommerce_thumbnail'); if (!$thumb) $thumb = wc_placeholder_img_src('woocommerce_thumbnail'); return [ 'id' => (int) $pid, 'title' => html_entity_decode(get_the_title($pid), ENT_QUOTES), 'thumb' => $thumb, 'edit' => get_edit_post_link($pid, 'raw'), 'score' => isset($a['score']) ? (int) $a['score'] : 0, 'issues' => array_keys($issues), ]; } /** * Soft, no-account "explain my audit" proxy. The browser sends the local * aggregate; we add verified store context server-side and forward to the * managed endpoint, which returns a warm, revenue-framed explanation. A * store lead is captured backend-side. If the request fails for any reason, * we still answer with a local deterministic explanation, this CTA can * never dead-end. * * @return void */ public static function ajax_explain() { check_ajax_referer('acpimg_action', '_wpnonce'); if (!current_user_can('edit_products')) wp_send_json_error(['error' => 'forbidden'], 403); $scope = (isset($_POST['scope']) && 'product' === sanitize_key(wp_unslash($_POST['scope']))) ? 'product' : 'store'; $score = isset($_POST['score']) ? absint(wp_unslash($_POST['score'])) : 0; $products = isset($_POST['products']) ? absint(wp_unslash($_POST['products'])) : 0; $raw = isset($_POST['issues']) ? array_map('absint', (array) wp_unslash($_POST['issues'])) : []; $issues = [ 'chatgpt' => isset($raw['chatgpt']) ? (int) $raw['chatgpt'] : 0, 'size' => isset($raw['size']) ? (int) $raw['size'] : 0, 'alt' => isset($raw['alt']) ? (int) $raw['alt'] : 0, 'pinterest' => isset($raw['pinterest']) ? (int) $raw['pinterest'] : 0, ]; $payload = [ 'scope' => $scope, 'home_url' => home_url(), 'wp_version' => get_bloginfo('version'), 'wc_version' => defined('WC_VERSION') ? WC_VERSION : '', 'plugin_version' => ACPIMG_VERSION, 'products' => $products, 'score' => $score, 'issues' => $issues, ]; $res = wp_remote_post(ACPIMG_API_BASE . '/product-images/audit/explain', [ 'timeout' => 12, 'headers' => ['Content-Type' => 'application/json'], 'body' => wp_json_encode($payload), ]); if (!is_wp_error($res) && wp_remote_retrieve_response_code($res) === 200) { $json = json_decode(wp_remote_retrieve_body($res), true); if (is_array($json) && !empty($json['headline'])) { wp_send_json_success($json); } } // Network/endpoint failure → local deterministic fallback. wp_send_json_success(self::local_explain($payload)); } /** * Local deterministic explanation, the belt-and-suspenders fallback if the * managed endpoint is unreachable. Mirrors the backend's deterministic build. * * @param array $p Payload with products/score/issues. * @return array */ private static function local_explain($p) { $products = (int) $p['products']; $score = (int) $p['score']; $i = $p['issues']; $parts = []; if (!empty($i['chatgpt'])) { /* translators: %d: number of products. */ $parts[] = sprintf(_n('%d product ships WebP/AVIF that ChatGPT Shopping cannot render.', '%d products ship WebP/AVIF that ChatGPT Shopping cannot render.', $i['chatgpt'], 'agentic-commerce-product-images'), $i['chatgpt']); } if (!empty($i['size'])) { /* translators: %d: number of images below the size minimum. */ $parts[] = sprintf(_n('%d image is below Google AI Mode\'s 500×500 minimum.', '%d images are below Google AI Mode\'s 500×500 minimum.', $i['size'], 'agentic-commerce-product-images'), $i['size']); } if (!empty($i['alt'])) { /* translators: %d: number of images missing alt text. */ $parts[] = sprintf(_n('%d image has no alt text.', '%d images have no alt text.', $i['alt'], 'agentic-commerce-product-images'), $i['alt']); } if (!empty($i['pinterest'])) { /* translators: %d: number of images outside the preferred aspect ratio. */ $parts[] = sprintf(_n('%d image is outside Pinterest\'s preferred ratio.', '%d images are outside Pinterest\'s preferred ratio.', $i['pinterest'], 'agentic-commerce-product-images'), $i['pinterest']); } $is_product = isset($p['scope']) && 'product' === $p['scope']; $grade = $score >= 90 ? __('excellent', 'agentic-commerce-product-images') : ($score >= 75 ? __('good', 'agentic-commerce-product-images') : ($score >= 55 ? __('shaky', 'agentic-commerce-product-images') : __('at risk', 'agentic-commerce-product-images'))); if ($is_product) { $headline = sprintf( /* translators: 1: score, 2: grade word. */ __('This product scores %1$d/100 for AI shoppers, %2$s.', 'agentic-commerce-product-images'), $score, $grade ); $intro = __('AI assistants read this product’s image before a shopper ever does, and they are stricter than a browser. ', 'agentic-commerce-product-images'); $clear = __('This product’s image already meets the format, size and alt-text bars ChatGPT, Google AI Mode and Pinterest look for.', 'agentic-commerce-product-images'); } else { $headline = sprintf( /* translators: 1: score, 2: grade word. */ __('Your catalog scores %1$d/100 for AI shoppers, %2$s.', 'agentic-commerce-product-images'), $score, $grade ); $intro = __('AI assistants read your product images before a shopper ever does, and they are stricter than a browser. ', 'agentic-commerce-product-images'); $clear = __('Your images already meet the format, size and alt-text bars ChatGPT, Google AI Mode and Pinterest look for. Keep new uploads in JPEG/PNG at 500px or larger.', 'agentic-commerce-product-images'); } $body = $parts ? $intro . implode(' ', $parts) : $clear; return [ 'headline' => $headline, 'body' => $body, 'action' => $parts ? __('Connect to fix these in one pass.', 'agentic-commerce-product-images') : __('You are in good shape, keep it up.', 'agentic-commerce-product-images'), 'source' => 'deterministic-local', ]; } public static function audit_product($product_id) { $product = function_exists('wc_get_product') ? wc_get_product($product_id) : null; if (!$product) return ['error' => 'not_a_product']; $image_id = $product->get_image_id(); $gallery = $product->get_gallery_image_ids(); $all = array_filter(array_merge([$image_id], $gallery)); $findings = []; $score = 100; foreach ($all as $id) { $path = get_attached_file($id); $url = wp_get_attachment_url($id); if (!$path || !file_exists($path)) continue; // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $size = @getimagesize($path); if (!$size) continue; list($w, $h) = $size; $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); // ChatGPT/ACP: requires JPEG or PNG. if (in_array($ext, ['webp', 'avif'], true)) { $findings[] = [ 'severity' => 'high', 'channel' => 'ChatGPT', 'attachment_id' => $id, 'msg' => sprintf('Image %d is %s, ChatGPT carousel cannot render. Re-save as JPEG.', $id, strtoupper($ext)), ]; $score -= 15; } // Google AI Mode: 500×500 min (effective 30 Jun 2026). if ($w < 500 || $h < 500) { $findings[] = [ 'severity' => 'high', 'channel' => 'Google AI Mode', 'attachment_id' => $id, 'msg' => sprintf('Image %d is %d×%d, below Google\'s 500×500 minimum. Enhance to 2x.', $id, $w, $h), ]; $score -= 10; } // Missing alt. $alt = get_post_meta($id, '_wp_attachment_image_alt', true); if (!$alt) { $findings[] = [ 'severity' => 'medium', 'channel' => 'SEO + AI', 'attachment_id' => $id, 'msg' => sprintf('Image %d has no alt text. Run AI Alt Text.', $id), ]; $score -= 5; } // Pinterest: aspect 2:3 to 1:1 ideal. if ($h > 0) { $ratio = $w / $h; if ($ratio > 1.3 || $ratio < 0.6) { $findings[] = [ 'severity' => 'low', 'channel' => 'Pinterest', 'attachment_id' => $id, 'msg' => sprintf('Image %d aspect ratio %.2f is outside Pinterest 2:3-1:1 ideal. Use Auto-Crop.', $id, $ratio), ]; $score -= 3; } } } $score = max(0, $score); return [ 'product_id' => $product_id, 'score' => $score, 'findings' => array_slice($findings, 0, 10), 'image_count' => count($all), ]; } }