| @@ -64,11 +64,15 @@ | ||
| 64 | 64 | |
| 65 | 65 | switch ( $media->mime_type ) { |
| 66 | 66 | case 'image/jpg': |
| 67 | 67 | case 'image/jpeg': |
| 68 | + case 'image/webp': | |
| 68 | 69 | case 'image/png': |
| 69 | 70 | case 'image/gif': |
| 70 | - $response = $this->import_image( $media->url, $media->file_name, $post_id ); | |
| 71 | + case 'application/pdf': | |
| 72 | + case 'application/rtf': | |
| 73 | + case 'text/rtf': | |
| 74 | + $response = $this->import_file( $media->url, $media->file_name, $post_id ); | |
| 71 | 75 | break; |
| 72 | 76 | case 'image/svg': |
| 73 | 77 | case 'image/svg+xml': |
| 74 | 78 | $response = $this->import_svg( $media->url, $media->file_name, $post_id ); |
| @@ -82,11 +86,11 @@ | ||
| 82 | 86 | return $response; |
| 83 | 87 | } |
| 84 | 88 | |
| 85 | 89 | /** |
| 86 | - * Import an image into the media library. | |
| 90 | + * Import a file into the media library. | |
| 87 | 91 | */ |
| 88 | - public function import_image( $url, $name, $post_id = 0 ) { | |
| 92 | + public function import_file( $url, $name, $post_id = 0 ) { | |
| 89 | 93 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 90 | 94 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 91 | 95 | require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 92 | 96 | |
| @@ -96,9 +100,9 @@ | ||
| 96 | 100 | if ( ! $tmp_path ) { |
| 97 | 101 | return [ 'error' => __( 'Error creating temp file.' ) ]; |
| 98 | 102 | } |
| 99 | 103 | |
| 100 | - $response = wp_remote_get( | |
| 104 | + $response = wp_safe_remote_get( | |
| 101 | 105 | $url, |
| 102 | 106 | [ |
| 103 | 107 | 'timeout' => 300, |
| 104 | 108 | 'stream' => true, |
| @@ -110,9 +114,9 @@ | ||
| 110 | 114 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 111 | 115 | |
| 112 | 116 | if ( 200 !== intval( $response_code ) ) { |
| 113 | 117 | @unlink( $tmp_path ); |
| 114 | - return [ 'error' => __( 'Error downloading image file.' ) ]; | |
| 118 | + return [ 'error' => __( 'Error downloading file.' ) ]; | |
| 115 | 119 | } |
| 116 | 120 | |
| 117 | 121 | $id = media_handle_sideload( |
| 118 | 122 | [ |
| @@ -123,11 +127,17 @@ | ||
| 123 | 127 | ); |
| 124 | 128 | |
| 125 | 129 | if ( is_wp_error( $id ) ) { |
| 126 | 130 | @unlink( $tmp_path ); |
| 127 | - return [ 'error' => __( 'Error importing image file.' ) ]; | |
| 131 | + return [ 'error' => __( 'Error importing file.' ) ]; | |
| 128 | 132 | } |
| 129 | 133 | |
| 134 | + $alt_tag = $this->get_alt_tag_from_post_content( $post_id, $name ); | |
| 135 | + | |
| 136 | + if ( !empty( $alt_tag ) ) { | |
| 137 | + update_post_meta( $id, '_wp_attachment_image_alt', $alt_tag ); | |
| 138 | + } | |
| 139 | + | |
| 130 | 140 | return [ |
| 131 | 141 | 'id' => $id, |
| 132 | 142 | 'url' => wp_get_attachment_url( $id ), |
| 133 | 143 | ]; |
| @@ -133,14 +143,46 @@ | ||
| 133 | 143 | ]; |
| 134 | 144 | } |
| 135 | 145 | |
| 136 | 146 | /** |
| 147 | + * Get the alt tag for a filename from the post content. | |
| 148 | + */ | |
| 149 | + public function get_alt_tag_from_post_content( $post_id, $filename ) { | |
| 150 | + $content = get_post_field( 'post_content', $post_id ); | |
| 151 | + $alt_tag = null; | |
| 152 | + | |
| 153 | + if ( !empty( $content ) ) { | |
| 154 | + $dom = new \DOMDocument(); | |
| 155 | + @$dom->loadHTML( $content ); | |
| 156 | + $images = $dom->getElementsByTagName( 'img' ); | |
| 157 | + | |
| 158 | + foreach ( $images as $image ) { | |
| 159 | + $src = $image->getAttribute( 'src' ); | |
| 160 | + $src_filename = basename( parse_url( $src, PHP_URL_PATH ) ); | |
| 161 | + | |
| 162 | + // Remove dimensions to get the full size filename | |
| 163 | + preg_match_all( '/(-(\d+)x(\d+))\.(jpg|jpeg|png|gif|svg)/', $src_filename, $matches ); | |
| 164 | + | |
| 165 | + if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) { | |
| 166 | + $src_filename = str_replace( $matches[1], '', $src_filename ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + if ( $src_filename === $filename ) { | |
| 170 | + $alt_tag = $image->getAttribute( 'alt' ); | |
| 171 | + break; | |
| 172 | + } | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + return $alt_tag; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 137 | 180 | * Import an svg file into the media library. |
| 138 | 181 | */ |
| 139 | 182 | public function import_svg( $url, $name, $post_id = 0 ) { |
| 140 | 183 | $this->add_svg_import_filters(); |
| 141 | - | |
| 142 | - return $this->import_image( $url, $name, $post_id ); | |
| 184 | + return $this->import_file( $url, $name, $post_id ); | |
| 143 | 185 | } |
| 144 | 186 | |
| 145 | 187 | /** |
| 146 | 188 | * WP core media filters for allowing svg upload. |