PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / 2.2.5
MainWP Child Reports v2.2.5
0.0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9.1 1.9.2 1.9.3 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1 2.1.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.3 2.3.1 trunk
mainwp-child-reports / classes / class-cli.php
mainwp-child-reports / classes Last commit date
class-admin.php 1 year ago class-author.php 5 years ago class-cli.php 5 years ago class-connector.php 2 years ago class-connectors.php 3 years ago class-date-interval.php 5 years ago class-db-driver-wpdb.php 2 years ago class-db-driver.php 5 years ago class-db.php 1 year ago class-export.php 5 years ago class-exporter.php 5 years ago class-filter-input.php 3 years ago class-form-generator.php 1 year ago class-install.php 3 years ago class-list-table.php 4 years ago class-live-update.php 3 years ago class-log.php 1 year ago class-mainwp-child-report-helper.php 5 years ago class-network.php 1 year ago class-plugin.php 3 years ago class-preview-list-table.php 5 years ago class-query.php 4 years ago class-record.php 5 years ago class-settings.php 1 year ago class-uninstall.php 2 years ago
class-cli.php
244 lines
1 <?php
2 /**
3 * Stream command for WP-CLI
4 *
5 * @see https://github.com/wp-cli/wp-cli
6 */
7
8 namespace WP_MainWP_Stream;
9
10 /**
11 * Class CLI.
12 * @package WP_MainWP_Stream
13 */
14 class CLI extends \WP_CLI_Command {
15
16 /**
17 * Query a set of Stream records.
18 *
19 * ## OPTIONS
20 *
21 * [--fields=<fields>]
22 * : Limit the output to specific object fields.
23 *
24 * [--<field>=<value>]
25 * : One or more args to pass to WP_MainWP_Stream_Query.
26 *
27 * [--format=<format>]
28 * : Accepted values: table, count, json, json_pretty, csv. Default: table
29 *
30 * ## AVAILABLE FIELDS TO QUERY
31 *
32 * You can build a query from these fields:
33 *
34 * * user_id
35 * * user_id__in
36 * * user_id__not_in
37 * * user_role
38 * * user_role__in
39 * * user_role__not_in
40 * * date
41 * * date_from
42 * * date_to
43 * * date_after
44 * * date_before
45 * * ip
46 * * ip__in
47 * * ip__not_in
48 * * connector
49 * * connector__in
50 * * connector__not_in
51 * * context
52 * * context__in
53 * * context__not_in
54 * * action
55 * * action__in
56 * * action__not_in
57 * * search
58 * * search_field
59 * * record
60 * * record__in
61 * * record__not_in
62 * * records_per_page
63 * * paged
64 * * order
65 * * orderby
66 *
67 * ## AVAILABLE FIELDS
68 *
69 * These fields will be displayed by default for each post:
70 *
71 * * created
72 * * ip
73 * * user_id
74 * * user_role
75 * * summary
76 *
77 * These fields are optionally available:
78 *
79 * * ID
80 * * site_id
81 * * blog_id
82 * * object_id
83 * * connector
84 * * context
85 * * action
86 *
87 * ## EXAMPLES
88 *
89 * wp stream query --user_role__not_in=administrator --date_after=2015-01-01T12:00:00
90 * wp stream query --user_id=1 --action=login --records_per_page=50 --fields=created
91 *
92 * @see WP_MainWP_Stream_Query
93 * @see https://github.com/wp-stream/stream/wiki/WP-CLI-Command
94 * @see https://github.com/wp-stream/stream/wiki/Query-Reference
95 *
96 * @param array $args Query arguments.
97 * @param array $assoc_args Association arguments.
98 *
99 * @uses \WP_MainWP_Stream\CLI::connection()
100 */
101 public function query( $args, $assoc_args ) {
102 unset( $args );
103
104 $query_args = array();
105 $formatted_records = array();
106
107 $this->connection();
108
109 if ( empty( $assoc_args['fields'] ) ) {
110 $fields = array(
111 'created',
112 'ip',
113 'user_id',
114 'user_role',
115 'summary',
116 );
117 } else {
118 $fields = explode( ',', $assoc_args['fields'] );
119 }
120
121 foreach ( $assoc_args as $key => $value ) {
122 if ( 'format' === $key ) {
123 continue;
124 }
125
126 $query_args[ $key ] = $value;
127 }
128
129 $query_args['fields'] = implode( ',', $fields );
130
131 $records = wp_mainwp_stream_get_instance()->db->query( $query_args );
132
133 // Make structure Formatter compatible
134 foreach ( (array) $records as $key => $record ) {
135 $formatted_records[ $key ] = array();
136
137 // Catch any fields missing in records
138 foreach ( $fields as $field ) {
139 if ( ! array_key_exists( $field, $record ) ) {
140 $record->$field = null;
141 }
142 }
143
144 foreach ( $record as $field_name => $field ) {
145
146 $formatted_records[ $key ] = array_merge(
147 $formatted_records[ $key ],
148 $this->format_field( $field_name, $field )
149 );
150 }
151 }
152
153 if ( isset( $assoc_args['format'] ) && 'table' !== $assoc_args['format'] ) {
154 if ( 'count' === $assoc_args['format'] ) {
155 \WP_CLI::line( count( $records ) );
156 }
157
158 if ( 'json' === $assoc_args['format'] ) {
159 \WP_CLI::line( wp_mainwp_stream_json_encode( $formatted_records ) );
160 }
161
162 if ( 'json_pretty' === $assoc_args['format'] ) {
163 if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
164 \WP_CLI::line( wp_mainwp_stream_json_encode( $formatted_records ) ); // xss ok
165 } else {
166 \WP_CLI::line( wp_mainwp_stream_json_encode( $formatted_records, JSON_PRETTY_PRINT ) ); // xss ok
167 }
168 }
169
170 if ( 'csv' === $assoc_args['format'] ) {
171 \WP_CLI::line( $this->csv_format( $formatted_records ) );
172 }
173
174 return;
175 }
176
177 $formatter = new \WP_CLI\Formatter(
178 $assoc_args,
179 $fields
180 );
181
182 $formatter->display_items( $formatted_records );
183 }
184
185 /**
186 * Convert any field to a flat array.
187 *
188 * @param string $name The output array element name
189 * @param mixed $object Any value to be converted to an array
190 *
191 * @return array The flat array
192 */
193 private function format_field( $name, $object ) {
194 $array = array();
195
196 if ( is_object( $object ) ) {
197 foreach ( $object as $key => $property ) {
198 $array = array_merge( $array, $this->format_field( $name . '.' . $key, $property ) );
199 }
200 } elseif ( is_array( $object ) ) {
201 $array[ $name ] = $object[0];
202 } else {
203 $array[ $name ] = $object;
204 }
205
206 return $array;
207 }
208
209 /**
210 * Convert an array of flat records to CSV
211 *
212 * @param array $array The input array of records
213 *
214 * @return string The CSV output
215 */
216 private function csv_format( $array ) {
217 $output = fopen( 'php://output', 'w' ); // @codingStandardsIgnoreLine Clever output for WP CLI using php://output
218
219 foreach ( $array as $line ) {
220 fputcsv( $output, $line ); // @codingStandardsIgnoreLine
221 }
222
223 fclose( $output ); // @codingStandardsIgnoreLine
224 }
225
226 /**
227 * Checks for a Stream connection and displays an error or success message.
228 *
229 * @return void
230 */
231 private function connection() {
232 $query = wp_mainwp_stream_get_instance()->db->query(
233 array(
234 'records_per_page' => 1,
235 'fields' => 'created',
236 )
237 );
238
239 if ( ! $query ) {
240 \WP_CLI::error( esc_html__( 'SITE IS DISCONNECTED', 'mainwp-child-reports' ) );
241 }
242 }
243 }
244