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

175 lines 4.0 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 $parts = explode( '.', $media->file_name );
18 $name = array_shift( $parts );
19
20 $row = $wpdb->get_row(
21 $wpdb->prepare(
22 "SELECT * FROM $wpdb->posts
23 WHERE post_type = 'attachment'
24 AND post_name = %s",
25 $name
26 )
27 );
28
29 if ( $row ) {
30 $path = get_attached_file( $row->ID );
31 if ( $path && sha1_file( $path ) === $media->upload_hash ) {
32 return $row->ID;
33 }
34 }
35
36 // Check if we've already imported this file using the original sha1.
37 $row = $wpdb->get_row(
38 $wpdb->prepare(
39 "SELECT * FROM $wpdb->postmeta
40 WHERE meta_key = '_fl_asst_imported_media_hash'
41 AND meta_value = %s",
42 $media->upload_hash
43 )
44 );
45
46 return $row ? $row->post_id : null;
47 }
48
49 /**
50 * Import a cloud media item into the media library.
51 */
52 public function import_cloud_media( $media, $post_id = 0 ) {
53 $response = null;
54 $imported_id = $this->is_cloud_media_imported( $media );
55
56 if ( $imported_id ) {
57 return [
58 'id' => $imported_id,
59 'url' => wp_get_attachment_url( $imported_id ),
60 ];
61 }
62
63 switch ( $media->mime_type ) {
64 case 'image/jpg':
65 case 'image/jpeg':
66 case 'image/png':
67 case 'image/gif':
68 $response = $this->import_image( $media->url, $media->file_name, $post_id );
69 break;
70 case 'image/svg':
71 case 'image/svg+xml':
72 $response = $this->import_svg( $media->url, $media->file_name, $post_id );
73 break;
74 }
75
76 if ( $response && isset( $response['id'] ) ) {
77 update_post_meta( $response['id'], '_fl_asst_imported_media_hash', $media->upload_hash );
78 }
79
80 return $response;
81 }
82
83 /**
84 * Import an image into the media library.
85 */
86 public function import_image( $url, $name, $post_id = 0 ) {
87 require_once( ABSPATH . 'wp-admin/includes/image.php' );
88 require_once( ABSPATH . 'wp-admin/includes/file.php' );
89 require_once( ABSPATH . 'wp-admin/includes/media.php' );
90
91 $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
92 $tmp_path = wp_tempnam( $url_filename );
93
94 if ( ! $tmp_path ) {
95 return [ 'error' => __( 'Error creating temp file.' ) ];
96 }
97
98 $response = wp_remote_get(
99 $url,
100 [
101 'timeout' => 300,
102 'stream' => true,
103 'filename' => $tmp_path,
104 'sslverify' => false,
105 ]
106 );
107
108 $response_code = wp_remote_retrieve_response_code( $response );
109
110 if ( 200 !== intval( $response_code ) ) {
111 @unlink( $tmp_path );
112 return [ 'error' => __( 'Error downloading image file.' ) ];
113 }
114
115 $id = media_handle_sideload(
116 [
117 'name' => sanitize_file_name( $name ),
118 'tmp_name' => $tmp_path,
119 ],
120 $post_id
121 );
122
123 if ( is_wp_error( $id ) ) {
124 @unlink( $tmp_path );
125 return [ 'error' => __( 'Error importing image file.' ) ];
126 }
127
128 return [
129 'id' => $id,
130 'url' => wp_get_attachment_url( $id ),
131 ];
132 }
133
134 /**
135 * Import an svg file into the media library.
136 */
137 public function import_svg( $url, $name, $post_id = 0 ) {
138 $this->add_svg_import_filters();
139
140 return $this->import_image( $url, $name, $post_id );
141 }
142
143 /**
144 * WP core media filters for allowing svg upload.
145 */
146 private function add_svg_import_filters() {
147 add_filter(
148 'upload_mimes', function( $mimes ) {
149 $mimes['svg'] = 'image/svg+xml';
150 $mimes['svgz'] = 'image/svg+xml';
151 return $mimes;
152 }, 99
153 );
154
155 add_filter(
156 'wp_check_filetype_and_ext', function ( $checked, $file, $filename, $mimes ) {
157 if ( ! $checked['type'] ) {
158 $check_filetype = wp_check_filetype( $filename, $mimes );
159 $ext = $check_filetype['ext'];
160 $type = $check_filetype['type'];
161 $proper_filename = $filename;
162
163 if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
164 $type = false;
165 $ext = false;
166 }
167
168 $checked = compact( 'ext', 'type', 'proper_filename' );
169 }
170 return $checked;
171 }, 10, 4
172 );
173 }
174 }
175