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 |
/** |
|
3 |
* Edit post administration panel. |
|
4 |
* |
|
5 |
* Manage Post actions: post, edit, delete, etc. |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Administration |
|
9 |
*/ |
|
10 |
||
11 |
/** WordPress Administration Bootstrap */ |
|
16 | 12 |
require_once __DIR__ . '/admin.php'; |
0 | 13 |
|
9 | 14 |
$parent_file = 'edit.php'; |
0 | 15 |
$submenu_file = 'edit.php'; |
16 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
17 |
$action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : ''; |
0 | 18 |
|
9 | 19 |
if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) { |
20 |
wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 ); |
|
21 |
} elseif ( isset( $_GET['post'] ) ) { |
|
16 | 22 |
$post_id = (int) $_GET['post']; |
9 | 23 |
} elseif ( isset( $_POST['post_ID'] ) ) { |
16 | 24 |
$post_id = (int) $_POST['post_ID']; |
9 | 25 |
} else { |
16 | 26 |
$post_id = 0; |
9 | 27 |
} |
16 | 28 |
$post_ID = $post_id; |
0 | 29 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
* @global string $post_type Global post type. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
* @global WP_Post_Type $post_type_object Global post type object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
* @global WP_Post $post Global post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
*/ |
5 | 35 |
global $post_type, $post_type_object, $post; |
0 | 36 |
|
9 | 37 |
if ( $post_id ) { |
0 | 38 |
$post = get_post( $post_id ); |
9 | 39 |
} |
0 | 40 |
|
41 |
if ( $post ) { |
|
9 | 42 |
$post_type = $post->post_type; |
0 | 43 |
$post_type_object = get_post_type_object( $post_type ); |
44 |
} |
|
45 |
||
9 | 46 |
if ( isset( $_POST['post_type'] ) && $post && $post_type !== $_POST['post_type'] ) { |
47 |
wp_die( __( 'A post type mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 ); |
|
48 |
} |
|
49 |
||
50 |
if ( isset( $_POST['deletepost'] ) ) { |
|
0 | 51 |
$action = 'delete'; |
16 | 52 |
} elseif ( isset( $_POST['wp-preview'] ) && 'dopreview' === $_POST['wp-preview'] ) { |
0 | 53 |
$action = 'preview'; |
9 | 54 |
} |
0 | 55 |
|
56 |
$sendback = wp_get_referer(); |
|
57 |
if ( ! $sendback || |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
str_contains( $sendback, 'post.php' ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
str_contains( $sendback, 'post-new.php' ) ) { |
16 | 60 |
if ( 'attachment' === $post_type ) { |
0 | 61 |
$sendback = admin_url( 'upload.php' ); |
62 |
} else { |
|
63 |
$sendback = admin_url( 'edit.php' ); |
|
5 | 64 |
if ( ! empty( $post_type ) ) { |
65 |
$sendback = add_query_arg( 'post_type', $post_type, $sendback ); |
|
66 |
} |
|
0 | 67 |
} |
68 |
} else { |
|
9 | 69 |
$sendback = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'ids' ), $sendback ); |
0 | 70 |
} |
71 |
||
9 | 72 |
switch ( $action ) { |
73 |
case 'post-quickdraft-save': |
|
16 | 74 |
// Check nonce and capabilities. |
9 | 75 |
$nonce = $_REQUEST['_wpnonce']; |
76 |
$error_msg = false; |
|
5 | 77 |
|
16 | 78 |
// For output of the Quick Draft dashboard widget. |
9 | 79 |
require_once ABSPATH . 'wp-admin/includes/dashboard.php'; |
5 | 80 |
|
9 | 81 |
if ( ! wp_verify_nonce( $nonce, 'add-post' ) ) { |
82 |
$error_msg = __( 'Unable to submit this form, please refresh and try again.' ); |
|
83 |
} |
|
5 | 84 |
|
9 | 85 |
if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) { |
86 |
exit; |
|
87 |
} |
|
88 |
||
89 |
if ( $error_msg ) { |
|
90 |
return wp_dashboard_quick_press( $error_msg ); |
|
91 |
} |
|
5 | 92 |
|
9 | 93 |
$post = get_post( $_REQUEST['post_ID'] ); |
94 |
check_admin_referer( 'add-' . $post->post_type ); |
|
5 | 95 |
|
9 | 96 |
$_POST['comment_status'] = get_default_comment_status( $post->post_type ); |
97 |
$_POST['ping_status'] = get_default_comment_status( $post->post_type, 'pingback' ); |
|
5 | 98 |
|
16 | 99 |
// Wrap Quick Draft content in the Paragraph block. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
100 |
if ( ! str_contains( $_POST['content'], '<!-- wp:paragraph -->' ) ) { |
16 | 101 |
$_POST['content'] = sprintf( |
102 |
'<!-- wp:paragraph -->%s<!-- /wp:paragraph -->', |
|
103 |
str_replace( array( "\r\n", "\r", "\n" ), '<br />', $_POST['content'] ) |
|
104 |
); |
|
105 |
} |
|
106 |
||
9 | 107 |
edit_post(); |
108 |
wp_dashboard_quick_press(); |
|
109 |
exit; |
|
5 | 110 |
|
9 | 111 |
case 'postajaxpost': |
112 |
case 'post': |
|
113 |
check_admin_referer( 'add-' . $post_type ); |
|
16 | 114 |
$post_id = 'postajaxpost' === $action ? edit_post() : write_post(); |
9 | 115 |
redirect_post( $post_id ); |
16 | 116 |
exit; |
5 | 117 |
|
9 | 118 |
case 'edit': |
119 |
$editing = true; |
|
0 | 120 |
|
9 | 121 |
if ( empty( $post_id ) ) { |
122 |
wp_redirect( admin_url( 'post.php' ) ); |
|
16 | 123 |
exit; |
9 | 124 |
} |
0 | 125 |
|
9 | 126 |
if ( ! $post ) { |
19 | 127 |
wp_die( __( 'You attempted to edit an item that does not exist. Perhaps it was deleted?' ) ); |
9 | 128 |
} |
0 | 129 |
|
9 | 130 |
if ( ! $post_type_object ) { |
131 |
wp_die( __( 'Invalid post type.' ) ); |
|
132 |
} |
|
0 | 133 |
|
16 | 134 |
if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ), true ) ) { |
9 | 135 |
wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ) ); |
136 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
|
9 | 138 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
139 |
wp_die( __( 'Sorry, you are not allowed to edit this item.' ) ); |
|
140 |
} |
|
0 | 141 |
|
16 | 142 |
if ( 'trash' === $post->post_status ) { |
19 | 143 |
wp_die( __( 'You cannot edit this item because it is in the Trash. Please restore it and try again.' ) ); |
9 | 144 |
} |
0 | 145 |
|
9 | 146 |
if ( ! empty( $_GET['get-post-lock'] ) ) { |
147 |
check_admin_referer( 'lock-post_' . $post_id ); |
|
148 |
wp_set_post_lock( $post_id ); |
|
149 |
wp_redirect( get_edit_post_link( $post_id, 'url' ) ); |
|
16 | 150 |
exit; |
9 | 151 |
} |
0 | 152 |
|
9 | 153 |
$post_type = $post->post_type; |
16 | 154 |
if ( 'post' === $post_type ) { |
9 | 155 |
$parent_file = 'edit.php'; |
156 |
$submenu_file = 'edit.php'; |
|
157 |
$post_new_file = 'post-new.php'; |
|
16 | 158 |
} elseif ( 'attachment' === $post_type ) { |
9 | 159 |
$parent_file = 'upload.php'; |
160 |
$submenu_file = 'upload.php'; |
|
161 |
$post_new_file = 'media-new.php'; |
|
162 |
} else { |
|
16 | 163 |
if ( isset( $post_type_object ) && $post_type_object->show_in_menu && true !== $post_type_object->show_in_menu ) { |
9 | 164 |
$parent_file = $post_type_object->show_in_menu; |
165 |
} else { |
|
166 |
$parent_file = "edit.php?post_type=$post_type"; |
|
167 |
} |
|
168 |
$submenu_file = "edit.php?post_type=$post_type"; |
|
169 |
$post_new_file = "post-new.php?post_type=$post_type"; |
|
170 |
} |
|
171 |
||
172 |
$title = $post_type_object->labels->edit_item; |
|
0 | 173 |
|
9 | 174 |
/** |
175 |
* Allows replacement of the editor. |
|
176 |
* |
|
177 |
* @since 4.9.0 |
|
178 |
* |
|
16 | 179 |
* @param bool $replace Whether to replace the editor. Default false. |
180 |
* @param WP_Post $post Post object. |
|
9 | 181 |
*/ |
16 | 182 |
if ( true === apply_filters( 'replace_editor', false, $post ) ) { |
9 | 183 |
break; |
184 |
} |
|
185 |
||
186 |
if ( use_block_editor_for_post( $post ) ) { |
|
16 | 187 |
require ABSPATH . 'wp-admin/edit-form-blocks.php'; |
9 | 188 |
break; |
189 |
} |
|
190 |
||
191 |
if ( ! wp_check_post_lock( $post->ID ) ) { |
|
192 |
$active_post_lock = wp_set_post_lock( $post->ID ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
|
9 | 194 |
if ( 'attachment' !== $post_type ) { |
195 |
wp_enqueue_script( 'autosave' ); |
|
196 |
} |
|
197 |
} |
|
0 | 198 |
|
9 | 199 |
$post = get_post( $post_id, OBJECT, 'edit' ); |
0 | 200 |
|
9 | 201 |
if ( post_type_supports( $post_type, 'comments' ) ) { |
202 |
wp_enqueue_script( 'admin-comments' ); |
|
203 |
enqueue_comment_hotkeys_js(); |
|
204 |
} |
|
205 |
||
16 | 206 |
require ABSPATH . 'wp-admin/edit-form-advanced.php'; |
0 | 207 |
|
9 | 208 |
break; |
0 | 209 |
|
9 | 210 |
case 'editattachment': |
211 |
check_admin_referer( 'update-post_' . $post_id ); |
|
212 |
||
16 | 213 |
// Don't let these be changed. |
9 | 214 |
unset( $_POST['guid'] ); |
215 |
$_POST['post_type'] = 'attachment'; |
|
0 | 216 |
|
16 | 217 |
// Update the thumbnail filename. |
9 | 218 |
$newmeta = wp_get_attachment_metadata( $post_id, true ); |
219 |
$newmeta['thumb'] = wp_basename( $_POST['thumb'] ); |
|
0 | 220 |
|
9 | 221 |
wp_update_attachment_metadata( $post_id, $newmeta ); |
0 | 222 |
|
9 | 223 |
// Intentional fall-through to trigger the edit_post() call. |
224 |
case 'editpost': |
|
225 |
check_admin_referer( 'update-post_' . $post_id ); |
|
0 | 226 |
|
9 | 227 |
$post_id = edit_post(); |
0 | 228 |
|
16 | 229 |
// Session cookie flag that the post was saved. |
9 | 230 |
if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) { |
231 |
setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() ); |
|
232 |
} |
|
0 | 233 |
|
16 | 234 |
redirect_post( $post_id ); // Send user on their way while we keep working. |
0 | 235 |
|
16 | 236 |
exit; |
0 | 237 |
|
9 | 238 |
case 'trash': |
239 |
check_admin_referer( 'trash-post_' . $post_id ); |
|
0 | 240 |
|
9 | 241 |
if ( ! $post ) { |
242 |
wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) ); |
|
243 |
} |
|
0 | 244 |
|
9 | 245 |
if ( ! $post_type_object ) { |
246 |
wp_die( __( 'Invalid post type.' ) ); |
|
247 |
} |
|
0 | 248 |
|
9 | 249 |
if ( ! current_user_can( 'delete_post', $post_id ) ) { |
250 |
wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) ); |
|
251 |
} |
|
0 | 252 |
|
16 | 253 |
$user_id = wp_check_post_lock( $post_id ); |
254 |
if ( $user_id ) { |
|
9 | 255 |
$user = get_userdata( $user_id ); |
16 | 256 |
/* translators: %s: User's display name. */ |
9 | 257 |
wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) ); |
258 |
} |
|
0 | 259 |
|
9 | 260 |
if ( ! wp_trash_post( $post_id ) ) { |
16 | 261 |
wp_die( __( 'Error in moving the item to Trash.' ) ); |
9 | 262 |
} |
0 | 263 |
|
9 | 264 |
wp_redirect( |
265 |
add_query_arg( |
|
266 |
array( |
|
267 |
'trashed' => 1, |
|
268 |
'ids' => $post_id, |
|
269 |
), |
|
270 |
$sendback |
|
271 |
) |
|
272 |
); |
|
16 | 273 |
exit; |
0 | 274 |
|
9 | 275 |
case 'untrash': |
276 |
check_admin_referer( 'untrash-post_' . $post_id ); |
|
277 |
||
278 |
if ( ! $post ) { |
|
279 |
wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) ); |
|
280 |
} |
|
0 | 281 |
|
9 | 282 |
if ( ! $post_type_object ) { |
283 |
wp_die( __( 'Invalid post type.' ) ); |
|
284 |
} |
|
0 | 285 |
|
9 | 286 |
if ( ! current_user_can( 'delete_post', $post_id ) ) { |
287 |
wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) ); |
|
288 |
} |
|
0 | 289 |
|
9 | 290 |
if ( ! wp_untrash_post( $post_id ) ) { |
16 | 291 |
wp_die( __( 'Error in restoring the item from Trash.' ) ); |
9 | 292 |
} |
0 | 293 |
|
18 | 294 |
$sendback = add_query_arg( |
295 |
array( |
|
296 |
'untrashed' => 1, |
|
297 |
'ids' => $post_id, |
|
298 |
), |
|
299 |
$sendback |
|
300 |
); |
|
301 |
wp_redirect( $sendback ); |
|
16 | 302 |
exit; |
0 | 303 |
|
9 | 304 |
case 'delete': |
305 |
check_admin_referer( 'delete-post_' . $post_id ); |
|
0 | 306 |
|
9 | 307 |
if ( ! $post ) { |
308 |
wp_die( __( 'This item has already been deleted.' ) ); |
|
309 |
} |
|
0 | 310 |
|
9 | 311 |
if ( ! $post_type_object ) { |
312 |
wp_die( __( 'Invalid post type.' ) ); |
|
313 |
} |
|
0 | 314 |
|
9 | 315 |
if ( ! current_user_can( 'delete_post', $post_id ) ) { |
316 |
wp_die( __( 'Sorry, you are not allowed to delete this item.' ) ); |
|
317 |
} |
|
0 | 318 |
|
16 | 319 |
if ( 'attachment' === $post->post_type ) { |
9 | 320 |
$force = ( ! MEDIA_TRASH ); |
321 |
if ( ! wp_delete_attachment( $post_id, $force ) ) { |
|
16 | 322 |
wp_die( __( 'Error in deleting the attachment.' ) ); |
9 | 323 |
} |
324 |
} else { |
|
325 |
if ( ! wp_delete_post( $post_id, true ) ) { |
|
16 | 326 |
wp_die( __( 'Error in deleting the item.' ) ); |
9 | 327 |
} |
328 |
} |
|
0 | 329 |
|
9 | 330 |
wp_redirect( add_query_arg( 'deleted', 1, $sendback ) ); |
16 | 331 |
exit; |
9 | 332 |
|
333 |
case 'preview': |
|
334 |
check_admin_referer( 'update-post_' . $post_id ); |
|
0 | 335 |
|
9 | 336 |
$url = post_preview(); |
0 | 337 |
|
9 | 338 |
wp_redirect( $url ); |
16 | 339 |
exit; |
0 | 340 |
|
9 | 341 |
case 'toggle-custom-fields': |
18 | 342 |
check_admin_referer( 'toggle-custom-fields', 'toggle-custom-fields-nonce' ); |
0 | 343 |
|
9 | 344 |
$current_user_id = get_current_user_id(); |
345 |
if ( $current_user_id ) { |
|
346 |
$enable_custom_fields = (bool) get_user_meta( $current_user_id, 'enable_custom_fields', true ); |
|
347 |
update_user_meta( $current_user_id, 'enable_custom_fields', ! $enable_custom_fields ); |
|
348 |
} |
|
349 |
||
350 |
wp_safe_redirect( wp_get_referer() ); |
|
16 | 351 |
exit; |
0 | 352 |
|
9 | 353 |
default: |
354 |
/** |
|
355 |
* Fires for a given custom post action request. |
|
356 |
* |
|
357 |
* The dynamic portion of the hook name, `$action`, refers to the custom post action. |
|
358 |
* |
|
359 |
* @since 4.6.0 |
|
360 |
* |
|
361 |
* @param int $post_id Post ID sent with the request. |
|
362 |
*/ |
|
363 |
do_action( "post_action_{$action}", $post_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
|
9 | 365 |
wp_redirect( admin_url( 'edit.php' ) ); |
16 | 366 |
exit; |
367 |
} // End switch. |
|
368 |
||
369 |
require_once ABSPATH . 'wp-admin/admin-footer.php'; |