PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.0.5
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.0.5
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 / API.php
matomo / classes / WpMatomo Last commit date
Admin 6 years ago Commands 6 years ago Db 6 years ago Ecommerce 6 years ago Report 6 years ago Site 6 years ago TrackingCode 6 years ago User 6 years ago views 6 years ago API.php 6 years ago Access.php 6 years ago AjaxTracker.php 6 years ago Annotations.php 6 years ago Bootstrap.php 6 years ago Capabilities.php 6 years ago Compatibility.php 6 years ago Email.php 6 years ago Installer.php 6 years ago Logger.php 6 years ago OptOut.php 6 years ago Paths.php 6 years ago PrivacyBadge.php 6 years ago Referral.php 6 years ago Roles.php 6 years ago ScheduledTasks.php 6 years ago Settings.php 6 years ago Site.php 6 years ago TrackingCode.php 6 years ago Uninstaller.php 6 years ago Updater.php 6 years ago User.php 6 years ago
API.php
216 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 Piwik\API\Request;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // if accessed directly
16 }
17
18 class API {
19 const VERSION = 'matomo/v1';
20
21 const ROUTE_HIT = 'hit';
22
23 public function register_hooks() {
24 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
25 }
26
27 public function register_routes() {
28 register_rest_route(
29 self::VERSION,
30 '/' . self::ROUTE_HIT . '/',
31 array(
32 'methods' => array( 'GET', 'POST' ),
33 'callback' => array( $this, 'hit' ),
34 )
35 );
36 $this->register_route( 'API', 'getProcessedReport' );
37 $this->register_route( 'API', 'getReportMetadata' );
38 $this->register_route( 'API', 'getMatomoVersion' );
39 $this->register_route( 'API', 'getMetadata' );
40 $this->register_route( 'API', 'getSegmentsMetadata' );
41 $this->register_route( 'API', 'getWidgetMetadata' );
42 $this->register_route( 'API', 'getRowEvolution' );
43 $this->register_route( 'API', 'getSuggestedValuesForSegment' );
44 $this->register_route( 'API', 'getSettings' );
45 $this->register_route( 'Annotations', 'add' );
46 $this->register_route( 'Annotations', 'getAll' );
47 $this->register_route( 'CoreAdminHome', 'invalidateArchivedReports' );
48 $this->register_route( 'CoreAdminHome', 'runScheduledTasks' );
49 $this->register_route( 'Dashboard', 'getDashboards' );
50 $this->register_route( 'ImageGraph', 'get' );
51 $this->register_route( 'LanguagesManager', 'getAvailableLanguages' );
52 $this->register_route( 'LanguagesManager', 'getAvailableLanguagesInfo' );
53 $this->register_route( 'LanguagesManager', 'getAvailableLanguageNames' );
54 $this->register_route( 'LanguagesManager', 'getLanguageForUser' );
55 $this->register_route( 'Live', 'getCounters' );
56 $this->register_route( 'Live', 'getLastVisitsDetails' );
57 $this->register_route( 'Live', 'getVisitorProfile' );
58 $this->register_route( 'Live', 'getMostRecentVisitorId' );
59 $this->register_route( 'PrivacyManager', 'deleteDataSubjects' );
60 $this->register_route( 'PrivacyManager', 'exportDataSubjects' );
61 $this->register_route( 'PrivacyManager', 'anonymizeSomeRawData' );
62 $this->register_route( 'ScheduledReports', 'getReports' );
63 $this->register_route( 'ScheduledReports', 'sendReport' );
64 $this->register_route( 'SegmentEditor', 'add' );
65 $this->register_route( 'SegmentEditor', 'update' );
66 $this->register_route( 'SegmentEditor', 'delete' );
67 $this->register_route( 'SegmentEditor', 'get' );
68 $this->register_route( 'SegmentEditor', 'getAll' );
69 $this->register_route( 'SitesManager', 'getAllSites' );
70 $this->register_route( 'SitesManager', 'getAllSitesId' );
71 $this->register_route( 'UsersManager', 'getUsers' );
72 $this->register_route( 'UsersManager', 'getUsersLogin' );
73 $this->register_route( 'UsersManager', 'getUser' );
74 $this->register_route( 'Goals', 'getGoals' );
75
76 // todo ideally we would make here work /goal/12345 to get goalId 12345
77 $this->register_route( 'Goals', 'getGoal' );
78 $this->register_route( 'Goals', 'addGoal' );
79 $this->register_route( 'Goals', 'updateGoal' );
80 $this->register_route( 'Goals', 'deleteGoal' );
81 }
82
83 public function hit() {
84 if ( empty( $_GET ) && empty( $_POST ) && empty( $_POST['idsite'] ) && empty( $_GET['idsite'] ) ) {
85 // todo if uploads dir is not writable, we may want to generate the matomo.js here and save it as an
86 // option... then we could also save it compressed
87 $paths = new Paths();
88 $path = $paths->get_matomo_js_upload_path();
89 header( 'Content-Type: application/javascript' );
90 header( 'Content-Length: ' . ( filesize( $path ) ) );
91 readfile( $paths->get_upload_base_dir() . '/matomo.js' ); // Reading the file into the output buffer
92 exit;
93 }
94 include_once plugin_dir_path( MATOMO_ANALYTICS_FILE ) . 'app/piwik.php';
95 exit;
96 }
97
98 public function execute_api_method( \WP_REST_Request $request ) {
99 $attributes = $request->get_attributes();
100 $method = $attributes['matomoModule'] . '.' . $attributes['matomoMethod'];
101
102 $with_idsite = true;
103
104 return $this->execute_request( $method, $with_idsite, $request->get_params() );
105 }
106
107 /**
108 * @param string $method
109 *
110 * @return string
111 * @internal
112 * for tests only
113 */
114 public function to_snake_case( $method ) {
115 preg_match_all( '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $method, $matches );
116
117 $snake_case = $matches[0];
118
119 foreach ( $snake_case as &$match ) {
120 if ( strtoupper( $match ) === $match ) {
121 $match = strtolower( $match );
122 } else {
123 $match = lcfirst( $match );
124 }
125 }
126
127 return implode( '_', $snake_case );
128 }
129
130 /**
131 * @api
132 */
133 public function register_route( $api_module, $api_method ) {
134 $methods = array(
135 'get' => 'GET',
136 'edit' => 'PUT',
137 'update' => 'PUT',
138 'create' => 'POST',
139 'add' => 'POST',
140 'anonymize' => 'POST',
141 'invalidate' => 'POST',
142 'run' => 'POST',
143 'send' => 'POST',
144 'delete' => 'DELETE',
145 'remove' => 'DELETE',
146 );
147 $starts_with_keep_prefix = array( 'anonymize', 'invalidate', 'run', 'send' );
148
149 $method = 'GET';
150 $wp_api_module = $this->to_snake_case( $api_module );
151 $wp_api_action = $this->to_snake_case( $api_method );
152
153 foreach ( $methods as $method_starts_with => $method_to_use ) {
154 if ( strpos( $api_method, $method_starts_with ) === 0 ) {
155 $method = $method_to_use;
156 if ( ! in_array( $method_starts_with, $starts_with_keep_prefix, true ) ) {
157 $new_action = trim( ltrim( substr( $wp_api_action, strlen( $method_starts_with ) ), '_' ) );
158 if ( ! empty( $new_action ) ) {
159 $wp_api_action = $new_action;
160 }
161 }
162 break;
163 }
164 }
165
166 register_rest_route(
167 self::VERSION,
168 '/' . $wp_api_module . '/' . $wp_api_action . '/',
169 array(
170 'methods' => $method,
171 'callback' => array( $this, 'execute_api_method' ),
172 'matomoModule' => $api_module,
173 'matomoMethod' => $api_method,
174 )
175 );
176 }
177
178 private function execute_request( $api_method, $with_idsite, $params ) {
179 if ( $with_idsite ) {
180 $site = new Site();
181 $idsite = $site->get_current_matomo_site_id();
182
183 if ( ! $idsite ) {
184 return new \WP_Error( 'Site not found. Make sure it is synced' );
185 }
186
187 $params['idSite'] = $idsite;
188 $params['idsite'] = $idsite;
189 $params['idsites'] = $idsite;
190 $params['idSites'] = $idsite;
191 }
192
193 // ensure user is authenticated through WordPress!
194 unset( $_GET['token_auth'] );
195 unset( $_POST['token_auth'] );
196
197 Bootstrap::do_bootstrap();
198
199 try {
200 $result = Request::processRequest( $api_method, $params );
201 } catch ( \Exception $e ) {
202 $code = 'matomo_error';
203 if ( $e->getCode() ) {
204 $code .= '_' . $code;
205 }
206 if ( get_class( $e ) !== 'Exception' ) {
207 $code = str_replace( 'piwik', 'matomo', $this->to_snake_case( get_class( $e ) ) );
208 }
209
210 return new \WP_Error( $code, $e->getMessage() );
211 }
212
213 return $result;
214 }
215 }
216