PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.3
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 +750 -753 5.14.3 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,838 +22,835 @@
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( self::class, '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', self::class );
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 + * [--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 ) {
75 75
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 - }
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 + }
82 82
83 - $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 ) );
84 84
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 );
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 );
104 104
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+', '', '', '', '' ) );
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+', '', '', '', '' ) );
108 108
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 - }
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 + }
112 112
113 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
114 - MainWP_DB::free_result( $websites );
115 - }
113 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . 's+', '', '', '', '' ) );
114 + MainWP_DB::free_result( $websites );
115 + }
116 116
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 - }
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 + }
142 142
143 143
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 - }
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 + }
191 191
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 - }
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 ( count( $args ) > 0 ) {
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 + }
228 228
229 - $sites[] = trim( $arg );
230 - }
231 - }
229 + $sites[] = trim( $arg );
230 + }
231 + }
232 232
233 - if ( is_array( $assoc_args ) && isset( $assoc_args['all'] ) ) {
234 - update_option( 'mainwp_last_synced_all_sites', time() );
235 - }
233 + if ( ( count( $sites ) == 0 ) && ( ! isset( $assoc_args['all'] ) ) ) {
234 + \WP_CLI::error( 'Please specify one or more child sites, or use --all.' );
235 + }
236 + MainWP_WP_CLI_Handle::handle_sync_sites( $args, $assoc_args );
237 + }
236 238
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 - }
239 + /**
240 + * Reconnect with Child Sites
241 + *
242 + * ## OPTIONS
243 + *
244 + * [<websiteid>]
245 + * : The id (or ids, comma separated) of the child sites that need to be reconnect.
246 + *
247 + * ## EXAMPLES
248 + *
249 + * wp mainwp reconnect 2,5
250 + *
251 + * ## Synopsis [<websiteid>]
252 + *
253 + * @param array $args Function arguments.
254 + * @param array $assoc_args Function associate arguments.
255 + *
256 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
257 + * @uses \MainWP\Dashboard\MainWP_DB::query()
258 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
259 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
260 + * @uses \MainWP\Dashboard\MainWP_DB::free_result()
261 + * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::m_reconnect_site()
262 + */
263 + public function reconnect( $args, $assoc_args ) {
264 + $sites = array();
265 + if ( 0 < count( $args ) ) {
266 + $args_exploded = explode( ',', $args[0] );
267 + foreach ( $args_exploded as $arg ) {
268 + if ( ! is_numeric( trim( $arg ) ) ) {
269 + \WP_CLI::error( 'Child site ids should be numeric.' );
270 + }
242 271
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 - }
272 + $sites[] = trim( $arg );
273 + }
274 + }
275 275
276 - $sites[] = trim( $arg );
277 - }
278 - }
276 + if ( count( $sites ) == 0 ) {
277 + \WP_CLI::error( 'Please specify one or more child sites.' );
278 + }
279 279
280 - if ( empty( $sites ) ) {
281 - \WP_CLI::error( 'Please specify one or more child sites.' );
282 - }
280 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
281 + \WP_CLI::line( 'Reconnect started' );
282 + $warnings = 0;
283 + $errors = 0;
284 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
285 + if ( ( 0 < count( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
286 + continue;
287 + }
288 + \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
289 + try {
290 + if ( MainWP_Manage_Sites_View::m_reconnect_site( $website ) ) {
291 + \WP_CLI::success( ' Reconnected successfully' );
292 + } else {
293 + \WP_CLI::warning( ' Reconnect failed' );
294 + $warnings++;
295 + }
296 + } catch ( \Exception $e ) {
297 + \WP_CLI::error( ' Reconnect failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
298 + $errors++;
299 + }
300 + }
301 + MainWP_DB::free_result( $websites );
302 + if ( 0 < $errors ) {
303 + \WP_CLI::error( 'Reconnect completed with errors' );
304 + } elseif ( 0 < $warnings ) {
305 + \WP_CLI::warning( 'Reconnect completed with warnings' );
306 + } else {
307 + \WP_CLI::success( 'Reconnect completed' );
308 + }
309 + }
283 310
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 - }
314 311
312 + /**
313 + * List information about plugin updates
314 + *
315 + * ## OPTIONS
316 + *
317 + * [<websiteid>]
318 + * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
319 + *
320 + * [--list]
321 + * : Get a list of plugins with available updates
322 + *
323 + * [--list-all]
324 + * : List all plugins
325 + *
326 + * [--upgrade=<pluginslug>]
327 + * : Update the plugin slugs
328 + *
329 + * [--upgrade-all]
330 + * : Update all plugins
331 + *
332 + * ## EXAMPLES
333 + *
334 + * wp mainwp plugin 2,5 --list
335 + * wp mainwp plugin --list
336 + * wp mainwp plugin 2,5 --list-all
337 + * wp mainwp plugin 2,5 --upgrade-all
338 + * wp mainwp plugin 2,5 --upgrade=mainwpchild
339 + *
340 + * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<pluginslug>] [--upgrade-all]
341 + *
342 + * @param array $args Function arguments.
343 + * @param array $assoc_args Function associate arguments.
344 + *
345 + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
346 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
347 + * @uses \MainWP\Dashboard\MainWP_DB::query()
348 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
349 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
350 + * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
351 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
352 + */
353 + public function plugin( $args, $assoc_args ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
354 + $sites = array();
355 + if ( 0 < count( $args ) ) {
356 + $args_exploded = explode( ',', $args[0] );
357 + foreach ( $args_exploded as $arg ) {
358 + if ( ! is_numeric( trim( $arg ) ) ) {
359 + \WP_CLI::error( 'Child site IDs should be numeric.' );
360 + }
315 361
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 - }
362 + $sites[] = trim( $arg );
363 + }
364 + }
365 365
366 - $sites[] = trim( $arg );
367 - }
368 - }
366 + if ( isset( $assoc_args['list'] ) ) {
367 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
368 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
369 + $websites_to_upgrade = array();
370 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
371 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
372 + continue;
373 + }
369 374
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 - }
375 + $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
378 376
379 - $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
377 + if ( is_array( $plugin_upgrades ) ) {
378 + $ignored_plugins = json_decode( $website->ignored_plugins, true );
379 + if ( is_array( $ignored_plugins ) ) {
380 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
381 + }
380 382
381 - if ( is_array( $plugin_upgrades ) ) {
382 - $ignored_plugins = json_decode( $website->ignored_plugins, true );
383 - if ( is_array( $ignored_plugins ) ) {
384 - $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
385 - }
383 + $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
384 + if ( is_array( $ignored_plugins ) ) {
385 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
386 + }
386 387
387 - $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
388 - if ( is_array( $ignored_plugins ) ) {
389 - $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
390 - }
388 + $tmp = array();
389 + foreach ( $plugin_upgrades as $plugin_upgrade ) {
390 + $tmp[] = array(
391 + 'name' => $plugin_upgrade['update']['slug'],
392 + 'version' => $plugin_upgrade['Version'],
393 + 'new_version' => $plugin_upgrade['update']['new_version'],
394 + );
395 + }
396 + $websites_to_upgrade[] = array(
397 + 'id' => $website->id,
398 + 'name' => $website->name,
399 + 'plugins' => $tmp,
400 + );
401 + }
402 + }
391 403
392 - $tmp = array();
393 - foreach ( $plugin_upgrades as $plugin_upgrade ) {
394 - $tmp[] = array(
395 - 'name' => $plugin_upgrade['update']['slug'],
396 - 'version' => $plugin_upgrade['Version'],
397 - 'new_version' => $plugin_upgrade['update']['new_version'],
398 - );
399 - }
400 - $websites_to_upgrade[] = array(
401 - 'id' => $website->id,
402 - 'name' => $website->name,
403 - 'plugins' => $tmp,
404 - );
405 - }
406 - }
404 + $idLength = strlen( 'id' );
405 + $nameLength = strlen( 'name' );
406 + $pluginLength = strlen( 'plugin' );
407 + $oldVersionLength = strlen( 'version' );
408 + $newVersionLength = strlen( 'new version' );
409 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
410 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
411 + $idLength = strlen( $website_to_upgrade['id'] );
412 + }
413 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
414 + $nameLength = strlen( $website_to_upgrade['name'] );
415 + }
407 416
408 - $idLength = strlen( 'id' );
409 - $nameLength = strlen( 'name' );
410 - $pluginLength = strlen( 'plugin' );
411 - $oldVersionLength = strlen( 'version' );
412 - $newVersionLength = strlen( 'new version' );
413 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
414 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
415 - $idLength = strlen( $website_to_upgrade['id'] );
416 - }
417 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
418 - $nameLength = strlen( $website_to_upgrade['name'] );
419 - }
417 + foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
418 + if ( $pluginLength < strlen( $plugin_to_upgrade['name'] ) ) {
419 + $pluginLength = strlen( $plugin_to_upgrade['name'] );
420 + }
421 + if ( $oldVersionLength < strlen( $plugin_to_upgrade['version'] ) ) {
422 + $oldVersionLength = strlen( $plugin_to_upgrade['version'] );
423 + }
424 + if ( $newVersionLength < strlen( $plugin_to_upgrade['new_version'] ) ) {
425 + $newVersionLength = strlen( $plugin_to_upgrade['new_version'] );
426 + }
427 + }
428 + }
420 429
421 - foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
422 - if ( $pluginLength < strlen( $plugin_to_upgrade['name'] ) ) {
423 - $pluginLength = strlen( $plugin_to_upgrade['name'] );
424 - }
425 - if ( $oldVersionLength < strlen( $plugin_to_upgrade['version'] ) ) {
426 - $oldVersionLength = strlen( $plugin_to_upgrade['version'] );
427 - }
428 - if ( $newVersionLength < strlen( $plugin_to_upgrade['new_version'] ) ) {
429 - $newVersionLength = strlen( $plugin_to_upgrade['new_version'] );
430 - }
431 - }
432 - }
430 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
431 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'plugin', 'version', 'new version' ) );
432 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
433 433
434 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
435 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'plugin', 'version', 'new version' ) );
436 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
434 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
435 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
436 + $idLength = strlen( $website_to_upgrade['id'] );
437 + }
438 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
439 + $nameLength = strlen( $website_to_upgrade['name'] );
440 + }
437 441
438 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
439 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
440 - $idLength = strlen( $website_to_upgrade['id'] );
441 - }
442 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
443 - $nameLength = strlen( $website_to_upgrade['name'] );
444 - }
442 + $i = 0;
443 + foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
444 + if ( 0 == $i ) {
445 + \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'] ) );
446 + } else {
447 + \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'] ) );
448 + }
449 + $i++;
450 + }
451 + }
445 452
446 - $i = 0;
447 - foreach ( $website_to_upgrade['plugins'] as $plugin_to_upgrade ) {
448 - if ( 0 === $i ) {
449 - \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'] ) );
450 - } else {
451 - \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'] ) );
452 - }
453 - ++$i;
454 - }
455 - }
453 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
454 + } elseif ( isset( $assoc_args['list-all'] ) ) {
455 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
456 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
457 + $websites_to_list = array();
458 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
459 + if ( ( 0 < count( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
460 + continue;
461 + }
456 462
457 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
458 - } elseif ( isset( $assoc_args['list-all'] ) ) {
459 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
460 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
461 - $websites_to_list = array();
462 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
463 - if ( ( ! empty( $sites ) ) && ( ! in_array( $website->id, $sites, true ) ) ) {
464 - continue;
465 - }
463 + $plugins_to_list = json_decode( $website->plugins, true );
464 + if ( is_array( $plugins_to_list ) ) {
465 + $tmp = array();
466 + foreach ( $plugins_to_list as $plugin ) {
467 + $tmp[] = array(
468 + 'name' => $plugin['slug'],
469 + 'version' => $plugin['version'],
470 + );
471 + }
472 + $websites_to_list[] = array(
473 + 'id' => $website->id,
474 + 'name' => $website->name,
475 + 'plugins' => $tmp,
476 + );
477 + }
478 + }
466 479
467 - $plugins_to_list = json_decode( $website->plugins, true );
468 - if ( is_array( $plugins_to_list ) ) {
469 - $tmp = array();
470 - foreach ( $plugins_to_list as $plugin ) {
471 - $tmp[] = array(
472 - 'name' => $plugin['slug'],
473 - 'version' => $plugin['version'],
474 - );
475 - }
476 - $websites_to_list[] = array(
477 - 'id' => $website->id,
478 - 'name' => $website->name,
479 - 'plugins' => $tmp,
480 - );
481 - }
482 - }
480 + $idLength = strlen( 'id' );
481 + $nameLength = strlen( 'name' );
482 + $pluginLength = strlen( 'plugin' );
483 + $oldVersionLength = strlen( 'version' );
484 + foreach ( $websites_to_list as $website_item ) {
485 + if ( $idLength < strlen( $website_item['id'] ) ) {
486 + $idLength = strlen( $website_item['id'] );
487 + }
488 + if ( $nameLength < strlen( $website_item['name'] ) ) {
489 + $nameLength = strlen( $website_item['name'] );
490 + }
483 491
484 - $idLength = strlen( 'id' );
485 - $nameLength = strlen( 'name' );
486 - $pluginLength = strlen( 'plugin' );
487 - $oldVersionLength = strlen( 'version' );
488 - foreach ( $websites_to_list as $website_item ) {
489 - if ( $idLength < strlen( $website_item['id'] ) ) {
490 - $idLength = strlen( $website_item['id'] );
491 - }
492 - if ( $nameLength < strlen( $website_item['name'] ) ) {
493 - $nameLength = strlen( $website_item['name'] );
494 - }
492 + foreach ( $website_item['plugins'] as $plugin_item ) {
493 + if ( $pluginLength < strlen( $plugin_item['name'] ) ) {
494 + $pluginLength = strlen( $plugin_item['name'] );
495 + }
496 + if ( $oldVersionLength < strlen( $plugin_item['version'] ) ) {
497 + $oldVersionLength = strlen( $plugin_item['version'] );
498 + }
499 + }
500 + }
495 501
496 - foreach ( $website_item['plugins'] as $plugin_item ) {
497 - if ( $pluginLength < strlen( $plugin_item['name'] ) ) {
498 - $pluginLength = strlen( $plugin_item['name'] );
499 - }
500 - if ( $oldVersionLength < strlen( $plugin_item['version'] ) ) {
501 - $oldVersionLength = strlen( $plugin_item['version'] );
502 - }
503 - }
504 - }
502 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
503 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'plugin', 'version' ) );
504 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
505 505
506 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
507 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'plugin', 'version' ) );
508 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
506 + foreach ( $websites_to_list as $website_item ) {
507 + if ( $idLength < strlen( $website_item['id'] ) ) {
508 + $idLength = strlen( $website_item['id'] );
509 + }
510 + if ( $nameLength < strlen( $website_item['name'] ) ) {
511 + $nameLength = strlen( $website_item['name'] );
512 + }
509 513
510 - foreach ( $websites_to_list as $website_item ) {
511 - if ( $idLength < strlen( $website_item['id'] ) ) {
512 - $idLength = strlen( $website_item['id'] );
513 - }
514 - if ( $nameLength < strlen( $website_item['name'] ) ) {
515 - $nameLength = strlen( $website_item['name'] );
516 - }
514 + $i = 0;
515 + foreach ( $website_item['plugins'] as $plugin_item ) {
516 + if ( 0 == $i ) {
517 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', $website_item['id'], $website_item['name'], $plugin_item['name'], $plugin_item['version'] ) );
518 + } else {
519 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', '', '', $plugin_item['name'], $plugin_item['version'] ) );
520 + }
521 + $i++;
522 + }
523 + }
517 524
518 - $i = 0;
519 - foreach ( $website_item['plugins'] as $plugin_item ) {
520 - if ( 0 === $i ) {
521 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', $website_item['id'], $website_item['name'], $plugin_item['name'], $plugin_item['version'] ) );
522 - } else {
523 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $pluginLength . 's | %-' . $oldVersionLength . 's |', '', '', $plugin_item['name'], $plugin_item['version'] ) );
524 - }
525 - ++$i;
526 - }
527 - }
525 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
526 + } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
528 527
529 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '' ) );
530 - } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
528 + $pluginSlugs = array();
529 + if ( isset( $assoc_args['upgrade'] ) ) {
530 + $pluginSlugs = explode( ',', $assoc_args['upgrade'] );
531 + }
531 532
532 - $pluginSlugs = array();
533 - if ( isset( $assoc_args['upgrade'] ) ) {
534 - $pluginSlugs = explode( ',', $assoc_args['upgrade'] );
535 - }
533 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
534 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
535 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
536 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
537 + continue;
538 + }
536 539
537 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
538 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
539 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
540 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
541 - continue;
542 - }
540 + $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
543 541
544 - $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
542 + if ( is_array( $plugin_upgrades ) ) {
543 + $ignored_plugins = json_decode( $website->ignored_plugins, true );
544 + if ( is_array( $ignored_plugins ) ) {
545 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
546 + }
545 547
546 - if ( is_array( $plugin_upgrades ) ) {
547 - $ignored_plugins = json_decode( $website->ignored_plugins, true );
548 - if ( is_array( $ignored_plugins ) ) {
549 - $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
550 - }
548 + $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
549 + if ( is_array( $ignored_plugins ) ) {
550 + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
551 + }
551 552
552 - $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
553 - if ( is_array( $ignored_plugins ) ) {
554 - $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins );
555 - }
553 + $tmp = array();
554 + foreach ( $plugin_upgrades as $key => $plugin_upgrade ) {
555 + if ( ( count( $pluginSlugs ) > 0 ) && ( ! in_array( $plugin_upgrade['update']['slug'], $pluginSlugs, true ) ) ) {
556 + continue;
557 + }
556 558
557 - $tmp = array();
558 - foreach ( $plugin_upgrades as $key => $plugin_upgrade ) {
559 - if ( ! empty( $pluginSlugs ) && ( ! in_array( $plugin_upgrade['update']['slug'], $pluginSlugs, true ) ) ) {
560 - continue;
561 - }
559 + $tmp[] = $key;
560 + }
562 561
563 - $tmp[] = $key;
564 - }
562 + if ( count( $tmp ) == 0 ) {
563 + \WP_CLI::line( 'No available plugin updates for ' . $website->name );
565 564
566 - if ( empty( $tmp ) ) {
567 - \WP_CLI::line( 'No available plugin updates for ' . $website->name );
565 + continue;
566 + }
568 567
569 - continue;
570 - }
568 + \WP_CLI::line( 'Updating ' . count( $tmp ) . ' plugins for ' . $website->name );
571 569
572 - \WP_CLI::line( 'Updating ' . count( $tmp ) . ' plugins for ' . $website->name );
570 + try {
571 + MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'plugin', implode( ',', $tmp ) );
572 + \WP_CLI::success( 'Updates completed' );
573 + } catch ( \Exception $e ) {
574 + \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
575 + if ( $e->getMesage() == 'WPERROR' ) {
576 + \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
577 + }
578 + }
579 + }
580 + }
581 + }
582 + }
573 583
574 - try {
575 - MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'plugin', implode( ',', $tmp ) );
576 - \WP_CLI::success( 'Updates completed' );
577 - } catch ( \Exception $e ) {
578 - \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
579 - if ( $e->getMesage() === 'WPERROR' ) {
580 - \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
581 - }
582 - }
583 - }
584 - }
585 - }
586 - }
584 + /**
585 + * List information about theme updates
586 + *
587 + * ## OPTIONS
588 + *
589 + * [<websiteid>]
590 + * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
591 + *
592 + * [--list]
593 + * : Get a list of themes with available updates
594 + *
595 + * [--list-all]
596 + * : list all themes
597 + *
598 + * [--upgrade=<theme>]
599 + * : Update the themes
600 + *
601 + * [--upgrade-all]
602 + * : Update all themes
603 + *
604 + * ## EXAMPLES
605 + *
606 + * wp mainwp theme 2,5 --list
607 + * wp mainwp theme --list
608 + * wp mainwp theme 2,5 --upgrade-all
609 + * wp mainwp theme 2,5 --upgrade=twentysixteen
610 + *
611 + * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<theme>] [--upgrade-all]
612 + *
613 + * @param array $args Function arguments.
614 + * @param array $assoc_args Function associate arguments.
615 + *
616 + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
617 + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
618 + * @uses \MainWP\Dashboard\MainWP_DB::query()
619 + * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
620 + * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
621 + * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
622 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
623 + */
624 + public function theme( $args, $assoc_args ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
625 + $sites = array();
626 + if ( count( $args ) > 0 ) {
627 + $args_exploded = explode( ',', $args[0] );
628 + foreach ( $args_exploded as $arg ) {
629 + if ( ! is_numeric( trim( $arg ) ) ) {
630 + \WP_CLI::error( 'Child site IDs should be numeric.' );
631 + }
587 632
588 - /**
589 - * List information about theme updates
590 - *
591 - * ## OPTIONS
592 - *
593 - * [<websiteid>]
594 - * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used.
595 - *
596 - * [--list]
597 - * : Get a list of themes with available updates
598 - *
599 - * [--list-all]
600 - * : list all themes
601 - *
602 - * [--upgrade=<theme>]
603 - * : Update the themes
604 - *
605 - * [--upgrade-all]
606 - * : Update all themes
607 - *
608 - * ## EXAMPLES
609 - *
610 - * wp mainwp theme 2,5 --list
611 - * wp mainwp theme --list
612 - * wp mainwp theme 2,5 --upgrade-all
613 - * wp mainwp theme 2,5 --upgrade=twentysixteen
614 - *
615 - * ## Synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<theme>] [--upgrade-all]
616 - *
617 - * @param array $args Function arguments.
618 - * @param array $assoc_args Function associate arguments.
619 - *
620 - * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
621 - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_console_error_message()
622 - * @uses \MainWP\Dashboard\MainWP_DB::query()
623 - * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
624 - * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
625 - * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
626 - * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
627 - */
628 - public function theme( $args, $assoc_args ) { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated.
629 - $sites = array();
630 - if ( ! empty( $args ) ) {
631 - $args_exploded = explode( ',', $args[0] );
632 - foreach ( $args_exploded as $arg ) {
633 - if ( ! is_numeric( trim( $arg ) ) ) {
634 - \WP_CLI::error( 'Child site IDs should be numeric.' );
635 - }
633 + $sites[] = trim( $arg );
634 + }
635 + }
636 636
637 - $sites[] = trim( $arg );
638 - }
639 - }
637 + if ( isset( $assoc_args['list'] ) ) {
638 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
639 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
640 + $websites_to_upgrade = array();
641 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
642 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
643 + continue;
644 + }
640 645
641 - if ( isset( $assoc_args['list'] ) ) {
642 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
643 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
644 - $websites_to_upgrade = array();
645 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
646 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
647 - continue;
648 - }
646 + $theme_upgrades = json_decode( $website->theme_upgrades, true );
649 647
650 - $theme_upgrades = json_decode( $website->theme_upgrades, true );
648 + if ( is_array( $theme_upgrades ) ) {
649 + $ignored_themes = json_decode( $website->ignored_themes, true );
650 + if ( is_array( $ignored_themes ) ) {
651 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
652 + }
651 653
652 - if ( is_array( $theme_upgrades ) ) {
653 - $ignored_themes = json_decode( $website->ignored_themes, true );
654 - if ( is_array( $ignored_themes ) ) {
655 - $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
656 - }
654 + $ignored_themes = json_decode( $userExtension->ignored_themes, true );
655 + if ( is_array( $ignored_themes ) ) {
656 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
657 + }
657 658
658 - $ignored_themes = json_decode( $userExtension->ignored_themes, true );
659 - if ( is_array( $ignored_themes ) ) {
660 - $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
661 - }
659 + $tmp = array();
660 + foreach ( $theme_upgrades as $theme_upgrade ) {
661 + $tmp[] = array(
662 + 'name' => $theme_upgrade['update']['theme'],
663 + 'version' => $theme_upgrade['Version'],
664 + 'new_version' => $theme_upgrade['update']['new_version'],
665 + );
666 + }
667 + $websites_to_upgrade[] = array(
668 + 'id' => $website->id,
669 + 'name' => $website->name,
670 + 'themes' => $tmp,
671 + );
672 + }
673 + }
662 674
663 - $tmp = array();
664 - foreach ( $theme_upgrades as $theme_upgrade ) {
665 - $tmp[] = array(
666 - 'name' => $theme_upgrade['update']['theme'],
667 - 'version' => $theme_upgrade['Version'],
668 - 'new_version' => $theme_upgrade['update']['new_version'],
669 - );
670 - }
671 - $websites_to_upgrade[] = array(
672 - 'id' => $website->id,
673 - 'name' => $website->name,
674 - 'themes' => $tmp,
675 - );
676 - }
677 - }
675 + $idLength = strlen( 'id' );
676 + $nameLength = strlen( 'name' );
677 + $themeLength = strlen( 'theme' );
678 + $oldVersionLength = strlen( 'version' );
679 + $newVersionLength = strlen( 'new version' );
680 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
681 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
682 + $idLength = strlen( $website_to_upgrade['id'] );
683 + }
684 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
685 + $nameLength = strlen( $website_to_upgrade['name'] );
686 + }
678 687
679 - $idLength = strlen( 'id' );
680 - $nameLength = strlen( 'name' );
681 - $themeLength = strlen( 'theme' );
682 - $oldVersionLength = strlen( 'version' );
683 - $newVersionLength = strlen( 'new version' );
684 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
685 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
686 - $idLength = strlen( $website_to_upgrade['id'] );
687 - }
688 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
689 - $nameLength = strlen( $website_to_upgrade['name'] );
690 - }
688 + foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
689 + if ( $themeLength < strlen( $theme_to_upgrade['name'] ) ) {
690 + $themeLength = strlen( $theme_to_upgrade['name'] );
691 + }
692 + if ( $oldVersionLength < strlen( $theme_to_upgrade['version'] ) ) {
693 + $oldVersionLength = strlen( $theme_to_upgrade['version'] );
694 + }
695 + if ( $newVersionLength < strlen( $theme_to_upgrade['new_version'] ) ) {
696 + $newVersionLength = strlen( $theme_to_upgrade['new_version'] );
697 + }
698 + }
699 + }
691 700
692 - foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
693 - if ( $themeLength < strlen( $theme_to_upgrade['name'] ) ) {
694 - $themeLength = strlen( $theme_to_upgrade['name'] );
695 - }
696 - if ( $oldVersionLength < strlen( $theme_to_upgrade['version'] ) ) {
697 - $oldVersionLength = strlen( $theme_to_upgrade['version'] );
698 - }
699 - if ( $newVersionLength < strlen( $theme_to_upgrade['new_version'] ) ) {
700 - $newVersionLength = strlen( $theme_to_upgrade['new_version'] );
701 - }
702 - }
703 - }
701 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
702 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'theme', 'version', 'new version' ) );
703 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
704 704
705 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
706 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $oldVersionLength . 's | %-' . $newVersionLength . 's |', 'id', 'name', 'theme', 'version', 'new version' ) );
707 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
705 + foreach ( $websites_to_upgrade as $website_to_upgrade ) {
706 + if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
707 + $idLength = strlen( $website_to_upgrade['id'] );
708 + }
709 + if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
710 + $nameLength = strlen( $website_to_upgrade['name'] );
711 + }
708 712
709 - foreach ( $websites_to_upgrade as $website_to_upgrade ) {
710 - if ( $idLength < strlen( $website_to_upgrade['id'] ) ) {
711 - $idLength = strlen( $website_to_upgrade['id'] );
712 - }
713 - if ( $nameLength < strlen( $website_to_upgrade['name'] ) ) {
714 - $nameLength = strlen( $website_to_upgrade['name'] );
715 - }
713 + $i = 0;
714 + foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
715 + if ( 0 == $i ) {
716 + \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'] ) );
717 + } else {
718 + \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'] ) );
719 + }
720 + $i++;
721 + }
722 + }
716 723
717 - $i = 0;
718 - foreach ( $website_to_upgrade['themes'] as $theme_to_upgrade ) {
719 - if ( 0 === $i ) {
720 - \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'] ) );
721 - } else {
722 - \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'] ) );
723 - }
724 - ++$i;
725 - }
726 - }
724 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
725 + } elseif ( isset( $assoc_args['list-all'] ) ) {
726 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
727 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
728 + $websites_to_upgrade = array();
729 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
730 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
731 + continue;
732 + }
733 + $theme_to_list = json_decode( $website->themes, true );
734 + if ( is_array( $theme_to_list ) ) {
735 + $tmp = array();
736 + foreach ( $theme_to_list as $theme ) {
737 + $tmp[] = array(
738 + 'name' => $theme['name'],
739 + 'active' => $theme['active'] ? 'yes' : '',
740 + 'version' => $theme['version'],
741 + );
742 + }
743 + $websites_to_upgrade[] = array(
744 + 'id' => $website->id,
745 + 'name' => $website->name,
746 + 'themes' => $tmp,
747 + );
748 + }
749 + }
727 750
728 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
729 - } elseif ( isset( $assoc_args['list-all'] ) ) {
730 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
731 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
732 - $websites_to_upgrade = array();
733 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
734 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
735 - continue;
736 - }
737 - $theme_to_list = json_decode( $website->themes, true );
738 - if ( is_array( $theme_to_list ) ) {
739 - $tmp = array();
740 - foreach ( $theme_to_list as $theme ) {
741 - $tmp[] = array(
742 - 'name' => $theme['name'],
743 - 'active' => $theme['active'] ? 'yes' : '',
744 - 'version' => $theme['version'],
745 - );
746 - }
747 - $websites_to_upgrade[] = array(
748 - 'id' => $website->id,
749 - 'name' => $website->name,
750 - 'themes' => $tmp,
751 - );
752 - }
753 - }
751 + $idLength = strlen( 'id' );
752 + $nameLength = strlen( 'name' );
753 + $themeLength = strlen( 'theme' );
754 + $oldVersionLength = strlen( 'version' );
755 + $activeLength = strlen( 'active' );
756 + foreach ( $websites_to_upgrade as $website_item ) {
757 + if ( $idLength < strlen( $website_item['id'] ) ) {
758 + $idLength = strlen( $website_item['id'] );
759 + }
760 + if ( $nameLength < strlen( $website_item['name'] ) ) {
761 + $nameLength = strlen( $website_item['name'] );
762 + }
754 763
755 - $idLength = strlen( 'id' );
756 - $nameLength = strlen( 'name' );
757 - $themeLength = strlen( 'theme' );
758 - $oldVersionLength = strlen( 'version' );
759 - $activeLength = strlen( 'active' );
760 - foreach ( $websites_to_upgrade as $website_item ) {
761 - if ( $idLength < strlen( $website_item['id'] ) ) {
762 - $idLength = strlen( $website_item['id'] );
763 - }
764 - if ( $nameLength < strlen( $website_item['name'] ) ) {
765 - $nameLength = strlen( $website_item['name'] );
766 - }
764 + foreach ( $website_item['themes'] as $theme_item ) {
765 + if ( $themeLength < strlen( $theme_item['name'] ) ) {
766 + $themeLength = strlen( $theme_item['name'] );
767 + }
768 + if ( $oldVersionLength < strlen( $theme_item['version'] ) ) {
769 + $oldVersionLength = strlen( $theme_item['version'] );
770 + }
771 + }
772 + }
767 773
768 - foreach ( $website_item['themes'] as $theme_item ) {
769 - if ( $themeLength < strlen( $theme_item['name'] ) ) {
770 - $themeLength = strlen( $theme_item['name'] );
771 - }
772 - if ( $oldVersionLength < strlen( $theme_item['version'] ) ) {
773 - $oldVersionLength = strlen( $theme_item['version'] );
774 - }
775 - }
776 - }
774 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
775 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'theme', 'active', 'version' ) );
776 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
777 777
778 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
779 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', 'id', 'name', 'theme', 'active', 'version' ) );
780 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
778 + foreach ( $websites_to_upgrade as $website_item ) {
779 + if ( $idLength < strlen( $website_item['id'] ) ) {
780 + $idLength = strlen( $website_item['id'] );
781 + }
782 + if ( $nameLength < strlen( $website_item['name'] ) ) {
783 + $nameLength = strlen( $website_item['name'] );
784 + }
781 785
782 - foreach ( $websites_to_upgrade as $website_item ) {
783 - if ( $idLength < strlen( $website_item['id'] ) ) {
784 - $idLength = strlen( $website_item['id'] );
785 - }
786 - if ( $nameLength < strlen( $website_item['name'] ) ) {
787 - $nameLength = strlen( $website_item['name'] );
788 - }
786 + $i = 0;
787 + foreach ( $website_item['themes'] as $theme_item ) {
788 + if ( 0 == $i ) {
789 + \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'] ) );
790 + } else {
791 + \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', '', '', $theme_item['name'], $theme_item['active'], $theme_item['version'] ) );
792 + }
793 + $i++;
794 + }
795 + }
789 796
790 - $i = 0;
791 - foreach ( $website_item['themes'] as $theme_item ) {
792 - if ( 0 === $i ) {
793 - \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'] ) );
794 - } else {
795 - \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $themeLength . 's | %-' . $activeLength . 's | %-' . $oldVersionLength . 's |', '', '', $theme_item['name'], $theme_item['active'], $theme_item['version'] ) );
796 - }
797 - ++$i;
798 - }
799 - }
797 + \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
798 + } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
800 799
801 - \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . 's+', '', '', '', '', '' ) );
802 - } elseif ( isset( $assoc_args['upgrade'] ) || isset( $assoc_args['upgrade-all'] ) ) {
800 + $themeSlugs = array();
801 + if ( isset( $assoc_args['upgrade'] ) ) {
802 + $themeSlugs = explode( ',', $assoc_args['upgrade'] );
803 + }
803 804
804 - $themeSlugs = array();
805 - if ( isset( $assoc_args['upgrade'] ) ) {
806 - $themeSlugs = explode( ',', $assoc_args['upgrade'] );
807 - }
805 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
806 + $userExtension = MainWP_DB_Common::instance()->get_user_extension();
807 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
808 + if ( ( count( $sites ) > 0 ) && ( ! in_array( $website->id, $sites, true ) ) ) {
809 + continue;
810 + }
808 811
809 - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
810 - $userExtension = MainWP_DB_Common::instance()->get_user_extension();
811 - while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
812 - if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
813 - continue;
814 - }
812 + $theme_upgrades = json_decode( $website->theme_upgrades, true );
815 813
816 - $theme_upgrades = json_decode( $website->theme_upgrades, true );
814 + if ( is_array( $theme_upgrades ) ) {
815 + $ignored_themes = json_decode( $website->ignored_themes, true );
816 + if ( is_array( $ignored_themes ) ) {
817 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
818 + }
817 819
818 - if ( is_array( $theme_upgrades ) ) {
819 - $ignored_themes = json_decode( $website->ignored_themes, true );
820 - if ( is_array( $ignored_themes ) ) {
821 - $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
822 - }
820 + $ignored_themes = json_decode( $userExtension->ignored_themes, true );
821 + if ( is_array( $ignored_themes ) ) {
822 + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
823 + }
823 824
824 - $ignored_themes = json_decode( $userExtension->ignored_themes, true );
825 - if ( is_array( $ignored_themes ) ) {
826 - $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes );
827 - }
825 + $tmp = array();
826 + foreach ( $theme_upgrades as $key => $theme_upgrade ) {
827 + if ( ( count( $themeSlugs ) > 0 ) && ( ! in_array( $theme_upgrade['update']['slug'], $themeSlugs, true ) ) ) {
828 + continue;
829 + }
828 830
829 - $tmp = array();
830 - foreach ( $theme_upgrades as $key => $theme_upgrade ) {
831 - if ( ! empty( $themeSlugs ) && ( ! in_array( $theme_upgrade['update']['slug'], $themeSlugs, true ) ) ) {
832 - continue;
833 - }
831 + $tmp[] = $key;
832 + }
834 833
835 - $tmp[] = $key;
836 - }
834 + if ( count( $tmp ) == 0 ) {
835 + \WP_CLI::line( 'No available theme updates for ' . $website->name );
837 836
838 - if ( empty( $tmp ) ) {
839 - \WP_CLI::line( 'No available theme updates for ' . $website->name );
837 + continue;
838 + }
840 839
841 - continue;
842 - }
840 + \WP_CLI::line( 'Updating ' . count( $tmp ) . ' themes for ' . $website->name );
843 841
844 - \WP_CLI::line( 'Updating ' . count( $tmp ) . ' themes for ' . $website->name );
842 + try {
843 + MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'theme', implode( ',', $tmp ) );
844 + \WP_CLI::success( 'Updates completed' );
845 + } catch ( \Exception $e ) {
846 + \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
847 + if ( $e->getMesage() == 'WPERROR' ) {
848 + \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
849 + }
850 + }
851 + }
852 + }
853 + }
854 + }
845 855
846 - try {
847 - MainWP_Updates_Handler::upgrade_plugin_theme_translation( $website->id, 'theme', implode( ',', $tmp ) );
848 - \WP_CLI::success( 'Updates completed' );
849 - } catch ( \Exception $e ) {
850 - \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
851 - if ( $e->getMesage() === 'WPERROR' ) {
852 - \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
853 - }
854 - }
855 - }
856 - }
857 - }
858 - }
859 856 }