post.php 8.4 KB

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