PluginProbe
Assistant – Every Day Productivity Apps / 1.4.5
Assistant – Every Day Productivity Apps v1.4.5
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
assistant / backend / src / Services / MediaLibraryService.php

MediaLibraryService.php in Assistant – Every Day Productivity Apps 1.4.5, at backend/src/Services/MediaLibraryService.php

218 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Services;
4
5 class MediaLibraryService {
6
7 /**
8 * Checks if a cloud media item was already imported.
9 *
10 * @param object $media
11 * @return int|null
12 */
13 public function is_cloud_media_imported( $media ) {
14 global $wpdb;
15
16 // Check if a file with this name and sha1 hash exists.
17 $media = (object) $media;
18 $parts = explode( '.', $media->file_name );
19 $name = array_shift( $parts );
20
21 $row = $wpdb->get_row(
22 $wpdb->prepare(
23 "SELECT * FROM $wpdb->posts
24 WHERE post_type = 'attachment'
25 AND post_name = %s",
26 $name
27 )
28 );
29
30 if ( $row ) {
31 $path = get_attached_file( $row->ID );
32 if ( $path && sha1_file( $path ) === $media->upload_hash ) {
33 return $row->ID;
34 }
35 }
36
37 // Check if we've already imported this file using the original sha1.
38 $row = $wpdb->get_row(
39 $wpdb->prepare(
40 "SELECT * FROM $wpdb->postmeta
41 WHERE meta_key = '_fl_asst_imported_media_hash'
42 AND meta_value = %s",
43 $media->upload_hash
44 )
45 );
46
47 return $row ? $row->post_id : null;
48 }
49
50 /**
51 * Import a cloud media item into the media library.
52 */
53 public function import_cloud_media( $media, $post_id = 0 ) {
54 $response = null;
55 $media = (object) $media;
56 $imported_id = $this->is_cloud_media_imported( $media );
57
58 if ( $imported_id ) {
59 return [
60 'id' => $imported_id,
61 'url' => wp_get_attachment_url( $imported_id ),
62 ];
63 }
64
65 switch ( $media->mime_type ) {
66 case 'image/jpg':
67 case 'image/jpeg':
68 case 'image/png':
69 case 'image/gif':
70 case 'application/pdf':
71 case 'application/rtf':
72 case 'text/rtf':
73 $response = $this->import_file( $media->url, $media->file_name, $post_id );
74 break;
75 case 'image/svg':
76 case 'image/svg+xml':
77 $response = $this->import_svg( $media->url, $media->file_name, $post_id );
78 break;
79 }
80
81 if ( $response && isset( $response['id'] ) ) {
82 update_post_meta( $response['id'], '_fl_asst_imported_media_hash', $media->upload_hash );
83 }
84
85 return $response;
86 }
87
88 /**
89 * Import a file into the media library.
90 */
91 public function import_file( $url, $name, $post_id = 0 ) {
92 require_once( ABSPATH . 'wp-admin/includes/image.php' );
93 require_once( ABSPATH . 'wp-admin/includes/file.php' );
94 require_once( ABSPATH . 'wp-admin/includes/media.php' );
95
96 $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
97 $tmp_path = wp_tempnam( $url_filename );
98
99 if ( ! $tmp_path ) {
100 return [ 'error' => __( 'Error creating temp file.' ) ];
101 }
102
103 $response = wp_safe_remote_get(
104 $url,
105 [
106 'timeout' => 300,
107 'stream' => true,
108 'filename' => $tmp_path,
109 'sslverify' => false,
110 ]
111 );
112
113 $response_code = wp_remote_retrieve_response_code( $response );
114
115 if ( 200 !== intval( $response_code ) ) {
116 @unlink( $tmp_path );
117 return [ 'error' => __( 'Error downloading file.' ) ];
118 }
119
120 $id = media_handle_sideload(
121 [
122 'name' => sanitize_file_name( $name ),
123 'tmp_name' => $tmp_path,
124 ],
125 $post_id
126 );
127
128 if ( is_wp_error( $id ) ) {
129 @unlink( $tmp_path );
130 return [ 'error' => __( 'Error importing file.' ) ];
131 }
132
133 $alt_tag = $this->get_alt_tag_from_post_content( $post_id, $name );
134
135 if ( !empty( $alt_tag ) ) {
136 update_post_meta( $id, '_wp_attachment_image_alt', $alt_tag );
137 }
138
139 return [
140 'id' => $id,
141 'url' => wp_get_attachment_url( $id ),
142 ];
143 }
144
145 /**
146 * Get the alt tag for a filename from the post content.
147 */
148 public function get_alt_tag_from_post_content( $post_id, $filename ) {
149 $content = get_post_field( 'post_content', $post_id );
150 $alt_tag = null;
151
152 if ( !empty( $content ) ) {
153 $dom = new \DOMDocument();
154 @$dom->loadHTML( $content );
155 $images = $dom->getElementsByTagName( 'img' );
156
157 foreach ( $images as $image ) {
158 $src = $image->getAttribute( 'src' );
159 $src_filename = basename( parse_url( $src, PHP_URL_PATH ) );
160
161 // Remove dimensions to get the full size filename
162 preg_match_all( '/(-(\d+)x(\d+))\.(jpg|jpeg|png|gif|svg)/', $src_filename, $matches );
163
164 if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
165 $src_filename = str_replace( $matches[1], '', $src_filename );
166 }
167
168 if ( $src_filename === $filename ) {
169 $alt_tag = $image->getAttribute( 'alt' );
170 break;
171 }
172 }
173 }
174
175 return $alt_tag;
176 }
177
178 /**
179 * Import an svg file into the media library.
180 */
181 public function import_svg( $url, $name, $post_id = 0 ) {
182 $this->add_svg_import_filters();
183 return $this->import_file( $url, $name, $post_id );
184 }
185
186 /**
187 * WP core media filters for allowing svg upload.
188 */
189 private function add_svg_import_filters() {
190 add_filter(
191 'upload_mimes', function( $mimes ) {
192 $mimes['svg'] = 'image/svg+xml';
193 $mimes['svgz'] = 'image/svg+xml';
194 return $mimes;
195 }, 99
196 );
197
198 add_filter(
199 'wp_check_filetype_and_ext', function ( $checked, $file, $filename, $mimes ) {
200 if ( ! $checked['type'] ) {
201 $check_filetype = wp_check_filetype( $filename, $mimes );
202 $ext = $check_filetype['ext'];
203 $type = $check_filetype['type'];
204 $proper_filename = $filename;
205
206 if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
207 $type = false;
208 $ext = false;
209 }
210
211 $checked = compact( 'ext', 'type', 'proper_filename' );
212 }
213 return $checked;
214 }, 10, 4
215 );
216 }
217 }
218