| @@ -13,8 +13,9 @@ | ||
| 13 | 13 | public function is_cloud_media_imported( $media ) { |
| 14 | 14 | global $wpdb; |
| 15 | 15 | |
| 16 | 16 | // Check if a file with this name and sha1 hash exists. |
| 17 | + $media = (object) $media; | |
| 17 | 18 | $parts = explode( '.', $media->file_name ); |
| 18 | 19 | $name = array_shift( $parts ); |
| 19 | 20 | |
| 20 | 21 | $row = $wpdb->get_row( |
| @@ -50,8 +51,9 @@ | ||
| 50 | 51 | * Import a cloud media item into the media library. |
| 51 | 52 | */ |
| 52 | 53 | public function import_cloud_media( $media, $post_id = 0 ) { |
| 53 | 54 | $response = null; |
| 55 | + $media = (object) $media; | |
| 54 | 56 | $imported_id = $this->is_cloud_media_imported( $media ); |
| 55 | 57 | |
| 56 | 58 | if ( $imported_id ) { |
| 57 | 59 | return [ |
| @@ -64,9 +66,12 @@ | ||
| 64 | 66 | case 'image/jpg': |
| 65 | 67 | case 'image/jpeg': |
| 66 | 68 | case 'image/png': |
| 67 | 69 | case 'image/gif': |
| 68 | - $response = $this->import_image( $media->url, $media->file_name, $post_id ); | |
| 70 | + case 'application/pdf': | |
| 71 | + case 'application/rtf': | |
| 72 | + case 'text/rtf': | |
| 73 | + $response = $this->import_file( $media->url, $media->file_name, $post_id ); | |
| 69 | 74 | break; |
| 70 | 75 | case 'image/svg': |
| 71 | 76 | case 'image/svg+xml': |
| 72 | 77 | $response = $this->import_svg( $media->url, $media->file_name, $post_id ); |
| @@ -80,11 +85,11 @@ | ||
| 80 | 85 | return $response; |
| 81 | 86 | } |
| 82 | 87 | |
| 83 | 88 | /** |
| 84 | - * Import an image into the media library. | |
| 89 | + * Import a file into the media library. | |
| 85 | 90 | */ |
| 86 | - public function import_image( $url, $name, $post_id = 0 ) { | |
| 91 | + public function import_file( $url, $name, $post_id = 0 ) { | |
| 87 | 92 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 88 | 93 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 89 | 94 | require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 90 | 95 | |
| @@ -108,9 +113,9 @@ | ||
| 108 | 113 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 109 | 114 | |
| 110 | 115 | if ( 200 !== intval( $response_code ) ) { |
| 111 | 116 | @unlink( $tmp_path ); |
| 112 | - return [ 'error' => __( 'Error downloading image file.' ) ]; | |
| 117 | + return [ 'error' => __( 'Error downloading file.' ) ]; | |
| 113 | 118 | } |
| 114 | 119 | |
| 115 | 120 | $id = media_handle_sideload( |
| 116 | 121 | [ |
| @@ -121,9 +126,9 @@ | ||
| 121 | 126 | ); |
| 122 | 127 | |
| 123 | 128 | if ( is_wp_error( $id ) ) { |
| 124 | 129 | @unlink( $tmp_path ); |
| 125 | - return [ 'error' => __( 'Error importing image file.' ) ]; | |
| 130 | + return [ 'error' => __( 'Error importing file.' ) ]; | |
| 126 | 131 | } |
| 127 | 132 | |
| 128 | 133 | return [ |
| 129 | 134 | 'id' => $id, |
| @@ -130,15 +135,15 @@ | ||
| 130 | 135 | 'url' => wp_get_attachment_url( $id ), |
| 131 | 136 | ]; |
| 132 | 137 | } |
| 133 | 138 | |
| 139 | + | |
| 134 | 140 | /** |
| 135 | 141 | * Import an svg file into the media library. |
| 136 | 142 | */ |
| 137 | 143 | public function import_svg( $url, $name, $post_id = 0 ) { |
| 138 | 144 | $this->add_svg_import_filters(); |
| 139 | - | |
| 140 | - return $this->import_image( $url, $name, $post_id ); | |
| 145 | + return $this->import_file( $url, $name, $post_id ); | |
| 141 | 146 | } |
| 142 | 147 | |
| 143 | 148 | /** |
| 144 | 149 | * WP core media filters for allowing svg upload. |