-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace background processing with queueing library
- Loading branch information
1 parent
5527baa
commit cbe751f
Showing
5 changed files
with
95 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"require": { | ||
"a5hleyrich/wp-background-processing": "^1.0" | ||
"php": ">=5.3.0", | ||
"a5hleyrich/wp-queue": "^1.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
use WP_Queue\Job; | ||
|
||
class Image_Processing_Job extends Job { | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $image; | ||
|
||
/** | ||
* Image_Processing_Job constructor. | ||
*/ | ||
public function __construct( $image ) { | ||
$this->image = $image; | ||
} | ||
|
||
/** | ||
* Handle job logic. | ||
*/ | ||
public function handle() { | ||
$item = wp_parse_args( $item, array( | ||
'post_id' => 0, | ||
'width' => 0, | ||
'height' => 0, | ||
'crop' => false, | ||
) ); | ||
|
||
$post_id = $item['post_id']; | ||
$width = $item['width']; | ||
$height = $item['height']; | ||
$crop = $item['crop']; | ||
|
||
if ( ! $width && ! $height ) { | ||
throw new IPQ_Process_Exception( "Invalid dimensions '{$width}x{$height}'" ); | ||
} | ||
|
||
if ( Image_Processing_Queue::does_size_already_exist_for_image( $post_id, array( $width, $height, $crop ) ) ) { | ||
return false; | ||
} | ||
|
||
$image_meta = Image_Processing_Queue::get_image_meta( $post_id ); | ||
|
||
if ( ! $image_meta ) { | ||
return false; | ||
} | ||
|
||
add_filter( 'as3cf_get_attached_file_copy_back_to_local', '__return_true' ); | ||
$img_path = Image_Processing_Queue::get_image_path( $post_id ); | ||
|
||
if ( ! $img_path ) { | ||
return false; | ||
} | ||
|
||
$editor = wp_get_image_editor( $img_path ); | ||
|
||
if ( is_wp_error( $editor ) ) { | ||
throw new IPQ_Process_Exception( 'Unable to get WP_Image_Editor for file "' . $img_path . '": ' . $editor->get_error_message() . ' (is GD or ImageMagick installed?)' ); | ||
} | ||
|
||
$resize = $editor->resize( $width, $height, $crop ); | ||
|
||
if ( is_wp_error( $resize ) ) { | ||
throw new IPQ_Process_Exception( 'Error resizing image: ' . $resize->get_error_message() ); | ||
} | ||
|
||
$resized_file = $editor->save(); | ||
|
||
if ( is_wp_error( $resized_file ) ) { | ||
throw new IPQ_Process_Exception( 'Unable to save resized image file: ' . $editor->get_error_message() ); | ||
} | ||
|
||
$size_name = Image_Processing_Queue::get_size_name( array( $width, $height, $crop ) ); | ||
$image_meta['sizes'][ $size_name ] = array( | ||
'file' => $resized_file['file'], | ||
'width' => $resized_file['width'], | ||
'height' => $resized_file['height'], | ||
'mime-type' => $resized_file['mime-type'], | ||
); | ||
wp_update_attachment_metadata( $post_id, $image_meta ); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Custom exception class for IPQ background processing | ||
*/ | ||
class IPQ_Process_Exception extends Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.