WordPress.org

Make WordPress Core

Changeset 21680


Ignore:
Timestamp:
08/31/2012 02:04:40 AM (8 years ago)
Author:
nacin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r21597 r21680  
    15311531}
    15321532add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' );
     1533
     1534
     1535/**
     1536 * Prepares an attachment post object for JS, where it is expected
     1537 * to be JSON-encoded and fit into an Attachment model.
     1538 *
     1539 * @since 3.5.0
     1540 *
     1541 * @param mixed $attachment Attachment ID or object.
     1542 * @return array Array of attachment details.
     1543 */
     1544function wp_prepare_attachment_for_js( $attachment ) {
     1545    if ( ! $attachment = get_post( $attachment ) )
     1546       return;
     1547
     1548    if ( 'attachment' != $attachment->post_type )
     1549       return;
     1550
     1551    $meta = wp_get_attachment_metadata( $attachment->ID );
     1552    list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
     1553
     1554    $attachment_url = wp_get_attachment_url( $attachment->ID );
     1555
     1556    $response = array(
     1557        'id'          => $attachment->ID,
     1558        'title'       => $attachment->post_title,
     1559        'filename'    => basename( $attachment->guid ),
     1560        'url'         => $attachment_url,
     1561        'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
     1562        'author'      => $attachment->post_author,
     1563        'description' => $attachment->post_content,
     1564        'caption'     => $attachment->post_excerpt,
     1565        'name'        => $attachment->post_name,
     1566        'status'      => $attachment->post_status,
     1567        'uploadedTo'  => $attachment->post_parent,
     1568        'date'        => $attachment->post_date_gmt . ' UTC',
     1569        'modified'    => $attachment->post_modified_gmt . ' UTC',
     1570        'mime'        => $attachment->post_mime_type,
     1571        'type'        => $type,
     1572        'subtype'     => $subtype,
     1573    );
     1574
     1575    if ( 'image' === $type ) {
     1576        $sizes = array();
     1577        $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
     1578
     1579        foreach ( $meta['sizes'] as $slug => $size ) {
     1580            $sizes[ $slug ] = array(
     1581                'height'      => $size['height'],
     1582                'width'       => $size['width'],
     1583                'url'         => $base_url . $size['file'],
     1584                'orientation' => $size['height'] > $size['width'] ? 'portrait' : 'landscape',
     1585            );
     1586        }
     1587
     1588        $response = array_merge( $response, array(
     1589            'height'      => $meta['height'],
     1590            'width'       => $meta['width'],
     1591            'sizes'       => $sizes,
     1592            'orientation' => $meta['height'] > $meta['width'] ? 'portrait' : 'landscape',
     1593        ) );
     1594    }
     1595
     1596    return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );
     1597}
Note: See TracChangeset for help on using the changeset viewer.