From bfbc9d46c6673d035ed992a93c320e99c7851e5c Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Wed, 19 Apr 2017 13:14:14 +0100 Subject: [PATCH] New function added for returning a single image URL --- includes/class-image-processing-queue.php | 80 ++++++++++++++++------- includes/ipq-template-functions.php | 17 +++++ 2 files changed, 75 insertions(+), 22 deletions(-) diff --git a/includes/class-image-processing-queue.php b/includes/class-image-processing-queue.php index 6342829..286ebee 100644 --- a/includes/class-image-processing-queue.php +++ b/includes/class-image-processing-queue.php @@ -109,6 +109,39 @@ public function filter_update_post_metadata( $check, $object_id, $meta_key, $met return $check; } + /** + * Check if the image sizes exist and push them to the queue if not. + * + * @param int $post_id + * @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; + } + + if ( self::is_size_larger_than_original( $post_id, $size ) ) { + continue; + } + + $item = array( + 'post_id' => $post_id, + 'width' => $size[0], + 'height' => $size[1], + 'crop' => $size[2], + ); + $this->process->push_to_queue( $item ); + $new_item = true; + } + + if ( $new_item ) { + $this->process->save()->dispatch(); + } + } + /** * Get image HTML for a specific context in a theme, specifying the exact sizes * for the image. The first image size is always used as the `src` and the other @@ -132,32 +165,34 @@ public function filter_update_post_metadata( $check, $object_id, $meta_key, $met * @return string HTML img element or empty string on failure. */ public function get_image( $post_id, $sizes, $attr = '' ) { - // If we haven't created the image yet, add it to the queue. - $new_item = false; - foreach ( $sizes as $size ) { - if ( self::does_size_already_exist_for_image( $post_id, $size ) ) { - continue; - } + $this->process_image( $post_id, $sizes ); - if ( self::is_size_larger_than_original( $post_id, $size ) ) { - continue; - } + return wp_get_attachment_image( $post_id, array( $sizes[0][0], $sizes[0][1] ), false, $attr ); + } - $item = array( - 'post_id' => $post_id, - 'width' => $size[0], - 'height' => $size[1], - 'crop' => $size[2], - ); - $this->process->push_to_queue( $item ); - $new_item = true; - } + /** + * Get image URL for a specific context in a theme, specifying the exact size + * for the image. If the image size does not currently exist, it is queued for + * creation by a background process. Example: + * + * echo ipq_get_theme_image_url( 1353, array( 600, 400, false ) ); + * + * @param int $post_id + * @param array $size + * + * @return string + */ + public function get_image_url( $post_id, $size ) { + $this->process_image( $post_id, array( $size ) ); - if ( $new_item ) { - $this->process->save()->dispatch(); + $size = self::get_size_name( $size ); + $src = wp_get_attachment_image_src( $post_id, $size ); + + if ( isset( $src[0] ) ) { + return $src[0]; } - return wp_get_attachment_image( $post_id, array( $sizes[0][0], $sizes[0][1] ), false, $attr ); + return ''; } /** @@ -211,7 +246,8 @@ public static function update_image_meta( $post_id, $data ) { */ public static function does_size_already_exist_for_image( $post_id, $size ) { $image_meta = self::get_image_meta( $post_id ); - $size_name = self::get_size_name( $size ); + $size_name = self::get_size_name( $size ); + return isset( $image_meta['sizes'][ $size_name ] ); } diff --git a/includes/ipq-template-functions.php b/includes/ipq-template-functions.php index c6b72ff..a0a397c 100644 --- a/includes/ipq-template-functions.php +++ b/includes/ipq-template-functions.php @@ -19,8 +19,25 @@ * @param int $post_id Image attachment ID. * @param array $sizes Array of arrays of sizes in the format array(width,height,crop). * @param string $attr Optional. Attributes for the image markup. Default empty. + * * @return string HTML img element or empty string on failure. */ function ipq_get_theme_image( $post_id, $sizes, $attr = '' ) { return Image_Processing_Queue::instance()->get_image( $post_id, $sizes, $attr ); } + +/** + * Get image URL for a specific context in a theme, specifying the exact size + * for the image. If the image size does not currently exist, it is queued for + * creation by a background process. Example: + * + * echo ipq_get_theme_image_url( 1353, array( 600, 400, false ) ); + * + * @param int $post_id + * @param array $size + * + * @return string Img URL + */ +function ipq_get_theme_image_url( $post_id, $size ) { + return Image_Processing_Queue::instance()->get_image_url( $post_id, $size ); +}