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