PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 / Period / Factory.php
matomo / app / core / Period Last commit date
Day.php 1 month ago Factory.php 3 months ago Month.php 1 month ago PeriodValidator.php 2 years ago Range.php 1 month ago Week.php 1 month ago Year.php 1 month ago
Factory.php
198 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\Period;
10
11 use Exception;
12 use Piwik\Container\StaticContainer;
13 use Piwik\Date;
14 use Piwik\Period;
15 use Piwik\Piwik;
16 use Piwik\Plugin;
17 /**
18 * Creates Period instances using the values used for the 'period' and 'date'
19 * query parameters.
20 *
21 * ## Custom Periods
22 *
23 * Plugins can define their own period factories all plugins to define new period types, in addition
24 * to "day", "week", "month", "year" and "range".
25 *
26 * To define a new period type:
27 *
28 * 1. create a new period class that derives from {@see \Piwik\Period}.
29 * 2. extend this class in a new PeriodFactory class and put it in /path/to/piwik/plugins/MyPlugin/PeriodFactory.php
30 *
31 * Period name collisions:
32 *
33 * If two plugins try to handle the same period label, the first one encountered will
34 * be used. In other words, avoid using another plugin's period label.
35 */
36 abstract class Factory
37 {
38 public function __construct()
39 {
40 // empty
41 }
42 /**
43 * Returns true if this factory should handle the period/date string combination.
44 *
45 * @return bool
46 */
47 public abstract function shouldHandle($strPeriod, $strDate);
48 /**
49 * Creates a period using the value of the 'date' query parameter.
50 *
51 * @param string $strPeriod
52 * @param string|Date $date
53 * @param string $timezone
54 * @return Period
55 */
56 public abstract function make($strPeriod, $date, $timezone);
57 /**
58 * Creates a new Period instance with a period ID and {@link Date} instance.
59 *
60 * _Note: This method cannot create {@link Period\Range} periods._
61 *
62 * @param string $period `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
63 * @param Date|string $date A date within the period or the range of dates.
64 * @param Date|string $timezone Optional timezone that will be used only when $period is 'range' or $date is 'last|previous'
65 * @throws Exception If `$strPeriod` is invalid or $date is invalid.
66 * @return \Piwik\Period
67 */
68 public static function build($period, $date, $timezone = 'UTC')
69 {
70 self::checkPeriodIsEnabled($period);
71 if (is_string($date)) {
72 [$period, $date] = self::convertRangeToDateIfNeeded($period, $date);
73 if (Period::isMultiplePeriod($date, $period) || $period == 'range') {
74 return new \Piwik\Period\Range($period, $date, $timezone);
75 }
76 $dateObject = Date::factory($date);
77 } elseif ($date instanceof Date) {
78 $dateObject = $date;
79 } else {
80 throw new \Exception("Invalid date supplied to Period\\Factory::build(): " . gettype($date));
81 }
82 switch ($period) {
83 case 'day':
84 return new \Piwik\Period\Day($dateObject);
85 case 'week':
86 return new \Piwik\Period\Week($dateObject);
87 case 'month':
88 return new \Piwik\Period\Month($dateObject);
89 case 'year':
90 return new \Piwik\Period\Year($dateObject);
91 }
92 $customPeriodFactories = Plugin\Manager::getInstance()->findComponents('PeriodFactory', self::class);
93 foreach ($customPeriodFactories as $customPeriodFactoryClass) {
94 $customPeriodFactory = StaticContainer::get($customPeriodFactoryClass);
95 if ($customPeriodFactory->shouldHandle($period, $date)) {
96 return $customPeriodFactory->make($period, $date, $timezone);
97 }
98 }
99 throw new \Exception("Don't know how to create a '{$period}' period! (date = {$date})");
100 }
101 public static function checkPeriodIsEnabled($period)
102 {
103 if (!self::isPeriodEnabledForAPI($period)) {
104 self::throwExceptionInvalidPeriod($period);
105 }
106 }
107 /**
108 * @param $strPeriod
109 * @throws \Exception
110 */
111 private static function throwExceptionInvalidPeriod($strPeriod)
112 {
113 $periods = self::getPeriodsEnabledForAPI();
114 $periods = implode(", ", $periods);
115 $message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, $periods));
116 throw new Exception($message);
117 }
118 private static function convertRangeToDateIfNeeded($period, $date)
119 {
120 if (is_string($period) && is_string($date) && $period === 'range') {
121 $dates = explode(',', $date);
122 if (count($dates) === 2 && $dates[0] === $dates[1]) {
123 $period = 'day';
124 $date = $dates[0];
125 }
126 }
127 return array($period, $date);
128 }
129 /**
130 * Creates a Period instance using a period, date and timezone.
131 *
132 * @param string $timezone The timezone of the date. Only used if `$date` is `'now'`, `'today'`,
133 * `'yesterday'` or `'yesterdaySameTime'`.
134 * @param string $period The period string: `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
135 * @param string $date The date or date range string. Can be a special value including
136 * `'now'`, `'today'`, `'yesterday'`, `'yesterdaySameTime'`.
137 * @return \Piwik\Period
138 */
139 public static function makePeriodFromQueryParams($timezone, $period, $date)
140 {
141 if (empty($timezone)) {
142 $timezone = 'UTC';
143 }
144 [$period, $date] = self::convertRangeToDateIfNeeded($period, $date);
145 if ($period == 'range') {
146 self::checkPeriodIsEnabled('range');
147 $oPeriod = new \Piwik\Period\Range('range', $date, $timezone, Date::factory('today', $timezone));
148 } else {
149 if (!$date instanceof Date) {
150 if (preg_match('/^(now|today|yesterday|yesterdaySameTime|last[ -]?(?:week|month|year))$/i', $date)) {
151 $date = Date::factoryInTimezone($date, $timezone);
152 }
153 $date = Date::factory($date);
154 }
155 $oPeriod = \Piwik\Period\Factory::build($period, $date);
156 }
157 return $oPeriod;
158 }
159 /**
160 * @param $period
161 * @return bool
162 */
163 public static function isPeriodEnabledForAPI($period)
164 {
165 $periodValidator = new \Piwik\Period\PeriodValidator();
166 return $periodValidator->isPeriodAllowedForAPI($period);
167 }
168 /**
169 * @return array
170 */
171 public static function getPeriodsEnabledForAPI()
172 {
173 $periodValidator = new \Piwik\Period\PeriodValidator();
174 return $periodValidator->getPeriodsAllowedForAPI();
175 }
176 public static function isAnyLowerPeriodDisabledForAPI($periodLabel)
177 {
178 $parentPeriod = null;
179 switch ($periodLabel) {
180 case 'week':
181 $parentPeriod = 'day';
182 break;
183 case 'month':
184 $parentPeriod = 'week';
185 break;
186 case 'year':
187 $parentPeriod = 'month';
188 break;
189 default:
190 break;
191 }
192 if ($parentPeriod === null) {
193 return \false;
194 }
195 return !self::isPeriodEnabledForAPI($parentPeriod) || self::isAnyLowerPeriodDisabledForAPI($parentPeriod);
196 }
197 }
198