wp/wp-admin/post.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    21 elseif ( isset( $_POST['post_ID'] ) )
    21 elseif ( isset( $_POST['post_ID'] ) )
    22  	$post_id = $post_ID = (int) $_POST['post_ID'];
    22  	$post_id = $post_ID = (int) $_POST['post_ID'];
    23 else
    23 else
    24  	$post_id = $post_ID = 0;
    24  	$post_id = $post_ID = 0;
    25 
    25 
       
    26 /**
       
    27  * @global string  $post_type
       
    28  * @global object  $post_type_object
       
    29  * @global WP_Post $post
       
    30  */
    26 global $post_type, $post_type_object, $post;
    31 global $post_type, $post_type_object, $post;
    27 
    32 
    28 if ( $post_id )
    33 if ( $post_id )
    29 	$post = get_post( $post_id );
    34 	$post = get_post( $post_id );
    30 
    35 
    31 if ( $post ) {
    36 if ( $post ) {
    32 	$post_type = $post->post_type;
    37 	$post_type = $post->post_type;
    33 	$post_type_object = get_post_type_object( $post_type );
    38 	$post_type_object = get_post_type_object( $post_type );
    34 }
       
    35 
       
    36 /**
       
    37  * Redirect to previous page.
       
    38  *
       
    39  * @param int $post_id Optional. Post ID.
       
    40  */
       
    41 function redirect_post($post_id = '') {
       
    42 	if ( isset($_POST['save']) || isset($_POST['publish']) ) {
       
    43 		$status = get_post_status( $post_id );
       
    44 
       
    45 		if ( isset( $_POST['publish'] ) ) {
       
    46 			switch ( $status ) {
       
    47 				case 'pending':
       
    48 					$message = 8;
       
    49 					break;
       
    50 				case 'future':
       
    51 					$message = 9;
       
    52 					break;
       
    53 				default:
       
    54 					$message = 6;
       
    55 			}
       
    56 		} else {
       
    57 				$message = 'draft' == $status ? 10 : 1;
       
    58 		}
       
    59 
       
    60 		$location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
       
    61 	} elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
       
    62 		$location = add_query_arg( 'message', 2, wp_get_referer() );
       
    63 		$location = explode('#', $location);
       
    64 		$location = $location[0] . '#postcustom';
       
    65 	} elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
       
    66 		$location = add_query_arg( 'message', 3, wp_get_referer() );
       
    67 		$location = explode('#', $location);
       
    68 		$location = $location[0] . '#postcustom';
       
    69 	} else {
       
    70 		$location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * Filter the post redirect destination URL.
       
    75 	 *
       
    76 	 * @since 2.9.0
       
    77 	 *
       
    78 	 * @param string $location The destination URL.
       
    79 	 * @param int    $post_id  The post ID.
       
    80 	 */
       
    81 	wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
       
    82 	exit;
       
    83 }
    39 }
    84 
    40 
    85 if ( isset( $_POST['deletepost'] ) )
    41 if ( isset( $_POST['deletepost'] ) )
    86 	$action = 'delete';
    42 	$action = 'delete';
    87 elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
    43 elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
   113 	require_once ABSPATH . 'wp-admin/includes/dashboard.php';
    69 	require_once ABSPATH . 'wp-admin/includes/dashboard.php';
   114 
    70 
   115 	if ( ! wp_verify_nonce( $nonce, 'add-post' ) )
    71 	if ( ! wp_verify_nonce( $nonce, 'add-post' ) )
   116 		$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
    72 		$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
   117 
    73 
   118 	if ( ! current_user_can( 'edit_posts' ) )
    74 	if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
   119 		$error_msg = __( 'Oops, you don’t have access to add new drafts.' );
    75 		exit;
       
    76 	}
   120 
    77 
   121 	if ( $error_msg )
    78 	if ( $error_msg )
   122 		return wp_dashboard_quick_press( $error_msg );
    79 		return wp_dashboard_quick_press( $error_msg );
   123 
    80 
   124 	$post = get_post( $_REQUEST['post_ID'] );
    81 	$post = get_post( $_REQUEST['post_ID'] );
   125 	check_admin_referer( 'add-' . $post->post_type );
    82 	check_admin_referer( 'add-' . $post->post_type );
   126 
    83 
   127 	$_POST['comment_status'] = get_option( 'default_comment_status' );
    84 	$_POST['comment_status'] = get_default_comment_status( $post->post_type );
   128 	$_POST['ping_status'] = get_option( 'default_ping_status' );
    85 	$_POST['ping_status']    = get_default_comment_status( $post->post_type, 'pingback' );
   129 
    86 
   130 	edit_post();
    87 	edit_post();
   131 	wp_dashboard_quick_press();
    88 	wp_dashboard_quick_press();
   132 	exit;
    89 	exit;
   133 
    90 
   148 
   105 
   149 	if ( ! $post )
   106 	if ( ! $post )
   150 		wp_die( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
   107 		wp_die( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
   151 
   108 
   152 	if ( ! $post_type_object )
   109 	if ( ! $post_type_object )
   153 		wp_die( __( 'Unknown post type.' ) );
   110 		wp_die( __( 'Invalid post type.' ) );
       
   111 
       
   112 	if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ) ) ) {
       
   113 		wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
       
   114 	}
   154 
   115 
   155 	if ( ! current_user_can( 'edit_post', $post_id ) )
   116 	if ( ! current_user_can( 'edit_post', $post_id ) )
   156 		wp_die( __( 'You are not allowed to edit this item.' ) );
   117 		wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
   157 
   118 
   158 	if ( 'trash' == $post->post_status )
   119 	if ( 'trash' == $post->post_status )
   159 		wp_die( __( 'You can’t edit this item because it is in the Trash. Please restore it and try again.' ) );
   120 		wp_die( __( 'You can’t edit this item because it is in the Trash. Please restore it and try again.' ) );
   160 
   121 
   161 	if ( ! empty( $_GET['get-post-lock'] ) ) {
   122 	if ( ! empty( $_GET['get-post-lock'] ) ) {
       
   123 		check_admin_referer( 'lock-post_' . $post_id );
   162 		wp_set_post_lock( $post_id );
   124 		wp_set_post_lock( $post_id );
   163 		wp_redirect( get_edit_post_link( $post_id, 'url' ) );
   125 		wp_redirect( get_edit_post_link( $post_id, 'url' ) );
   164 		exit();
   126 		exit();
   165 	}
   127 	}
   166 
   128 
   180 			$parent_file = "edit.php?post_type=$post_type";
   142 			$parent_file = "edit.php?post_type=$post_type";
   181 		$submenu_file = "edit.php?post_type=$post_type";
   143 		$submenu_file = "edit.php?post_type=$post_type";
   182 		$post_new_file = "post-new.php?post_type=$post_type";
   144 		$post_new_file = "post-new.php?post_type=$post_type";
   183 	}
   145 	}
   184 
   146 
       
   147 	/**
       
   148 	 * Allows replacement of the editor.
       
   149 	 *
       
   150 	 * @since 4.9.0
       
   151 	 *
       
   152 	 * @param boolean      Whether to replace the editor. Default false.
       
   153 	 * @param object $post Post object.
       
   154 	 */
       
   155 	if ( apply_filters( 'replace_editor', false, $post ) === true ) {
       
   156 		break;
       
   157 	}
       
   158 
   185 	if ( ! wp_check_post_lock( $post->ID ) ) {
   159 	if ( ! wp_check_post_lock( $post->ID ) ) {
   186 		$active_post_lock = wp_set_post_lock( $post->ID );
   160 		$active_post_lock = wp_set_post_lock( $post->ID );
   187 
   161 
   188 		if ( 'attachment' !== $post_type )
   162 		if ( 'attachment' !== $post_type )
   189 			wp_enqueue_script('autosave');
   163 			wp_enqueue_script('autosave');
   190 	}
       
   191 
       
   192 	if ( is_multisite() ) {
       
   193 		add_action( 'admin_footer', '_admin_notice_post_locked' );
       
   194 	} else {
       
   195 		$check_users = get_users( array( 'fields' => 'ID', 'number' => 2 ) );
       
   196 
       
   197 		if ( count( $check_users ) > 1 )
       
   198 			add_action( 'admin_footer', '_admin_notice_post_locked' );
       
   199 
       
   200 		unset( $check_users );
       
   201 	}
   164 	}
   202 
   165 
   203 	$title = $post_type_object->labels->edit_item;
   166 	$title = $post_type_object->labels->edit_item;
   204 	$post = get_post($post_id, OBJECT, 'edit');
   167 	$post = get_post($post_id, OBJECT, 'edit');
   205 
   168 
   230 
   193 
   231 	$post_id = edit_post();
   194 	$post_id = edit_post();
   232 
   195 
   233 	// Session cookie flag that the post was saved
   196 	// Session cookie flag that the post was saved
   234 	if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
   197 	if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
   235 		setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS );
   198 		setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
   236 	}
   199 	}
   237 
   200 
   238 	redirect_post($post_id); // Send user on their way while we keep working
   201 	redirect_post($post_id); // Send user on their way while we keep working
   239 
   202 
   240 	exit();
   203 	exit();
   244 
   207 
   245 	if ( ! $post )
   208 	if ( ! $post )
   246 		wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
   209 		wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
   247 
   210 
   248 	if ( ! $post_type_object )
   211 	if ( ! $post_type_object )
   249 		wp_die( __( 'Unknown post type.' ) );
   212 		wp_die( __( 'Invalid post type.' ) );
   250 
   213 
   251 	if ( ! current_user_can( 'delete_post', $post_id ) )
   214 	if ( ! current_user_can( 'delete_post', $post_id ) )
   252 		wp_die( __( 'You are not allowed to move this item to the Trash.' ) );
   215 		wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
   253 
   216 
   254 	if ( $user_id = wp_check_post_lock( $post_id ) ) {
   217 	if ( $user_id = wp_check_post_lock( $post_id ) ) {
   255 		$user = get_userdata( $user_id );
   218 		$user = get_userdata( $user_id );
   256 		wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
   219 		wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
   257 	}
   220 	}
   267 
   230 
   268 	if ( ! $post )
   231 	if ( ! $post )
   269 		wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
   232 		wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
   270 
   233 
   271 	if ( ! $post_type_object )
   234 	if ( ! $post_type_object )
   272 		wp_die( __( 'Unknown post type.' ) );
   235 		wp_die( __( 'Invalid post type.' ) );
   273 
   236 
   274 	if ( ! current_user_can( 'delete_post', $post_id ) )
   237 	if ( ! current_user_can( 'delete_post', $post_id ) )
   275 		wp_die( __( 'You are not allowed to move this item out of the Trash.' ) );
   238 		wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
   276 
   239 
   277 	if ( ! wp_untrash_post( $post_id ) )
   240 	if ( ! wp_untrash_post( $post_id ) )
   278 		wp_die( __( 'Error in restoring from Trash.' ) );
   241 		wp_die( __( 'Error in restoring from Trash.' ) );
   279 
   242 
   280 	wp_redirect( add_query_arg('untrashed', 1, $sendback) );
   243 	wp_redirect( add_query_arg('untrashed', 1, $sendback) );
   285 
   248 
   286 	if ( ! $post )
   249 	if ( ! $post )
   287 		wp_die( __( 'This item has already been deleted.' ) );
   250 		wp_die( __( 'This item has already been deleted.' ) );
   288 
   251 
   289 	if ( ! $post_type_object )
   252 	if ( ! $post_type_object )
   290 		wp_die( __( 'Unknown post type.' ) );
   253 		wp_die( __( 'Invalid post type.' ) );
   291 
   254 
   292 	if ( ! current_user_can( 'delete_post', $post_id ) )
   255 	if ( ! current_user_can( 'delete_post', $post_id ) )
   293 		wp_die( __( 'You are not allowed to delete this item.' ) );
   256 		wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
   294 
   257 
   295 	$force = ! EMPTY_TRASH_DAYS;
       
   296 	if ( $post->post_type == 'attachment' ) {
   258 	if ( $post->post_type == 'attachment' ) {
   297 		$force = ( $force || ! MEDIA_TRASH );
   259 		$force = ( ! MEDIA_TRASH );
   298 		if ( ! wp_delete_attachment( $post_id, $force ) )
   260 		if ( ! wp_delete_attachment( $post_id, $force ) )
   299 			wp_die( __( 'Error in deleting.' ) );
   261 			wp_die( __( 'Error in deleting.' ) );
   300 	} else {
   262 	} else {
   301 		if ( ! wp_delete_post( $post_id, $force ) )
   263 		if ( ! wp_delete_post( $post_id, true ) )
   302 			wp_die( __( 'Error in deleting.' ) );
   264 			wp_die( __( 'Error in deleting.' ) );
   303 	}
   265 	}
   304 
   266 
   305 	wp_redirect( add_query_arg('deleted', 1, $sendback) );
   267 	wp_redirect( add_query_arg('deleted', 1, $sendback) );
   306 	exit();
   268 	exit();
   312 
   274 
   313 	wp_redirect($url);
   275 	wp_redirect($url);
   314 	exit();
   276 	exit();
   315 
   277 
   316 default:
   278 default:
       
   279 	/**
       
   280 	 * Fires for a given custom post action request.
       
   281 	 *
       
   282 	 * The dynamic portion of the hook name, `$action`, refers to the custom post action.
       
   283 	 *
       
   284 	 * @since 4.6.0
       
   285 	 *
       
   286 	 * @param int $post_id Post ID sent with the request.
       
   287 	 */
       
   288 	do_action( "post_action_{$action}", $post_id );
       
   289 
   317 	wp_redirect( admin_url('edit.php') );
   290 	wp_redirect( admin_url('edit.php') );
   318 	exit();
   291 	exit();
   319 } // end switch
   292 } // end switch
   320 include( ABSPATH . 'wp-admin/admin-footer.php' );
   293 include( ABSPATH . 'wp-admin/admin-footer.php' );