views
3 months ago
Data.php
2 months ago
Dates.php
1 year ago
Metadata.php
1 year ago
Renderer.php
2 months ago
Dates.php
136 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\Report; |
| 11 | |
| 12 | use Piwik\Plugins\UsersManager\UserPreferences; |
| 13 | use WpMatomo\Bootstrap; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // if accessed directly |
| 17 | } |
| 18 | |
| 19 | class Dates { |
| 20 | const TODAY = 'today'; |
| 21 | const YESTERDAY = 'yesterday'; |
| 22 | const THIS_WEEK = 'thisweek'; |
| 23 | const LAST_WEEK = 'lastweek'; |
| 24 | const THIS_MONTH = 'thismonth'; |
| 25 | const LAST_MONTH = 'lastmonth'; |
| 26 | const THIS_YEAR = 'thisyear'; |
| 27 | |
| 28 | public function get_supported_dates() { |
| 29 | return [ |
| 30 | self::YESTERDAY => 'Yesterday', |
| 31 | self::TODAY => 'Today', |
| 32 | self::THIS_WEEK => 'This week', |
| 33 | self::LAST_WEEK => 'Last week', |
| 34 | self::THIS_MONTH => 'This month', |
| 35 | self::LAST_MONTH => 'Last month', |
| 36 | self::THIS_YEAR => 'This year', |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | public function detect_period_and_date( $report_date ) { |
| 41 | $period = 'day'; |
| 42 | $date = 'yesterday'; |
| 43 | |
| 44 | switch ( $report_date ) { |
| 45 | case self::TODAY: |
| 46 | $period = 'day'; |
| 47 | $date = 'today'; |
| 48 | break; |
| 49 | case self::YESTERDAY: |
| 50 | $period = 'day'; |
| 51 | $date = 'yesterday'; |
| 52 | break; |
| 53 | case self::THIS_MONTH: |
| 54 | $period = 'month'; |
| 55 | $date = 'today'; |
| 56 | break; |
| 57 | case self::LAST_MONTH: |
| 58 | $period = 'month'; |
| 59 | $date = gmdate( 'Y-m-d', strtotime( '1 month ago' ) ); |
| 60 | break; |
| 61 | case self::THIS_WEEK: |
| 62 | $period = 'week'; |
| 63 | $date = 'today'; |
| 64 | break; |
| 65 | case self::LAST_WEEK: |
| 66 | $period = 'week'; |
| 67 | $date = gmdate( 'Y-m-d', strtotime( '1 week ago' ) ); |
| 68 | break; |
| 69 | case self::THIS_YEAR: |
| 70 | $period = 'year'; |
| 71 | $date = 'today'; |
| 72 | break; |
| 73 | default: |
| 74 | if ( preg_match( '/\d{4}-\d{2}-\d{2}/', $report_date ) ) { |
| 75 | $period = 'day'; |
| 76 | $date = $report_date; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return [ $period, $date ]; |
| 81 | } |
| 82 | |
| 83 | public function get_date_from_query() { |
| 84 | Bootstrap::do_bootstrap(); |
| 85 | |
| 86 | $report_dates = $this->get_supported_dates(); |
| 87 | |
| 88 | $report_date = 'yesterday'; |
| 89 | |
| 90 | $user_preference = new UserPreferences(); |
| 91 | $default_date = $user_preference->getDefaultDate(); |
| 92 | $report_period = $user_preference->getDefaultPeriod(); |
| 93 | switch ( $report_period ) { |
| 94 | case 'day': |
| 95 | $report_date = $default_date; |
| 96 | break; |
| 97 | case 'year': |
| 98 | case 'month': |
| 99 | case 'week': |
| 100 | switch ( $default_date ) { |
| 101 | case 'yesterday': |
| 102 | $report_date = 'last' . $report_period; |
| 103 | break; |
| 104 | case 'today': |
| 105 | $report_date = 'this' . $report_period; |
| 106 | break; |
| 107 | } |
| 108 | break; |
| 109 | case 'range': |
| 110 | switch ( $default_date ) { |
| 111 | case 'previous30': |
| 112 | $report_date = 'lastmonth'; |
| 113 | break; |
| 114 | case 'previous7': |
| 115 | $report_date = 'lastweek'; |
| 116 | break; |
| 117 | case 'last30': |
| 118 | $report_date = 'thismonth'; |
| 119 | break; |
| 120 | case 'last7': |
| 121 | $report_date = 'thisweek'; |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | if ( isset( $_REQUEST['report_date'] ) && isset( $report_dates[ $_REQUEST['report_date'] ] ) ) { |
| 131 | $report_date = sanitize_text_field( wp_unslash( $_REQUEST['report_date'] ) ); |
| 132 | } |
| 133 | return $report_date; |
| 134 | } |
| 135 | } |
| 136 |