| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class WP_Site_Icon_Admin_Processing { |
|---|
| 4 | public function __construct() { |
|---|
| 5 | |
|---|
| 6 | add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ), 10, 1 ); |
|---|
| 7 | add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 ); |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * Upload a Site Icon image. |
|---|
| 12 | * |
|---|
| 13 | * This method is only used by the no Javascript admin UI flow. |
|---|
| 14 | * |
|---|
| 15 | * @since 4.3.0 |
|---|
| 16 | */ |
|---|
| 17 | public function upload_site_icon_image() { |
|---|
| 18 | // Verify that the uploaded file is an image. |
|---|
| 19 | $file_type = wp_check_filetype_and_ext( $_FILES['site-icon']['tmp_name'], $_FILES['site-icon']['name'] ); |
|---|
| 20 | if ( ! wp_match_mime_types( 'image', $file_type['type'] ) ) { |
|---|
| 21 | wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | // Upload image. |
|---|
| 25 | $file_attributes = wp_handle_upload( $_FILES['site-icon'], array( 'test_form' => false ) ); |
|---|
| 26 | if ( isset( $file_attributes['error'] ) ) { |
|---|
| 27 | wp_die( $file_attributes['error'], __( 'Image Upload Error' ) ); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | // Save image as an attachment. |
|---|
| 31 | $attachment_id = wp_insert_attachment( array( |
|---|
| 32 | 'post_title' => basename( $file_attributes['file'] ), |
|---|
| 33 | 'post_content' => $file_attributes['url'], |
|---|
| 34 | 'post_mime_type' => $file_attributes['type'], |
|---|
| 35 | 'guid' => $file_attributes['url'], |
|---|
| 36 | 'context' => 'site-icon', |
|---|
| 37 | ), |
|---|
| 38 | $file_attributes['file'] ); |
|---|
| 39 | |
|---|
| 40 | if ( 0 === $attachment_id || is_wp_error( $attachment_id ) ) { |
|---|
| 41 | wp_die( __( 'Could not save the uploaded image.' ) ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | return $attachment_id; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public function get_image_size_from_attachment( $attachment_id ) { |
|---|
| 48 | $image_file = get_attached_file( absint( $attachment_id ), true ); |
|---|
| 49 | |
|---|
| 50 | if ( ! $image_file ) { |
|---|
| 51 | wp_die( __( 'The uploaded image is corrupted.' ) ); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | return getimagesize( $image_file ); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Checks whether the uploaded image is large enough to be a Site Icon. |
|---|
| 59 | * |
|---|
| 60 | * @param int $attachment_id ID of the attachment containing the image. |
|---|
| 61 | * @param int $min_width Minimum width in pixels. |
|---|
| 62 | * @param int $min_height Minimum height in pixels. |
|---|
| 63 | * |
|---|
| 64 | * @return bool|array True if the image is large enough. An array with the errors if the image is too small. |
|---|
| 65 | */ |
|---|
| 66 | public function verify_site_icon_image_min_size( $attachment_id, $min_width, $min_height ) { |
|---|
| 67 | $image_file = get_attached_file( absint( $attachment_id ), true ); |
|---|
| 68 | |
|---|
| 69 | if ( ! $image_file ) { |
|---|
| 70 | wp_die( __( 'The uploaded image is corrupted.' ) ); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | $image_size = getimagesize( $image_file ); |
|---|
| 74 | |
|---|
| 75 | $errors = array(); |
|---|
| 76 | if ( $image_size[0] < $min_width ) { |
|---|
| 77 | $errors['width'] = true; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if ( $image_size[1] < $min_height ) { |
|---|
| 81 | $errors['height'] = true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if ( $errors ) { |
|---|
| 85 | return $errors; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public function get_attachment_image_size( $attachment_id ) { |
|---|
| 92 | $image_file = get_attached_file( absint( $attachment_id ), true ); |
|---|
| 93 | |
|---|
| 94 | if ( ! $image_file ) { |
|---|
| 95 | wp_die( __( 'The uploaded image is corrupted.' ) ); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | return getimagesize( $image_file ); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | public function get_site_icon_image_url( $attachment_id ) { |
|---|
| 102 | $image_data = wp_get_attachment_image_src( $attachment_id, 'full' ); |
|---|
| 103 | |
|---|
| 104 | if ( ! $image_data ) { |
|---|
| 105 | return false; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | return $image_data[0]; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Calculate the size of the image displayed on the cropping screen. |
|---|
| 113 | * |
|---|
| 114 | * @param $attachment_id |
|---|
| 115 | * @param $max_size |
|---|
| 116 | */ |
|---|
| 117 | public function calculate_height_of_image_for_cropping( $attachment_id, $max_width ) { |
|---|
| 118 | // Get the size of the full image. |
|---|
| 119 | $full_size = $this->get_attachment_image_size( absint( $attachment_id ) ); |
|---|
| 120 | |
|---|
| 121 | // Calculate the relationship between the height and the width of the full image. |
|---|
| 122 | $ratio = $full_size[1] / $full_size[0]; |
|---|
| 123 | |
|---|
| 124 | // Multiply the max width of the image in the admin to get the correct height to keeping the ratio intact. |
|---|
| 125 | return (int) floor( $max_width * $ratio ); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public function calculate_default_values_for_crop( $width, $height ) { |
|---|
| 129 | if ( $width < $height ) { |
|---|
| 130 | // Portrait |
|---|
| 131 | $crop_x = 0; |
|---|
| 132 | $crop_y = absint( ( $height - $width ) / 2 ); |
|---|
| 133 | $crop_size = $width; |
|---|
| 134 | } elseif ( $width > $height ) { |
|---|
| 135 | // Landscape |
|---|
| 136 | $crop_x = absint( ( $width - $height ) / 2 ); |
|---|
| 137 | $crop_y = 0; |
|---|
| 138 | $crop_size = $height; |
|---|
| 139 | } else { |
|---|
| 140 | // Square |
|---|
| 141 | $crop_x = 0; |
|---|
| 142 | $crop_y = 0; |
|---|
| 143 | $crop_size = $width; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return compact( 'crop_x', 'crop_y', 'crop_size' ); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * This function is used to pass data to the localize script |
|---|
| 151 | * so that we can center the cropper and also set the minimum |
|---|
| 152 | * cropper. |
|---|
| 153 | * |
|---|
| 154 | * @since 4.3.0 |
|---|
| 155 | * |
|---|
| 156 | * @param float $ratio |
|---|
| 157 | * @param array $cropped_size |
|---|
| 158 | * @return array |
|---|
| 159 | */ |
|---|
| 160 | public function calculate_default_values_for_javascript_cropper( $ratio, $max_size, $resized_width, $resized_height, $orginial_width, $original_height ) { |
|---|
| 161 | $init_x = $init_y = $init_size = 0; |
|---|
| 162 | $min_crop_size = ( $max_size / $ratio ); |
|---|
| 163 | |
|---|
| 164 | if ( $resized_width < $resized_height ) { |
|---|
| 165 | // Portrait |
|---|
| 166 | $init_y = ( $max_size - $resized_width ) / 2; |
|---|
| 167 | $init_size = $resized_height; |
|---|
| 168 | } elseif ( $resized_width > $resized_height ) { |
|---|
| 169 | // Landscape |
|---|
| 170 | $init_x = ( $max_size - $resized_height ) / 2; |
|---|
| 171 | $init_size = $resized_height; |
|---|
| 172 | } else { |
|---|
| 173 | // Square |
|---|
| 174 | $init_size = $resized_height; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | return array( |
|---|
| 178 | 'init_x' => (int) floor( $init_x ), |
|---|
| 179 | 'init_y' => (int) floor($init_y ), |
|---|
| 180 | 'init_size' => $init_size, |
|---|
| 181 | 'min_size' => (int) floor( $min_crop_size ), |
|---|
| 182 | 'width' => $orginial_width, |
|---|
| 183 | 'height' => $original_height, |
|---|
| 184 | ); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Converts the coordinates from the downsized image to the original image for accurate cropping. |
|---|
| 189 | * |
|---|
| 190 | * @since 4.3.0 |
|---|
| 191 | * |
|---|
| 192 | * @param int $crop_x |
|---|
| 193 | * @param int $crop_y |
|---|
| 194 | * @param int $crop_width |
|---|
| 195 | * @param int $crop_height |
|---|
| 196 | * @param float $ratio |
|---|
| 197 | * @return array |
|---|
| 198 | */ |
|---|
| 199 | public function convert_coordinates_from_resized_to_full( $crop_x, $crop_y, $crop_width, $crop_height, $ratio ) { |
|---|
| 200 | return array( |
|---|
| 201 | 'crop_x' => floor( $crop_x * $ratio ), |
|---|
| 202 | 'crop_y' => floor( $crop_y * $ratio ), |
|---|
| 203 | 'crop_width' => floor( $crop_width * $ratio ), |
|---|
| 204 | 'crop_height' => floor( $crop_height * $ratio ), |
|---|
| 205 | ); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * Create an attachment 'object'. |
|---|
| 210 | * |
|---|
| 211 | * @since 4.3.0 |
|---|
| 212 | * |
|---|
| 213 | * @param string $cropped Cropped image URL. |
|---|
| 214 | * @param int $parent_attachment_id Attachment ID of parent image. |
|---|
| 215 | * @return array Attachment object. |
|---|
| 216 | */ |
|---|
| 217 | public function create_attachment_object( $cropped, $parent_attachment_id ) { |
|---|
| 218 | $parent = get_post( $parent_attachment_id ); |
|---|
| 219 | $parent_url = $parent->guid; |
|---|
| 220 | $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); |
|---|
| 221 | |
|---|
| 222 | $size = @getimagesize( $cropped ); |
|---|
| 223 | $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; |
|---|
| 224 | |
|---|
| 225 | $object = array( |
|---|
| 226 | 'ID' => $parent_attachment_id, |
|---|
| 227 | 'post_title' => basename( $cropped ), |
|---|
| 228 | 'post_content' => $url, |
|---|
| 229 | 'post_mime_type' => $image_type, |
|---|
| 230 | 'guid' => $url, |
|---|
| 231 | 'context' => 'site-icon' |
|---|
| 232 | ); |
|---|
| 233 | |
|---|
| 234 | return $object; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | * Insert an attachment and its metadata. |
|---|
| 239 | * |
|---|
| 240 | * @since 4.3.0 |
|---|
| 241 | * |
|---|
| 242 | * @param array $object Attachment object. |
|---|
| 243 | * @param string $cropped Cropped image URL. |
|---|
| 244 | * @return int Attachment ID. |
|---|
| 245 | */ |
|---|
| 246 | public function insert_attachment( $object, $cropped ) { |
|---|
| 247 | $attachment_id = wp_insert_attachment( $object, $cropped ); |
|---|
| 248 | $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); |
|---|
| 249 | |
|---|
| 250 | /** |
|---|
| 251 | * Filter the site icon attachment metadata. |
|---|
| 252 | * |
|---|
| 253 | * @since 4.3.0 |
|---|
| 254 | * |
|---|
| 255 | * @see wp_generate_attachment_metadata() |
|---|
| 256 | * |
|---|
| 257 | * @param array $metadata Attachment metadata. |
|---|
| 258 | */ |
|---|
| 259 | $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata ); |
|---|
| 260 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
|---|
| 261 | |
|---|
| 262 | return $attachment_id; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | /** |
|---|
| 266 | * Deletes all additional image sizes, used for site icons. |
|---|
| 267 | * |
|---|
| 268 | * @since 4.3.0 |
|---|
| 269 | * |
|---|
| 270 | * @return bool |
|---|
| 271 | */ |
|---|
| 272 | public function delete_site_icon() { |
|---|
| 273 | // We add the filter to make sure that we also delete all the added images. |
|---|
| 274 | add_filter( 'intermediate_image_sizes', array( WP_Site_Icon::get_instance(), 'intermediate_image_sizes' ) ); |
|---|
| 275 | wp_delete_attachment( get_option( 'site_icon' ), true ); |
|---|
| 276 | remove_filter( 'intermediate_image_sizes', array( WP_Site_Icon::get_instance(), 'intermediate_image_sizes' ) ); |
|---|
| 277 | |
|---|
| 278 | return delete_option( 'site_icon' ); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | /** |
|---|
| 282 | * Deletes the Site Icon when the image file is deleted. |
|---|
| 283 | * |
|---|
| 284 | * @since 4.3.0 |
|---|
| 285 | * |
|---|
| 286 | * @param int $post_id Attachment ID. |
|---|
| 287 | */ |
|---|
| 288 | public function delete_attachment_data( $post_id ) { |
|---|
| 289 | $site_icon_id = get_option( 'site_icon' ); |
|---|
| 290 | |
|---|
| 291 | if ( $site_icon_id && $post_id == $site_icon_id ) { |
|---|
| 292 | delete_option( 'site_icon' ); |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /** |
|---|
| 297 | * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon. |
|---|
| 298 | * |
|---|
| 299 | * @since 4.3.0 |
|---|
| 300 | * |
|---|
| 301 | * @param null|array|string $value The value get_metadata() should |
|---|
| 302 | * return - a single metadata value, |
|---|
| 303 | * or an array of values. |
|---|
| 304 | * @param int $post_id Post ID. |
|---|
| 305 | * @param string $meta_key Meta key. |
|---|
| 306 | * @param string|array $single Meta value, or an array of values. |
|---|
| 307 | * @return array|null|string |
|---|
| 308 | */ |
|---|
| 309 | public function get_post_metadata( $value, $post_id, $meta_key, $single ) { |
|---|
| 310 | $site_icon_id = get_option( 'site_icon' ); |
|---|
| 311 | |
|---|
| 312 | if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) { |
|---|
| 313 | add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) ); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | return $value; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | } |
|---|