PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.5
Independent Analytics – WordPress Analytics Plugin v1.5
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
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
326 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 == 'author') {
110 $html .= '<select class="author-operand operand-field">';
111 foreach (IAWP()->get_users_can_write() as $author) {
112 $html .= '<option value="' . esc_attr($author->ID) . '">' . esc_html($author->display_name) . '</option>';
113 }
114 $html .= '</select>';
115 } elseif ($key == 'type') {
116 $html .= '<select class="type-operand operand-field">';
117 $html .= '<option value="post">' . esc_html__('Post', 'iawp') . '</option>';
118 $html .= '<option value="page">' . esc_html__('Page', 'iawp') . '</option>';
119 $html .= '<option value="attachment">' . esc_html__('Attachment', 'iawp') . '</option>';
120 foreach (IAWP()->get_custom_types() as $cpt) {
121 $html .= '<option value="' . esc_attr($cpt) . '">' . esc_html(get_post_type_object($cpt)->labels->singular_name) . '</option>';
122 }
123 $html .= '<option value="category">' . esc_html__('Category', 'iawp') . '</option>';
124 $html .= '<option value="post_tag">' . esc_html__('Tag', 'iawp') . '</option>';
125 foreach (IAWP()->get_custom_types(true) as $tax) {
126 $html .= '<option value="' . esc_attr($tax) . '">' . esc_html(get_taxonomy_labels(get_taxonomy($tax))->singular_name) . '</option>';
127 }
128 $html .= '<option value="blog-archive">' . esc_html__('Blog Home', 'iawp') . '</option>';
129 $html .= '<option value="author-archive">' . esc_html__('Author Archive', 'iawp') . '</option>';
130 $html .= '<option value="date-archive">' . esc_html__('Date Archive', 'iawp') . '</option>';
131 $html .= '<option value="search-archive">' . esc_html__('Search Results', 'iawp') . '</option>';
132 $html .= '<option value="not-found">' . esc_html__('404', 'iawp') . '</option>';
133 $html .= '</select>';
134 } elseif ($key == 'date') {
135 $html .= '<input class="date-operand operand-field" type="text"
136 data-css="' . esc_url(IAWP_URL) . 'dist/styles/easepick/datepicker.css" data-dow="' . absint(IAWP()->get_option('iawp_dow', 1)) . '"
137 data-format="' . esc_attr(Date_Format::js()) . '" />';
138 } elseif ($key == 'category') {
139 $html .= '<select class="category-operand operand-field">';
140 foreach (get_categories() as $category) {
141 $html .= '<option value="' . esc_attr($category->term_id) . '">' . esc_html($category->name) . '</option>';
142 }
143 $html .= '</select>';
144 }
145 }
146
147 return $html;
148 }
149
150 private function get_data_types()
151 {
152 return ['string', 'int', 'date', 'bool'];
153 }
154
155 private function get_operators(string $data_type)
156 {
157 if ($data_type == 'string') {
158 return [
159 'contains' => esc_html__('Contains', 'iawp'),
160 'exact' => esc_html__('Exactly matches', 'iawp'),
161 ];
162 } elseif ($data_type == 'int') {
163 return [
164 'greater' => esc_html__('Greater than', 'iawp'),
165 'lesser' => esc_html__('Less than', 'iawp'),
166 'equal' => esc_html__('Equal to', 'iawp'),
167 ];
168 } elseif ($data_type == 'bool') {
169 return [
170 'is' => esc_html__('Is', 'iawp'),
171 'isnt' => esc_html__('Isn\'t', 'iawp'),
172 ];
173 } elseif ($data_type == 'date') {
174 return [
175 'before' => esc_html__('Before', 'iawp'),
176 'after' => esc_html__('After', 'iawp'),
177 'on' => esc_html__('On', 'iawp'),
178 ];
179 } else {
180 return null;
181 }
182 }
183
184 private function get_column_data_type(string $column)
185 {
186 if ($column == 'title' || $column == 'url' || $column == 'referrer') {
187 return 'string';
188 } elseif ($column == 'author' || $column == 'type' || $column == 'referrer_type' || $column == 'category') {
189 return 'bool';
190 } elseif ($column == 'views') {
191 return 'int';
192 } elseif ($column == 'date') {
193 return 'date';
194 } else {
195 return null;
196 }
197 }
198
199 public function filter_views(array $rows, array $filters)
200 {
201 if ($filters == '') {
202 return $rows;
203 }
204 $filtered_rows = [];
205
206 foreach ($rows as $row) {
207 $match_count = 0;
208 foreach ($filters as $filter) {
209 if (self::include_row($row, $filter)) {
210 $match_count++;
211 }
212 }
213 if ($match_count == count($filters)) {
214 $filtered_rows[] = $row;
215 }
216 }
217
218 return $filtered_rows;
219 }
220
221 public function include_row($row, $filter)
222 {
223 $operand = strtolower($filter['operand']);
224 $match = false;
225 if ($filter['column'] == 'title') {
226 $title = strtolower($row->title());
227 if ($filter['operator'] == 'contains') {
228 if (strpos($title, $operand) !== false) {
229 $match = true;
230 }
231 } elseif ($filter['operator'] == 'exact') {
232 if ($title == $operand) {
233 $match = true;
234 }
235 }
236 } elseif ($filter['column'] == 'referrer') {
237 if ($filter['operator'] == 'contains') {
238 $referrer = strtolower($row->referrer());
239 if (strpos($referrer, $operand) !== false) {
240 $match = true;
241 }
242 } elseif ($filter['operator'] == 'exact') {
243 $referrer = strtolower($row->referrer());
244 if ($referrer == $operand) {
245 $match = true;
246 }
247 }
248 } elseif ($filter['column'] == 'referrer_type') {
249 $referrer_type = strtolower($row->referrer_type());
250 if ($filter['operator'] == 'is' && $referrer_type == $operand) {
251 $match = true;
252 } elseif ($filter['operator'] == 'isnt' && $referrer_type != $operand) {
253 $match = true;
254 }
255 } elseif ($filter['column'] == 'url') {
256 $url = strtolower($row->url());
257 if ($filter['operator'] == 'contains') {
258 if (strpos($url, $operand) !== false) {
259 $match = true;
260 }
261 } elseif ($filter['operator'] == 'exact') {
262 if ($url == $operand) {
263 $match = true;
264 }
265 }
266 } elseif ($filter['column'] == 'views') {
267 if ($filter['operator'] == 'greater') {
268 if ($row->views() > $operand) {
269 $match = true;
270 }
271 } elseif ($filter['operator'] == 'lesser') {
272 if ($row->views() < $operand) {
273 $match = true;
274 }
275 } elseif ($filter['operator'] == 'equal') {
276 if ($row->views() == $operand) {
277 $match = true;
278 }
279 }
280 } elseif ($filter['column'] == 'author') {
281 if ($filter['operator'] == 'is' && $row->author_id() == $operand) {
282 $match = true;
283 } elseif ($filter['operator'] == 'isnt' && $row->author_id() != $operand) {
284 $match = true;
285 }
286 } elseif ($filter['column'] == 'type') {
287 if ($filter['operator'] == 'is' && $row->type() == $operand) {
288 $match = true;
289 } elseif ($filter['operator'] == 'isnt' && $row->type() != $operand) {
290 $match = true;
291 }
292 } elseif ($filter['column'] == 'date') {
293 // Don't "continue" or row won't be included when excluding
294 if ($row->date() != null) {
295 $date = new \DateTime($operand);
296 if ($filter['operator'] == 'before' && $row->date() < $date->format('U')) {
297 $match = true;
298 } elseif ($filter['operator'] == 'after' && $row->date() > $date->format('U')) {
299 $match = true;
300 } elseif ($filter['operator'] == 'on') {
301 $formatted_date = \DateTime::createFromFormat('U', $row->date());
302 $formatted_date = $formatted_date->format('Y-m-d');
303 if ($date->format('Y-m-d') == $formatted_date) {
304 $match = true;
305 }
306 }
307 }
308 } elseif ($filter['column'] == 'category') {
309 if ($filter['operator'] == 'is' && in_array($operand, $row->category(false) ?? [])) {
310 $match = true;
311 } elseif ($filter['operator'] == 'isnt' && !in_array($operand, $row->category(false) ?? [])) {
312 $match = true;
313 }
314 }
315
316 if (
317 ($filter['inclusion'] == 'include' && $match) ||
318 ($filter['inclusion'] == 'exclude' && !$match)
319 ) {
320 return true;
321 } else {
322 return false;
323 }
324 }
325 }
326