PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
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-dynamic-image-support.php

class-dynamic-image-support.php in WP-Stateless – Google Cloud Storage trunk, at lib/classes/class-dynamic-image-support.php

89 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Dynamic image support
5 *
6 * Upload image thumbnails generated by your theme and
7 * plugins that do not register media objects with the media library.
8 *
9 * @since 3.3.0
10 */
11
12 namespace wpCloud\StatelessMedia {
13 if (!class_exists('wpCloud\StatelessMedia\DynamicImageSupport')) {
14
15 /**
16 * Class DynamicImageSupport
17 *
18 * @package wpCloud\StatelessMedia
19 */
20 class DynamicImageSupport {
21 use Singleton;
22
23 const ID = 'dynamic-image-support';
24
25 public function __construct() {
26 // On Google App Engine not working
27 if ( apply_filters('wp_stateless_is_app_engine', false) ) {
28 return;
29 }
30
31 $enabled = ud_get_stateless_media()->get( 'sm.dynamic_image_support', false ) == 'true';
32
33 // Legacy setting
34 if (!$enabled) {
35 $enabled = get_option('sm_on_fly', false);
36 }
37
38 if ($enabled) {
39 $this->init_module();
40 }
41 }
42
43 /**
44 * Initialize module
45 */
46 private function init_module() {
47 // Handle any other on fly generated media
48 add_filter('image_make_intermediate_size', array($this, 'handle_on_fly'));
49 }
50
51 /**
52 * Handle images on fly
53 * f6a7dfd9 Anton Korotkov, 2 years ago (September 29th, 2015 6:09am)
54 * @param $file
55 * @return mixed
56 */
57 public function handle_on_fly($file) {
58 $client = ud_get_stateless_media()->get_client();
59 $upload_dir = wp_upload_dir();
60
61 $file_path = str_replace(trailingslashit($upload_dir['path']), '', $file);
62 $file_info = @getimagesize($file);
63 $mimeType = wp_check_filetype($file);
64
65 if ($file_info) {
66 $_metadata = array(
67 'width' => $file_info[0],
68 'height' => $file_info[1],
69 'object-id' => 'unknown', // we really don't know it
70 'source-id' => md5($file . ud_get_stateless_media()->get('sm.bucket')),
71 'file-hash' => md5($file)
72 );
73 }
74
75 $client->add_media(apply_filters('sm:item:on_fly:before_add', array_filter(array(
76 'name' => $file_path,
77 'absolutePath' => wp_normalize_path($file),
78 'cacheControl' => apply_filters('sm:item:cacheControl', ud_get_stateless_media()->get_default_cache_control(), $_metadata),
79 'contentDisposition' => null,
80 'mimeType' => $mimeType['type'],
81 'metadata' => $_metadata
82 ))));
83
84 return $file;
85 }
86 }
87 }
88 }
89