-
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?
Conversation
Some Wordpress plugins allow the user in the backend to add some extra meta information to an image. For instance, the plugin „WP Gallery Custom Links“ allows to add a custom url, a css class and other information to an image. This commit adds a function to the Image Field class with which it is possible to fetch these custom meta fields. The function is called „fetchCustomMetaValues“ and receives as input a comma separated list or an array with the meta keys names that should be fetched. Testing was included with the function „testImageCustomMetadataValues“ in ContentFieldsTest.php. The database was updated and includes now images with some extra meta fields. Here are the results of the tests: $ ./vendor/bin/phpunit --filter {ContentFieldsTest::testImageCustomMetadataValues} PHPUnit 4.2.2 by Sebastian Bergmann. Configuration read from ……/phpunit.xml . Time: 167 ms, Memory: 6.00MB OK (1 test, 6 assertions)
$customMetaValues = []; | ||
|
||
foreach ($metaKeys as $metaKey) { | ||
$customMetaValues[] = $this->attachment->meta->{trim($metaKey)}; |
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.
Did you check how many queries are you doing here?
* | ||
* @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 comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to create a metadata
method passing or just 1 string or an array of metadata fields to be returned.
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.
Maybe this metadata
method should be another method, and then refactor fetchCustomMetadataValues()
one to accept both parameters.
src/Field/Image.php
Outdated
@@ -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']; |
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.
Some Wordpress plugins allow the user in the backend to add some extra
meta information to an image. For instance, the plugin „WP Gallery
Custom Links“ allows to add a custom url, a css class and other
information to an image. This commit adds a function to the Image Field
class with which it is possible to fetch these custom meta fields. The
function is called „fetchCustomMetaValues“ and receives as input a comma
separated list or an array with the meta keys names that should be
fetched.
Testing was included with the function „testImageCustomMetadataValues“
in ContentFieldsTest.php. The database was updated and includes now
images with some extra meta fields. Here are the results of the tests:
$ ./vendor/bin/phpunit --filter
{ContentFieldsTest::testImageCustomMetadataValues}
PHPUnit 4.2.2 by Sebastian Bergmann.
Configuration read from ……/phpunit.xml
.
Time: 167 ms, Memory: 6.00MB
OK (1 test, 6 assertions)