[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/ -> async-upload.php (source)

   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/plain; 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'] ) && intval( $_REQUEST['attachment_id'] ) && $_REQUEST['fetch'] ) {
  43      $id   = intval( $_REQUEST['attachment_id'] );
  44      $post = get_post( $id );
  45      if ( 'attachment' != $post->post_type ) {
  46          wp_die( __( 'Invalid post type.' ) );
  47      }
  48      if ( ! current_user_can( 'edit_post', $id ) ) {
  49          wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
  50      }
  51  
  52      switch ( $_REQUEST['fetch'] ) {
  53          case 3:
  54              $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true );
  55              if ( $thumb_url ) {
  56                  echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
  57              }
  58              echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
  59  
  60              // Title shouldn't ever be empty, but use filename just in case.
  61              $file  = get_attached_file( $post->ID );
  62              $title = $post->post_title ? $post->post_title : wp_basename( $file );
  63              echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ) . '</span></div>';
  64              break;
  65          case 2:
  66              add_filter( 'attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2 );
  67              echo get_media_item(
  68                  $id,
  69                  array(
  70                      'send'   => false,
  71                      'delete' => true,
  72                  )
  73              );
  74              break;
  75          default:
  76              add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
  77              echo get_media_item( $id );
  78              break;
  79      }
  80      exit;
  81  }
  82  
  83  check_admin_referer( 'media-form' );
  84  
  85  $post_id = 0;
  86  if ( isset( $_REQUEST['post_id'] ) ) {
  87      $post_id = absint( $_REQUEST['post_id'] );
  88      if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
  89          $post_id = 0;
  90      }
  91  }
  92  
  93  $id = media_handle_upload( 'async-upload', $post_id );
  94  if ( is_wp_error( $id ) ) {
  95      printf(
  96          '<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
  97          sprintf(
  98              '<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
  99              __( 'Dismiss' )
 100          ),
 101          sprintf(
 102              /* translators: %s: Name of the file that failed to upload. */
 103              __( '&#8220;%s&#8221; has failed to upload.' ),
 104              esc_html( $_FILES['async-upload']['name'] )
 105          ),
 106          esc_html( $id->get_error_message() )
 107      );
 108      exit;
 109  }
 110  
 111  if ( $_REQUEST['short'] ) {
 112      // Short form response - attachment ID only.
 113      echo $id;
 114  } else {
 115      // Long form response - big chunk of html.
 116      $type = $_REQUEST['type'];
 117  
 118      /**
 119       * Filters the returned ID of an uploaded attachment.
 120       *
 121       * The dynamic portion of the hook name, `$type`, refers to the attachment type,
 122       * such as 'image', 'audio', 'video', 'file', etc.
 123       *
 124       * @since 2.5.0
 125       *
 126       * @param int $id Uploaded attachment ID.
 127       */
 128      echo apply_filters( "async_upload_{$type}", $id );
 129  }


Generated: Wed Oct 23 08:20:01 2019 Cross-referenced by PHPXref 0.7