PluginProbe
Assistant – Every Day Productivity Apps / 0.7.0.2
Assistant – Every Day Productivity Apps v0.7.0.2
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 0.7.0.2, at backend/src/Services/MediaLibraryService.php

177 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 $response = $this->import_image( $media->url, $media->file_name, $post_id );
71 break;
72 case 'image/svg':
73 case 'image/svg+xml':
74 $response = $this->import_svg( $media->url, $media->file_name, $post_id );
75 break;
76 }
77
78 if ( $response && isset( $response['id'] ) ) {
79 update_post_meta( $response['id'], '_fl_asst_imported_media_hash', $media->upload_hash );
80 }
81
82 return $response;
83 }
84
85 /**
86 * Import an image into the media library.
87 */
88 public function import_image( $url, $name, $post_id = 0 ) {
89 require_once( ABSPATH . 'wp-admin/includes/image.php' );
90 require_once( ABSPATH . 'wp-admin/includes/file.php' );
91 require_once( ABSPATH . 'wp-admin/includes/media.php' );
92
93 $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
94 $tmp_path = wp_tempnam( $url_filename );
95
96 if ( ! $tmp_path ) {
97 return [ 'error' => __( 'Error creating temp file.' ) ];
98 }
99
100 $response = wp_remote_get(
101 $url,
102 [
103 'timeout' => 300,
104 'stream' => true,
105 'filename' => $tmp_path,
106 'sslverify' => false,
107 ]
108 );
109
110 $response_code = wp_remote_retrieve_response_code( $response );
111
112 if ( 200 !== intval( $response_code ) ) {
113 @unlink( $tmp_path );
114 return [ 'error' => __( 'Error downloading image file.' ) ];
115 }
116
117 $id = media_handle_sideload(
118 [
119 'name' => sanitize_file_name( $name ),
120 'tmp_name' => $tmp_path,
121 ],
122 $post_id
123 );
124
125 if ( is_wp_error( $id ) ) {
126 @unlink( $tmp_path );
127 return [ 'error' => __( 'Error importing image file.' ) ];
128 }
129
130 return [
131 'id' => $id,
132 'url' => wp_get_attachment_url( $id ),
133 ];
134 }
135
136 /**
137 * Import an svg file into the media library.
138 */
139 public function import_svg( $url, $name, $post_id = 0 ) {
140 $this->add_svg_import_filters();
141
142 return $this->import_image( $url, $name, $post_id );
143 }
144
145 /**
146 * WP core media filters for allowing svg upload.
147 */
148 private function add_svg_import_filters() {
149 add_filter(
150 'upload_mimes', function( $mimes ) {
151 $mimes['svg'] = 'image/svg+xml';
152 $mimes['svgz'] = 'image/svg+xml';
153 return $mimes;
154 }, 99
155 );
156
157 add_filter(
158 'wp_check_filetype_and_ext', function ( $checked, $file, $filename, $mimes ) {
159 if ( ! $checked['type'] ) {
160 $check_filetype = wp_check_filetype( $filename, $mimes );
161 $ext = $check_filetype['ext'];
162 $type = $check_filetype['type'];
163 $proper_filename = $filename;
164
165 if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
166 $type = false;
167 $ext = false;
168 }
169
170 $checked = compact( 'ext', 'type', 'proper_filename' );
171 }
172 return $checked;
173 }, 10, 4
174 );
175 }
176 }
177