Skip to content

Commit

Permalink
improved media field
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Mar 29, 2020
1 parent e90704c commit b9b22e0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
19 changes: 11 additions & 8 deletions Aeria/Field/Fields/MediaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,26 @@ public function get(array $saved_fields, bool $skip_filter = false)
}
$value = (int) $id;
$attachment = get_post($id);
$sizes = isset($this->config['get_sizes']) ? $this->config['get_sizes'] : get_intermediate_image_sizes();
$result = [
'meta' => $attachment,
];
if (strpos($attachment->post_mime_type, 'image') !== false) {
if (isset($this->config['get_sizes'])) {
$sizes = $this->config['get_sizes'];
} else {
$sizes = array_merge(['full'], get_intermediate_image_sizes());
}
foreach ($sizes as $size) {
$result[$size] = wp_get_attachment_image_src($value, $size);
$result[$size][3] = $result[$size][2] / $result[$size][1];
}
} else {
$result['url'] = wp_get_attachment_url($value);
$result['full'] = [wp_get_attachment_url($value)];
$media_meta = wp_get_attachment_metadata($value);
if (isset($media_meta['width']) && isset($media_meta['height'])) {
$result['size'] = [
$media_meta['width'],
$media_meta['height'],
$media_meta['width'] / $media_meta['height'],
];
$result['full'][] = $media_meta['width'];
$result['full'][] = $media_meta['height'];
$result['full'][] = $media_meta['width'] / $media_meta['height'];
}
}

Expand Down Expand Up @@ -83,12 +85,13 @@ public function getAdmin(array $saved_fields, array $errors)
$attachment = get_post($result['value']);

if (is_object($attachment)) {
$result['filename'] = basename($attachment->guid);
$result['fileName'] = basename($attachment->guid);
$result['mimeType'] = $attachment->post_mime_type;
if (strpos($attachment->post_mime_type, 'image') !== false) {
$result['url'] = wp_get_attachment_image_src($result['value'])[0];
} else {
$result['showFilename'] = true;
$result['naturalSize'] = true;
$result['url'] = wp_mime_type_icon($attachment->post_mime_type);
}
}
Expand Down
1 change: 1 addition & 0 deletions Aeria/Kernel/Tasks/CreateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function do(array $args)
$args['service']['field']->register('repeater', \Aeria\Field\Fields\RepeaterField::class);
$args['service']['field']->register('gallery', \Aeria\Field\Fields\GalleryField::class);
$args['service']['field']->register('picture', \Aeria\Field\Fields\PictureField::class);
$args['service']['field']->register('media', \Aeria\Field\Fields\MediaField::class);
$args['service']['field']->register('sections', \Aeria\Field\Fields\SectionsField::class);
$args['service']['field']->register('select', \Aeria\Field\Fields\SelectField::class);
$args['service']['field']->register('switch', \Aeria\Field\Fields\SwitchField::class);
Expand Down
12 changes: 6 additions & 6 deletions assets/js/aeria.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"webpack-cli": "^3.3.11"
},
"dependencies": {
"@aeria/core": "0.0.13",
"@aeria/uikit": "0.0.15",
"@aeria/core": "0.0.14",
"@aeria/uikit": "0.0.16",
"lodash.throttle": "^4.1.1",
"polished": "^3.5.1",
"react": "^16.13.1",
Expand Down
13 changes: 10 additions & 3 deletions scripts/hoc/withUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ export default function withPreventPostUpdate(WrappedComponent) {
const {models} = frame.state().get('selection')
const attachments = models.map(element => {
const attachment = element.toJSON()
return {
const data = {
mimeType: attachment.mime,
filename: attachment.filename,
showFilename: attachment.mime.indexOf('image') < 0,
fileName: attachment.filename,
showFilename: false,
value: attachment.id,
url: attachment.url,
}
if (attachment.mime.indexOf('image') < 0) {
data.showFilename = true
data.url = attachment.icon
data.naturalSize = true
}

return data
})

if (multiple) {
Expand Down

0 comments on commit b9b22e0

Please sign in to comment.