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