Column.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Tables\Columns; |
| 4 | |
| 5 | use IAWP\Plugin_Group_Option; |
| 6 | use IAWP\Tables\Groups\Group; |
| 7 | /** @internal */ |
| 8 | class Column implements Plugin_Group_Option |
| 9 | { |
| 10 | private $id; |
| 11 | private $name; |
| 12 | private $plugin_group; |
| 13 | private $plugin_group_header; |
| 14 | private $visible; |
| 15 | private $type; |
| 16 | private $exportable; |
| 17 | private $options; |
| 18 | private $filter_placeholder; |
| 19 | private $unavailable_for; |
| 20 | private $database_column; |
| 21 | private $is_nullable; |
| 22 | private $is_plugin_active; |
| 23 | private $requires_pro; |
| 24 | private $aggregatable; |
| 25 | public function __construct($attributes) |
| 26 | { |
| 27 | $this->id = $attributes['id']; |
| 28 | $this->name = $attributes['name']; |
| 29 | $this->plugin_group = $attributes['plugin_group'] ?? 'general'; |
| 30 | $this->plugin_group_header = $attributes['plugin_group_header'] ?? null; |
| 31 | $this->visible = $attributes['visible'] ?? \false; |
| 32 | $this->type = $attributes['type']; |
| 33 | $this->exportable = $attributes['exportable'] ?? \true; |
| 34 | $this->options = $attributes['options'] ?? []; |
| 35 | $this->filter_placeholder = $attributes['filter_placeholder'] ?? ''; |
| 36 | $this->unavailable_for = $attributes['unavailable_for'] ?? []; |
| 37 | $this->database_column = $attributes['database_column'] ?? null; |
| 38 | $this->is_nullable = $attributes['is_nullable'] ?? \false; |
| 39 | $this->is_plugin_active = $attributes['is_subgroup_plugin_active'] ?? \true; |
| 40 | $this->requires_pro = $attributes['requires_pro'] ?? \false; |
| 41 | $this->aggregatable = $attributes['aggregatable'] ?? \false; |
| 42 | } |
| 43 | public function is_enabled() : bool |
| 44 | { |
| 45 | if ($this->requires_pro === \true && \IAWPSCOPED\iawp_is_free()) { |
| 46 | return \false; |
| 47 | } |
| 48 | return \true; |
| 49 | } |
| 50 | public function aggregatable() : bool |
| 51 | { |
| 52 | return $this->aggregatable; |
| 53 | } |
| 54 | public function is_enabled_for_group(Group $group) : bool |
| 55 | { |
| 56 | return !\in_array($group->id(), $this->unavailable_for); |
| 57 | } |
| 58 | public function is_group_dependent() : bool |
| 59 | { |
| 60 | return \count($this->unavailable_for) > 0; |
| 61 | } |
| 62 | public function id() : string |
| 63 | { |
| 64 | return $this->id; |
| 65 | } |
| 66 | public function name() : string |
| 67 | { |
| 68 | return $this->name; |
| 69 | } |
| 70 | public function plugin_group() : string |
| 71 | { |
| 72 | return $this->plugin_group; |
| 73 | } |
| 74 | public function database_column() : string |
| 75 | { |
| 76 | return !\is_null($this->database_column) ? $this->database_column : $this->id; |
| 77 | } |
| 78 | public function is_group_plugin_enabled() : bool |
| 79 | { |
| 80 | switch ($this->plugin_group) { |
| 81 | case "ecommerce": |
| 82 | return \IAWPSCOPED\iawp()->is_ecommerce_support_enabled(); |
| 83 | case "forms": |
| 84 | return \IAWPSCOPED\iawp()->is_form_submission_support_enabled(); |
| 85 | default: |
| 86 | return \true; |
| 87 | } |
| 88 | } |
| 89 | public function is_subgroup_plugin_enabled() : bool |
| 90 | { |
| 91 | return $this->is_plugin_active; |
| 92 | } |
| 93 | public function is_visible() : bool |
| 94 | { |
| 95 | return $this->visible; |
| 96 | } |
| 97 | public function is_member_of_plugin_group(string $plugin_group) : bool |
| 98 | { |
| 99 | return $this->plugin_group === $plugin_group; |
| 100 | } |
| 101 | public function plugin_group_header() : ?string |
| 102 | { |
| 103 | return $this->plugin_group_header; |
| 104 | } |
| 105 | public function type() : string |
| 106 | { |
| 107 | return $this->type; |
| 108 | } |
| 109 | /** |
| 110 | * @return string[] |
| 111 | */ |
| 112 | public function filter_operators() : array |
| 113 | { |
| 114 | switch ($this->type) { |
| 115 | case 'string': |
| 116 | return ['contains', 'exact']; |
| 117 | case 'date': |
| 118 | return ['before', 'after', 'on']; |
| 119 | case 'select': |
| 120 | return ['is', 'isnt']; |
| 121 | default: |
| 122 | // int |
| 123 | return ['greater', 'lesser', 'equal']; |
| 124 | } |
| 125 | } |
| 126 | public function is_valid_filter_operator(string $operator) : bool |
| 127 | { |
| 128 | return \in_array($operator, $this->filter_operators()); |
| 129 | } |
| 130 | public function sort_direction() : string |
| 131 | { |
| 132 | $descending_types = ['int', 'date']; |
| 133 | return \in_array($this->type, $descending_types) ? 'desc' : 'asc'; |
| 134 | } |
| 135 | public function set_visibility(bool $visible) : void |
| 136 | { |
| 137 | $this->visible = $visible; |
| 138 | } |
| 139 | public function exportable() : bool |
| 140 | { |
| 141 | return $this->exportable; |
| 142 | } |
| 143 | /** |
| 144 | * @return array List of possible options for this filter such as a list of authors or list of post categories |
| 145 | */ |
| 146 | public function options() : array |
| 147 | { |
| 148 | return $this->options; |
| 149 | } |
| 150 | public function filter_placeholder() : string |
| 151 | { |
| 152 | return $this->filter_placeholder; |
| 153 | } |
| 154 | public function is_nullable() : bool |
| 155 | { |
| 156 | return $this->is_nullable; |
| 157 | } |
| 158 | } |
| 159 |