/** * @param \Jigoshop\Entity\Product $product Product to find attachments for. * @param string $size Size for images. * * @return array List of Attachments attached to the product. */ public function getAttachments(Product $product, $size = Options::IMAGE_THUMBNAIL) { $this->wp->wpUploadDir(); $uploadUrl = $this->wp->wpUploadDir()['baseurl']; $wpdb = $this->wp->getWPDB(); $attachments = $this->wp->applyFilters('jigoshop\\service\\product\\attachments\\types', array('gallery' => array(), 'downloads' => array())); $query = $wpdb->prepare("SELECT post.ID as id, post.post_title as title, post.guid as url, meta.meta_value as meta, attachment.type\n\t\t\t\tFROM {$wpdb->prefix}jigoshop_product_attachment as attachment\n\t\t\t\tLEFT JOIN {$wpdb->posts} as post ON (attachment.attachment_id = post.ID)\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} as meta ON (meta.meta_key = '_wp_attachment_metadata' AND meta.post_id = post.ID)\n\t\t\t\tWHERE product_id = %d", $product->getId()); $results = $wpdb->get_results($query, ARRAY_A); foreach ($results as $attachment) { $attachment['meta'] = unserialize($attachment['meta']); if (isset($attachment['meta']['sizes']) && isset($attachment['meta']['sizes'][$size])) { $thumbUrl = $uploadUrl . '/' . str_replace(basename($attachment['meta']['file']), basename($attachment['meta']['sizes'][$size]['file']), $attachment['meta']['file']); } else { $thumbUrl = $uploadUrl . '/' . $attachment['meta']['file']; } $attachments[$attachment['type']][] = array('id' => $attachment['id'], 'title' => $attachment['title'], 'url' => isset($attachment['meta']['file']) ? $uploadUrl . '/' . $attachment['meta']['file'] : $attachment['url'], 'thumbnail' => $thumbUrl, 'image' => $this->wp->wpGetAttachmentImage($attachment['id'], $size)); } return $attachments; }