MatomoCommands.php
174 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 | require_once ABSPATH . '/wp-includes/ms-blogs.php'; |
| 13 | |
| 14 | use Piwik\Access; |
| 15 | use WP_CLI; |
| 16 | use WP_CLI_Command; |
| 17 | use WP_Site; |
| 18 | use WpMatomo\Installer; |
| 19 | use WpMatomo\Settings; |
| 20 | use WpMatomo\Uninstaller; |
| 21 | use WpMatomo\Updater; |
| 22 | use WpMatomo\WpStatistics\Importer; |
| 23 | use WpMatomo\WpStatistics\Logger\WpCliLogger; |
| 24 | use WpMatomo\Bootstrap; |
| 25 | use WpMatomo\Site; |
| 26 | if ( ! defined( 'ABSPATH' ) ) { |
| 27 | exit; |
| 28 | } |
| 29 | if ( ! defined( 'WP_CLI' ) ) { |
| 30 | exit; |
| 31 | } |
| 32 | |
| 33 | class MatomoCommands extends WP_CLI_Command { |
| 34 | /** |
| 35 | * Uninstalls Matomo. |
| 36 | * |
| 37 | * ## OPTIONS |
| 38 | * |
| 39 | * [--force] |
| 40 | * : To delete all data stored in all tables |
| 41 | * |
| 42 | * ## EXAMPLES |
| 43 | * |
| 44 | * wp matomo uninstall --force |
| 45 | * |
| 46 | * @when after_wp_load |
| 47 | */ |
| 48 | public function uninstall( $args, $assoc_args ) { |
| 49 | if ( ! empty( $assoc_args['force'] ) ) { |
| 50 | $delete_all_data = true; |
| 51 | WP_CLI::log( 'Deleting all data is forced.' ); |
| 52 | } else { |
| 53 | $delete_all_data = false; |
| 54 | WP_CLI::log( 'Deleting all data is NOT forced. To remove all data set the --force option.' ); |
| 55 | } |
| 56 | |
| 57 | $uninstaller = new Uninstaller(); |
| 58 | $uninstaller->uninstall( $delete_all_data ); |
| 59 | |
| 60 | WP_CLI::success( 'Uninstalled Matomo Analytics' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Imports wp-statistics data |
| 65 | * |
| 66 | * ## OPTIONS |
| 67 | * |
| 68 | * [--blog=<blogId>] |
| 69 | * : the blog id to import. Only needed if using WP MultiSite and only wanting to import one blog |
| 70 | * ## EXAMPLES |
| 71 | * |
| 72 | * wp matomo importWpStatistics --blog=1 |
| 73 | * |
| 74 | * @when after_wp_load |
| 75 | */ |
| 76 | public function importWpStatistics( $args, $assoc_args ) { |
| 77 | $logger = new WpCliLogger(); |
| 78 | if ( ! is_plugin_active( 'wp-statistics/wp-statistics.php' ) ) { |
| 79 | $logger->error( 'Plugin wpstatistics must be installed and activated' ); |
| 80 | return; |
| 81 | } |
| 82 | $logger->info( 'Starting wp-statistics import' ); |
| 83 | try { |
| 84 | Bootstrap::do_bootstrap(); |
| 85 | Access::getInstance()->setSuperUserAccess( true ); |
| 86 | $importer = new Importer( $logger ); |
| 87 | if ( function_exists( 'is_multisite' ) && is_multisite() && function_exists( 'get_sites' ) ) { |
| 88 | $id_blog = ! empty( $assoc_args['blog'] ) ? $assoc_args['blog'] : null; |
| 89 | foreach ( get_sites() as $site ) { |
| 90 | /** @var WP_Site $site */ |
| 91 | if ( is_null( $id_blog ) || ( $site->blog_id === $id_blog ) ) { |
| 92 | switch_to_blog( $site->blog_id ); |
| 93 | // this way we make sure all blogs get updated eventually |
| 94 | $logger->info( 'Blog ID ' . $site->blog_id ); |
| 95 | $id_site = Site::get_matomo_site_id( $site->blog_id ); |
| 96 | $importer->import( $id_site ); |
| 97 | restore_current_blog(); |
| 98 | } |
| 99 | } |
| 100 | } else { |
| 101 | $site = new Site(); |
| 102 | $id_site = $site->get_current_matomo_site_id(); |
| 103 | $importer->import( $id_site ); |
| 104 | } |
| 105 | |
| 106 | $logger->info( 'Matomo Analytics wp-statistics import finished' ); |
| 107 | } catch ( \Exception $e ) { |
| 108 | $logger->error( $e->getMessage() ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Updates Matomo. |
| 114 | * |
| 115 | * ## OPTIONS |
| 116 | * |
| 117 | * [--force] |
| 118 | * : To force running the update |
| 119 | * |
| 120 | * ## EXAMPLES |
| 121 | * |
| 122 | * wp matomo update --force |
| 123 | * |
| 124 | * @when after_wp_load |
| 125 | */ |
| 126 | public function update( $args, $assoc_args ) { |
| 127 | if ( function_exists( 'is_multisite' ) && is_multisite() && function_exists( 'get_sites' ) ) { |
| 128 | foreach ( get_sites() as $site ) { |
| 129 | /** @var WP_Site $site */ |
| 130 | switch_to_blog( $site->blog_id ); |
| 131 | // this way we make sure all blogs get updated eventually |
| 132 | WP_CLI::log( 'Blog ID' . $site->blog_id ); |
| 133 | $this->do_update( ! empty( $assoc_args['force'] ) ); |
| 134 | restore_current_blog(); |
| 135 | } |
| 136 | } else { |
| 137 | $this->do_update( ! empty( $assoc_args['force'] ) ); |
| 138 | } |
| 139 | |
| 140 | WP_CLI::success( 'Matomo Analytics Updater finished' ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param $assoc_args |
| 145 | */ |
| 146 | private function do_update( $force ) { |
| 147 | $settings = new Settings(); |
| 148 | |
| 149 | $installer = new Installer( $settings ); |
| 150 | if ( ! $installer->looks_like_it_is_installed() ) { |
| 151 | WP_CLI::log( 'Skipping as looks like Matomo is not yet installed' ); |
| 152 | |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $updater = new Updater( $settings ); |
| 157 | if ( $force ) { |
| 158 | WP_CLI::log( 'Force running updates' ); |
| 159 | $updater->update(); |
| 160 | } else { |
| 161 | WP_CLI::log( 'Running update if needed' ); |
| 162 | $updater->update_if_needed(); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | WP_CLI::add_command( |
| 168 | 'matomo', |
| 169 | '\WpMatomo\Commands\MatomoCommands', |
| 170 | [ |
| 171 | 'shortdesc' => 'Manage your Matomo Analytics. Commands are recommended only to be used in development mode', |
| 172 | ] |
| 173 | ); |
| 174 |