├── LICENSE ├── README.md ├── wp-cli.phar └── wp-jit-image-sizes.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kurtis Shaner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JIT Image Sizes 2 | 3 | JIT (Just in Time) Image Sizes is a WordPress Plugin that tell WordPress not to create a thumbnail for every size that is registered, but only create it whenever it is requested by the front end of the site. 4 | 5 | When thumbnails are (re) generated via upload, another plugin or wp-cli, they are actually deleted and re-generated again the next time they are requested. This is ideal for large sites with a large amount of image assets in the media library. 6 | 7 | **This plugin is still in development and is not recommended for use on production sites** 8 | -------------------------------------------------------------------------------- /wp-cli.phar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wdgdc/wp-jit-image-size/eef1663a8b4e7aeaf8d31e22a3c680ed953a86e9/wp-cli.phar -------------------------------------------------------------------------------- /wp-jit-image-sizes.php: -------------------------------------------------------------------------------- 1 | whitelist = get_option('wp-jit-image-size-whitelist'); 35 | if (is_array($this->whitelist)) $this->whitelist = array_keys($this->whitelist); 36 | } 37 | 38 | public function admin_init() { 39 | add_action('wp_ajax_jit_image_sizes_thumbnail_delete_count', array($this, 'wp_ajax_jit_image_sizes_thumbnail_delete_count')); 40 | add_action('wp_ajax_jit_image_sizes_thumbnail_delete', array($this, 'wp_ajax_jit_image_sizes_thumbnail_delete')); 41 | add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2); 42 | 43 | add_settings_section('wp-jit-image-size-settings', '', null, 'wp-jit-image-sizes'); 44 | register_setting('wp-jit-image-sizes', 'wp-jit-image-size-whitelist'); 45 | add_settings_field('wp-jit-image-size-whitelist', 'Whitelist Image Sizes to Generate', array($this, 'setting_wp_jit_image_size_whitelist'), 'wp-jit-image-sizes', 'wp-jit-image-size-settings'); 46 | // add_settings_field('wp-jit-image-size-delete-thumbs', 'Delete All Thumbnails', array($this, 'setting_wp_jit_image_size_delete_thumbs'), 'wp-jit-image-sizes', 'wp-jit-image-size-settings'); 47 | } 48 | 49 | public function setting_wp_jit_image_size_delete_thumbs() { ?> 50 |
51 | 134 | 140 | 154 | 171 |
172 |

JIT Image Sizes

173 | 174 |
175 | 179 |

180 | 181 | 182 | 183 |

184 |

PHP Exec support is needed to delete all thumbnails

185 |
186 | 187 | 188 | 189 | 190 | 191 | 192 |

193 | setting_wp_jit_image_size_delete_thumbs(); 195 | ?> 196 |
197 |
198 | &1'; 205 | exec($cmd, $ret); 206 | array_shift($ret); 207 | echo json_encode($ret); 208 | exit; 209 | } 210 | 211 | public function wp_ajax_jit_image_sizes_thumbnail_delete_count() { 212 | 213 | check_ajax_referer('wp-jit-image-sizes-count', 'nonce'); 214 | 215 | $json = array(); 216 | 217 | $whitelist = (isset($_POST['wp-jit-image-size-whitelist']) && !empty($_POST['wp-jit-image-size-whitelist'])) ? $_POST['wp-jit-image-size-whitelist'] : ''; 218 | update_option('wp-jit-image-size-whitelist', $whitelist); 219 | $json['whitelist'] = $whitelist; 220 | $json['nonce'] = wp_create_nonce('wp-jit-image-size-delete-thumbs'); 221 | 222 | $query = new WP_Query(array( 223 | 'post_type' => 'attachment', 224 | 'post_mime_type' => 'image', 225 | 'post_status' => 'any', 226 | 'posts_per_page' => -1, 227 | 'fields' => 'ids' 228 | )); 229 | 230 | $json['count'] = $query->post_count; 231 | $json['ids'] = $query->posts; 232 | 233 | echo json_encode($json); 234 | exit; 235 | } 236 | 237 | public function plugin_action_links($links, $file) { 238 | if (basename($file) === basename(__FILE__)) { 239 | $links['settings'] = 'Settings'; 240 | } 241 | return $links; 242 | } 243 | 244 | public function intermediate_image_sizes_advanced($sizes) { 245 | 246 | if (!isset($this->whitelist[$this->jit_size]) && isset($sizes[$this->jit_size])) { 247 | $this->jit_sizes[$this->jit_size] = $sizes[$this->jit_size]; 248 | } 249 | 250 | if (!empty($this->whitelist)){ 251 | foreach($this->whitelist as $whitelist_size) { 252 | $this->whitelist_sizes[$whitelist_size] = $sizes[$whitelist_size]; 253 | } 254 | } 255 | 256 | if (!empty($this->jit_size) && isset($sizes[$this->jit_size])) { 257 | $this->jit_sizes = array_merge( 258 | $this->jit_sizes, 259 | array( 260 | $this->jit_size => $sizes[$this->jit_size] 261 | ) 262 | ); 263 | 264 | $this->jit_metadata['sizes'] = $this->jit_sizes; 265 | return $this->jit_sizes; 266 | } 267 | 268 | return $this->whitelist_sizes; 269 | } 270 | 271 | public function image_downsize($out, $id, $size) { 272 | if (!is_string($size)) { 273 | return $out; 274 | } 275 | 276 | $this->jit_metadata = wp_get_attachment_metadata( $id ); 277 | $this->jit_size = $size; 278 | $this->jit_sizes = $this->jit_metadata['sizes']; 279 | 280 | if (!isset($this->jit_sizes[$this->jit_size])) { 281 | include_once(ABSPATH . 'wp-admin/includes/image.php'); 282 | $new_meta = wp_generate_attachment_metadata($id, get_attached_file($id)); 283 | update_post_meta($id, '_wp_attachment_metadata', $new_meta); 284 | } 285 | 286 | unset($this->jit_metadata, $this->jit_size, $this->jit_sizes); 287 | } 288 | } 289 | 290 | WP_JIT_Image_Size::instance(); 291 | --------------------------------------------------------------------------------