3 * Accepts file uploads from swfupload or other asynchronous upload methods.
6 * @subpackage Administration
9 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
10 define( 'DOING_AJAX', true );
13 if ( ! defined( 'WP_ADMIN' ) ) {
14 define( 'WP_ADMIN', true );
17 if ( defined('ABSPATH') )
18 require_once(ABSPATH . 'wp-load.php');
20 require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
22 if ( ! ( isset( $_REQUEST['action'] ) && 'upload-attachment' == $_REQUEST['action'] ) ) {
23 // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
24 if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
25 $_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
26 elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
27 $_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
28 if ( empty($_COOKIE[LOGGED_IN_COOKIE]) && !empty($_REQUEST['logged_in_cookie']) )
29 $_COOKIE[LOGGED_IN_COOKIE] = $_REQUEST['logged_in_cookie'];
33 require_once( ABSPATH . 'wp-admin/admin.php' );
35 if ( !current_user_can('upload_files') )
36 wp_die(__('You do not have permission to upload files.'));
38 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
40 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
41 include( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
43 send_nosniff_header();
46 wp_ajax_upload_attachment();
50 // just fetch the detail form for that attachment
51 if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
52 $post = get_post( $id );
53 if ( 'attachment' != $post->post_type )
54 wp_die( __( 'Unknown post type.' ) );
55 if ( ! current_user_can( 'edit_post', $id ) )
56 wp_die( __( 'You are not allowed to edit this item.' ) );
58 switch ( $_REQUEST['fetch'] ) {
60 if ( $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ) )
61 echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
62 echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
63 $title = $post->post_title ? $post->post_title : wp_basename( $post->guid ); // title shouldn't ever be empty, but use filename just in cas.e
64 echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '…' ) ) . '</span></div>';
67 add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
68 echo get_media_item($id, array( 'send' => false, 'delete' => true ));
71 add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
72 echo get_media_item($id);
78 check_admin_referer('media-form');
81 if ( isset( $_REQUEST['post_id'] ) ) {
82 $post_id = absint( $_REQUEST['post_id'] );
83 if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
87 $id = media_handle_upload( 'async-upload', $post_id );
88 if ( is_wp_error($id) ) {
89 echo '<div class="error-div error">
90 <a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
91 <strong>' . sprintf(__('“%s” has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
92 esc_html($id->get_error_message()) . '</div>';
96 if ( $_REQUEST['short'] ) {
97 // Short form response - attachment ID only.
100 // Long form response - big chunk o html.
101 $type = $_REQUEST['type'];
104 * Filter the returned ID of an uploaded attachment.
106 * The dynamic portion of the hook name, $type, refers to the attachment type,
107 * such as 'image', 'audio', 'video', 'file', etc.
111 * @param int $id Uploaded attachment ID.
113 echo apply_filters( "async_upload_{$type}", $id );