| @@ -1,863 +1,658 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * MainWP-CLI | |
| 4 | - * | |
| 5 | - * This file extends the WP-CLI and provides a set of SubCommands to Control your | |
| 6 | - * Child Sites that are added to the MainWP Dashboard. | |
| 7 | - * | |
| 8 | - * @devtodo: allow to add or remove child sites | |
| 9 | - * @package MainWP/Dashboard | |
| 10 | - */ | |
| 11 | - | |
| 12 | -namespace MainWP\Dashboard; | |
| 13 | - | |
| 14 | -// Exit if access directly. | |
| 15 | -if ( ! defined( 'WP_CLI' ) ) { | |
| 16 | - return; // NOSONAR - jump to avoid CLI. | |
| 17 | -} | |
| 18 | - | |
| 19 | -/** | |
| 20 | - * Class MainWP_WP_CLI_Command | |
| 21 | - * | |
| 22 | - * Manage all child sites added to the MainWP Dashboard. | |
| 23 | - * | |
| 24 | - * @package MainWP\Dashboard | |
| 25 | - */ | |
| 26 | -class MainWP_WP_CLI_Command extends \WP_CLI_Command { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. | |
| 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 | - } | |
| 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 | - } | |
| 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 ) { | |
| 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 | - } | |
| 82 | - | |
| 83 | - $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) ); | |
| 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 ); | |
| 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+', '', '', '', '' ) ); | |
| 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 | - } | |
| 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 | - } | |
| 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 | - } | |
| 142 | - | |
| 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 | - } | |
| 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 | - } | |
| 228 | - | |
| 229 | - $sites[] = trim( $arg ); | |
| 230 | - } | |
| 231 | - } | |
| 232 | - | |
| 233 | - if ( is_array( $assoc_args ) && isset( $assoc_args['all'] ) ) { | |
| 234 | - update_option( 'mainwp_last_synced_all_sites', time() ); | |
| 235 | - } | |
| 236 | - | |
| 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 | - | |
| 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 | - } | |
| 275 | - | |
| 276 | - $sites[] = trim( $arg ); | |
| 277 | - } | |
| 278 | - } | |
| 279 | - | |
| 280 | - if ( empty( $sites ) ) { | |
| 281 | - \WP_CLI::error( 'Please specify one or more child sites.' ); | |
| 282 | - } | |
| 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 | - } | |
| 314 | - | |
| 315 | - | |
| 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 | - } | |
| 365 | - | |
| 366 | - $sites[] = trim( $arg ); | |
| 367 | - } | |
| 368 | - } | |
| 369 | - | |
| 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 | - } | |
| 378 | - | |
| 379 | - $plugin_upgrades = json_decode( $website->plugin_upgrades, true ); | |
| 380 | - | |
| 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 ); | |
| 385 | - | |
| 386 | - } | |
| 387 | - | |
| 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 ); | |
| 391 | - | |
| 392 | - } | |
| 393 | - | |
| 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 | - } | |
| 409 | - | |
| 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 | - } | |
| 422 | - | |
| 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 | - } | |
| 435 | - | |
| 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+', '', '', '', '', '' ) ); | |
| 439 | - | |
| 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 | - } | |
| 447 | - | |
| 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 | - } | |
| 458 | - | |
| 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 | - } | |
| 468 | - | |
| 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 | - } | |
| 485 | - | |
| 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 | - } | |
| 497 | - | |
| 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 | - } | |
| 507 | - | |
| 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+', '', '', '', '' ) ); | |
| 511 | - | |
| 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 | - } | |
| 519 | - | |
| 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 | - } | |
| 530 | - | |
| 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'] ) ) { | |
| 533 | - | |
| 534 | - $pluginSlugs = array(); | |
| 535 | - if ( isset( $assoc_args['upgrade'] ) ) { | |
| 536 | - $pluginSlugs = explode( ',', $assoc_args['upgrade'] ); | |
| 537 | - } | |
| 538 | - | |
| 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 | - } | |
| 545 | - | |
| 546 | - $plugin_upgrades = json_decode( $website->plugin_upgrades, true ); | |
| 547 | - | |
| 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 ); | |
| 552 | - | |
| 553 | - } | |
| 554 | - | |
| 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 ); | |
| 558 | - | |
| 559 | - } | |
| 560 | - | |
| 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 | - } | |
| 566 | - | |
| 567 | - $tmp[] = $key; | |
| 568 | - } | |
| 569 | - | |
| 570 | - if ( empty( $tmp ) ) { | |
| 571 | - \WP_CLI::line( 'No available plugin updates for ' . $website->name ); | |
| 572 | - | |
| 573 | - continue; | |
| 574 | - } | |
| 575 | - | |
| 576 | - \WP_CLI::line( 'Updating ' . count( $tmp ) . ' plugins for ' . $website->name ); | |
| 577 | - | |
| 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 | - } | |
| 591 | - | |
| 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 | - } | |
| 640 | - | |
| 641 | - $sites[] = trim( $arg ); | |
| 642 | - } | |
| 643 | - } | |
| 644 | - | |
| 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 | - } | |
| 653 | - | |
| 654 | - $theme_upgrades = json_decode( $website->theme_upgrades, true ); | |
| 655 | - | |
| 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 | - } | |
| 661 | - | |
| 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 | - } | |
| 666 | - | |
| 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 | - } | |
| 682 | - | |
| 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 | - } | |
| 695 | - | |
| 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 | - } | |
| 708 | - | |
| 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+', '', '', '', '', '' ) ); | |
| 712 | - | |
| 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 | - } | |
| 720 | - | |
| 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 | - } | |
| 731 | - | |
| 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 | - } | |
| 758 | - | |
| 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 | - } | |
| 771 | - | |
| 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 | - } | |
| 781 | - | |
| 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+', '', '', '', '', '' ) ); | |
| 785 | - | |
| 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 | - } | |
| 793 | - | |
| 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 | - } | |
| 804 | - | |
| 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'] ) ) { | |
| 807 | - | |
| 808 | - $themeSlugs = array(); | |
| 809 | - if ( isset( $assoc_args['upgrade'] ) ) { | |
| 810 | - $themeSlugs = explode( ',', $assoc_args['upgrade'] ); | |
| 811 | - } | |
| 812 | - | |
| 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 | - } | |
| 819 | - | |
| 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 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +if ( !defined( 'WP_CLI' ) ) { | |
| 4 | + return; | |
| 5 | +} | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Manage all child sites added to the MainWP Dashboard | |
| 9 | + */ | |
| 10 | +class MainWP_WP_CLI_Command extends WP_CLI_Command { | |
| 11 | + | |
| 12 | + public static function init() { | |
| 13 | + add_action( 'plugins_loaded', array( 'MainWP_WP_CLI_Command', 'init_wpcli_commands' ), 99999 ); | |
| 14 | + } | |
| 15 | + | |
| 16 | + public static function init_wpcli_commands() { | |
| 17 | + WP_CLI::add_command( 'mainwp', 'MainWP_WP_CLI_Command' ); | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * List information about added child sites | |
| 22 | + * | |
| 23 | + * ## OPTIONS | |
| 24 | + * | |
| 25 | + * [--list] | |
| 26 | + * : Get a list of all child sites | |
| 27 | + * | |
| 28 | + * @todo: allow to add or remove child sites | |
| 29 | + * @synopsis [--list] | |
| 30 | + */ | |
| 31 | + public function sites( $args, $assoc_args ) { | |
| 32 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 33 | + $idLength = strlen( 'id' ); | |
| 34 | + $nameLength = strlen( 'name' ); | |
| 35 | + $urlLength = strlen( 'url' ); | |
| 36 | + $versionLength = strlen( 'version' ); | |
| 37 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 38 | + if ( $idLength < strlen( $website->id ) ) | |
| 39 | + $idLength = strlen( $website->id ); | |
| 40 | + if ( $nameLength < strlen( $website->name ) ) | |
| 41 | + $nameLength = strlen( $website->name ); | |
| 42 | + if ( $urlLength < strlen( $website->url ) ) | |
| 43 | + $urlLength = strlen( $website->url ); | |
| 44 | + if ( $versionLength < strlen( $website->version ) ) | |
| 45 | + $versionLength = strlen( $website->version ); | |
| 46 | + } | |
| 47 | + @MainWP_DB::data_seek( $websites, 0 ); | |
| 48 | + | |
| 49 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . "s+", '', '', '', '' ) ); | |
| 50 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $urlLength . "s | %-" . $versionLength . "s |", 'id', 'name', 'url', 'version' ) ); | |
| 51 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . "s+", '', '', '', '' ) ); | |
| 52 | + | |
| 53 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 54 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $urlLength . "s | %-" . $versionLength . "s |", $website->id, $website->name, $website->url, $website->version ) ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . "s+%'--" . ( $versionLength + 2 ) . "s+", '', '', '', '' ) ); | |
| 58 | + @MainWP_DB::free_result( $websites ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Sync Data with Child Sites | |
| 63 | + * | |
| 64 | + * ## OPTIONS | |
| 65 | + * | |
| 66 | + * [<websiteid>] | |
| 67 | + * : The id (or ids, comma separated) of the child sites that need to be synced. | |
| 68 | + * | |
| 69 | + * [--all] | |
| 70 | + * : If set, all child sites will be synced. | |
| 71 | + * | |
| 72 | + * ## EXAMPLES | |
| 73 | + * | |
| 74 | + * wp mainwp sync 2,5 | |
| 75 | + * wp mainwp sync --all | |
| 76 | + * | |
| 77 | + * @synopsis [<websiteid>] [--all] | |
| 78 | + */ | |
| 79 | + public function sync( $args, $assoc_args ) { | |
| 80 | + $sites = array(); | |
| 81 | + if ( count( $args ) > 0 ) { | |
| 82 | + $args_exploded = explode( ',', $args[ 0 ] ); | |
| 83 | + foreach ( $args_exploded as $arg ) { | |
| 84 | + if ( !is_numeric( trim( $arg ) ) ) { | |
| 85 | + WP_CLI::error( 'Child site ids should be numeric.' ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + $sites[] = trim( $arg ); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + if ( ( count( $sites ) == 0 ) && (!isset( $assoc_args[ 'all' ] ) ) ) | |
| 93 | + WP_CLI::error( 'Please specify one or more child sites, or use --all.' ); | |
| 94 | + | |
| 95 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 96 | + WP_CLI::line( 'Sync started' ); | |
| 97 | + $warnings = 0; | |
| 98 | + $errors = 0; | |
| 99 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 100 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) | |
| 101 | + continue; | |
| 102 | + | |
| 103 | + WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' ); | |
| 104 | + try { | |
| 105 | + if ( MainWP_Sync::syncSite( $website ) ) { | |
| 106 | + WP_CLI::success( ' Sync succeeded' ); | |
| 107 | + } else { | |
| 108 | + WP_CLI::warning( ' Sync failed' ); | |
| 109 | + $warnings++; | |
| 110 | + } | |
| 111 | + } catch ( Exception $e ) { | |
| 112 | + WP_CLI::error( ' Sync failed: ' . MainWP_Error_Helper::getConsoleErrorMessage( $e ) ); | |
| 113 | + $errors++; | |
| 114 | + } | |
| 115 | + } | |
| 116 | + @MainWP_DB::free_result( $websites ); | |
| 117 | + if ( $errors > 0 ) { | |
| 118 | + WP_CLI::error( 'Sync completed with errors' ); | |
| 119 | + } else if ( $warnings > 0 ) { | |
| 120 | + WP_CLI::warning( 'Sync completed with warnings' ); | |
| 121 | + } else { | |
| 122 | + WP_CLI::success( 'Sync completed' ); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Reconnect with Child Sites | |
| 128 | + * | |
| 129 | + * ## OPTIONS | |
| 130 | + * | |
| 131 | + * [<websiteid>] | |
| 132 | + * : The id (or ids, comma separated) of the child sites that need to be reconnect. | |
| 133 | + * | |
| 134 | + * ## EXAMPLES | |
| 135 | + * | |
| 136 | + * wp mainwp reconnect 2,5 | |
| 137 | + * | |
| 138 | + * @synopsis [<websiteid>] | |
| 139 | + */ | |
| 140 | + public function reconnect( $args, $assoc_args ) { | |
| 141 | + $sites = array(); | |
| 142 | + if ( count( $args ) > 0 ) { | |
| 143 | + $args_exploded = explode( ',', $args[0] ); | |
| 144 | + foreach ($args_exploded as $arg) { | |
| 145 | + if ( ! is_numeric( trim( $arg ) ) ) { | |
| 146 | + WP_CLI::error('Child site ids should be numeric.'); | |
| 147 | + } | |
| 148 | + | |
| 149 | + $sites[] = trim( $arg ); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + if ( count($sites) == 0 ) WP_CLI::error('Please specify one or more child sites.'); | |
| 154 | + | |
| 155 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser(false, null, 'wp.url', false, false, null, true) ); | |
| 156 | + WP_CLI::line( 'Reconnect started' ); | |
| 157 | + $warnings = 0; | |
| 158 | + $errors = 0; | |
| 159 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 160 | + if ( ( count( $sites ) > 0 ) && ( !in_array( $website->id, $sites ) ) ) continue; | |
| 161 | + WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' ); | |
| 162 | + try { | |
| 163 | + if ( MainWP_Manage_Sites::_reconnectSite( $website ) ) { | |
| 164 | + //Reconnected | |
| 165 | + WP_CLI::success( ' Reconnected successfully' ); | |
| 166 | + } else { | |
| 167 | + WP_CLI::warning( ' Reconnect failed' ); | |
| 168 | + $warnings++; | |
| 169 | + } | |
| 170 | + } catch ( Exception $e ) { | |
| 171 | + //something wrong | |
| 172 | + WP_CLI::error( ' Reconnect failed: ' . MainWP_Error_Helper::getConsoleErrorMessage( $e ) ); | |
| 173 | + $errors++; | |
| 174 | + } | |
| 175 | + } | |
| 176 | + @MainWP_DB::free_result( $websites ); | |
| 177 | + if ( $errors > 0 ) { | |
| 178 | + WP_CLI::error( 'Reconnect completed with errors' ); | |
| 179 | + } else if ( $warnings > 0 ) { | |
| 180 | + WP_CLI::warning( 'Reconnect completed with warnings' ); | |
| 181 | + } else { | |
| 182 | + WP_CLI::success( 'Reconnect completed' ); | |
| 183 | + } | |
| 184 | + } | |
| 185 | + | |
| 186 | + | |
| 187 | + /** | |
| 188 | + * List information about plugin updates | |
| 189 | + * | |
| 190 | + * ## OPTIONS | |
| 191 | + * | |
| 192 | + * [<websiteid>] | |
| 193 | + * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used. | |
| 194 | + * | |
| 195 | + * [--list] | |
| 196 | + * : Get a list of plugins with available updates | |
| 197 | + * | |
| 198 | + * [--list-all] | |
| 199 | + * : List all plugins | |
| 200 | + * | |
| 201 | + * [--upgrade=<pluginslug>] | |
| 202 | + * : Update the plugin slugs | |
| 203 | + * | |
| 204 | + * [--upgrade-all] | |
| 205 | + * : Update all plugins | |
| 206 | + * | |
| 207 | + * ## EXAMPLES | |
| 208 | + * | |
| 209 | + * wp mainwp plugin 2,5 --list | |
| 210 | + * wp mainwp plugin --list | |
| 211 | + * wp mainwp plugin 2,5 --list-all | |
| 212 | + * wp mainwp plugin 2,5 --upgrade-all | |
| 213 | + * wp mainwp plugin 2,5 --upgrade=mainwpchild | |
| 214 | + * | |
| 215 | + * @synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<pluginslug>] [--upgrade-all] | |
| 216 | + */ | |
| 217 | + public function plugin( $args, $assoc_args ) { | |
| 218 | + $sites = array(); | |
| 219 | + if ( count( $args ) > 0 ) { | |
| 220 | + $args_exploded = explode( ',', $args[ 0 ] ); | |
| 221 | + foreach ( $args_exploded as $arg ) { | |
| 222 | + if ( !is_numeric( trim( $arg ) ) ) { | |
| 223 | + WP_CLI::error( 'Child site IDs should be numeric.' ); | |
| 224 | + } | |
| 225 | + | |
| 226 | + $sites[] = trim( $arg ); | |
| 227 | + } | |
| 228 | + } | |
| 229 | + | |
| 230 | + if ( isset( $assoc_args[ 'list' ] ) ) { | |
| 231 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 232 | + $userExtension = MainWP_DB::Instance()->getUserExtension(); | |
| 233 | + $websites_to_upgrade = array(); | |
| 234 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 235 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) { | |
| 236 | + continue; | |
| 237 | + } | |
| 238 | + | |
| 239 | + $plugin_upgrades = json_decode( $website->plugin_upgrades, true ); | |
| 240 | + | |
| 241 | + if ( is_array( $plugin_upgrades ) ) { | |
| 242 | + $ignored_plugins = json_decode( $website->ignored_plugins, true ); | |
| 243 | + if ( is_array( $ignored_plugins ) ) { | |
| 244 | + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins ); | |
| 245 | + } | |
| 246 | + | |
| 247 | + $ignored_plugins = json_decode( $userExtension->ignored_plugins, true ); | |
| 248 | + if ( is_array( $ignored_plugins ) ) { | |
| 249 | + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins ); | |
| 250 | + } | |
| 251 | + | |
| 252 | + $tmp = array(); | |
| 253 | + foreach ( $plugin_upgrades as $plugin_upgrade ) { | |
| 254 | + $tmp[] = array( 'name' => $plugin_upgrade[ 'update' ][ 'slug' ], 'version' => $plugin_upgrade[ 'Version' ], 'new_version' => $plugin_upgrade[ 'update' ][ 'new_version' ] ); | |
| 255 | + } | |
| 256 | + $websites_to_upgrade[] = array( 'id' => $website->id, 'name' => $website->name, 'plugins' => $tmp ); | |
| 257 | + } | |
| 258 | + } | |
| 259 | + | |
| 260 | + $idLength = strlen( 'id' ); | |
| 261 | + $nameLength = strlen( 'name' ); | |
| 262 | + $pluginLength = strlen( 'plugin' ); | |
| 263 | + $oldVersionLength = strlen( 'version' ); | |
| 264 | + $newVersionLength = strlen( 'new version' ); | |
| 265 | + foreach ( $websites_to_upgrade as $website_to_upgrade ) { | |
| 266 | + if ( $idLength < strlen( $website_to_upgrade[ 'id' ] ) ) | |
| 267 | + $idLength = strlen( $website_to_upgrade[ 'id' ] ); | |
| 268 | + if ( $nameLength < strlen( $website_to_upgrade[ 'name' ] ) ) | |
| 269 | + $nameLength = strlen( $website_to_upgrade[ 'name' ] ); | |
| 270 | + | |
| 271 | + foreach ( $website_to_upgrade[ 'plugins' ] as $plugin_to_upgrade ) { | |
| 272 | + if ( $pluginLength < strlen( $plugin_to_upgrade[ 'name' ] ) ) | |
| 273 | + $pluginLength = strlen( $plugin_to_upgrade[ 'name' ] ); | |
| 274 | + if ( $oldVersionLength < strlen( $plugin_to_upgrade[ 'version' ] ) ) | |
| 275 | + $oldVersionLength = strlen( $plugin_to_upgrade[ 'version' ] ); | |
| 276 | + if ( $newVersionLength < strlen( $plugin_to_upgrade[ 'new_version' ] ) ) | |
| 277 | + $newVersionLength = strlen( $plugin_to_upgrade[ 'new_version' ] ); | |
| 278 | + } | |
| 279 | + } | |
| 280 | + | |
| 281 | + | |
| 282 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 283 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $pluginLength . "s | %-" . $oldVersionLength . "s | %-" . $newVersionLength . "s |", 'id', 'name', 'plugin', 'version', 'new version' ) ); | |
| 284 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 285 | + | |
| 286 | + foreach ( $websites_to_upgrade as $website_to_upgrade ) { | |
| 287 | + if ( $idLength < strlen( $website_to_upgrade[ 'id' ] ) ) | |
| 288 | + $idLength = strlen( $website_to_upgrade[ 'id' ] ); | |
| 289 | + if ( $nameLength < strlen( $website_to_upgrade[ 'name' ] ) ) | |
| 290 | + $nameLength = strlen( $website_to_upgrade[ 'name' ] ); | |
| 291 | + | |
| 292 | + $i = 0; | |
| 293 | + foreach ( $website_to_upgrade[ 'plugins' ] as $plugin_to_upgrade ) { | |
| 294 | + if ( $i == 0 ) { | |
| 295 | + 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' ] ) ); | |
| 296 | + } else { | |
| 297 | + 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' ] ) ); | |
| 298 | + } | |
| 299 | + $i++; | |
| 300 | + } | |
| 301 | + } | |
| 302 | + | |
| 303 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 304 | + } else if ( isset( $assoc_args[ 'list-all' ] ) ) { | |
| 305 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 306 | + $userExtension = MainWP_DB::Instance()->getUserExtension(); | |
| 307 | + $websites_to_list = array(); | |
| 308 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 309 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) { | |
| 310 | + continue; | |
| 311 | + } | |
| 312 | + | |
| 313 | + $plugins_to_list = json_decode( $website->plugins, true ); | |
| 314 | + if ( is_array( $plugins_to_list ) ) { | |
| 315 | + $tmp = array(); | |
| 316 | + foreach ( $plugins_to_list as $plugin ) { | |
| 317 | + $tmp[] = array( 'name' => $plugin[ 'slug' ], 'version' => $plugin[ 'version' ] ); | |
| 318 | + } | |
| 319 | + $websites_to_list[] = array( 'id' => $website->id, 'name' => $website->name, 'plugins' => $tmp ); | |
| 320 | + } | |
| 321 | + } | |
| 322 | + | |
| 323 | + $idLength = strlen( 'id' ); | |
| 324 | + $nameLength = strlen( 'name' ); | |
| 325 | + $pluginLength = strlen( 'plugin' ); | |
| 326 | + $oldVersionLength = strlen( 'version' ); | |
| 327 | + foreach ( $websites_to_list as $website_item ) { | |
| 328 | + if ( $idLength < strlen( $website_item[ 'id' ] ) ) | |
| 329 | + $idLength = strlen( $website_item[ 'id' ] ); | |
| 330 | + if ( $nameLength < strlen( $website_item[ 'name' ] ) ) | |
| 331 | + $nameLength = strlen( $website_item[ 'name' ] ); | |
| 332 | + | |
| 333 | + foreach ( $website_item[ 'plugins' ] as $plugin_item ) { | |
| 334 | + if ( $pluginLength < strlen( $plugin_item[ 'name' ] ) ) | |
| 335 | + $pluginLength = strlen( $plugin_item[ 'name' ] ); | |
| 336 | + if ( $oldVersionLength < strlen( $plugin_item[ 'version' ] ) ) | |
| 337 | + $oldVersionLength = strlen( $plugin_item[ 'version' ] ); | |
| 338 | + } | |
| 339 | + } | |
| 340 | + | |
| 341 | + | |
| 342 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+", '', '', '', '' ) ); | |
| 343 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $pluginLength . "s | %-" . $oldVersionLength . "s |", 'id', 'name', 'plugin', 'version' ) ); | |
| 344 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+", '', '', '', '' ) ); | |
| 345 | + | |
| 346 | + foreach ( $websites_to_list as $website_item ) { | |
| 347 | + if ( $idLength < strlen( $website_item[ 'id' ] ) ) | |
| 348 | + $idLength = strlen( $website_item[ 'id' ] ); | |
| 349 | + if ( $nameLength < strlen( $website_item[ 'name' ] ) ) | |
| 350 | + $nameLength = strlen( $website_item[ 'name' ] ); | |
| 351 | + | |
| 352 | + $i = 0; | |
| 353 | + foreach ( $website_item[ 'plugins' ] as $plugin_item ) { | |
| 354 | + if ( $i == 0 ) { | |
| 355 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $pluginLength . "s | %-" . $oldVersionLength . "s |", $website_item[ 'id' ], $website_item[ 'name' ], $plugin_item[ 'name' ], $plugin_item[ 'version' ] ) ); | |
| 356 | + } else { | |
| 357 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $pluginLength . "s | %-" . $oldVersionLength . "s |", '', '', $plugin_item[ 'name' ], $plugin_item[ 'version' ] ) ); | |
| 358 | + } | |
| 359 | + $i++; | |
| 360 | + } | |
| 361 | + } | |
| 362 | + | |
| 363 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $pluginLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+", '', '', '', '' ) ); | |
| 364 | + } else if ( isset( $assoc_args[ 'upgrade' ] ) || isset( $assoc_args[ 'upgrade-all' ] ) ) { | |
| 365 | + //slugs to update | |
| 366 | + | |
| 367 | + $pluginSlugs = array(); | |
| 368 | + if ( isset( $assoc_args[ 'upgrade' ] ) ) { | |
| 369 | + $pluginSlugs = explode( ',', $assoc_args[ 'upgrade' ] ); | |
| 370 | + } | |
| 371 | + | |
| 372 | + | |
| 373 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 374 | + $userExtension = MainWP_DB::Instance()->getUserExtension(); | |
| 375 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 376 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) { | |
| 377 | + continue; | |
| 378 | + } | |
| 379 | + | |
| 380 | + $plugin_upgrades = json_decode( $website->plugin_upgrades, true ); | |
| 381 | + | |
| 382 | + if ( is_array( $plugin_upgrades ) ) { | |
| 383 | + $ignored_plugins = json_decode( $website->ignored_plugins, true ); | |
| 384 | + if ( is_array( $ignored_plugins ) ) { | |
| 385 | + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins ); | |
| 386 | + } | |
| 387 | + | |
| 388 | + $ignored_plugins = json_decode( $userExtension->ignored_plugins, true ); | |
| 389 | + if ( is_array( $ignored_plugins ) ) { | |
| 390 | + $plugin_upgrades = array_diff_key( $plugin_upgrades, $ignored_plugins ); | |
| 391 | + } | |
| 392 | + | |
| 393 | + $tmp = array(); | |
| 394 | + foreach ( $plugin_upgrades as $key => $plugin_upgrade ) { | |
| 395 | + if ( ( count( $pluginSlugs ) > 0 ) && (!in_array( $plugin_upgrade[ 'update' ][ 'slug' ], $pluginSlugs ) ) ) | |
| 396 | + continue; | |
| 397 | + | |
| 398 | + $tmp[] = $key; | |
| 399 | + } | |
| 400 | + | |
| 401 | + if ( count( $tmp ) == 0 ) { | |
| 402 | + WP_CLI::line( 'No available plugin updates for ' . $website->name ); | |
| 403 | + | |
| 404 | + continue; | |
| 405 | + } | |
| 406 | + | |
| 407 | + WP_CLI::line( 'Updating ' . count( $tmp ) . ' plugins for ' . $website->name ); | |
| 408 | + | |
| 409 | + try { | |
| 410 | + MainWP_Updates::upgradePluginThemeTranslation( $website->id, 'plugin', implode( ',', $tmp ) ); | |
| 411 | + WP_CLI::success( 'Updates completed' ); | |
| 412 | + } catch ( Exception $e ) { | |
| 413 | + WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::getConsoleErrorMessage( $e ) ); | |
| 414 | + if ( $e->getMesage() == 'WPERROR' ) { | |
| 415 | + WP_CLI::debug( 'Error: ' . print_r( $e->getMessageExtra(), 1 ) ); | |
| 416 | + } | |
| 417 | + } | |
| 418 | + } | |
| 419 | + } | |
| 420 | + } | |
| 421 | + } | |
| 422 | + | |
| 423 | + /** | |
| 424 | + * List information about theme updates | |
| 425 | + * | |
| 426 | + * ## OPTIONS | |
| 427 | + * | |
| 428 | + * [<websiteid>] | |
| 429 | + * : The id (or ids, comma separated) of the child sites that need to be listed/upgraded, when omitted all childsites are used. | |
| 430 | + * | |
| 431 | + * [--list] | |
| 432 | + * : Get a list of themes with available updates | |
| 433 | + * | |
| 434 | + * [--list-all] | |
| 435 | + * : list all themes | |
| 436 | + * | |
| 437 | + * [--upgrade=<theme>] | |
| 438 | + * : Update the themes | |
| 439 | + * | |
| 440 | + * [--upgrade-all] | |
| 441 | + * : Update all themes | |
| 442 | + * | |
| 443 | + * ## EXAMPLES | |
| 444 | + * | |
| 445 | + * wp mainwp theme 2,5 --list | |
| 446 | + * wp mainwp theme --list | |
| 447 | + * wp mainwp theme 2,5 --upgrade-all | |
| 448 | + * wp mainwp theme 2,5 --upgrade=twentysixteen | |
| 449 | + * | |
| 450 | + * @synopsis [<websiteid>] [--list] [--list-all] [--upgrade=<theme>] [--upgrade-all] | |
| 451 | + */ | |
| 452 | + public function theme( $args, $assoc_args ) { | |
| 453 | + $sites = array(); | |
| 454 | + if ( count( $args ) > 0 ) { | |
| 455 | + $args_exploded = explode( ',', $args[ 0 ] ); | |
| 456 | + foreach ( $args_exploded as $arg ) { | |
| 457 | + if ( !is_numeric( trim( $arg ) ) ) { | |
| 458 | + WP_CLI::error( 'Child site IDs should be numeric.' ); | |
| 459 | + } | |
| 460 | + | |
| 461 | + $sites[] = trim( $arg ); | |
| 462 | + } | |
| 463 | + } | |
| 464 | + | |
| 465 | + if ( isset( $assoc_args[ 'list' ] ) ) { | |
| 466 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 467 | + $userExtension = MainWP_DB::Instance()->getUserExtension(); | |
| 468 | + $websites_to_upgrade = array(); | |
| 469 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 470 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) { | |
| 471 | + continue; | |
| 472 | + } | |
| 473 | + | |
| 474 | + $theme_upgrades = json_decode( $website->theme_upgrades, true ); | |
| 475 | + | |
| 476 | + if ( is_array( $theme_upgrades ) ) { | |
| 477 | + $ignored_themes = json_decode( $website->ignored_themes, true ); | |
| 478 | + if ( is_array( $ignored_themes ) ) { | |
| 479 | + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes ); | |
| 480 | + } | |
| 481 | + | |
| 482 | + $ignored_themes = json_decode( $userExtension->ignored_themes, true ); | |
| 483 | + if ( is_array( $ignored_themes ) ) { | |
| 484 | + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes ); | |
| 485 | + } | |
| 486 | + | |
| 487 | + $tmp = array(); | |
| 488 | + foreach ( $theme_upgrades as $theme_upgrade ) { | |
| 489 | + $tmp[] = array( 'name' => $theme_upgrade[ 'update' ][ 'theme' ], 'version' => $theme_upgrade[ 'Version' ], 'new_version' => $theme_upgrade[ 'update' ][ 'new_version' ] ); | |
| 490 | + } | |
| 491 | + $websites_to_upgrade[] = array( 'id' => $website->id, 'name' => $website->name, 'themes' => $tmp ); | |
| 492 | + } | |
| 493 | + } | |
| 494 | + | |
| 495 | + $idLength = strlen( 'id' ); | |
| 496 | + $nameLength = strlen( 'name' ); | |
| 497 | + $themeLength = strlen( 'theme' ); | |
| 498 | + $oldVersionLength = strlen( 'version' ); | |
| 499 | + $newVersionLength = strlen( 'new version' ); | |
| 500 | + foreach ( $websites_to_upgrade as $website_to_upgrade ) { | |
| 501 | + if ( $idLength < strlen( $website_to_upgrade[ 'id' ] ) ) | |
| 502 | + $idLength = strlen( $website_to_upgrade[ 'id' ] ); | |
| 503 | + if ( $nameLength < strlen( $website_to_upgrade[ 'name' ] ) ) | |
| 504 | + $nameLength = strlen( $website_to_upgrade[ 'name' ] ); | |
| 505 | + | |
| 506 | + foreach ( $website_to_upgrade[ 'themes' ] as $theme_to_upgrade ) { | |
| 507 | + if ( $themeLength < strlen( $theme_to_upgrade[ 'name' ] ) ) | |
| 508 | + $themeLength = strlen( $theme_to_upgrade[ 'name' ] ); | |
| 509 | + if ( $oldVersionLength < strlen( $theme_to_upgrade[ 'version' ] ) ) | |
| 510 | + $oldVersionLength = strlen( $theme_to_upgrade[ 'version' ] ); | |
| 511 | + if ( $newVersionLength < strlen( $theme_to_upgrade[ 'new_version' ] ) ) | |
| 512 | + $newVersionLength = strlen( $theme_to_upgrade[ 'new_version' ] ); | |
| 513 | + } | |
| 514 | + } | |
| 515 | + | |
| 516 | + | |
| 517 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 518 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $themeLength . "s | %-" . $oldVersionLength . "s | %-" . $newVersionLength . "s |", 'id', 'name', 'theme', 'version', 'new version' ) ); | |
| 519 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 520 | + | |
| 521 | + foreach ( $websites_to_upgrade as $website_to_upgrade ) { | |
| 522 | + if ( $idLength < strlen( $website_to_upgrade[ 'id' ] ) ) | |
| 523 | + $idLength = strlen( $website_to_upgrade[ 'id' ] ); | |
| 524 | + if ( $nameLength < strlen( $website_to_upgrade[ 'name' ] ) ) | |
| 525 | + $nameLength = strlen( $website_to_upgrade[ 'name' ] ); | |
| 526 | + | |
| 527 | + $i = 0; | |
| 528 | + foreach ( $website_to_upgrade[ 'themes' ] as $theme_to_upgrade ) { | |
| 529 | + if ( $i == 0 ) { | |
| 530 | + 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' ] ) ); | |
| 531 | + } else { | |
| 532 | + 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' ] ) ); | |
| 533 | + } | |
| 534 | + $i++; | |
| 535 | + } | |
| 536 | + } | |
| 537 | + | |
| 538 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+%'--" . ( $newVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 539 | + } else if ( isset( $assoc_args[ 'list-all' ] ) ) { | |
| 540 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 541 | + $userExtension = MainWP_DB::Instance()->getUserExtension(); | |
| 542 | + $websites_to_upgrade = array(); | |
| 543 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 544 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) { | |
| 545 | + continue; | |
| 546 | + } | |
| 547 | + $theme_to_list = json_decode( $website->themes, true ); | |
| 548 | + if ( is_array( $theme_to_list ) ) { | |
| 549 | + $tmp = array(); | |
| 550 | + foreach ( $theme_to_list as $theme ) { | |
| 551 | + $tmp[] = array( 'name' => $theme[ 'name' ], 'active' => $theme[ 'active' ] ? 'yes' : '', 'version' => $theme[ 'version' ] ); | |
| 552 | + } | |
| 553 | + $websites_to_upgrade[] = array( 'id' => $website->id, 'name' => $website->name, 'themes' => $tmp ); | |
| 554 | + } | |
| 555 | + } | |
| 556 | + | |
| 557 | + $idLength = strlen( 'id' ); | |
| 558 | + $nameLength = strlen( 'name' ); | |
| 559 | + $themeLength = strlen( 'theme' ); | |
| 560 | + $oldVersionLength = strlen( 'version' ); | |
| 561 | + $activeLength = strlen( 'active' ); | |
| 562 | + foreach ( $websites_to_upgrade as $website_item ) { | |
| 563 | + if ( $idLength < strlen( $website_item[ 'id' ] ) ) | |
| 564 | + $idLength = strlen( $website_item[ 'id' ] ); | |
| 565 | + if ( $nameLength < strlen( $website_item[ 'name' ] ) ) | |
| 566 | + $nameLength = strlen( $website_item[ 'name' ] ); | |
| 567 | + | |
| 568 | + foreach ( $website_item[ 'themes' ] as $theme_item ) { | |
| 569 | + if ( $themeLength < strlen( $theme_item[ 'name' ] ) ) | |
| 570 | + $themeLength = strlen( $theme_item[ 'name' ] ); | |
| 571 | + if ( $oldVersionLength < strlen( $theme_item[ 'version' ] ) ) | |
| 572 | + $oldVersionLength = strlen( $theme_item[ 'version' ] ); | |
| 573 | + } | |
| 574 | + } | |
| 575 | + | |
| 576 | + | |
| 577 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 578 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $themeLength . "s | %-" . $activeLength . "s | %-" . $oldVersionLength . "s |", 'id', 'name', 'theme', 'active', 'version' ) ); | |
| 579 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 580 | + | |
| 581 | + foreach ( $websites_to_upgrade as $website_item ) { | |
| 582 | + if ( $idLength < strlen( $website_item[ 'id' ] ) ) | |
| 583 | + $idLength = strlen( $website_item[ 'id' ] ); | |
| 584 | + if ( $nameLength < strlen( $website_item[ 'name' ] ) ) | |
| 585 | + $nameLength = strlen( $website_item[ 'name' ] ); | |
| 586 | + | |
| 587 | + $i = 0; | |
| 588 | + foreach ( $website_item[ 'themes' ] as $theme_item ) { | |
| 589 | + if ( $i == 0 ) { | |
| 590 | + 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' ] ) ); | |
| 591 | + } else { | |
| 592 | + WP_CLI::line( sprintf( "| %-" . $idLength . "s | %-" . $nameLength . "s | %-" . $themeLength . "s | %-" . $activeLength . "s | %-" . $oldVersionLength . "s |", '', '', $theme_item[ 'name' ], $theme_item[ 'active' ], $theme_item[ 'version' ] ) ); | |
| 593 | + } | |
| 594 | + $i++; | |
| 595 | + } | |
| 596 | + } | |
| 597 | + | |
| 598 | + WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $themeLength + 2 ) . "s+%'--" . ( $activeLength + 2 ) . "s+%'--" . ( $oldVersionLength + 2 ) . "s+", '', '', '', '', '' ) ); | |
| 599 | + } else if ( isset( $assoc_args[ 'upgrade' ] ) || isset( $assoc_args[ 'upgrade-all' ] ) ) { | |
| 600 | + //slugs to update | |
| 601 | + | |
| 602 | + $themeSlugs = array(); | |
| 603 | + if ( isset( $assoc_args[ 'upgrade' ] ) ) { | |
| 604 | + $themeSlugs = explode( ',', $assoc_args[ 'upgrade' ] ); | |
| 605 | + } | |
| 606 | + | |
| 607 | + | |
| 608 | + $websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser( false, null, 'wp.url', false, false, null, true ) ); | |
| 609 | + $userExtension = MainWP_DB::Instance()->getUserExtension(); | |
| 610 | + while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { | |
| 611 | + if ( ( count( $sites ) > 0 ) && (!in_array( $website->id, $sites ) ) ) { | |
| 612 | + continue; | |
| 613 | + } | |
| 614 | + | |
| 615 | + $theme_upgrades = json_decode( $website->theme_upgrades, true ); | |
| 616 | + | |
| 617 | + if ( is_array( $theme_upgrades ) ) { | |
| 618 | + $ignored_themes = json_decode( $website->ignored_themes, true ); | |
| 619 | + if ( is_array( $ignored_themes ) ) { | |
| 620 | + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes ); | |
| 621 | + } | |
| 622 | + | |
| 623 | + $ignored_themes = json_decode( $userExtension->ignored_themes, true ); | |
| 624 | + if ( is_array( $ignored_themes ) ) { | |
| 625 | + $theme_upgrades = array_diff_key( $theme_upgrades, $ignored_themes ); | |
| 626 | + } | |
| 627 | + | |
| 628 | + $tmp = array(); | |
| 629 | + foreach ( $theme_upgrades as $key => $theme_upgrade ) { | |
| 630 | + if ( ( count( $themeSlugs ) > 0 ) && (!in_array( $theme_upgrade[ 'update' ][ 'slug' ], $themeSlugs ) ) ) | |
| 631 | + continue; | |
| 632 | + | |
| 633 | + $tmp[] = $key; | |
| 634 | + } | |
| 635 | + | |
| 636 | + if ( count( $tmp ) == 0 ) { | |
| 637 | + WP_CLI::line( 'No available theme updates for ' . $website->name ); | |
| 638 | + | |
| 639 | + continue; | |
| 640 | + } | |
| 641 | + | |
| 642 | + WP_CLI::line( 'Updating ' . count( $tmp ) . ' themes for ' . $website->name ); | |
| 643 | + | |
| 644 | + try { | |
| 645 | + MainWP_Updates::upgradePluginThemeTranslation( $website->id, 'theme', implode( ',', $tmp ) ); | |
| 646 | + WP_CLI::success( 'Updates completed' ); | |
| 647 | + } catch ( Exception $e ) { | |
| 648 | + WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::getConsoleErrorMessage( $e ) ); | |
| 649 | + if ( $e->getMesage() == 'WPERROR' ) { | |
| 650 | + WP_CLI::debug( 'Error: ' . print_r( $e->getMessageExtra(), 1 ) ); | |
| 651 | + } | |
| 652 | + } | |
| 653 | + } | |
| 654 | + } | |
| 655 | + } | |
| 656 | + } | |
| 657 | + | |
| 658 | +} | |