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