- md5
- 47b06ac7ed19734c82ba141aab2e83a8
- sha1
- 5e8eddbca7ccba1e7a968994d1634122748bccd7
- sha256
- bf1e2df092ca19ad20780d65e1d90749fa0469b344282c515d6284ed80cbe651
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
<?php
/**
* Get Stats ability for Akismet.
*
* @package Akismet
* @since 5.7
*/
declare( strict_types = 1 );
/**
* Class Akismet_Ability_Get_Stats
*
* Registers and handles the ability to retrieve Akismet statistics.
*/
class Akismet_Ability_Get_Stats extends Akismet_Ability {
/**
* Get the ability name.
*
* @return string The ability name.
*/
protected function get_ability_name(): string {
return 'akismet/get-stats';
}
/**
* Get the human-readable label.
*
* @return string The label.
*/
protected function get_label(): string {
return __( 'Get Akismet statistics', 'akismet' );
}
/**
* Get the ability description.
*
* @return string The description.
*/
protected function get_description(): string {
return __( 'Retrieves Akismet spam protection statistics including spam blocked count, accuracy percentage, and other key metrics.', 'akismet' );
}
/**
* Get the input schema.
*
* @return array The input schema.
*/
protected function get_input_schema(): array {
return array(
'type' => array( 'object', 'null' ),
'properties' => array(
'interval' => array(
'type' => 'string',
'description' => __( 'The time interval for stats. Options: "6-months", "all", or "60-days".', 'akismet' ),
'enum' => array( '6-months', 'all', '60-days' ),
'default' => '6-months',
),
),
'additionalProperties' => false,
);
}
/**
* Get the output schema.
*
* @return array The output schema.
*/
protected function get_output_schema(): array {
return array(
'type' => 'object',
'properties' => array(
'success' => array(
'type' => 'boolean',
'description' => __( 'Whether the stats were successfully retrieved.', 'akismet' ),
),
'spam' => array(
'type' => 'integer',
'description' => __( 'Total number of spam comments blocked.', 'akismet' ),
),
'ham' => array(
'type' => 'integer',
'description' => __( 'Total number of legitimate comments approved.', 'akismet' ),
),
'missed_spam' => array(
'type' => 'integer',
'description' => __( 'Number of spam comments that were missed.', 'akismet' ),
),
'false_positives' => array(
'type' => 'integer',
'description' => __( 'Number of legitimate comments incorrectly marked as spam.', 'akismet' ),
),
'accuracy' => array(
'type' => 'number',
'description' => __( 'Accuracy percentage of spam detection.', 'akismet' ),
),
'time_saved' => array(
'type' => 'integer',
'description' => __( 'Estimated time saved by Akismet blocking spam, in seconds.', 'akismet' ),
),
'breakdown' => array(
'type' => 'object',
'description' => __( 'Monthly breakdown of statistics.', 'akismet' ),
'additionalProperties' => array(
'type' => 'object',
'properties' => array(
'spam' => array(
'type' => 'integer',
'description' => __( 'Total number of spam comments blocked in this period.', 'akismet' ),
),
'ham' => array(
'type' => 'integer',
'description' => __( 'Total number of legitimate comments approved in this period.', 'akismet' ),
),
'missed_spam' => array(
'type' => 'integer',
'description' => __( 'Number of spam comments that were missed in this period.', 'akismet' ),
),
'false_positives' => array(
'type' => 'integer',
'description' => __( 'Number of legitimate comments incorrectly marked as spam in this period.', 'akismet' ),
),
'da' => array(
'type' => 'string',
'description' => __( 'Date for this period.', 'akismet' ),
),
),
),
),
'interval' => array(
'type' => 'string',
'description' => __( 'The time interval for these stats.', 'akismet' ),
),
'error' => array(
'type' => 'string',
'description' => __( 'Error message if the operation could not be completed.', 'akismet' ),
),
),
'additionalProperties' => false,
);
}
/**
* Get the ability configuration.
*
* @return array The ability configuration.
*/
public function get_config(): array {
return array(
'label' => $this->get_label(),
'description' => $this->get_description(),
'category' => Akismet_Abilities::CATEGORY_SLUG,
'input_schema' => $this->get_input_schema(),
'output_schema' => $this->get_output_schema(),
'execute_callback' => array( $this, 'execute' ),
'permission_callback' => array( $this, 'current_user_has_permission' ),
'meta' => array(
'annotations' => array(
'readonly' => true,
'destructive' => false,
'idempotent' => true,
),
'mcp' => array(
'public' => ( get_option( 'akismet_enable_mcp_access' ) === '1' ),
'type' => 'tool',
),
'show_in_rest' => true,
),
);
}
/**
* Execute callback for the get-stats ability.
*
* @param array|null $input The input parameters with optional interval.
* @return array|WP_Error The stats data or error.
*/
public function execute( ?array $input = null ) {
// Get interval from input or use default.
$interval = isset( $input['interval'] ) ? $input['interval'] : '6-months';
// Fetch stats from Akismet API.
$data = Akismet::get_stats( $interval );
if ( ! $data ) {
return new WP_Error(
'stats_fetch_failed',
__( 'Failed to retrieve stats from Akismet API.', 'akismet' )
);
}
// Build response with data from API (already properly typed by get_stats).
return array_merge(
array(
'success' => true,
'interval' => $interval,
),
(array) $data
);
}
}
The same content is in 15 releases, WordPress 7.1 to 7.2-RC3. See them all, or every version of this file.