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