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