Skip to content

Commit

Permalink
Replace background processing with queueing library
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Nov 5, 2017
1 parent 5527baa commit cbe751f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 119 deletions.
3 changes: 2 additions & 1 deletion composer.json
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"
}
}
5 changes: 2 additions & 3 deletions image-processing-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
exit;
}

require_once plugin_dir_path( __FILE__ ) . 'vendor/a5hleyrich/wp-background-processing/classes/wp-async-request.php';
require_once plugin_dir_path( __FILE__ ) . 'vendor/a5hleyrich/wp-background-processing/classes/wp-background-process.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ipq-process.php';
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-image-processing-job.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-image-processing-queue.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/ipq-template-functions.php';

Expand Down
89 changes: 89 additions & 0 deletions includes/class-image-processing-job.php
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 {}
18 changes: 2 additions & 16 deletions includes/class-image-processing-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ class Image_Processing_Queue {
*/
private $is_updating_backup_sizes = false;

/**
* Instance of the background process class
*
* @var IPQ_Process|null
*/
public $process = null;

/**
* Singleton
*
Expand All @@ -49,7 +42,6 @@ public static function instance() {
* Image_Processing_Queue constructor.
*/
public function __construct() {
$this->process = new IPQ_Process();
add_filter( 'update_post_metadata', array( $this, 'filter_update_post_metadata' ), 10, 5 );
}

Expand Down Expand Up @@ -116,8 +108,6 @@ public function filter_update_post_metadata( $check, $object_id, $meta_key, $met
* @param array $sizes
*/
protected function process_image( $post_id, $sizes ) {
$new_item = false;

foreach ( $sizes as $size ) {
if ( self::does_size_already_exist_for_image( $post_id, $size ) ) {
continue;
Expand All @@ -133,12 +123,8 @@ protected function process_image( $post_id, $sizes ) {
'height' => $size[1],
'crop' => $size[2],
);
$this->process->push_to_queue( $item );
$new_item = true;
}

if ( $new_item ) {
$this->process->save()->dispatch();

wp_queue()->push( new Image_Processing_Job( $item ) );
}
}

Expand Down
99 changes: 0 additions & 99 deletions includes/class-ipq-process.php

This file was deleted.

0 comments on commit cbe751f

Please sign in to comment.