MatomoCommands.php
116 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo\Commands; |
| 11 | |
| 12 | use WpMatomo\Installer; |
| 13 | use WpMatomo\Settings; |
| 14 | use WpMatomo\Uninstaller; |
| 15 | use WP_CLI; |
| 16 | use WP_CLI_Command; |
| 17 | use WpMatomo\Updater; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | if ( ! defined( 'WP_CLI' ) ) { |
| 23 | exit; |
| 24 | } |
| 25 | |
| 26 | class MatomoCommands extends WP_CLI_Command { |
| 27 | /** |
| 28 | * Uninstalls Matomo. |
| 29 | * |
| 30 | * ## OPTIONS |
| 31 | * |
| 32 | * [--force] |
| 33 | * : To delete all data stored in all tables |
| 34 | * |
| 35 | * ## EXAMPLES |
| 36 | * |
| 37 | * wp matomo uninstall --force |
| 38 | * |
| 39 | * @when after_wp_load |
| 40 | */ |
| 41 | public function uninstall( $args, $assoc_args ) { |
| 42 | if ( ! empty( $assoc_args['force'] ) ) { |
| 43 | $delete_all_data = true; |
| 44 | WP_CLI::log( 'Deleting all data is forced.' ); |
| 45 | } else { |
| 46 | $delete_all_data = false; |
| 47 | WP_CLI::log( 'Deleting all data is NOT forced. To remove all data set the --force option.' ); |
| 48 | } |
| 49 | |
| 50 | $uninstaller = new Uninstaller(); |
| 51 | $uninstaller->uninstall( $delete_all_data ); |
| 52 | |
| 53 | WP_CLI::success( 'Uninstalled Matomo Analytics' ); |
| 54 | } |
| 55 | /** |
| 56 | * Updates Matomo. |
| 57 | * |
| 58 | * ## OPTIONS |
| 59 | * |
| 60 | * [--force] |
| 61 | * : To force running the update |
| 62 | * |
| 63 | * ## EXAMPLES |
| 64 | * |
| 65 | * wp matomo update --force |
| 66 | * |
| 67 | * @when after_wp_load |
| 68 | */ |
| 69 | public function update( $args, $assoc_args ) { |
| 70 | if ( function_exists('is_multisite') && is_multisite() && function_exists( 'get_sites' ) ) { |
| 71 | foreach ( get_sites() as $site ) { |
| 72 | /** @var \WP_Site $site */ |
| 73 | switch_to_blog( $site->blog_id ); |
| 74 | // this way we make sure all blogs get updated eventually |
| 75 | WP_CLI::log( 'Blog ID' . $site->blog_id ); |
| 76 | $this->_doUpdate( ! empty( $assoc_args['force'] ) ); |
| 77 | restore_current_blog(); |
| 78 | } |
| 79 | } else { |
| 80 | $this->_doUpdate( ! empty( $assoc_args['force'] ) ); |
| 81 | } |
| 82 | |
| 83 | WP_CLI::success( 'Matomo Analytics Updater finished' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param $assoc_args |
| 88 | */ |
| 89 | public function _doUpdate( $force ) { |
| 90 | $settings = new Settings(); |
| 91 | |
| 92 | $installer = new Installer( $settings ); |
| 93 | if ( ! $installer->looks_like_it_is_installed() ) { |
| 94 | WP_CLI::log( 'Skipping as looks like Matomo is not yet installed' ); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $updater = new Updater( $settings ); |
| 99 | if ( $force ) { |
| 100 | WP_CLI::log( 'Force running updates' ); |
| 101 | $updater->update(); |
| 102 | } else { |
| 103 | WP_CLI::log( 'Running update if needed' ); |
| 104 | $updater->update_if_needed(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | WP_CLI::add_command( |
| 110 | 'matomo', |
| 111 | '\WpMatomo\Commands\MatomoCommands', |
| 112 | array( |
| 113 | 'shortdesc' => 'Manage your Matomo Analytics. Commands are recommended only to be used in development mode', |
| 114 | ) |
| 115 | ); |
| 116 |