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

Allow to fetch custom meta fields for uploaded images. #22

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
34 changes: 34 additions & 0 deletions src/Field/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class Image extends BasicField implements FieldInterface
*/
protected $loadFromPost = false;

/**
* @var Post
*/
protected $attachment;

/**
* @param string $field
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'];
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Nice, thanks.



return $size;
}

Expand Down Expand Up @@ -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)
Copy link
Member

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.

Copy link
Member

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.

{
if (!is_array($metaKeys)) {
$metaKeys = explode(',', $metaKeys);
}

$customMetaValues = [];

foreach ($metaKeys as $metaKey) {
$customMetaValues[] = $this->attachment->meta->{trim($metaKey)};
Copy link
Member

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?

}

if (count($customMetaValues) === 1) {
return $customMetaValues[0];
}

return $customMetaValues;
}
}
21 changes: 21 additions & 0 deletions tests/ContentFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,25 @@ public function testGalleryFieldValue()
$this->assertEquals(1920, $image->width);
$this->assertEquals(1080, $image->height);
}

public function testImageCustomMetadataValues($value='')
{
$image = new Image($this->post);
$image->process('fake_image');

$this->assertCount(
4,
$image->fetchCustomMetadataValues(
'_gallery_link_url,
_gallery_link_target,
_gallery_link_preserve_click,
_gallery_link_additional_css_classes')
);
$this->assertTrue(is_string($image->fetchCustomMetadataValues('_gallery_link_url')));

$this->assertEquals('www.example.com', $image->fetchCustomMetadataValues('_gallery_link_url'));
$this->assertEquals('_blank', $image->fetchCustomMetadataValues('_gallery_link_target'));
$this->assertEquals('remove', $image->fetchCustomMetadataValues('_gallery_link_preserve_click'));
$this->assertEquals('underlined', $image->fetchCustomMetadataValues('_gallery_link_additional_css_classes'));
}
}
Binary file modified tests/config/database.sql.zip
Binary file not shown.