FeaturedImageDisplay.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Setting\ComponentFactory\Post; |
| 6 | |
| 7 | use AC\Expression\StringComparisonSpecification; |
| 8 | use AC\Formatter\FileSizeReadable; |
| 9 | use AC\Formatter\Media\Dimensions; |
| 10 | use AC\Formatter\Media\FileSize; |
| 11 | use AC\FormatterCollection; |
| 12 | use AC\Setting\Children; |
| 13 | use AC\Setting\ComponentCollection; |
| 14 | use AC\Setting\ComponentFactory\BaseComponentFactory; |
| 15 | use AC\Setting\ComponentFactory\ImageSize; |
| 16 | use AC\Setting\Config; |
| 17 | use AC\Setting\Control\Input; |
| 18 | use AC\Setting\Control\Input\OptionFactory; |
| 19 | use AC\Setting\Control\OptionCollection; |
| 20 | |
| 21 | class FeaturedImageDisplay extends BaseComponentFactory |
| 22 | { |
| 23 | public const NAME = 'featured_image_display'; |
| 24 | |
| 25 | private ImageSize $image_size; |
| 26 | |
| 27 | public function __construct(ImageSize $image_size) |
| 28 | { |
| 29 | $this->image_size = $image_size; |
| 30 | } |
| 31 | |
| 32 | protected function get_label(Config $config): string |
| 33 | { |
| 34 | return __('Display', 'codepress-admin-columns'); |
| 35 | } |
| 36 | |
| 37 | protected function get_input(Config $config): Input |
| 38 | { |
| 39 | return OptionFactory::create_select( |
| 40 | self::NAME, |
| 41 | OptionCollection::from_array([ |
| 42 | 'image' => __('Image', 'codepress-admin-columns'), |
| 43 | 'filesize' => __('Filesize', 'codepress-admin-columns'), |
| 44 | 'dimensions' => __('Dimensions', 'codepress-admin-columns'), |
| 45 | ]), |
| 46 | $config->get(self::NAME, 'image') |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | protected function get_children(Config $config): ?Children |
| 51 | { |
| 52 | return new Children( |
| 53 | new ComponentCollection([ |
| 54 | $this->image_size->create( |
| 55 | $config, |
| 56 | StringComparisonSpecification::equal('image') |
| 57 | ), |
| 58 | ]) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | protected function add_formatters(Config $config, FormatterCollection $formatters): void |
| 63 | { |
| 64 | if ('filesize' === $this->get_input($config)->get_value()) { |
| 65 | $formatters->add(new FileSize()); |
| 66 | $formatters->add(new FileSizeReadable()); |
| 67 | } |
| 68 | |
| 69 | if ('dimensions' === $this->get_input($config)->get_value()) { |
| 70 | $formatters->add(new Dimensions()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } |
| 75 |