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

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

180 lines 4.1 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_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 return [
134 'id' => $id,
135 'url' => wp_get_attachment_url( $id ),
136 ];
137 }
138
139
140 /**
141 * Import an svg file into the media library.
142 */
143 public function import_svg( $url, $name, $post_id = 0 ) {
144 $this->add_svg_import_filters();
145 return $this->import_file( $url, $name, $post_id );
146 }
147
148 /**
149 * WP core media filters for allowing svg upload.
150 */
151 private function add_svg_import_filters() {
152 add_filter(
153 'upload_mimes', function( $mimes ) {
154 $mimes['svg'] = 'image/svg+xml';
155 $mimes['svgz'] = 'image/svg+xml';
156 return $mimes;
157 }, 99
158 );
159
160 add_filter(
161 'wp_check_filetype_and_ext', function ( $checked, $file, $filename, $mimes ) {
162 if ( ! $checked['type'] ) {
163 $check_filetype = wp_check_filetype( $filename, $mimes );
164 $ext = $check_filetype['ext'];
165 $type = $check_filetype['type'];
166 $proper_filename = $filename;
167
168 if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
169 $type = false;
170 $ext = false;
171 }
172
173 $checked = compact( 'ext', 'type', 'proper_filename' );
174 }
175 return $checked;
176 }, 10, 4
177 );
178 }
179 }
180