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