Commit inicial powerservice
This commit is contained in:
+263
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/create-ai-visibility-brand
|
||||
*
|
||||
* @since 1.0.273
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\AI_Visibility
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\AI_Visibility;
|
||||
|
||||
use WP_REST_Request;
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
use RankMath\AI_Visibility\Platforms;
|
||||
use RankMath\AI_Visibility\Api\Brands_Controller;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/create-ai-visibility-brand ability.
|
||||
*/
|
||||
class Create_AI_Visibility_Brand implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Brands REST controller instance.
|
||||
*
|
||||
* @var Brands_Controller
|
||||
*/
|
||||
private $controller;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
* @param Brands_Controller|null $controller Controller instance.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta, ?Brands_Controller $controller = null ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
$this->controller = $controller ?? new Brands_Controller();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/create-ai-visibility-brand',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Create AI Visibility brand', 'seo-by-rank-math' ),
|
||||
'description' => sprintf(
|
||||
/* translators: %s: supported platform IDs. */
|
||||
esc_html__(
|
||||
'Creates a new brand for AI Visibility monitoring, triggering an initial analysis automatically. Ask which AI platforms to track. Supported platforms: %s. Selecting more than one requires the Expert plan; other plans may choose only one.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
Platforms::supported_as_string()
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'name', 'url', 'language', 'platforms' ],
|
||||
'properties' => [
|
||||
'name' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Brand name.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'description' => esc_html__( 'Brand website URL.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'description' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Short description of the brand or product.', 'seo-by-rank-math' ),
|
||||
'default' => '',
|
||||
],
|
||||
'locale' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'ISO 3166-1 alpha-2 country code (e.g. "US", "HU"). Defaults to no country filter.', 'seo-by-rank-math' ),
|
||||
'default' => '',
|
||||
],
|
||||
'language' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Language the brand should be analyzed in (e.g. "US English", "French"). Required; immutable after creation — ask the user which language to use.', 'seo-by-rank-math' ),
|
||||
'enum' => Brands_Controller::get_language_choices( false ),
|
||||
],
|
||||
'interval' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Analysis refresh cadence. Defaults to the account plan\'s default when omitted.', 'seo-by-rank-math' ),
|
||||
'enum' => array_merge( [ '' ], Brands_Controller::get_interval_choices() ),
|
||||
'default' => '',
|
||||
],
|
||||
'platforms' => [
|
||||
'type' => 'array',
|
||||
'minItems' => 1,
|
||||
'description' => sprintf(
|
||||
/* translators: %s: supported platform IDs. */
|
||||
esc_html__( 'AI platforms to track. Supported: %s. Selecting more than one requires the Expert plan; otherwise choose exactly one. Do not assume a default — ask the user which platform(s) to use.', 'seo-by-rank-math' ),
|
||||
Platforms::supported_as_string()
|
||||
),
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
'enum' => Platforms::supported(),
|
||||
],
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => false,
|
||||
'destructive' => false,
|
||||
'idempotent' => false,
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$platforms = $this->validate_platforms( $input['platforms'] ?? null );
|
||||
if ( isset( $platforms['error'] ) ) {
|
||||
return $platforms;
|
||||
}
|
||||
|
||||
$request = new WP_REST_Request( 'POST' );
|
||||
$request->set_param( 'name', sanitize_text_field( (string) ( $input['name'] ?? '' ) ) );
|
||||
$request->set_param( 'url', esc_url_raw( (string) ( $input['url'] ?? '' ) ) );
|
||||
$request->set_param( 'description', sanitize_textarea_field( (string) ( $input['description'] ?? '' ) ) );
|
||||
$request->set_param( 'locale', sanitize_text_field( (string) ( $input['locale'] ?? '' ) ) );
|
||||
$request->set_param( 'language', sanitize_text_field( (string) ( $input['language'] ?? '' ) ) );
|
||||
$request->set_param( 'interval', sanitize_text_field( (string) ( $input['interval'] ?? '' ) ) );
|
||||
$request->set_param( 'platforms', $platforms );
|
||||
|
||||
$response = $this->controller->create_brand( $request );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return [ 'error' => $response->get_error_message() ];
|
||||
}
|
||||
|
||||
$data = $response->get_data();
|
||||
$brand = $data['data']['brand'] ?? [];
|
||||
|
||||
return [
|
||||
'id' => $brand['id'] ?? '',
|
||||
'name' => $brand['name'] ?? '',
|
||||
'url' => $brand['url'] ?? '',
|
||||
'interval' => $brand['interval'] ?? null,
|
||||
'platforms' => $brand['platforms'] ?? [],
|
||||
'analysis_status' => 'pending',
|
||||
'created_at' => $brand['created_at'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize and validate platforms. No silent defaults.
|
||||
*
|
||||
* @param mixed $platforms Raw platforms value.
|
||||
* @return string[]|array{error: string}
|
||||
*/
|
||||
private function validate_platforms( $platforms ) {
|
||||
$valid = Platforms::validate( $platforms );
|
||||
if ( is_wp_error( $valid ) ) {
|
||||
return [ 'error' => $valid->get_error_message() ];
|
||||
}
|
||||
|
||||
return Platforms::sanitize( $platforms );
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'id' => [
|
||||
'type' => 'string',
|
||||
'description' => 'UUID of the newly created brand. Use this as brand_id in subsequent ability calls.',
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Brand name.',
|
||||
],
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'description' => 'Brand website URL.',
|
||||
],
|
||||
'interval' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'enum' => array_merge( [ null ], Brands_Controller::get_interval_choices() ),
|
||||
'description' => 'Resolved analysis cadence for this brand.',
|
||||
],
|
||||
'platforms' => [
|
||||
'type' => 'array',
|
||||
'description' => 'AI platforms selected for this brand.',
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
'enum' => Platforms::supported(),
|
||||
],
|
||||
],
|
||||
'analysis_status' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'pending' ],
|
||||
'description' => 'Always "pending" on creation — the initial analysis is seeded automatically and will complete asynchronously.',
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'date-time',
|
||||
'description' => 'ISO 8601 datetime when the brand was created.',
|
||||
],
|
||||
'error' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Present only on failure — human-readable error from validation or the upstream API.',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-ai-visibility-brand-insights
|
||||
*
|
||||
* @since 1.0.273
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\AI_Visibility
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\AI_Visibility;
|
||||
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
use RankMath\AI_Visibility\Cache;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-ai-visibility-brand-insights ability.
|
||||
*/
|
||||
class Get_AI_Visibility_Brand_Insights implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-ai-visibility-brand-insights',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get AI Visibility brand insights', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns the full analysis results for a single brand — AI Visibility score, rank, sentiment, mention and citation counts, per-competitor breakdown, and per-query results including the raw AI model responses.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'brand_id' ],
|
||||
'properties' => [
|
||||
'brand_id' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Brand UUID. Obtain from get-ai-visibility-overview.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => true,
|
||||
'destructive' => false,
|
||||
'idempotent' => true,
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$brand_id = sanitize_text_field( (string) ( $input['brand_id'] ?? '' ) );
|
||||
|
||||
$cached = Cache::get_analysis( $brand_id );
|
||||
|
||||
if ( false === $cached || Cache::is_brand_stale( $brand_id ) ) {
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'AI Visibility Brand Insights Fetched',
|
||||
[
|
||||
'brand_id' => $brand_id,
|
||||
'pending' => true,
|
||||
'query_count' => 0,
|
||||
'competitor_count' => 0,
|
||||
],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return [ 'pending' => true ];
|
||||
}
|
||||
|
||||
$query_count = count( $cached['query_results'] ?? [] );
|
||||
$competitor_count = count( $cached['competitors'] ?? [] );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'AI Visibility Brand Insights Fetched',
|
||||
[
|
||||
'brand_id' => $brand_id,
|
||||
'pending' => false,
|
||||
'query_count' => $query_count,
|
||||
'competitor_count' => $competitor_count,
|
||||
],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return array_merge( $cached, [ 'pending' => false ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'pending' => [
|
||||
'type' => 'boolean',
|
||||
'description' => 'True when no completed analysis exists yet (e.g. brand was just added). All other fields are absent in this case.',
|
||||
],
|
||||
'score' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'AI Visibility score for this brand.',
|
||||
],
|
||||
'rank' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Brand ranking position among competitors.',
|
||||
],
|
||||
'avg_sentiment' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Average sentiment score across all mentions.',
|
||||
],
|
||||
'mentions' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Total number of AI mentions.',
|
||||
],
|
||||
'citations' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Total number of AI citations.',
|
||||
],
|
||||
'analyses' => [
|
||||
'type' => 'array',
|
||||
'description' => 'The latest completed analysis for each tracked AI platform (one entry per platform).',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'id' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'Analysis UUID.',
|
||||
],
|
||||
'platform' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'AI platform this analysis ran on (e.g. chatgpt, gemini).',
|
||||
],
|
||||
'status' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'Analysis status (e.g. done, processing, error).',
|
||||
],
|
||||
'started_at' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'date-time',
|
||||
'description' => 'ISO 8601 datetime when the analysis started.',
|
||||
],
|
||||
'finished_at' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'date-time',
|
||||
'description' => 'ISO 8601 datetime when the analysis completed.',
|
||||
],
|
||||
'duration_seconds' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'How long the analysis took in seconds.',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'competitors' => [
|
||||
'type' => 'array',
|
||||
'description' => 'Per-competitor breakdown for this brand.',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'name' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Competitor brand name.',
|
||||
],
|
||||
'url' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'uri',
|
||||
'description' => 'Competitor website URL.',
|
||||
],
|
||||
'mentions' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Number of AI mentions for this competitor.',
|
||||
],
|
||||
'avg_sentiment' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Average sentiment score for this competitor.',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'query_results' => [
|
||||
'type' => 'array',
|
||||
'description' => 'Per-query analysis results including raw AI model responses.',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'query_id' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'Query UUID.',
|
||||
],
|
||||
'query_text' => [
|
||||
'type' => 'string',
|
||||
'description' => 'The query that was submitted to the AI model.',
|
||||
],
|
||||
'platform' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'AI platform that produced this result (e.g. chatgpt, gemini).',
|
||||
],
|
||||
'found' => [
|
||||
'type' => 'boolean',
|
||||
'description' => 'Whether the brand was mentioned in the AI response.',
|
||||
],
|
||||
'rank' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Brand rank among competitors for this query.',
|
||||
],
|
||||
'sentiment' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Sentiment score for the brand mention in this query.',
|
||||
],
|
||||
'citations' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Number of citations in the AI response for this query.',
|
||||
],
|
||||
'response' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Raw text response returned by the AI model for this query.',
|
||||
],
|
||||
'extraction_data' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Raw extraction data used to derive the structured results.',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-ai-visibility-brand-queries
|
||||
*
|
||||
* @since 1.0.273
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\AI_Visibility
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\AI_Visibility;
|
||||
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
use RankMath\AI_Visibility\Cache;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-ai-visibility-brand-queries ability.
|
||||
*/
|
||||
class Get_AI_Visibility_Brand_Queries implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-ai-visibility-brand-queries',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get AI Visibility brand queries', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns the list of search queries being monitored for a brand, including which are currently enabled and which were auto-generated as baselines.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'brand_id' ],
|
||||
'properties' => [
|
||||
'brand_id' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Brand UUID. Obtain from get-ai-visibility-overview or get-ai-visibility-brand-insights.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => true,
|
||||
'destructive' => false,
|
||||
'idempotent' => true,
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$brand_id = sanitize_text_field( (string) ( $input['brand_id'] ?? '' ) );
|
||||
|
||||
$cached = Cache::get_queries( $brand_id );
|
||||
$queries = null !== $cached ? (array) ( $cached['queries'] ?? [] ) : [];
|
||||
$total = count( $queries );
|
||||
|
||||
$enabled_count = count(
|
||||
array_filter(
|
||||
$queries,
|
||||
function ( $q ) {
|
||||
return ! empty( $q['enabled'] );
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'AI Visibility Brand Queries Fetched',
|
||||
[
|
||||
'brand_id' => $brand_id,
|
||||
'total' => $total,
|
||||
'enabled_count' => $enabled_count,
|
||||
],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return [
|
||||
'brand_id' => $brand_id,
|
||||
'queries' => $queries,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'brand_id' => [
|
||||
'type' => 'string',
|
||||
'description' => 'The brand UUID the queries belong to.',
|
||||
],
|
||||
'queries' => [
|
||||
'type' => 'array',
|
||||
'description' => 'List of monitored queries for this brand.',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'id' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Query UUID.',
|
||||
],
|
||||
'text' => [
|
||||
'type' => 'string',
|
||||
'description' => 'The query text submitted to AI models.',
|
||||
],
|
||||
'enabled' => [
|
||||
'type' => 'boolean',
|
||||
'description' => 'Whether this query is currently active for analysis.',
|
||||
],
|
||||
'is_baseline' => [
|
||||
'type' => 'boolean',
|
||||
'description' => 'True when this query was auto-generated as a baseline by Rank Math.',
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'date-time',
|
||||
'description' => 'ISO 8601 datetime when the query was created.',
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'date-time',
|
||||
'description' => 'ISO 8601 datetime when the query was last updated.',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'total' => [
|
||||
'type' => 'integer',
|
||||
'description' => 'Total number of queries returned.',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-ai-visibility-overview
|
||||
*
|
||||
* @since 1.0.273
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\AI_Visibility
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\AI_Visibility;
|
||||
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
use RankMath\AI_Visibility\Cache;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-ai-visibility-overview ability.
|
||||
*/
|
||||
class Get_AI_Visibility_Overview implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-ai-visibility-overview',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get AI Visibility overview', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns the site-level AI Visibility summary and the full list of tracked brands with their current scores, ranks, sentiment, and analysis status.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'refresh' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Pass true to bypass the 12-hour dashboard cache and return a stale-flagged response. Use the REST endpoint to trigger a fresh upstream fetch.', 'seo-by-rank-math' ),
|
||||
'default' => false,
|
||||
],
|
||||
'search' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Filter brands by name or URL (case-insensitive substring match).', 'seo-by-rank-math' ),
|
||||
'default' => '',
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => true,
|
||||
'destructive' => false,
|
||||
'idempotent' => true,
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$refresh = (bool) ( $input['refresh'] ?? false );
|
||||
$search = sanitize_text_field( (string) ( $input['search'] ?? '' ) );
|
||||
|
||||
$data = Cache::get_dashboard();
|
||||
|
||||
$summary = isset( $data['summary'] ) ? (array) $data['summary'] : [];
|
||||
$brands = isset( $data['brands'] ) ? (array) $data['brands'] : [];
|
||||
|
||||
if ( '' !== $search ) {
|
||||
$brands = array_values(
|
||||
array_filter(
|
||||
$brands,
|
||||
function ( $row ) use ( $search ) {
|
||||
return false !== stripos( $row['name'] ?? '', $search )
|
||||
|| false !== stripos( $row['url'] ?? '', $search );
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Strip internal cache fields not relevant to MCP consumers.
|
||||
$brands = array_map(
|
||||
function ( $row ) {
|
||||
unset( $row['stale'], $row['next_scheduled'], $row['created_at'] );
|
||||
return $row;
|
||||
},
|
||||
$brands
|
||||
);
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'AI Visibility Overview Fetched',
|
||||
[
|
||||
'brand_count' => count( $brands ),
|
||||
'refresh' => $refresh,
|
||||
],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return [
|
||||
'summary' => $summary,
|
||||
'brands' => $brands,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'summary' => [
|
||||
'type' => 'object',
|
||||
'description' => 'Site-level AI Visibility summary metrics.',
|
||||
],
|
||||
'brands' => [
|
||||
'type' => 'array',
|
||||
'description' => 'List of tracked brands with their current AI Visibility data.',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'id' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Brand UUID.',
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'string',
|
||||
'description' => 'Brand name.',
|
||||
],
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'description' => 'Brand website URL.',
|
||||
],
|
||||
'locale' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'ISO 3166-1 alpha-2 country code (e.g. "US").',
|
||||
],
|
||||
'status' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'active', 'inactive' ],
|
||||
'description' => 'Whether the brand is actively tracked.',
|
||||
],
|
||||
'score' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'AI Visibility score for this brand.',
|
||||
],
|
||||
'rank' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Brand ranking position.',
|
||||
],
|
||||
'avg_sentiment' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Average sentiment score across mentions.',
|
||||
],
|
||||
'mentions' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Total number of AI mentions.',
|
||||
],
|
||||
'citations' => [
|
||||
'type' => [ 'number', 'null' ],
|
||||
'description' => 'Total number of AI citations.',
|
||||
],
|
||||
'analysis_status' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'description' => 'Current analysis status (e.g. pending, processing, success, error).',
|
||||
],
|
||||
'last_analyzed' => [
|
||||
'type' => [ 'string', 'null' ],
|
||||
'format' => 'date-time',
|
||||
'description' => 'ISO 8601 datetime of the last completed analysis.',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Subscriber for AI Visibility abilities.
|
||||
*
|
||||
* @since 1.0.273
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\AI_Visibility
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\AI_Visibility;
|
||||
|
||||
use RankMath\Traits\Hooker;
|
||||
use RankMath\Abilities\Subscriber_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers the AI Visibility ability category and its abilities.
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
|
||||
use Hooker;
|
||||
|
||||
/**
|
||||
* Ability category slug for AI Visibility abilities.
|
||||
*/
|
||||
const CATEGORY_SLUG = 'rank-math-ai-visibility';
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $shared_meta Shared meta args from the top-level Abilities class.
|
||||
*/
|
||||
public function __construct( array $shared_meta ) {
|
||||
$this->shared_meta = $shared_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wire hooks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->action( 'wp_abilities_api_categories_init', 'register_category' );
|
||||
$this->action( 'wp_abilities_api_init', 'register_get_ai_visibility_overview' );
|
||||
$this->action( 'wp_abilities_api_init', 'register_get_ai_visibility_brand_insights' );
|
||||
$this->action( 'wp_abilities_api_init', 'register_get_ai_visibility_brand_queries' );
|
||||
$this->action( 'wp_abilities_api_init', 'register_create_ai_visibility_brand' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the AI Visibility ability category.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_category(): void {
|
||||
\wp_register_ability_category(
|
||||
self::CATEGORY_SLUG,
|
||||
[
|
||||
'label' => esc_html__( 'AI Visibility', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Abilities to retrieve AI brand visibility data tracked by Rank Math.', 'seo-by-rank-math' ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the rank-math/get-ai-visibility-overview ability.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_get_ai_visibility_overview(): void {
|
||||
( new Get_AI_Visibility_Overview( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the rank-math/get-ai-visibility-brand-insights ability.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_get_ai_visibility_brand_insights(): void {
|
||||
( new Get_AI_Visibility_Brand_Insights( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the rank-math/get-ai-visibility-brand-queries ability.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_get_ai_visibility_brand_queries(): void {
|
||||
( new Get_AI_Visibility_Brand_Queries( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the rank-math/create-ai-visibility-brand ability.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_create_ai_visibility_brand(): void {
|
||||
( new Create_AI_Visibility_Brand( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden.
|
||||
Reference in New Issue
Block a user