] * : Which images to process. * --- * default: missing * options: * - missing * - bad * - all * --- * * [--limit=] * : Stop after this many images. 0 processes everything. * * [--dry-run] * : Print what would be generated without saving anything. * * ## EXAMPLES * * wp ai-seo alt --scope=missing --limit=500 * wp ai-seo alt --scope=bad --dry-run * * @param array $args Positional arguments. * @param array $assoc_args Flags. */ public function alt( array $args, array $assoc_args ): void { $scope = $assoc_args['scope'] ?? 'missing'; $limit = (int) ( $assoc_args['limit'] ?? 0 ); $dry_run = isset( $assoc_args['dry-run'] ); $ids = 'bad' === $scope ? AI_SEO_Tools_Alt_Text::find_low_quality( $limit > 0 ? $limit : 5000 ) : AI_SEO_Tools_Alt_Text::find_missing( $limit ); if ( 'all' === $scope ) { $ids = get_posts( array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'fields' => 'ids', 'posts_per_page' => $limit > 0 ? $limit : -1, ) ); } if ( $limit > 0 ) { $ids = array_slice( $ids, 0, $limit ); } $module = new AI_SEO_Tools_Alt_Text(); $this->run( $ids, __( 'Generating alt text', 'ai-seo-tools' ), static function ( int $id ) use ( $module, $scope, $dry_run ) { return $module->generate( $id, array( 'scope' => $scope, 'dry_run' => $dry_run ) ); } ); } /** * Generates tags for published content. * * ## OPTIONS * * [--scope=] * : untagged (default) or all. * * [--mode=] * : append (default) or replace. * * [--limit=] * : Stop after this many posts. * * [--dry-run] * : Print suggestions without saving. * * ## EXAMPLES * * wp ai-seo tags --scope=untagged --limit=200 */ public function tags( array $args, array $assoc_args ): void { $scope = $assoc_args['scope'] ?? 'untagged'; $mode = $assoc_args['mode'] ?? 'append'; $limit = (int) ( $assoc_args['limit'] ?? 0 ); $dry_run = isset( $assoc_args['dry-run'] ); $ids = 'all' === $scope ? AI_SEO_Tools_Auto_Tagging::find_all( $limit ) : AI_SEO_Tools_Auto_Tagging::find_untagged( $limit ); $module = new AI_SEO_Tools_Auto_Tagging(); $this->run( $ids, __( 'Generating tags', 'ai-seo-tools' ), static function ( int $id ) use ( $module, $mode, $dry_run ) { return $module->generate( $id, array( 'mode' => $mode, 'dry_run' => $dry_run ) ); } ); } /** * Generates SEO titles and meta descriptions. * * ## OPTIONS * * [--scope=] * : missing (default) or all. * * [--limit=] * : Stop after this many posts. * * [--dry-run] * : Print suggestions without saving. */ public function meta( array $args, array $assoc_args ): void { $scope = $assoc_args['scope'] ?? 'missing'; $limit = (int) ( $assoc_args['limit'] ?? 0 ); $dry_run = isset( $assoc_args['dry-run'] ); $ids = 'all' === $scope ? AI_SEO_Tools_Meta::published_ids() : AI_SEO_Tools_Meta::find_missing( $limit ); if ( $limit > 0 ) { $ids = array_slice( $ids, 0, $limit ); } $module = new AI_SEO_Tools_Meta(); $this->run( $ids, __( 'Writing snippets', 'ai-seo-tools' ), static function ( int $id ) use ( $module, $scope, $dry_run ) { return $module->generate( $id, array( 'scope' => $scope, 'dry_run' => $dry_run ) ); } ); } /** * Generates Schema.org structured data. * * ## OPTIONS * * [--scope=] * : missing (default) or all. * * [--limit=] * : Stop after this many posts. */ public function schema( array $args, array $assoc_args ): void { $scope = $assoc_args['scope'] ?? 'missing'; $limit = (int) ( $assoc_args['limit'] ?? 0 ); $ids = 'all' === $scope ? AI_SEO_Tools_Meta::published_ids() : AI_SEO_Tools_Schema::find_missing( $limit ); if ( $limit > 0 ) { $ids = array_slice( $ids, 0, $limit ); } $module = new AI_SEO_Tools_Schema(); $this->run( $ids, __( 'Building structured data', 'ai-seo-tools' ), static function ( int $id ) use ( $module, $scope ) { return $module->generate( $id, array( 'scope' => $scope ) ); } ); } /** * Drafts posts from topics. * * Every post is created as a draft; nothing is published. * * ## OPTIONS * * [...] * : One or more topics. Omit to read them from --file or STDIN. * * [--file=] * : Read topics from a file, one per line. * * [--post-type=] * : Post type to create. Defaults to post. * * [--length=] * : short, medium (default) or long. * * [--keyword=] * : Focus keyword to work in. * * [--tone=] * : Tone of voice, e.g. "plain and practical". * * [--language=] * : Language to write in. Defaults to matching the topic. * * [--skip-tags] * : Do not generate tags. * * [--skip-meta] * : Do not generate the SEO title and meta description. * * [--publish] * : Publish straight away instead of saving as a draft. * * ## EXAMPLES * * wp ai-seo write "How to sharpen a chisel" --length=short * wp ai-seo write --file=topics.txt --post-type=post * * @param array $args Topics. * @param array $assoc_args Flags. */ public function write( array $args, array $assoc_args ): void { $topics = $args; if ( ! empty( $assoc_args['file'] ) ) { $path = (string) $assoc_args['file']; if ( ! is_readable( $path ) ) { WP_CLI::error( sprintf( 'Cannot read %s', $path ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- CLI input. $topics = array_merge( $topics, preg_split( '/\r\n|\r|\n/', (string) file_get_contents( $path ) ) ?: array() ); } $topics = array_values( array_filter( array_map( 'trim', $topics ) ) ); if ( ! $topics ) { WP_CLI::error( 'Give it at least one topic, or point --file at a list.' ); } $post_type = $assoc_args['post-type'] ?? 'post'; $options = array( 'post_type' => $post_type, 'status' => AI_SEO_Tools_Post_Generator::resolve_status( isset( $assoc_args['publish'] ), $post_type ), 'length' => $assoc_args['length'] ?? 'medium', 'keyword' => $assoc_args['keyword'] ?? '', 'tone' => $assoc_args['tone'] ?? '', 'language' => $assoc_args['language'] ?? '', 'with_tags' => ! isset( $assoc_args['skip-tags'] ), 'with_meta' => ! isset( $assoc_args['skip-meta'] ), 'author' => get_current_user_id(), ); // WP-CLI usually runs with no signed-in user, so the capability check would // demote --publish to pending. On the command line the operator already has // shell access; take the flag at face value unless --user was given. if ( isset( $assoc_args['publish'] ) && ! get_current_user_id() ) { $options['status'] = 'publish'; } $module = new AI_SEO_Tools_Post_Generator(); if ( ! AI_SEO_Tools_Client::is_ready() ) { WP_CLI::error( __( 'No API key is configured.', 'ai-seo-tools' ) ); } $progress = WP_CLI\Utils\make_progress_bar( __( 'Writing drafts', 'ai-seo-tools' ), count( $topics ) ); $rows = array(); foreach ( $topics as $topic ) { $result = $module->generate( $topic, $options ); $rows[] = is_wp_error( $result ) ? array( 'id' => '-', 'status' => 'failed', 'words' => '-', 'result' => $result->get_error_message() ) : array( 'id' => $result['post_id'], 'status' => $result['status'], 'words' => $result['words'], 'result' => $result['title'] ); $progress->tick(); } $progress->finish(); WP_CLI\Utils\format_items( 'table', $rows, array( 'id', 'status', 'words', 'result' ) ); WP_CLI::success( sprintf( /* translators: %s: estimated spend. */ __( 'Done. Estimated spend this month: $%s', 'ai-seo-tools' ), AI_SEO_Tools_Log::format_cost( AI_SEO_Tools_Log::month_spend() ) ) ); } /** * Shows estimated spend and call counts. * * ## OPTIONS * * [--since=] * : Start of the reporting window, as YYYY-MM-DD. Defaults to this month. * * ## EXAMPLES * * wp ai-seo usage --since=2026-01-01 */ public function usage( array $args, array $assoc_args ): void { $since = isset( $assoc_args['since'] ) ? gmdate( 'Y-m-d 00:00:00', strtotime( (string) $assoc_args['since'] ) ) : gmdate( 'Y-m-01 00:00:00' ); $totals = AI_SEO_Tools_Log::totals( $since ); $rows = AI_SEO_Tools_Log::by_module( $since ); WP_CLI::log( sprintf( 'Since %s: %d calls, %s input + %s output tokens, ~$%s estimated.', $since, $totals['calls'], number_format_i18n( $totals['input_tokens'] ), number_format_i18n( $totals['output_tokens'] ), AI_SEO_Tools_Log::format_cost( $totals['cost'] ) ) ); if ( $rows ) { WP_CLI\Utils\format_items( 'table', $rows, array( 'module', 'calls', 'input_tokens', 'output_tokens', 'cost' ) ); } } /** * Reports on queue state, and can clear stuck runs. * * ## OPTIONS * * [--stop] * : Stop every running queue. * * @subcommand queue */ public function queue( array $args, array $assoc_args ): void { $rows = array(); foreach ( AI_SEO_Tools_Queue::registered() as $slug ) { if ( isset( $assoc_args['stop'] ) ) { AI_SEO_Tools_Queue::stop( $slug ); } $state = AI_SEO_Tools_Queue::status( $slug ); $rows[] = array( 'queue' => $slug, 'status' => $state['status'], 'processed' => $state['processed'], 'failed' => $state['failed'], 'skipped' => $state['skipped'], 'remaining' => $state['remaining'], ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'queue', 'status', 'processed', 'failed', 'skipped', 'remaining' ) ); } /** * Shared runner with a progress bar and a summary. */ private function run( array $ids, string $label, callable $worker ): void { $ids = array_values( array_map( 'intval', $ids ) ); if ( ! $ids ) { WP_CLI::success( __( 'Nothing to do.', 'ai-seo-tools' ) ); return; } if ( ! AI_SEO_Tools_Client::is_ready() ) { WP_CLI::error( __( 'No API key is configured.', 'ai-seo-tools' ) ); } $progress = WP_CLI\Utils\make_progress_bar( $label, count( $ids ) ); $done = 0; $skipped = 0; $failed = 0; foreach ( $ids as $id ) { $result = $worker( $id ); if ( is_wp_error( $result ) ) { $failed++; WP_CLI::debug( sprintf( '#%d: %s', $id, $result->get_error_message() ) ); if ( 'budget_exceeded' === $result->get_error_code() ) { $progress->finish(); WP_CLI::error( $result->get_error_message() ); } } elseif ( 'skipped' === $result ) { $skipped++; } else { $done++; } $progress->tick(); } $progress->finish(); WP_CLI::success( sprintf( /* translators: 1: processed count, 2: skipped count, 3: failed count, 4: estimated cost. */ __( '%1$d done, %2$d skipped, %3$d failed. Estimated spend this month: $%4$s', 'ai-seo-tools' ), $done, $skipped, $failed, AI_SEO_Tools_Log::format_cost( AI_SEO_Tools_Log::month_spend() ) ) ); } } WP_CLI::add_command( 'ai-seo', 'AI_SEO_Tools_CLI' ); }