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