`. * * @package aeoora */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Manage aeoora from the command line. */ class Aeoora_Cli { /** * Register. */ public static function register() { if ( class_exists( 'WP_CLI' ) ) { WP_CLI::add_command( 'aeoora', __CLASS__ ); } } /** * Show AI crawler and referral stats. * * ## OPTIONS * * [--days=] * : Period. Default 30. * * [--scope=] * : ai, all or a category. Default ai. * * @param array $args Args. * @param array $assoc Assoc args. */ public function stats( $args, $assoc ) { $o = Aeoora_Stats::overview( isset( $assoc['days'] ) ? (int) $assoc['days'] : 30, isset( $assoc['scope'] ) ? $assoc['scope'] : 'ai' ); WP_CLI::log( sprintf( 'Hits: %d · Bots: %d · Pages: %d · AI referrals: %d', $o['kpis']['hits'], $o['kpis']['bots'], $o['kpis']['pages'], $o['kpis']['referrals'] ) ); if ( $o['top_bots'] ) { WP_CLI\Utils\format_items( 'table', $o['top_bots'], array( 'name', 'operator', 'category', 'hits', 'policy' ) ); } if ( $o['referrers'] ) { WP_CLI\Utils\format_items( 'table', $o['referrers'], array( 'label', 'vendor', 'hits' ) ); } } /** * Regenerate llms.txt and llms-full.txt. * * @subcommand llms */ public function llms() { $s = Aeoora_Llms::regenerate(); if ( ! empty( $s['busy'] ) ) { WP_CLI::error( 'Another llms.txt build is running. Try again in a few minutes.' ); } $s = wp_parse_args( $s, array( 'llms_bytes' => 0, 'full_bytes' => 0, 'items' => 0, ) ); WP_CLI::success( sprintf( 'llms.txt %d bytes, llms-full.txt %d bytes, %d items.', $s['llms_bytes'], $s['full_bytes'], $s['items'] ) ); } /** * Score posts for AEO. * * ## OPTIONS * * [--post=] * : One post. * * [--limit=] * : Number of posts. Default 100. * * [--site] * : Run the technical site audit instead. * * @param array $args Args. * @param array $assoc Assoc args. */ public function audit( $args, $assoc ) { if ( isset( $assoc['site'] ) ) { $r = Aeoora_Audit::site( true ); if ( is_wp_error( $r ) ) { WP_CLI::error( $r->get_error_message() ); } WP_CLI::log( sprintf( 'Site AEO score: %d (%s)', $r['score'], $r['grade'] ) ); WP_CLI\Utils\format_items( 'table', $r['checks'], array( 'label', 'status', 'points', 'max', 'message' ) ); return; } $ids = isset( $assoc['post'] ) ? array( (int) $assoc['post'] ) : get_posts( array( 'post_type' => Aeoora_Audit::post_types(), 'post_status' => 'publish', 'posts_per_page' => isset( $assoc['limit'] ) ? (int) $assoc['limit'] : -1, // Every page unless --limit is given. 'fields' => 'ids', ) ); $rows = array(); foreach ( $ids as $id ) { $r = Aeoora_Audit::score_post( $id ); $rows[] = array( 'id' => $id, 'title' => get_the_title( $id ), 'score' => $r['score'], 'grade' => $r['grade'], ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'id', 'title', 'score', 'grade' ) ); } /** * Run the daily AI suggestions now. * * [--posts=] * : Number of posts. Default from settings. * * @param array $args Args. * @param array $assoc Assoc args. */ public function suggest( $args, $assoc ) { $r = Aeoora_Suggestions::run_daily( isset( $assoc['posts'] ) ? (int) $assoc['posts'] : ( 'daily' !== Aeoora_Settings::get( 'suggestions', 'scope', 'all' ) ? 100000 : (int) Aeoora_Settings::get( 'suggestions', 'per_day', 10 ) ) ); if ( ! empty( $r['busy'] ) ) { WP_CLI::error( 'Suggestions are already being prepared. Try again in a few minutes.' ); } WP_CLI::success( sprintf( '%d posts reviewed, %d suggestions created.', $r['posts'], isset( $r['created'] ) ? $r['created'] : 0 ) ); } /** * Detect the bot for a user agent string. * * * : User-Agent. * * @param array $args Args. */ public function detect( $args ) { $bot = Aeoora_Bots::detect( $args[0] ); if ( ! $bot ) { WP_CLI::log( 'No bot detected (looks like a human browser).' ); return; } WP_CLI::log( sprintf( '%s · %s · %s · policy: %s', $bot['name'], $bot['operator'], $bot['category'], Aeoora_Bots::policy( $bot['id'] ) ) ); } }