Skip to content

Commit

Permalink
enhancement of attachment model function "getThumbnailUrl"
Browse files Browse the repository at this point in the history
Now you can call:

```php
$attachment->getThumbnailUrl('thumbnail');
```
  • Loading branch information
diegomarangoni committed Jan 4, 2014
1 parent a765258 commit 72ca5bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 58 deletions.
36 changes: 13 additions & 23 deletions Model/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,34 @@
class Attachment extends Post implements AttachmentInterface
{
protected $post;
protected $url;
protected $metadata;
protected $url;
protected $thumbnailUrl;
protected $mimeType;

public function __construct(Post $post)
{
$this->post = $post;
}

public function setMetadata($metadata)
{
$this->metadata = $metadata;
$this->metadata = $post->getMetas()
->filter(function (PostMeta $meta) {
return '_wp_attachment_metadata' == $meta->getKey();
})
->first()
;
}

public function getMetadata()
{
return $this->metadata;
}

public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
}

public function getMimeType()
public function getThumbnailUrl($size = 'post-thumbnail')
{
return $this->mimeType;
}
$rawMetadata = $this->metadata->getValue();

public function setThumbnailUrl($thumbnail)
{
$this->thumbnailUrl = $thumbnail;
}
if (isset($rawMetadata['sizes'][$size])) {
return dirname($rawMetadata['file']) . '/' . $rawMetadata['sizes'][$size]['file'];
}

public function getThumbnailUrl($size = null)
{
return $this->thumbnailUrl;
return null;
}

public function setUrl($url)
Expand Down
37 changes: 2 additions & 35 deletions Model/AttachmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,7 @@ public function findAttachmentsByPost(Post $post)
$result = array();
/** @var $post Post */
foreach ($posts as $post) {
/** @var $meta PostMeta */
$meta = $this->postMetaManager->findOneMetaBy(array(
'post' => $post,
'key' => '_wp_attachment_metadata'
));

if ($meta) {
$rawMeta = $meta->getValue();
$attachment = new Attachment($post);

$attachment->setUrl($rawMeta['file']);
if (isset($rawMeta['sizes']['thumbnail'])) {
$attachment->setThumbnailUrl(substr($rawMeta['file'], 0, strrpos($rawMeta['file'], '/') + 1) . $rawMeta['sizes']['thumbnail']['file']);
}

$result[] = $attachment;
}
$result[] = new Attachment($post);
}

return $result;
Expand All @@ -83,24 +67,7 @@ public function findOneAttachmentById($id)
'type' => 'attachment',
));

/** @var $meta PostMeta */
$meta = $this->postMetaManager->findOneMetaBy(array(
'post' => $post,
'key' => '_wp_attachment_metadata'
));

if ($meta) {
$rawMeta = $meta->getValue();
$attachment = new Attachment($post);

$attachment->setUrl($rawMeta['file']);
if (isset($rawMeta['sizes']['thumbnail'])) {
$attachment->setThumbnailUrl(substr($rawMeta['file'], 0, strrpos($rawMeta['file'], '/') + 1) . $rawMeta['sizes']['thumbnail']['file']);
}
return $attachment;
}

return null;
return new Attachment($post);
}

public function getAttachmentOfSize(Attachment $attachment, $size = null)
Expand Down

1 comment on commit 72ca5bf

@benglass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks the getUrl method of Attachments returned by AttachmentManager::findAttachmentsByPost because it never sets the Url

Please sign in to comment.