web/wp-admin/async-upload.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    11 if ( defined('ABSPATH') )
    11 if ( defined('ABSPATH') )
    12 	require_once(ABSPATH . 'wp-load.php');
    12 	require_once(ABSPATH . 'wp-load.php');
    13 else
    13 else
    14 	require_once('../wp-load.php');
    14 	require_once('../wp-load.php');
    15 
    15 
    16 // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
    16 if ( ! ( isset( $_REQUEST['action'] ) && 'upload-attachment' == $_REQUEST['action'] ) ) {
    17 if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
    17 	// Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
    18 	$_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
    18 	if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
    19 elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
    19 		$_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
    20 	$_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
    20 	elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
    21 if ( empty($_COOKIE[LOGGED_IN_COOKIE]) && !empty($_REQUEST['logged_in_cookie']) )
    21 		$_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
    22 	$_COOKIE[LOGGED_IN_COOKIE] = $_REQUEST['logged_in_cookie'];
    22 	if ( empty($_COOKIE[LOGGED_IN_COOKIE]) && !empty($_REQUEST['logged_in_cookie']) )
    23 unset($current_user);
    23 		$_COOKIE[LOGGED_IN_COOKIE] = $_REQUEST['logged_in_cookie'];
       
    24 	unset($current_user);
       
    25 }
       
    26 
    24 require_once('./admin.php');
    27 require_once('./admin.php');
       
    28 
       
    29 if ( !current_user_can('upload_files') )
       
    30 	wp_die(__('You do not have permission to upload files.'));
    25 
    31 
    26 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
    32 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
    27 
    33 
    28 if ( !current_user_can('upload_files') )
    34 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
    29 	wp_die(__('You do not have permission to upload files.'));
    35 	define( 'DOING_AJAX', true );
       
    36 	include ABSPATH . 'wp-admin/includes/ajax-actions.php';
       
    37 
       
    38 	send_nosniff_header();
       
    39 	nocache_headers();
       
    40 
       
    41 	wp_ajax_upload_attachment();
       
    42 	die( '0' );
       
    43 }
    30 
    44 
    31 // just fetch the detail form for that attachment
    45 // just fetch the detail form for that attachment
    32 if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
    46 if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
    33 	$post = get_post( $id );
    47 	$post = get_post( $id );
    34 	if ( 'attachment' != $post->post_type )
    48 	if ( 'attachment' != $post->post_type )
    35 		wp_die( __( 'Unknown post type.' ) );
    49 		wp_die( __( 'Unknown post type.' ) );
    36 	$post_type_object = get_post_type_object( 'attachment' );
    50 	$post_type_object = get_post_type_object( 'attachment' );
    37 	if ( ! current_user_can( $post_type_object->cap->edit_post, $id ) )
    51 	if ( ! current_user_can( $post_type_object->cap->edit_post, $id ) )
    38 		wp_die( __( 'You are not allowed to edit this item.' ) );
    52 		wp_die( __( 'You are not allowed to edit this item.' ) );
    39 
    53 
    40 	if ( 2 == $_REQUEST['fetch'] ) {
    54 	switch ( $_REQUEST['fetch'] ) {
    41 		add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
    55 		case 3 :
    42 		echo get_media_item($id, array( 'send' => false, 'delete' => true ));
    56 			if ( $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ) )
    43 	} else {
    57 				echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
    44 		add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
    58 			echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
    45 		echo get_media_item($id);
    59 			$title = $post->post_title ? $post->post_title : wp_basename( $post->guid ); // title shouldn't ever be empty, but use filename just in cas.e
       
    60 			echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60 ) ) . '</span></div>';
       
    61 			break;
       
    62 		case 2 :
       
    63 			add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
       
    64 			echo get_media_item($id, array( 'send' => false, 'delete' => true ));
       
    65 			break;
       
    66 		default:
       
    67 			add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
       
    68 			echo get_media_item($id);
       
    69 			break;
    46 	}
    70 	}
    47 	exit;
    71 	exit;
    48 }
    72 }
    49 
    73 
    50 check_admin_referer('media-form');
    74 check_admin_referer('media-form');
    51 
    75 
    52 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
    76 $post_id = 0;
       
    77 if ( isset( $_REQUEST['post_id'] ) ) {
       
    78 	$post_id = absint( $_REQUEST['post_id'] );
       
    79 	if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
       
    80 		$post_id = 0;
       
    81 }
       
    82 
       
    83 $id = media_handle_upload( 'async-upload', $post_id );
    53 if ( is_wp_error($id) ) {
    84 if ( is_wp_error($id) ) {
    54 	echo '<div class="error-div">
    85 	echo '<div class="error-div">
    55 	<a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
    86 	<a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
    56 	<strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
    87 	<strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
    57 	esc_html($id->get_error_message()) . '</div>';
    88 	esc_html($id->get_error_message()) . '</div>';