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_Referrers_AJAX.php
3 years ago
Export_Views_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
194 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP\AJAX; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWP_SCOPED\IAWP\Statistics\Campaign_Statistics; |
| 7 | use IAWP_SCOPED\IAWP\Statistics\Geo_Statistics; |
| 8 | use IAWP_SCOPED\IAWP\Statistics\Page_Statistics; |
| 9 | use IAWP_SCOPED\IAWP\Statistics\Referrer_Statistics; |
| 10 | use Throwable; |
| 11 | use IAWP_SCOPED\IAWP\Chart; |
| 12 | use IAWP_SCOPED\IAWP\Chart_Geo; |
| 13 | use IAWP_SCOPED\IAWP\Date_Range\Date_Range; |
| 14 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range; |
| 15 | use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range; |
| 16 | use IAWP_SCOPED\IAWP\Filters; |
| 17 | use IAWP_SCOPED\IAWP\Models\Campaign; |
| 18 | use IAWP_SCOPED\IAWP\Models\Geo; |
| 19 | use IAWP_SCOPED\IAWP\Models\Referrer; |
| 20 | use IAWP_SCOPED\IAWP\Queries\Campaigns; |
| 21 | use IAWP_SCOPED\IAWP\Queries\City_Statistics; |
| 22 | use IAWP_SCOPED\IAWP\Queries\Country_Statistics; |
| 23 | use IAWP_SCOPED\IAWP\Queries\Referrers; |
| 24 | use IAWP_SCOPED\IAWP\Queries\Resources; |
| 25 | use IAWP_SCOPED\IAWP\Quick_Stats; |
| 26 | use IAWP_SCOPED\IAWP\Tables\Table_Campaigns; |
| 27 | use IAWP_SCOPED\IAWP\Tables\Table_Geo; |
| 28 | use IAWP_SCOPED\IAWP\Tables\Table_Referrers; |
| 29 | use IAWP_SCOPED\IAWP\Tables\Table_Pages; |
| 30 | use IAWP_SCOPED\IAWP\Utils\Timezone; |
| 31 | class Filters_AJAX extends AJAX |
| 32 | { |
| 33 | protected function action_name() : string |
| 34 | { |
| 35 | return 'iawp_filter'; |
| 36 | } |
| 37 | protected function action_required_fields() : array |
| 38 | { |
| 39 | return ['table_type', 'columns']; |
| 40 | } |
| 41 | /** |
| 42 | * Get the date range for the filter request |
| 43 | * |
| 44 | * The date info can be supplied in one of two ways. |
| 45 | * |
| 46 | * The first is to provide a relative_range_id which is converted into start, end, and label. |
| 47 | * |
| 48 | * The second is to provide explicit start and end fields which will be used as is. |
| 49 | * |
| 50 | * @return Date_Range |
| 51 | */ |
| 52 | private function get_date_range() : Date_Range |
| 53 | { |
| 54 | $relative_range_id = $this->get_field('relative_range_id'); |
| 55 | $start_timestamp = $this->get_field('exact_start'); |
| 56 | $end_timestamp = $this->get_field('exact_end'); |
| 57 | if (!\is_null($start_timestamp) && !\is_null($end_timestamp)) { |
| 58 | try { |
| 59 | $start = new DateTime($start_timestamp, Timezone::wordpress_site_timezone()); |
| 60 | $end = new DateTime($end_timestamp, Timezone::wordpress_site_timezone()); |
| 61 | return new Exact_Date_Range($start, $end); |
| 62 | } catch (Throwable $e) { |
| 63 | // Do nothing and fall back to default relative date range |
| 64 | } |
| 65 | } |
| 66 | return new Relative_Date_Range($relative_range_id); |
| 67 | } |
| 68 | protected function action_callback() : void |
| 69 | { |
| 70 | $filters = $this->get_field('filters') ?? []; |
| 71 | $columns = $this->get_field('columns'); |
| 72 | $table_type = $this->get_field('table_type'); |
| 73 | $is_pages_table = $table_type === 'views'; |
| 74 | $is_referrers_table = $table_type === 'referrers'; |
| 75 | $is_geo_table = $table_type === 'geo'; |
| 76 | $is_campaigns_table = $table_type === 'campaigns'; |
| 77 | $date_range = $this->get_date_range(); |
| 78 | $sort_by = $this->get_field('sort_by') ?? 'visitors'; |
| 79 | $sort_direction = $this->get_field('sort_direction') ?? 'desc'; |
| 80 | $page = $this->get_field('page') ?? 1; |
| 81 | $unfiltered_statistics = null; |
| 82 | if (!$is_pages_table && !$is_referrers_table && !$is_geo_table && !$is_campaigns_table) { |
| 83 | return; |
| 84 | } |
| 85 | if ($is_pages_table) { |
| 86 | $table = new Table_Pages($columns); |
| 87 | $resources = new Resources($date_range); |
| 88 | $rows = $resources->fetch(); |
| 89 | $viewed_resource_ids = $this->get_viewed_resource_ids($rows); |
| 90 | $statistics = new Page_Statistics($date_range, $viewed_resource_ids); |
| 91 | if (!empty($filters)) { |
| 92 | $unfiltered_statistics = $statistics; |
| 93 | $rows = $this->get_filtered_rows($rows, $filters); |
| 94 | $viewed_resource_ids = $this->get_viewed_resource_ids($rows); |
| 95 | $statistics = new Page_Statistics($date_range, $viewed_resource_ids); |
| 96 | } |
| 97 | $chart = new Chart($statistics, $date_range->label()); |
| 98 | } elseif ($is_referrers_table) { |
| 99 | $table = new Table_Referrers($columns); |
| 100 | $referrers = new Referrers($date_range); |
| 101 | $rows = $referrers->fetch(); |
| 102 | $referrer_ids = Referrer::referrer_ids($rows); |
| 103 | $statistics = new Referrer_Statistics($date_range, $referrer_ids); |
| 104 | if (!empty($filters)) { |
| 105 | $unfiltered_statistics = $statistics; |
| 106 | $rows = $this->get_filtered_rows($rows, $filters); |
| 107 | $referrer_ids = Referrer::referrer_ids($rows); |
| 108 | $statistics = new Referrer_Statistics($date_range, $referrer_ids); |
| 109 | } |
| 110 | $chart = new Chart($statistics, $date_range->label()); |
| 111 | } elseif ($is_geo_table) { |
| 112 | $table = new Table_Geo($columns); |
| 113 | $geos = new City_Statistics($date_range); |
| 114 | $rows = $geos->fetch(); |
| 115 | $geo_ids = Geo::geo_ids($rows); |
| 116 | $statistics = new Geo_Statistics($date_range, $geo_ids); |
| 117 | if (!empty($filters)) { |
| 118 | $unfiltered_statistics = $statistics; |
| 119 | $rows = $this->get_filtered_rows($rows, $filters); |
| 120 | $geo_ids = Geo::geo_ids($rows); |
| 121 | $statistics = new Geo_Statistics($date_range, $geo_ids); |
| 122 | } |
| 123 | $country_statistics = new Country_Statistics($date_range); |
| 124 | $stats = $this->get_filtered_rows($country_statistics->fetch(), $filters); |
| 125 | $chart = new Chart_Geo($stats, $date_range->label()); |
| 126 | } elseif ($is_campaigns_table) { |
| 127 | $table = new Table_Campaigns($columns); |
| 128 | $rows = (new Campaigns($date_range))->fetch(); |
| 129 | $campaign_ids = Campaign::campaigns_to_ids($rows); |
| 130 | $statistics = new Campaign_Statistics($date_range, $campaign_ids); |
| 131 | if (!empty($filters)) { |
| 132 | $unfiltered_statistics = $statistics; |
| 133 | $rows = $this->get_filtered_rows($rows, $filters); |
| 134 | $campaign_ids = Campaign::campaigns_to_ids($rows); |
| 135 | $statistics = new Campaign_Statistics($date_range, $campaign_ids); |
| 136 | } |
| 137 | $chart = new Chart($statistics, $date_range->label()); |
| 138 | } else { |
| 139 | return; |
| 140 | } |
| 141 | $rows = $this->sort_rows($rows, $sort_direction, $sort_by); |
| 142 | $page_size = \IAWP_SCOPED\iawp()->pagination_page_size(); |
| 143 | $paged_rows = \array_slice($rows, 0, $page_size * $page); |
| 144 | $is_last_page = \count($rows) === \count($paged_rows); |
| 145 | $table->set_statistics($statistics); |
| 146 | $quick_stats = new Quick_Stats($statistics, $unfiltered_statistics); |
| 147 | $html = $table->get_row_markup($paged_rows); |
| 148 | \wp_send_json_success(['rows' => $html, 'totalRowCount' => \count($rows), 'chart' => $chart->get_html(), 'stats' => $quick_stats->get_html(), 'label' => $date_range->label(), 'isLastPage' => $is_last_page]); |
| 149 | } |
| 150 | protected function get_viewed_resource_ids($rows) |
| 151 | { |
| 152 | return \array_map(function ($resource) { |
| 153 | return $resource->id(); |
| 154 | }, $rows); |
| 155 | } |
| 156 | protected function get_filtered_rows($rows, $filters) |
| 157 | { |
| 158 | $filterer = new Filters(); |
| 159 | return $filterer->fitler_rows($rows, $filters); |
| 160 | } |
| 161 | protected function sort_rows($rows, $sort_direction, $sort_by) |
| 162 | { |
| 163 | \usort($rows, function ($a, $b) use($sort_direction, $sort_by) { |
| 164 | if (\method_exists($a, $sort_by) && \method_exists($b, $sort_by)) { |
| 165 | $a_val = $a->{$sort_by}(); |
| 166 | $b_val = $b->{$sort_by}(); |
| 167 | $switch = $sort_direction === 'asc' ? 1 : -1; |
| 168 | // Null and empty values at bottom in asc and top in desc |
| 169 | $a_empty = \is_null($a_val) || \strlen($a_val) === 0; |
| 170 | $b_empty = \is_null($b_val) || \strlen($b_val) === 0; |
| 171 | if ($a_empty && !$b_empty) { |
| 172 | return 1; |
| 173 | } elseif ($b_empty && !$a_empty) { |
| 174 | return -1; |
| 175 | } elseif ($a_empty && $b_empty) { |
| 176 | return 0; |
| 177 | } |
| 178 | // Numbers below letters |
| 179 | $a_num = \is_numeric($a_val); |
| 180 | $b_num = \is_numeric($b_val); |
| 181 | if ($a_num && !$b_num) { |
| 182 | return $switch; |
| 183 | } elseif ($b_num && !$a_num) { |
| 184 | return $switch * -1; |
| 185 | } |
| 186 | return (\strtolower($a_val) <=> \strtolower($b_val)) * $switch; |
| 187 | } else { |
| 188 | return 0; |
| 189 | } |
| 190 | }); |
| 191 | return $rows; |
| 192 | } |
| 193 | } |
| 194 |