PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.13.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.13.0
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Commands / MatomoCommands.php
matomo / classes / WpMatomo / Commands Last commit date
MatomoCommands.php 4 days ago
MatomoCommands.php
267 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 // number => 0 means no limit. WP_Site_Query defaults to 100, which would silently
179 // skip importing into every blog after that
180 foreach ( get_sites( [ 'number' => 0 ] ) as $site ) {
181 /** @var WP_Site $site */
182 if ( is_null( $id_blog ) || ( $site->blog_id === $id_blog ) ) {
183 switch_to_blog( $site->blog_id );
184 // this way we make sure all blogs get updated eventually
185 $logger->info( 'Blog ID ' . $site->blog_id );
186 $id_site = Site::get_matomo_site_id( $site->blog_id );
187 $importer->import( $id_site );
188 restore_current_blog();
189 }
190 }
191 } else {
192 $site = new Site();
193 $id_site = $site->get_current_matomo_site_id();
194 $importer->import( $id_site );
195 }
196
197 $logger->info( 'Matomo Analytics wp-statistics import finished' );
198 } catch ( \Exception $e ) {
199 $logger->error( $e->getMessage() );
200 }
201 }
202
203 /**
204 * Updates Matomo.
205 *
206 * ## OPTIONS
207 *
208 * [--force]
209 * : To force running the update
210 *
211 * ## EXAMPLES
212 *
213 * wp matomo update --force
214 *
215 * @when after_wp_load
216 */
217 public function update( $args, $assoc_args ) {
218 if ( function_exists( 'is_multisite' ) && is_multisite() && function_exists( 'get_sites' ) ) {
219 // number => 0 means no limit. WP_Site_Query defaults to 100, which would silently leave
220 // every blog after that un-updated
221 foreach ( get_sites( [ 'number' => 0 ] ) as $site ) {
222 /** @var WP_Site $site */
223 switch_to_blog( $site->blog_id );
224 // this way we make sure all blogs get updated eventually
225 WP_CLI::log( 'Blog ID' . $site->blog_id );
226 $this->do_update( ! empty( $assoc_args['force'] ) );
227 restore_current_blog();
228 }
229 } else {
230 $this->do_update( ! empty( $assoc_args['force'] ) );
231 }
232
233 WP_CLI::success( 'Matomo Analytics Updater finished' );
234 }
235
236 /**
237 * @param bool $force
238 */
239 private function do_update( $force ) {
240 $settings = new Settings();
241
242 $installer = new Installer( $settings );
243 if ( ! $installer->looks_like_it_is_installed() ) {
244 WP_CLI::log( 'Skipping as looks like Matomo is not yet installed' );
245
246 return;
247 }
248
249 $updater = new Updater( $settings );
250 if ( $force ) {
251 WP_CLI::log( 'Force running updates' );
252 $updater->update();
253 } else {
254 WP_CLI::log( 'Running update if needed' );
255 $updater->update_if_needed();
256 }
257 }
258 }
259
260 WP_CLI::add_command(
261 'matomo',
262 '\WpMatomo\Commands\MatomoCommands',
263 [
264 'shortdesc' => 'Manage your Matomo Analytics. Commands are recommended only to be used in development mode',
265 ]
266 );
267