Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed query_images() for compatibility with Plugin "WP-Offload-S3" #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions models/attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,47 @@ function import_wp_object($wp_attachment) {
function is_image() {
return (substr($this->mime_type, 0, 5) == 'image');
}

function query_images() {
$sizes = array('thumbnail', 'medium', 'large', 'full');
if (function_exists('get_intermediate_image_sizes')) {
$sizes = array_merge(array('full'), get_intermediate_image_sizes());
}
$this->images = array();
$home = get_bloginfo('url');
$hostOfHome = parse_url($home, PHP_URL_HOST);
foreach ($sizes as $size) {

list($url, $width, $height) = wp_get_attachment_image_src($this->id, $size);
$filename = ABSPATH . substr($url, strlen($home) + 1);
if (file_exists($filename)) {
list($measured_width, $measured_height) = getimagesize($filename);
if ($measured_width == $width &&
$measured_height == $height) {
$this->images[$size] = (object) array(

// changelog 2016-01-13 from @itinance https://github.com/itinance:
//
// if plugin "WP-Offload-S3" is installed, the attachments are stored at s3/cloudfront and not necessarily
// hosted on the server beeing available at ABSPATH. In this case, let us trust the result
// of wp_get_attachment_image_src() and renounce calls to file_exists() and getimagesize()

$hostOfAttachment = parse_url($url, PHP_URL_HOST);
if($hostOfAttachment === $hostOfHome) {
$filename = ABSPATH . substr($url, strlen($home) + 1);
if (file_exists($filename)) {
list($measured_width, $measured_height) = getimagesize($filename);
if ($measured_width == $width &&
$measured_height == $height) {
$this->images[$size] = (object) array(
'url' => $url,
'width' => $width,
'height' => $height
);
}
}
} else {

$this->images[$size] = (object) array(
'url' => $url,
'width' => $width,
'height' => $height
);
}
);

}
}
}
Expand Down