3 * Edit post administration panel.
5 * Manage Post actions: post, edit, delete, etc.
8 * @subpackage Administration
11 /** WordPress Administration Bootstrap */
12 require_once( dirname( __FILE__ ) . '/admin.php' );
14 $parent_file = 'edit.php';
15 $submenu_file = 'edit.php';
17 wp_reset_vars( array( 'action' ) );
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'];
24 $post_id = $post_ID = 0;
27 * @global string $post_type
28 * @global object $post_type_object
29 * @global WP_Post $post
31 global $post_type, $post_type_object, $post;
34 $post = get_post( $post_id );
37 $post_type = $post->post_type;
38 $post_type_object = get_post_type_object( $post_type );
42 * Redirect to previous page.
44 * @param int $post_id Optional. Post ID.
46 function redirect_post($post_id = '') {
47 if ( isset($_POST['save']) || isset($_POST['publish']) ) {
48 $status = get_post_status( $post_id );
50 if ( isset( $_POST['publish'] ) ) {
62 $message = 'draft' == $status ? 10 : 1;
65 $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
66 } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
67 $location = add_query_arg( 'message', 2, wp_get_referer() );
68 $location = explode('#', $location);
69 $location = $location[0] . '#postcustom';
70 } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
71 $location = add_query_arg( 'message', 3, wp_get_referer() );
72 $location = explode('#', $location);
73 $location = $location[0] . '#postcustom';
75 $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
79 * Filter the post redirect destination URL.
83 * @param string $location The destination URL.
84 * @param int $post_id The post ID.
86 wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
90 if ( isset( $_POST['deletepost'] ) )
92 elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
95 $sendback = wp_get_referer();
97 strpos( $sendback, 'post.php' ) !== false ||
98 strpos( $sendback, 'post-new.php' ) !== false ) {
99 if ( 'attachment' == $post_type ) {
100 $sendback = admin_url( 'upload.php' );
102 $sendback = admin_url( 'edit.php' );
103 if ( ! empty( $post_type ) ) {
104 $sendback = add_query_arg( 'post_type', $post_type, $sendback );
108 $sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), $sendback );
112 case 'post-quickdraft-save':
113 // Check nonce and capabilities
114 $nonce = $_REQUEST['_wpnonce'];
117 // For output of the quickdraft dashboard widget
118 require_once ABSPATH . 'wp-admin/includes/dashboard.php';
120 if ( ! wp_verify_nonce( $nonce, 'add-post' ) )
121 $error_msg = __( 'Unable to submit this form, please refresh and try again.' );
123 if ( ! current_user_can( 'edit_posts' ) ) {
128 return wp_dashboard_quick_press( $error_msg );
130 $post = get_post( $_REQUEST['post_ID'] );
131 check_admin_referer( 'add-' . $post->post_type );
133 $_POST['comment_status'] = get_default_comment_status( $post->post_type );
134 $_POST['ping_status'] = get_default_comment_status( $post->post_type, 'pingback' );
137 wp_dashboard_quick_press();
142 check_admin_referer( 'add-' . $post_type );
143 $post_id = 'postajaxpost' == $action ? edit_post() : write_post();
144 redirect_post( $post_id );
150 if ( empty( $post_id ) ) {
151 wp_redirect( admin_url('post.php') );
156 wp_die( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
158 if ( ! $post_type_object )
159 wp_die( __( 'Unknown post type.' ) );
161 if ( ! current_user_can( 'edit_post', $post_id ) )
162 wp_die( __( 'You are not allowed to edit this item.' ) );
164 if ( 'trash' == $post->post_status )
165 wp_die( __( 'You can’t edit this item because it is in the Trash. Please restore it and try again.' ) );
167 if ( ! empty( $_GET['get-post-lock'] ) ) {
168 check_admin_referer( 'lock-post_' . $post_id );
169 wp_set_post_lock( $post_id );
170 wp_redirect( get_edit_post_link( $post_id, 'url' ) );
174 $post_type = $post->post_type;
175 if ( 'post' == $post_type ) {
176 $parent_file = "edit.php";
177 $submenu_file = "edit.php";
178 $post_new_file = "post-new.php";
179 } elseif ( 'attachment' == $post_type ) {
180 $parent_file = 'upload.php';
181 $submenu_file = 'upload.php';
182 $post_new_file = 'media-new.php';
184 if ( isset( $post_type_object ) && $post_type_object->show_in_menu && $post_type_object->show_in_menu !== true )
185 $parent_file = $post_type_object->show_in_menu;
187 $parent_file = "edit.php?post_type=$post_type";
188 $submenu_file = "edit.php?post_type=$post_type";
189 $post_new_file = "post-new.php?post_type=$post_type";
192 if ( ! wp_check_post_lock( $post->ID ) ) {
193 $active_post_lock = wp_set_post_lock( $post->ID );
195 if ( 'attachment' !== $post_type )
196 wp_enqueue_script('autosave');
199 if ( is_multisite() ) {
200 add_action( 'admin_footer', '_admin_notice_post_locked' );
202 $check_users = get_users( array( 'fields' => 'ID', 'number' => 2 ) );
204 if ( count( $check_users ) > 1 )
205 add_action( 'admin_footer', '_admin_notice_post_locked' );
207 unset( $check_users );
210 $title = $post_type_object->labels->edit_item;
211 $post = get_post($post_id, OBJECT, 'edit');
213 if ( post_type_supports($post_type, 'comments') ) {
214 wp_enqueue_script('admin-comments');
215 enqueue_comment_hotkeys_js();
218 include( ABSPATH . 'wp-admin/edit-form-advanced.php' );
222 case 'editattachment':
223 check_admin_referer('update-post_' . $post_id);
225 // Don't let these be changed
226 unset($_POST['guid']);
227 $_POST['post_type'] = 'attachment';
229 // Update the thumbnail filename
230 $newmeta = wp_get_attachment_metadata( $post_id, true );
231 $newmeta['thumb'] = $_POST['thumb'];
233 wp_update_attachment_metadata( $post_id, $newmeta );
236 check_admin_referer('update-post_' . $post_id);
238 $post_id = edit_post();
240 // Session cookie flag that the post was saved
241 if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
242 setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS );
245 redirect_post($post_id); // Send user on their way while we keep working
250 check_admin_referer('trash-post_' . $post_id);
253 wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
255 if ( ! $post_type_object )
256 wp_die( __( 'Unknown post type.' ) );
258 if ( ! current_user_can( 'delete_post', $post_id ) )
259 wp_die( __( 'You are not allowed to move this item to the Trash.' ) );
261 if ( $user_id = wp_check_post_lock( $post_id ) ) {
262 $user = get_userdata( $user_id );
263 wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
266 if ( ! wp_trash_post( $post_id ) )
267 wp_die( __( 'Error in moving to Trash.' ) );
269 wp_redirect( add_query_arg( array('trashed' => 1, 'ids' => $post_id), $sendback ) );
273 check_admin_referer('untrash-post_' . $post_id);
276 wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
278 if ( ! $post_type_object )
279 wp_die( __( 'Unknown post type.' ) );
281 if ( ! current_user_can( 'delete_post', $post_id ) )
282 wp_die( __( 'You are not allowed to restore this item from the Trash.' ) );
284 if ( ! wp_untrash_post( $post_id ) )
285 wp_die( __( 'Error in restoring from Trash.' ) );
287 wp_redirect( add_query_arg('untrashed', 1, $sendback) );
291 check_admin_referer('delete-post_' . $post_id);
294 wp_die( __( 'This item has already been deleted.' ) );
296 if ( ! $post_type_object )
297 wp_die( __( 'Unknown post type.' ) );
299 if ( ! current_user_can( 'delete_post', $post_id ) )
300 wp_die( __( 'You are not allowed to delete this item.' ) );
302 $force = ! EMPTY_TRASH_DAYS;
303 if ( $post->post_type == 'attachment' ) {
304 $force = ( $force || ! MEDIA_TRASH );
305 if ( ! wp_delete_attachment( $post_id, $force ) )
306 wp_die( __( 'Error in deleting.' ) );
308 if ( ! wp_delete_post( $post_id, $force ) )
309 wp_die( __( 'Error in deleting.' ) );
312 wp_redirect( add_query_arg('deleted', 1, $sendback) );
316 check_admin_referer( 'update-post_' . $post_id );
318 $url = post_preview();
324 wp_redirect( admin_url('edit.php') );
327 include( ABSPATH . 'wp-admin/admin-footer.php' );