post.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 __DIR__ . '/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. false !== strpos( $sendback, 'post.php' ) ||
  49. false !== strpos( $sendback, 'post-new.php' ) ) {
  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 Quick Draft 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. // Wrap Quick Draft content in the Paragraph block.
  82. if ( false === strpos( $_POST['content'], '<!-- wp:paragraph -->' ) ) {
  83. $_POST['content'] = sprintf(
  84. '<!-- wp:paragraph -->%s<!-- /wp:paragraph -->',
  85. str_replace( array( "\r\n", "\r", "\n" ), '<br />', $_POST['content'] )
  86. );
  87. }
  88. edit_post();
  89. wp_dashboard_quick_press();
  90. exit;
  91. case 'postajaxpost':
  92. case 'post':
  93. check_admin_referer( 'add-' . $post_type );
  94. $post_id = 'postajaxpost' === $action ? edit_post() : write_post();
  95. redirect_post( $post_id );
  96. exit;
  97. case 'edit':
  98. $editing = true;
  99. if ( empty( $post_id ) ) {
  100. wp_redirect( admin_url( 'post.php' ) );
  101. exit;
  102. }
  103. if ( ! $post ) {
  104. wp_die( __( 'You attempted to edit an item that does not exist. Perhaps it was deleted?' ) );
  105. }
  106. if ( ! $post_type_object ) {
  107. wp_die( __( 'Invalid post type.' ) );
  108. }
  109. if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ), true ) ) {
  110. wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
  111. }
  112. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  113. wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
  114. }
  115. if ( 'trash' === $post->post_status ) {
  116. wp_die( __( 'You cannot edit this item because it is in the Trash. Please restore it and try again.' ) );
  117. }
  118. if ( ! empty( $_GET['get-post-lock'] ) ) {
  119. check_admin_referer( 'lock-post_' . $post_id );
  120. wp_set_post_lock( $post_id );
  121. wp_redirect( get_edit_post_link( $post_id, 'url' ) );
  122. exit;
  123. }
  124. $post_type = $post->post_type;
  125. if ( 'post' === $post_type ) {
  126. $parent_file = 'edit.php';
  127. $submenu_file = 'edit.php';
  128. $post_new_file = 'post-new.php';
  129. } elseif ( 'attachment' === $post_type ) {
  130. $parent_file = 'upload.php';
  131. $submenu_file = 'upload.php';
  132. $post_new_file = 'media-new.php';
  133. } else {
  134. if ( isset( $post_type_object ) && $post_type_object->show_in_menu && true !== $post_type_object->show_in_menu ) {
  135. $parent_file = $post_type_object->show_in_menu;
  136. } else {
  137. $parent_file = "edit.php?post_type=$post_type";
  138. }
  139. $submenu_file = "edit.php?post_type=$post_type";
  140. $post_new_file = "post-new.php?post_type=$post_type";
  141. }
  142. $title = $post_type_object->labels->edit_item;
  143. /**
  144. * Allows replacement of the editor.
  145. *
  146. * @since 4.9.0
  147. *
  148. * @param bool $replace Whether to replace the editor. Default false.
  149. * @param WP_Post $post Post object.
  150. */
  151. if ( true === apply_filters( 'replace_editor', false, $post ) ) {
  152. break;
  153. }
  154. if ( use_block_editor_for_post( $post ) ) {
  155. require ABSPATH . 'wp-admin/edit-form-blocks.php';
  156. break;
  157. }
  158. if ( ! wp_check_post_lock( $post->ID ) ) {
  159. $active_post_lock = wp_set_post_lock( $post->ID );
  160. if ( 'attachment' !== $post_type ) {
  161. wp_enqueue_script( 'autosave' );
  162. }
  163. }
  164. $post = get_post( $post_id, OBJECT, 'edit' );
  165. if ( post_type_supports( $post_type, 'comments' ) ) {
  166. wp_enqueue_script( 'admin-comments' );
  167. enqueue_comment_hotkeys_js();
  168. }
  169. require ABSPATH . 'wp-admin/edit-form-advanced.php';
  170. break;
  171. case 'editattachment':
  172. check_admin_referer( 'update-post_' . $post_id );
  173. // Don't let these be changed.
  174. unset( $_POST['guid'] );
  175. $_POST['post_type'] = 'attachment';
  176. // Update the thumbnail filename.
  177. $newmeta = wp_get_attachment_metadata( $post_id, true );
  178. $newmeta['thumb'] = wp_basename( $_POST['thumb'] );
  179. wp_update_attachment_metadata( $post_id, $newmeta );
  180. // Intentional fall-through to trigger the edit_post() call.
  181. case 'editpost':
  182. check_admin_referer( 'update-post_' . $post_id );
  183. $post_id = edit_post();
  184. // Session cookie flag that the post was saved.
  185. if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
  186. setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
  187. }
  188. redirect_post( $post_id ); // Send user on their way while we keep working.
  189. exit;
  190. case 'trash':
  191. check_admin_referer( 'trash-post_' . $post_id );
  192. if ( ! $post ) {
  193. wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
  194. }
  195. if ( ! $post_type_object ) {
  196. wp_die( __( 'Invalid post type.' ) );
  197. }
  198. if ( ! current_user_can( 'delete_post', $post_id ) ) {
  199. wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
  200. }
  201. $user_id = wp_check_post_lock( $post_id );
  202. if ( $user_id ) {
  203. $user = get_userdata( $user_id );
  204. /* translators: %s: User's display name. */
  205. wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
  206. }
  207. if ( ! wp_trash_post( $post_id ) ) {
  208. wp_die( __( 'Error in moving the item to Trash.' ) );
  209. }
  210. wp_redirect(
  211. add_query_arg(
  212. array(
  213. 'trashed' => 1,
  214. 'ids' => $post_id,
  215. ),
  216. $sendback
  217. )
  218. );
  219. exit;
  220. case 'untrash':
  221. check_admin_referer( 'untrash-post_' . $post_id );
  222. if ( ! $post ) {
  223. wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
  224. }
  225. if ( ! $post_type_object ) {
  226. wp_die( __( 'Invalid post type.' ) );
  227. }
  228. if ( ! current_user_can( 'delete_post', $post_id ) ) {
  229. wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
  230. }
  231. if ( ! wp_untrash_post( $post_id ) ) {
  232. wp_die( __( 'Error in restoring the item from Trash.' ) );
  233. }
  234. $sendback = add_query_arg(
  235. array(
  236. 'untrashed' => 1,
  237. 'ids' => $post_id,
  238. ),
  239. $sendback
  240. );
  241. wp_redirect( $sendback );
  242. exit;
  243. case 'delete':
  244. check_admin_referer( 'delete-post_' . $post_id );
  245. if ( ! $post ) {
  246. wp_die( __( 'This item has already been deleted.' ) );
  247. }
  248. if ( ! $post_type_object ) {
  249. wp_die( __( 'Invalid post type.' ) );
  250. }
  251. if ( ! current_user_can( 'delete_post', $post_id ) ) {
  252. wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
  253. }
  254. if ( 'attachment' === $post->post_type ) {
  255. $force = ( ! MEDIA_TRASH );
  256. if ( ! wp_delete_attachment( $post_id, $force ) ) {
  257. wp_die( __( 'Error in deleting the attachment.' ) );
  258. }
  259. } else {
  260. if ( ! wp_delete_post( $post_id, true ) ) {
  261. wp_die( __( 'Error in deleting the item.' ) );
  262. }
  263. }
  264. wp_redirect( add_query_arg( 'deleted', 1, $sendback ) );
  265. exit;
  266. case 'preview':
  267. check_admin_referer( 'update-post_' . $post_id );
  268. $url = post_preview();
  269. wp_redirect( $url );
  270. exit;
  271. case 'toggle-custom-fields':
  272. check_admin_referer( 'toggle-custom-fields', 'toggle-custom-fields-nonce' );
  273. $current_user_id = get_current_user_id();
  274. if ( $current_user_id ) {
  275. $enable_custom_fields = (bool) get_user_meta( $current_user_id, 'enable_custom_fields', true );
  276. update_user_meta( $current_user_id, 'enable_custom_fields', ! $enable_custom_fields );
  277. }
  278. wp_safe_redirect( wp_get_referer() );
  279. exit;
  280. default:
  281. /**
  282. * Fires for a given custom post action request.
  283. *
  284. * The dynamic portion of the hook name, `$action`, refers to the custom post action.
  285. *
  286. * @since 4.6.0
  287. *
  288. * @param int $post_id Post ID sent with the request.
  289. */
  290. do_action( "post_action_{$action}", $post_id );
  291. wp_redirect( admin_url( 'edit.php' ) );
  292. exit;
  293. } // End switch.
  294. require_once ABSPATH . 'wp-admin/admin-footer.php';