Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
Vinícius Nunes 2019-12-18 17:05:01 -03:00
commit dad602dc58
2 changed files with 106 additions and 8 deletions

View File

@ -6,7 +6,8 @@ export default {
initFrame: function() {
wp.media.view.settings.post = {
id: this.params.relatedPostId
id: this.params.relatedPostId,
wp_customize: 'off'
}
this.frame = wp.media({
@ -57,11 +58,41 @@ export default {
}
});
}
}),
}),
// CroppedImageControl, with presets for thumbnail dimensions
thumbnailControl: wp.customize.CroppedImageControl.extend({
initFrame: function() {
var MyCustomizeImageCropper = wp.media.controller.Cropper.extend({
doCrop: function( attachment ) {
var cropDetails = attachment.get( 'cropDetails' ),
control = this.get( 'control' ),
ratio = cropDetails.width / cropDetails.height;
// Use crop measurements when flexible in both directions.
if ( control.params.flex_width && control.params.flex_height ) {
cropDetails.dst_width = cropDetails.width;
cropDetails.dst_height = cropDetails.height;
// Constrain flexible side based on image ratio and size of the fixed side.
} else {
cropDetails.dst_width = control.params.flex_width ? control.params.height * ratio : control.params.width;
cropDetails.dst_height = control.params.flex_height ? control.params.width / ratio : control.params.height;
}
return wp.ajax.post( 'crop-image', {
wp_customize: 'off',
nonce: attachment.get( 'nonces' ).edit,
id: attachment.get( 'id' ),
context: control.id,
cropDetails: cropDetails
});
}
});
var l10n = _wpMediaViewsL10n;
wp.media.view.settings.post = {
@ -96,7 +127,7 @@ export default {
suggestedHeight: this.params.height,
uploadedTo: this.params.relatedPostId
}),
new wp.media.controller.CustomizeImageCropper({
new MyCustomizeImageCropper({
imgSelectOptions: this.calculateImageSelectOptions,
control: this
})
@ -193,7 +224,7 @@ export default {
initFrame: function() {
wp.media.view.settings.post = {
id: this.params.relatedPostId
id: this.params.relatedPostId
}
this.frame = wp.media({

View File

@ -95,7 +95,8 @@ class REST_Items_Controller extends REST_Controller {
'callback' => array($this, 'get_item_attachments'),
'permission_callback' => array($this, 'get_item_attachments_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE),
)
),
'schema' => [$this, 'get_attachments_schema'],
)
);
register_rest_route(
@ -327,20 +328,30 @@ class REST_Items_Controller extends REST_Controller {
}
$attachments[] = [
'id' => $post->ID,
'title' => get_the_title( $post ),
'description' => get_the_content( $post ),
'mime' => $post->post_mime_type,
'mime_type' => $post->post_mime_type,
'date' => $post->post_date,
'date_gmt' => $post->post_date_gmt,
'author' => $post->post_author,
'url' => wp_get_attachment_url( $post->ID ),
'media_type' => wp_attachment_is_image( $post->ID ) ? 'image' : 'file',
'thumbnails' => $thumbs
];
}
return new \WP_REST_Response(apply_filters('tainacan-rest-response', $attachments, $request), 200);
$total_items = $posts_query->found_posts;
$max_pages = ceil($total_items / (int) $posts_query->query_vars['posts_per_page']);
$rest_response = new \WP_REST_Response(apply_filters('tainacan-rest-response', $attachments, $request), 200);
$rest_response->header('X-WP-Total', (int) $total_items);
$rest_response->header('X-WP-TotalPages', (int) $max_pages);
$rest_response->header('X-WP-ItemsPerPage', (int) $posts_query->query_vars['posts_per_page']);
return $rest_response;
}
/**
@ -880,6 +891,62 @@ class REST_Items_Controller extends REST_Controller {
return $query_params;
}
function get_attachments_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'collection',
'type' => 'object'
];
$schema = [
'title' => [
'description' => esc_html__('The attachment title', 'tainacan'),
'type' => 'string'
],
'description' => [
'description' => esc_html__('The attachment description', 'tainacan'),
'type' => 'string'
],
'mime_type' => [
'description' => esc_html__('The attachment MIME type', 'tainacan'),
'type' => 'string'
],
'date' => [
'description' => esc_html__('The attachment creation date', 'tainacan'),
'type' => 'string'
],
'date_gmt' => [
'description' => esc_html__('The attachment creation date in GMT', 'tainacan'),
'type' => 'string'
],
'author' => [
'description' => esc_html__('The ID of the user who uploaded the attachment', 'tainacan'),
'type' => 'string'
],
'url' => [
'description' => esc_html__('The URL to the attachment file', 'tainacan'),
'type' => 'string'
],
'media_type' => [
'description' => esc_html__('The attachment Media type', 'tainacan'),
'type' => 'string',
'enum' => [ 'image', 'file' ]
],
'thumbnails' => [
'description' => esc_html__('The attachment thumbnails', 'tainacan'),
'type' => 'array'
],
];
$schema['properties'] = array_merge(
parent::get_base_properties_schema(),
$schema
);
return $schema;
}
}
?>