get( 'hardening', \AiRankSuite\Modules\Hardening\HardeningModule::defaults() );
if ( ! empty( $hardening['hide_admin_bar'] ) ) {
return false;
}
/**
* Filter who sees the toolbar light.
*
* @param bool $visible Whether to render it.
*/
return (bool) apply_filters( 'airanksuite/toolbar/visible', current_user_can( 'manage_options' ) );
}
public static function render( WP_Admin_Bar $bar ): void {
if ( ! self::visible() ) {
return;
}
$post = self::current_post();
if ( null !== $post ) {
self::page_node( $bar, $post );
return;
}
$pulse = Pulse::read();
$root = admin_url( 'admin.php?page=ai-rank-suite' );
/*
* What the badge counts: the work waiting, not the alarms.
*
* Counting blockers alone left the badge empty on almost every site,
* because a blocker is the rare case where something is outright
* broken. Pages missing a description and failing checks are the
* numbers that are actually non-zero and actually worth a trip.
*/
$open = count( (array) $pulse['blockers'] )
+ (int) ( $pulse['missing'] ?? 0 )
+ (int) ( $pulse['failing'] ?? 0 );
$bar->add_node(
array(
'id' => 'airanksuite',
// A count beside the label, the way every other plugin's toolbar
// entry carries one: it is the number of things waiting, so the
// menu is worth opening before it is opened.
'title' => sprintf(
'%2$s%3$s',
esc_attr( (string) $pulse['level'] ),
esc_html( (string) $pulse['label'] ),
$open > 0
? sprintf(
'%s',
esc_html( $open > 99 ? '99+' : (string) $open )
)
: ''
),
'href' => $root,
'meta' => array( 'title' => __( 'AI Rank Suite', 'ai-rank-suite' ) ),
)
);
foreach ( (array) $pulse['blockers'] as $index => $blocker ) {
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-blocker-' . $index,
'title' => esc_html( (string) $blocker['text'] ),
'href' => $root . '#/' . ltrim( (string) $blocker['href'], '/' ),
)
);
}
foreach ( (array) $pulse['facts'] as $index => $fact ) {
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-fact-' . $index,
'title' => esc_html( (string) $fact['text'] ),
'href' => isset( $fact['href'] ) ? $root . '#/' . ltrim( (string) $fact['href'], '/' ) : '',
)
);
}
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-open',
'title' => __( 'Open the dashboard', 'ai-rank-suite' ),
'href' => $root,
)
);
self::jump_nodes( $bar, $root );
// The cache-purge entry used to live here too. It named another
// plugin's cache in our menu, which read as noise to anyone who never
// asked for it — and the plugin already purges on save. Purging by
// hand stays available where cache questions are asked and answered:
// Settings → Tools.
}
/**
* Direct jumps to the screens people actually go looking for.
*
* The menu told you what was wrong and offered one link to the dashboard,
* which means every routine trip — change a title template, check the
* sitemap, paste an analytics id — went through the dashboard first. These
* are the destinations, in the order they get used, so the toolbar becomes
* a way to work rather than only a status light.
*
* @param WP_Admin_Bar $bar The bar.
* @param string $root Admin URL of the plugin's page.
*/
private static function jump_nodes( WP_Admin_Bar $bar, string $root ): void {
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-jump',
'title' => __( 'Go to', 'ai-rank-suite' ),
'meta' => array( 'class' => 'airanksuite-bar-group' ),
)
);
/**
* Filters the toolbar's jump list.
*
* @param array $links
*/
$links = (array) apply_filters(
'airanksuite/toolbar/links',
array(
array( 'id' => 'content', 'label' => __( 'Content', 'ai-rank-suite' ), 'route' => 'content' ),
array( 'id' => 'audit', 'label' => __( 'Site Audit', 'ai-rank-suite' ), 'route' => 'audit' ),
array( 'id' => 'seo', 'label' => __( 'Titles & metas', 'ai-rank-suite' ), 'route' => 'seo/templates' ),
array( 'id' => 'social', 'label' => __( 'Social cards', 'ai-rank-suite' ), 'route' => 'seo/social' ),
array( 'id' => 'schema', 'label' => __( 'Schema', 'ai-rank-suite' ), 'route' => 'schema' ),
array( 'id' => 'sitemap', 'label' => __( 'Sitemap', 'ai-rank-suite' ), 'route' => 'sitemap' ),
array( 'id' => 'llms', 'label' => __( 'llms.txt', 'ai-rank-suite' ), 'route' => 'llms-txt' ),
array( 'id' => 'links', 'label' => __( 'Links & redirects', 'ai-rank-suite' ), 'route' => 'links' ),
array( 'id' => 'indexing', 'label' => __( 'Instant indexing', 'ai-rank-suite' ), 'route' => 'google/indexnow' ),
array( 'id' => 'analytics', 'label' => __( 'Analytics', 'ai-rank-suite' ), 'route' => 'analytics' ),
array( 'id' => 'autopilot', 'label' => __( 'AI Autopilot', 'ai-rank-suite' ), 'route' => 'autopilot' ),
array( 'id' => 'setup', 'label' => __( 'Setup', 'ai-rank-suite' ), 'route' => 'setup' ),
array( 'id' => 'wizard', 'label' => __( 'Configuration wizard', 'ai-rank-suite' ), 'route' => 'wizard' ),
)
);
foreach ( $links as $link ) {
if ( empty( $link['id'] ) || empty( $link['label'] ) ) {
continue;
}
$bar->add_node(
array(
'parent' => 'airanksuite-jump',
'id' => 'airanksuite-go-' . sanitize_key( (string) $link['id'] ),
'title' => esc_html( (string) $link['label'] ),
'href' => $root . '#/' . ltrim( (string) ( $link['route'] ?? '' ), '/' ),
)
);
}
}
/**
* The score node for one page.
*/
private static function page_node( WP_Admin_Bar $bar, \WP_Post $post ): void {
$geo = get_post_meta( $post->ID, '_airanksuite_geo_score', true );
$seo = get_post_meta( $post->ID, '_airanksuite_seo_score', true );
// A page nobody has analyzed has no score. Showing a zero would read as
// "this page scores nothing", which is a different and untrue claim.
$analyzed = '' !== (string) $geo;
$level = ! $analyzed ? 'idle' : ( (int) $geo >= 80 ? 'green' : ( (int) $geo >= 50 ? 'amber' : 'red' ) );
$bar->add_node(
array(
'id' => 'airanksuite',
'title' => sprintf(
'%2$s',
esc_attr( $level ),
$analyzed
/* translators: 1: AI score, 2: SEO score. */
? esc_html( sprintf( __( 'GEO %1$d · SEO %2$d', 'ai-rank-suite' ), (int) $geo, (int) $seo ) )
: esc_html__( 'Not analyzed', 'ai-rank-suite' )
),
'href' => admin_url( 'admin.php?page=ai-rank-suite#/content/' . $post->ID ),
'meta' => array( 'title' => __( 'AI Rank Suite — this page', 'ai-rank-suite' ) ),
)
);
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-page-open',
'title' => __( 'Optimize this page', 'ai-rank-suite' ),
'href' => admin_url( 'admin.php?page=ai-rank-suite#/content/' . $post->ID ),
)
);
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-page-head',
// The admin bar runs titles through wp_kses, which eats a literal
// and leaves the label ending in "puts in".
'title' => esc_html__( 'See what this page puts in ', 'ai-rank-suite' ),
'href' => admin_url( 'admin.php?page=ai-rank-suite#/content/' . $post->ID ),
)
);
self::jump_nodes( $bar, admin_url( 'admin.php?page=ai-rank-suite' ) );
$bar->add_node(
array(
'parent' => 'airanksuite',
'id' => 'airanksuite-page-site',
'title' => __( 'Site overview', 'ai-rank-suite' ),
'href' => admin_url( 'admin.php?page=ai-rank-suite' ),
)
);
}
/**
* The post this request is about, if any.
*
* @return \WP_Post|null
*/
private static function current_post(): ?\WP_Post {
if ( ! is_admin() && is_singular() ) {
$post = get_queried_object();
return $post instanceof \WP_Post ? $post : null;
}
if ( is_admin() && function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
if ( $screen instanceof \WP_Screen && 'post' === $screen->base ) {
$post = get_post();
return $post instanceof \WP_Post && 'auto-draft' !== $post->post_status ? $post : null;
}
}
return null;
}
/**
* The light's own styles and its poll loop.
*
* Inline rather than a file: it is under 2KB, it has to be present on the
* front end too, and one more HTTP request on every page an editor loads is
* a worse trade than the bytes.
*/
public static function assets(): void {
if ( ! self::visible() ) {
return;
}
$css = '
#wpadminbar .airanksuite-bar{display:inline-flex;align-items:center;gap:6px;height:100%}
#wpadminbar .airanksuite-bar-dot{width:9px;height:9px;border-radius:50%;background:#8c8f94;flex:none;transition:background 240ms ease,box-shadow 240ms ease}
#wpadminbar .airanksuite-bar.is-green .airanksuite-bar-dot{background:#46d07f;box-shadow:0 0 0 3px rgba(70,208,127,.18)}
#wpadminbar .airanksuite-bar.is-amber .airanksuite-bar-dot{background:#ffc23d;box-shadow:0 0 0 3px rgba(255,194,61,.2)}
#wpadminbar .airanksuite-bar.is-red .airanksuite-bar-dot{background:#f2545b;box-shadow:0 0 0 3px rgba(242,84,91,.2)}
#wpadminbar .airanksuite-bar.is-working .airanksuite-bar-dot{background:#6b46e5;animation:airanksuite-bar-pulse 1.15s ease-in-out infinite}
#wpadminbar .airanksuite-bar.is-idle .airanksuite-bar-dot{background:#6c7181}
@keyframes airanksuite-bar-pulse{0%,100%{box-shadow:0 0 0 3px rgba(139,92,240,.22)}50%{box-shadow:0 0 0 8px rgba(139,92,240,.03)}}
@media (prefers-reduced-motion:reduce){#wpadminbar .airanksuite-bar .airanksuite-bar-dot{animation:none}}
@media screen and (max-width:782px){#wpadminbar .airanksuite-bar-text{display:none}}
/* The count. Kept on the small screens where the label is hidden — a number
still reads at that size, and it is the part that says "look at me". */
#wpadminbar .airanksuite-bar-count{display:inline-flex;align-items:center;justify-content:center;min-width:18px;height:18px;padding:0 5px;border-radius:9px;background:#f2545b;color:#fff;font-size:11px;font-weight:700;line-height:1;flex:none}
#wpadminbar .airanksuite-bar.is-green .airanksuite-bar-count{background:#46d07f;color:#04321a}
#wpadminbar .airanksuite-bar.is-amber .airanksuite-bar-count{background:#ffc23d;color:#3d2c00}
/* The jump list is a section header, not a link. */
#wpadminbar #wp-admin-bar-airanksuite-jump > .ab-item{font-weight:600;opacity:.62;text-transform:uppercase;font-size:10.5px;letter-spacing:.06em;pointer-events:none}
';
wp_register_style( 'airanksuite-toolbar', false, array(), AIRANKSUITE_VERSION ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_style( 'airanksuite-toolbar' );
wp_add_inline_style( 'airanksuite-toolbar', $css );
// The two runtime values reach the script as data, encoded as JSON: that
// is the escaping for a JavaScript context. They used to be substituted
// into quoted strings inside the script, which escaped the URL as a URL
// and not as the JavaScript literal it was sitting in.
$config = array(
'endpoint' => esc_url_raw( rest_url( 'airanksuite/v1/pulse' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
);
$js = <<<'JS'
(function () {
var config = window.airanksuiteToolbar || {};
var node = document.querySelector('#wp-admin-bar-airanksuite .airanksuite-bar');
if (!node || !config.endpoint) return;
var text = node.querySelector('.airanksuite-bar-text');
var tries = 0;
function apply(data) {
node.className = 'airanksuite-bar is-' + data.level;
if (text) {
text.textContent = data.running && data.progress
? data.progress.done + '/' + data.progress.total
: data.label;
}
return data.running;
}
function poll() {
fetch(config.endpoint + '?fresh=1', {
headers: { 'X-WP-Nonce': config.nonce },
credentials: 'same-origin'
})
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (data) {
if (!data) { return; }
tries = 0;
// Only keep asking while there is something to watch. A toolbar
// that polls forever on every tab an editor leaves open is a
// plugin that gets uninstalled.
if (apply(data)) { setTimeout(poll, 4000); }
})
.catch(function () {
// Three failures and it gives up until the next page load
// rather than hammering a server that is already unhappy.
if (++tries < 3) { setTimeout(poll, 8000); }
});
}
if (node.classList.contains('is-working')) { poll(); }
// The dashboard tells the toolbar when it starts a run, so the light comes
// on immediately instead of on the next page load.
window.addEventListener('airanksuite:working', function () {
node.className = 'airanksuite-bar is-working';
poll();
});
})();
JS;
wp_register_script( 'airanksuite-toolbar', false, array(), AIRANKSUITE_VERSION, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_script( 'airanksuite-toolbar' );
wp_add_inline_script( 'airanksuite-toolbar', 'window.airanksuiteToolbar = ' . wp_json_encode( $config ) . ';', 'before' );
wp_add_inline_script( 'airanksuite-toolbar', $js );
}
}