/wp-content/plugins/akismet/class.akismet-cli.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 185 lines · 100 code · 18 blank · 67 comment · 20 complexity · 065a8481ab20ebe3e8714539673a0449 MD5 · raw file

  1. <?php
  2. WP_CLI::add_command( 'akismet', 'Akismet_CLI' );
  3. /**
  4. * Filter spam comments.
  5. */
  6. class Akismet_CLI extends WP_CLI_Command {
  7. /**
  8. * Checks one or more comments against the Akismet API.
  9. *
  10. * ## OPTIONS
  11. * <comment_id>...
  12. * : The ID(s) of the comment(s) to check.
  13. *
  14. * [--noaction]
  15. * : Don't change the status of the comment. Just report what Akismet thinks it is.
  16. *
  17. * ## EXAMPLES
  18. *
  19. * wp akismet check 12345
  20. *
  21. * @alias comment-check
  22. */
  23. public function check( $args, $assoc_args ) {
  24. foreach ( $args as $comment_id ) {
  25. if ( isset( $assoc_args['noaction'] ) ) {
  26. // Check the comment, but don't reclassify it.
  27. $api_response = Akismet::check_db_comment( $comment_id, 'wp-cli' );
  28. }
  29. else {
  30. $api_response = Akismet::recheck_comment( $comment_id, 'wp-cli' );
  31. }
  32. if ( 'true' === $api_response ) {
  33. WP_CLI::line( sprintf( __( "Comment #%d is spam.", 'akismet' ), $comment_id ) );
  34. }
  35. else if ( 'false' === $api_response ) {
  36. WP_CLI::line( sprintf( __( "Comment #%d is not spam.", 'akismet' ), $comment_id ) );
  37. }
  38. else {
  39. if ( false === $api_response ) {
  40. WP_CLI::error( __( "Failed to connect to Akismet.", 'akismet' ) );
  41. }
  42. else if ( is_wp_error( $api_response ) ) {
  43. WP_CLI::warning( sprintf( __( "Comment #%d could not be checked.", 'akismet' ), $comment_id ) );
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * Recheck all comments in the Pending queue.
  50. *
  51. * ## EXAMPLES
  52. *
  53. * wp akismet recheck_queue
  54. *
  55. * @alias recheck-queue
  56. */
  57. public function recheck_queue() {
  58. $batch_size = 100;
  59. $start = 0;
  60. $total_counts = array();
  61. do {
  62. $result_counts = Akismet_Admin::recheck_queue_portion( $start, $batch_size );
  63. if ( $result_counts['processed'] > 0 ) {
  64. foreach ( $result_counts as $key => $count ) {
  65. if ( ! isset( $total_counts[ $key ] ) ) {
  66. $total_counts[ $key ] = $count;
  67. }
  68. else {
  69. $total_counts[ $key ] += $count;
  70. }
  71. }
  72. $start += $batch_size;
  73. $start -= $result_counts['spam']; // These comments will have been removed from the queue.
  74. }
  75. } while ( $result_counts['processed'] > 0 );
  76. WP_CLI::line( sprintf( _n( "Processed %d comment.", "Processed %d comments.", $total_counts['processed'], 'akismet' ), number_format( $total_counts['processed'] ) ) );
  77. WP_CLI::line( sprintf( _n( "%d comment moved to Spam.", "%d comments moved to Spam.", $total_counts['spam'], 'akismet' ), number_format( $total_counts['spam'] ) ) );
  78. if ( $total_counts['error'] ) {
  79. WP_CLI::line( sprintf( _n( "%d comment could not be checked.", "%d comments could not be checked.", $total_counts['error'], 'akismet' ), number_format( $total_counts['error'] ) ) );
  80. }
  81. }
  82. /**
  83. * Fetches stats from the Akismet API.
  84. *
  85. * ## OPTIONS
  86. *
  87. * [<interval>]
  88. * : The time period for which to retrieve stats.
  89. * ---
  90. * default: all
  91. * options:
  92. * - days
  93. * - months
  94. * - all
  95. * ---
  96. *
  97. * [--format=<format>]
  98. * : Allows overriding the output of the command when listing connections.
  99. * ---
  100. * default: table
  101. * options:
  102. * - table
  103. * - json
  104. * - csv
  105. * - yaml
  106. * - count
  107. * ---
  108. *
  109. * [--summary]
  110. * : When set, will display a summary of the stats.
  111. *
  112. * ## EXAMPLES
  113. *
  114. * wp akismet stats
  115. * wp akismet stats all
  116. * wp akismet stats days
  117. * wp akismet stats months
  118. * wp akismet stats all --summary
  119. */
  120. public function stats( $args, $assoc_args ) {
  121. $api_key = Akismet::get_api_key();
  122. if ( empty( $api_key ) ) {
  123. WP_CLI::error( __( 'API key must be set to fetch stats.', 'akismet' ) );
  124. }
  125. switch ( $args[0] ) {
  126. case 'days':
  127. $interval = '60-days';
  128. break;
  129. case 'months':
  130. $interval = '6-months';
  131. break;
  132. default:
  133. $interval = 'all';
  134. break;
  135. }
  136. $response = Akismet::http_post(
  137. Akismet::build_query( array(
  138. 'blog' => get_option( 'home' ),
  139. 'key' => $api_key,
  140. 'from' => $interval,
  141. ) ),
  142. 'get-stats'
  143. );
  144. if ( empty( $response[1] ) ) {
  145. WP_CLI::error( __( 'Currently unable to fetch stats. Please try again.', 'akismet' ) );
  146. }
  147. $response_body = json_decode( $response[1], true );
  148. if ( is_null( $response_body ) ) {
  149. WP_CLI::error( __( 'Stats response could not be decoded.', 'akismet' ) );
  150. }
  151. if ( isset( $assoc_args['summary'] ) ) {
  152. $keys = array(
  153. 'spam',
  154. 'ham',
  155. 'missed_spam',
  156. 'false_positives',
  157. 'accuracy',
  158. 'time_saved',
  159. );
  160. WP_CLI\Utils\format_items( $assoc_args['format'], array( $response_body ), $keys );
  161. }
  162. else {
  163. $stats = $response_body['breakdown'];
  164. WP_CLI\Utils\format_items( $assoc_args['format'], $stats, array_keys( end( $stats ) ) );
  165. }
  166. }
  167. }