class-tiny-as3cf.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tiny_AS3CF |
| 5 | * Manages integration with WP Media Offload (aka Amazon S3 and CloudFront). |
| 6 | * |
| 7 | * Known issues with integration: |
| 8 | * - When as3cf removes local files, Tinify can't do compression on files that are |
| 9 | * already offloaded. Possible solutions are to download the remote file, compress |
| 10 | * We can use `tiny_image_after_compression` to possible remove the file after |
| 11 | * compression is done. A lot of functonality for Tinify is based on having the |
| 12 | * has to be changed in order to support remote files. |
| 13 | */ |
| 14 | class Tiny_AS3CF { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Tiny_Plugin $settings |
| 19 | * |
| 20 | * @var Tiny_Settings |
| 21 | */ |
| 22 | private $tiny_settings; |
| 23 | |
| 24 | /** |
| 25 | * Checks wether the lite version is active |
| 26 | */ |
| 27 | public static function lite_is_active() { |
| 28 | return class_exists( 'Amazon_S3_And_CloudFront' ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Checks wether the pro version is active |
| 33 | */ |
| 34 | public static function pro_is_active() { |
| 35 | return class_exists( 'Amazon_S3_And_CloudFront_Pro' ); |
| 36 | } |
| 37 | |
| 38 | public function __construct( $settings ) { |
| 39 | $this->tiny_settings = $settings; |
| 40 | $this->add_hooks(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Will verify if either the Lite or Pro version of AS3CF is active. |
| 45 | */ |
| 46 | public static function is_active() { |
| 47 | return Tiny_AS3CF::pro_is_active() || Tiny_AS3CF::lite_is_active(); |
| 48 | } |
| 49 | |
| 50 | public static function remove_local_files_setting_enabled() { |
| 51 | if ( ! Tiny_AS3CF::is_active() ) { |
| 52 | return false; |
| 53 | } |
| 54 | $settings = get_option( 'tantan_wordpress_s3' ); |
| 55 | if ( ! is_array( $settings ) ) { |
| 56 | return false; |
| 57 | } |
| 58 | return array_key_exists( 'remove-local-file', $settings ) && $settings['remove-local-file']; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Registers hooks required for the AS3CF integration. |
| 63 | */ |
| 64 | public function add_hooks() { |
| 65 | add_action( 'as3cf_pre_upload_object', array( $this, 'as3cf_before_offload' ), 10, 2 ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * handler for 'as3cf_pre_upload_object' action |
| 70 | * |
| 71 | * @see Tiny_Image->compress() |
| 72 | * |
| 73 | * Will handle file before file is possibly offloaded |
| 74 | * |
| 75 | * @param Item $as3cf_item |
| 76 | * @param array $args |
| 77 | */ |
| 78 | public function as3cf_before_offload( $as3cf_item, $args ) { |
| 79 | if ( ! $this->tiny_settings->auto_compress_enabled() ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $tiny_image = new Tiny_Image( $this->tiny_settings, $as3cf_item->source_id() ); |
| 84 | $result = $tiny_image->compress(); |
| 85 | } |
| 86 | } |
| 87 |