AJAX
2 years ago
Date_Range
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years 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_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.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
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 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
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
Report_Finder.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | /** @internal */ |
| 6 | class Report_Finder |
| 7 | { |
| 8 | public function __construct() |
| 9 | { |
| 10 | } |
| 11 | public function fetch_reports_by_type() : array |
| 12 | { |
| 13 | return [['name' => \__('Pages', 'independent-analytics'), 'reports' => $this->fetch_page_reports()], ['name' => \__('Referrers', 'independent-analytics'), 'reports' => $this->fetch_referrer_reports()], ['name' => \__('Geographic', 'independent-analytics'), 'reports' => $this->fetch_geographic_reports()], ['name' => \__('Devices', 'independent-analytics'), 'reports' => $this->fetch_device_reports()], ['name' => \__('Campaigns', 'independent-analytics'), 'reports' => $this->fetch_campaign_reports()]]; |
| 14 | } |
| 15 | /** |
| 16 | * @return Report[] |
| 17 | */ |
| 18 | public function fetch_page_reports() : array |
| 19 | { |
| 20 | return $this->by_type('views'); |
| 21 | } |
| 22 | /** |
| 23 | * @return Report[] |
| 24 | */ |
| 25 | public function fetch_referrer_reports() : array |
| 26 | { |
| 27 | return $this->by_type('referrers'); |
| 28 | } |
| 29 | /** |
| 30 | * @return Report[] |
| 31 | */ |
| 32 | public function fetch_geographic_reports() : array |
| 33 | { |
| 34 | return $this->by_type('geo'); |
| 35 | } |
| 36 | /** |
| 37 | * @return Report[] |
| 38 | */ |
| 39 | public function fetch_device_reports() : array |
| 40 | { |
| 41 | return $this->by_type('devices'); |
| 42 | } |
| 43 | /** |
| 44 | * @return Report[] |
| 45 | */ |
| 46 | public function fetch_campaign_reports() : array |
| 47 | { |
| 48 | return $this->by_type('campaigns'); |
| 49 | } |
| 50 | public function is_real_time() : bool |
| 51 | { |
| 52 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'real-time'; |
| 53 | } |
| 54 | public function is_settings_page() : bool |
| 55 | { |
| 56 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'settings'; |
| 57 | } |
| 58 | public function is_campaign_builder_page() : bool |
| 59 | { |
| 60 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'campaign-builder'; |
| 61 | } |
| 62 | public function is_page_report() : bool |
| 63 | { |
| 64 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'views'; |
| 65 | } |
| 66 | public function is_referrer_report() : bool |
| 67 | { |
| 68 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'referrers'; |
| 69 | } |
| 70 | public function is_geographic_report() : bool |
| 71 | { |
| 72 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'geo'; |
| 73 | } |
| 74 | public function is_device_report() : bool |
| 75 | { |
| 76 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'devices'; |
| 77 | } |
| 78 | public function is_campaign_report() : bool |
| 79 | { |
| 80 | return \IAWP_SCOPED\iawp()->get_current_tab() === 'campaigns'; |
| 81 | } |
| 82 | public function is_saved_report() : bool |
| 83 | { |
| 84 | $report = $this->current(); |
| 85 | if (\is_null($report)) { |
| 86 | return \false; |
| 87 | } |
| 88 | return $report->is_saved_report(); |
| 89 | } |
| 90 | public function current() : ?Report |
| 91 | { |
| 92 | $report_id = \array_key_exists('report', $_GET) ? \sanitize_text_field($_GET['report']) : null; |
| 93 | if (\is_null($report_id)) { |
| 94 | return self::get_base_report_for_current_tab(); |
| 95 | } |
| 96 | $report = self::by_id($report_id); |
| 97 | if (\is_null($report)) { |
| 98 | return self::get_base_report_for_current_tab(); |
| 99 | } |
| 100 | return $report; |
| 101 | } |
| 102 | /** |
| 103 | * @param array $ids |
| 104 | * |
| 105 | * @return array|Report[]|null |
| 106 | */ |
| 107 | public function by_ids(array $ids) : ?array |
| 108 | { |
| 109 | $reports_table = Query::get_table_name(Query::REPORTS); |
| 110 | $rows = Illuminate_Builder::get_builder()->from($reports_table)->whereIn('report_id', $ids)->get()->toArray(); |
| 111 | return \array_map(function ($row) { |
| 112 | return new Report($row); |
| 113 | }, $rows); |
| 114 | } |
| 115 | /** |
| 116 | * @param string $type |
| 117 | * |
| 118 | * @return Report[] |
| 119 | */ |
| 120 | public function by_type(string $type) : array |
| 121 | { |
| 122 | $reports_table = Query::get_table_name(Query::REPORTS); |
| 123 | $builder = Illuminate_Builder::get_builder()->from($reports_table)->where('type', '=', $type)->orderByRaw('position IS NULL')->orderBy('position')->orderBy('report_id')->get()->escapeWhenCastingToString(); |
| 124 | $rows = $builder->toArray(); |
| 125 | return \array_map(function ($row) { |
| 126 | return new Report($row); |
| 127 | }, $rows); |
| 128 | } |
| 129 | public static function get_base_report_for_current_tab() : ?Report |
| 130 | { |
| 131 | return self::get_base_report_for_type(\IAWP_SCOPED\iawp()->get_current_tab()); |
| 132 | } |
| 133 | public static function get_favorite() : ?Report |
| 134 | { |
| 135 | $raw_id = \get_user_meta(\get_current_user_id(), 'iawp_favorite_report_id', \true); |
| 136 | $id = \filter_var($raw_id, \FILTER_VALIDATE_INT); |
| 137 | if ($id !== \false) { |
| 138 | return self::by_id($id); |
| 139 | } |
| 140 | $raw_type = \get_user_meta(\get_current_user_id(), 'iawp_favorite_report_type', \true); |
| 141 | $type = \filter_var($raw_type, \FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
| 142 | return self::get_base_report_for_type($type); |
| 143 | } |
| 144 | /** |
| 145 | * @param string|int $id |
| 146 | * |
| 147 | * @return Report|null |
| 148 | */ |
| 149 | public static function by_id($id) : ?Report |
| 150 | { |
| 151 | $id = (string) $id; |
| 152 | $reports_table = Query::get_table_name(Query::REPORTS); |
| 153 | if (!\ctype_digit($id)) { |
| 154 | return null; |
| 155 | } |
| 156 | $row = Illuminate_Builder::get_builder()->from($reports_table)->where('report_id', '=', $id)->first(); |
| 157 | if (\is_null($row)) { |
| 158 | return null; |
| 159 | } |
| 160 | return new Report($row); |
| 161 | } |
| 162 | public static function create_report(array $attributes) : Report |
| 163 | { |
| 164 | $reports_table = Query::get_table_name(Query::REPORTS); |
| 165 | if (\array_key_exists('columns', $attributes) && \is_array($attributes['columns'])) { |
| 166 | $attributes['columns'] = \json_encode($attributes['columns']); |
| 167 | } |
| 168 | if (\array_key_exists('filters', $attributes) && \is_array($attributes['filters'])) { |
| 169 | $attributes['filters'] = \json_encode($attributes['filters']); |
| 170 | } |
| 171 | if (\array_key_exists('visible_datasets', $attributes) && \is_array($attributes['visible_datasets'])) { |
| 172 | $attributes['visible_datasets'] = \json_encode($attributes['visible_datasets']); |
| 173 | } |
| 174 | $report_id = Illuminate_Builder::get_builder()->from($reports_table)->insertGetId($attributes); |
| 175 | return self::by_id($report_id); |
| 176 | } |
| 177 | private static function get_base_report_for_type(string $type) : ?Report |
| 178 | { |
| 179 | switch ($type) { |
| 180 | case 'views': |
| 181 | return new Report((object) ['name' => 'Pages', 'type' => 'views']); |
| 182 | case 'referrers': |
| 183 | return new Report((object) ['name' => 'Referrers', 'type' => 'referrers']); |
| 184 | case 'geo': |
| 185 | return new Report((object) ['name' => 'Geographic', 'type' => 'geo']); |
| 186 | case 'devices': |
| 187 | return new Report((object) ['name' => 'Devices', 'type' => 'devices']); |
| 188 | case 'campaigns': |
| 189 | return new Report((object) ['name' => 'Campaigns', 'type' => 'campaigns']); |
| 190 | default: |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 |