PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.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 / app / core / Site.php
matomo / app / core Last commit date
API 1 year ago Access 1 year ago Application 1 year ago Archive 1 year ago ArchiveProcessor 1 year ago Archiver 2 years ago AssetManager 1 year ago Auth 1 year ago Category 2 years ago Changes 1 year ago CliMulti 1 year ago Columns 1 year ago Concurrency 1 year ago Config 1 year ago Container 1 year ago CronArchive 1 year ago DataAccess 1 year ago DataFiles 2 years ago DataTable 1 year ago Db 1 year ago DeviceDetector 1 year ago Email 2 years ago Exception 1 year ago Http 1 year ago Intl 1 year ago Log 2 years ago Mail 1 year ago Measurable 1 year ago Menu 1 year ago Metrics 1 year ago Notification 1 year ago Period 1 year ago Plugin 1 year ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 1 year ago Scheduler 1 year ago Segment 1 year ago Session 1 year ago Settings 1 year ago Tracker 1 year ago Translation 1 year ago Twig 1 year ago UpdateCheck 1 year ago Updater 1 year ago Updates 1 year ago Validators 1 year ago View 1 year ago ViewDataTable 1 year ago Visualization 1 year ago Widget 1 year ago .htaccess 2 years ago Access.php 1 year ago Archive.php 1 year ago ArchiveProcessor.php 1 year ago AssetManager.php 1 year ago Auth.php 2 years ago AuthResult.php 2 years ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 1 year ago CliMulti.php 1 year ago Common.php 1 year ago Config.php 1 year ago Console.php 1 year ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 1 year ago DI.php 1 year ago DataArray.php 1 year ago DataTable.php 1 year ago Date.php 1 year ago Db.php 1 year ago DbHelper.php 1 year ago Development.php 1 year ago ErrorHandler.php 1 year ago EventDispatcher.php 1 year ago ExceptionHandler.php 1 year ago FileIntegrity.php 1 year ago Filechecks.php 1 year ago Filesystem.php 1 year ago FrontController.php 1 year ago Http.php 1 year ago IP.php 1 year ago Log.php 2 years ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 1 year ago NoAccessException.php 2 years ago Nonce.php 1 year ago Notification.php 1 year ago NumberFormatter.php 1 year ago Option.php 1 year ago Period.php 1 year ago Piwik.php 1 year ago Plugin.php 1 year ago Process.php 1 year ago Profiler.php 1 year ago ProxyHeaders.php 2 years ago ProxyHttp.php 1 year ago QuickForm2.php 1 year ago RankingQuery.php 1 year ago ReportRenderer.php 1 year ago Request.php 1 year ago Segment.php 1 year ago Sequence.php 2 years ago Session.php 1 year ago SettingsPiwik.php 1 year ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 1 year ago SiteContentDetector.php 1 year ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 2 years ago Tracker.php 1 year ago Twig.php 1 year ago Unzip.php 1 year ago UpdateCheck.php 1 year ago Updater.php 1 year ago UpdaterErrorException.php 2 years ago Updates.php 1 year ago Url.php 1 year ago UrlHelper.php 1 year ago Version.php 1 year ago View.php 1 year ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 2 years ago
Site.php
586 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik;
10
11 use Exception;
12 use Piwik\Exception\UnexpectedWebsiteFoundException;
13 use Piwik\Plugins\SitesManager\API;
14 /**
15 * Provides access to individual [site entity](/guides/persistence-and-the-mysql-backend#websites-aka-sites) data
16 * (including name, URL, etc.).
17 *
18 * **Data Cache**
19 *
20 * Site data can be cached in order to avoid performing too many queries.
21 * If a method needs many site entities, it is more efficient to query all of what
22 * you need beforehand via the **SitesManager** API, then cache it using {@link setSites()} or
23 * {@link setSitesFromArray()}.
24 *
25 * Subsequent calls to `new Site($id)` will use the data in the cache instead of querying the database.
26 *
27 * ### Examples
28 *
29 * **Basic usage**
30 *
31 * $site = new Site($idSite);
32 * $name = $site->getName();
33 *
34 * **Without allocation**
35 *
36 * $name = Site::getNameFor($idSite);
37 *
38 * @api
39 */
40 class Site
41 {
42 public const DEFAULT_SITE_TYPE = "website";
43 private static $intProperties = ['idsite', 'ecommerce', 'sitesearch', 'exclude_unknown_urls', 'keep_url_fragment'];
44 /**
45 * @var int|null
46 */
47 protected $id = null;
48 /**
49 * @var array
50 */
51 protected static $infoSites = array();
52 private $site = array();
53 /**
54 * Constructor.
55 *
56 * @param int $idsite The ID of the site we want data for.
57 * @throws UnexpectedWebsiteFoundException
58 */
59 public function __construct($idsite)
60 {
61 $this->id = (int) $idsite;
62 if (!empty(self::$infoSites[$this->id])) {
63 $site = self::$infoSites[$this->id];
64 } else {
65 $site = API::getInstance()->getSiteFromId($this->id);
66 if (empty($site)) {
67 throw new UnexpectedWebsiteFoundException('The requested website id = ' . (int) $this->id . ' couldn\'t be found');
68 }
69 }
70 $sites = array(&$site);
71 self::triggerSetSitesEvent($sites);
72 self::setSiteFromArray($this->id, $site);
73 $this->site = $site;
74 // for serialized format to be predictable across php/mysql/pdo/mysqli versions, make sure the int props stay ints
75 foreach (self::$intProperties as $propertyName) {
76 $this->site[$propertyName] = (int) $this->site[$propertyName];
77 }
78 }
79 /**
80 * Sets the cached site data with an array that associates site IDs with
81 * individual site data.
82 *
83 * @param array $sites The array of sites data. Indexed by site ID. eg,
84 *
85 * array('1' => array('name' => 'Site 1', ...),
86 * '2' => array('name' => 'Site 2', ...))`
87 */
88 public static function setSites($sites)
89 {
90 self::triggerSetSitesEvent($sites);
91 foreach ($sites as $idsite => $site) {
92 self::setSiteFromArray($idsite, $site);
93 }
94 }
95 private static function triggerSetSitesEvent(&$sites)
96 {
97 /**
98 * Triggered so plugins can modify website entities without modifying the database.
99 *
100 * This event should **not** be used to add data that is expensive to compute. If you
101 * need to make HTTP requests or query the database for more information, this is not
102 * the place to do it.
103 *
104 * **Example**
105 *
106 * Piwik::addAction('Site.setSites', function (&$sites) {
107 * foreach ($sites as &$site) {
108 * $site['name'] .= " (original)";
109 * }
110 * });
111 *
112 * @param array $sites An array of website entities. [Learn more.](/guides/persistence-and-the-mysql-backend#websites-aka-sites)
113 *
114 * This is not yet public as it doesn't work 100% accurately. Eg if `setSiteFromArray()` is called directly this event will not be triggered.
115 * @ignore
116 */
117 \Piwik\Piwik::postEvent('Site.setSites', array(&$sites));
118 }
119 /**
120 * Sets a site information in memory (statically cached).
121 *
122 * Plugins can filter the website attributes before it is cached, eg. to change the website name,
123 * creation date, etc.
124 *
125 * @param $idSite
126 * @param $infoSite
127 * @throws Exception if website or idsite is invalid
128 * @internal
129 */
130 public static function setSiteFromArray($idSite, $infoSite)
131 {
132 if (empty($idSite) || empty($infoSite)) {
133 throw new UnexpectedWebsiteFoundException("An unexpected website was found in the request: website id was set to '{$idSite}' .");
134 }
135 self::$infoSites[$idSite] = $infoSite;
136 }
137 /**
138 * Sets the cached Site data with a non-associated array of site data.
139 *
140 * This method will trigger the `Sites.setSites` event modifying `$sites` before setting cached
141 * site data. In other words, this method will change the site data before it is cached and then
142 * return the modified array.
143 *
144 * @param array $sites The array of sites data. eg,
145 *
146 * array(
147 * array('idsite' => '1', 'name' => 'Site 1', ...),
148 * array('idsite' => '2', 'name' => 'Site 2', ...),
149 * )
150 * @return array The modified array.
151 * @internal
152 */
153 public static function setSitesFromArray($sites)
154 {
155 self::triggerSetSitesEvent($sites);
156 foreach ($sites as $site) {
157 $idSite = null;
158 if (!empty($site['idsite'])) {
159 $idSite = $site['idsite'];
160 }
161 self::setSiteFromArray($idSite, $site);
162 }
163 return $sites;
164 }
165 /**
166 * The Multisites reports displays the first calendar date as the earliest day available for all websites.
167 * Also, today is the later "today" available across all timezones.
168 * @param array $siteIds Array of IDs for each site being displayed.
169 * @return Date[] of two Date instances. First is the min-date & the second
170 * is the max date.
171 * @ignore
172 */
173 public static function getMinMaxDateAcrossWebsites($siteIds)
174 {
175 $siteIds = self::getIdSitesFromIdSitesString($siteIds);
176 $now = \Piwik\Date::now();
177 $minDate = null;
178 $maxDate = $now->subDay(1)->getTimestamp();
179 foreach ($siteIds as $idsite) {
180 // look for 'now' in the website's timezone
181 $timezone = \Piwik\Site::getTimezoneFor($idsite);
182 $date = \Piwik\Date::adjustForTimezone($now->getTimestamp(), $timezone);
183 if ($date > $maxDate) {
184 $maxDate = $date;
185 }
186 // look for the absolute minimum date
187 $creationDate = \Piwik\Site::getCreationDateFor($idsite);
188 $date = \Piwik\Date::adjustForTimezone(strtotime($creationDate), $timezone);
189 if (is_null($minDate) || $date < $minDate) {
190 $minDate = $date;
191 }
192 }
193 return array(\Piwik\Date::factory($minDate), \Piwik\Date::factory($maxDate));
194 }
195 /**
196 * Returns a string representation of the site this instance references.
197 *
198 * Useful for debugging.
199 *
200 * @return string
201 */
202 public function __toString()
203 {
204 return "site id=" . $this->getId() . ",\n\t\t\t\t name=" . $this->getName() . ",\n\t\t\t\t url = " . $this->getMainUrl() . ",\n\t\t\t\t IPs excluded = " . $this->getExcludedIps() . ",\n\t\t\t\t timezone = " . $this->getTimezone() . ",\n\t\t\t\t currency = " . $this->getCurrency() . ",\n\t\t\t\t creation date = " . $this->getCreationDate();
205 }
206 /**
207 * Returns the name of the site.
208 *
209 * @return string
210 * @throws Exception if data for the site cannot be found.
211 */
212 public function getName()
213 {
214 return $this->get('name');
215 }
216 /**
217 * Returns the main url of the site.
218 *
219 * @return string
220 * @throws Exception if data for the site cannot be found.
221 */
222 public function getMainUrl()
223 {
224 return $this->get('main_url');
225 }
226 /**
227 * Returns the id of the site.
228 *
229 * @return int
230 * @throws Exception if data for the site cannot be found.
231 */
232 public function getId()
233 {
234 return $this->id;
235 }
236 /**
237 * Returns a site property by name.
238 *
239 * @param string $name Name of the property to return (eg, `'main_url'` or `'name'`).
240 * @return mixed
241 * @throws Exception
242 */
243 protected function get($name)
244 {
245 if (isset($this->site[$name])) {
246 return $this->site[$name];
247 }
248 throw new Exception("The property {$name} could not be found on the website ID " . (int) $this->id);
249 }
250 /**
251 * Returns the website type (by default `"website"`, which means it is a single website).
252 *
253 * @return string
254 */
255 public function getType()
256 {
257 $type = $this->get('type');
258 return $type;
259 }
260 /**
261 * Returns the creation date of the site.
262 *
263 * @return Date
264 * @throws Exception if data for the site cannot be found.
265 */
266 public function getCreationDate()
267 {
268 $date = $this->get('ts_created');
269 return \Piwik\Date::factory($date);
270 }
271 /**
272 * Returns the timezone of the size.
273 *
274 * @return string
275 * @throws Exception if data for the site cannot be found.
276 */
277 public function getTimezone()
278 {
279 return $this->get('timezone');
280 }
281 /**
282 * Returns the currency of the site.
283 *
284 * @return string
285 * @throws Exception if data for the site cannot be found.
286 */
287 public function getCurrency()
288 {
289 return $this->get('currency');
290 }
291 /**
292 * Returns the excluded ips of the site.
293 *
294 * @return string
295 * @throws Exception if data for the site cannot be found.
296 */
297 public function getExcludedIps()
298 {
299 return $this->get('excluded_ips');
300 }
301 /**
302 * Returns the excluded query parameters of the site.
303 *
304 * @return string
305 * @throws Exception if data for the site cannot be found.
306 */
307 public function getExcludedQueryParameters()
308 {
309 return $this->get('excluded_parameters');
310 }
311 /**
312 * Returns whether ecommerce is enabled for the site.
313 *
314 * @return bool
315 * @throws Exception if data for the site cannot be found.
316 */
317 public function isEcommerceEnabled()
318 {
319 return $this->get('ecommerce') == 1;
320 }
321 /**
322 * Returns the site search keyword query parameters for the site.
323 *
324 * @return string
325 * @throws Exception if data for the site cannot be found.
326 */
327 public function getSearchKeywordParameters()
328 {
329 return $this->get('sitesearch_keyword_parameters');
330 }
331 /**
332 * Returns the site search category query parameters for the site.
333 *
334 * @return string
335 * @throws Exception if data for the site cannot be found.
336 */
337 public function getSearchCategoryParameters()
338 {
339 return $this->get('sitesearch_category_parameters');
340 }
341 /**
342 * Returns whether Site Search Tracking is enabled for the site.
343 *
344 * @return bool
345 * @throws Exception if data for the site cannot be found.
346 */
347 public function isSiteSearchEnabled()
348 {
349 return $this->get('sitesearch') == 1;
350 }
351 /**
352 * Returns the user that created this site.
353 *
354 * @return string|null If null, the site was created before the creation user was tracked.
355 */
356 public function getCreatorLogin()
357 {
358 return $this->get('creator_login');
359 }
360 /**
361 * Checks the given string for valid site IDs and returns them as an array.
362 *
363 * @param string|array $ids Comma separated idSite list, eg, `'1,2,3,4'` or an array of IDs, eg,
364 * `array(1, 2, 3, 4)`.
365 * @param bool|string $_restrictSitesToLogin Implementation detail. Used only when running as a scheduled task.
366 * @return array An array of valid, unique integers.
367 */
368 public static function getIdSitesFromIdSitesString($ids, $_restrictSitesToLogin = \false)
369 {
370 if (empty($ids)) {
371 return [];
372 }
373 if ($ids === 'all') {
374 return API::getInstance()->getSitesIdWithAtLeastViewAccess($_restrictSitesToLogin);
375 }
376 if (is_bool($ids)) {
377 return array();
378 }
379 if (!is_array($ids)) {
380 $ids = explode(',', $ids);
381 }
382 $validIds = array();
383 foreach ($ids as $id) {
384 $id = is_string($id) ? trim($id) : $id;
385 if (!empty($id) && is_numeric($id) && $id > 0) {
386 $validIds[] = $id;
387 }
388 }
389 $validIds = array_filter($validIds);
390 $validIds = array_unique($validIds);
391 return $validIds;
392 }
393 /**
394 * Clears the site data cache.
395 *
396 * See also {@link setSites()} and {@link setSitesFromArray()}.
397 */
398 public static function clearCache()
399 {
400 self::$infoSites = array();
401 }
402 /**
403 * Clears the site data cache.
404 *
405 * See also {@link setSites()} and {@link setSitesFromArray()}.
406 */
407 public static function clearCacheForSite($idSite)
408 {
409 $idSite = (int) $idSite;
410 unset(self::$infoSites[$idSite]);
411 }
412 /**
413 * Utility function. Returns the value of the specified field for the
414 * site with the specified ID.
415 *
416 * @param int $idsite The ID of the site whose data is being accessed.
417 * @param string $field The name of the field to get.
418 * @return string
419 */
420 protected static function getFor($idsite, $field)
421 {
422 if (!isset(self::$infoSites[$idsite])) {
423 $site = API::getInstance()->getSiteFromId($idsite);
424 self::setSiteFromArray($idsite, $site);
425 }
426 return self::$infoSites[$idsite][$field];
427 }
428 /**
429 * Returns all websites pre-cached
430 *
431 * @ignore
432 */
433 public static function getSites()
434 {
435 return self::$infoSites;
436 }
437 /**
438 * @ignore
439 */
440 public static function getSite($idsite)
441 {
442 $idsite = (int) $idsite;
443 if (!isset(self::$infoSites[$idsite])) {
444 $site = API::getInstance()->getSiteFromId($idsite);
445 self::setSiteFromArray($idsite, $site);
446 }
447 return self::$infoSites[$idsite];
448 }
449 /**
450 * Returns the name of the site with the specified ID.
451 *
452 * @param int $idsite The site ID.
453 * @return string
454 */
455 public static function getNameFor($idsite)
456 {
457 return self::getFor($idsite, 'name');
458 }
459 /**
460 * Returns the group of the site with the specified ID.
461 *
462 * @param int $idsite The site ID.
463 * @return string
464 */
465 public static function getGroupFor($idsite)
466 {
467 return self::getFor($idsite, 'group');
468 }
469 /**
470 * Returns the timezone of the site with the specified ID.
471 *
472 * @param int $idsite The site ID.
473 * @return string
474 */
475 public static function getTimezoneFor($idsite)
476 {
477 return self::getFor($idsite, 'timezone');
478 }
479 /**
480 * Returns the type of the site with the specified ID.
481 *
482 * @param $idsite
483 * @return string
484 */
485 public static function getTypeFor($idsite)
486 {
487 return self::getFor($idsite, 'type');
488 }
489 /**
490 * Returns the creation date of the site with the specified ID.
491 *
492 * @param int $idsite The site ID.
493 * @return string
494 */
495 public static function getCreationDateFor($idsite)
496 {
497 return self::getFor($idsite, 'ts_created');
498 }
499 /**
500 * Returns the url for the site with the specified ID.
501 *
502 * @param int $idsite The site ID.
503 * @return string
504 */
505 public static function getMainUrlFor($idsite)
506 {
507 return self::getFor($idsite, 'main_url');
508 }
509 /**
510 * Returns whether the site with the specified ID is ecommerce enabled or not.
511 *
512 * @param int $idsite The site ID.
513 * @return string
514 */
515 public static function isEcommerceEnabledFor($idsite)
516 {
517 return self::getFor($idsite, 'ecommerce') == 1;
518 }
519 /**
520 * Returns whether the site with the specified ID is Site Search enabled.
521 *
522 * @param int $idsite The site ID.
523 * @return string
524 */
525 public static function isSiteSearchEnabledFor($idsite)
526 {
527 return self::getFor($idsite, 'sitesearch') == 1;
528 }
529 /**
530 * Returns the currency of the site with the specified ID.
531 *
532 * @param int $idsite The site ID.
533 * @return string
534 */
535 public static function getCurrencyFor($idsite)
536 {
537 return self::getFor($idsite, 'currency');
538 }
539 /**
540 * Returns the currency of the site with the specified ID.
541 *
542 * @param int $idsite The site ID.
543 * @return string
544 */
545 public static function getCurrencySymbolFor($idsite)
546 {
547 $currencyCode = self::getCurrencyFor($idsite);
548 $key = 'Intl_CurrencySymbol_' . $currencyCode;
549 $symbol = \Piwik\Piwik::translate($key);
550 if ($key === $symbol) {
551 return $currencyCode;
552 }
553 return $symbol;
554 }
555 /**
556 * Returns the excluded IP addresses of the site with the specified ID.
557 *
558 * @param int $idsite The site ID.
559 * @return string
560 */
561 public static function getExcludedIpsFor($idsite)
562 {
563 return self::getFor($idsite, 'excluded_ips');
564 }
565 /**
566 * Returns the excluded query parameters for the site with the specified ID.
567 *
568 * @param int $idsite The site ID.
569 * @return string
570 */
571 public static function getExcludedQueryParametersFor($idsite)
572 {
573 return self::getFor($idsite, 'excluded_parameters');
574 }
575 /**
576 * Returns the user that created this site.
577 *
578 * @param int $idsite The site ID.
579 * @return string|null If null, the site was created before the creation user was tracked.
580 */
581 public static function getCreatorLoginFor($idsite)
582 {
583 return self::getFor($idsite, 'creator_login');
584 }
585 }
586