AJAX
2 years ago
Date_Range
2 years ago
Interval
3 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Queries
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
2 years ago
Chart_SVG.php
3 years ago
City_To_Country_Converter.php
3 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
3 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Downloader.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
3 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
3 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Reset_Database.php
3 years ago
Resource_Identifier.php
3 years ago
Settings.php
2 years ago
Sort_Configuration.php
3 years ago
Track_Resource_Changes.php
3 years ago
View.php
2 years ago
View_Counter.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
Dashboard_Options.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWP_SCOPED\IAWP\Date_Range\Date_Range; |
| 7 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range; |
| 8 | use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range; |
| 9 | use IAWP_SCOPED\IAWP\Statistics\Intervals\Interval; |
| 10 | use IAWP_SCOPED\IAWP\Statistics\Intervals\Intervals; |
| 11 | use IAWP_SCOPED\Proper\Timezone; |
| 12 | use Throwable; |
| 13 | /** |
| 14 | * Dashboards support various options via the search query string portion of the URL. |
| 15 | * |
| 16 | * The Dashboard_Options class give you an interface for fetching any set values or falling back |
| 17 | * to a default value as needed. |
| 18 | */ |
| 19 | class Dashboard_Options |
| 20 | { |
| 21 | public function __construct() |
| 22 | { |
| 23 | } |
| 24 | /** |
| 25 | * @return Date_Range |
| 26 | */ |
| 27 | public function get_date_range() : Date_Range |
| 28 | { |
| 29 | if ($this->has_exact_range()) { |
| 30 | try { |
| 31 | $start = new DateTime($this->start(), Timezone::site_timezone()); |
| 32 | $end = new DateTime($this->end(), Timezone::site_timezone()); |
| 33 | return new Exact_Date_Range($start, $end); |
| 34 | } catch (Throwable $e) { |
| 35 | // Do nothing and fall back to default relative date range |
| 36 | } |
| 37 | } |
| 38 | return new Relative_Date_Range($this->relative_range_id()); |
| 39 | } |
| 40 | public function start() |
| 41 | { |
| 42 | $start = $this->get_query_value('start'); |
| 43 | if (!$this->has_exact_range()) { |
| 44 | return null; |
| 45 | } |
| 46 | return $start; |
| 47 | } |
| 48 | public function end() |
| 49 | { |
| 50 | $end = $this->get_query_value('end'); |
| 51 | if (!$this->has_exact_range()) { |
| 52 | return null; |
| 53 | } |
| 54 | return $end; |
| 55 | } |
| 56 | /** |
| 57 | * Prefer exact range to relative range if both are provided |
| 58 | */ |
| 59 | public function relative_range_id() |
| 60 | { |
| 61 | $relative_range_id = $this->get_query_value('relative_range'); |
| 62 | if (!$this->has_exact_range() && $relative_range_id === null) { |
| 63 | return 'LAST_THIRTY'; |
| 64 | } elseif ($this->has_exact_range()) { |
| 65 | return null; |
| 66 | } elseif (Relative_Date_Range::is_valid_range($relative_range_id) === \false) { |
| 67 | return 'LAST_THIRTY'; |
| 68 | } |
| 69 | return $relative_range_id; |
| 70 | } |
| 71 | public function columns() |
| 72 | { |
| 73 | $value = $_GET['cols'] ?? null; |
| 74 | if ($value === null) { |
| 75 | return null; |
| 76 | } |
| 77 | $columns = \explode(',', $value); |
| 78 | \array_map(function ($column) { |
| 79 | return \sanitize_text_field($column); |
| 80 | }, $columns); |
| 81 | return $columns; |
| 82 | } |
| 83 | public function visible_datasets() |
| 84 | { |
| 85 | $value = $_GET['visible_datasets'] ?? null; |
| 86 | if ($value === null) { |
| 87 | return ['visitors', 'views']; |
| 88 | } |
| 89 | $visible_datasets = \explode(',', $value); |
| 90 | \array_map(function ($column) { |
| 91 | return \sanitize_text_field($column); |
| 92 | }, $visible_datasets); |
| 93 | return $visible_datasets; |
| 94 | } |
| 95 | public function filters() |
| 96 | { |
| 97 | $value = $_GET['filters'] ?? null; |
| 98 | if ($value === null) { |
| 99 | return []; |
| 100 | } else { |
| 101 | $value = \base64_decode($value); |
| 102 | $filters = \json_decode($value); |
| 103 | $sanitized_filters = []; |
| 104 | foreach ($filters as $filter) { |
| 105 | $sanitized_filter = []; |
| 106 | foreach ($filter as $key => $value) { |
| 107 | $sanitized_filter[$key] = \sanitize_text_field($value); |
| 108 | } |
| 109 | $sanitized_filters[] = $sanitized_filter; |
| 110 | } |
| 111 | } |
| 112 | return $sanitized_filters; |
| 113 | } |
| 114 | public function sort_by() |
| 115 | { |
| 116 | return $this->get_query_value('sort_by') ?? 'visitors'; |
| 117 | } |
| 118 | public function sort_direction() |
| 119 | { |
| 120 | return $this->get_query_value('sort_direction') ?? 'desc'; |
| 121 | } |
| 122 | public function group() : ?string |
| 123 | { |
| 124 | return $this->get_query_value('group') ?? null; |
| 125 | } |
| 126 | public function chart_interval() : ?Interval |
| 127 | { |
| 128 | $chart_interval = $this->get_query_value('chart_interval'); |
| 129 | if (\is_null($chart_interval)) { |
| 130 | return Intervals::default_for($this->get_date_range()->number_of_days()); |
| 131 | } |
| 132 | return Intervals::find_by_id($this->get_query_value('chart_interval')); |
| 133 | } |
| 134 | private function has_exact_range() |
| 135 | { |
| 136 | return $this->get_query_value('start') !== null && $this->get_query_value('end') !== null; |
| 137 | } |
| 138 | private function get_query_value($key) |
| 139 | { |
| 140 | $value = $_GET[$key] ?? null; |
| 141 | if ($value === null) { |
| 142 | return null; |
| 143 | } |
| 144 | return \sanitize_text_field($value); |
| 145 | } |
| 146 | } |
| 147 |