Comment
1 day ago
Media
1 day ago
Post
1 day ago
User
1 day ago
ActionsFactory.php
1 day ago
CustomFieldFactory.php
1 day ago
IntegrationPlaceholder.php
1 day ago
OriginalFactory.php
1 day ago
CustomFieldFactory.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\ColumnFactory; |
| 6 | |
| 7 | use AC; |
| 8 | use AC\Column\BaseColumnFactory; |
| 9 | use AC\Column\Context; |
| 10 | use AC\Column\CustomFieldContext; |
| 11 | use AC\Formatter\Aggregate; |
| 12 | use AC\FormatterCollection; |
| 13 | use AC\Setting\ComponentCollection; |
| 14 | use AC\Setting\ComponentFactory; |
| 15 | use AC\Setting\ComponentFactory\ColumnInfo; |
| 16 | use AC\Setting\ComponentFactory\FieldType; |
| 17 | use AC\Setting\Config; |
| 18 | use AC\Setting\DefaultSettingsBuilder; |
| 19 | use AC\Type\TableScreenContext; |
| 20 | |
| 21 | class CustomFieldFactory extends BaseColumnFactory |
| 22 | { |
| 23 | private ComponentFactory\CustomFieldFactory $custom_field_factory; |
| 24 | |
| 25 | private FieldType $field_type; |
| 26 | |
| 27 | private TableScreenContext $table_context; |
| 28 | |
| 29 | private ComponentFactory\BeforeAfter $before_after; |
| 30 | |
| 31 | private ComponentFactory\Pro\TogglePromotionFactory $pro_promotion_factory; |
| 32 | |
| 33 | public function __construct( |
| 34 | DefaultSettingsBuilder $default_settings_builder, |
| 35 | ComponentFactory\CustomFieldFactory $custom_field_factory, |
| 36 | TableScreenContext $table_context, |
| 37 | FieldType $field_type, |
| 38 | ComponentFactory\BeforeAfter $before_after, |
| 39 | ComponentFactory\Pro\TogglePromotionFactory $pro_promotion_factory |
| 40 | ) { |
| 41 | parent::__construct( |
| 42 | $default_settings_builder |
| 43 | ); |
| 44 | |
| 45 | $this->custom_field_factory = $custom_field_factory; |
| 46 | $this->field_type = $field_type; |
| 47 | $this->table_context = $table_context; |
| 48 | $this->before_after = $before_after; |
| 49 | $this->pro_promotion_factory = $pro_promotion_factory; |
| 50 | } |
| 51 | |
| 52 | protected function get_settings(Config $config): ComponentCollection |
| 53 | { |
| 54 | $items = []; |
| 55 | |
| 56 | $meta_key = $config->get('field', ''); |
| 57 | |
| 58 | if ('' !== $meta_key) { |
| 59 | $items[] = ['label' => __('Meta Key', 'codepress-admin-columns'), 'value' => $meta_key]; |
| 60 | } |
| 61 | |
| 62 | $components = [ |
| 63 | $this->custom_field_factory->create($this->table_context)->create($config), |
| 64 | $this->field_type->create($config), |
| 65 | ]; |
| 66 | |
| 67 | if (! empty($items)) { |
| 68 | $components[] = (new ColumnInfo($items))->create($config); |
| 69 | } |
| 70 | |
| 71 | $components[] = $this->before_after->create($config); |
| 72 | $components[] = $this->pro_promotion_factory->create(__('Enable Editing', 'codepress-admin-columns'), 'editing')->create($config); |
| 73 | $components[] = $this->pro_promotion_factory->create(__('Enable Bulk Editing', 'codepress-admin-columns'), 'bulk-edit')->create($config); |
| 74 | $components[] = $this->pro_promotion_factory->create(__('Enable Export', 'codepress-admin-columns'), 'export')->create($config); |
| 75 | $components[] = $this->pro_promotion_factory->create(__('Enable Smart Filtering', 'codepress-admin-columns'), 'search')->create($config); |
| 76 | $components[] = $this->pro_promotion_factory->create(__('Enable Filtering', 'codepress-admin-columns'), 'filter')->create($config); |
| 77 | $components[] = $this->pro_promotion_factory->create(__('Enable Sorting', 'codepress-admin-columns'), 'sorting')->create($config); |
| 78 | |
| 79 | return new ComponentCollection($components); |
| 80 | } |
| 81 | |
| 82 | public function get_column_type(): string |
| 83 | { |
| 84 | return 'column-meta'; |
| 85 | } |
| 86 | |
| 87 | public function get_label(): string |
| 88 | { |
| 89 | return __('Custom Field', 'codepress-admin-columns'); |
| 90 | } |
| 91 | |
| 92 | private function get_meta_key(Config $config): string |
| 93 | { |
| 94 | return (string)$config->get('field', ''); |
| 95 | } |
| 96 | |
| 97 | private function get_field_type(Config $config): string |
| 98 | { |
| 99 | return (string)$config->get('field_type', ''); |
| 100 | } |
| 101 | |
| 102 | protected function get_context(Config $config): Context |
| 103 | { |
| 104 | return new CustomFieldContext( |
| 105 | $config, |
| 106 | $this->get_label(), |
| 107 | $this->get_field_type($config), |
| 108 | $this->get_meta_key($config), |
| 109 | $this->table_context |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | protected function get_formatters(Config $config): FormatterCollection |
| 114 | { |
| 115 | $formatters = parent::get_formatters($config); |
| 116 | |
| 117 | switch ($this->get_field_type($config)) { |
| 118 | case FieldType::TYPE_COUNT: |
| 119 | return $formatters->prepend( |
| 120 | Aggregate::from_array([ |
| 121 | new AC\Formatter\MetaValueCollection( |
| 122 | $this->table_context->get_meta_type(), |
| 123 | $this->get_meta_key($config) |
| 124 | ), |
| 125 | new AC\Formatter\Count(), |
| 126 | ]) |
| 127 | ); |
| 128 | case FieldType::TYPE_META_COUNT: |
| 129 | return $formatters->prepend( |
| 130 | Aggregate::from_array([ |
| 131 | new AC\Formatter\MetaCollection( |
| 132 | $this->table_context->get_meta_type(), |
| 133 | $this->get_meta_key($config) |
| 134 | ), |
| 135 | new AC\Formatter\Count(), |
| 136 | ]) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | return $formatters->prepend( |
| 141 | new AC\Formatter\Meta($this->table_context->get_meta_type(), $this->get_meta_key($config)) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | } |
| 146 |