[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/plugins/akismet/abilities/ -> class-akismet-ability-comment-check.php (source)

   1  <?php
   2  /**
   3   * Comment Check ability for Akismet.
   4   *
   5   * @package Akismet
   6   * @since 5.7
   7   */
   8  
   9  declare( strict_types = 1 );
  10  
  11  /**
  12   * Class Akismet_Ability_Comment_Check
  13   *
  14   * Registers and handles the ability to check comments for spam.
  15   */
  16  class Akismet_Ability_Comment_Check extends Akismet_Ability implements Akismet_Ability_Interface {
  17  
  18      /**
  19       * Get the ability name.
  20       *
  21       * @return string The ability name.
  22       */
  23  	protected function get_ability_name(): string {
  24          return 'akismet/comment-check';
  25      }
  26  
  27      /**
  28       * Get the human-readable label.
  29       *
  30       * @return string The label.
  31       */
  32  	protected function get_label(): string {
  33          return __( 'Check comment for spam', 'akismet' );
  34      }
  35  
  36      /**
  37       * Get the ability description.
  38       *
  39       * @return string The description.
  40       */
  41  	protected function get_description(): string {
  42          return __( 'Checks a comment against the Akismet spam filter to determine if it is spam or legitimate content.', 'akismet' );
  43      }
  44  
  45      /**
  46       * Get the input schema.
  47       *
  48       * @return array The input schema.
  49       */
  50  	protected function get_input_schema(): array {
  51          return array(
  52              'type'                 => 'object',
  53              'properties'           => array(
  54                  'comment_author'       => array(
  55                      'type'        => 'string',
  56                      'description' => __( 'Name of the comment author.', 'akismet' ),
  57                  ),
  58                  'comment_author_email' => array(
  59                      'type'        => 'string',
  60                      'description' => __( 'Email address of the comment author.', 'akismet' ),
  61                      'format'      => 'email',
  62                  ),
  63                  'comment_author_url'   => array(
  64                      'type'        => 'string',
  65                      'description' => __( 'URL/website of the comment author.', 'akismet' ),
  66                      'format'      => 'uri',
  67                  ),
  68                  'comment_content'      => array(
  69                      'type'        => 'string',
  70                      'description' => __( 'The comment content/text.', 'akismet' ),
  71                  ),
  72                  'comment_type'         => array(
  73                      'type'        => 'string',
  74                      'description' => __( 'The comment type (e.g., "comment", "trackback", "pingback").', 'akismet' ),
  75                      'default'     => 'comment',
  76                  ),
  77                  'comment_post_ID'      => array(
  78                      'type'        => 'integer',
  79                      'description' => __( 'The ID of the post the comment is being submitted to.', 'akismet' ),
  80                  ),
  81                  'permalink'            => array(
  82                      'type'        => 'string',
  83                      'description' => __( 'The permanent link to the post or page.', 'akismet' ),
  84                      'format'      => 'uri',
  85                  ),
  86                  'user_ip'              => array(
  87                      'type'        => 'string',
  88                      'description' => __( 'IP address of the commenter.', 'akismet' ),
  89                  ),
  90                  'user_agent'           => array(
  91                      'type'        => 'string',
  92                      'description' => __( 'User agent string of the web browser submitting the comment.', 'akismet' ),
  93                  ),
  94                  'referrer'             => array(
  95                      'type'        => 'string',
  96                      'description' => __( 'The HTTP_REFERER header.', 'akismet' ),
  97                  ),
  98                  'user_role'            => array(
  99                      'type'        => 'string',
 100                      'description' => __( 'The user role of the comment author if logged in.', 'akismet' ),
 101                  ),
 102              ),
 103              'additionalProperties' => false,
 104          );
 105      }
 106  
 107      /**
 108       * Get the output schema.
 109       *
 110       * @return array The output schema.
 111       */
 112  	protected function get_output_schema(): array {
 113          return array(
 114              'type'                 => 'object',
 115              'properties'           => array(
 116                  'success'    => array(
 117                      'type'        => 'boolean',
 118                      'description' => __( 'Whether the check was successfully performed.', 'akismet' ),
 119                  ),
 120                  'is_spam'    => array(
 121                      'type'        => 'boolean',
 122                      'description' => __( 'Whether the comment is identified as spam.', 'akismet' ),
 123                  ),
 124                  'pro_tip'    => array(
 125                      'type'        => 'string',
 126                      'description' => __( 'Optional recommendation from Akismet (e.g., "discard" for obvious spam).', 'akismet' ),
 127                  ),
 128                  'guid'       => array(
 129                      'type'        => 'string',
 130                      'description' => __( 'Unique identifier for this check, used for webhooks and updates.', 'akismet' ),
 131                  ),
 132                  'error'      => array(
 133                      'type'        => 'string',
 134                      'description' => __( 'Error message if the check could not be completed.', 'akismet' ),
 135                  ),
 136                  'debug_help' => array(
 137                      'type'        => 'string',
 138                      'description' => __( 'Debug information to help troubleshoot issues.', 'akismet' ),
 139                  ),
 140              ),
 141              'additionalProperties' => false,
 142          );
 143      }
 144  
 145      /**
 146       * Get the ability configuration.
 147       *
 148       * @return array The ability configuration.
 149       */
 150  	public function get_config(): array {
 151          return array(
 152              'label'               => $this->get_label(),
 153              'description'         => $this->get_description(),
 154              'category'            => Akismet_Abilities::CATEGORY_SLUG,
 155              'input_schema'        => $this->get_input_schema(),
 156              'output_schema'       => $this->get_output_schema(),
 157              'execute_callback'    => array( $this, 'execute' ),
 158              'permission_callback' => array( $this, 'current_user_has_permission' ),
 159              'meta'                => array(
 160                  'annotations'  => array(
 161                      'readonly'    => true,
 162                      'destructive' => false,
 163                      'idempotent'  => false,
 164                  ),
 165                  'mcp'          => array(
 166                      'public' => ( get_option( 'akismet_enable_mcp_access' ) === '1' ),
 167                      'type'   => 'tool',
 168                  ),
 169                  'show_in_rest' => true,
 170              ),
 171          );
 172      }
 173  
 174      /**
 175       * Execute callback for the comment-check ability.
 176       *
 177       * @param array|null $input The comment data to check.
 178       * @return array|WP_Error The spam check result or error.
 179       */
 180  	public function execute( ?array $input = null ) {
 181          // Check for required API key.
 182          if ( ! Akismet::get_api_key() ) {
 183              return new WP_Error(
 184                  'akismet_not_configured',
 185                  __( 'Akismet is not configured. Please enter an API key.', 'akismet' )
 186              );
 187          }
 188  
 189          // Perform the comment check.
 190          $result = Akismet::comment_check( $input );
 191  
 192          if ( ! $result ) {
 193              return new WP_Error(
 194                  'comment_check_failed',
 195                  __( 'Failed to check comment with Akismet API.', 'akismet' )
 196              );
 197          }
 198  
 199          // Build response array.
 200          $response = array(
 201              'success' => true,
 202              'is_spam' => $result->is_spam,
 203          );
 204  
 205          // Include optional fields if present.
 206          if ( isset( $result->pro_tip ) ) {
 207              $response['pro_tip'] = $result->pro_tip;
 208          }
 209  
 210          if ( isset( $result->guid ) ) {
 211              $response['guid'] = $result->guid;
 212          }
 213  
 214          if ( isset( $result->error ) ) {
 215              $response['error'] = $result->error;
 216          }
 217  
 218          if ( isset( $result->debug_help ) ) {
 219              $response['debug_help'] = $result->debug_help;
 220          }
 221  
 222          return $response;
 223      }
 224  }


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref