PluginProbe
Assistant – Every Day Productivity Apps / 1.4.7
Assistant – Every Day Productivity Apps v1.4.7
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
← All changes | backend/src/Services/MediaLibraryService.php +52 -8 0.6.01.4.7 View file →
@@ -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 [
@@ -62,11 +64,15 @@
62 64
63 65 switch ( $media->mime_type ) {
64 66 case 'image/jpg':
65 67 case 'image/jpeg':
68 + case 'image/webp':
66 69 case 'image/png':
67 70 case 'image/gif':
68 - $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 );
69 75 break;
70 76 case 'image/svg':
71 77 case 'image/svg+xml':
72 78 $response = $this->import_svg( $media->url, $media->file_name, $post_id );
@@ -80,11 +86,11 @@
80 86 return $response;
81 87 }
82 88
83 89 /**
84 - * Import an image into the media library.
90 + * Import a file into the media library.
85 91 */
86 - public function import_image( $url, $name, $post_id = 0 ) {
92 + public function import_file( $url, $name, $post_id = 0 ) {
87 93 require_once( ABSPATH . 'wp-admin/includes/image.php' );
88 94 require_once( ABSPATH . 'wp-admin/includes/file.php' );
89 95 require_once( ABSPATH . 'wp-admin/includes/media.php' );
90 96
@@ -94,9 +100,9 @@
94 100 if ( ! $tmp_path ) {
95 101 return [ 'error' => __( 'Error creating temp file.' ) ];
96 102 }
97 103
98 - $response = wp_remote_get(
104 + $response = wp_safe_remote_get(
99 105 $url,
100 106 [
101 107 'timeout' => 300,
102 108 'stream' => true,
@@ -108,9 +114,9 @@
108 114 $response_code = wp_remote_retrieve_response_code( $response );
109 115
110 116 if ( 200 !== intval( $response_code ) ) {
111 117 @unlink( $tmp_path );
112 - return [ 'error' => __( 'Error downloading image file.' ) ];
118 + return [ 'error' => __( 'Error downloading file.' ) ];
113 119 }
114 120
115 121 $id = media_handle_sideload(
116 122 [
@@ -121,11 +127,17 @@
121 127 );
122 128
123 129 if ( is_wp_error( $id ) ) {
124 130 @unlink( $tmp_path );
125 - return [ 'error' => __( 'Error importing image file.' ) ];
131 + return [ 'error' => __( 'Error importing file.' ) ];
126 132 }
127 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 +
128 140 return [
129 141 'id' => $id,
130 142 'url' => wp_get_attachment_url( $id ),
131 143 ];
@@ -131,14 +143,46 @@
131 143 ];
132 144 }
133 145
134 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 + /**
135 180 * Import an svg file into the media library.
136 181 */
137 182 public function import_svg( $url, $name, $post_id = 0 ) {
138 183 $this->add_svg_import_filters();
139 -
140 - return $this->import_image( $url, $name, $post_id );
184 + return $this->import_file( $url, $name, $post_id );
141 185 }
142 186
143 187 /**
144 188 * WP core media filters for allowing svg upload.