wp/wp-admin/async-upload.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Accepts file uploads from swfupload or other asynchronous upload methods.
     3  * Server-side file upload handler from wp-plupload, swfupload or other asynchronous upload methods.
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
     7  */
     7  */
     8 
     8 
     9 define('WP_ADMIN', true);
     9 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
       
    10 	define( 'DOING_AJAX', true );
       
    11 }
       
    12 
       
    13 if ( ! defined( 'WP_ADMIN' ) ) {
       
    14 	define( 'WP_ADMIN', true );
       
    15 }
    10 
    16 
    11 if ( defined('ABSPATH') )
    17 if ( defined('ABSPATH') )
    12 	require_once(ABSPATH . 'wp-load.php');
    18 	require_once(ABSPATH . 'wp-load.php');
    13 else
    19 else
    14 	require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
    20 	require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
    24 	unset($current_user);
    30 	unset($current_user);
    25 }
    31 }
    26 
    32 
    27 require_once( ABSPATH . 'wp-admin/admin.php' );
    33 require_once( ABSPATH . 'wp-admin/admin.php' );
    28 
    34 
    29 if ( !current_user_can('upload_files') )
    35 header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
    30 	wp_die(__('You do not have permission to upload files.'));
       
    31 
       
    32 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
       
    33 
    36 
    34 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
    37 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
    35 	define( 'DOING_AJAX', true );
    38 	include( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
    36 	include ABSPATH . 'wp-admin/includes/ajax-actions.php';
       
    37 
    39 
    38 	send_nosniff_header();
    40 	send_nosniff_header();
    39 	nocache_headers();
    41 	nocache_headers();
    40 
    42 
    41 	wp_ajax_upload_attachment();
    43 	wp_ajax_upload_attachment();
    42 	die( '0' );
    44 	die( '0' );
       
    45 }
       
    46 
       
    47 if ( ! current_user_can( 'upload_files' ) ) {
       
    48 	wp_die( __( 'You do not have permission to upload files.' ) );
    43 }
    49 }
    44 
    50 
    45 // just fetch the detail form for that attachment
    51 // just fetch the detail form for that attachment
    46 if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
    52 if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
    47 	$post = get_post( $id );
    53 	$post = get_post( $id );
    79 		$post_id = 0;
    85 		$post_id = 0;
    80 }
    86 }
    81 
    87 
    82 $id = media_handle_upload( 'async-upload', $post_id );
    88 $id = media_handle_upload( 'async-upload', $post_id );
    83 if ( is_wp_error($id) ) {
    89 if ( is_wp_error($id) ) {
    84 	echo '<div class="error-div">
    90 	echo '<div class="error-div error">
    85 	<a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
    91 	<a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
    86 	<strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
    92 	<strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
    87 	esc_html($id->get_error_message()) . '</div>';
    93 	esc_html($id->get_error_message()) . '</div>';
    88 	exit;
    94 	exit;
    89 }
    95 }
    90 
    96 
    91 if ( $_REQUEST['short'] ) {
    97 if ( $_REQUEST['short'] ) {
    92 	// short form response - attachment ID only
    98 	// Short form response - attachment ID only.
    93 	echo $id;
    99 	echo $id;
    94 } else {
   100 } else {
    95 	// long form response - big chunk o html
   101 	// Long form response - big chunk o html.
    96 	$type = $_REQUEST['type'];
   102 	$type = $_REQUEST['type'];
    97 	echo apply_filters("async_upload_{$type}", $id);
   103 
       
   104 	/**
       
   105 	 * Filter the returned ID of an uploaded attachment.
       
   106 	 *
       
   107 	 * The dynamic portion of the hook name, `$type`, refers to the attachment type,
       
   108 	 * such as 'image', 'audio', 'video', 'file', etc.
       
   109 	 *
       
   110 	 * @since 2.5.0
       
   111 	 *
       
   112 	 * @param int $id Uploaded attachment ID.
       
   113 	 */
       
   114 	echo apply_filters( "async_upload_{$type}", $id );
    98 }
   115 }