PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.7.2
Independent Analytics – WordPress Analytics Plugin v2.7.2
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 / IAWP / Rows / Filter.php
independent-analytics / IAWP / Rows Last commit date
Campaigns.php 2 years ago Cities.php 2 years ago Countries.php 2 years ago Device_Browsers.php 2 years ago Device_OSS.php 2 years ago Device_Types.php 2 years ago Filter.php 2 years ago Pages.php 2 years ago Referrers.php 2 years ago Rows.php 2 years ago
Filter.php
204 lines
1 <?php
2
3 namespace IAWP\Rows;
4
5 use IAWP\Filter_Lists\Author_Filter_List;
6 use IAWP\Filter_Lists\Category_Filter_List;
7 use IAWP\Filter_Lists\Device_Browser_Filter_List;
8 use IAWP\Filter_Lists\Device_OS_Filter_List;
9 use IAWP\Filter_Lists\Device_Type_Filter_List;
10 use IAWP\Filter_Lists\Page_Type_Filter_List;
11 use IAWP\Filter_Lists\Referrer_Type_Filter_List;
12 use IAWP\Form_Submissions\Form;
13 use IAWP\Utils\String_Util;
14 use IAWP\Utils\WordPress_Site_Date_Format_Pattern;
15 use IAWPSCOPED\Illuminate\Database\Query\Builder;
16 use JsonSerializable;
17 /** @internal */
18 class Filter implements JsonSerializable
19 {
20 private $filter;
21 public function __construct(array $filter)
22 {
23 $this->filter = $filter;
24 }
25 public function filter() : array
26 {
27 return $this->filter;
28 }
29 public function apply_to_query(Builder $query) : void
30 {
31 if ($this->filter['column'] === 'category') {
32 $this->apply_category_filter($query);
33 } else {
34 $method = $this->method();
35 $query->{$method}($this->column(), $this->operator(), $this->value());
36 }
37 // If a filter is based on comments, remove all non-singular pages
38 if ($this->filter['column'] === 'comments') {
39 $query->whereNotNull('pages.singular_id');
40 }
41 }
42 public function html_description() : string
43 {
44 return $this->condition_string($this->filter());
45 }
46 public function method() : string
47 {
48 if ($this->filter['column'] === $this->filter['database_column']) {
49 return 'having';
50 } else {
51 return 'where';
52 }
53 }
54 public function column() : string
55 {
56 return $this->filter['database_column'];
57 }
58 // Fix deprecation warning in PHP 8 while still working in PHP 7
59 #[\ReturnTypeWillChange]
60 public function jsonSerialize()
61 {
62 return $this->filter;
63 }
64 /**
65 * Category filters are different from the other filters
66 *
67 * @param Builder $query
68 * @return void
69 */
70 private function apply_category_filter(Builder $query) : void
71 {
72 $wp_query = new \WP_Query(['posts_per_page' => -1, 'cat' => $this->filter['operand'], 'fields' => 'ids']);
73 $post_ids = $wp_query->posts;
74 $include = $this->filter['inclusion'] === 'include';
75 $is = $this->filter['operator'] === 'is';
76 if ($include === $is) {
77 $query->whereIn('singular_id', $post_ids);
78 } else {
79 $query->where(function (Builder $query) use($post_ids) {
80 $query->whereNotIn('singular_id', $post_ids)->orWhereNull('singular_id');
81 });
82 }
83 }
84 private function condition_string(array $condition) : string
85 {
86 $full_string = '';
87 foreach ($condition as $key => $value) {
88 if (!\in_array($key, ['inclusion', 'column', 'operator', 'operand'])) {
89 continue;
90 }
91 $condition_string = '';
92 if ($key == 'inclusion' && $value == 'include') {
93 $condition_string = \esc_html__('Include', 'independent-analytics');
94 } elseif ($key == 'inclusion' && $value == 'exclude') {
95 $condition_string = \esc_html__('Exclude', 'independent-analytics');
96 } elseif ($key == 'operator' && $value == 'lesser') {
97 $condition_string = '<';
98 } elseif ($key == 'operator' && $value == 'greater') {
99 $condition_string = '>';
100 } elseif ($key == 'operator' && $value == 'equal') {
101 $condition_string = '=';
102 } elseif ($key == 'operator' && $value == 'on') {
103 $condition_string = \esc_html__('On', 'independent-analytics');
104 } elseif ($key == 'operator' && $value == 'before') {
105 $condition_string = \esc_html__('Before', 'independent-analytics');
106 } elseif ($key == 'operator' && $value == 'after') {
107 $condition_string = \esc_html__('After', 'independent-analytics');
108 } elseif ($key == 'operator' && $value == 'contains') {
109 $condition_string = \esc_html__('Contains', 'independent-analytics');
110 } elseif ($key == 'operator' && $value == 'exact') {
111 $condition_string = \esc_html__('Exactly matches', 'independent-analytics');
112 } elseif ($key == 'operator' && $value == 'is') {
113 $condition_string = \esc_html__('Is', 'independent-analytics');
114 } elseif ($key == 'operator' && $value == 'isnt') {
115 $condition_string = \esc_html__("Isn't", 'independent-analytics');
116 } elseif ($key == 'operand' && $condition['column'] == 'browser') {
117 $condition_string = Device_Browser_Filter_List::option($value);
118 } elseif ($key == 'operand' && $condition['column'] == 'os') {
119 $condition_string = Device_OS_Filter_List::option($value);
120 } elseif ($key == 'operand' && $condition['column'] == 'device_type') {
121 $condition_string = Device_Type_Filter_List::option($value);
122 } elseif ($key == 'operand' && $condition['column'] == 'date') {
123 try {
124 $date = \DateTime::createFromFormat('U', $value);
125 $condition_string = $date->format(WordPress_Site_Date_Format_Pattern::for_php());
126 } catch (\Throwable $e) {
127 $condition_string = $value;
128 }
129 } elseif ($key == 'operand' && $condition['column'] == 'author') {
130 $condition_string = Author_Filter_List::option($value);
131 } elseif ($key == 'operand' && $condition['column'] == 'category') {
132 $condition_string = Category_Filter_List::option($value);
133 } elseif ($key == 'operand' && $condition['column'] == 'referrer_type') {
134 $condition_string = Referrer_Type_Filter_List::option($value);
135 } elseif ($key == 'operand' && $condition['column'] == 'type') {
136 $condition_string = Page_Type_Filter_List::option($value);
137 } elseif ($key == 'column' && \str_contains($value, 'wc')) {
138 $condition_string = \str_replace(['_', '-'], ' ', $value);
139 $condition_string = \str_replace('wc', 'WC', $condition_string);
140 } elseif ($key == 'column' && String_Util::str_starts_with($condition['column'], 'form_submissions_for_')) {
141 $condition_string = \__('Submissions for', 'independent-analytics') . ' ' . Form::find_form_by_column_name($condition['column'])->title();
142 } elseif ($key == 'column' && String_Util::str_starts_with($condition['column'], 'form_conversion_rate_for_')) {
143 $condition_string = \__('Conversion rate for', 'independent-analytics') . ' ' . Form::find_form_by_column_name($condition['column'])->title();
144 } else {
145 $condition_string .= \ucwords(\str_replace(['_', '-'], ' ', $value)) . ' ';
146 }
147 if ($key == 'column' || $key == 'operand') {
148 $condition_string = '<strong>' . $condition_string . '</strong> ';
149 }
150 $full_string .= $condition_string . ' ';
151 }
152 return \trim($full_string);
153 }
154 private function operator() : string
155 {
156 $operator = $this->filter['operator'];
157 $result = '';
158 if ($operator === 'equal' || $operator === 'is' || $operator === 'exact' || $operator === 'on') {
159 $result = '=';
160 }
161 if ($operator === 'contains') {
162 $result = 'like';
163 }
164 if ($operator === 'isnt') {
165 $result = '!=';
166 }
167 if ($operator === 'greater' || $operator === 'after') {
168 $result = '>';
169 }
170 if ($operator === 'lesser' || $operator === 'before') {
171 $result = '<';
172 }
173 if ($this->filter['inclusion'] === 'exclude') {
174 if ($result === '=') {
175 return '!=';
176 } elseif ($result === '!=') {
177 return '=';
178 } elseif ($result === '>') {
179 return '<=';
180 } elseif ($result === '<') {
181 return '>=';
182 } elseif ($result === 'like') {
183 return 'not like';
184 }
185 }
186 return $result;
187 }
188 private function value() : string
189 {
190 if ($this->filter['operator'] === 'contains') {
191 return '%' . $this->filter['operand'] . '%';
192 }
193 if ($this->filter['database_column'] === 'cached_date') {
194 try {
195 $date = \DateTime::createFromFormat('U', $this->filter['operand']);
196 } catch (\Throwable $e) {
197 $date = new \DateTime();
198 }
199 return $date->format('Y-m-d');
200 }
201 return $this->filter['operand'];
202 }
203 }
204