| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: All plugins |
| 4 |
* Plugin URI: |
| 5 |
* |
| 6 |
* Compatibility Description: Upload image thumbnails generated by your theme and |
| 7 |
* plugins that do not register media objects with the media library. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace wpCloud\StatelessMedia { |
| 12 |
|
| 13 |
if(!class_exists('wpCloud\StatelessMedia\DynamicImageSupport')) { |
| 14 |
|
| 15 |
class DynamicImageSupport extends ICompatibility { |
| 16 |
protected $id = 'dynamic-image-support'; |
| 17 |
protected $title = 'Dynamic Image Support'; |
| 18 |
protected $constant = 'WP_STATELESS_MEDIA_ON_FLY'; |
| 19 |
protected $description = 'Upload image thumbnails generated by your theme and plugins that do not register media objects with the media library.'; |
| 20 |
protected $enabled = false; |
| 21 |
|
| 22 |
public function __construct(){ |
| 23 |
$modules = get_option('stateless-modules', array()); |
| 24 |
|
| 25 |
if (empty($modules[$this->id])) { |
| 26 |
// Lagecy settings |
| 27 |
$this->enabled = get_option('sm_on_fly', false); |
| 28 |
} |
| 29 |
|
| 30 |
$this->init(); |
| 31 |
} |
| 32 |
|
| 33 |
public function module_init($sm){ |
| 34 |
|
| 35 |
/** |
| 36 |
* Handle any other on fly generated media |
| 37 |
* 7d23984e Anton Korotkov, 2 years ago (February 19th, 2016 9:02am) new options added |
| 38 |
*/ |
| 39 |
add_filter('image_make_intermediate_size', array($this, 'handle_on_fly')); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Handle images on fly |
| 44 |
* f6a7dfd9 Anton Korotkov, 2 years ago (September 29th, 2015 6:09am) |
| 45 |
* @param $file |
| 46 |
* @return mixed |
| 47 |
*/ |
| 48 |
public function handle_on_fly( $file ) { |
| 49 |
|
| 50 |
$client = ud_get_stateless_media()->get_client(); |
| 51 |
$upload_dir = wp_upload_dir(); |
| 52 |
|
| 53 |
$file_path = str_replace(trailingslashit($upload_dir[ 'basedir' ]), '', $file); |
| 54 |
$file_info = @getimagesize($file); |
| 55 |
$mimeType = wp_check_filetype($file); |
| 56 |
|
| 57 |
if ($file_info) { |
| 58 |
$_metadata = array( |
| 59 |
'width' => $file_info[0], |
| 60 |
'height' => $file_info[1], |
| 61 |
'object-id' => 'unknown', // we really don't know it |
| 62 |
'source-id' => md5( $file . ud_get_stateless_media()->get( 'sm.bucket' ) ), |
| 63 |
'file-hash' => md5( $file ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
$client->add_media(apply_filters('sm:item:on_fly:before_add', array_filter(array( |
| 68 |
'name' => $file_path, |
| 69 |
'absolutePath' => wp_normalize_path($file), |
| 70 |
'cacheControl' => apply_filters('sm:item:cacheControl', 'public, max-age=36000, must-revalidate', $_metadata), |
| 71 |
'contentDisposition' => null, |
| 72 |
'mimeType' => $mimeType['type'], |
| 73 |
'metadata' => $_metadata |
| 74 |
)))); |
| 75 |
|
| 76 |
return $file; |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|