wp/wp-content/plugins/akismet/class.akismet-cli.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    86 		
    86 		
    87 		if ( $total_counts['error'] ) {
    87 		if ( $total_counts['error'] ) {
    88 			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'] ) ) );
    88 			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'] ) ) );
    89 		}
    89 		}
    90 	}
    90 	}
       
    91 	
       
    92 	/**
       
    93 	 * Fetches stats from the Akismet API.
       
    94 	 *
       
    95 	 * ## OPTIONS
       
    96 	 *
       
    97 	 * [<interval>]
       
    98 	 * : The time period for which to retrieve stats.
       
    99 	 * ---
       
   100 	 * default: all
       
   101 	 * options:
       
   102 	 *  - days
       
   103 	 *  - months
       
   104 	 *  - all
       
   105 	 * ---
       
   106 	 *
       
   107 	 * [--format=<format>]
       
   108 	 * : Allows overriding the output of the command when listing connections.
       
   109 	 * ---
       
   110 	 * default: table
       
   111 	 * options:
       
   112 	 *  - table
       
   113 	 *  - json
       
   114 	 *  - csv
       
   115 	 *  - yaml
       
   116 	 *  - count
       
   117 	 * ---
       
   118 	 *
       
   119 	 * [--summary]
       
   120 	 * : When set, will display a summary of the stats.
       
   121 	 *
       
   122 	 * ## EXAMPLES
       
   123 	 *
       
   124 	 * wp akismet stats
       
   125 	 * wp akismet stats all
       
   126 	 * wp akismet stats days
       
   127 	 * wp akismet stats months
       
   128 	 * wp akismet stats all --summary
       
   129 	 */
       
   130 	public function stats( $args, $assoc_args ) {
       
   131 		$api_key = Akismet::get_api_key();
       
   132  
       
   133 		if ( empty( $api_key ) ) {
       
   134 			WP_CLI::error( __( 'API key must be set to fetch stats.', 'akismet' ) );
       
   135 		}
       
   136  
       
   137 		switch ( $args[0] ) {
       
   138 			case 'days':
       
   139 				$interval = '60-days';
       
   140 				break;
       
   141 			case 'months':
       
   142 				$interval = '6-months';
       
   143 				break;
       
   144 			default:
       
   145 				$interval = 'all';
       
   146 				break;
       
   147 		}
       
   148  
       
   149 		$response = Akismet::http_post(
       
   150 			Akismet::build_query( array(
       
   151 				'blog' => get_option( 'home' ),
       
   152 				'key'  => $api_key,
       
   153 				'from' => $interval,
       
   154 			) ),
       
   155 			'get-stats'
       
   156 		);
       
   157  
       
   158 		if ( empty( $response[1] ) ) {
       
   159 			WP_CLI::error( __( 'Currently unable to fetch stats. Please try again.', 'akismet' ) );
       
   160 		}
       
   161  
       
   162 		$response_body = json_decode( $response[1], true );
       
   163 		
       
   164 		if ( is_null( $response_body ) ) {
       
   165 			WP_CLI::error( __( 'Stats response could not be decoded.', 'akismet' ) );
       
   166 		}
       
   167  
       
   168 		if ( isset( $assoc_args['summary'] ) ) {
       
   169 			$keys = array(
       
   170 				'spam',
       
   171 				'ham',
       
   172 				'missed_spam',
       
   173 				'false_positives',
       
   174 				'accuracy',
       
   175 				'time_saved',
       
   176 			);
       
   177  
       
   178 			WP_CLI\Utils\format_items( $assoc_args['format'], array( $response_body ), $keys );
       
   179 		}
       
   180 		else {
       
   181 			$stats = $response_body['breakdown'];
       
   182 			WP_CLI\Utils\format_items( $assoc_args['format'], $stats, array_keys( end( $stats ) ) );
       
   183 		}
       
   184 	}
    91 }
   185 }