From 5546df59d3d7e75190390d15b6bc1119befe5b0c Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Fri, 31 Aug 2012 02:04:40 +0000 Subject: [PATCH] Introduce wp_prepare_attachment_for_js(). Prepares an attachment post object to be JSON-encoded and fitted into an Attachment model, for 3.5 media. props koopersmith. see #21390. git-svn-id: http://core.svn.wordpress.org/trunk@21680 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/wp-includes/media.php b/wp-includes/media.php index 477d0005863d..edb2761d06f4 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1530,3 +1530,68 @@ function wp_plupload_default_settings() { $wp_scripts->add_data( 'wp-plupload', 'data', $script ); } add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' ); + + +/** + * Prepares an attachment post object for JS, where it is expected + * to be JSON-encoded and fit into an Attachment model. + * + * @since 3.5.0 + * + * @param mixed $attachment Attachment ID or object. + * @return array Array of attachment details. + */ +function wp_prepare_attachment_for_js( $attachment ) { + if ( ! $attachment = get_post( $attachment ) ) + return; + + if ( 'attachment' != $attachment->post_type ) + return; + + $meta = wp_get_attachment_metadata( $attachment->ID ); + list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); + + $attachment_url = wp_get_attachment_url( $attachment->ID ); + + $response = array( + 'id' => $attachment->ID, + 'title' => $attachment->post_title, + 'filename' => basename( $attachment->guid ), + 'url' => $attachment_url, + 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), + 'author' => $attachment->post_author, + 'description' => $attachment->post_content, + 'caption' => $attachment->post_excerpt, + 'name' => $attachment->post_name, + 'status' => $attachment->post_status, + 'uploadedTo' => $attachment->post_parent, + 'date' => $attachment->post_date_gmt . ' UTC', + 'modified' => $attachment->post_modified_gmt . ' UTC', + 'mime' => $attachment->post_mime_type, + 'type' => $type, + 'subtype' => $subtype, + ); + + if ( 'image' === $type ) { + $sizes = array(); + $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); + + foreach ( $meta['sizes'] as $slug => $size ) { + $sizes[ $slug ] = array( + 'height' => $size['height'], + 'width' => $size['width'], + 'url' => $base_url . $size['file'], + 'orientation' => $size['height'] > $size['width'] ? 'portrait' : 'landscape', + ); + } + + $response = array_merge( $response, array( + 'height' => $meta['height'], + 'width' => $meta['width'], + 'sizes' => $sizes, + 'orientation' => $meta['height'] > $meta['width'] ? 'portrait' : 'landscape', + ) ); + } + + return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); +}