AJAX
1 year ago
Admin_Page
1 year ago
Custom_WordPress_Columns
2 years ago
Data_Pruning
1 year ago
Date_Picker
1 year ago
Date_Range
1 year ago
Ecommerce
1 year ago
Email_Reports
1 year ago
Filter_Lists
2 years ago
Form_Submissions
1 year ago
Interval
2 years ago
Menu_Bar_Stats
1 year ago
Migrations
1 year ago
Models
1 year ago
Public_API
2 years ago
Rows
1 year ago
Statistics
1 year ago
Tables
1 year ago
Utils
1 year ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
1 year ago
Chart.php
1 year ago
Chart_Geo.php
1 year ago
Cron_Job.php
1 year ago
Cron_Job_Autoloader.php
1 year ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
1 year ago
Filters.php
1 year ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
1 year ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
1 year ago
Interrupt.php
1 year ago
Known_Referrers.php
2 years ago
Patch.php
1 year ago
Plugin_Conflict_Detector.php
1 year ago
Plugin_Group.php
1 year ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
1 year ago
Quick_Stats.php
2 years ago
REST_API.php
1 year ago
Real_Time.php
1 year ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
1 year ago
Resource_Identifier.php
1 year ago
Settings.php
1 year ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
1 year ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
Dashboard_Options.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWP\Date_Range\Date_Range; |
| 7 | use IAWP\Date_Range\Exact_Date_Range; |
| 8 | use IAWP\Date_Range\Relative_Date_Range; |
| 9 | use IAWP\Statistics\Intervals\Interval; |
| 10 | use IAWP\Statistics\Intervals\Intervals; |
| 11 | use IAWP\Tables\Table; |
| 12 | use IAWP\Utils\Request; |
| 13 | use IAWP\Utils\Singleton; |
| 14 | use IAWPSCOPED\Proper\Timezone; |
| 15 | use Throwable; |
| 16 | /** |
| 17 | * Dashboards support various options via the search query string portion of the URL. |
| 18 | * |
| 19 | * The Dashboard_Options class give you an interface for fetching any set values or falling back |
| 20 | * to a default value as needed. |
| 21 | * @internal |
| 22 | */ |
| 23 | class Dashboard_Options |
| 24 | { |
| 25 | use Singleton; |
| 26 | private $report; |
| 27 | private static $default_visible_quick_stats = ['visitors', 'views', 'sessions', 'average_session_duration', 'bounce_rate', 'views_per_session', 'wc_orders', 'wc_net_sales']; |
| 28 | private function __construct() |
| 29 | { |
| 30 | $this->report = $this->fetch_current_report(); |
| 31 | } |
| 32 | public function report_name() : ?string |
| 33 | { |
| 34 | if (\is_null($this->report->name ?? null)) { |
| 35 | return 'Report'; |
| 36 | } |
| 37 | return $this->report->name; |
| 38 | } |
| 39 | public function visible_columns() : ?array |
| 40 | { |
| 41 | if (Request::get_post_array('columns')) { |
| 42 | return Request::get_post_array('columns'); |
| 43 | } |
| 44 | if (\is_null($this->report) || \is_null($this->report->columns)) { |
| 45 | return null; |
| 46 | } |
| 47 | return \json_decode($this->report->columns, \true); |
| 48 | } |
| 49 | public function visible_quick_stats() : array |
| 50 | { |
| 51 | if (Request::get_post_array('quick_stats')) { |
| 52 | return Request::get_post_array('quick_stats'); |
| 53 | } |
| 54 | $decoded_value = \json_decode($this->report->quick_stats ?? 'null', \true); |
| 55 | if (\is_array($decoded_value)) { |
| 56 | return $decoded_value; |
| 57 | } |
| 58 | return self::$default_visible_quick_stats; |
| 59 | } |
| 60 | public function primary_chart_metric_id() : string |
| 61 | { |
| 62 | if (Request::get_post_string('primary_chart_metric_id')) { |
| 63 | return Request::get_post_string('primary_chart_metric_id'); |
| 64 | } |
| 65 | if (\is_null($this->report->primary_chart_metric_id ?? null)) { |
| 66 | return 'visitors'; |
| 67 | } |
| 68 | return $this->report->primary_chart_metric_id; |
| 69 | } |
| 70 | public function secondary_chart_metric_id() : ?string |
| 71 | { |
| 72 | if (Request::get_post_string('secondary_chart_metric_id')) { |
| 73 | return Request::get_post_string('secondary_chart_metric_id'); |
| 74 | } |
| 75 | if (\is_null($this->report->secondary_chart_metric_id ?? null)) { |
| 76 | return 'views'; |
| 77 | } |
| 78 | return $this->report->secondary_chart_metric_id; |
| 79 | } |
| 80 | public function filters() : array |
| 81 | { |
| 82 | if (\is_null($this->report) || \is_null($this->report->filters)) { |
| 83 | return []; |
| 84 | } |
| 85 | $table_class = Table::get_table_by_type($this->report->type); |
| 86 | $table = new $table_class($this->report->group_name ?? null); |
| 87 | $filters = \json_decode($this->report->filters, \true); |
| 88 | return \is_null($filters) ? [] : $table->sanitize_filters($filters); |
| 89 | } |
| 90 | public function sort_column() : string |
| 91 | { |
| 92 | return $this->report->sort_column ?? 'visitors'; |
| 93 | } |
| 94 | public function sort_direction() : string |
| 95 | { |
| 96 | return $this->report->sort_direction ?? 'desc'; |
| 97 | } |
| 98 | public function group() : ?string |
| 99 | { |
| 100 | return $this->report->group_name ?? null; |
| 101 | } |
| 102 | public function chart_interval() : ?Interval |
| 103 | { |
| 104 | if (\is_null($this->report->chart_interval ?? null)) { |
| 105 | return Intervals::default_for($this->get_date_range()->number_of_days()); |
| 106 | } |
| 107 | return Intervals::find_by_id($this->report->chart_interval); |
| 108 | } |
| 109 | /** |
| 110 | * @return Date_Range |
| 111 | */ |
| 112 | public function get_date_range() : Date_Range |
| 113 | { |
| 114 | if ($this->has_exact_range()) { |
| 115 | try { |
| 116 | $start = new DateTime($this->start(), Timezone::site_timezone()); |
| 117 | $end = new DateTime($this->end(), Timezone::site_timezone()); |
| 118 | return new Exact_Date_Range($start, $end); |
| 119 | } catch (Throwable $e) { |
| 120 | // Do nothing and fall back to default relative date range |
| 121 | } |
| 122 | } |
| 123 | return new Relative_Date_Range($this->relative_range_id()); |
| 124 | } |
| 125 | public function start() : ?string |
| 126 | { |
| 127 | if (!$this->has_exact_range()) { |
| 128 | return null; |
| 129 | } |
| 130 | return $this->report->exact_start; |
| 131 | } |
| 132 | public function end() : ?string |
| 133 | { |
| 134 | if (!$this->has_exact_range()) { |
| 135 | return null; |
| 136 | } |
| 137 | return $this->report->exact_end; |
| 138 | } |
| 139 | /** |
| 140 | * Prefer exact range to relative range if both are provided |
| 141 | */ |
| 142 | public function relative_range_id() : ?string |
| 143 | { |
| 144 | $relative_range_id = $this->report->relative_range_id ?? null; |
| 145 | if (!$this->has_exact_range() && $relative_range_id === null) { |
| 146 | return 'LAST_THIRTY'; |
| 147 | } elseif ($this->has_exact_range()) { |
| 148 | return null; |
| 149 | } elseif (Relative_Date_Range::is_valid_range($relative_range_id) === \false) { |
| 150 | return 'LAST_THIRTY'; |
| 151 | } |
| 152 | return $relative_range_id; |
| 153 | } |
| 154 | public function maybe_redirect() : void |
| 155 | { |
| 156 | if (\IAWP\Env::get_page() !== 'independent-analytics') { |
| 157 | return; |
| 158 | } |
| 159 | if (empty($_GET['report']) && empty($_GET['tab'])) { |
| 160 | $favorite_report = \IAWP\Report_Finder::get_favorite(); |
| 161 | if (\is_null($favorite_report)) { |
| 162 | return; |
| 163 | } |
| 164 | \wp_safe_redirect($favorite_report->url()); |
| 165 | exit; |
| 166 | } |
| 167 | if (!empty($_GET['report']) && \is_null($this->report)) { |
| 168 | \wp_safe_redirect(\IAWPSCOPED\iawp_dashboard_url(['tab' => \IAWP\Env::get_tab()])); |
| 169 | exit; |
| 170 | } |
| 171 | if (!\is_null($this->report) && \IAWP\Env::get_tab() !== $this->report->type) { |
| 172 | \wp_safe_redirect(\IAWPSCOPED\iawp_dashboard_url(['tab' => $this->report->type, 'report' => $this->report->report_id])); |
| 173 | exit; |
| 174 | } |
| 175 | } |
| 176 | public function is_sidebar_collapsed() : bool |
| 177 | { |
| 178 | $is_sidebar_collapsed = \get_user_meta(\get_current_user_id(), 'iawp_is_sidebar_collapsed', \true) === '1'; |
| 179 | return $is_sidebar_collapsed; |
| 180 | } |
| 181 | private function fetch_current_report() : ?object |
| 182 | { |
| 183 | $reports_table = \IAWP\Query::get_table_name(\IAWP\Query::REPORTS); |
| 184 | $report_id = \filter_input(\INPUT_GET, 'report', \FILTER_VALIDATE_INT); |
| 185 | if (!\is_int($report_id)) { |
| 186 | return null; |
| 187 | } |
| 188 | return \IAWP\Illuminate_Builder::get_builder()->from($reports_table)->where('report_id', '=', $report_id)->first(); |
| 189 | } |
| 190 | private function has_exact_range() : bool |
| 191 | { |
| 192 | return !\is_null($this->report->exact_start ?? null) && !\is_null($this->report->exact_end ?? null); |
| 193 | } |
| 194 | } |
| 195 |