AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
Filters.php
302 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Tables\Columns\Column; |
| 6 | use IAWP_SCOPED\IAWP\Utils\WordPress_Site_Date_Format_Pattern; |
| 7 | /** @internal */ |
| 8 | class Filters |
| 9 | { |
| 10 | public function get_filters_html(array $columns) : string |
| 11 | { |
| 12 | $opts = new Dashboard_Options(); |
| 13 | \ob_start(); |
| 14 | ?> |
| 15 | <div class="modal-parent filters" |
| 16 | data-controller="filters" |
| 17 | data-filters-filters-value="<?php |
| 18 | \esc_html_e(\json_encode($opts->filters())); |
| 19 | ?>" |
| 20 | > |
| 21 | <span class="dashicons dashicons-filter"></span> |
| 22 | <div data-filters-target="conditionButtons" class="filter-condition-buttons"> |
| 23 | <?php |
| 24 | echo $this->condition_buttons_html($opts->filters()); |
| 25 | ?> |
| 26 | </div> |
| 27 | <button id="filters-button" class="filters-button" |
| 28 | data-action="filters#toggleModal" |
| 29 | data-filters-target="modalButton" |
| 30 | > |
| 31 | <span class="iawp-label"><?php |
| 32 | \esc_html_e('+ Add Filter', 'independent-analytics'); |
| 33 | ?></span> |
| 34 | </button> |
| 35 | <div id="modal-filters" |
| 36 | class="modal large" |
| 37 | data-filters-target="modal" |
| 38 | > |
| 39 | <div class="modal-inner"> |
| 40 | <div class="title-small"><?php |
| 41 | \esc_html_e('Filters', 'independent-analytics'); |
| 42 | ?></div> |
| 43 | <div id="filters" data-filters-target="filters" class="filters" data-filters="[]"> |
| 44 | </div> |
| 45 | <template data-filters-target="blueprint"> |
| 46 | <?php |
| 47 | echo $this->get_condition_html($columns); |
| 48 | ?> |
| 49 | </template> |
| 50 | <div> |
| 51 | <button id="add-condition" class="iawp-text-button" |
| 52 | data-action="filters#addCondition" |
| 53 | > |
| 54 | <?php |
| 55 | \esc_html_e('+ Add another condition', 'independent-analytics'); |
| 56 | ?> |
| 57 | </button> |
| 58 | </div> |
| 59 | <div class="actions"> |
| 60 | <button id="filters-apply" class="iawp-button purple" |
| 61 | data-action="filters#apply" |
| 62 | > |
| 63 | <?php |
| 64 | \esc_html_e('Apply', 'independent-analytics'); |
| 65 | ?> |
| 66 | </button> |
| 67 | <button id="filters-reset" class="iawp-button ghost-purple" |
| 68 | data-action="filters#reset" |
| 69 | data-filters-target="reset" |
| 70 | disabled |
| 71 | > |
| 72 | <?php |
| 73 | \esc_html_e('Reset', 'independent-analytics'); |
| 74 | ?> |
| 75 | </button> |
| 76 | </div> |
| 77 | </div> |
| 78 | </div> |
| 79 | </div><?php |
| 80 | $html = \ob_get_contents(); |
| 81 | \ob_end_clean(); |
| 82 | return $html; |
| 83 | } |
| 84 | public function get_condition_html(array $columns) |
| 85 | { |
| 86 | \ob_start(); |
| 87 | ?> |
| 88 | <div class="condition" data-filters-target="condition"> |
| 89 | <div class="input-group"> |
| 90 | <div> |
| 91 | <?php |
| 92 | echo self::get_inclusion_selects(); |
| 93 | ?> |
| 94 | </div> |
| 95 | <div> |
| 96 | <?php |
| 97 | self::get_column_select($columns); |
| 98 | ?> |
| 99 | </div> |
| 100 | <div class="operator-select-container"> |
| 101 | <?php |
| 102 | echo self::get_all_operator_selects(); |
| 103 | ?> |
| 104 | </div> |
| 105 | <div class="operand-field-container"> |
| 106 | <?php |
| 107 | echo self::get_all_operand_fields($columns); |
| 108 | ?> |
| 109 | </div> |
| 110 | </div> |
| 111 | <button class="delete-button" data-action="filters#removeCondition"> |
| 112 | <span class="dashicons dashicons-no"></span></button> |
| 113 | </div> |
| 114 | <?php |
| 115 | $html = \ob_get_contents(); |
| 116 | \ob_end_clean(); |
| 117 | return $html; |
| 118 | } |
| 119 | public function condition_buttons_html(array $filters) : string |
| 120 | { |
| 121 | \ob_start(); |
| 122 | foreach ($filters as $filter) { |
| 123 | ?> |
| 124 | <button class="filters-condition-button" |
| 125 | data-action="filters#toggleModal" |
| 126 | data-filters-target="modalButton"> |
| 127 | <?php |
| 128 | echo \wp_kses_post($this->condition_string($filter)); |
| 129 | ?> |
| 130 | </button> |
| 131 | <?php |
| 132 | } |
| 133 | $html = \ob_get_contents(); |
| 134 | \ob_end_clean(); |
| 135 | return $html; |
| 136 | } |
| 137 | public function condition_string(array $condition) : string |
| 138 | { |
| 139 | $full_string = ''; |
| 140 | foreach ($condition as $key => $value) { |
| 141 | if (!\in_array($key, ['inclusion', 'column', 'operator', 'operand'])) { |
| 142 | continue; |
| 143 | } |
| 144 | $condition_string = ''; |
| 145 | if ($key == 'inclusion' && $value == 'include') { |
| 146 | $condition_string = \esc_html__('Include', 'independent-analytics'); |
| 147 | } elseif ($key == 'inclusion' && $value == 'exclude') { |
| 148 | $condition_string = \esc_html__('Exclude', 'independent-analytics'); |
| 149 | } elseif ($key == 'operator' && $value == 'lesser') { |
| 150 | $condition_string = '<'; |
| 151 | } elseif ($key == 'operator' && $value == 'greater') { |
| 152 | $condition_string = '>'; |
| 153 | } elseif ($key == 'operator' && $value == 'equal') { |
| 154 | $condition_string = '='; |
| 155 | } elseif ($key == 'operator' && $value == 'on') { |
| 156 | $condition_string = \esc_html__('On', 'independent-analytics'); |
| 157 | } elseif ($key == 'operator' && $value == 'before') { |
| 158 | $condition_string = \esc_html__('Before', 'independent-analytics'); |
| 159 | } elseif ($key == 'operator' && $value == 'after') { |
| 160 | $condition_string = \esc_html__('After', 'independent-analytics'); |
| 161 | } elseif ($key == 'operator' && $value == 'contains') { |
| 162 | $condition_string = \esc_html__('Contains', 'independent-analytics'); |
| 163 | } elseif ($key == 'operator' && $value == 'exact') { |
| 164 | $condition_string = \esc_html__('Exactly matches', 'independent-analytics'); |
| 165 | } elseif ($key == 'operator' && $value == 'is') { |
| 166 | $condition_string = \esc_html__('Is', 'independent-analytics'); |
| 167 | } elseif ($key == 'operator' && $value == 'isnt') { |
| 168 | $condition_string = \esc_html__("Isn't", 'independent-analytics'); |
| 169 | } elseif ($key == 'operand' && $condition['column'] == 'date') { |
| 170 | try { |
| 171 | $date = \DateTime::createFromFormat('U', $value); |
| 172 | $condition_string = $date->format(WordPress_Site_Date_Format_Pattern::for_php()); |
| 173 | } catch (\Throwable $e) { |
| 174 | $condition_string = $value; |
| 175 | } |
| 176 | } elseif ($key == 'operand' && $condition['column'] == 'author') { |
| 177 | $condition_string = \get_the_author_meta('display_name', $value); |
| 178 | } elseif ($key == 'operand' && $condition['column'] == 'category') { |
| 179 | $condition_string = \get_cat_name($value); |
| 180 | } else { |
| 181 | $condition_string .= \ucwords(\str_replace(['_', '-'], ' ', $value)) . ' '; |
| 182 | } |
| 183 | if ($key == 'column' || $key == 'operand') { |
| 184 | $condition_string = '<strong>' . $condition_string . '</strong> '; |
| 185 | } |
| 186 | $full_string .= $condition_string; |
| 187 | } |
| 188 | return \trim($full_string); |
| 189 | } |
| 190 | private function get_inclusion_selects() |
| 191 | { |
| 192 | $html = '<select class="filters-include" data-filters-target="inclusion">'; |
| 193 | $html .= '<option value="include">' . \esc_html__('Include', 'independent-analytics') . '</option>'; |
| 194 | $html .= '<option value="exclude">' . \esc_html__('Exclude', 'independent-analytics') . '</option>'; |
| 195 | $html .= '</select>'; |
| 196 | return $html; |
| 197 | } |
| 198 | /** |
| 199 | * @param Column[] $columns |
| 200 | * |
| 201 | * @return void |
| 202 | */ |
| 203 | private function get_column_select(array $columns) |
| 204 | { |
| 205 | ?> |
| 206 | <select class="filters-column" data-filters-target="column" |
| 207 | data-action="filters#columnSelect" |
| 208 | > |
| 209 | <option value=""> |
| 210 | <?php |
| 211 | \esc_html_e('Choose a column', 'independent-analytics'); |
| 212 | ?> |
| 213 | </option> |
| 214 | <?php |
| 215 | foreach ($columns as $column) { |
| 216 | ?> |
| 217 | <?php |
| 218 | if ($column->requires_woocommerce() && !\IAWP_SCOPED\iawp_using_woocommerce()) { |
| 219 | continue; |
| 220 | } |
| 221 | ?> |
| 222 | <option value="<?php |
| 223 | echo \esc_attr($column->id()); |
| 224 | ?>" |
| 225 | data-type="<?php |
| 226 | echo \esc_attr($column->type()); |
| 227 | ?>" |
| 228 | > |
| 229 | <?php |
| 230 | echo \esc_html($column->label()); |
| 231 | ?> |
| 232 | </option> |
| 233 | <?php |
| 234 | } |
| 235 | ?> |
| 236 | </select> |
| 237 | <?php |
| 238 | } |
| 239 | private function get_all_operator_selects() |
| 240 | { |
| 241 | $html = ''; |
| 242 | foreach (self::get_data_types() as $data_type) { |
| 243 | $html .= '<select data-filters-target="operator" data-type="' . \esc_attr($data_type) . '" data-testid="' . \esc_attr($data_type) . '-operator">'; |
| 244 | foreach (self::get_operators($data_type) as $key => $value) { |
| 245 | $html .= '<option value="' . \esc_attr($key) . '">' . \esc_html($value) . '</option>'; |
| 246 | } |
| 247 | $html .= '</select>'; |
| 248 | } |
| 249 | return $html; |
| 250 | } |
| 251 | private function get_all_operand_fields(array $columns) |
| 252 | { |
| 253 | $html = ''; |
| 254 | foreach ($columns as $column) { |
| 255 | switch ($column->type()) { |
| 256 | case 'string': |
| 257 | $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="' . \esc_attr($column->id()) . '" type="text" data-testid="' . \esc_attr($column->id()) . '-operand" placeholder="' . \esc_attr($column->filter_placeholder()) . '" />'; |
| 258 | break; |
| 259 | case 'int': |
| 260 | $html .= '<input data-filters-target="operand" data-action="keydown->filters#operandKeyDown filters#operandChange" data-column="' . \esc_attr($column->id()) . '" type="number" data-testid="' . \esc_attr($column->id()) . '-operand" placeholder="' . \esc_attr($column->filter_placeholder()) . '" />'; |
| 261 | break; |
| 262 | case 'date': |
| 263 | $html .= '<input type="text" |
| 264 | data-filters-target="operand" |
| 265 | data-action="keydown->filters#operandKeyDown filters#operandChange" |
| 266 | data-column="' . \esc_attr($column->id()) . '" |
| 267 | data-controller="easepick" |
| 268 | data-css="' . \esc_url(\IAWP_SCOPED\iawp_url_to('dist/styles/easepick/datepicker.css')) . '" data-dow="' . \absint(\IAWP_SCOPED\iawp()->get_option('iawp_dow', 1)) . '" |
| 269 | data-format="' . \esc_attr(WordPress_Site_Date_Format_Pattern::for_javascript()) . '" |
| 270 | data-testid="' . \esc_attr($column->id()) . '-operand" />'; |
| 271 | break; |
| 272 | case 'select': |
| 273 | $html .= '<select data-filters-target="operand" data-column="' . \esc_attr($column->id()) . '" data-testid="' . \esc_attr($column->id()) . '-operand">'; |
| 274 | foreach ($column->options() as $option) { |
| 275 | $html .= '<option value="' . \esc_attr($option[0]) . '">' . \esc_html($option[1]) . '</option>'; |
| 276 | } |
| 277 | $html .= '</select>'; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | return $html; |
| 282 | } |
| 283 | private function get_data_types() |
| 284 | { |
| 285 | return ['string', 'int', 'date', 'select']; |
| 286 | } |
| 287 | private function get_operators(string $data_type) |
| 288 | { |
| 289 | if ($data_type == 'string') { |
| 290 | return ['contains' => \esc_html__('Contains', 'independent-analytics'), 'exact' => \esc_html__('Exactly matches', 'independent-analytics')]; |
| 291 | } elseif ($data_type == 'int') { |
| 292 | return ['greater' => \esc_html__('Greater than', 'independent-analytics'), 'lesser' => \esc_html__('Less than', 'independent-analytics'), 'equal' => \esc_html__('Equal to', 'independent-analytics')]; |
| 293 | } elseif ($data_type == 'select') { |
| 294 | return ['is' => \esc_html__('Is', 'independent-analytics'), 'isnt' => \esc_html__('Isn\'t', 'independent-analytics')]; |
| 295 | } elseif ($data_type == 'date') { |
| 296 | return ['before' => \esc_html__('Before', 'independent-analytics'), 'after' => \esc_html__('After', 'independent-analytics'), 'on' => \esc_html__('On', 'independent-analytics')]; |
| 297 | } else { |
| 298 | return null; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 |