PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.11.6
Independent Analytics – WordPress Analytics Plugin v2.11.6
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 All 120 releases
independent-analytics / IAWP / AJAX / Filter.php
Filter.php
111 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IAWP\AJAX;
4
5 use DateTime;
6 use IAWP\Chart;
7 use IAWP\Date_Range\Date_Range;
8 use IAWP\Date_Range\Exact_Date_Range;
9 use IAWP\Date_Range\Relative_Date_Range;
10 use IAWP\Env;
11 use IAWP\Map;
12 use IAWP\Map_Data;
13 use IAWP\Quick_Stats;
14 use IAWP\Statistics\Intervals\Intervals;
15 use IAWP\Statistics\Statistics;
16 use IAWP\Tables\Table;
17 use IAWP\Utils\Timezone;
18 use Throwable;
19 /** @internal */
20 class Filter extends \IAWP\AJAX\AJAX
21 {
22 protected function action_name() : string
23 {
24 return 'iawp_filter';
25 }
26 protected function action_required_fields() : array
27 {
28 return ['table_type', 'columns'];
29 }
30 protected function action_callback() : void
31 {
32 $date_range = $this->get_date_range();
33 $is_new_date_range = $this->get_field('is_new_date_range') === 'true';
34 $filters = $this->get_field('filters') ?? [];
35 $sort_column = $this->get_field('sort_column') ?? null;
36 $sort_direction = $this->get_field('sort_direction') ?? null;
37 $group = $this->get_field('group') ?? null;
38 $as_csv = $this->get_field('as_csv') ?? \false;
39 $is_new_group = $this->get_field('is_new_group') === 'true';
40 $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval'));
41 $page = \intval($this->get_field('page') ?? 1);
42 $number_of_rows = $page * \IAWPSCOPED\iawp()->pagination_page_size();
43 $table_type = $this->get_field('table_type');
44 $is_geo_table = $table_type === 'geo';
45 $table_class = Env::get_table();
46 /** @var Table $table */
47 $table = new $table_class($group, $is_new_group);
48 $filters = $table->sanitize_filters($filters);
49 $sort_configuration = $table->sanitize_sort_parameters($sort_column, $sort_direction);
50 $rows_class = $table->group()->rows_class();
51 $statistics_class = $table->group()->statistics_class();
52 if ($as_csv) {
53 $rows_query = new $rows_class($date_range, null, $filters, $sort_configuration);
54 $rows = $rows_query->rows();
55 $csv = $table->csv($rows, \true);
56 echo $csv->to_string();
57 return;
58 }
59 if ($is_geo_table) {
60 $rows_query = new $rows_class($date_range, null, $filters, $sort_configuration);
61 } else {
62 $rows_query = new $rows_class($date_range, $number_of_rows, $filters, $sort_configuration);
63 }
64 $rows = $rows_query->rows();
65 if (empty($filters)) {
66 /** @var Statistics $statistics */
67 $statistics = new $statistics_class($date_range, null, $chart_interval);
68 } else {
69 $statistics = new $statistics_class($date_range, $rows_query, $chart_interval);
70 }
71 $total_number_of_rows = $statistics->total_number_of_rows();
72 if ($is_geo_table) {
73 $map_data = new Map_Data($rows);
74 $chart = new Map($map_data->get_country_data(), $date_range->label());
75 $rows = \array_slice($rows, 0, $number_of_rows);
76 } else {
77 $chart = new Chart($statistics);
78 }
79 $table->set_statistics($statistics);
80 $quick_stats = new Quick_Stats($statistics);
81 \wp_send_json_success(['rows' => $table->get_rendered_template($rows, \true, $sort_configuration->column(), $sort_configuration->direction()), 'table' => $table->get_rendered_template($rows, \false, $sort_configuration->column(), $sort_configuration->direction()), 'totalNumberOfRows' => $total_number_of_rows, 'chart' => $chart->get_html(), 'stats' => $quick_stats->get_html(), 'label' => $date_range->label(), 'isLastPage' => \count($rows) < \IAWPSCOPED\iawp()->pagination_page_size() * $page, 'columns' => $table->visible_column_ids(), 'columnsHTML' => $table->column_picker_html(), 'groupId' => $table->group()->id(), 'filters' => $filters, 'filtersTemplateHTML' => $table->filters_template_html(), 'filtersButtonsHTML' => $table->filters_condition_buttons_html($filters), 'chartInterval' => $chart_interval->id()]);
82 }
83 /**
84 * Get the date range for the filter request
85 *
86 * The date info can be supplied in one of two ways.
87 *
88 * The first is to provide a relative_range_id which is converted into start, end, and label.
89 *
90 * The second is to provide explicit start and end fields which will be used as is.
91 *
92 * @return Date_Range
93 */
94 private function get_date_range() : Date_Range
95 {
96 $relative_range_id = $this->get_field('relative_range_id');
97 $exact_start = $this->get_field('exact_start');
98 $exact_end = $this->get_field('exact_end');
99 if (!\is_null($exact_start) && !\is_null($exact_end)) {
100 try {
101 $start = new DateTime($exact_start, Timezone::site_timezone());
102 $end = new DateTime($exact_end, Timezone::site_timezone());
103 return new Exact_Date_Range($start, $end);
104 } catch (Throwable $e) {
105 // Do nothing and fall back to default relative date range
106 }
107 }
108 return new Relative_Date_Range($relative_range_id);
109 }
110 }
111