home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / async-upload.php < prev    next >
Encoding:
PHP Script  |  2017-09-21  |  3.5 KB  |  109 lines

  1. <?php
  2. /**
  3.  * Server-side file upload handler from wp-plupload or other asynchronous upload methods.
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Administration
  7.  */
  8.  
  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. }
  16.  
  17. if ( defined( 'ABSPATH' ) ) {
  18.     require_once( ABSPATH . 'wp-load.php' );
  19. } else {
  20.     require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
  21. }
  22.  
  23. require_once( ABSPATH . 'wp-admin/admin.php' );
  24.  
  25. header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
  26.  
  27. if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
  28.     include( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
  29.  
  30.     send_nosniff_header();
  31.     nocache_headers();
  32.  
  33.     wp_ajax_upload_attachment();
  34.     die( '0' );
  35. }
  36.  
  37. if ( ! current_user_can( 'upload_files' ) ) {
  38.     wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
  39. }
  40.  
  41. // just fetch the detail form for that attachment
  42. if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
  43.     $post = get_post( $id );
  44.     if ( 'attachment' != $post->post_type )
  45.         wp_die( __( 'Invalid post type.' ) );
  46.     if ( ! current_user_can( 'edit_post', $id ) )
  47.         wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
  48.  
  49.     switch ( $_REQUEST['fetch'] ) {
  50.         case 3 :
  51.             if ( $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ) )
  52.                 echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
  53.             echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
  54.  
  55.             // Title shouldn't ever be empty, but use filename just in case.
  56.             $file = get_attached_file( $post->ID );
  57.             $title = $post->post_title ? $post->post_title : wp_basename( $file );
  58.             echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '…' ) ) . '</span></div>';
  59.             break;
  60.         case 2 :
  61.             add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
  62.             echo get_media_item($id, array( 'send' => false, 'delete' => true ));
  63.             break;
  64.         default:
  65.             add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  66.             echo get_media_item($id);
  67.             break;
  68.     }
  69.     exit;
  70. }
  71.  
  72. check_admin_referer('media-form');
  73.  
  74. $post_id = 0;
  75. if ( isset( $_REQUEST['post_id'] ) ) {
  76.     $post_id = absint( $_REQUEST['post_id'] );
  77.     if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
  78.         $post_id = 0;
  79. }
  80.  
  81. $id = media_handle_upload( 'async-upload', $post_id );
  82. if ( is_wp_error($id) ) {
  83.     echo '<div class="error-div error">
  84.     <a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
  85.     <strong>' . sprintf(__('“%s” has failed to upload.'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
  86.     esc_html($id->get_error_message()) . '</div>';
  87.     exit;
  88. }
  89.  
  90. if ( $_REQUEST['short'] ) {
  91.     // Short form response - attachment ID only.
  92.     echo $id;
  93. } else {
  94.     // Long form response - big chunk o html.
  95.     $type = $_REQUEST['type'];
  96.  
  97.     /**
  98.      * Filters the returned ID of an uploaded attachment.
  99.      *
  100.      * The dynamic portion of the hook name, `$type`, refers to the attachment type,
  101.      * such as 'image', 'audio', 'video', 'file', etc.
  102.      *
  103.      * @since 2.5.0
  104.      *
  105.      * @param int $id Uploaded attachment ID.
  106.      */
  107.     echo apply_filters( "async_upload_{$type}", $id );
  108. }
  109.