PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.2
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
← All changes | class/class-mainwp-wp-cli-command.php +728 -763 6.24.2 View file →
@@ -4,22 +4,17 @@
4 4 *
5 5 * This file extends the WP-CLI and provides a set of SubCommands to Control your
6 6 * Child Sites that are added to the MainWP Dashboard.
7 7 *
8 - * @devtodo: allow to add or remove child sites
8 + * @todo: allow to add or remove child sites
9 9 * @package MainWP/Dashboard
10 10 */
11 11
12 12 namespace MainWP\Dashboard;
13 13
14 -// Exit if accessed directly.
15 -if ( ! defined( 'ABSPATH' ) ) {
16 - exit;
17 -}
18 -
19 14 // Exit if access directly.
20 15 if ( ! defined( 'WP_CLI' ) ) {
21 - return; // NOSONAR - jump to avoid CLI.
16 + return;
22 17 }
23 18
24 19 /**
25 20 * Class MainWP_WP_CLI_Command
@@ -27,842 +22,812 @@
27 22 * Manage all child sites added to the MainWP Dashboard.
28 23 *
29 24 * @package MainWP\Dashboard
30 25 */
31 -class MainWP_WP_CLI_Command extends \WP_CLI_Command { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
26 +class MainWP_WP_CLI_Command extends \WP_CLI_Command {
32 27
33 - /**
34 - * Method init()
35 - *
36 - * Initiate the MainWP CLI after all Plugins have loaded.
37 - */
38 - public static function init() {
39 - add_action( 'plugins_loaded', array( static::class, 'init_wpcli_commands' ), 99999 );
40 - }
28 + /**
29 + * Method init()
30 + *
31 + * Initiate the MainWP CLI after all Plugins have loaded.
32 + */
33 + public static function init() {
34 + add_action( 'plugins_loaded', array( self::class, 'init_wpcli_commands' ), 99999 );
35 + }
41 36
42 - /**
43 - * Method init_wpcli_commands
44 - *
45 - * Adds the MainWP WP CLI Commands via WP_CLI::add_command
46 - */
47 - public static function init_wpcli_commands() {
48 - \WP_CLI::add_command( 'mainwp', static::class );
49 - }
37 + /**
38 + * Method init_wpcli_commands
39 + *
40 + * Adds the MainWP WP CLI Commands via WP_CLI::add_command
41 + */
42 + public static function init_wpcli_commands() {
43 + \WP_CLI::add_command( 'mainwp', self::class );
44 + }
50 45
51 - /**
52 - * List information about added child sites.
53 - *
54 - * ## OPTIONS
55 - *
56 - * [--list]
57 - * : Get a list of all child sites
58 - *
59 - * [--count]
60 - * : If set, count child sites.
61 - *
62 - *
63 - * ## EXAMPLES
64 - *
65 - * wp mainwp sites --list
66 - * wp mainwp sites --all-sites-count
67 - *
68 - * ## Synopsis [--list] [--all-sites-count]
69 - *
70 - * @param array $args Function arguments.
71 - * @param array $assoc_args Function associate arguments.
72 - *
73 - * @uses \MainWP\Dashboard\MainWP_DB::query()
74 - * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
75 - * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
76 - * @uses \MainWP\Dashboard\MainWP_DB::free_result()
77 - * @uses \MainWP\Dashboard\MainWP_DB::data_seek()
78 - */
79 - public function sites( $args, $assoc_args ) {
46 + /**
47 + * List information about added child sites.
48 + *
49 + * ## OPTIONS
50 + *
51 + * [--list]
52 + * : Get a list of all child sites
53 + *
54 + * [--count]
55 + * : If set, count child sites.
56 + *
57 + *
58 + * ## EXAMPLES
59 + *
60 + * wp mainwp sites --list
61 + * wp mainwp sites --all-sites-count
62 + *
63 + * ## Synopsis [--list] [--all-sites-count]
64 + *
65 + * @param array $args Function arguments.
66 + * @param array $assoc_args Function associate arguments.
67 + *
68 + * @uses \MainWP\Dashboard\MainWP_DB::query()
69 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
70 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
71 + * @uses \MainWP\Dashboard\MainWP_DB::free_result()
72 + * @uses \MainWP\Dashboard\MainWP_DB::data_seek()
73 + */
74 + public function sites( $args, $assoc_args ) {
80 75
81 - // support new mainwp sites cli commands.
82 - $handle = MainWP_WP_CLI_Handle::get_assoc_args_commands( 'sites', $assoc_args );
83 - if ( ! empty( $handle ) ) {
84 - MainWP_WP_CLI_Handle::handle_cli_callback( 'sites', $args, $assoc_args );
85 - return;
86 - }
76 + // support new mainwp sites cli commands.
77 + $handle = MainWP_WP_CLI_Handle::get_assoc_args_commands( 'sites', $assoc_args );
78 + if ( ! empty( $handle ) ) {
79 + MainWP_WP_CLI_Handle::handle_cli_callback( 'sites', $args, $assoc_args );
80 + return;
81 + }
87 82
88 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
83 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
89 84
90 - $idLength = strlen( 'id' );
91 - $nameLength = strlen( 'name' );
92 - $urlLength = strlen( 'url' );
93 - $versionLength = strlen( 'version' );
94 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
95 - if ( $idLength < strlen( $website->id ) ) {
96 - $idLength = strlen( $website->id );
97 - }
98 - if ( $nameLength < strlen( $website->name ) ) {
99 - $nameLength = strlen( $website->name );
100 - }
101 - if ( $urlLength < strlen( $website->url ) ) {
102 - $urlLength = strlen( $website->url );
103 - }
104 - if ( $versionLength < strlen( $website->version ) ) {
105 - $versionLength = strlen( $website->version );
106 - }
107 - }
108 - MainWP_DB::data_seek( $websites, 0 );
85 + $idLength = strlen( 'id' );
86 + $nameLength = strlen( 'name' );
87 + $urlLength = strlen( 'url' );
88 + $versionLength = strlen( 'version' );
89 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
90 + if ( $idLength < strlen( $website->id ) ) {
91 + $idLength = strlen( $website->id );
92 + }
93 + if ( $nameLength < strlen( $website->name ) ) {
94 + $nameLength = strlen( $website->name );
95 + }
96 + if ( $urlLength < strlen( $website->url ) ) {
97 + $urlLength = strlen( $website->url );
98 + }
99 + if ( $versionLength < strlen( $website->version ) ) {
100 + $versionLength = strlen( $website->version );
101 + }
102 + }
103 + MainWP_DB::data_seek( $websites, 0 );
109 104
110 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
111 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's | %-' . $versionLength . 's |', 'id', 'name', 'url', 'version' ) );
112 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
105 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
106 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's | %-' . $versionLength . 's |', 'id', 'name', 'url', 'version' ) );
107 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
113 108
114 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
115 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's | %-' . $versionLength . 's |', $website->id, $website->name, $website->url, $website->version ) );
116 - }
109 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
110 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's | %-' . $versionLength . 's |', $website->id, $website->name, $website->url, $website->version ) );
111 + }
117 112
118 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
119 - MainWP_DB::free_result( $websites );
120 - }
113 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
114 + MainWP_DB::free_result( $websites );
115 + }
121 116
122 - /**
123 - * Site commands.
124 - *
125 - * ## OPTIONS
126 - *
127 - * [--site]
128 - * : Get site data
129 - *
130 - * [--site-info]
131 - * : If set, get site info.
132 - *
133 - *
134 - * ## EXAMPLES
135 - *
136 - * wp mainwp site --site [<websiteid>]
137 - * wp mainwp site --site-info [<websiteid>]
138 - *
139 - * ## Synopsis [--site] [--site-info] [...]
140 - *
141 - * @param array $args Function arguments.
142 - * @param array $assoc_args Function associate arguments.
143 - */
144 - public function site( $args, $assoc_args ) {
145 - MainWP_WP_CLI_Handle::handle_cli_callback( 'site', $args, $assoc_args );
146 - }
117 + /**
118 + * Site commands.
119 + *
120 + * ## OPTIONS
121 + *
122 + * [--site]
123 + * : Get site data
124 + *
125 + * [--site-info]
126 + * : If set, get site info.
127 + *
128 + *
129 + * ## EXAMPLES
130 + *
131 + * wp mainwp site --site [<websiteid>]
132 + * wp mainwp site --site-info [<websiteid>]
133 + *
134 + * ## Synopsis [--site] [--site-info] [...]
135 + *
136 + * @param array $args Function arguments.
137 + * @param array $assoc_args Function associate arguments.
138 + */
139 + public function site( $args, $assoc_args ) {
140 + MainWP_WP_CLI_Handle::handle_cli_callback( 'site', $args, $assoc_args );
141 + }
147 142
143 + /**
144 + * Updates commands.
145 + *
146 + * ## OPTIONS
147 + *
148 + * [--available-updates]
149 + * : Get available updates
150 + *
151 + * [--ignored-plugins-updates]
152 + * : If set, get ignored plugins updates.
153 + *
154 + *
155 + * ## EXAMPLES
156 + *
157 + * wp mainwp updates --available-updates
158 + * wp mainwp updates --ignored-plugins-updates
159 + *
160 + * ## Synopsis [--available-updates] [--ignored-plugins-updates] [...]
161 + *
162 + * @param array $args Function arguments.
163 + * @param array $assoc_args Function associate arguments.
164 + */
165 + public function updates( $args, $assoc_args ) {
166 + MainWP_WP_CLI_Handle::handle_cli_callback( 'updates', $args, $assoc_args );
167 + }
148 168
149 - /**
150 - * Updates commands.
151 - *
152 - * ## OPTIONS
153 - *
154 - * [--available-updates <websiteid>]
155 - * : Get available updates
156 - *
157 - * [--ignored-plugins-updates <websiteid>]
158 - * : If set, get ignored plugins updates.
159 - *
160 - * [--site-ignored-plugins-updates <websiteid>]
161 - * : If set, get ignored plugins updates of site.
162 - *
163 - * [--ignored-themes-updates <websiteid>]
164 - * : If set, get ignored themes updates.
165 - *
166 - * [--site-ignored-themes-updates <websiteid>]
167 - * : If set, get ignored themes updates of site.
168 - *
169 - * [--ignore-updates --type=[type] --slug=[slug]]
170 - * : If set, ignored updates.
171 - *
172 - * [--ignore-update <websiteid> --type=[type] --slug=[slug]]
173 - * : If set, ignored update.
174 - *
175 - * [--unignore-updates --type=[type] --slug=[slug]]
176 - * : If set, unignore updates.
177 - *
178 - * [--unignore-update <websiteid> --type=[type] --slug=[slug]]
179 - * : If set, unignore update.
180 - *
181 - *
182 - * ## EXAMPLES
183 - *
184 - * wp mainwp updates --available-updates
185 - * wp mainwp updates --ignored-plugins-updates
186 - * wp mainwp updates --site-ignored-plugins-updates 3
187 - *
188 - * ## Synopsis [--available-updates] [--ignored-plugins-updates] [--site-ignored-plugins-updates] [...]
189 - *
190 - * @param array $args Function arguments.
191 - * @param array $assoc_args Function associate arguments.
192 - */
193 - public function updates( $args, $assoc_args ) {
194 - MainWP_WP_CLI_Handle::handle_cli_callback( 'updates', $args, $assoc_args );
195 - }
169 + /**
170 + * Sync Data with Child Sites
171 + *
172 + * ## OPTIONS
173 + *
174 + * [<websiteid>]
175 + * : The id (or ids, comma separated) of the child sites that need to be synced.
176 + *
177 + * [--all]
178 + * : If set, all child sites will be synced.
179 + *
180 + * ## EXAMPLES
181 + *
182 + * wp mainwp sync 2,5
183 + * wp mainwp sync --all
184 + *
185 + * ## Synopsis [<websiteid>] [--all]
186 + *
187 + * @param array $args Function arguments.
188 + * @param array $assoc_args Function associate arguments.
189 + *
190 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
191 + * @uses \MainWP\Dashboard\MainWP_DB::query()
192 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
193 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
194 + * @uses \MainWP\Dashboard\MainWP_DB::free_result()
195 + * @uses \MainWP\Dashboard\MainWP_Sync::sync_site()
196 + */
197 + public function sync( $args, $assoc_args ) {
198 + $sites = array();
199 + if ( count( $args ) > 0 ) {
200 + $args_exploded = explode( ',', $args[0] );
201 + foreach ( $args_exploded as $arg ) {
202 + if ( ! is_numeric( trim( $arg ) ) ) {
203 + \WP_CLI::error( 'Child site ids should be numeric.' );
204 + }
196 205
197 - /**
198 - * Sync Data with Child Sites
199 - *
200 - * ## OPTIONS
201 - *
202 - * [<websiteid>]
203 - * : The id (or ids, comma separated) of the child sites that need to be synced.
204 - *
205 - * [--all]
206 - * : If set, all child sites will be synced.
207 - *
208 - * ## EXAMPLES
209 - *
210 - * wp mainwp sync 2,5
211 - * wp mainwp sync --all
212 - *
213 - * ## Synopsis [<websiteid>] [--all]
214 - *
215 - * @param array $args Function arguments.
216 - * @param array $assoc_args Function associate arguments.
217 - *
218 - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
219 - * @uses \MainWP\Dashboard\MainWP_DB::query()
220 - * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
221 - * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
222 - * @uses \MainWP\Dashboard\MainWP_DB::free_result()
223 - * @uses \MainWP\Dashboard\MainWP_Sync::sync_site()
224 - */
225 - public function sync( $args, $assoc_args ) {
226 - $sites = array();
227 - if ( ! empty( $args ) ) {
228 - $args_exploded = explode( ',', $args[0] );
229 - foreach ( $args_exploded as $arg ) {
230 - if ( ! is_numeric( trim( $arg ) ) ) {
231 - \WP_CLI::error( 'Child site ids should be numeric.' );
232 - }
206 + $sites[] = trim( $arg );
207 + }
208 + }
233 209
234 - $sites[] = trim( $arg );
235 - }
236 - }
210 + if ( ( count( $sites ) == 0 ) && ( ! isset( $assoc_args['all'] ) ) ) {
211 + \WP_CLI::error( 'Please specify one or more child sites, or use --all.' );
212 + }
213 + MainWP_WP_CLI_Handle::handle_sync_sites( $args, $assoc_args );
214 + }
237 215
238 - if ( is_array( $assoc_args ) && isset( $assoc_args['all'] ) ) {
239 - update_option( 'mainwp_last_synced_all_sites', time() );
240 - }
216 + /**
217 + * Reconnect with Child Sites
218 + *
219 + * ## OPTIONS
220 + *
221 + * [<websiteid>]
222 + * : The id (or ids, comma separated) of the child sites that need to be reconnect.
223 + *
224 + * ## EXAMPLES
225 + *
226 + * wp mainwp reconnect 2,5
227 + *
228 + * ## Synopsis [<websiteid>]
229 + *
230 + * @param array $args Function arguments.
231 + * @param array $assoc_args Function associate arguments.
232 + *
233 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
234 + * @uses \MainWP\Dashboard\MainWP_DB::query()
235 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
236 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
237 + * @uses \MainWP\Dashboard\MainWP_DB::free_result()
238 + * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::m_reconnect_site()
239 + */
240 + public function reconnect( $args, $assoc_args ) {
241 + $sites = array();
242 + if ( 0 < count( $args ) ) {
243 + $args_exploded = explode( ',', $args[0] );
244 + foreach ( $args_exploded as $arg ) {
245 + if ( ! is_numeric( trim( $arg ) ) ) {
246 + \WP_CLI::error( 'Child site ids should be numeric.' );
247 + }
241 248
242 - if ( empty( $sites ) && ( ! isset( $assoc_args['all'] ) ) ) {
243 - \WP_CLI::error( 'Please specify one or more child sites, or use --all.' );
244 - }
245 - MainWP_WP_CLI_Handle::handle_sync_sites( $args, $assoc_args );
246 - }
249 + $sites[] = trim( $arg );
250 + }
251 + }
247 252
248 - /**
249 - * Reconnect with Child Sites
250 - *
251 - * ## OPTIONS
252 - *
253 - * [<websiteid>]
254 - * : The id (or ids, comma separated) of the child sites that need to be reconnect.
255 - *
256 - * ## EXAMPLES
257 - *
258 - * wp mainwp reconnect 2,5
259 - *
260 - * ## Synopsis [<websiteid>]
261 - *
262 - * @param array $args Function arguments.
263 - * @param array $assoc_args Function associate arguments.
264 - *
265 - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
266 - * @uses \MainWP\Dashboard\MainWP_DB::query()
267 - * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
268 - * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
269 - * @uses \MainWP\Dashboard\MainWP_DB::free_result()
270 - * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::m_reconnect_site()
271 - */
272 - public function reconnect( $args, $assoc_args ) { //phpcs:ignore -- NOSONAR - complex.
273 - $sites = array();
274 - if ( ! empty( $args ) ) {
275 - $args_exploded = explode( ',', $args[0] );
276 - foreach ( $args_exploded as $arg ) {
277 - if ( ! is_numeric( trim( $arg ) ) ) {
278 - \WP_CLI::error( 'Child site ids should be numeric.' );
279 - }
253 + if ( count( $sites ) == 0 ) {
254 + \WP_CLI::error( 'Please specify one or more child sites.' );
255 + }
280 256
281 - $sites[] = trim( $arg );
282 - }
283 - }
257 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
258 + \WP_CLI::line( 'Reconnect started' );
259 + $warnings = 0;
260 + $errors = 0;
261 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
262 + if ( ( 0 < count( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
263 + continue;
264 + }
265 + \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
266 + try {
267 + if ( MainWP_Manage_Sites_View::m_reconnect_site( $website ) ) {
268 + \WP_CLI::success( ' Reconnected successfully' );
269 + } else {
270 + \WP_CLI::warning( ' Reconnect failed' );
271 + $warnings++;
272 + }
273 + } catch ( \Exception $e ) {
274 + \WP_CLI::error( ' Reconnect failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
275 + $errors++;
276 + }
277 + }
278 + MainWP_DB::free_result( $websites );
279 + if ( 0 < $errors ) {
280 + \WP_CLI::error( 'Reconnect completed with errors' );
281 + } elseif ( 0 < $warnings ) {
282 + \WP_CLI::warning( 'Reconnect completed with warnings' );
283 + } else {
284 + \WP_CLI::success( 'Reconnect completed' );
285 + }
286 + }
284 287
285 - if ( empty( $sites ) ) {
286 - \WP_CLI::error( 'Please specify one or more child sites.' );
287 - }
288 288
289 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
290 - \WP_CLI::line( 'Reconnect started' );
291 - $warnings = 0;
292 - $errors = 0;
293 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
294 - if ( ( ! empty( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
295 - continue;
296 - }
297 - \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
298 - try {
299 - if ( MainWP_Manage_Sites_View::m_reconnect_site( $website ) ) {
300 - \WP_CLI::success( ' Reconnected successfully' );
301 - } else {
302 - \WP_CLI::warning( ' Reconnect failed' );
303 - ++$warnings;
304 - }
305 - } catch ( \Exception $e ) {
306 - \WP_CLI::error( ' Reconnect failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
307 - ++$errors;
308 - }
309 - }
310 - MainWP_DB::free_result( $websites );
311 - if ( 0 < $errors ) {
312 - \WP_CLI::error( 'Reconnect completed with errors' );
313 - } elseif ( 0 < $warnings ) {
314 - \WP_CLI::warning( 'Reconnect completed with warnings' );
315 - } else {
316 - \WP_CLI::success( 'Reconnect completed' );
317 - }
318 - }
289 + /**
290 + * List information about plugin updates
291 + *
292 + * ## OPTIONS
293 + *
294 + * [<websiteid>]
295 + * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
296 + *
297 + * [--list]
298 + * : Get a list of plugins with available updates
299 + *
300 + * [--list-all]
301 + * : List all plugins
302 + *
303 + * [--upgrade=<pluginslug>]
304 + * : Update the plugin slugs
305 + *
306 + * [--upgrade-all]
307 + * : Update all plugins
308 + *
309 + * ## EXAMPLES
310 + *
311 + * wp mainwp plugin 2,5 --list
312 + * wp mainwp plugin --list
313 + * wp mainwp plugin 2,5 --list-all
314 + * wp mainwp plugin 2,5 --upgrade-all
315 + * wp mainwp plugin 2,5 --upgrade=mainwpchild
316 + *
317 + * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<pluginslug>] [--upgrade-all]
318 + *
319 + * @param array $args Function arguments.
320 + * @param array $assoc_args Function associate arguments.
321 + *
322 + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
323 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
324 + * @uses \MainWP\Dashboard\MainWP_DB::query()
325 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
326 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
327 + * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
328 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
329 + */
330 + public function plugin( $args, $assoc_args ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
331 + $sites = array();
332 + if ( 0 < count( $args ) ) {
333 + $args_exploded = explode( ',', $args[0] );
334 + foreach ( $args_exploded as $arg ) {
335 + if ( ! is_numeric( trim( $arg ) ) ) {
336 + \WP_CLI::error( 'Child site IDs should be numeric.' );
337 + }
319 338
339 + $sites[] = trim( $arg );
340 + }
341 + }
320 342
321 - /**
322 - * List information about plugin updates
323 - *
324 - * ## OPTIONS
325 - *
326 - * [<websiteid>]
327 - * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
328 - *
329 - * [--list]
330 - * : Get a list of plugins with available updates
331 - *
332 - * [--list-all]
333 - * : List all plugins
334 - *
335 - * [--upgrade=<pluginslug>]
336 - * : Update the plugin slugs
337 - *
338 - * [--upgrade-all]
339 - * : Update all plugins
340 - *
341 - * ## EXAMPLES
342 - *
343 - * wp mainwp plugin 2,5 --list
344 - * wp mainwp plugin --list
345 - * wp mainwp plugin 2,5 --list-all
346 - * wp mainwp plugin 2,5 --upgrade-all
347 - * wp mainwp plugin 2,5 --upgrade=mainwpchild
348 - *
349 - * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<pluginslug>] [--upgrade-all]
350 - *
351 - * @param array $args Function arguments.
352 - * @param array $assoc_args Function associate arguments.
353 - *
354 - * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
355 - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
356 - * @uses \MainWP\Dashboard\MainWP_DB::query()
357 - * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
358 - * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
359 - * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
360 - * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
361 - */
362 - public function plugin( $args, $assoc_args ) { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated.
363 - $sites = array();
364 - if ( ! empty( $args ) ) {
365 - $args_exploded = explode( ',', $args[0] );
366 - foreach ( $args_exploded as $arg ) {
367 - if ( ! is_numeric( trim( $arg ) ) ) {
368 - \WP_CLI::error( 'Child site IDs should be numeric.' );
369 - }
343 + if ( isset( $assoc_args['list'] ) ) {
344 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
345 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
346 + $websites_to_upgrade = array();
347 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
348 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
349 + continue;
350 + }
370 351
371 - $sites[] = trim( $arg );
372 - }
373 - }
352 + $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
374 353
375 - if ( isset( $assoc_args['list'] ) ) {
376 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
377 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
378 - $websites_to_upgrade = array();
379 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
380 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
381 - continue;
382 - }
354 + if ( is_array( $plugin_upgrades ) ) {
355 + $ignored_plugins = json_decode( $website->ignored_plugins, true );
356 + if ( is_array( $ignored_plugins ) ) {
357 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
358 + }
383 359
384 - $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
360 + $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
361 + if ( is_array( $ignored_plugins ) ) {
362 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
363 + }
385 364
386 - if ( is_array( $plugin_upgrades ) ) {
387 - $ignored_plugins = json_decode( $website->ignored_plugins, true );
388 - if ( is_array( $ignored_plugins ) ) {
389 - $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
365 + $tmp = array();
366 + foreach ( $plugin_upgrades as $plugin_upgrade ) {
367 + $tmp[] = array(
368 + 'name' => $plugin_upgrade['update']['slug'],
369 + 'version' => $plugin_upgrade['Version'],
370 + 'new_version' => $plugin_upgrade['update']['new_version'],
371 + );
372 + }
373 + $websites_to_upgrade[] = array(
374 + 'id' => $website->id,
375 + 'name' => $website->name,
376 + 'plugins' => $tmp,
377 + );
378 + }
379 + }
390 380
391 - }
381 + $idLength = strlen( 'id' );
382 + $nameLength = strlen( 'name' );
383 + $pluginLength = strlen( 'plugin' );
384 + $oldVersionLength = strlen( 'version' );
385 + $newVersionLength = strlen( 'new version' );
386 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
387 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
388 + $idLength = strlen( $website_to_upgrade['id'] );
389 + }
390 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
391 + $nameLength = strlen( $website_to_upgrade['name'] );
392 + }
392 393
393 - $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
394 - if ( is_array( $ignored_plugins ) ) {
395 - $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
394 + foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
395 + if ( $pluginLength < strlen( $plugin_to_upgrade['name'] ) ) {
396 + $pluginLength = strlen( $plugin_to_upgrade['name'] );
397 + }
398 + if ( $oldVersionLength < strlen( $plugin_to_upgrade['version'] ) ) {
399 + $oldVersionLength = strlen( $plugin_to_upgrade['version'] );
400 + }
401 + if ( $newVersionLength < strlen( $plugin_to_upgrade['new_version'] ) ) {
402 + $newVersionLength = strlen( $plugin_to_upgrade['new_version'] );
403 + }
404 + }
405 + }
396 406
397 - }
407 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
408 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'plugin', 'version', 'new version' ) );
409 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
398 410
399 - $tmp = array();
400 - foreach ( $plugin_upgrades as $plugin_upgrade ) {
401 - $tmp[] = array(
402 - 'name' => $plugin_upgrade['update']['slug'],
403 - 'version' => $plugin_upgrade['Version'],
404 - 'new_version' => $plugin_upgrade['update']['new_version'],
405 - );
406 - }
407 - $websites_to_upgrade[] = array(
408 - 'id' => $website->id,
409 - 'name' => $website->name,
410 - 'plugins' => $tmp,
411 - );
412 - }
413 - }
411 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
412 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
413 + $idLength = strlen( $website_to_upgrade['id'] );
414 + }
415 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
416 + $nameLength = strlen( $website_to_upgrade['name'] );
417 + }
414 418
415 - $idLength = strlen( 'id' );
416 - $nameLength = strlen( 'name' );
417 - $pluginLength = strlen( 'plugin' );
418 - $oldVersionLength = strlen( 'version' );
419 - $newVersionLength = strlen( 'new version' );
420 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
421 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
422 - $idLength = strlen( $website_to_upgrade['id'] );
423 - }
424 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
425 - $nameLength = strlen( $website_to_upgrade['name'] );
426 - }
419 + $i = 0;
420 + foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
421 + if ( 0 == $i ) {
422 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', $website_to_upgrade['id'], $website_to_upgrade['name'], $plugin_to_upgrade['name'], $plugin_to_upgrade['version'], $plugin_to_upgrade['new_version'] ) );
423 + } else {
424 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', '', '', $plugin_to_upgrade['name'], $plugin_to_upgrade['version'], $plugin_to_upgrade['new_version'] ) );
425 + }
426 + $i++;
427 + }
428 + }
427 429
428 - foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
429 - if ( $pluginLength < strlen( $plugin_to_upgrade['name'] ) ) {
430 - $pluginLength = strlen( $plugin_to_upgrade['name'] );
431 - }
432 - if ( $oldVersionLength < strlen( $plugin_to_upgrade['version'] ) ) {
433 - $oldVersionLength = strlen( $plugin_to_upgrade['version'] );
434 - }
435 - if ( $newVersionLength < strlen( $plugin_to_upgrade['new_version'] ) ) {
436 - $newVersionLength = strlen( $plugin_to_upgrade['new_version'] );
437 - }
438 - }
439 - }
430 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
431 + } elseif ( isset( $assoc_args['list-all'] ) ) {
432 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
433 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
434 + $websites_to_list = array();
435 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
436 + if ( ( 0 < count( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
437 + continue;
438 + }
440 439
441 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
442 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'plugin', 'version', 'new version' ) );
443 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
440 + $plugins_to_list = json_decode( $website->plugins, true );
441 + if ( is_array( $plugins_to_list ) ) {
442 + $tmp = array();
443 + foreach ( $plugins_to_list as $plugin ) {
444 + $tmp[] = array(
445 + 'name' => $plugin['slug'],
446 + 'version' => $plugin['version'],
447 + );
448 + }
449 + $websites_to_list[] = array(
450 + 'id' => $website->id,
451 + 'name' => $website->name,
452 + 'plugins' => $tmp,
453 + );
454 + }
455 + }
444 456
445 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
446 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
447 - $idLength = strlen( $website_to_upgrade['id'] );
448 - }
449 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
450 - $nameLength = strlen( $website_to_upgrade['name'] );
451 - }
457 + $idLength = strlen( 'id' );
458 + $nameLength = strlen( 'name' );
459 + $pluginLength = strlen( 'plugin' );
460 + $oldVersionLength = strlen( 'version' );
461 + foreach ( $websites_to_list as $website_item ) {
462 + if ( $idLength < strlen( $website_item['id'] ) ) {
463 + $idLength = strlen( $website_item['id'] );
464 + }
465 + if ( $nameLength < strlen( $website_item['name'] ) ) {
466 + $nameLength = strlen( $website_item['name'] );
467 + }
452 468
453 - $i = 0;
454 - foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
455 - if ( 0 === $i ) {
456 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', $website_to_upgrade['id'], $website_to_upgrade['name'], $plugin_to_upgrade['name'], $plugin_to_upgrade['version'], $plugin_to_upgrade['new_version'] ) );
457 - } else {
458 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', '', '', $plugin_to_upgrade['name'], $plugin_to_upgrade['version'], $plugin_to_upgrade['new_version'] ) );
459 - }
460 - ++$i;
461 - }
462 - }
469 + foreach ( $website_item['plugins'] as $plugin_item ) {
470 + if ( $pluginLength < strlen( $plugin_item['name'] ) ) {
471 + $pluginLength = strlen( $plugin_item['name'] );
472 + }
473 + if ( $oldVersionLength < strlen( $plugin_item['version'] ) ) {
474 + $oldVersionLength = strlen( $plugin_item['version'] );
475 + }
476 + }
477 + }
463 478
464 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
465 - } elseif ( isset( $assoc_args['list-all'] ) ) {
466 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
467 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
468 - $websites_to_list = array();
469 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
470 - if ( ( ! empty( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
471 - continue;
472 - }
479 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
480 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'plugin', 'version' ) );
481 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
473 482
474 - $plugins_to_list = json_decode( $website->plugins, true );
475 - if ( is_array( $plugins_to_list ) ) {
476 - $tmp = array();
477 - foreach ( $plugins_to_list as $plugin ) {
478 - $tmp[] = array(
479 - 'name' => $plugin['slug'],
480 - 'version' => $plugin['version'],
481 - );
482 - }
483 - $websites_to_list[] = array(
484 - 'id' => $website->id,
485 - 'name' => $website->name,
486 - 'plugins' => $tmp,
487 - );
488 - }
489 - }
483 + foreach ( $websites_to_list as $website_item ) {
484 + if ( $idLength < strlen( $website_item['id'] ) ) {
485 + $idLength = strlen( $website_item['id'] );
486 + }
487 + if ( $nameLength < strlen( $website_item['name'] ) ) {
488 + $nameLength = strlen( $website_item['name'] );
489 + }
490 490
491 - $idLength = strlen( 'id' );
492 - $nameLength = strlen( 'name' );
493 - $pluginLength = strlen( 'plugin' );
494 - $oldVersionLength = strlen( 'version' );
495 - foreach ( $websites_to_list as $website_item ) {
496 - if ( $idLength < strlen( $website_item['id'] ) ) {
497 - $idLength = strlen( $website_item['id'] );
498 - }
499 - if ( $nameLength < strlen( $website_item['name'] ) ) {
500 - $nameLength = strlen( $website_item['name'] );
501 - }
491 + $i = 0;
492 + foreach ( $website_item['plugins'] as $plugin_item ) {
493 + if ( 0 == $i ) {
494 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', $website_item['id'], $website_item['name'], $plugin_item['name'], $plugin_item['version'] ) );
495 + } else {
496 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', '', '', $plugin_item['name'], $plugin_item['version'] ) );
497 + }
498 + $i++;
499 + }
500 + }
502 501
503 - foreach ( $website_item['plugins'] as $plugin_item ) {
504 - if ( $pluginLength < strlen( $plugin_item['name'] ) ) {
505 - $pluginLength = strlen( $plugin_item['name'] );
506 - }
507 - if ( $oldVersionLength < strlen( $plugin_item['version'] ) ) {
508 - $oldVersionLength = strlen( $plugin_item['version'] );
509 - }
510 - }
511 - }
502 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
503 + } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
512 504
513 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
514 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'plugin', 'version' ) );
515 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
505 + $pluginSlugs = array();
506 + if ( isset( $assoc_args['upgrade'] ) ) {
507 + $pluginSlugs = explode( ',', $assoc_args['upgrade'] );
508 + }
516 509
517 - foreach ( $websites_to_list as $website_item ) {
518 - if ( $idLength < strlen( $website_item['id'] ) ) {
519 - $idLength = strlen( $website_item['id'] );
520 - }
521 - if ( $nameLength < strlen( $website_item['name'] ) ) {
522 - $nameLength = strlen( $website_item['name'] );
523 - }
510 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
511 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
512 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
513 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
514 + continue;
515 + }
524 516
525 - $i = 0;
526 - foreach ( $website_item['plugins'] as $plugin_item ) {
527 - if ( 0 === $i ) {
528 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', $website_item['id'], $website_item['name'], $plugin_item['name'], $plugin_item['version'] ) );
529 - } else {
530 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', '', '', $plugin_item['name'], $plugin_item['version'] ) );
531 - }
532 - ++$i;
533 - }
534 - }
517 + $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
535 518
536 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
537 - } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
519 + if ( is_array( $plugin_upgrades ) ) {
520 + $ignored_plugins = json_decode( $website->ignored_plugins, true );
521 + if ( is_array( $ignored_plugins ) ) {
522 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
523 + }
538 524
539 - $pluginSlugs = array();
540 - if ( isset( $assoc_args['upgrade'] ) ) {
541 - $pluginSlugs = explode( ',', $assoc_args['upgrade'] );
542 - }
525 + $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
526 + if ( is_array( $ignored_plugins ) ) {
527 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
528 + }
543 529
544 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
545 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
546 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
547 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
548 - continue;
549 - }
530 + $tmp = array();
531 + foreach ( $plugin_upgrades as $key => $plugin_upgrade ) {
532 + if ( ( count( $pluginSlugs ) > 0 ) && ( ! in_array( $plugin_upgrade['update']['slug'], $pluginSlugs, true ) ) ) {
533 + continue;
534 + }
550 535
551 - $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
536 + $tmp[] = $key;
537 + }
552 538
553 - if ( is_array( $plugin_upgrades ) ) {
554 - $ignored_plugins = json_decode( $website->ignored_plugins, true );
555 - if ( is_array( $ignored_plugins ) ) {
556 - $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
539 + if ( count( $tmp ) == 0 ) {
540 + \WP_CLI::line( 'No available plugin updates for ' . $website->name );
557 541
558 - }
542 + continue;
543 + }
559 544
560 - $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
561 - if ( is_array( $ignored_plugins ) ) {
562 - $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
545 + \WP_CLI::line( 'Updating ' . count( $tmp ) . ' plugins for ' . $website->name );
563 546
564 - }
547 + try {
548 + MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'plugin', implode( ',', $tmp ) );
549 + \WP_CLI::success( 'Updates completed' );
550 + } catch ( \Exception $e ) {
551 + \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
552 + if ( $e->getMesage() == 'WPERROR' ) {
553 + \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
554 + }
555 + }
556 + }
557 + }
558 + }
559 + }
565 560
566 - $tmp = array();
567 - foreach ( $plugin_upgrades as $key => $plugin_upgrade ) {
568 - if ( ! empty( $pluginSlugs ) && ( ! in_array( $plugin_upgrade['update']['slug'], $pluginSlugs, true ) ) ) {
569 - continue;
570 - }
561 + /**
562 + * List information about theme updates
563 + *
564 + * ## OPTIONS
565 + *
566 + * [<websiteid>]
567 + * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
568 + *
569 + * [--list]
570 + * : Get a list of themes with available updates
571 + *
572 + * [--list-all]
573 + * : list all themes
574 + *
575 + * [--upgrade=<theme>]
576 + * : Update the themes
577 + *
578 + * [--upgrade-all]
579 + * : Update all themes
580 + *
581 + * ## EXAMPLES
582 + *
583 + * wp mainwp theme 2,5 --list
584 + * wp mainwp theme --list
585 + * wp mainwp theme 2,5 --upgrade-all
586 + * wp mainwp theme 2,5 --upgrade=twentysixteen
587 + *
588 + * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<theme>] [--upgrade-all]
589 + *
590 + * @param array $args Function arguments.
591 + * @param array $assoc_args Function associate arguments.
592 + *
593 + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
594 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
595 + * @uses \MainWP\Dashboard\MainWP_DB::query()
596 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
597 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
598 + * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
599 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
600 + */
601 + public function theme( $args, $assoc_args ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
602 + $sites = array();
603 + if ( count( $args ) > 0 ) {
604 + $args_exploded = explode( ',', $args[0] );
605 + foreach ( $args_exploded as $arg ) {
606 + if ( ! is_numeric( trim( $arg ) ) ) {
607 + \WP_CLI::error( 'Child site IDs should be numeric.' );
608 + }
571 609
572 - $tmp[] = $key;
573 - }
610 + $sites[] = trim( $arg );
611 + }
612 + }
574 613
575 - if ( empty( $tmp ) ) {
576 - \WP_CLI::line( 'No available plugin updates for ' . $website->name );
614 + if ( isset( $assoc_args['list'] ) ) {
615 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
616 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
617 + $websites_to_upgrade = array();
618 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
619 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
620 + continue;
621 + }
577 622
578 - continue;
579 - }
623 + $theme_upgrades = json_decode( $website->theme_upgrades, true );
580 624
581 - \WP_CLI::line( 'Updating ' . count( $tmp ) . ' plugins for ' . $website->name );
625 + if ( is_array( $theme_upgrades ) ) {
626 + $ignored_themes = json_decode( $website->ignored_themes, true );
627 + if ( is_array( $ignored_themes ) ) {
628 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
629 + }
582 630
583 - try {
584 - MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'plugin', implode( ',', $tmp ) );
585 - \WP_CLI::success( 'Updates completed' );
586 - } catch ( \Exception $e ) {
587 - \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
588 - if ( $e->getMesage() === 'WPERROR' ) {
589 - \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
590 - }
591 - }
592 - }
593 - }
594 - }
595 - }
631 + $ignored_themes = json_decode( $userExtension->ignored_themes, true );
632 + if ( is_array( $ignored_themes ) ) {
633 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
634 + }
596 635
597 - /**
598 - * List information about theme updates
599 - *
600 - * ## OPTIONS
601 - *
602 - * [<websiteid>]
603 - * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
604 - *
605 - * [--list]
606 - * : Get a list of themes with available updates
607 - *
608 - * [--list-all]
609 - * : list all themes
610 - *
611 - * [--upgrade=<theme>]
612 - * : Update the themes
613 - *
614 - * [--upgrade-all]
615 - * : Update all themes
616 - *
617 - * ## EXAMPLES
618 - *
619 - * wp mainwp theme 2,5 --list
620 - * wp mainwp theme --list
621 - * wp mainwp theme 2,5 --upgrade-all
622 - * wp mainwp theme 2,5 --upgrade=twentysixteen
623 - *
624 - * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<theme>] [--upgrade-all]
625 - *
626 - * @param array $args Function arguments.
627 - * @param array $assoc_args Function associate arguments.
628 - *
629 - * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
630 - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
631 - * @uses \MainWP\Dashboard\MainWP_DB::query()
632 - * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
633 - * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
634 - * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
635 - * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
636 - */
637 - public function theme( $args, $assoc_args ) { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated.
638 - $sites = array();
639 - if ( ! empty( $args ) ) {
640 - $args_exploded = explode( ',', $args[0] );
641 - foreach ( $args_exploded as $arg ) {
642 - if ( ! is_numeric( trim( $arg ) ) ) {
643 - \WP_CLI::error( 'Child site IDs should be numeric.' );
644 - }
636 + $tmp = array();
637 + foreach ( $theme_upgrades as $theme_upgrade ) {
638 + $tmp[] = array(
639 + 'name' => $theme_upgrade['update']['theme'],
640 + 'version' => $theme_upgrade['Version'],
641 + 'new_version' => $theme_upgrade['update']['new_version'],
642 + );
643 + }
644 + $websites_to_upgrade[] = array(
645 + 'id' => $website->id,
646 + 'name' => $website->name,
647 + 'themes' => $tmp,
648 + );
649 + }
650 + }
645 651
646 - $sites[] = trim( $arg );
647 - }
648 - }
652 + $idLength = strlen( 'id' );
653 + $nameLength = strlen( 'name' );
654 + $themeLength = strlen( 'theme' );
655 + $oldVersionLength = strlen( 'version' );
656 + $newVersionLength = strlen( 'new version' );
657 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
658 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
659 + $idLength = strlen( $website_to_upgrade['id'] );
660 + }
661 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
662 + $nameLength = strlen( $website_to_upgrade['name'] );
663 + }
649 664
650 - if ( isset( $assoc_args['list'] ) ) {
651 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
652 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
653 - $websites_to_upgrade = array();
654 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
655 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
656 - continue;
657 - }
665 + foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
666 + if ( $themeLength < strlen( $theme_to_upgrade['name'] ) ) {
667 + $themeLength = strlen( $theme_to_upgrade['name'] );
668 + }
669 + if ( $oldVersionLength < strlen( $theme_to_upgrade['version'] ) ) {
670 + $oldVersionLength = strlen( $theme_to_upgrade['version'] );
671 + }
672 + if ( $newVersionLength < strlen( $theme_to_upgrade['new_version'] ) ) {
673 + $newVersionLength = strlen( $theme_to_upgrade['new_version'] );
674 + }
675 + }
676 + }
658 677
659 - $theme_upgrades = json_decode( $website->theme_upgrades, true );
678 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
679 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'theme', 'version', 'new version' ) );
680 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
660 681
661 - if ( is_array( $theme_upgrades ) ) {
662 - $ignored_themes = json_decode( $website->ignored_themes, true );
663 - if ( is_array( $ignored_themes ) ) {
664 - $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
665 - }
682 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
683 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
684 + $idLength = strlen( $website_to_upgrade['id'] );
685 + }
686 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
687 + $nameLength = strlen( $website_to_upgrade['name'] );
688 + }
666 689
667 - $ignored_themes = json_decode( $userExtension->ignored_themes, true );
668 - if ( is_array( $ignored_themes ) ) {
669 - $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
670 - }
690 + $i = 0;
691 + foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
692 + if ( 0 == $i ) {
693 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', $website_to_upgrade['id'], $website_to_upgrade['name'], $theme_to_upgrade['name'], $theme_to_upgrade['version'], $theme_to_upgrade['new_version'] ) );
694 + } else {
695 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', '', '', $theme_to_upgrade['name'], $theme_to_upgrade['version'], $theme_to_upgrade['new_version'] ) );
696 + }
697 + $i++;
698 + }
699 + }
671 700
672 - $tmp = array();
673 - foreach ( $theme_upgrades as $theme_upgrade ) {
674 - $tmp[] = array(
675 - 'name' => $theme_upgrade['update']['theme'],
676 - 'version' => $theme_upgrade['Version'],
677 - 'new_version' => $theme_upgrade['update']['new_version'],
678 - );
679 - }
680 - $websites_to_upgrade[] = array(
681 - 'id' => $website->id,
682 - 'name' => $website->name,
683 - 'themes' => $tmp,
684 - );
685 - }
686 - }
701 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
702 + } elseif ( isset( $assoc_args['list-all'] ) ) {
703 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
704 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
705 + $websites_to_upgrade = array();
706 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
707 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
708 + continue;
709 + }
710 + $theme_to_list = json_decode( $website->themes, true );
711 + if ( is_array( $theme_to_list ) ) {
712 + $tmp = array();
713 + foreach ( $theme_to_list as $theme ) {
714 + $tmp[] = array(
715 + 'name' => $theme['name'],
716 + 'active' => $theme['active'] ? 'yes' : '',
717 + 'version' => $theme['version'],
718 + );
719 + }
720 + $websites_to_upgrade[] = array(
721 + 'id' => $website->id,
722 + 'name' => $website->name,
723 + 'themes' => $tmp,
724 + );
725 + }
726 + }
687 727
688 - $idLength = strlen( 'id' );
689 - $nameLength = strlen( 'name' );
690 - $themeLength = strlen( 'theme' );
691 - $oldVersionLength = strlen( 'version' );
692 - $newVersionLength = strlen( 'new version' );
693 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
694 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
695 - $idLength = strlen( $website_to_upgrade['id'] );
696 - }
697 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
698 - $nameLength = strlen( $website_to_upgrade['name'] );
699 - }
728 + $idLength = strlen( 'id' );
729 + $nameLength = strlen( 'name' );
730 + $themeLength = strlen( 'theme' );
731 + $oldVersionLength = strlen( 'version' );
732 + $activeLength = strlen( 'active' );
733 + foreach ( $websites_to_upgrade as $website_item ) {
734 + if ( $idLength < strlen( $website_item['id'] ) ) {
735 + $idLength = strlen( $website_item['id'] );
736 + }
737 + if ( $nameLength < strlen( $website_item['name'] ) ) {
738 + $nameLength = strlen( $website_item['name'] );
739 + }
700 740
701 - foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
702 - if ( $themeLength < strlen( $theme_to_upgrade['name'] ) ) {
703 - $themeLength = strlen( $theme_to_upgrade['name'] );
704 - }
705 - if ( $oldVersionLength < strlen( $theme_to_upgrade['version'] ) ) {
706 - $oldVersionLength = strlen( $theme_to_upgrade['version'] );
707 - }
708 - if ( $newVersionLength < strlen( $theme_to_upgrade['new_version'] ) ) {
709 - $newVersionLength = strlen( $theme_to_upgrade['new_version'] );
710 - }
711 - }
712 - }
741 + foreach ( $website_item['themes'] as $theme_item ) {
742 + if ( $themeLength < strlen( $theme_item['name'] ) ) {
743 + $themeLength = strlen( $theme_item['name'] );
744 + }
745 + if ( $oldVersionLength < strlen( $theme_item['version'] ) ) {
746 + $oldVersionLength = strlen( $theme_item['version'] );
747 + }
748 + }
749 + }
713 750
714 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
715 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'theme', 'version', 'new version' ) );
716 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
751 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
752 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'theme', 'active', 'version' ) );
753 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
717 754
718 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
719 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
720 - $idLength = strlen( $website_to_upgrade['id'] );
721 - }
722 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
723 - $nameLength = strlen( $website_to_upgrade['name'] );
724 - }
755 + foreach ( $websites_to_upgrade as $website_item ) {
756 + if ( $idLength < strlen( $website_item['id'] ) ) {
757 + $idLength = strlen( $website_item['id'] );
758 + }
759 + if ( $nameLength < strlen( $website_item['name'] ) ) {
760 + $nameLength = strlen( $website_item['name'] );
761 + }
725 762
726 - $i = 0;
727 - foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
728 - if ( 0 === $i ) {
729 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', $website_to_upgrade['id'], $website_to_upgrade['name'], $theme_to_upgrade['name'], $theme_to_upgrade['version'], $theme_to_upgrade['new_version'] ) );
730 - } else {
731 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', '', '', $theme_to_upgrade['name'], $theme_to_upgrade['version'], $theme_to_upgrade['new_version'] ) );
732 - }
733 - ++$i;
734 - }
735 - }
763 + $i = 0;
764 + foreach ( $website_item['themes'] as $theme_item ) {
765 + if ( 0 == $i ) {
766 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', $website_item['id'], $website_item['name'], $theme_item['name'], $theme_item['active'], $theme_item['version'] ) );
767 + } else {
768 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', '', '', $theme_item['name'], $theme_item['active'], $theme_item['version'] ) );
769 + }
770 + $i++;
771 + }
772 + }
736 773
737 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
738 - } elseif ( isset( $assoc_args['list-all'] ) ) {
739 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
740 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
741 - $websites_to_upgrade = array();
742 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
743 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
744 - continue;
745 - }
746 - $theme_to_list = json_decode( $website->themes, true );
747 - if ( is_array( $theme_to_list ) ) {
748 - $tmp = array();
749 - foreach ( $theme_to_list as $theme ) {
750 - $tmp[] = array(
751 - 'name' => $theme['name'],
752 - 'active' => $theme['active'] ? 'yes' : '',
753 - 'version' => $theme['version'],
754 - );
755 - }
756 - $websites_to_upgrade[] = array(
757 - 'id' => $website->id,
758 - 'name' => $website->name,
759 - 'themes' => $tmp,
760 - );
761 - }
762 - }
774 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
775 + } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
763 776
764 - $idLength = strlen( 'id' );
765 - $nameLength = strlen( 'name' );
766 - $themeLength = strlen( 'theme' );
767 - $oldVersionLength = strlen( 'version' );
768 - $activeLength = strlen( 'active' );
769 - foreach ( $websites_to_upgrade as $website_item ) {
770 - if ( $idLength < strlen( $website_item['id'] ) ) {
771 - $idLength = strlen( $website_item['id'] );
772 - }
773 - if ( $nameLength < strlen( $website_item['name'] ) ) {
774 - $nameLength = strlen( $website_item['name'] );
775 - }
777 + $themeSlugs = array();
778 + if ( isset( $assoc_args['upgrade'] ) ) {
779 + $themeSlugs = explode( ',', $assoc_args['upgrade'] );
780 + }
776 781
777 - foreach ( $website_item['themes'] as $theme_item ) {
778 - if ( $themeLength < strlen( $theme_item['name'] ) ) {
779 - $themeLength = strlen( $theme_item['name'] );
780 - }
781 - if ( $oldVersionLength < strlen( $theme_item['version'] ) ) {
782 - $oldVersionLength = strlen( $theme_item['version'] );
783 - }
784 - }
785 - }
782 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
783 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
784 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
785 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
786 + continue;
787 + }
786 788
787 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
788 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'theme', 'active', 'version' ) );
789 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
789 + $theme_upgrades = json_decode( $website->theme_upgrades, true );
790 790
791 - foreach ( $websites_to_upgrade as $website_item ) {
792 - if ( $idLength < strlen( $website_item['id'] ) ) {
793 - $idLength = strlen( $website_item['id'] );
794 - }
795 - if ( $nameLength < strlen( $website_item['name'] ) ) {
796 - $nameLength = strlen( $website_item['name'] );
797 - }
791 + if ( is_array( $theme_upgrades ) ) {
792 + $ignored_themes = json_decode( $website->ignored_themes, true );
793 + if ( is_array( $ignored_themes ) ) {
794 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
795 + }
798 796
799 - $i = 0;
800 - foreach ( $website_item['themes'] as $theme_item ) {
801 - if ( 0 === $i ) {
802 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', $website_item['id'], $website_item['name'], $theme_item['name'], $theme_item['active'], $theme_item['version'] ) );
803 - } else {
804 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', '', '', $theme_item['name'], $theme_item['active'], $theme_item['version'] ) );
805 - }
806 - ++$i;
807 - }
808 - }
797 + $ignored_themes = json_decode( $userExtension->ignored_themes, true );
798 + if ( is_array( $ignored_themes ) ) {
799 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
800 + }
809 801
810 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
811 - } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
802 + $tmp = array();
803 + foreach ( $theme_upgrades as $key => $theme_upgrade ) {
804 + if ( ( count( $themeSlugs ) > 0 ) && ( ! in_array( $theme_upgrade['update']['slug'], $themeSlugs, true ) ) ) {
805 + continue;
806 + }
812 807
813 - $themeSlugs = array();
814 - if ( isset( $assoc_args['upgrade'] ) ) {
815 - $themeSlugs = explode( ',', $assoc_args['upgrade'] );
816 - }
808 + $tmp[] = $key;
809 + }
817 810
818 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
819 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
820 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
821 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
822 - continue;
823 - }
811 + if ( count( $tmp ) == 0 ) {
812 + \WP_CLI::line( 'No available theme updates for ' . $website->name );
824 813
825 - $theme_upgrades = json_decode( $website->theme_upgrades, true );
814 + continue;
815 + }
826 816
827 - if ( is_array( $theme_upgrades ) ) {
828 - $ignored_themes = json_decode( $website->ignored_themes, true );
829 - if ( is_array( $ignored_themes ) ) {
830 - $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
831 - }
817 + \WP_CLI::line( 'Updating ' . count( $tmp ) . ' themes for ' . $website->name );
832 818
833 - $ignored_themes = json_decode( $userExtension->ignored_themes, true );
834 - if ( is_array( $ignored_themes ) ) {
835 - $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
836 - }
819 + try {
820 + MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'theme', implode( ',', $tmp ) );
821 + \WP_CLI::success( 'Updates completed' );
822 + } catch ( \Exception $e ) {
823 + \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
824 + if ( $e->getMesage() == 'WPERROR' ) {
825 + \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
826 + }
827 + }
828 + }
829 + }
830 + }
831 + }
837 832
838 - $tmp = array();
839 - foreach ( $theme_upgrades as $key => $theme_upgrade ) {
840 - if ( ! empty( $themeSlugs ) && ( ! in_array( $theme_upgrade['update']['slug'], $themeSlugs, true ) ) ) {
841 - continue;
842 - }
843 -
844 - $tmp[] = $key;
845 - }
846 -
847 - if ( empty( $tmp ) ) {
848 - \WP_CLI::line( 'No available theme updates for ' . $website->name );
849 -
850 - continue;
851 - }
852 -
853 - \WP_CLI::line( 'Updating ' . count( $tmp ) . ' themes for ' . $website->name );
854 -
855 - try {
856 - MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'theme', implode( ',', $tmp ) );
857 - \WP_CLI::success( 'Updates completed' );
858 - } catch ( \Exception $e ) {
859 - \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
860 - if ( $e->getMesage() === 'WPERROR' ) {
861 - \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
862 - }
863 - }
864 - }
865 - }
866 - }
867 - }
868 833 }