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