PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.30.0
Independent Analytics – WordPress Analytics Plugin v1.30.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 2 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 2 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
149 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\Quick_Stats;
13 use IAWP_SCOPED\IAWP\Statistics\Intervals\Intervals;
14 use IAWP_SCOPED\IAWP\Tables\Table_Campaigns;
15 use IAWP_SCOPED\IAWP\Tables\Table_Devices;
16 use IAWP_SCOPED\IAWP\Tables\Table_Geo;
17 use IAWP_SCOPED\IAWP\Tables\Table_Pages;
18 use IAWP_SCOPED\IAWP\Tables\Table_Referrers;
19 use IAWP_SCOPED\Proper\Timezone;
20 use Throwable;
21 class Filters_AJAX extends AJAX
22 {
23 protected function action_name() : string
24 {
25 return 'iawp_filter';
26 }
27 protected function action_required_fields() : array
28 {
29 return ['table_type', 'columns'];
30 }
31 protected function action_callback() : void
32 {
33 $date_range = $this->get_date_range();
34 $is_new_date_range = $this->get_field('is_new_date_range') === 'true';
35 $filters = $this->get_field('filters') ?? [];
36 $visible_columns = $this->get_field('columns');
37 $visible_datasets = $this->get_field('visible_datasets') ?? null;
38 $table_type = $this->get_field('table_type');
39 $sort_by = $this->get_field('sort_by') ?? null;
40 $sort_direction = $this->get_field('sort_direction') ?? null;
41 $group = $this->get_field('group') ?? null;
42 $as_csv = $this->get_field('as_csv') ?? \false;
43 $is_new_group = $this->get_field('is_new_group') === 'true';
44 $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval'));
45 $page = $this->get_field('page') ?? 1;
46 $is_pages_table = $table_type === 'views';
47 $is_referrers_table = $table_type === 'referrers';
48 $is_geo_table = $table_type === 'geo';
49 $is_campaigns_table = $table_type === 'campaigns';
50 $is_devices_table = $table_type === 'devices';
51 if ($is_pages_table) {
52 // Todo - A table should know how to generate the resource it's going to work with
53 $table = new Table_Pages($visible_columns);
54 $filters = $table->sanitize_filters($filters);
55 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
56 $table_data_class = $table->group()->table_data_class();
57 $resources = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
58 $rows = $resources->rows();
59 $unfiltered_statistics = $resources->unfiltered_statistics();
60 $filtered_statistics = $resources->statistics();
61 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
62 } elseif ($is_referrers_table) {
63 $table = new Table_Referrers($visible_columns);
64 $filters = $table->sanitize_filters($filters);
65 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
66 $table_data_class = $table->group()->table_data_class();
67 $referrers = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
68 $rows = $referrers->rows();
69 $unfiltered_statistics = $referrers->unfiltered_statistics();
70 $filtered_statistics = $referrers->statistics();
71 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
72 } elseif ($is_geo_table) {
73 $table = new Table_Geo($visible_columns, $group, $is_new_group);
74 $filters = $table->sanitize_filters($filters);
75 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
76 $table_data_class = $table->group()->table_data_class();
77 $geos = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
78 $rows = $geos->rows();
79 $unfiltered_statistics = $geos->unfiltered_statistics();
80 $filtered_statistics = $geos->statistics();
81 if ($table->group()->has_id('city')) {
82 $country_rows = City_To_Country_Converter::convert($rows);
83 $chart = new Chart_Geo($country_rows, $date_range->label());
84 } else {
85 $chart = new Chart_Geo($rows, $date_range->label());
86 }
87 } elseif ($is_devices_table) {
88 $table = new Table_Devices($visible_columns, $group, $is_new_group);
89 $filters = $table->sanitize_filters($filters);
90 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
91 $table_data_class = $table->group()->table_data_class();
92 $devices = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
93 $rows = $devices->rows();
94 $unfiltered_statistics = $devices->unfiltered_statistics();
95 $filtered_statistics = $devices->statistics();
96 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
97 } elseif ($is_campaigns_table) {
98 $table = new Table_Campaigns($visible_columns);
99 $filters = $table->sanitize_filters($filters);
100 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
101 $table_data_class = $table->group()->table_data_class();
102 $campaigns = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval);
103 $rows = $campaigns->rows();
104 $unfiltered_statistics = $campaigns->unfiltered_statistics();
105 $filtered_statistics = $campaigns->statistics();
106 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
107 } else {
108 return;
109 }
110 $table->set_statistics($filtered_statistics ?? $unfiltered_statistics);
111 if ($as_csv) {
112 echo $table->csv($rows, \true);
113 return;
114 }
115 $page_size = \IAWP_SCOPED\iawp()->pagination_page_size();
116 $paged_rows = \array_slice($rows, 0, $page_size * $page);
117 $is_last_page = \count($rows) === \count($paged_rows);
118 $quick_stats = new Quick_Stats($filtered_statistics, $unfiltered_statistics);
119 \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()]);
120 }
121 /**
122 * Get the date range for the filter request
123 *
124 * The date info can be supplied in one of two ways.
125 *
126 * The first is to provide a relative_range_id which is converted into start, end, and label.
127 *
128 * The second is to provide explicit start and end fields which will be used as is.
129 *
130 * @return Date_Range
131 */
132 private function get_date_range() : Date_Range
133 {
134 $relative_range_id = $this->get_field('relative_range_id');
135 $start_timestamp = $this->get_field('exact_start');
136 $end_timestamp = $this->get_field('exact_end');
137 if (!\is_null($start_timestamp) && !\is_null($end_timestamp)) {
138 try {
139 $start = new DateTime($start_timestamp, Timezone::site_timezone());
140 $end = new DateTime($end_timestamp, Timezone::site_timezone());
141 return new Exact_Date_Range($start, $end);
142 } catch (Throwable $e) {
143 // Do nothing and fall back to default relative date range
144 }
145 }
146 return new Relative_Date_Range($relative_range_id);
147 }
148 }
149