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