author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
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 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
17 |
/** Load WordPress Bootstrap */ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
18 |
require_once dirname( __DIR__ ) . '/wp-load.php'; |
0 | 19 |
|
16 | 20 |
require_once ABSPATH . 'wp-admin/admin.php'; |
0 | 21 |
|
9 | 22 |
header( 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ) ); |
0 | 23 |
|
24 |
if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) { |
|
16 | 25 |
require ABSPATH . 'wp-admin/includes/ajax-actions.php'; |
0 | 26 |
|
27 |
send_nosniff_header(); |
|
28 |
nocache_headers(); |
|
29 |
||
30 |
wp_ajax_upload_attachment(); |
|
31 |
die( '0' ); |
|
32 |
} |
|
33 |
||
5 | 34 |
if ( ! current_user_can( 'upload_files' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
wp_die( __( 'Sorry, you are not allowed to upload files.' ) ); |
5 | 36 |
} |
37 |
||
16 | 38 |
// Just fetch the detail form for that attachment. |
18 | 39 |
if ( isset( $_REQUEST['attachment_id'] ) && (int) $_REQUEST['attachment_id'] && $_REQUEST['fetch'] ) { |
40 |
$id = (int) $_REQUEST['attachment_id']; |
|
0 | 41 |
$post = get_post( $id ); |
16 | 42 |
if ( 'attachment' !== $post->post_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
wp_die( __( 'Invalid post type.' ) ); |
9 | 44 |
} |
0 | 45 |
|
46 |
switch ( $_REQUEST['fetch'] ) { |
|
9 | 47 |
case 3: |
18 | 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 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
|
18 | 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"><strong><?php echo esc_html( wp_html_excerpt( $title, 60, '…' ) ); ?></strong></span> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
64 |
<span class="media-list-subtitle"><?php echo esc_html( wp_basename( $file ) ); ?></span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
65 |
<div class="attachment-tools"> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
66 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
if ( current_user_can( 'edit_post', $id ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '">' . _x( 'Edit', 'media item' ) . '</a>'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
echo '<span class="edit-attachment">' . _x( 'Success', 'media item' ) . '</span>'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
<span class="media-item-copy-container copy-to-clipboard-container edit-attachment"> |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
74 |
<button type="button" class="button button-small copy-attachment-url" |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
75 |
data-clipboard-text="<?php echo esc_url( $file_url ); ?>" |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
76 |
><?php _e( 'Copy URL to clipboard' ); ?></button> |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
<span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
</span> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
</div> |
18 | 80 |
</div> |
81 |
</div> |
|
82 |
</div> |
|
83 |
<?php |
|
0 | 84 |
break; |
9 | 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 |
); |
|
0 | 94 |
break; |
95 |
default: |
|
9 | 96 |
add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); |
97 |
echo get_media_item( $id ); |
|
0 | 98 |
break; |
99 |
} |
|
100 |
exit; |
|
101 |
} |
|
102 |
||
9 | 103 |
check_admin_referer( 'media-form' ); |
0 | 104 |
|
105 |
$post_id = 0; |
|
106 |
if ( isset( $_REQUEST['post_id'] ) ) { |
|
107 |
$post_id = absint( $_REQUEST['post_id'] ); |
|
9 | 108 |
if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { |
0 | 109 |
$post_id = 0; |
9 | 110 |
} |
0 | 111 |
} |
112 |
||
113 |
$id = media_handle_upload( 'async-upload', $post_id ); |
|
9 | 114 |
if ( is_wp_error( $id ) ) { |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
115 |
$button_unique_id = uniqid( 'dismiss-' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
116 |
$message = sprintf( |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
'%s <strong>%s</strong><br />%s', |
16 | 118 |
sprintf( |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
119 |
'<button type="button" id="%s" class="dismiss button-link">%s</button>', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
120 |
esc_attr( $button_unique_id ), |
16 | 121 |
__( 'Dismiss' ) |
122 |
), |
|
123 |
sprintf( |
|
124 |
/* translators: %s: Name of the file that failed to upload. */ |
|
125 |
__( '“%s” has failed to upload.' ), |
|
126 |
esc_html( $_FILES['async-upload']['name'] ) |
|
127 |
), |
|
128 |
esc_html( $id->get_error_message() ) |
|
129 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
wp_admin_notice( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
'additional_classes' => array( 'error-div', 'error' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
'paragraph_wrap' => false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
); |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
137 |
echo "<script>jQuery( 'button#{$button_unique_id}' ).on( 'click', function() {jQuery(this).parents('div.media-item').slideUp(200, function(){jQuery(this).remove();})});</script>\n"; |
0 | 138 |
exit; |
139 |
} |
|
140 |
||
141 |
if ( $_REQUEST['short'] ) { |
|
5 | 142 |
// Short form response - attachment ID only. |
0 | 143 |
echo $id; |
144 |
} else { |
|
16 | 145 |
// Long form response - big chunk of HTML. |
0 | 146 |
$type = $_REQUEST['type']; |
5 | 147 |
|
148 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* Filters the returned ID of an uploaded attachment. |
5 | 150 |
* |
18 | 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` |
|
5 | 159 |
* |
160 |
* @since 2.5.0 |
|
161 |
* |
|
162 |
* @param int $id Uploaded attachment ID. |
|
163 |
*/ |
|
164 |
echo apply_filters( "async_upload_{$type}", $id ); |
|
0 | 165 |
} |