Campaigns.php
1 year ago
Cities.php
1 year ago
Countries.php
1 year ago
Device_Browsers.php
1 year ago
Device_OSS.php
1 year ago
Device_Types.php
1 year ago
Filter.php
1 year ago
Pages.php
1 year ago
Referrers.php
1 year ago
Rows.php
1 year ago
Filter.php
207 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 | if (\in_array($this->filter['database_column'], ['wc_gross_sales', 'wc_refunded_amount', 'wc_net_sales', 'wc_earnings_per_visitor', 'wc_average_order_volume'])) { |
| 202 | return \strval(\floatval($this->filter['operand']) * 100); |
| 203 | } |
| 204 | return $this->filter['operand']; |
| 205 | } |
| 206 | } |
| 207 |