PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.15
Independent Analytics – WordPress Analytics Plugin v1.15
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 3 years ago migrations 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 dashboard_options.php 3 years ago dashboard_widget.php 3 years ago db.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 independent_analytics.php 3 years ago known_referrers.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
391 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 $key => $value): ?>
102 <option value="<?php esc_attr_e($key) ?>"
103 data-type="<?php esc_attr_e(self::get_column_data_type($key)) ?>"
104 >
105 <?php esc_html_e($value) ?>
106 </option>
107 <?php endforeach ?>
108 </select>
109 <?php
110 }
111
112 private function get_all_operator_selects()
113 {
114 $html = '';
115 foreach (self::get_data_types() as $data_type) {
116 $html .= '<select data-filters-target="operator" data-type="' . esc_attr($data_type) . '">';
117 foreach (self::get_operators($data_type) as $key => $value) {
118 $html .= '<option value="' . esc_attr($key) . '">' . esc_html($value) . '</option>';
119 }
120 $html .= '</select>';
121 }
122
123 return $html;
124 }
125
126 private function get_all_operand_fields($columns)
127 {
128 $html = '';
129 foreach ($columns as $key => $value) {
130 if ($key == 'title') {
131 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="title" type="text" />';
132 } elseif ($key == 'referrer') {
133 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="referrer" type="text" />';
134 } elseif ($key == 'continent') {
135 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="continent" type="text" />';
136 } elseif ($key == 'country') {
137 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="country" type="text" />';
138 } elseif ($key == 'subdivision') {
139 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="subdivision" type="text" />';
140 } elseif ($key == 'city') {
141 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="city" type="text" />';
142 } elseif ($key == 'utm_source') {
143 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_source" type="text" />';
144 } elseif ($key == 'utm_medium') {
145 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_medium" type="text" />';
146 } elseif ($key == 'utm_campaign') {
147 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_campaign" type="text" />';
148 } elseif ($key == 'utm_term') {
149 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_term" type="text" />';
150 } elseif ($key == 'utm_content') {
151 $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="utm_content" type="text" />';
152 } elseif ($key == 'referrer_type') {
153 $html .= '<select data-filters-target="operand" data-column="referrer_type">';
154 $html .= '<option value="Search">' . esc_html__('Search', 'iawp') . '</option>';
155 $html .= '<option value="Social">' . esc_html__('Social', 'iawp') . '</option>';
156 $html .= '<option value="Referrer">' . esc_html__('Referrer', 'iawp') . '</option>';
157 $html .= '<option value="Direct">' . esc_html__('Direct', 'iawp') . '</option>';
158 $html .= '</select>';
159 } elseif ($key == 'url') {
160 $html .= '<input type="text" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="url"/>';
161 } elseif ($key == 'views') {
162 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="views"/>';
163 } elseif ($key == 'views_growth') {
164 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="views_growth"/>';
165 } elseif ($key == 'visitors') {
166 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="visitors"/>';
167 } elseif ($key == 'visitors_growth') {
168 $html .= '<input type="number" data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="visitors_growth"/>';
169 } elseif ($key == 'author') {
170 $html .= '<select data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="author">';
171 foreach (IAWP()->get_users_can_write() as $author) {
172 $html .= '<option value="' . esc_attr($author->ID) . '">' . esc_html($author->display_name) . '</option>';
173 }
174 $html .= '</select>';
175 } elseif ($key == 'type') {
176 $html .= '<select data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="type">';
177 $html .= '<option value="post">' . esc_html__('Post', 'iawp') . '</option>';
178 $html .= '<option value="page">' . esc_html__('Page', 'iawp') . '</option>';
179 $html .= '<option value="attachment">' . esc_html__('Attachment', 'iawp') . '</option>';
180 foreach (IAWP()->get_custom_types() as $cpt) {
181 $html .= '<option value="' . esc_attr($cpt) . '">' . esc_html(get_post_type_object($cpt)->labels->singular_name) . '</option>';
182 }
183 $html .= '<option value="category">' . esc_html__('Category', 'iawp') . '</option>';
184 $html .= '<option value="post_tag">' . esc_html__('Tag', 'iawp') . '</option>';
185 foreach (IAWP()->get_custom_types(true) as $tax) {
186 $html .= '<option value="' . esc_attr($tax) . '">' . esc_html(get_taxonomy_labels(get_taxonomy($tax))->singular_name) . '</option>';
187 }
188 $html .= '<option value="blog-archive">' . esc_html__('Blog Home', 'iawp') . '</option>';
189 $html .= '<option value="author-archive">' . esc_html__('Author Archive', 'iawp') . '</option>';
190 $html .= '<option value="date-archive">' . esc_html__('Date Archive', 'iawp') . '</option>';
191 $html .= '<option value="search-archive">' . esc_html__('Search Results', 'iawp') . '</option>';
192 $html .= '<option value="not-found">' . esc_html__('404', 'iawp') . '</option>';
193 $html .= '</select>';
194 } elseif ($key == 'date') {
195 $html .= '<input type="text"
196 data-filters-target="operand"
197 data-action="keydown->filters#operandKeyDown filters#operandChange"
198 data-column="date"
199 data-controller="easepick"
200 data-css="' . esc_url(url_to('dist/styles/easepick/datepicker.css')) . '" data-dow="' . absint(IAWP()->get_option('iawp_dow', 1)) . '"
201 data-format="' . esc_attr(Date_Format::js()) . '" />';
202 } elseif ($key == 'category') {
203 $html .= '<select data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="category">';
204 foreach (get_categories() as $category) {
205 $html .= '<option value="' . esc_attr($category->term_id) . '">' . esc_html($category->name) . '</option>';
206 }
207 $html .= '</select>';
208 }
209 }
210
211 return $html;
212 }
213
214 private function get_data_types()
215 {
216 return ['string', 'int', 'date', 'bool'];
217 }
218
219 private function get_operators(string $data_type)
220 {
221 if ($data_type == 'string') {
222 return [
223 'contains' => esc_html__('Contains', 'iawp'),
224 'exact' => esc_html__('Exactly matches', 'iawp'),
225 ];
226 } elseif ($data_type == 'int') {
227 return [
228 'greater' => esc_html__('Greater than', 'iawp'),
229 'lesser' => esc_html__('Less than', 'iawp'),
230 'equal' => esc_html__('Equal to', 'iawp'),
231 ];
232 } elseif ($data_type == 'bool') {
233 return [
234 'is' => esc_html__('Is', 'iawp'),
235 'isnt' => esc_html__('Isn\'t', 'iawp'),
236 ];
237 } elseif ($data_type == 'date') {
238 return [
239 'before' => esc_html__('Before', 'iawp'),
240 'after' => esc_html__('After', 'iawp'),
241 'on' => esc_html__('On', 'iawp'),
242 ];
243 } else {
244 return null;
245 }
246 }
247
248 private function get_column_data_type(string $column)
249 {
250 $columns = [
251 'title' => 'string',
252 'url' => 'string',
253 'referrer' => 'string',
254 'continent' => 'string',
255 'country' => 'string',
256 'subdivision' => 'string',
257 'city' => 'string',
258 'utm_source' => 'string',
259 'utm_medium' => 'string',
260 'utm_campaign' => 'string',
261 'utm_term' => 'string',
262 'utm_content' => 'string',
263 'author' => 'bool',
264 'type' => 'bool',
265 'referrer_type' => 'bool',
266 'category' => 'bool',
267 'views' => 'int',
268 'views_growth' => 'int',
269 'visitors' => 'int',
270 'visitors_growth' => 'int',
271 'date' => 'date',
272 ];
273
274 if (array_key_exists($column, $columns)) {
275 return $columns[$column];
276 } else {
277 return null;
278 }
279 }
280
281 public function filter_views(array $rows, array $filters)
282 {
283 if ($filters == '') {
284 return $rows;
285 }
286 $filtered_rows = [];
287
288 foreach ($rows as $row) {
289 $match_count = 0;
290 foreach ($filters as $filter) {
291 if (self::include_row($row, $filter)) {
292 $match_count++;
293 }
294 }
295 if ($match_count == count($filters)) {
296 $filtered_rows[] = $row;
297 }
298 }
299
300 return $filtered_rows;
301 }
302
303 public function include_row($row, $filter)
304 {
305 $inclusion = $filter['inclusion'];
306 $column = $filter['column'];
307 $operator = $filter['operator'];
308 $operand = strtolower($filter['operand']);
309 $column_data_type = $this->get_column_data_type($column);
310 $match = false;
311
312 if ($column_data_type === 'int') {
313 if ($operator === 'greater') {
314 if ($row->$column() > $operand) {
315 $match = true;
316 }
317 } elseif ($operator === 'lesser') {
318 if ($row->$column() < $operand) {
319 $match = true;
320 }
321 } elseif ($operator === 'equal') {
322 if ($row->$column() == $operand) {
323 $match = true;
324 }
325 }
326 } elseif ($column_data_type === 'string') {
327 $value = strtolower($row->$column());
328
329 if ($operator == 'contains') {
330 if (strpos($value, $operand) !== false) {
331 $match = true;
332 }
333 } elseif ($operator == 'exact') {
334 if ($value == $operand) {
335 $match = true;
336 }
337 }
338 } elseif ($filter['column'] == 'referrer_type') {
339 $referrer_type = strtolower($row->referrer_type());
340 if ($filter['operator'] == 'is' && $referrer_type == $operand) {
341 $match = true;
342 } elseif ($filter['operator'] == 'isnt' && $referrer_type != $operand) {
343 $match = true;
344 }
345 } elseif ($filter['column'] == 'author') {
346 if ($filter['operator'] == 'is' && $row->author_id() == $operand) {
347 $match = true;
348 } elseif ($filter['operator'] == 'isnt' && $row->author_id() != $operand) {
349 $match = true;
350 }
351 } elseif ($filter['column'] == 'type') {
352 if ($filter['operator'] == 'is' && $row->type(true) == $operand) {
353 $match = true;
354 } elseif ($filter['operator'] == 'isnt' && $row->type(true) != $operand) {
355 $match = true;
356 }
357 } elseif ($filter['column'] == 'date') {
358 // Don't "continue" or row won't be included when excluding
359 if ($row->date() != null) {
360 $date = new \DateTime($operand);
361 if ($filter['operator'] == 'before' && $row->date() < $date->format('U')) {
362 $match = true;
363 } elseif ($filter['operator'] == 'after' && $row->date() > $date->format('U')) {
364 $match = true;
365 } elseif ($filter['operator'] == 'on') {
366 $formatted_date = \DateTime::createFromFormat('U', $row->date());
367 $formatted_date = $formatted_date->format('Y-m-d');
368 if ($date->format('Y-m-d') == $formatted_date) {
369 $match = true;
370 }
371 }
372 }
373 } elseif ($filter['column'] == 'category') {
374 if ($filter['operator'] == 'is' && in_array($operand, $row->category(false) ?? [])) {
375 $match = true;
376 } elseif ($filter['operator'] == 'isnt' && !in_array($operand, $row->category(false) ?? [])) {
377 $match = true;
378 }
379 }
380
381 if (
382 ($filter['inclusion'] == 'include' && $match) ||
383 ($filter['inclusion'] == 'exclude' && !$match)
384 ) {
385 return true;
386 } else {
387 return false;
388 }
389 }
390 }
391