[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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( __DIR__ ) . '/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      require  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'] ) && (int) $_REQUEST['attachment_id'] && $_REQUEST['fetch'] ) {
  43      $id   = (int) $_REQUEST['attachment_id'];
  44      $post = get_post( $id );
  45      if ( 'attachment' !== $post->post_type ) {
  46          wp_die( __( 'Invalid post type.' ) );
  47      }
  48  
  49      switch ( $_REQUEST['fetch'] ) {
  50          case 3:
  51              ?>
  52              <div class="media-item-wrapper">
  53                  <div class="attachment-details">
  54                      <?php
  55                      $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true );
  56                      if ( $thumb_url ) {
  57                          echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
  58                      }
  59  
  60                      // Title shouldn't ever be empty, but use filename just in case.
  61                      $file     = get_attached_file( $post->ID );
  62                      $file_url = wp_get_attachment_url( $post->ID );
  63                      $title    = $post->post_title ? $post->post_title : wp_basename( $file );
  64                      ?>
  65                      <div class="filename new">
  66                          <span class="media-list-title"><strong><?php echo esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ); ?></strong></span>
  67                          <span class="media-list-subtitle"><?php echo esc_html( wp_basename( $file ) ); ?></span>
  68                          <div class="attachment-tools">
  69                              <?php
  70                              if ( current_user_can( 'edit_post', $id ) ) {
  71                                  echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '">' . _x( 'Edit', 'media item' ) . '</a>';
  72                              } else {
  73                                  echo '<span class="edit-attachment">' . _x( 'Success', 'media item' ) . '</span>';
  74                              }
  75                              ?>
  76                              <span class="media-item-copy-container copy-to-clipboard-container edit-attachment">
  77                                  <button type="button" class="button button-small copy-attachment-url"
  78                                      data-clipboard-text="<?php echo esc_url( $file_url ); ?>"
  79                                  ><?php _e( 'Copy URL to clipboard' ); ?></button>
  80                                  <span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span>
  81                              </span>
  82                          </div>
  83                      </div>
  84                  </div>
  85              </div>
  86              <?php
  87              break;
  88          case 2:
  89              add_filter( 'attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2 );
  90              echo get_media_item(
  91                  $id,
  92                  array(
  93                      'send'   => false,
  94                      'delete' => true,
  95                  )
  96              );
  97              break;
  98          default:
  99              add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
 100              echo get_media_item( $id );
 101              break;
 102      }
 103      exit;
 104  }
 105  
 106  check_admin_referer( 'media-form' );
 107  
 108  $post_id = 0;
 109  if ( isset( $_REQUEST['post_id'] ) ) {
 110      $post_id = absint( $_REQUEST['post_id'] );
 111      if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
 112          $post_id = 0;
 113      }
 114  }
 115  
 116  $id = media_handle_upload( 'async-upload', $post_id );
 117  if ( is_wp_error( $id ) ) {
 118      $message = sprintf(
 119          '%s <strong>%s</strong><br />%s',
 120          sprintf(
 121              '<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
 122              __( 'Dismiss' )
 123          ),
 124          sprintf(
 125              /* translators: %s: Name of the file that failed to upload. */
 126              __( '&#8220;%s&#8221; has failed to upload.' ),
 127              esc_html( $_FILES['async-upload']['name'] )
 128          ),
 129          esc_html( $id->get_error_message() )
 130      );
 131      wp_admin_notice(
 132          $message,
 133          array(
 134              'additional_classes' => array( 'error-div', 'error' ),
 135              'paragraph_wrap'     => false,
 136          )
 137      );
 138      exit;
 139  }
 140  
 141  if ( $_REQUEST['short'] ) {
 142      // Short form response - attachment ID only.
 143      echo $id;
 144  } else {
 145      // Long form response - big chunk of HTML.
 146      $type = $_REQUEST['type'];
 147  
 148      /**
 149       * Filters the returned ID of an uploaded attachment.
 150       *
 151       * The dynamic portion of the hook name, `$type`, refers to the attachment type.
 152       *
 153       * Possible hook names include:
 154       *
 155       *  - `async_upload_audio`
 156       *  - `async_upload_file`
 157       *  - `async_upload_image`
 158       *  - `async_upload_video`
 159       *
 160       * @since 2.5.0
 161       *
 162       * @param int $id Uploaded attachment ID.
 163       */
 164      echo apply_filters( "async_upload_{$type}", $id );
 165  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref