PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.11.5
Independent Analytics – WordPress Analytics Plugin v2.11.5
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 / Export_Report_Table.php
Export_Report_Table.php
68 lines 2.4 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\Date_Range\Date_Range;
7 use IAWP\Date_Range\Exact_Date_Range;
8 use IAWP\Date_Range\Relative_Date_Range;
9 use IAWP\Env;
10 use IAWP\Tables\Table;
11 use IAWP\Utils\Timezone;
12 use Throwable;
13 /** @internal */
14 class Export_Report_Table extends \IAWP\AJAX\AJAX
15 {
16 protected function action_name() : string
17 {
18 return 'iawp_export_report_table';
19 }
20 protected function action_callback() : void
21 {
22 $date_range = $this->get_date_range();
23 $filters = $this->get_field('filters') ?? [];
24 $sort_column = $this->get_field('sort_column') ?? null;
25 $sort_direction = $this->get_field('sort_direction') ?? null;
26 $group = $this->get_field('group') ?? null;
27 $table_class = Env::get_table();
28 /** @var Table $table */
29 $table = new $table_class($group);
30 $filters = $table->sanitize_filters($filters);
31 $sort_configuration = $table->sanitize_sort_parameters($sort_column, $sort_direction);
32 $rows_class = $table->group()->rows_class();
33 $rows_query = new $rows_class($date_range, null, $filters, $sort_configuration);
34 $rows = $rows_query->rows();
35 $csv = $table->csv($rows, \true)->to_string();
36 $csv = \wp_kses($csv, 'strip');
37 $csv = \str_replace('&amp;', '&', $csv);
38 \wp_send_json_success(['csv' => $csv]);
39 }
40 /**
41 * Get the date range for the filter request
42 *
43 * The date info can be supplied in one of two ways.
44 *
45 * The first is to provide a relative_range_id which is converted into start, end, and label.
46 *
47 * The second is to provide explicit start and end fields which will be used as is.
48 *
49 * @return Date_Range
50 */
51 private function get_date_range() : Date_Range
52 {
53 $relative_range_id = $this->get_field('relative_range_id');
54 $exact_start = $this->get_field('exact_start');
55 $exact_end = $this->get_field('exact_end');
56 if (!\is_null($exact_start) && !\is_null($exact_end)) {
57 try {
58 $start = new DateTime($exact_start, Timezone::site_timezone());
59 $end = new DateTime($exact_end, Timezone::site_timezone());
60 return new Exact_Date_Range($start, $end);
61 } catch (Throwable $e) {
62 // Do nothing and fall back to default relative date range
63 }
64 }
65 return new Relative_Date_Range($relative_range_id);
66 }
67 }
68