config
9 years ago
css
9 years ago
data
9 years ago
images
9 years ago
js
9 years ago
vendor
9 years ago
views
9 years ago
class-tiny-compress-client.php
9 years ago
class-tiny-compress-fopen.php
9 years ago
class-tiny-compress.php
9 years ago
class-tiny-exception.php
9 years ago
class-tiny-image-size.php
9 years ago
class-tiny-image.php
9 years ago
class-tiny-notices.php
9 years ago
class-tiny-php.php
9 years ago
class-tiny-plugin.php
9 years ago
class-tiny-settings.php
9 years ago
class-tiny-wp-base.php
9 years ago
class-tiny-image-size.php
151 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2017 Voormedia B.V. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | class Tiny_Image_Size { |
| 22 | public $filename; |
| 23 | public $meta = array(); |
| 24 | |
| 25 | /* Used more than once and not trivial, so we are memoizing these */ |
| 26 | private $_exists; |
| 27 | private $_file_size; |
| 28 | private $_duplicate = false; |
| 29 | private $_duplicate_of_size = ''; |
| 30 | |
| 31 | public function __construct( $filename = null ) { |
| 32 | $this->filename = $filename; |
| 33 | } |
| 34 | |
| 35 | public function end_time() { |
| 36 | if ( isset( $this->meta['end'] ) ) { |
| 37 | return $this->meta['end']; |
| 38 | } elseif ( isset( $this->meta['timestamp'] ) ) { |
| 39 | return $this->meta['timestamp']; |
| 40 | } else { |
| 41 | return null; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public function add_tiny_meta_start() { |
| 46 | $this->meta = array( 'start' => time() ); |
| 47 | } |
| 48 | |
| 49 | public function add_tiny_meta( $response ) { |
| 50 | if ( isset( $this->meta['start'] ) ) { |
| 51 | $this->meta = $response; |
| 52 | $this->meta['end'] = time(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public function add_tiny_meta_error( $exception ) { |
| 57 | if ( isset( $this->meta['start'] ) ) { |
| 58 | $this->meta = array( |
| 59 | 'error' => $exception->get_type(), |
| 60 | 'message' => $exception->get_message(), |
| 61 | 'timestamp' => time(), |
| 62 | ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function has_been_compressed() { |
| 67 | return isset( $this->meta['output'] ); |
| 68 | } |
| 69 | |
| 70 | public function never_compressed() { |
| 71 | return ! $this->has_been_compressed(); |
| 72 | } |
| 73 | |
| 74 | public function filesize() { |
| 75 | if ( is_null( $this->_file_size ) ) { |
| 76 | if ( $this->exists() ) { |
| 77 | $this->_file_size = filesize( $this->filename ); |
| 78 | } else { |
| 79 | $this->_file_size = 0; |
| 80 | } |
| 81 | } |
| 82 | return $this->_file_size; |
| 83 | } |
| 84 | |
| 85 | public function exists() { |
| 86 | if ( is_null( $this->_exists ) ) { |
| 87 | $this->_exists = $this->filename && file_exists( $this->filename ); |
| 88 | } |
| 89 | return $this->_exists; |
| 90 | } |
| 91 | |
| 92 | private function same_size() { |
| 93 | return ( $this->filesize() == $this->meta['output']['size'] ); |
| 94 | } |
| 95 | |
| 96 | public function still_exists() { |
| 97 | return $this->has_been_compressed() && $this->exists(); |
| 98 | } |
| 99 | |
| 100 | public function missing() { |
| 101 | return $this->has_been_compressed() && ! $this->exists(); |
| 102 | } |
| 103 | |
| 104 | public function compressed() { |
| 105 | return $this->still_exists() && $this->same_size(); |
| 106 | } |
| 107 | |
| 108 | public function modified() { |
| 109 | return $this->still_exists() && ! $this->same_size(); |
| 110 | } |
| 111 | |
| 112 | public function uncompressed() { |
| 113 | return $this->exists() && |
| 114 | ! $this->is_duplicate() && |
| 115 | ! ( isset( $this->meta['output'] ) && $this->same_size() ); |
| 116 | } |
| 117 | |
| 118 | public function in_progress() { |
| 119 | return $this->recently_started() && ! isset( $this->meta['output'] ); |
| 120 | } |
| 121 | |
| 122 | public function resized() { |
| 123 | return ( |
| 124 | isset( $this->meta['output'] ) && |
| 125 | isset( $this->meta['output']['resized'] ) && |
| 126 | $this->meta['output']['resized'] |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | public function mark_duplicate( $duplicate_size_name ) { |
| 131 | $this->_duplicate = true; |
| 132 | $this->_duplicate_of_size = $duplicate_size_name; |
| 133 | } |
| 134 | |
| 135 | public function is_duplicate() { |
| 136 | return $this->_duplicate; |
| 137 | } |
| 138 | |
| 139 | public function duplicate_of_size() { |
| 140 | return $this->_duplicate_of_size; |
| 141 | } |
| 142 | |
| 143 | private function recently_started() { |
| 144 | $thirty_minutes_ago = date( 'U' ) - ( 60 * 30 ); |
| 145 | return ( |
| 146 | isset( $this->meta['start'] ) && |
| 147 | $this->meta['start'] > $thirty_minutes_ago |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 |