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