[label, render (callable), cap].
*/
public static function views() {
if (self::$views !== null) {
return self::$views;
}
$admin = AIFLT_PLUGIN_PATH . 'includes/admin/';
$template = static function ($file) use ($admin) {
return static function () use ($admin, $file) {
include $admin . $file;
};
};
$views = [
'leak-scan' => [
'slug' => 'ai_flash_tune',
'icon' => 'droplet',
'title' => __('Leak Scan', 'ai-flash-tune'),
'group' => 'main',
'tabs' => ['overview' => ['label' => '', 'render' => $template('leak-scan.php')]],
],
'funnel' => [
'slug' => 'aiflt-funnel',
'icon' => 'funnel',
'title' => __('Funnel', 'ai-flash-tune'),
'group' => 'main',
'tabs' => ['overview' => ['label' => '', 'render' => $template('funnel-analysis.php')]],
],
'products' => [
'slug' => 'aiflt-products',
'icon' => 'package',
'title' => __('Products', 'ai-flash-tune'),
'group' => 'main',
'tabs' => ['performance' => ['label' => '', 'render' => $template('products.php')]],
],
'product-detail' => [
'slug' => 'aiflt-product-detail',
'icon' => 'package',
'title' => __('Product', 'ai-flash-tune'),
// The product's own name as the page title.
'title_cb' => static function () {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only.
$id = isset($_GET['product_id']) ? absint($_GET['product_id']) : 0;
return $id && get_post_type($id) === 'product' ? get_the_title($id) : '';
},
'group' => 'hidden',
'parent' => 'products',
'tabs' => ['overview' => ['label' => '', 'render' => $template('product-detail.php')]],
],
'carts' => [
'slug' => 'aiflt-abandoned-carts',
'icon' => 'cart',
'title' => __('Carts', 'ai-flash-tune'),
'group' => 'main',
'tabs' => ['carts' => ['label' => '', 'render' => $template('abandoned-carts.php')]],
],
'checkout' => [
'slug' => 'aiflt-checkout',
'icon' => 'card',
'title' => __('Checkout', 'ai-flash-tune'),
'group' => 'main',
'tabs' => ['fields' => ['label' => '', 'render' => $template('checkout.php')]],
],
'reports' => [
'slug' => 'aiflt-reports',
'icon' => 'chart',
'title' => __('Reports', 'ai-flash-tune'),
'group' => 'main',
'tabs' => [
// AI Flash Tune Pro replaces this render with the store summary.
'summary' => ['label' => __('Summary', 'ai-flash-tune'), 'render' => self::locked('summary')],
'client' => ['label' => __('Client reports', 'ai-flash-tune'), 'render' => $template('growth-tools.php')],
'ai-log' => ['label' => __('AI activity', 'ai-flash-tune'), 'render' => $template('ai-usage-reports.php')],
],
],
'import' => [
'slug' => 'aiflt-import',
'icon' => 'download',
'title' => __('Import', 'ai-flash-tune'),
'group' => 'footer',
'cap' => 'manage_woocommerce',
// Another cart plugin's data found here and not brought over yet.
'badge' => static function () {
return class_exists('AIFLT_Cart_Import') ? AIFLT_Cart_Import::waiting_count() : 0;
},
'tabs' => ['overview' => ['label' => '', 'cap' => 'manage_woocommerce', 'render' => $template('import.php')]],
],
'settings' => [
'slug' => 'aiflt_settings',
'icon' => 'sliders',
'title' => __('Settings', 'ai-flash-tune'),
'group' => 'footer',
'tabs' => [
'general' => ['label' => __('General', 'ai-flash-tune'), 'render' => $template('settings.php')],
'privacy' => ['label' => __('Privacy requests', 'ai-flash-tune'), 'render' => $template('privacy-tools.php')],
'system' => ['label' => __('System status', 'ai-flash-tune'), 'render' => $template('help.php')],
],
],
'licence' => [
'slug' => 'aiflt-licence',
'icon' => 'key',
'title' => __('Licence', 'ai-flash-tune'),
'group' => 'footer',
'status' => static function () {
return aiflt_is_pro_active() ? ['Active', 'good'] : ['Free', 'neutral'];
},
'tabs' => ['overview' => ['label' => '', 'render' => $template('pro.php')]],
],
// Pro pages are always listed; without a licence they explain what they do.
// AI Flash Tune Pro replaces their render when it is licensed.
'action-plan' => [
'slug' => 'aiflt-action-plan',
'icon' => 'list-checks',
'title' => __('Action Plan', 'ai-flash-tune'),
'group' => 'pro',
'pro' => true,
'tabs' => ['overview' => ['label' => '', 'render' => self::locked('action-plan')]],
],
'alerts' => [
'slug' => 'aiflt-alerts',
'icon' => 'bell',
'title' => __('Alerts', 'ai-flash-tune'),
'group' => 'pro',
'pro' => true,
'tabs' => ['overview' => ['label' => '', 'render' => self::locked('alerts')]],
],
'help' => [
'slug' => 'aiflt-help',
'icon' => 'help',
'title' => __('Help', 'ai-flash-tune'),
'group' => 'footer',
'tabs' => ['overview' => ['label' => '', 'render' => $template('help-guide.php')]],
],
];
/**
* Add pages or tabs. AI Flash Tune Pro uses this for its modules.
*/
// Footer order: Settings, Licence, Help.
$help = $views['help'];
unset($views['help']);
$views['help'] = $help;
$views = apply_filters('aiflt_admin_views', $views);
foreach ($views as $key => &$view) {
$view['key'] = $key;
$view['cap'] = $view['cap'] ?? 'manage_options';
$view['group'] = $view['group'] ?? 'main';
}
unset($view);
self::$views = $views;
return $views;
}
public static function view_by_slug($slug) {
foreach (self::views() as $view) {
if ($view['slug'] === $slug) {
return $view;
}
}
return null;
}
public static function register_menus() {
$views = self::views();
add_menu_page(__('AI Flash Tune', 'ai-flash-tune'), __('AI Flash Tune', 'ai-flash-tune'), 'manage_options', 'ai_flash_tune', [__CLASS__, 'render'], self::menu_icon(), 25);
// Same order as the plugin's own sidebar: main pages, Pro, then Import, Settings, Licence, Help.
$rank = ['main' => 0, 'pro' => 1, 'footer' => 2, 'hidden' => 3];
$position = array_flip(array_keys($views));
uasort($views, static function ($a, $b) use ($rank, $position) {
$ga = $rank[$a['group']] ?? 0;
$gb = $rank[$b['group']] ?? 0;
return $ga === $gb ? $position[$a['key']] <=> $position[$b['key']] : $ga <=> $gb;
});
foreach ($views as $view) {
add_submenu_page(
$view['group'] === 'hidden' ? 'ai_flash_tune_hide' : 'ai_flash_tune',
$view['title'],
$view['title'],
$view['cap'],
$view['slug'],
[__CLASS__, 'render']
);
}
// Kept for anything that still hooks the old menu action.
do_action('aiflt_register_submenu_pages');
}
/**
* Pages that were merged keep working: their old addresses open the page
* and tab that now holds them, with any other query arguments kept.
*/
public static function redirect_old_pages() {
$page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
$map = [
'aiflt-setup-wizard' => ['leak-scan', ''],
'aiftas-segmentation' => ['funnel', ''],
'aiftpo-product-optimizer' => ['products', ''],
'aiftcr-recovery-center' => ['carts', ''],
'aiftcr-email-log' => ['carts', ''],
'aiftco-optimizer-dashboard' => ['checkout', ''],
'aiftrp-reports' => ['reports', 'summary'],
'aiflt-ai-usage-reports' => ['reports', 'ai-log'],
'aiflt-growth-tools' => ['reports', 'client'],
'aiflt-privacy' => ['settings', 'privacy'],
'aiflt-pro' => ['licence', ''],
'aiftac-action-center' => ['action-plan', ''],
'aiftal-alert-center' => ['alerts', ''],
];
// Tabs that became part of their page (2.0 layout) or moved.
$tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : '';
$moved_tabs = [
'aiflt-funnel' => ['compare' => ['funnel', '', ['compare' => 'device']]],
'aiflt-products' => ['optimizer' => ['products', '', []]],
'aiflt-abandoned-carts' => ['recovery' => ['carts', '', []]],
'aiflt-checkout' => ['optimizer' => ['checkout', '', []], 'fields' => ['checkout', '', []]],
'aiflt_settings' => ['pro' => ['licence', '', []]],
];
if (isset($moved_tabs[$page][$tab])) {
[$view, $new_tab, $extra] = $moved_tabs[$page][$tab];
$args = [];
foreach ((array) $_GET as $key => $value) {
if (!in_array($key, ['page', 'tab'], true) && is_scalar($value)) {
$args[sanitize_key($key)] = sanitize_text_field(wp_unslash($value));
}
}
wp_safe_redirect(add_query_arg($args + $extra, aiflt_admin_url($view, $new_tab)));
exit;
}
// The page it lands on checks permissions itself.
if (!isset($map[$page]) || !(current_user_can('manage_options') || current_user_can('manage_woocommerce'))) {
return;
}
[$view, $tab] = $map[$page];
if ($page === 'aiftas-segmentation') {
$_GET['compare'] = 'device';
}
$args = [];
foreach ((array) $_GET as $key => $value) {
if ($key !== 'page' && is_scalar($value)) {
$args[sanitize_key($key)] = sanitize_text_field(wp_unslash($value));
}
}
// The old Settings screen used its own "tab" values.
if ($page !== 'aiflt_settings') {
unset($args['tab']);
}
wp_safe_redirect(add_query_arg($args, aiflt_admin_url($view, $tab)));
exit;
}
/**
* Render callback for a Pro page or tab when Pro is not active.
*
* @param string $feature action-plan|alerts|summary
* @return callable
*/
private static function locked($feature) {
return static function () use ($feature) {
$features = [
'action-plan' => [
'list-checks',
__('One plan for the whole store', 'ai-flash-tune'),
__('Action Plan reads your funnel, carts, checkout, products and page speed together and gives you a short, ranked list of safe changes, each with how to measure it and how to undo it.', 'ai-flash-tune'),
],
'alerts' => [
'bell',
__('Know the moment sales stop', 'ai-flash-tune'),
__('Every hour, Pro compares orders, checkout, payments, visits and cart reminders with your store\'s own normal. You get one email when something breaks and one when it is back to normal.', 'ai-flash-tune'),
],
'summary' => [
'chart',
__('A store summary you can share', 'ai-flash-tune'),
__('Summary writes a short report for any period, with the biggest opportunity, risks and next steps. Agencies get a white-label version for clients, and a CSV export.', 'ai-flash-tune'),
],
];
[$icon, $title, $text] = $features[$feature];
echo '
' . esc_html__('A cart reminder won back an order for you. If AI Flash Tune helps your store, a short review on WordPress.org helps other store owners find it.', 'ai-flash-tune') . '
' . $button('rate', __('Leave a review', 'ai-flash-tune'), 'aiflt-button aiflt-button-sm', true) . $button('dismiss', __('No thanks', 'ai-flash-tune'), 'aiflt-button aiflt-button-quiet aiflt-button-sm', false) . '