PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.1.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.1.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 / Paths.php
matomo / classes / WpMatomo Last commit date
Admin 2 years ago Commands 2 years ago Db 2 years ago Ecommerce 2 years ago Report 2 years ago Site 2 years ago TrackingCode 2 years ago Updater 4 years ago User 2 years ago Workarounds 2 years ago WpStatistics 2 years ago views 4 years ago API.php 2 years ago Access.php 4 years ago AjaxTracker.php 2 years ago Annotations.php 4 years ago Bootstrap.php 2 years ago Capabilities.php 4 years ago Compatibility.php 2 years ago Email.php 2 years ago ErrorNotice.php 2 years ago Installer.php 2 years ago Logger.php 2 years ago OptOut.php 4 years ago Paths.php 2 years ago PluginAdminOverrides.php 2 years ago PrivacyBadge.php 4 years ago RedirectOnActivation.php 4 years ago Referral.php 4 years ago Roles.php 4 years ago ScheduledTasks.php 2 years ago Settings.php 2 years ago Site.php 3 years ago TrackingCode.php 4 years ago Uninstaller.php 2 years ago Updater.php 2 years ago User.php 4 years ago
Paths.php
237 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;
11
12 use stdClass;
13 use WP_Filesystem_Direct;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // if accessed directly
17 }
18
19 class Paths {
20
21 private static $host_init_filesystem = false;
22 private static $host_init_filesystem_succeeded = false;
23
24 public function get_file_system() {
25 if ( ! function_exists( 'WP_Filesystem' ) ) {
26 require_once ABSPATH . '/wp-admin/includes/file.php';
27 }
28
29 if ( ! self::$host_init_filesystem ) {
30 self::$host_init_filesystem = true;
31 self::$host_init_filesystem_succeeded = WP_Filesystem();
32 }
33
34 if ( ! class_exists( '\WP_Filesystem_Direct' ) ) {
35 require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
36 require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
37 }
38
39 return new WP_Filesystem_Direct( new stdClass() );
40 }
41
42 public function get_host_init_filesystem_succeeded() {
43 return self::$host_init_filesystem_succeeded;
44 }
45
46 public function get_upload_base_url() {
47 $upload_dir = wp_upload_dir();
48 $path_upload_url = $upload_dir['baseurl'];
49
50 return rtrim( $path_upload_url, '/' ) . '/' . MATOMO_UPLOAD_DIR;
51 }
52
53 public function get_upload_base_dir() {
54 $upload_dir = wp_upload_dir();
55 $path_upload_dir = $upload_dir['basedir'];
56 $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/' . MATOMO_UPLOAD_DIR;
57
58 return $path_upload_dir;
59 }
60
61 public function get_matomo_js_upload_path() {
62 return $this->get_upload_base_dir() . '/' . MATOMO_JS_NAME;
63 }
64
65 public function get_config_ini_path() {
66 return $this->get_upload_base_dir() . '/' . MATOMO_CONFIG_PATH;
67 }
68
69 public function get_tracker_api_rest_api_endpoint() {
70 return path_join( get_rest_url(), API::VERSION . '/' . API::ROUTE_HIT . '/' );
71 }
72
73 public function get_tracker_api_url_in_matomo_dir() {
74 return plugins_url( 'app/matomo.php', MATOMO_ANALYTICS_FILE );
75 }
76
77 public function get_js_tracker_rest_api_endpoint() {
78 return $this->get_tracker_api_rest_api_endpoint();
79 }
80
81 public function get_js_tracker_url_in_matomo_dir() {
82 $paths = new Paths();
83
84 if ( file_exists( $paths->get_matomo_js_upload_path() ) ) {
85 return $this->get_upload_base_url() . '/' . MATOMO_JS_NAME;
86 }
87
88 return plugins_url( 'app/matomo.js', MATOMO_ANALYTICS_FILE );
89 }
90
91 public function get_tmp_dir() {
92 $is_multi_site = function_exists( 'is_multisite' ) && is_multisite();
93
94 $cache_dir_alternative = $this->get_upload_base_dir() . '/tmp';
95 $base_cache_dir = WP_CONTENT_DIR . '/cache';
96 $default_cache_dir = $base_cache_dir . '/' . MATOMO_UPLOAD_DIR;
97
98 if ( ! $is_multi_site &&
99 ( ( is_writable( WP_CONTENT_DIR ) && ! is_dir( $base_cache_dir ) )
100 || is_writable( $base_cache_dir ) ) ) {
101 // we prefer wp-content/cache
102 $cache_dir = $default_cache_dir;
103
104 if ( ! is_dir( $cache_dir ) ) {
105 wp_mkdir_p( $cache_dir );
106 }
107
108 if ( ! is_writable( $cache_dir ) ) {
109 // wasn't made writable for some reason so we prefer to use the upload dir just to be safe
110 $cache_dir = $cache_dir_alternative;
111 }
112 } else {
113 // fallback wp-content/uploads/matomo/tmp if $defaultCacheDir is not writable or if multisite is used
114 // with multisite we need to make sure to cache files per site
115 $cache_dir = $cache_dir_alternative;
116
117 if ( ! is_dir( $cache_dir ) ) {
118 wp_mkdir_p( $cache_dir );
119 }
120 }
121
122 return $cache_dir;
123 }
124
125 /**
126 * parameter matomo_file is required for the unit test cases (when checking with a path including the string matomo)
127 *
128 * @param string $target_dir
129 * @param string $matomo_file
130 *
131 * @return string
132 */
133 public function get_relative_dir_to_matomo( $target_dir, $matomo_file = MATOMO_ANALYTICS_FILE ) {
134 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
135 return matomo_rel_path( $target_dir, dirname( $matomo_file ) . '/app' );
136 }
137
138 public function get_gloal_upload_dir_if_possible( $file_to_look_for = '' ) {
139 if ( defined( 'MATOMO_GLOBAL_UPLOAD_DIR' ) ) {
140 return MATOMO_GLOBAL_UPLOAD_DIR;
141 }
142
143 $path_upload_dir = $this->get_upload_base_dir();
144
145 if ( ! is_multisite() || is_network_admin() ) {
146 return $path_upload_dir;
147 }
148
149 if ( preg_match( '/sites\/(\d)+$/', $path_upload_dir ) ) {
150 $path_upload_dir = preg_replace( '/sites\/(\d)+$/', '', $path_upload_dir );
151 } else {
152 // re-implement _wp_upload_dir to find hopefully the upload_dir for the network site
153 $upload_path = trim( get_option( 'upload_path' ) );
154 if ( empty( $upload_path ) || 'wp-content/uploads' === $upload_path ) {
155 $path_upload_dir = WP_CONTENT_DIR . '/uploads';
156 } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
157 // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
158 $path_upload_dir = path_join( ABSPATH, $upload_path );
159 } else {
160 $path_upload_dir = $upload_path;
161 }
162 }
163
164 if ( ! empty( $file_to_look_for ) ) {
165 $file_to_look_for = MATOMO_UPLOAD_DIR . '/' . ltrim( $file_to_look_for, '/' );
166 }
167
168 $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/';
169 if ( ! empty( $file_to_look_for )
170 && ! file_exists( $path_upload_dir . $file_to_look_for ) ) {
171 // seems we haven't auto detected the right one yet... (or it is not yet installed)
172 // we go up the site upload dir step by step to try and find the network upload dir
173 $parent_dir = $path_upload_dir;
174 do {
175 $parent_dir = dirname( $parent_dir );
176 if ( file_exists( $parent_dir . $file_to_look_for ) ) {
177 return $parent_dir;
178 }
179 } while ( strpos( $parent_dir, ABSPATH ) === 0 ); // we don't go outside WP dir
180 }
181
182 $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/' . MATOMO_UPLOAD_DIR;
183
184 return $path_upload_dir;
185 }
186
187 public function clear_assets_dir() {
188 $tmp_dir = $this->get_tmp_dir() . '/assets';
189 if ( $tmp_dir && is_dir( $tmp_dir ) ) {
190 $file_system_direct = $this->get_file_system();
191 $file_system_direct->rmdir( $tmp_dir, true );
192 }
193 }
194
195 public function clear_cache_dir() {
196 $tmp_dir = $this->get_tmp_dir();
197 if ( $tmp_dir
198 && is_dir( $tmp_dir )
199 && is_dir( $tmp_dir . '/cache' ) ) {
200 // we make sure it's a matomo cache dir to not delete something falsely
201 $file_system_direct = $this->get_file_system();
202 $file_system_direct->rmdir( $tmp_dir, true );
203 }
204 }
205
206 public function uninstall() {
207 $this->clear_cache_dir();
208
209 $dir = $this->get_upload_base_dir();
210
211 $file_system_direct = $this->get_file_system();
212 $file_system_direct->rmdir( $dir, true );
213
214 $global_dir = $this->get_upload_base_dir();
215 if ( $global_dir && $global_dir !== $dir ) {
216 $file_system_direct->rmdir( $dir );
217 }
218 }
219
220 /**
221 * Must be called after Paths::get_file_system().
222 *
223 * @return bool
224 */
225 public function is_upload_dir_writable() {
226 $upload_dir = $this->get_upload_base_dir();
227
228 // the WP direct filesystem abstraction may not be available based on user
229 // configuration. in this case, we can't write to the upload dir using WP_Filesystem
230 $is_filesystem_direct_available = defined( 'FS_CHMOD_FILE' );
231 return is_dir( $upload_dir )
232 && is_writable( $upload_dir )
233 && $this->get_host_init_filesystem_succeeded()
234 && $is_filesystem_direct_available;
235 }
236 }
237