post.php 9.4 KB

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