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