PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.27.0
Independent Analytics – WordPress Analytics Plugin v1.27.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_Data_AJAX.php 3 years ago Export_Campaigns_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
134 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\Cities;
14 use IAWP_SCOPED\IAWP\Queries\Countries;
15 use IAWP_SCOPED\IAWP\Queries\Referrers;
16 use IAWP_SCOPED\IAWP\Queries\Resources;
17 use IAWP_SCOPED\IAWP\Quick_Stats;
18 use IAWP_SCOPED\IAWP\Statistics\Intervals\Intervals;
19 use IAWP_SCOPED\IAWP\Tables\Table_Campaigns;
20 use IAWP_SCOPED\IAWP\Tables\Table_Geo;
21 use IAWP_SCOPED\IAWP\Tables\Table_Pages;
22 use IAWP_SCOPED\IAWP\Tables\Table_Referrers;
23 use IAWP_SCOPED\Proper\Timezone;
24 use Throwable;
25 class Filters_AJAX extends AJAX
26 {
27 protected function action_name() : string
28 {
29 return 'iawp_filter';
30 }
31 protected function action_required_fields() : array
32 {
33 return ['table_type', 'columns'];
34 }
35 protected function action_callback() : void
36 {
37 $date_range = $this->get_date_range();
38 $is_new_date_range = $this->get_field('is_new_date_range') === 'true';
39 $filters = $this->get_field('filters') ?? [];
40 $visible_columns = $this->get_field('columns');
41 $visible_datasets = $this->get_field('visible_datasets') ?? null;
42 $table_type = $this->get_field('table_type');
43 $sort_by = $this->get_field('sort_by') ?? null;
44 $sort_direction = $this->get_field('sort_direction') ?? null;
45 $group = $this->get_field('group') ?? null;
46 $is_new_group = $this->get_field('is_new_group') === 'true';
47 $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval'));
48 $page = $this->get_field('page') ?? 1;
49 $is_pages_table = $table_type === 'views';
50 $is_referrers_table = $table_type === 'referrers';
51 $is_geo_table = $table_type === 'geo';
52 $is_campaigns_table = $table_type === 'campaigns';
53 if ($is_pages_table) {
54 // Todo - A table should know how to generate the resource it's going to work with
55 $table = new Table_Pages($visible_columns);
56 $filters = $table->sanitize_filters($filters);
57 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
58 $resources = new Resources($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 $referrers = new Referrers($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 $data_class = $table->group()->has_id('city') ? Cities::class : Countries::class;
77 $geos = new $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_campaigns_table) {
88 $table = new Table_Campaigns($visible_columns);
89 $filters = $table->sanitize_filters($filters);
90 $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction);
91 $campaigns = new Campaigns($date_range, $filters, $sort_configuration);
92 $rows = $campaigns->rows();
93 $unfiltered_statistics = $campaigns->unfiltered_statistics();
94 $filtered_statistics = $campaigns->statistics();
95 $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label());
96 } else {
97 return;
98 }
99 $table->set_statistics($filtered_statistics ?? $unfiltered_statistics);
100 $page_size = \IAWP_SCOPED\iawp()->pagination_page_size();
101 $paged_rows = \array_slice($rows, 0, $page_size * $page);
102 $is_last_page = \count($rows) === \count($paged_rows);
103 $quick_stats = new Quick_Stats($filtered_statistics, $unfiltered_statistics);
104 \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()->name(), 'filters' => $filters, 'filtersTemplateHTML' => $table->filters_template_html(), 'chartInterval' => $chart_interval->id()]);
105 }
106 /**
107 * Get the date range for the filter request
108 *
109 * The date info can be supplied in one of two ways.
110 *
111 * The first is to provide a relative_range_id which is converted into start, end, and label.
112 *
113 * The second is to provide explicit start and end fields which will be used as is.
114 *
115 * @return Date_Range
116 */
117 private function get_date_range() : Date_Range
118 {
119 $relative_range_id = $this->get_field('relative_range_id');
120 $start_timestamp = $this->get_field('exact_start');
121 $end_timestamp = $this->get_field('exact_end');
122 if (!\is_null($start_timestamp) && !\is_null($end_timestamp)) {
123 try {
124 $start = new DateTime($start_timestamp, Timezone::site_timezone());
125 $end = new DateTime($end_timestamp, Timezone::site_timezone());
126 return new Exact_Date_Range($start, $end);
127 } catch (Throwable $e) {
128 // Do nothing and fall back to default relative date range
129 }
130 }
131 return new Relative_Date_Range($relative_range_id);
132 }
133 }
134