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