Make WordPress Core


Ignore:
Timestamp:
03/15/2012 04:14:05 AM (12 years ago)
Author:
koopersmith
Message:

Theme Customizer: First pass for upload controls, using background image as an example. Add a wrapper for Plupload that allows for custom upload UIs. see #19910.

wp.Uploader is a wrapper that provides a simple way to upload an attachment (using the wp_ajax_upload_attachment handler). It is intentionally decoupled from the UI. When an upload succeeds, it will receive the attachment information (id, url, meta, etc) as a JSON response. If the upload fails, the wrapper handles both WordPress and plupload errors through a single handler.

As todos, we should add drag classes for the uploader dropzone and account for the rough 100mb filesize limit in most browsers. The UI for the customizer upload controls could be improved as well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/ajax-actions.php

    r20072 r20179  
    15451545}
    15461546
     1547function wp_ajax_upload_attachment() {
     1548    check_ajax_referer( 'media-form' );
     1549
     1550    if ( ! current_user_can( 'upload_files' ) )
     1551        wp_die( -1 );
     1552
     1553    if ( isset( $_REQUEST['post_id'] ) ) {
     1554        $post_id = $_REQUEST['post_id'];
     1555        if ( ! current_user_can( 'edit_post', $post_id ) )
     1556            wp_die( -1 );
     1557    } else {
     1558        $post_id = null;
     1559    }
     1560
     1561    $post_data = is_array( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array();
     1562
     1563    $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );
     1564
     1565    if ( is_wp_error( $attachment_id ) ) {
     1566        echo json_encode( array(
     1567            'type' => 'error',
     1568            'data' => array(
     1569                'message'  => $attachment_id->get_error_message(),
     1570                'filename' => $_FILES['async-upload']['name'],
     1571            ),
     1572        ) );
     1573        wp_die();
     1574    }
     1575
     1576    $post = get_post( $attachment_id );
     1577
     1578    echo json_encode( array(
     1579        'type' => 'success',
     1580        'data' => array(
     1581            'id'       => $attachment_id,
     1582            'title'    => esc_attr( $post->post_title ),
     1583            'filename' => esc_html( basename( $post->guid ) ),
     1584            'url'      => wp_get_attachment_url( $attachment_id ),
     1585            'meta'     => wp_get_attachment_metadata( $attachment_id ),
     1586        ),
     1587    ) );
     1588    wp_die();
     1589}
     1590
    15471591function wp_ajax_image_editor() {
    15481592    $attachment_id = intval($_POST['postid']);
Note: See TracChangeset for help on using the changeset viewer.