DateFormat
2 days ago
FieldTypeConfigurator
2 days ago
Media
2 days ago
Post
2 days ago
Pro
2 days ago
ActionIcons.php
2 days ago
AttachmentDisplay.php
2 days ago
BaseComponentFactory.php
2 days ago
BeforeAfter.php
2 days ago
BooleanValues.php
2 days ago
CharacterLimit.php
2 days ago
ColumnInfo.php
2 days ago
CommentDisplay.php
2 days ago
CommentLink.php
2 days ago
CommentStatus.php
2 days ago
CustomField.php
2 days ago
CustomFieldFactory.php
2 days ago
DateFormat.php
2 days ago
DateSaveFormat.php
2 days ago
ExifData.php
2 days ago
FieldType.php
2 days ago
FieldTypeFactory.php
2 days ago
FieldTypeFactoryBuilder.php
2 days ago
FileDisplay.php
2 days ago
FileLink.php
2 days ago
FilePreviewSize.php
2 days ago
HiddenInput.php
2 days ago
HiddenLabel.php
2 days ago
ImageSize.php
2 days ago
ImageSizeBase.php
2 days ago
IncludeMissingSizes.php
2 days ago
InputNameAware.php
2 days ago
IsLink.php
2 days ago
IsLinkable.php
2 days ago
IsMultiple.php
2 days ago
Label.php
2 days ago
LinkLabel.php
2 days ago
LinkToMenu.php
2 days ago
LinkablePostProperty.php
2 days ago
MediaLink.php
2 days ago
Message.php
2 days ago
ModalDisplay.php
2 days ago
Name.php
2 days ago
NumberFormat.php
2 days ago
NumberOfItems.php
2 days ago
Password.php
2 days ago
PathScope.php
2 days ago
PostExtendedProperty.php
2 days ago
PostLink.php
2 days ago
PostProperty.php
2 days ago
PostStatus.php
2 days ago
PostStatusIcon.php
2 days ago
PostType.php
2 days ago
PostTypeFactory.php
2 days ago
RelatedUserMetaField.php
2 days ago
SelectOptions.php
2 days ago
Separator.php
2 days ago
SerializedArrayKeys.php
2 days ago
SerializedDisplay.php
2 days ago
StringLimit.php
2 days ago
Taxonomy.php
2 days ago
TaxonomyFactory.php
2 days ago
TermLink.php
2 days ago
TermProperty.php
2 days ago
UseIcon.php
2 days ago
UserLink.php
2 days ago
UserLinkFactory.php
2 days ago
UserProperty.php
2 days ago
VideoDisplay.php
2 days ago
Width.php
2 days ago
WordLimit.php
2 days ago
WordsPerMinute.php
2 days ago
ImageSizeBase.php
178 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Setting\ComponentFactory; |
| 6 | |
| 7 | use AC\Expression\StringComparisonSpecification; |
| 8 | use AC\Setting\Children; |
| 9 | use AC\Setting\Component; |
| 10 | use AC\Setting\ComponentCollection; |
| 11 | use AC\Setting\Config; |
| 12 | use AC\Setting\Control\Input; |
| 13 | use AC\Setting\Control\Input\Number; |
| 14 | use AC\Setting\Control\Input\OptionFactory; |
| 15 | use AC\Setting\Control\OptionCollection; |
| 16 | use AC\Setting\Control\Type\Option; |
| 17 | |
| 18 | abstract class ImageSizeBase extends BaseComponentFactory |
| 19 | { |
| 20 | abstract protected function get_size_name(): string; |
| 21 | |
| 22 | abstract protected function get_width_name(): string; |
| 23 | |
| 24 | abstract protected function get_height_name(): string; |
| 25 | |
| 26 | protected function get_default_size(): string |
| 27 | { |
| 28 | return 'cpac-custom'; |
| 29 | } |
| 30 | |
| 31 | protected function get_default_width(): int |
| 32 | { |
| 33 | return 60; |
| 34 | } |
| 35 | |
| 36 | protected function get_default_height(): int |
| 37 | { |
| 38 | return 60; |
| 39 | } |
| 40 | |
| 41 | protected function get_input(Config $config): ?Input |
| 42 | { |
| 43 | return OptionFactory::create_select( |
| 44 | $this->get_size_name(), |
| 45 | $this->get_grouped_image_sizes(), |
| 46 | $config->get($this->get_size_name()) ?: $this->get_default_size() |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | protected function get_children(Config $config): ?Children |
| 51 | { |
| 52 | $width = $config->has($this->get_width_name()) |
| 53 | ? (int)$config->get($this->get_width_name()) |
| 54 | : $this->get_default_width(); |
| 55 | |
| 56 | $height = $config->has($this->get_height_name()) |
| 57 | ? (int)$config->get($this->get_height_name()) |
| 58 | : $this->get_default_height(); |
| 59 | |
| 60 | return new Children( |
| 61 | new ComponentCollection([ |
| 62 | new Component( |
| 63 | __('Width', 'codepress-admin-columns'), |
| 64 | null, |
| 65 | Number::create_single_step($this->get_width_name(), 0, null, $width), |
| 66 | StringComparisonSpecification::equal('cpac-custom') |
| 67 | ), |
| 68 | new Component( |
| 69 | __('Height', 'codepress-admin-columns'), |
| 70 | null, |
| 71 | Number::create_single_step($this->get_height_name(), 0, null, $height), |
| 72 | StringComparisonSpecification::equal('cpac-custom') |
| 73 | ), |
| 74 | ]), |
| 75 | true |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return string|int[] |
| 81 | */ |
| 82 | protected function get_size(Config $config) |
| 83 | { |
| 84 | $size = $config->get($this->get_size_name()) ?: $this->get_default_size(); |
| 85 | |
| 86 | if ($size === 'cpac-custom') { |
| 87 | return [ |
| 88 | $config->has($this->get_width_name()) |
| 89 | ? (int)$config->get($this->get_width_name()) |
| 90 | : $this->get_default_width(), |
| 91 | $config->has($this->get_height_name()) |
| 92 | ? (int)$config->get($this->get_height_name()) |
| 93 | : $this->get_default_height(), |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | return $size; |
| 98 | } |
| 99 | |
| 100 | protected function get_dimension_for_size(string $size): ?array |
| 101 | { |
| 102 | global $_wp_additional_image_sizes; |
| 103 | |
| 104 | $width = $_wp_additional_image_sizes[$size]['width'] ?? get_option("{$size}_size_w"); |
| 105 | $height = $_wp_additional_image_sizes[$size]['height'] ?? get_option("{$size}_size_h"); |
| 106 | |
| 107 | return $width && $height |
| 108 | ? [(int)$width, (int)$height] |
| 109 | : null; |
| 110 | } |
| 111 | |
| 112 | private function get_grouped_image_sizes(): OptionCollection |
| 113 | { |
| 114 | $default_group = __('Default', 'codepress-admin-columns'); |
| 115 | |
| 116 | $options = new OptionCollection([ |
| 117 | new Option(__('Thumbnail', 'codepress-admin-columns'), 'thumbnail', $default_group), |
| 118 | new Option(__('Medium', 'codepress-admin-columns'), 'medium', $default_group), |
| 119 | new Option(__('Large', 'codepress-admin-columns'), 'large', $default_group), |
| 120 | new Option(__('Full Size', 'codepress-admin-columns'), 'full', $default_group), |
| 121 | ]); |
| 122 | |
| 123 | $options->add( |
| 124 | new Option( |
| 125 | __('Custom Size', 'codepress-admin-columns'), |
| 126 | 'cpac-custom', |
| 127 | __('Custom', 'codepress-admin-columns') |
| 128 | ) |
| 129 | ); |
| 130 | |
| 131 | $custom_group = __('Others', 'codepress-admin-columns'); |
| 132 | |
| 133 | foreach (get_intermediate_image_sizes() as $size) { |
| 134 | if ('medium_large' === $size || isset($sizes['default']['options'][$size])) { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | if (! is_string($size)) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | $options->add( |
| 143 | new Option( |
| 144 | ucwords(str_replace(['-', '_'], ' ', $size)), |
| 145 | $size, |
| 146 | $custom_group |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | return $this->append_dimensions($options); |
| 152 | } |
| 153 | |
| 154 | private function append_dimensions(OptionCollection $options): OptionCollection |
| 155 | { |
| 156 | $labeled_options = new OptionCollection(); |
| 157 | |
| 158 | foreach ($options as $option) { |
| 159 | $size = $this->get_dimension_for_size($option->get_value()); |
| 160 | |
| 161 | if (is_array($size)) { |
| 162 | $size = sprintf(' (%s x %s)', $size[0], $size[1]); |
| 163 | } |
| 164 | |
| 165 | $labeled_options->add( |
| 166 | new Option( |
| 167 | $option->get_label() . $size, |
| 168 | $option->get_value(), |
| 169 | $option->get_group() |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | return $labeled_options; |
| 175 | } |
| 176 | |
| 177 | } |
| 178 |