1 | class ManagedWPHostingRegenerateThumbnails_Later { |
---|
2 | public $fullsizepath; |
---|
3 | private $thumbslaterkey = 'mwp_thumbslater'; |
---|
4 | private $batch_limit = 1; |
---|
5 | |
---|
6 | public function __construct( ) { |
---|
7 | global $wpdb; |
---|
8 | |
---|
9 | if( (isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) && ! isset( $_POST['post_id'] ) ) { |
---|
10 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'intermediate_image_sizes_advanced' ), 999, 2 ); |
---|
11 | add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_generate_attachment_metadata' ), 999, 2 ); |
---|
12 | } else { |
---|
13 | $sql = $wpdb->prepare( 'SELECT post_id FROM '. $wpdb->postmeta . ' WHERE meta_key = %s LIMIT %d', $this->thumbslaterkey, $this->batch_limit ); |
---|
14 | |
---|
15 | foreach( $wpdb->get_results( $sql ) as $thumbs_later ) { |
---|
16 | if( $this->init( $thumbs_later->post_id ) ) { |
---|
17 | delete_post_meta( $thumbs_later->post_id, $this->thumbslaterkey ); |
---|
18 | } |
---|
19 | } |
---|
20 | } |
---|
21 | } |
---|
22 | |
---|
23 | public function intermediate_image_sizes_advanced( $sizes, $metadata ) { |
---|
24 | define( 'MWP_THUMBSLATER_FLAG', true ); return array( ); |
---|
25 | } |
---|
26 | |
---|
27 | public function wp_generate_attachment_metadata ( $metadata, $attachment_id ) { |
---|
28 | if( defined( 'MWP_THUMBSLATER_FLAG' ) || ( isset( $metadata['sizes'] ) && $metadata['sizes'] == array () ) ) { |
---|
29 | add_post_meta( $attachment_id, $this->thumbslaterkey, true ); |
---|
30 | } |
---|
31 | return $metadata; |
---|
32 | } |
---|
33 | |
---|
34 | private function init( $attachment_id = 0 ) { |
---|
35 | if( !$attachment_id ) { |
---|
36 | return; |
---|
37 | } |
---|
38 | |
---|
39 | $attachment = get_post( $attachment_id ); |
---|
40 | if ( ! $attachment ) { |
---|
41 | return new WP_Error( 'regenerate_thumbnails_regenerator_attachment_doesnt_exist', __( 'No attachment exists with that ID.', 'regenerate-thumbnails' ), array( 'status' => 404, ) ); |
---|
42 | } |
---|
43 | |
---|
44 | if ( 'attachment' !== get_post_type( $attachment ) ) { |
---|
45 | return new WP_Error( 'regenerate_thumbnails_regenerator_not_attachment', __( 'This item is not an attachment.', 'regenerate-thumbnails' ), array( 'status' => 400, ) ); |
---|
46 | } |
---|
47 | |
---|
48 | if ( self::is_site_icon( $attachment ) ) { |
---|
49 | return new WP_Error( 'regenerate_thumbnails_regenerator_is_site_icon', __( "This attachment is a site icon and therefore the thumbnails shouldn't be touched.", 'regenerate-thumbnails' ), array( 'status' => 415, 'attachment' => $attachment, ) ); |
---|
50 | } |
---|
51 | $this->attachment = $attachment; |
---|
52 | return $this->regenerate( ); |
---|
53 | } |
---|
54 | |
---|
55 | private function get_fullsizepath() { |
---|
56 | if ( $this->fullsizepath ) { |
---|
57 | return $this->fullsizepath; |
---|
58 | } |
---|
59 | |
---|
60 | $this->fullsizepath = get_attached_file( $this->attachment->ID ); |
---|
61 | |
---|
62 | if ( false === $this->fullsizepath || ! file_exists( $this->fullsizepath ) ) { |
---|
63 | $error = new WP_Error( 'regenerate_thumbnails_regenerator_file_not_found', sprintf( __( "The fullsize image file cannot be found in your uploads directory at <code>%s</code>. Without it, new thumbnail images can't be generated.", 'regenerate-thumbnails' ), _wp_relative_upload_path( $this->fullsizepath ) ), array( 'status' => 404, 'fullsizepath' => _wp_relative_upload_path( $this->fullsizepath ), 'attachment' => $this->attachment, ) ); |
---|
64 | |
---|
65 | $this->fullsizepath = $error; |
---|
66 | } |
---|
67 | return $this->fullsizepath; |
---|
68 | } |
---|
69 | |
---|
70 | private static function is_site_icon( WP_Post $attachment ) { |
---|
71 | return ( 'site-icon' === get_post_meta( $attachment->ID, '_wp_attachment_context', true ) ); |
---|
72 | } |
---|
73 | |
---|
74 | private function regenerate( $args = array() ) { |
---|
75 | $args = wp_parse_args( $args, array( ) ); |
---|
76 | $fullsizepath = $this->get_fullsizepath(); |
---|
77 | if ( is_wp_error( $fullsizepath ) ) { |
---|
78 | $fullsizepath->add_data( array( 'attachment' => $this->attachment ) ); |
---|
79 | return $fullsizepath; |
---|
80 | } |
---|
81 | |
---|
82 | $old_metadata = wp_get_attachment_metadata( $this->attachment->ID ); |
---|
83 | require_once( ABSPATH . 'wp-admin/includes/admin.php' ); |
---|
84 | $new_metadata = wp_generate_attachment_metadata( $this->attachment->ID, $fullsizepath ); |
---|
85 | wp_update_attachment_metadata( $this->attachment->ID, $new_metadata ); |
---|
86 | return $new_metadata; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | function ManagedWPHostingRegenerateThumbnails_Later() { |
---|
91 | $ManagedWPHostingRegenerateThumbnails_Later = new ManagedWPHostingRegenerateThumbnails_Later(); |
---|
92 | } |
---|