PluginProbe
WP-Stateless – Google Cloud Storage / 1.9.0
WP-Stateless – Google Cloud Storage v1.9.0
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-utility.php

class-utility.php in WP-Stateless – Google Cloud Storage 1.9.0, at lib/classes/class-utility.php

298 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper Functions List
4 *
5 * Can be called via Singleton. Since Singleton uses magic method __call().
6 * Example:
7 *
8 * Add Media to GS storage:
9 * ud_get_stateless_media()->add_media( false, $post_id );
10 *
11 * @class Utility
12 */
13 namespace wpCloud\StatelessMedia {
14
15 if( !class_exists( 'wpCloud\StatelessMedia\Utility' ) ) {
16
17 class Utility {
18
19 /**
20 * Override Cache Control
21 * @param $cacheControl
22 * @return mixed
23 */
24 public static function override_cache_control( $cacheControl ) {
25 return ud_get_stateless_media()->get( 'sm.cache_control' );
26 }
27
28 /**
29 * wp_normalize_path was added in 3.9.0
30 *
31 * @param $path
32 * @return mixed|string
33 *
34 */
35 public static function normalize_path( $path ) {
36
37 if( function_exists( 'wp_normalize_path' ) ) {
38 return wp_normalize_path( $path );
39 }
40
41 $path = str_replace( '\\', '/', $path );
42 $path = preg_replace( '|/+|','/', $path );
43 return $path;
44
45 }
46
47 /**
48 * Randomize file name
49 * @param $filename
50 * @return string
51 */
52 public static function randomize_filename( $filename ) {
53 $info = pathinfo($filename);
54 $ext = empty($info['extension']) ? '' : '' . $info['extension'];
55 $_parts = array();
56 if (strpos($info['filename'], '@')) {
57 $_cleanName = explode('@', $info['filename'])[0];
58 $_retna = explode('@', $info['filename'])[1];
59 $_parts[] = substr(md5(time()), 0, 8);
60 $_parts[] = '-';
61 $_parts[] = strtolower($_cleanName);
62 $_parts[] = '@' . strtolower($_retna);
63 } else {
64 $_parts[] = substr(md5(time()), 0, 8);
65 $_parts[] = '-';
66 $_parts[] = strtolower($info['filename']);
67 }
68 return join('', $_parts) . '.' . $ext;
69 }
70
71 /**
72 * Get Media Item Content Disposition
73 *
74 * @param null $attachment_id
75 * @param array $metadata
76 * @param array $data
77 * @return string
78 */
79 public static function getContentDisposition( $attachment_id = null, $metadata = array(), $data = array() ) {
80 // return 'Content-Disposition: attachment; filename=some-file.sql';
81
82 return apply_filters( 'sm:item:contentDisposition', null, array( 'attachment_id' => $attachment_id, 'mime_type' => get_post_mime_type( $attachment_id ), 'metadata' => $metadata, 'data' => $data ) );
83
84 }
85
86 /**
87 * @param null $attachment_id
88 * @param array $metadata
89 * @param array $data
90 * @return string
91 */
92 public static function getCacheControl( $attachment_id = null, $metadata = array(), $data = array() ) {
93
94 if( !$attachment_id ) {
95 return apply_filters( 'sm:item:cacheControl', 'private, no-cache, no-store', $attachment_id, array( 'attachment_id' => null, 'mime_type' => null, 'metadata' => $metadata, 'data' => $data ) );
96 }
97
98 $_mime_type = get_post_mime_type( $attachment_id );
99
100 // Treat images as public.
101 if( strpos( $_mime_type, 'image/' ) !== false ) {
102 return apply_filters( 'sm:item:cacheControl', 'public, max-age=36000, must-revalidate', array( 'attachment_id' => $attachment_id, 'mime_type' => null, 'metadata' => $metadata, 'data' => $data ) );
103 }
104
105 // Treat images as public.
106 if( strpos( $_mime_type, 'sql' ) !== false ) {
107 return apply_filters( 'sm:item:cacheControl', 'private, no-cache, no-store', array( 'attachment_id' => $attachment_id, 'mime_type' => null, 'metadata' => $metadata, 'data' => $data ) );
108 }
109
110 return apply_filters( 'sm:item:cacheControl', 'public, max-age=30, no-store, must-revalidate', array( 'attachment_id' => $attachment_id, 'mime_type' => null, 'metadata' => $metadata, 'data' => $data ) );
111
112 }
113
114
115 /**
116 * Add/Update Media to Bucket
117 * Fired for every action with image add or update
118 *
119 * @action wp_generate_attachment_metadata
120 * @author peshkov@UD
121 * @param $metadata
122 * @param $attachment_id
123 * @return bool|string
124 */
125 public static function add_media( $metadata, $attachment_id ) {
126
127 /* Get metadata in case if method is called directly. */
128 if( current_filter() !== 'wp_generate_attachment_metadata' ) {
129 $metadata = wp_get_attachment_metadata( $attachment_id );
130 }
131
132 $client = ud_get_stateless_media()->get_client();
133
134 if( !is_wp_error( $client ) ) {
135
136 // Make non-images uploadable.
137 if( empty( $metadata['file'] ) && $attachment_id ) {
138 $upload_dir = wp_upload_dir();
139 $metadata = array( "file" => str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', get_attached_file( $attachment_id ) ) );
140 }
141
142 $file = wp_normalize_path( $metadata[ 'file' ] );
143
144 $bucketLink = 'https://storage.googleapis.com/' . ud_get_stateless_media()->get( 'sm.bucket' );
145
146 $_metadata = array(
147 "width" => isset( $metadata[ 'width' ] ) ? $metadata[ 'width' ] : null,
148 "height" => isset( $metadata[ 'height' ] ) ? $metadata[ 'height' ] : null,
149 'object-id' => $attachment_id,
150 'source-id' => md5( $attachment_id.ud_get_stateless_media()->get( 'sm.bucket' ) ),
151 'file-hash' => md5( $metadata[ 'file' ] )
152 );
153
154 /* Add default image */
155 $media = $client->add_media( $_mediaOptions = array_filter( array(
156 'name' => $file,
157 'absolutePath' => wp_normalize_path( get_attached_file( $attachment_id ) ),
158 'cacheControl' => $_cacheControl = self::getCacheControl( $attachment_id, $metadata, null ),
159 'contentDisposition' => $_contentDisposition = self::getContentDisposition( $attachment_id, $metadata, null ),
160 'mimeType' => get_post_mime_type( $attachment_id ),
161 'metadata' => $_metadata
162 ) ));
163
164 // Break if we have errors.
165 // @note Errors could be due to key being invalid or now having sufficient permissions in which case should notify user.
166 if( is_wp_error( $media ) ) {
167 return $metadata;
168 }
169
170 /* Add Google Storage metadata to our attachment */
171 $fileLink = $bucketLink . '/' . ( !empty($media['name']) ? $media['name'] : $file );
172
173 $cloud_meta = array(
174 'id' => $media[ 'id' ],
175 'name' => !empty($media['name']) ? $media['name'] : $file,
176 'fileLink' => $fileLink,
177 'storageClass' => $media[ 'storageClass' ],
178 'mediaLink' => $media[ 'mediaLink' ],
179 'selfLink' => $media[ 'selfLink' ],
180 'bucket' => ud_get_stateless_media()->get( 'sm.bucket' ),
181 'object' => $media,
182 'sizes' => array(),
183 );
184
185 if( isset( $_cacheControl ) && $_cacheControl ) {
186 //update_post_meta( $attachment_id, 'sm_cloud:cacheControl', $_cacheControl );
187 $cloud_meta[ 'cacheControl' ] = $_cacheControl;
188 }
189
190 if( isset( $_contentDisposition ) && $_contentDisposition ) {
191 //update_post_meta( $attachment_id, 'sm_cloud:contentDisposition', $_contentDisposition );
192 $cloud_meta[ 'contentDisposition' ] = $_contentDisposition;
193 }
194
195 if( empty( $metadata[ 'sizes' ] ) ) {
196 // @note This could happen if WordPress does not have any wp_get_image_editor(), e.g. Imagemagic not installed.
197 }
198
199 /* Now we go through all available image sizes and upload them to Google Storage */
200 if( !empty( $metadata[ 'sizes' ] ) && is_array( $metadata[ 'sizes' ] ) ) {
201
202 $path = wp_normalize_path( dirname( get_attached_file( $attachment_id ) ) );
203 $mediaPath = wp_normalize_path( trim( str_replace( basename( $metadata[ 'file' ] ), '', $metadata[ 'file' ] ), '\/\\' ) );
204
205 foreach( (array) $metadata[ 'sizes' ] as $image_size => $data ) {
206
207 $absolutePath = wp_normalize_path( $path . '/' . $data[ 'file' ] );
208
209 /* Add 'image size' image */
210 $media = $client->add_media( array(
211 'name' => $file_path = $mediaPath . '/' . $data[ 'file' ],
212 'absolutePath' => $absolutePath,
213 'cacheControl' => $_cacheControl,
214 'contentDisposition' => $_contentDisposition,
215 'mimeType' => $data[ 'mime-type' ],
216 'metadata' => array_merge( $_metadata, array(
217 'width' => $data['width'],
218 'height' => $data['height'],
219 'child-of' => $attachment_id,
220 'file-hash' => md5( $data[ 'file' ] )
221 ))
222 ));
223
224 /* Break if we have errors. */
225 if( !is_wp_error( $media ) ) {
226
227 $fileLink = $bucketLink . '/' . (!empty($media['name']) ? $media['name'] : $file_path);
228
229 // @note We don't add storageClass because it's same as parent...
230 $cloud_meta[ 'sizes' ][ $image_size ] = array(
231 'id' => $mediaPath . '/' . $media[ 'id' ],
232 'name' => !empty($media['name']) ? $media['name'] : $file_path,
233 'fileLink' => $fileLink,
234 'mediaLink' => $media[ 'mediaLink' ],
235 'selfLink' => $media[ 'selfLink' ]
236 );
237
238 }
239
240 }
241
242 }
243
244 update_post_meta( $attachment_id, 'sm_cloud', $cloud_meta );
245
246 }
247
248 return $metadata;
249 }
250
251 /**
252 * Remove Media from Bucket by post ID
253 * Fired on calling function wp_delete_attachment()
254 *
255 * @todo: add error logging. peshkov@UD
256 * @see wp_delete_attachment()
257 * @action delete_attachment
258 * @author peshkov@UD
259 * @param $post_id
260 */
261 public static function remove_media( $post_id ) {
262 /* Get attahcment's metadata */
263 $metadata = wp_get_attachment_metadata( $post_id );
264
265 /* Be sure we have the same bucket in settings and have GS object's name before proceed. */
266 if(
267 isset( $metadata[ 'gs_name' ] ) &&
268 isset( $metadata[ 'gs_bucket' ] ) &&
269 $metadata[ 'gs_bucket' ] == ud_get_stateless_media()->get( 'sm.bucket' )
270 ) {
271
272 $client = ud_get_stateless_media()->get_client();
273 if( !is_wp_error( $client ) ) {
274
275 /* Remove default image */
276 $client->remove_media( $metadata[ 'gs_name' ] );
277
278 /* Now, go through all sizes and remove 'image sizes' images from Bucket too. */
279 if( !empty( $metadata[ 'sizes' ] ) && is_array( $metadata[ 'sizes' ] ) ) {
280 foreach( $metadata[ 'sizes' ] as $k => $v ) {
281 if( !empty( $v[ 'gs_name' ] ) ) {
282 $client->remove_media( $v[ 'gs_name' ] );
283 }
284 }
285 }
286
287 }
288
289 }
290
291 }
292
293 }
294
295 }
296
297 }
298