MatomoCommands.php
263 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 Piwik\Access; |
| 13 | use WP_CLI; |
| 14 | use WP_CLI_Command; |
| 15 | use WP_Site; |
| 16 | use WpMatomo\Installer; |
| 17 | use WpMatomo\Settings; |
| 18 | use WpMatomo\Uninstaller; |
| 19 | use WpMatomo\Updater; |
| 20 | use WpMatomo\WpStatistics\Importer; |
| 21 | use WpMatomo\WpStatistics\Logger\WpCliLogger; |
| 22 | use WpMatomo\Bootstrap; |
| 23 | use WpMatomo\Site; |
| 24 | use WpMatomo\User; |
| 25 | |
| 26 | if ( ! defined( 'ABSPATH' ) ) { |
| 27 | exit; |
| 28 | } |
| 29 | if ( ! defined( 'WP_CLI' ) ) { |
| 30 | exit; |
| 31 | } |
| 32 | |
| 33 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 34 | require_once ABSPATH . '/wp-includes/ms-blogs.php'; |
| 35 | } |
| 36 | |
| 37 | class MatomoCommands extends WP_CLI_Command { |
| 38 | |
| 39 | /** |
| 40 | * Installs Matomo if not already installed. |
| 41 | * |
| 42 | * @when after_wp_load |
| 43 | */ |
| 44 | public function install() { |
| 45 | $installer = new Installer( \WpMatomo::$settings ); |
| 46 | $installer->register_hooks(); |
| 47 | if ( $installer->looks_like_it_is_installed() ) { |
| 48 | WP_CLI::success( 'Already installed.' ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( ! $installer->can_be_installed() ) { |
| 53 | WP_CLI::error( 'Unable to install.' ); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $installer->install(); |
| 58 | WP_CLI::success( 'Finished installing Matomo.' ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets or sets a Matomo for WordPress global setting. |
| 63 | * |
| 64 | * ## OPTIONS |
| 65 | * |
| 66 | * <mode> |
| 67 | * : Either 'get' or 'set'. |
| 68 | * |
| 69 | * <name> |
| 70 | * : The name of the setting. |
| 71 | * |
| 72 | * [<value>] |
| 73 | * : Required if 'set' is used. The value to set the setting to. |
| 74 | * |
| 75 | * @when after_wp_load |
| 76 | */ |
| 77 | public function globalSetting( $args, $assoc_args ) { |
| 78 | $mode = $args[0]; |
| 79 | $key = $args[1]; |
| 80 | |
| 81 | if ( 'set' === $mode ) { |
| 82 | $value = $args[2]; |
| 83 | \WpMatomo::$settings->set_global_option( $key, $value ); |
| 84 | \WpMatomo::$settings->save(); |
| 85 | WP_CLI::success( sprintf( 'Modified Matomo setting %s.', $key ) ); |
| 86 | } elseif ( 'get' === $mode ) { |
| 87 | $value = \WpMatomo::$settings->get_global_option( $key ); |
| 88 | WP_CLI::success( $value ); |
| 89 | } else { |
| 90 | WP_CLI::error( sprintf( 'Invalid mode "%s".', $mode ) ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Perform a site or user sync manually. |
| 96 | * |
| 97 | * ## OPTIONS |
| 98 | * |
| 99 | * <mode> |
| 100 | * : Either 'sites' or 'users'. |
| 101 | * |
| 102 | * @when after_wp_load |
| 103 | */ |
| 104 | public function sync( $args, $assoc_args ) { |
| 105 | $mode = $args[0]; |
| 106 | |
| 107 | if ( 'sites' === $mode ) { |
| 108 | $sync = new Site\Sync( \WpMatomo::$settings ); |
| 109 | $success = $sync->sync_all(); |
| 110 | if ( ! $success ) { |
| 111 | WP_CLI::error( sprintf( 'Failed to execute site sync, enable logging for more info.' ) ); |
| 112 | } |
| 113 | } elseif ( 'users' === $mode ) { |
| 114 | $sync = new User\Sync(); |
| 115 | $sync->sync_all(); |
| 116 | } else { |
| 117 | WP_CLI::error( sprintf( 'Invalid mode "%s".', $mode ) ); |
| 118 | } |
| 119 | |
| 120 | WP_CLI::success( sprintf( 'Done syncing %s.', $mode ) ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Uninstalls Matomo. |
| 125 | * |
| 126 | * ## OPTIONS |
| 127 | * |
| 128 | * [--force] |
| 129 | * : To delete all data stored in all tables |
| 130 | * |
| 131 | * ## EXAMPLES |
| 132 | * |
| 133 | * wp matomo uninstall --force |
| 134 | * |
| 135 | * @when after_wp_load |
| 136 | */ |
| 137 | public function uninstall( $args, $assoc_args ) { |
| 138 | if ( ! empty( $assoc_args['force'] ) ) { |
| 139 | $delete_all_data = true; |
| 140 | WP_CLI::log( 'Deleting all data is forced.' ); |
| 141 | } else { |
| 142 | $delete_all_data = false; |
| 143 | WP_CLI::log( 'Deleting all data is NOT forced. To remove all data set the --force option.' ); |
| 144 | } |
| 145 | |
| 146 | $uninstaller = new Uninstaller(); |
| 147 | $uninstaller->uninstall( $delete_all_data ); |
| 148 | |
| 149 | WP_CLI::success( 'Uninstalled Matomo Analytics' ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Imports wp-statistics data |
| 154 | * |
| 155 | * ## OPTIONS |
| 156 | * |
| 157 | * [--blog=<blogId>] |
| 158 | * : the blog id to import. Only needed if using WP MultiSite and only wanting to import one blog |
| 159 | * ## EXAMPLES |
| 160 | * |
| 161 | * wp matomo importWpStatistics --blog=1 |
| 162 | * |
| 163 | * @when after_wp_load |
| 164 | */ |
| 165 | public function importWpStatistics( $args, $assoc_args ) { |
| 166 | $logger = new WpCliLogger(); |
| 167 | if ( ! is_plugin_active( 'wp-statistics/wp-statistics.php' ) ) { |
| 168 | $logger->error( 'Plugin wpstatistics must be installed and activated' ); |
| 169 | return; |
| 170 | } |
| 171 | $logger->info( 'Starting wp-statistics import' ); |
| 172 | try { |
| 173 | Bootstrap::do_bootstrap(); |
| 174 | Access::getInstance()->setSuperUserAccess( true ); |
| 175 | $importer = new Importer( $logger ); |
| 176 | if ( function_exists( 'is_multisite' ) && is_multisite() && function_exists( 'get_sites' ) ) { |
| 177 | $id_blog = ! empty( $assoc_args['blog'] ) ? $assoc_args['blog'] : null; |
| 178 | foreach ( get_sites() as $site ) { |
| 179 | /** @var WP_Site $site */ |
| 180 | if ( is_null( $id_blog ) || ( $site->blog_id === $id_blog ) ) { |
| 181 | switch_to_blog( $site->blog_id ); |
| 182 | // this way we make sure all blogs get updated eventually |
| 183 | $logger->info( 'Blog ID ' . $site->blog_id ); |
| 184 | $id_site = Site::get_matomo_site_id( $site->blog_id ); |
| 185 | $importer->import( $id_site ); |
| 186 | restore_current_blog(); |
| 187 | } |
| 188 | } |
| 189 | } else { |
| 190 | $site = new Site(); |
| 191 | $id_site = $site->get_current_matomo_site_id(); |
| 192 | $importer->import( $id_site ); |
| 193 | } |
| 194 | |
| 195 | $logger->info( 'Matomo Analytics wp-statistics import finished' ); |
| 196 | } catch ( \Exception $e ) { |
| 197 | $logger->error( $e->getMessage() ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Updates Matomo. |
| 203 | * |
| 204 | * ## OPTIONS |
| 205 | * |
| 206 | * [--force] |
| 207 | * : To force running the update |
| 208 | * |
| 209 | * ## EXAMPLES |
| 210 | * |
| 211 | * wp matomo update --force |
| 212 | * |
| 213 | * @when after_wp_load |
| 214 | */ |
| 215 | public function update( $args, $assoc_args ) { |
| 216 | if ( function_exists( 'is_multisite' ) && is_multisite() && function_exists( 'get_sites' ) ) { |
| 217 | foreach ( get_sites() as $site ) { |
| 218 | /** @var WP_Site $site */ |
| 219 | switch_to_blog( $site->blog_id ); |
| 220 | // this way we make sure all blogs get updated eventually |
| 221 | WP_CLI::log( 'Blog ID' . $site->blog_id ); |
| 222 | $this->do_update( ! empty( $assoc_args['force'] ) ); |
| 223 | restore_current_blog(); |
| 224 | } |
| 225 | } else { |
| 226 | $this->do_update( ! empty( $assoc_args['force'] ) ); |
| 227 | } |
| 228 | |
| 229 | WP_CLI::success( 'Matomo Analytics Updater finished' ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @param bool $force |
| 234 | */ |
| 235 | private function do_update( $force ) { |
| 236 | $settings = new Settings(); |
| 237 | |
| 238 | $installer = new Installer( $settings ); |
| 239 | if ( ! $installer->looks_like_it_is_installed() ) { |
| 240 | WP_CLI::log( 'Skipping as looks like Matomo is not yet installed' ); |
| 241 | |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | $updater = new Updater( $settings ); |
| 246 | if ( $force ) { |
| 247 | WP_CLI::log( 'Force running updates' ); |
| 248 | $updater->update(); |
| 249 | } else { |
| 250 | WP_CLI::log( 'Running update if needed' ); |
| 251 | $updater->update_if_needed(); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | WP_CLI::add_command( |
| 257 | 'matomo', |
| 258 | '\WpMatomo\Commands\MatomoCommands', |
| 259 | [ |
| 260 | 'shortdesc' => 'Manage your Matomo Analytics. Commands are recommended only to be used in development mode', |
| 261 | ] |
| 262 | ); |
| 263 |