| @@ -172,9 +172,9 @@ | ||
| 172 | 172 | */ |
| 173 | 173 | 'alt_text' => '', |
| 174 | 174 | |
| 175 | 175 | /** |
| 176 | - * Whether this image should be set as the post's thumbail. If the post_id | |
| 176 | + * Whether this image should be set as the post's thumbnail. If the post_id | |
| 177 | 177 | * is 0, this setting will be ignored. |
| 178 | 178 | */ |
| 179 | 179 | 'is_post_thumbnail' => false, |
| 180 | 180 | |
| @@ -370,9 +370,9 @@ | ||
| 370 | 370 | |
| 371 | 371 | $this->set_attachment_id( $result ); |
| 372 | 372 | $this->set_post_thumbnail(); |
| 373 | 373 | $this->set_alt_text(); |
| 374 | - $this->set_owner_source(); | |
| 374 | + $this->set_owner_source(); | |
| 375 | 375 | |
| 376 | 376 | /** |
| 377 | 377 | * Do something when image import succeeds. |
| 378 | 378 | * |
| @@ -489,12 +489,40 @@ | ||
| 489 | 489 | if ( is_wp_error( $attachment_id ) ) { |
| 490 | 490 | return $attachment_id; |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | - // If the image does not have a width or height, return WP_Error. | |
| 493 | + $attachment_id = absint( $attachment_id ); | |
| 494 | + | |
| 495 | + if ( $attachment_id === 0 ) { | |
| 496 | + return new WP_Error( 'dfrapi_attachment_id_is_zero', __( 'Attachment ID is zero.', 'datafeedr' ) ); | |
| 497 | + } | |
| 498 | + | |
| 499 | + /** | |
| 500 | + * Get attachment metadata just to confirm an image | |
| 501 | + * was uploaded to the server properly. | |
| 502 | + */ | |
| 494 | 503 | $meta = wp_get_attachment_metadata( $attachment_id ); |
| 495 | - if ( ! isset( $meta['width'], $meta['height'] ) ) { | |
| 496 | - return new WP_Error( 'http_no_image_width_or_height', __( 'Image data does not exist.', 'datafeedr' ) ); | |
| 504 | + | |
| 505 | + if ( ! $meta ) { | |
| 506 | + return new WP_Error( 'dfrapi_wp_get_attachment_metadata_returned_false', __( 'wp_get_attachment_metadata() returned false.', 'datafeedr' ) ); | |
| 507 | + } | |
| 508 | + | |
| 509 | + $width = isset( $meta['width'] ) ? absint( $meta['width'] ) : 0; | |
| 510 | + $height = isset( $meta['width'] ) ? absint( $meta['height'] ) : 0; | |
| 511 | + | |
| 512 | + // If the image width is 0, return WP_Error. | |
| 513 | + if ( $width === 0 ) { | |
| 514 | + return new WP_Error( 'dfrapi_attachment_width_is_zero', __( 'Attachment width is zero.', 'datafeedr' ) ); | |
| 515 | + } | |
| 516 | + | |
| 517 | + // If the image height is 0, return WP_Error. | |
| 518 | + if ( $height === 0 ) { | |
| 519 | + return new WP_Error( 'dfrapi_attachment_height_is_zero', __( 'Attachment height is zero.', 'datafeedr' ) ); | |
| 520 | + } | |
| 521 | + | |
| 522 | + // If the image is missing a file, return WP_Error. | |
| 523 | + if ( ! isset( $meta['file'] ) || empty( $meta['file'] ) ) { | |
| 524 | + return new WP_Error( 'dfrapi_attachment_has_no_file', __( 'Attachment has no file.', 'datafeedr' ) ); | |
| 497 | 525 | } |
| 498 | 526 | |
| 499 | 527 | // If we made it this far, the image is valid so return the image's attachment ID. |
| 500 | 528 | return $attachment_id; |