label. * * @return array */ public static function tabs() { return array( 'ai-features' => __('AI Features', 'agentic-commerce-product-images'), 'display' => __('On-store Display', 'agentic-commerce-product-images'), 'billing' => __('Billing & Usage', 'agentic-commerce-product-images'), 'privacy' => __('Privacy', 'agentic-commerce-product-images'), ); } public static function boot() { add_action('admin_menu', [__CLASS__, 'menu']); add_filter('plugin_action_links_' . plugin_basename(ACPIMG_FILE), [__CLASS__, 'action_links']); add_action('admin_init', [__CLASS__, 'settings']); add_action('admin_init', [__CLASS__, 'maybe_activation_redirect']); add_action('admin_init', [__CLASS__, 'handle_privacy']); add_action('admin_post_acpimg_token_callback', [__CLASS__, 'token_callback']); add_action('admin_enqueue_scripts', [__CLASS__, 'assets']); add_action('wp_ajax_acpimg_buy_pack', [__CLASS__, 'ajax_buy_pack']); } /** * One-time redirect to the dashboard right after activation. Never on bulk * activation, hijacking a bulk activate is the pattern reviewers object to. * * @return void */ public static function maybe_activation_redirect() { if (!get_option('acpimg_activation_redirect')) { return; } // Clear the flag first so a failed redirect can never loop. delete_option('acpimg_activation_redirect'); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if (isset($_GET['activate-multi']) || wp_doing_ajax() || is_network_admin()) { return; } if (!current_user_can('manage_options')) { return; } wp_safe_redirect(admin_url('admin.php?page=' . self::SLUG)); exit; } public static function ajax_buy_pack() { check_ajax_referer('acpimg_action', '_wpnonce'); if (!current_user_can('manage_options')) wp_send_json_error(['error' => 'forbidden'], 403); $sku = isset($_POST['sku']) ? sanitize_key(wp_unslash($_POST['sku'])) : 'acpimg_bundle_500'; $res = Acpimg_Provider_Xpay_Managed::create_checkout($sku); if (is_wp_error($res)) wp_send_json_error(['error' => $res->get_error_code(), 'message' => $res->get_error_message()]); wp_send_json_success($res); } /** * Add Dashboard + Settings links to the plugin's row on the Plugins screen, * so it's reachable without hunting the sidebar (helpful on busy sites). * * @param array $links Existing action links. * @return array */ public static function action_links($links) { $mine = [ '' . esc_html__('Dashboard', 'agentic-commerce-product-images') . '', '' . esc_html__('Settings', 'agentic-commerce-product-images') . '', ]; return array_merge($mine, $links); } /** * Shared footer byline (matches the sibling plugins). * * @return void */ public static function footer() { echo ''; } public static function menu() { add_menu_page( __('Product Images', 'agentic-commerce-product-images'), __('Product Images', 'agentic-commerce-product-images'), 'manage_options', self::SLUG, [__CLASS__, 'render_dashboard'], 'dashicons-format-image', 58 ); add_submenu_page(self::SLUG, __('Dashboard', 'agentic-commerce-product-images'), __('Dashboard', 'agentic-commerce-product-images'), 'manage_options', self::SLUG, [__CLASS__, 'render_dashboard']); add_submenu_page(self::SLUG, __('AI Shopper Audit', 'agentic-commerce-product-images'), __('AI Shopper Audit', 'agentic-commerce-product-images'), 'edit_products', 'acpimg-audit', [__CLASS__, 'render_audit']); add_submenu_page(self::SLUG, __('Settings', 'agentic-commerce-product-images'), __('Settings', 'agentic-commerce-product-images'), 'manage_options', self::SETTINGS_SLUG, [__CLASS__, 'render_settings']); } public static function settings() { register_setting('acpimg_settings_group', 'acpimg_settings', [__CLASS__, 'sanitize']); } /** * Merge-preserving sanitize. The AI Features and On-store Display tabs share * one option row, so a save from one tab must not wipe the other tab's keys * (or the xpay token, which is set out-of-band by the OAuth callback). Text * keys update only when present; the checkboxes are updated only when the * Display tab's marker is submitted. * * @param array $input Raw submitted values. * @return array */ public static function sanitize($input) { $existing = get_option('acpimg_settings', []); $out = is_array($existing) ? $existing : []; if (!is_array($input)) { return $out; } if (array_key_exists('xpay_token', $input)) { $out['xpay_token'] = sanitize_text_field($input['xpay_token']); } if (array_key_exists('default_tier', $input)) { $out['default_tier'] = ($input['default_tier'] === 'premium') ? 'premium' : 'default'; } // Checkboxes are owned by the Display tab; only touch them when that tab // posted (an unchecked box submits nothing, so we need the marker). if (!empty($input['_display_tab'])) { $out['flipper_disabled'] = !empty($input['flipper_disabled']) ? 1 : 0; } unset($out['_display_tab']); return $out; } /** * Cache-busting asset version: the file's mtime when it exists, so a changed * stylesheet/script is always refetched even without a plugin version bump; * falls back to the plugin version. Avoids the "reinstalled but browser served * a stale admin.css" trap. * * @param string $rel Path relative to the plugin root (e.g. 'public/admin.css'). * @return string */ private static function asset_ver($rel) { $path = ACPIMG_DIR . $rel; $mtime = file_exists($path) ? filemtime($path) : 0; return $mtime ? (string) $mtime : ACPIMG_VERSION; } public static function assets($hook) { if (strpos($hook, 'acpimg') === false) return; wp_enqueue_style('acpimg-admin', ACPIMG_URL . 'public/admin.css', [], self::asset_ver('public/admin.css')); wp_enqueue_script('acpimg-admin', ACPIMG_URL . 'public/admin.js', ['jquery'], self::asset_ver('public/admin.js'), true); wp_localize_script('acpimg-admin', 'ACPIMG', [ 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('acpimg_action'), 'api_base' => ACPIMG_API_BASE, 'app_base' => ACPIMG_APP_BASE, ]); // The audit page app (progressive local scan + soft explain CTA). if (strpos($hook, 'acpimg-audit') !== false) { wp_enqueue_script('acpimg-audit', ACPIMG_URL . 'public/audit.js', ['acpimg-admin'], self::asset_ver('public/audit.js'), true); } } public static function render_dashboard() { $mode = Acpimg_Plugin::auth_mode(); $usage = get_option('acpimg_last_usage', null); ?>

4×

1:1 · GMC
4:5 · Pinterest
16:9 · Video

/
/


array('success', __('This site was disconnected. AI tools are off until you sign in again.', 'agentic-commerce-product-images')), 'connected' => array('success', __('Connected to xpay✦ Commerce. AI features are ready.', 'agentic-commerce-product-images')), ); if (!isset($map[$code])) { return; } printf('

%s

', esc_attr($map[$code][0]), esc_html($map[$code][1])); } private static function current_tab() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'ai-features'; $tabs = self::tabs(); return isset($tabs[$tab]) ? $tab : 'ai-features'; } private static function settings_url($tab = 'ai-features', $notice = '') { $args = array('page' => self::SETTINGS_SLUG, 'tab' => $tab); if ($notice) { $args['acpimg_notice'] = $notice; } return add_query_arg($args, admin_url('admin.php')); } public static function onboard_url() { // Build the return URL with RAW ampersands. wp_nonce_url() HTML-encodes // & to & (it's meant for HTML attributes); if that entity reaches the // browser redirect, _wpnonce arrives as "amp;_wpnonce" and the callback // nonce check fails with "The link you followed has expired." $return = add_query_arg([ 'action' => 'acpimg_token_callback', '_wpnonce' => wp_create_nonce('acpimg_token_callback'), ], admin_url('admin-post.php')); return add_query_arg([ 'plugin' => 'acpimg', 'site' => rawurlencode(home_url()), 'returnTo' => rawurlencode($return), ], ACPIMG_APP_BASE . '/onboard/product-images'); } public static function token_callback() { if (!current_user_can('manage_options')) wp_die('forbidden'); check_admin_referer('acpimg_token_callback'); $token = isset($_GET['token']) ? sanitize_text_field(wp_unslash($_GET['token'])) : ''; if (!$token) { wp_safe_redirect(self::settings_url('ai-features')); exit; } $email = isset($_GET['email']) ? sanitize_email(wp_unslash($_GET['email'])) : ''; $opts = get_option('acpimg_settings', []); $opts['xpay_token'] = $token; if ($email) { $opts['xpay_email'] = $email; } update_option('acpimg_settings', $opts); // Record the site -> account (email) mapping. The backend resolves the // authoritative email from the Privy user (the client param is empty for // Google/OAuth logins), so prefer whatever it returns. $res = Acpimg_Provider_Xpay_Managed::connect($email); if (is_array($res) && !empty($res['email'])) { $opts = get_option('acpimg_settings', []); $opts['xpay_email'] = sanitize_email($res['email']); update_option('acpimg_settings', $opts); } Acpimg_Provider_Xpay_Managed::get_usage(); wp_safe_redirect(self::settings_url('ai-features', 'connected')); exit; } }