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