send_get_request('chatbots');
$chatbots = isset($chatbots_data['data']) && is_array($chatbots_data['data']) ? $chatbots_data['data'] : [];
$total_chatbots = isset($chatbots_data['total']) ? $chatbots_data['total'] : 0;
$total_pages = ceil($total_chatbots / $items_per_page);
$nonce = wp_create_nonce('ai_botkit_chatbots');
// Only when the SaaS answered and said "zero". A failed request also leaves $chatbots empty, and
// telling that owner they have no chatbot would be wrong.
$has_no_chatbots = ! empty( $chatbots_data['success'] ) && empty( $chatbots );
$chatbots_failed = empty( $chatbots_data['success'] ) && empty( $chatbots );
// One link for every create. The SaaS decides at click time: an owner who has not been through
// first-run goes to /welcome (profile questions + first bot); everyone else gets the creation
// modal on My agents. Deciding there, not here, keeps a tab opened before the first bot existed
// from sending the owner back through the questions.
$create_chatbot_url = $ai_botkit_url . '/dashboard/my-bots?create=1';
/*
* THE AGENT CARD AND THE DEPLOY DIALOG (plan U13). The card follows the lab prototype's AgentCard
* (wordpress-simple-onboarding/_components/Dashboard.tsx, `activeStatus`); its pill is the chat
* widget switch only, labelled "Chat widget" (R1), never AI BotKit's `live`. The dialog itself is
* drawn by admin/js/deploy-dialog.js from the JSON printed below.
*/
$abk_lower = function ($text) {
return function_exists('mb_strtolower') ? mb_strtolower((string) $text, 'UTF-8') : strtolower((string) $text);
};
// Lucide icons (lucide.dev, ISC). Fixed markup, no data in it.
$abk_icons = [
'pencil' => '',
'external' => '',
'rocket' => '',
'message' => '',
'eye' => '',
'x' => '',
'rotate' => '',
'loader' => '',
];
$abk_icon = function ($name, $class = '') use ($abk_icons) {
return '';
};
// Public post types the rules can target: pages, posts and products first, as in the prototype.
$abk_post_types = [];
foreach ((array) get_post_types(['public' => true], 'objects') as $abk_name => $abk_type) {
$abk_name = (string) $abk_name;
if (in_array($abk_name, Ajax_Handler::EXCLUDED_POST_TYPES, true)) {
continue;
}
$abk_plural = isset($abk_type->label) ? (string) $abk_type->label : $abk_name;
$abk_singular = isset($abk_type->labels->singular_name) ? (string) $abk_type->labels->singular_name : $abk_plural;
$abk_post_types[] = [
'name' => $abk_name,
'plural' => $abk_lower($abk_plural),
'singular' => $abk_lower($abk_singular),
];
}
$abk_first = ['page', 'post', 'product'];
usort($abk_post_types, function ($a, $b) use ($abk_first) {
$ra = array_search($a['name'], $abk_first, true);
$rb = array_search($b['name'], $abk_first, true);
$ra = false === $ra ? 99 : $ra;
$rb = false === $rb ? 99 : $rb;
return $ra === $rb ? strcmp($a['plural'], $b['plural']) : $ra - $rb;
});
// Each agent's switch (from the list AI BotKit just sent) and placement on this site.
$abk_agents = [];
$abk_item_ids = [];
foreach ($chatbots as $abk_row) {
if (!is_array($abk_row)) {
continue;
}
$abk_status = API_Handler::agent_status($abk_row);
if ('' === $abk_status['namespace']) {
continue;
}
$abk_known = array_key_exists('widget_enabled', $abk_row) || array_key_exists('active', $abk_row);
$abk_placement = Placement_Store::get($abk_status['namespace']);
$abk_agents[$abk_status['namespace']] = [
'id' => $abk_status['id'],
'namespace' => $abk_status['namespace'],
'name' => $abk_status['name'],
'widget_enabled' => $abk_known ? $abk_status['widget_enabled'] : null,
'deployed' => null !== $abk_placement,
'placement' => $abk_placement,
];
if (null !== $abk_placement) {
$abk_item_ids = array_merge($abk_item_ids, $abk_placement['show']['items'], $abk_placement['hide']['items']);
}
}
// Titles for the items the saved rules name, so the dialog can show them.
$abk_items = [];
foreach (array_unique($abk_item_ids) as $abk_id) {
$abk_post = get_post((int) $abk_id);
if (!$abk_post) {
continue;
}
$abk_title = trim((string) $abk_post->post_title);
$abk_items[(string) (int) $abk_id] = [
'title' => '' === $abk_title ? __('(no title)', 'ai-botkit-for-lead-generation') : $abk_title,
'type' => (string) $abk_post->post_type,
];
}
$abk_site = (string) wp_parse_url(home_url(), PHP_URL_HOST);
$abk_strings = [
'entireSite' => __('Entire site', 'ai-botkit-for-lead-generation'),
'homepage' => __('Homepage', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case, e.g. "pages" */
'allType' => __('All %s', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'specificType' => __('Specific %s', 'ai-botkit-for-lead-generation'),
'coversSite' => __('Every page, post and product, including new ones', 'ai-botkit-for-lead-generation'),
'coversHome' => __('Your front page only', 'ai-botkit-for-lead-generation'),
/* translators: a post type singular, lower case */
'coversType' => __('Every %s, including new ones', 'ai-botkit-for-lead-generation'),
/* translators: a post type singular, lower case */
'oneItem' => __('1 %s', 'ai-botkit-for-lead-generation'),
/* translators: 1: a number, 2: a post type plural, lower case */
'manyItems' => __('%1$d %2$s', 'ai-botkit-for-lead-generation'),
'whereShortcode' => __('only where you paste the shortcode', 'ai-botkit-for-lead-generation'),
'whereSite' => __('the entire site', 'ai-botkit-for-lead-generation'),
/* translators: where the agent shows, e.g. "the entire site" */
'whereOn' => __('on %s', 'ai-botkit-for-lead-generation'),
/* translators: 1: where it shows, 2: where it is hidden */
'whereExcept' => __('%1$s · except %2$s', 'ai-botkit-for-lead-generation'),
'active' => __('Active', 'ai-botkit-for-lead-generation'),
'inactive' => __('Inactive', 'ai-botkit-for-lead-generation'),
/* translators: the site domain */
'stripNotYetHead' => __('Inactive · not on %s yet', 'ai-botkit-for-lead-generation'),
'stripNotYetLine' => __('Choose where it shows below, then Deploy.', 'ai-botkit-for-lead-generation'),
/* translators: where it shows, e.g. "on the entire site" */
'stripActiveLine' => __('Shows %s.', 'ai-botkit-for-lead-generation'),
'stripOffLine' => __('The chatbot is hidden on your websites, including shortcodes. Your choices below stay saved.', 'ai-botkit-for-lead-generation'),
'stripLoadingHead' => __('Checking status…', 'ai-botkit-for-lead-generation'),
'stripLoadingLine' => __('Getting the chat widget’s status from AI BotKit.', 'ai-botkit-for-lead-generation'),
'stripErrorHead' => __('Status unknown', 'ai-botkit-for-lead-generation'),
'stripErrorLine' => __('We can’t check the status right now.', 'ai-botkit-for-lead-generation'),
'retry' => __('Retry', 'ai-botkit-for-lead-generation'),
'noteFinish' => __('Finish the rules above to save.', 'ai-botkit-for-lead-generation'),
'noteUnsaved' => __('Unsaved changes', 'ai-botkit-for-lead-generation'),
'deploy' => __('Deploy', 'ai-botkit-for-lead-generation'),
'saveChanges' => __('Save changes', 'ai-botkit-for-lead-generation'),
'deploying' => __('Deploying…', 'ai-botkit-for-lead-generation'),
'saving' => __('Saving…', 'ai-botkit-for-lead-generation'),
'close' => __('Close', 'ai-botkit-for-lead-generation'),
'cancel' => __('Cancel', 'ai-botkit-for-lead-generation'),
/* translators: 1: page number, 2: number of pages */
'pageOf' => __('Page %1$d of %2$d', 'ai-botkit-for-lead-generation'),
'errSession' => __('Your session has expired. Reload the page and try again.', 'ai-botkit-for-lead-generation'),
'errUnreachable' => __('We couldn’t reach your WordPress site. Check your connection and try again.', 'ai-botkit-for-lead-generation'),
'errGeneric' => __('Something went wrong. Please try again.', 'ai-botkit-for-lead-generation'),
/* translators: Active, Inactive or Status unknown */
'statusUnknown' => __('Status unknown', 'ai-botkit-for-lead-generation'),
'dialogTitle' => __('Deploy to your site', 'ai-botkit-for-lead-generation'),
'dialogClose' => __('Close', 'ai-botkit-for-lead-generation'),
/* translators: 1: agent name, 2: site domain, 3: active or inactive */
'switchLabel' => __('%1$s on %2$s: %3$s', 'ai-botkit-for-lead-generation'),
'switchActive' => __('active', 'ai-botkit-for-lead-generation'),
'switchInactive' => __('inactive', 'ai-botkit-for-lead-generation'),
'putTitle' => __('Put it on your site', 'ai-botkit-for-lead-generation'),
'putLine' => __('Choose where your agent shows.', 'ai-botkit-for-lead-generation'),
'shortcode' => __('Shortcode', 'ai-botkit-for-lead-generation'),
'shortcodeLine' => __('Paste it into any page, post or widget to show the agent there too.', 'ai-botkit-for-lead-generation'),
'copy' => __('Copy', 'ai-botkit-for-lead-generation'),
'copied' => __('Copied', 'ai-botkit-for-lead-generation'),
'copyFailed' => __('Couldn’t copy. Select the shortcode and copy it yourself.', 'ai-botkit-for-lead-generation'),
'showOn' => __('Show on', 'ai-botkit-for-lead-generation'),
'showOnHint' => __('Any of these', 'ai-botkit-for-lead-generation'),
'addRule' => __('Add rule', 'ai-botkit-for-lead-generation'),
'hideOn' => __('Hide on', 'ai-botkit-for-lead-generation'),
'hideOnHint' => __('Optional · hiding wins over showing', 'ai-botkit-for-lead-generation'),
'addException' => __('Add exception', 'ai-botkit-for-lead-generation'),
'wholeSections' => __('Whole sections', 'ai-botkit-for-lead-generation'),
'oneByOne' => __('Pick one by one', 'ai-botkit-for-lead-generation'),
/* translators: Show on or Hide on */
'conditionLabel' => __('%s: condition', 'ai-botkit-for-lead-generation'),
/* translators: show on or hide on, lower case */
'removeRule' => __('Remove this %s rule', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'chooseItems' => __('Choose %s…', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'searchItems' => __('Search %s…', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'searchItemsLabel' => __('Search %s', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'chooseItemsGroup' => __('Choose %s', 'ai-botkit-for-lead-generation'),
/* translators: a post type singular, lower case */
'chosenOne' => __('1 %s chosen', 'ai-botkit-for-lead-generation'),
/* translators: 1: a number, 2: a post type plural, lower case */
'chosenMany' => __('%1$d %2$s chosen', 'ai-botkit-for-lead-generation'),
/* translators: 1: a post type plural, lower case, 2: the search text */
'noMatches' => __('No %1$s match “%2$s”.', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'noneYet' => __('No published %s yet.', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'loadingItems' => __('Loading %s…', 'ai-botkit-for-lead-generation'),
/* translators: a post type plural, lower case */
'loadFailed' => __('We couldn’t load your %s.', 'ai-botkit-for-lead-generation'),
'previous' => __('Previous', 'ai-botkit-for-lead-generation'),
'next' => __('Next', 'ai-botkit-for-lead-generation'),
'done' => __('Done', 'ai-botkit-for-lead-generation'),
/* translators: a page or post title */
/* translators: the most items one list can hold */
'capReached' => __('You’ve picked %d items, the most one list can hold. Remove one to add another.', 'ai-botkit-for-lead-generation'),
/* translators: a post type singular, lower case */
'mustChoose' => __('Choose at least one %s, or remove this rule.', 'ai-botkit-for-lead-generation'),
'discardTitle' => __('Discard your changes?', 'ai-botkit-for-lead-generation'),
/* translators: agent name */
'discardDeployed' => __('%s keeps showing where it does now.', 'ai-botkit-for-lead-generation'),
'discardNew' => __('Nothing has been deployed — your choices here will be lost.', 'ai-botkit-for-lead-generation'),
'keepEditing' => __('Keep editing', 'ai-botkit-for-lead-generation'),
'discard' => __('Discard', 'ai-botkit-for-lead-generation'),
];
$abk_data = [
'site' => $abk_site,
'maxItems' => Ajax_Handler::MAX_RULE_ITEMS,
'postTypes' => $abk_post_types,
'items' => (object) $abk_items,
'agents' => (object) $abk_agents,
'i18n' => $abk_strings,
];
/*
* PREVIEW (plan U15, R12, R13, KTD10). A card gets Preview only when AI BotKit says it can serve the
* agent's preview page (`previewAvailable: true`); an older AI BotKit omits the field. The panel below
* is drawn closed, with no frame: admin/js/agent-preview.js makes the frame on the first click, with
* `{$ai_botkit_url}/chatbot/{namespace}/preview` as its src. No AJAX and no token (R15).
*/
$abk_can_preview = function ($row) {
return is_array($row) && isset($row['previewAvailable']) && true === $row['previewAvailable'];
};
$abk_any_preview = false;
foreach ($chatbots as $abk_row) {
if ($abk_can_preview($abk_row) && isset($abk_agents[API_Handler::agent_status($abk_row)['namespace']])) {
$abk_any_preview = true;
break;
}
}
$abk_preview_data = [
'origin' => rtrim((string) $ai_botkit_url, '/'),
'i18n' => [
/* translators: %s: the agent's name */
'panelLabel' => __('Preview of %s', 'ai-botkit-for-lead-generation'),
/* translators: %s: the agent's name */
'closeLabel' => __('Close the preview of %s', 'ai-botkit-for-lead-generation'),
/* translators: %s: the agent's name */
'frameTitle' => __('Chat with %s', 'ai-botkit-for-lead-generation'),
],
];
$abk_pill = function ($widget_enabled) {
if (true === $widget_enabled) {
return ['active', __('Active', 'ai-botkit-for-lead-generation')];
}
if (false === $widget_enabled) {
return ['inactive', __('Inactive', 'ai-botkit-for-lead-generation')];
}
return ['unknown', __('Status unknown', 'ai-botkit-for-lead-generation')];
};
?>