wp/wp-admin/async-upload.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     3
 * Server-side file upload handler from wp-plupload, swfupload or other asynchronous upload methods.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Administration
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
	define( 'DOING_AJAX', true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
if ( ! defined( 'WP_ADMIN' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
	define( 'WP_ADMIN', true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    15
}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
if ( defined('ABSPATH') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	require_once(ABSPATH . 'wp-load.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
	require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
if ( ! ( isset( $_REQUEST['action'] ) && 'upload-attachment' == $_REQUEST['action'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	// Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	if ( is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		$_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		$_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	if ( empty($_COOKIE[LOGGED_IN_COOKIE]) && !empty($_REQUEST['logged_in_cookie']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
		$_COOKIE[LOGGED_IN_COOKIE] = $_REQUEST['logged_in_cookie'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	unset($current_user);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
require_once( ABSPATH . 'wp-admin/admin.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
	include( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	send_nosniff_header();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	nocache_headers();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	wp_ajax_upload_attachment();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	die( '0' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
if ( ! current_user_can( 'upload_files' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
	wp_die( __( 'You do not have permission to upload files.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
// just fetch the detail form for that attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	$post = get_post( $id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	if ( 'attachment' != $post->post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		wp_die( __( 'Unknown post type.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	if ( ! current_user_can( 'edit_post', $id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		wp_die( __( 'You are not allowed to edit this item.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	switch ( $_REQUEST['fetch'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		case 3 :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
			if ( $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
				echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
			echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
			$title = $post->post_title ? $post->post_title : wp_basename( $post->guid ); // title shouldn't ever be empty, but use filename just in cas.e
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
			echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ) . '</span></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		case 2 :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
			echo get_media_item($id, array( 'send' => false, 'delete' => true ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			echo get_media_item($id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
check_admin_referer('media-form');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
$post_id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
if ( isset( $_REQUEST['post_id'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	$post_id = absint( $_REQUEST['post_id'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		$post_id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
$id = media_handle_upload( 'async-upload', $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
if ( is_wp_error($id) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
	echo '<div class="error-div error">
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	<a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	<strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload due to an error'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	esc_html($id->get_error_message()) . '</div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
if ( $_REQUEST['short'] ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
	// Short form response - attachment ID only.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	echo $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
	// Long form response - big chunk o html.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	$type = $_REQUEST['type'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
	 * Filter the returned ID of an uploaded attachment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
	 * The dynamic portion of the hook name, `$type`, refers to the attachment type,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
	 * such as 'image', 'audio', 'video', 'file', etc.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
	 * @param int $id Uploaded attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
	echo apply_filters( "async_upload_{$type}", $id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
}