Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

91 rader
2.6 KiB

  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. }