PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.29.0
Independent Analytics – WordPress Analytics Plugin v1.29.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / AJAX / Filters_AJAX.php
independent-analytics / IAWP / AJAX Last commit date
AJAX.php 3 years ago Cleared_Cache_AJAX.php 3 years ago Create_Campaign_AJAX.php 3 years ago Delete_Campaign_AJAX.php 3 years ago Delete_Data_AJAX.php 3 years ago Export_Campaigns_AJAX.php 3 years ago Export_Devices_AJAX.php 3 years ago Export_Geo_AJAX.php 3 years ago Export_Pages_AJAX.php 3 years ago Export_Referrers_AJAX.php 3 years ago Filters_AJAX.php 3 years ago Migration_Status_AJAX.php 3 years ago Real_Time_AJAX.php 3 years ago Test_Email_AJAX.php 3 years ago Update_Capabilities_AJAX.php 3 years ago
Filters_AJAX.php
146 lines
1 <?php
2
3 namespace IAWP_SCOPED\IAWP\AJAX;
4
5 use DateTime;
6 use IAWP_SCOPED\IAWP\Chart;
7 use IAWP_SCOPED\IAWP\Chart_Geo;
8 use IAWP_SCOPED\IAWP\City_To_Country_Converter;
9 use IAWP_SCOPED\IAWP\Date_Range\Date_Range;
10 use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range;
11 use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range;
12 use IAWP_SCOPED\IAWP\Queries\Campaigns;
13 use IAWP_SCOPED\IAWP\Queries\Referrers;
14 use IAWP_SCOPED\IAWP\Quick_Stats;
15 use IAWP_SCOPED\IAWP\Statistics\Intervals\Intervals;
16 use IAWP_SCOPED\IAWP\Tables\Table_Campaigns;
17 use IAWP_SCOPED\IAWP\Tables\Table_Devices;
18 use IAWP_SCOPED\IAWP\Tables\Table_Geo;
19 use IAWP_SCOPED\IAWP\Tables\Table_Pages;
20 use IAWP_SCOPED\IAWP\Tables\Table_Referrers;
21 use IAWP_SCOPED\Proper\Timezone;
22 use Throwable;
23 class Filters_AJAX extends AJAX
24 {
25 protected function action_name() : string
26 {
27 return 'iawp_filter';
28 }
29 protected function action_required_fields() : array
30 {
31 return ['table_type', 'columns'];
32 }
33 protected function action_callback() : void
34 {
35 $date_range = $this->get_date_range();
36 $is_new_date_range = $this->get_field('is_new_date_range') === 'true';
37 $filters = $this->get_field('filters') ?? [];
38 $visible_columns = $this->get_field('columns');
39 $visible_datasets = $this->get_field('visible_datasets') ?? null;
40 $table_type = $this->get_field('table_type');
41 $sort_by = $this->get_field('sort_by') ?? null;
42 $sort_direction = $this->get_field('sort_direction') ?? null;
43 $group = $this->get_field('group') ?? null;
44 $is_new_group = $this->get_field('is_new_group') === 'true';
45 $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval'));
46 $page = $this->get_field('page') ?? 1;
47 $is_pages_table = $table_type === 'views';
48 $is_referrers_table = $table_type === 'referrers';
49 $is_geo_table = $table_type === 'geo';
50 $is_campaigns_table = $table_type === 'campaigns';
51 $is_devices_table = $table_type === 'devices';
52 if ($is_pages_table) {
53 // Todo - A table should know how to generate the resource it's going to work with
54 $table = new Table_Pages($visible_columns);
55 $filters = $table->sanitize_filters($filters);
56 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
57 $table_data_class = $table->group()->table_data_class();
58 $resources = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
59 $rows = $resources->rows();
60 $unfiltered_statistics = $resources->unfiltered_statistics();
61 $filtered_statistics = $resources->statistics();
62 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
63 } elseif ($is_referrers_table) {
64 $table = new Table_Referrers($visible_columns);
65 $filters = $table->sanitize_filters($filters);
66 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
67 $table_data_class = $table->group()->table_data_class();
68 $referrers = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
69 $rows = $referrers->rows();
70 $unfiltered_statistics = $referrers->unfiltered_statistics();
71 $filtered_statistics = $referrers->statistics();
72 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
73 } elseif ($is_geo_table) {
74 $table = new Table_Geo($visible_columns, $group, $is_new_group);
75 $filters = $table->sanitize_filters($filters);
76 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
77 $table_data_class = $table->group()->table_data_class();
78 $geos = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
79 $rows = $geos->rows();
80 $unfiltered_statistics = $geos->unfiltered_statistics();
81 $filtered_statistics = $geos->statistics();
82 if ($table->group()->has_id('city')) {
83 $country_rows = City_To_Country_Converter::convert($rows);
84 $chart = new Chart_Geo($country_rows, $date_range->label());
85 } else {
86 $chart = new Chart_Geo($rows, $date_range->label());
87 }
88 } elseif ($is_devices_table) {
89 $table = new Table_Devices($visible_columns, $group, $is_new_group);
90 $filters = $table->sanitize_filters($filters);
91 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
92 $table_data_class = $table->group()->table_data_class();
93 $devices = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
94 $rows = $devices->rows();
95 $unfiltered_statistics = $devices->unfiltered_statistics();
96 $filtered_statistics = $devices->statistics();
97 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
98 } elseif ($is_campaigns_table) {
99 $table = new Table_Campaigns($visible_columns);
100 $filters = $table->sanitize_filters($filters);
101 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
102 $table_data_class = $table->group()->table_data_class();
103 $campaigns = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
104 $rows = $campaigns->rows();
105 $unfiltered_statistics = $campaigns->unfiltered_statistics();
106 $filtered_statistics = $campaigns->statistics();
107 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
108 } else {
109 return;
110 }
111 $table->set_statistics($filtered_statistics ?? $unfiltered_statistics);
112 $page_size = \IAWP_SCOPED\iawp()->pagination_page_size();
113 $paged_rows = \array_slice($rows, 0, $page_size * $page);
114 $is_last_page = \count($rows) === \count($paged_rows);
115 $quick_stats = new Quick_Stats($filtered_statistics, $unfiltered_statistics);
116 \wp_send_json_success(['rows' => $table->get_row_markup($paged_rows), 'table' => $table->get_row_markup($paged_rows, \false, $sort_configuration->column(), $sort_configuration->is_ascending() ? 'asc' : 'desc'), 'totalRowCount' => \count($rows), 'chart' => $chart->get_html($visible_datasets), 'stats' => $quick_stats->get_html(), 'label' => $date_range->label(), 'isLastPage' => $is_last_page, 'columns' => $table->visible_column_ids(), 'columnsHTML' => $table->column_picker_html(), 'groupId' => $table->group()->id(), 'groupName' => $table->group()->singular(), 'pluralGroupName' => $table->group()->plural(), 'filters' => $filters, 'filtersTemplateHTML' => $table->filters_template_html(), 'chartInterval' => $chart_interval->id()]);
117 }
118 /**
119 * Get the date range for the filter request
120 *
121 * The date info can be supplied in one of two ways.
122 *
123 * The first is to provide a relative_range_id which is converted into start, end, and label.
124 *
125 * The second is to provide explicit start and end fields which will be used as is.
126 *
127 * @return Date_Range
128 */
129 private function get_date_range() : Date_Range
130 {
131 $relative_range_id = $this->get_field('relative_range_id');
132 $start_timestamp = $this->get_field('exact_start');
133 $end_timestamp = $this->get_field('exact_end');
134 if (!\is_null($start_timestamp) && !\is_null($end_timestamp)) {
135 try {
136 $start = new DateTime($start_timestamp, Timezone::site_timezone());
137 $end = new DateTime($end_timestamp, Timezone::site_timezone());
138 return new Exact_Date_Range($start, $end);
139 } catch (Throwable $e) {
140 // Do nothing and fall back to default relative date range
141 }
142 }
143 return new Relative_Date_Range($relative_range_id);
144 }
145 }
146