| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: WP Stateless |
| 4 |
* Plugin URI: https://wordpress.org/plugins/wp-stateless/ |
| 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' => 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT' ]; |
| 19 |
protected $description = 'Upload image thumbnails generated by your theme and plugins that do not register media objects with the media library. This could be significantly impact performance negatively.'; |
| 20 |
protected $first_party = true; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
$modules = get_option( 'stateless-modules', array() ); |
| 24 |
|
| 25 |
if( empty( $modules[ $this->id ] ) ) { |
| 26 |
// Legacy settings |
| 27 |
$this->enabled = get_option( 'sm_on_fly', false ); |
| 28 |
} |
| 29 |
|
| 30 |
$this->init(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @param $sm |
| 35 |
*/ |
| 36 |
public function module_init( $sm ) { |
| 37 |
/** |
| 38 |
* On Google App Engine not working |
| 39 |
*/ |
| 40 |
if ( isset($_SERVER["GAE_VERSION"]) ) return; |
| 41 |
/** |
| 42 |
* Handle any other on fly generated media |
| 43 |
* 7d23984e Anton Korotkov, 2 years ago (February 19th, 2016 9:02am) new options added |
| 44 |
*/ |
| 45 |
add_filter( 'image_make_intermediate_size', array( $this, 'handle_on_fly' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle images on fly |
| 50 |
* f6a7dfd9 Anton Korotkov, 2 years ago (September 29th, 2015 6:09am) |
| 51 |
* @param $file |
| 52 |
* @return mixed |
| 53 |
*/ |
| 54 |
public function handle_on_fly( $file ) { |
| 55 |
$client = ud_get_stateless_media()->get_client(); |
| 56 |
$upload_dir = wp_upload_dir(); |
| 57 |
|
| 58 |
$file_path = str_replace( trailingslashit( $upload_dir[ 'path' ] ), '', $file ); |
| 59 |
$file_info = @getimagesize( $file ); |
| 60 |
$mimeType = wp_check_filetype( $file ); |
| 61 |
|
| 62 |
if ($file_info) { |
| 63 |
$_metadata = array( |
| 64 |
'width' => $file_info[0], |
| 65 |
'height' => $file_info[1], |
| 66 |
'object-id' => 'unknown', // we really don't know it |
| 67 |
'source-id' => md5( $file . ud_get_stateless_media()->get( 'sm.bucket' ) ), |
| 68 |
'file-hash' => md5( $file ) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
$client->add_media(apply_filters('sm:item:on_fly:before_add', array_filter(array( |
| 73 |
'name' => $file_path, |
| 74 |
'absolutePath' => wp_normalize_path($file), |
| 75 |
'cacheControl' => apply_filters('sm:item:cacheControl', 'public, max-age=36000, must-revalidate', $_metadata), |
| 76 |
'contentDisposition' => null, |
| 77 |
'mimeType' => $mimeType['type'], |
| 78 |
'metadata' => $_metadata |
| 79 |
)))); |
| 80 |
|
| 81 |
return $file; |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|