| 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 |
|