-
Notifications
You must be signed in to change notification settings - Fork 101
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
Allow to fetch custom meta fields for uploaded images. #22
base: develop
Are you sure you want to change the base?
Changes from 7 commits
6441c14
261b99b
62c9057
89abcdd
09d379b
5ad2a27
3de94a7
2bb09ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,11 @@ class Image extends BasicField implements FieldInterface | |
*/ | ||
protected $loadFromPost = false; | ||
|
||
/** | ||
* @var Post | ||
*/ | ||
protected $attachment; | ||
|
||
/** | ||
* @param string $field | ||
*/ | ||
|
@@ -87,6 +92,7 @@ protected function fillFields(Post $attachment) | |
$this->mime_type = $attachment->post_mime_type; | ||
$this->url = $attachment->guid; | ||
$this->description = $attachment->post_excerpt; | ||
$this->attachment = $attachment; | ||
} | ||
|
||
/** | ||
|
@@ -116,6 +122,10 @@ protected function fillThumbnailFields(array $data) | |
$size->height = $data['height']; | ||
$size->mime_type = $data['mime-type']; | ||
|
||
$imgDir = substr($this->url, 0, strrpos($this->url, '/')); | ||
$size->url = $imgDir.'/'.$data['file']; | ||
|
||
|
||
return $size; | ||
} | ||
|
||
|
@@ -164,4 +174,28 @@ protected function fillMetadataFields(array $imageData) | |
$this->height = $imageData['height']; | ||
$this->sizes = $imageData['sizes']; | ||
} | ||
|
||
/** | ||
* @param string|array comma seperated string or an array | ||
* | ||
* @return string|array string if only one meta key was received as input, otherwise an array with the values of all meta keys received as input | ||
*/ | ||
public function fetchCustomMetadataValues($metaKeys) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be better to create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this |
||
{ | ||
if (!is_array($metaKeys)) { | ||
$metaKeys = explode(',', $metaKeys); | ||
} | ||
|
||
$customMetaValues = []; | ||
|
||
foreach ($metaKeys as $metaKey) { | ||
$customMetaValues[] = $this->attachment->meta->{trim($metaKey)}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you check how many queries are you doing here? |
||
} | ||
|
||
if (count($customMetaValues) === 1) { | ||
return $customMetaValues[0]; | ||
} | ||
|
||
return $customMetaValues; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not being tested. If you can please write a test. I guess no fields are being tested, so if you can please test all of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines weren't intended to be included. I included them by mistake. I have already removed them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks.