| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 /** Load WordPress Bootstrap */ 18 require_once dirname( __DIR__ ) . '/wp-load.php'; 19 20 require_once ABSPATH . 'wp-admin/admin.php'; 21 22 header( 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ) ); 23 24 if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) { 25 require ABSPATH . 'wp-admin/includes/ajax-actions.php'; 26 27 send_nosniff_header(); 28 nocache_headers(); 29 30 wp_ajax_upload_attachment(); 31 die( '0' ); 32 } 33 34 if ( ! current_user_can( 'upload_files' ) ) { 35 wp_die( __( 'Sorry, you are not allowed to upload files.' ) ); 36 } 37 38 // Just fetch the detail form for that attachment. 39 if ( isset( $_REQUEST['attachment_id'] ) && (int) $_REQUEST['attachment_id'] && $_REQUEST['fetch'] ) { 40 $id = (int) $_REQUEST['attachment_id']; 41 $post = get_post( $id ); 42 if ( 'attachment' !== $post->post_type ) { 43 wp_die( __( 'Invalid post type.' ) ); 44 } 45 46 switch ( $_REQUEST['fetch'] ) { 47 case 3: 48 ?> 49 <div class="media-item-wrapper"> 50 <div class="attachment-details"> 51 <?php 52 $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ); 53 if ( $thumb_url ) { 54 echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />'; 55 } 56 57 // Title shouldn't ever be empty, but use filename just in case. 58 $file = get_attached_file( $post->ID ); 59 $file_url = wp_get_attachment_url( $post->ID ); 60 $title = $post->post_title ? $post->post_title : wp_basename( $file ); 61 ?> 62 <div class="filename new"> 63 <span class="media-list-title word-wrap-break-word"><strong><?php echo esc_html( wp_html_excerpt( $title, 60, '…' ) ); ?></strong></span> 64 <span class="media-list-subtitle word-wrap-break-word"><?php echo esc_html( wp_basename( $file ) ); ?></span> 65 <div class="attachment-tools"> 66 <?php 67 if ( current_user_can( 'edit_post', $id ) ) { 68 echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '">' . _x( 'Edit', 'media item' ) . '</a>'; 69 } else { 70 echo '<span class="edit-attachment">' . _x( 'Success', 'media item' ) . '</span>'; 71 } 72 ?> 73 <span class="media-item-copy-container copy-to-clipboard-container edit-attachment"> 74 <button type="button" class="button button-small copy-attachment-url" 75 data-clipboard-text="<?php echo esc_url( $file_url ); ?>" 76 ><?php _e( 'Copy URL to clipboard' ); ?></button> 77 <span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span> 78 </span> 79 </div> 80 </div> 81 </div> 82 </div> 83 <?php 84 break; 85 case 2: 86 add_filter( 'attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2 ); 87 echo get_media_item( 88 $id, 89 array( 90 'send' => false, 91 'delete' => true, 92 ) 93 ); 94 break; 95 default: 96 add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); 97 echo get_media_item( $id ); 98 break; 99 } 100 exit; 101 } 102 103 check_admin_referer( 'media-form' ); 104 105 $post_id = 0; 106 if ( isset( $_REQUEST['post_id'] ) ) { 107 $post_id = absint( $_REQUEST['post_id'] ); 108 if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { 109 $post_id = 0; 110 } 111 } 112 113 $id = media_handle_upload( 'async-upload', $post_id ); 114 if ( is_wp_error( $id ) ) { 115 $button_unique_id = uniqid( 'dismiss-' ); 116 $error_description_id = uniqid( 'error-description-' ); 117 $message = sprintf( 118 '%s <strong>%s</strong><br />%s', 119 sprintf( 120 '<button type="button" id="%1$s" class="dismiss button-link" aria-describedby="%2$s">%3$s</button>', 121 esc_attr( $button_unique_id ), 122 esc_attr( $error_description_id ), 123 __( 'Dismiss' ) 124 ), 125 sprintf( 126 /* translators: %s: Name of the file that failed to upload. */ 127 __( '“%s” has failed to upload.' ), 128 esc_html( $_FILES['async-upload']['name'] ) 129 ), 130 esc_html( $id->get_error_message() ) 131 ); 132 133 wp_admin_notice( 134 $message, 135 array( 136 'id' => $error_description_id, 137 'additional_classes' => array( 'error-div', 'error' ), 138 'paragraph_wrap' => false, 139 ) 140 ); 141 142 $speak_message = sprintf( 143 /* translators: %s: Name of the file that failed to upload. */ 144 __( '%s has failed to upload.' ), 145 $_FILES['async-upload']['name'] 146 ); 147 148 $js_function = <<<'JS' 149 /** 150 * Announces the upload failure and wires up its dismiss button. 151 * 152 * @param {JQueryStatic} $ The jQuery object. 153 * @param {Object} args PHP exports. 154 * @param {string} args.speakMessage Message announced to assistive technology. 155 * @param {string} args.buttonSelector Selector for the notice's dismiss button. 156 * @return {void} 157 */ 158 ( $, { speakMessage, buttonSelector } ) => { 159 setTimeout( () => wp.a11y.speak( speakMessage ), 1500 ); 160 $( buttonSelector ).on( 'click', function () { 161 $( this ) 162 .parents( 'div.media-item' ) 163 .slideUp( 200, function () { 164 $( this ).remove(); 165 wp.a11y.speak( wp.i18n.__( 'Error dismissed.' ) ); 166 $( '#plupload-browse-button' ).trigger( 'focus' ); 167 } ); 168 } ); 169 } 170 JS; 171 wp_print_inline_script_tag( 172 sprintf( 173 '( %s )( jQuery, %s )', 174 $js_function, 175 wp_json_encode( 176 array( 177 'speakMessage' => $speak_message, 178 'buttonSelector' => 'button#' . $button_unique_id, 179 ), 180 JSON_HEX_TAG | JSON_UNESCAPED_SLASHES 181 ) 182 ) 183 ); 184 exit; 185 } 186 187 if ( $_REQUEST['short'] ) { 188 // Short form response - attachment ID only. 189 echo $id; 190 } else { 191 // Long form response - big chunk of HTML. 192 $type = $_REQUEST['type']; 193 194 /** 195 * Filters the returned ID of an uploaded attachment. 196 * 197 * The dynamic portion of the hook name, `$type`, refers to the attachment type. 198 * 199 * Possible hook names include: 200 * 201 * - `async_upload_audio` 202 * - `async_upload_file` 203 * - `async_upload_image` 204 * - `async_upload_video` 205 * 206 * @since 2.5.0 207 * 208 * @param int $id Uploaded attachment ID. 209 */ 210 echo apply_filters( "async_upload_{$type}", $id ); 211 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Sep 9 08:20:27 2026 | Cross-referenced by PHPXref |