BeforeAfter
5 years ago
Pro
5 years ago
ActionIcons.php
5 years ago
AttachmentDisplay.php
5 years ago
BeforeAfter.php
5 years ago
CharacterLimit.php
5 years ago
Comment.php
5 years ago
CommentCount.php
5 years ago
CommentLink.php
5 years ago
CustomField.php
5 years ago
CustomFieldType.php
5 years ago
Date.php
5 years ago
DateTimeFormat.php
5 years ago
ExifData.php
5 years ago
Image.php
5 years ago
Images.php
5 years ago
Label.php
5 years ago
LinkLabel.php
5 years ago
LinkToMenu.php
5 years ago
MediaLink.php
5 years ago
Message.php
5 years ago
Meta.php
5 years ago
MissingImageSize.php
5 years ago
NumberFormat.php
5 years ago
NumberOfItems.php
5 years ago
Password.php
5 years ago
PathScope.php
5 years ago
Post.php
5 years ago
PostFormatIcon.php
5 years ago
PostLink.php
5 years ago
PostStatus.php
5 years ago
PostType.php
5 years ago
Pro.php
5 years ago
Separator.php
5 years ago
StatusIcon.php
5 years ago
StringLimit.php
5 years ago
Taxonomy.php
5 years ago
Term.php
5 years ago
TermLink.php
5 years ago
Time.php
5 years ago
Toggle.php
5 years ago
Type.php
5 years ago
User.php
5 years ago
UserLink.php
5 years ago
Width.php
5 years ago
WordLimit.php
5 years ago
WordsPerMinute.php
5 years ago
Image.php
200 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Settings\Column; |
| 4 | |
| 5 | use AC\Settings; |
| 6 | use AC\View; |
| 7 | |
| 8 | class Image extends Settings\Column |
| 9 | implements Settings\FormatValue { |
| 10 | |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | private $image_size; |
| 15 | |
| 16 | /** |
| 17 | * @var integer |
| 18 | */ |
| 19 | private $image_size_w; |
| 20 | |
| 21 | /** |
| 22 | * @var integer |
| 23 | */ |
| 24 | private $image_size_h; |
| 25 | |
| 26 | protected function set_name() { |
| 27 | return $this->name = 'image'; |
| 28 | } |
| 29 | |
| 30 | protected function define_options() { |
| 31 | return [ |
| 32 | 'image_size' => 'cpac-custom', |
| 33 | 'image_size_w' => 60, |
| 34 | 'image_size_h' => 60, |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | public function create_view() { |
| 39 | $width = new View( [ |
| 40 | 'setting' => $this->create_element( 'number', 'image_size_w' ), |
| 41 | 'label' => __( 'Width', 'codepress-admin-columns' ), |
| 42 | 'tooltip' => __( 'Width in pixels', 'codepress-admin-columns' ), |
| 43 | ] ); |
| 44 | |
| 45 | $height = new View( [ |
| 46 | 'setting' => $this->create_element( 'number', 'image_size_h' ), |
| 47 | 'label' => __( 'Height', 'codepress-admin-columns' ), |
| 48 | 'tooltip' => __( 'Height in pixels', 'codepress-admin-columns' ), |
| 49 | ] ); |
| 50 | |
| 51 | $size = $this->create_element( 'select', 'image_size' ) |
| 52 | ->set_options( $this->get_grouped_image_sizes() ); |
| 53 | |
| 54 | $view = new View( [ |
| 55 | 'label' => __( 'Image Size', 'codepress-admin-columns' ), |
| 56 | 'setting' => $size, |
| 57 | 'sections' => [ $width, $height ], |
| 58 | ] ); |
| 59 | |
| 60 | return $view; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return array |
| 65 | * @since 1.0 |
| 66 | */ |
| 67 | private function get_grouped_image_sizes() { |
| 68 | global $_wp_additional_image_sizes; |
| 69 | |
| 70 | $sizes = [ |
| 71 | 'default' => [ |
| 72 | 'title' => __( 'Default', 'codepress-admin-columns' ), |
| 73 | 'options' => [ |
| 74 | 'thumbnail' => __( 'Thumbnail', 'codepress-admin-columns' ), |
| 75 | 'medium' => __( 'Medium', 'codepress-admin-columns' ), |
| 76 | 'large' => __( 'Large', 'codepress-admin-columns' ), |
| 77 | ], |
| 78 | ], |
| 79 | ]; |
| 80 | |
| 81 | $all_sizes = get_intermediate_image_sizes(); |
| 82 | |
| 83 | if ( ! empty( $all_sizes ) ) { |
| 84 | foreach ( $all_sizes as $size ) { |
| 85 | if ( 'medium_large' == $size || isset( $sizes['default']['options'][ $size ] ) ) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if ( ! isset( $sizes['defined'] ) ) { |
| 90 | $sizes['defined']['title'] = __( 'Others', 'codepress-admin-columns' ); |
| 91 | } |
| 92 | |
| 93 | $sizes['defined']['options'][ $size ] = ucwords( str_replace( '-', ' ', $size ) ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | foreach ( $sizes as $key => $group ) { |
| 98 | foreach ( array_keys( $group['options'] ) as $_size ) { |
| 99 | |
| 100 | $w = isset( $_wp_additional_image_sizes[ $_size ]['width'] ) ? $_wp_additional_image_sizes[ $_size ]['width'] : get_option( "{$_size}_size_w" ); |
| 101 | $h = isset( $_wp_additional_image_sizes[ $_size ]['height'] ) ? $_wp_additional_image_sizes[ $_size ]['height'] : get_option( "{$_size}_size_h" ); |
| 102 | |
| 103 | if ( $w && $h ) { |
| 104 | $sizes[ $key ]['options'][ $_size ] .= " ($w x $h)"; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $sizes['default']['options']['full'] = __( 'Full Size', 'codepress-admin-columns' ); |
| 110 | |
| 111 | $sizes['custom'] = [ |
| 112 | 'title' => __( 'Custom', 'codepress-admin-columns' ), |
| 113 | 'options' => [ 'cpac-custom' => __( 'Custom Size', 'codepress-admin-columns' ) . '…' ], |
| 114 | ]; |
| 115 | |
| 116 | return $sizes; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return string |
| 121 | */ |
| 122 | public function get_image_size() { |
| 123 | return $this->image_size; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @param string $image_size |
| 128 | * |
| 129 | * @return bool |
| 130 | */ |
| 131 | public function set_image_size( $image_size ) { |
| 132 | $this->image_size = $image_size; |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return int |
| 139 | */ |
| 140 | public function get_image_size_w() { |
| 141 | return $this->image_size_w; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param int $image_size_w |
| 146 | * |
| 147 | * @return bool |
| 148 | */ |
| 149 | public function set_image_size_w( $image_size_w ) { |
| 150 | if ( ! is_numeric( $image_size_w ) ) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | $this->image_size_w = $image_size_w; |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return int |
| 161 | */ |
| 162 | public function get_image_size_h() { |
| 163 | return $this->image_size_h; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @param int $image_size_h |
| 168 | * |
| 169 | * @return bool |
| 170 | */ |
| 171 | public function set_image_size_h( $image_size_h ) { |
| 172 | if ( ! is_numeric( $image_size_h ) ) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | $this->image_size_h = $image_size_h; |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | protected function get_size_args() { |
| 182 | $size = $this->get_image_size(); |
| 183 | |
| 184 | if ( 'cpac-custom' === $size ) { |
| 185 | $size = [ $this->get_image_size_w(), $this->get_image_size_h() ]; |
| 186 | } |
| 187 | |
| 188 | // fallback size |
| 189 | if ( empty( $size ) ) { |
| 190 | $size = [ 60, 60 ]; |
| 191 | } |
| 192 | |
| 193 | return $size; |
| 194 | } |
| 195 | |
| 196 | public function format( $value, $original_value ) { |
| 197 | return ac_helper()->image->get_image( $value, $this->get_size_args() ); |
| 198 | } |
| 199 | |
| 200 | } |