| @@ -3,11 +3,13 @@ | ||
| 3 | 3 | * Stream command for WP-CLI |
| 4 | 4 | * |
| 5 | 5 | * @see https://github.com/wp-cli/wp-cli |
| 6 | 6 | */ |
| 7 | + | |
| 7 | 8 | namespace WP_Stream; |
| 8 | 9 | |
| 9 | 10 | class CLI extends \WP_CLI_Command { |
| 11 | + | |
| 10 | 12 | /** |
| 11 | 13 | * Query a set of Stream records. |
| 12 | 14 | * |
| 13 | 15 | * ## OPTIONS |
| @@ -95,9 +97,15 @@ | ||
| 95 | 97 | |
| 96 | 98 | $this->connection(); |
| 97 | 99 | |
| 98 | 100 | if ( empty( $assoc_args['fields'] ) ) { |
| 99 | - $fields = array( 'created', 'ip', 'user_id', 'user_role', 'summary' ); | |
| 101 | + $fields = array( | |
| 102 | + 'created', | |
| 103 | + 'ip', | |
| 104 | + 'user_id', | |
| 105 | + 'user_role', | |
| 106 | + 'summary', | |
| 107 | + ); | |
| 100 | 108 | } else { |
| 101 | 109 | $fields = explode( ',', $assoc_args['fields'] ); |
| 102 | 110 | } |
| 103 | 111 | |
| @@ -110,9 +118,9 @@ | ||
| 110 | 118 | } |
| 111 | 119 | |
| 112 | 120 | $query_args['fields'] = implode( ',', $fields ); |
| 113 | 121 | |
| 114 | - $records = wp_stream_get_instance()->db->query->query( $query_args ); | |
| 122 | + $records = wp_stream_get_instance()->db->query( $query_args ); | |
| 115 | 123 | |
| 116 | 124 | // Make structure Formatter compatible |
| 117 | 125 | foreach ( (array) $records as $key => $record ) { |
| 118 | 126 | $formatted_records[ $key ] = array(); |
| @@ -134,25 +142,25 @@ | ||
| 134 | 142 | } |
| 135 | 143 | |
| 136 | 144 | if ( isset( $assoc_args['format'] ) && 'table' !== $assoc_args['format'] ) { |
| 137 | 145 | if ( 'count' === $assoc_args['format'] ) { |
| 138 | - WP_CLI::line( count( $records ) ); | |
| 146 | + \WP_CLI::line( count( $records ) ); | |
| 139 | 147 | } |
| 140 | 148 | |
| 141 | 149 | if ( 'json' === $assoc_args['format'] ) { |
| 142 | - WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); | |
| 150 | + \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); | |
| 143 | 151 | } |
| 144 | 152 | |
| 145 | 153 | if ( 'json_pretty' === $assoc_args['format'] ) { |
| 146 | 154 | if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { |
| 147 | - WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); // xss ok | |
| 155 | + \WP_CLI::line( wp_stream_json_encode( $formatted_records ) ); // xss ok | |
| 148 | 156 | } else { |
| 149 | - WP_CLI::line( wp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok | |
| 157 | + \WP_CLI::line( wp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok | |
| 150 | 158 | } |
| 151 | 159 | } |
| 152 | 160 | |
| 153 | 161 | if ( 'csv' === $assoc_args['format'] ) { |
| 154 | - WP_CLI::line( $this->csv_format( $formatted_records ) ); | |
| 162 | + \WP_CLI::line( $this->csv_format( $formatted_records ) ); | |
| 155 | 163 | } |
| 156 | 164 | |
| 157 | 165 | return; |
| 158 | 166 | } |
| @@ -167,10 +175,10 @@ | ||
| 167 | 175 | |
| 168 | 176 | /** |
| 169 | 177 | * Convert any field to a flat array. |
| 170 | 178 | * |
| 171 | - * @param string $name The output array element name | |
| 172 | - * @param mixed $object Any value to be converted to an array | |
| 179 | + * @param string $name The output array element name | |
| 180 | + * @param mixed $object Any value to be converted to an array | |
| 173 | 181 | * |
| 174 | 182 | * @return array The flat array |
| 175 | 183 | */ |
| 176 | 184 | private function format_field( $name, $object ) { |
| @@ -191,20 +199,20 @@ | ||
| 191 | 199 | |
| 192 | 200 | /** |
| 193 | 201 | * Convert an array of flat records to CSV |
| 194 | 202 | * |
| 195 | - * @param array $array The input array of records | |
| 203 | + * @param array $array The input array of records | |
| 196 | 204 | * |
| 197 | 205 | * @return string The CSV output |
| 198 | 206 | */ |
| 199 | 207 | private function csv_format( $array ) { |
| 200 | - $output = fopen( 'php://output', 'w' ); | |
| 208 | + $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output | |
| 201 | 209 | |
| 202 | 210 | foreach ( $array as $line ) { |
| 203 | - fputcsv( $output, $line ); | |
| 211 | + fputcsv( $output, $line ); // @codingStandardsIgnoreLine | |
| 204 | 212 | } |
| 205 | 213 | |
| 206 | - fclose( $output ); | |
| 214 | + fclose( $output ); // @codingStandardsIgnoreLine | |
| 207 | 215 | } |
| 208 | 216 | |
| 209 | 217 | /** |
| 210 | 218 | * Checks for a Stream connection and displays an error or success message. |
| @@ -211,11 +219,16 @@ | ||
| 211 | 219 | * |
| 212 | 220 | * @return void |
| 213 | 221 | */ |
| 214 | 222 | private function connection() { |
| 215 | - $query = wp_stream_get_instance()->db->query->query( array( 'records_per_page' => 1, 'fields' => 'created' ) ); | |
| 223 | + $query = wp_stream_get_instance()->db->query( | |
| 224 | + array( | |
| 225 | + 'records_per_page' => 1, | |
| 226 | + 'fields' => 'created', | |
| 227 | + ) | |
| 228 | + ); | |
| 216 | 229 | |
| 217 | 230 | if ( ! $query ) { |
| 218 | - WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) ); | |
| 231 | + \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'stream' ) ); | |
| 219 | 232 | } |
| 220 | 233 | } |
| 221 | 234 | } |