Skip to content

Commit

Permalink
Introduce wp_prepare_attachment_for_js(). Prepares an attachment post…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
nacin committed Aug 31, 2012
1 parent c8a328f commit 5546df5
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions wp-includes/media.php
Expand Up @@ -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 );
}

0 comments on commit 5546df5

Please sign in to comment.