Skip to content

Commit

Permalink
Lock an attachment to prevent multiple queue jobs being created
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Nov 12, 2017
1 parent 0378135 commit 985a4ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Image_Processing_Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public function filter_update_post_metadata( $check, $object_id, $meta_key, $met
* @param array $sizes
*/
protected function process_image( $post_id, $sizes ) {
if ( self::is_attachment_locked( $post_id ) ) {
return;
}

$lock_attachment = false;

foreach ( $sizes as $size ) {
if ( self::does_size_already_exist_for_image( $post_id, $size ) ) {
continue;
Expand All @@ -117,6 +123,12 @@ protected function process_image( $post_id, $sizes ) {
);

wp_queue()->push( new Resize_Job( $item ) );

$lock_attachment = true;
}

if ( $lock_attachment ) {
self::lock_attachment( $post_id );
}
}

Expand Down Expand Up @@ -250,4 +262,34 @@ public static function is_size_larger_than_original( $post_id, $size ) {

return false;
}

/**
* Is an attachment locked?
*
* @param int $post_id
*
* @return bool
*/
public static function is_attachment_locked( $post_id ) {
$image_meta = self::get_image_meta( $post_id );

if ( isset( $image_meta['ipq_locked'] ) && $image_meta['ipq_locked'] ) {
return true;
}

return false;
}

/**
* Lock an attachment to prevent multiple queue jobs being created.
*
* @param int $post_id
*/
public static function lock_attachment( $post_id ) {
$image_meta = self::get_image_meta( $post_id );

$image_meta['ipq_locked'] = true;

wp_update_attachment_metadata( $post_id, $image_meta );
}
}
2 changes: 2 additions & 0 deletions src/Image_Processing_Queue/Resize_Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function handle() {
'height' => $resized_file['height'],
'mime-type' => $resized_file['mime-type'],
);

unset( $image_meta['ipq_locked'] );
wp_update_attachment_metadata( $post_id, $image_meta );
}

Expand Down

0 comments on commit 985a4ce

Please sign in to comment.