PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.17
Independent Analytics – WordPress Analytics Plugin v1.17
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 / filters.php
independent-analytics / IAWP Last commit date
Migrations 3 years ago ajax 3 years ago models 3 years ago queries 3 years ago sql 3 years ago tables 3 years ago utils 3 years ago campaign_builder.php 3 years ago capability_manager.php 3 years ago chart.php 3 years ago chart_geo.php 3 years ago chart_svg.php 3 years ago dashboard_options.php 3 years ago dashboard_widget.php 3 years ago email_reports.php 3 years ago filters.php 3 years ago freemius.php 3 years ago geo_database.php 3 years ago geo_database_download_job.php 3 years ago health_check.php 3 years ago independent_analytics.php 3 years ago known_referrers.php 3 years ago pdf.php 3 years ago query.php 3 years ago quick_stats.php 3 years ago real_time.php 3 years ago rest_api.php 3 years ago settings.php 3 years ago track_resource_changes.php 3 years ago view_counter.php 3 years ago
filters.php
402 lines
1 <?php
2
3 namespace IAWP;
4
5 class Filters
6 {
7 public function output_filters(array $columns)
8 {
9 $opts = new Dashboard_Options(); ?>
10 <div class="modal-parent"
11 data-controller="filters"
12 data-filters-filters-value="<?php esc_html_e(json_encode($opts->filters())) ?>"
13 >
14 <button id="filters-button" class="iawp-button ghost-white"
15 data-action="filters#toggleModal"
16 data-filters-target="modalButton"
17 >
18 <span class="dashicons dashicons-filter"></span>
19 <?php echo ' ' . esc_html__('Filter Rows', 'iawp') . ' ' ?>
20 <span class="count" data-filters-target="filterCount"></span>
21 </button>
22 <div class="modal large"
23 data-filters-target="modal"
24 >
25 <div class="modal-inner">
26 <div class="title-small"><?php esc_html_e('Filters', 'iawp'); ?></div>
27 <div id="filters" data-filters-target="filters" class="filters" data-filters="[]">
28 </div>
29 <template data-filters-target="blueprint">
30 <?php echo Security::form(self::get_condition_html($columns)); ?>
31 </template>
32 <div>
33 <button id="add-condition" class="iawp-button text"
34 data-action="filters#addCondition"
35 >
36 <?php esc_html_e('+ Add another condition', 'iawp'); ?>
37 </button>
38 </div>
39 <div class="actions">
40 <button id="filters-apply" class="iawp-button purple"
41 data-action="filters#apply"
42 >
43 <?php esc_html_e('Apply', 'iawp') ?>
44 </button>
45 <button id="filters-reset" class="iawp-button ghost-purple"
46 data-action="filters#reset"
47 data-filters-target="reset"
48 disabled
49 >
50 <?php esc_html_e('Reset', 'iawp'); ?>
51 </button>
52 </div>
53 </div>
54 </div>
55 </div><?php
56 }
57
58 private function get_condition_html(array $columns)
59 {
60 ?>
61 <div class="condition" data-filters-target="condition">
62 <div class="input-group">
63 <div>
64 <?php echo self::get_inclusion_selects(); ?>
65 </div>
66 <div>
67 <?php echo self::get_column_select($columns); ?>
68 </div>
69 <div class="operator-select-container">
70 <?php echo self::get_all_operator_selects(); ?>
71 </div>
72 <div class="operand-field-container">
73 <?php echo self::get_all_operand_fields($columns); ?>
74 </div>
75 </div>
76 <button class="delete-button" data-action="filters#removeCondition">
77 <span class="dashicons dashicons-no"></span></button>
78 </div>
79 <?php
80 }
81
82 private function get_inclusion_selects()
83 {
84 $html = '<select class="filters-include" data-filters-target="inclusion">';
85 $html .= '<option value="include">' . esc_html__('Include', 'iawp') . '</option>';
86 $html .= '<option value="exclude">' . esc_html__('Exclude', 'iawp') . '</option>';
87 $html .= '</select>';
88
89 return $html;
90 }
91
92 private function get_column_select(array $columns)
93 {
94 ?>
95 <select class="filters-column" data-filters-target="column"
96 data-action="filters#columnSelect"
97 >
98 <option value="">
99 <?php esc_html_e('Choose a column', 'iawp') ?>
100 </option>
101 <?php foreach ($columns as $column): ?>
102 <?php if ($column->requires_pro() && !IAWP_FS()->can_use_premium_code__premium_only()) {
103 continue;
104 } ?>
105 <option value="<?php esc_attr_e($column->id()) ?>"
106 data-type="<?php esc_attr_e(self::get_column_data_type($column->id())) ?>"
107 >
108 <?php esc_html_e($column->label()) ?>
109 </option>
110 <?php endforeach ?>
111 </select>
112 <?php
113 }
114
115 private function get_all_operator_selects()
116 {
117 $html = '';
118 foreach (self::get_data_types() as $data_type) {
119 $html .= '<select data-filters-target="operator" data-type="' . esc_attr($data_type) . '">';
120 foreach (self::get_operators($data_type) as $key => $value) {
121 $html .= '<option value="' . esc_attr($key) . '">' . esc_html($value) . '</option>';
122 }
123 $html .= '</select>';
124 }
125
126 return $html;
127 }
128
129 private function get_all_operand_fields($columns)
130 {
131 $html = '';
132 foreach ($columns as $column) {
133 $id = $column->id();
134 if ($id == 'title') {
135 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="title" type="text" />';
136 } elseif ($id == 'referrer') {
137 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="referrer" type="text" />';
138 } elseif ($id == 'continent') {
139 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="continent" type="text" />';
140 } elseif ($id == 'country') {
141 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="country" type="text" />';
142 } elseif ($id == 'subdivision') {
143 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="subdivision" type="text" />';
144 } elseif ($id == 'city') {
145 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="city" type="text" />';
146 } elseif ($id == 'utm_source') {
147 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_source" type="text" />';
148 } elseif ($id == 'utm_medium') {
149 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_medium" type="text" />';
150 } elseif ($id == 'utm_campaign') {
151 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_campaign" type="text" />';
152 } elseif ($id == 'utm_term') {
153 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_term" type="text" />';
154 } elseif ($id == 'utm_content') {
155 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_content" type="text" />';
156 } elseif ($id == 'referrer_type') {
157 $html .= '<select data-filters-target="operand" data-column="referrer_type">';
158 $html .= '<option value="Search">' . esc_html__('Search', 'iawp') . '</option>';
159 $html .= '<option value="Social">' . esc_html__('Social', 'iawp') . '</option>';
160 $html .= '<option value="Referrer">' . esc_html__('Referrer', 'iawp') . '</option>';
161 $html .= '<option value="Direct">' . esc_html__('Direct', 'iawp') . '</option>';
162 $html .= '</select>';
163 } elseif ($id == 'url') {
164 $html .= '<input type="text" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="url"/>';
165 } elseif ($id == 'views') {
166 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="views"/>';
167 } elseif ($id == 'views_growth') {
168 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="views_growth"/>';
169 } elseif ($id == 'visitors') {
170 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="visitors"/>';
171 } elseif ($id == 'visitors_growth') {
172 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="visitors_growth"/>';
173 } elseif (in_array($id, ['wc_orders', 'wc_gross_sales', 'wc_refunds', 'wc_refunded_amount', 'wc_net_sales'])) {
174 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="' . esc_attr($id) . '"/>';
175 } elseif ($id == 'author') {
176 $html .= '<select data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="author">';
177 foreach (IAWP()->get_users_can_write() as $author) {
178 $html .= '<option value="' . esc_attr($author->ID) . '">' . esc_html($author->display_name) . '</option>';
179 }
180 $html .= '</select>';
181 } elseif ($id == 'type') {
182 $html .= '<select data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="type">';
183 $html .= '<option value="post">' . esc_html__('Post', 'iawp') . '</option>';
184 $html .= '<option value="page">' . esc_html__('Page', 'iawp') . '</option>';
185 $html .= '<option value="attachment">' . esc_html__('Attachment', 'iawp') . '</option>';
186 foreach (IAWP()->get_custom_types() as $cpt) {
187 $html .= '<option value="' . esc_attr($cpt) . '">' . esc_html(get_post_type_object($cpt)->labels->singular_name) . '</option>';
188 }
189 $html .= '<option value="category">' . esc_html__('Category', 'iawp') . '</option>';
190 $html .= '<option value="post_tag">' . esc_html__('Tag', 'iawp') . '</option>';
191 foreach (IAWP()->get_custom_types(true) as $tax) {
192 $html .= '<option value="' . esc_attr($tax) . '">' . esc_html(get_taxonomy_labels(get_taxonomy($tax))->singular_name) . '</option>';
193 }
194 $html .= '<option value="blog-archive">' . esc_html__('Blog Home', 'iawp') . '</option>';
195 $html .= '<option value="author-archive">' . esc_html__('Author Archive', 'iawp') . '</option>';
196 $html .= '<option value="date-archive">' . esc_html__('Date Archive', 'iawp') . '</option>';
197 $html .= '<option value="search-archive">' . esc_html__('Search Results', 'iawp') . '</option>';
198 $html .= '<option value="not-found">' . esc_html__('404', 'iawp') . '</option>';
199 $html .= '</select>';
200 } elseif ($id == 'date') {
201 $html .= '<input type="text"
202 data-filters-target="operand"
203 data-action="keydown->filters#operandKeyDown filters#operandChange"
204 data-column="date"
205 data-controller="easepick"
206 data-css="' . esc_url(url_to('dist/styles/easepick/datepicker.css')) . '" data-dow="' . absint(IAWP()->get_option('iawp_dow', 1)) . '"
207 data-format="' . esc_attr(Date_Format::js()) . '" />';
208 } elseif ($id == 'category') {
209 $html .= '<select data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="category">';
210 foreach (get_categories() as $category) {
211 $html .= '<option value="' . esc_attr($category->term_id) . '">' . esc_html($category->name) . '</option>';
212 }
213 $html .= '</select>';
214 }
215 }
216
217 return $html;
218 }
219
220 private function get_data_types()
221 {
222 return ['string', 'int', 'date', 'bool'];
223 }
224
225 private function get_operators(string $data_type)
226 {
227 if ($data_type == 'string') {
228 return [
229 'contains' => esc_html__('Contains', 'iawp'),
230 'exact' => esc_html__('Exactly matches', 'iawp'),
231 ];
232 } elseif ($data_type == 'int') {
233 return [
234 'greater' => esc_html__('Greater than', 'iawp'),
235 'lesser' => esc_html__('Less than', 'iawp'),
236 'equal' => esc_html__('Equal to', 'iawp'),
237 ];
238 } elseif ($data_type == 'bool') {
239 return [
240 'is' => esc_html__('Is', 'iawp'),
241 'isnt' => esc_html__('Isn\'t', 'iawp'),
242 ];
243 } elseif ($data_type == 'date') {
244 return [
245 'before' => esc_html__('Before', 'iawp'),
246 'after' => esc_html__('After', 'iawp'),
247 'on' => esc_html__('On', 'iawp'),
248 ];
249 } else {
250 return null;
251 }
252 }
253
254 private function get_column_data_type(string $column)
255 {
256 $columns = [
257 'title' => 'string',
258 'url' => 'string',
259 'referrer' => 'string',
260 'continent' => 'string',
261 'country' => 'string',
262 'subdivision' => 'string',
263 'city' => 'string',
264 'utm_source' => 'string',
265 'utm_medium' => 'string',
266 'utm_campaign' => 'string',
267 'utm_term' => 'string',
268 'utm_content' => 'string',
269 'author' => 'bool',
270 'type' => 'bool',
271 'referrer_type' => 'bool',
272 'category' => 'bool',
273 'views' => 'int',
274 'views_growth' => 'int',
275 'visitors' => 'int',
276 'visitors_growth' => 'int',
277 'date' => 'date',
278 'wc_orders' => 'int',
279 'wc_gross_sales' => 'int',
280 'wc_refunds' => 'int',
281 'wc_refunded_amount' => 'int',
282 'wc_net_sales' => 'int',
283 ];
284
285 if (array_key_exists($column, $columns)) {
286 return $columns[$column];
287 } else {
288 return null;
289 }
290 }
291
292 public function filter_views(array $rows, array $filters)
293 {
294 if ($filters == '') {
295 return $rows;
296 }
297 $filtered_rows = [];
298
299 foreach ($rows as $row) {
300 $match_count = 0;
301 foreach ($filters as $filter) {
302 if (self::include_row($row, $filter)) {
303 $match_count++;
304 }
305 }
306 if ($match_count == count($filters)) {
307 $filtered_rows[] = $row;
308 }
309 }
310
311 return $filtered_rows;
312 }
313
314 public function include_row($row, $filter)
315 {
316 $inclusion = $filter['inclusion'];
317 $column = $filter['column'];
318 $operator = $filter['operator'];
319 $operand = strtolower($filter['operand']);
320 $column_data_type = $this->get_column_data_type($column);
321 $match = false;
322
323 if ($column_data_type === 'int') {
324 if ($operator === 'greater') {
325 if ($row->$column() > $operand) {
326 $match = true;
327 }
328 } elseif ($operator === 'lesser') {
329 if ($row->$column() < $operand) {
330 $match = true;
331 }
332 } elseif ($operator === 'equal') {
333 if ($row->$column() == $operand) {
334 $match = true;
335 }
336 }
337 } elseif ($column_data_type === 'string') {
338 $value = strtolower($row->$column());
339
340 if ($operator == 'contains') {
341 if (strpos($value, $operand) !== false) {
342 $match = true;
343 }
344 } elseif ($operator == 'exact') {
345 if ($value == $operand) {
346 $match = true;
347 }
348 }
349 } elseif ($filter['column'] == 'referrer_type') {
350 $referrer_type = strtolower($row->referrer_type());
351 if ($filter['operator'] == 'is' && $referrer_type == $operand) {
352 $match = true;
353 } elseif ($filter['operator'] == 'isnt' && $referrer_type != $operand) {
354 $match = true;
355 }
356 } elseif ($filter['column'] == 'author') {
357 if ($filter['operator'] == 'is' && $row->author_id() == $operand) {
358 $match = true;
359 } elseif ($filter['operator'] == 'isnt' && $row->author_id() != $operand) {
360 $match = true;
361 }
362 } elseif ($filter['column'] == 'type') {
363 if ($filter['operator'] == 'is' && $row->type(true) == $operand) {
364 $match = true;
365 } elseif ($filter['operator'] == 'isnt' && $row->type(true) != $operand) {
366 $match = true;
367 }
368 } elseif ($filter['column'] == 'date') {
369 // Don't "continue" or row won't be included when excluding
370 if ($row->date() != null) {
371 $date = new \DateTime($operand);
372 if ($filter['operator'] == 'before' && $row->date() < $date->format('U')) {
373 $match = true;
374 } elseif ($filter['operator'] == 'after' && $row->date() > $date->format('U')) {
375 $match = true;
376 } elseif ($filter['operator'] == 'on') {
377 $formatted_date = \DateTime::createFromFormat('U', $row->date());
378 $formatted_date = $formatted_date->format('Y-m-d');
379 if ($date->format('Y-m-d') == $formatted_date) {
380 $match = true;
381 }
382 }
383 }
384 } elseif ($filter['column'] == 'category') {
385 if ($filter['operator'] == 'is' && in_array($operand, $row->category(false) ?? [])) {
386 $match = true;
387 } elseif ($filter['operator'] == 'isnt' && !in_array($operand, $row->category(false) ?? [])) {
388 $match = true;
389 }
390 }
391
392 if (
393 ($filter['inclusion'] == 'include' && $match) ||
394 ($filter['inclusion'] == 'exclude' && !$match)
395 ) {
396 return true;
397 } else {
398 return false;
399 }
400 }
401 }
402