[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> post.php (source)

   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  
  11  /** WordPress Administration Bootstrap */
  12  require_once  __DIR__ . '/admin.php';
  13  
  14  $parent_file  = 'edit.php';
  15  $submenu_file = 'edit.php';
  16  
  17  $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : '';
  18  
  19  if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) {
  20      wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
  21  } elseif ( isset( $_GET['post'] ) ) {
  22      $post_id = (int) $_GET['post'];
  23  } elseif ( isset( $_POST['post_ID'] ) ) {
  24      $post_id = (int) $_POST['post_ID'];
  25  } else {
  26      $post_id = 0;
  27  }
  28  $post_ID = $post_id;
  29  
  30  /**
  31   * @global string       $post_type        Global post type.
  32   * @global WP_Post_Type $post_type_object Global post type object.
  33   * @global WP_Post      $post             Global post object.
  34   */
  35  global $post_type, $post_type_object, $post;
  36  
  37  if ( $post_id ) {
  38      $post = get_post( $post_id );
  39  }
  40  
  41  if ( $post ) {
  42      $post_type        = $post->post_type;
  43      $post_type_object = get_post_type_object( $post_type );
  44  }
  45  
  46  if ( isset( $_POST['post_type'] ) && $post && $post_type !== $_POST['post_type'] ) {
  47      wp_die( __( 'A post type mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
  48  }
  49  
  50  if ( isset( $_POST['deletepost'] ) ) {
  51      $action = 'delete';
  52  } elseif ( isset( $_POST['wp-preview'] ) && 'dopreview' === $_POST['wp-preview'] ) {
  53      $action = 'preview';
  54  }
  55  
  56  $sendback = wp_get_referer();
  57  if ( ! $sendback ||
  58      str_contains( $sendback, 'post.php' ) ||
  59      str_contains( $sendback, 'post-new.php' ) ) {
  60      if ( 'attachment' === $post_type ) {
  61          $sendback = admin_url( 'upload.php' );
  62      } else {
  63          $sendback = admin_url( 'edit.php' );
  64          if ( ! empty( $post_type ) ) {
  65              $sendback = add_query_arg( 'post_type', $post_type, $sendback );
  66          }
  67      }
  68  } else {
  69      $sendback = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'ids' ), $sendback );
  70  }
  71  
  72  switch ( $action ) {
  73      case 'post-quickdraft-save':
  74          // Check nonce and capabilities.
  75          $nonce     = $_REQUEST['_wpnonce'];
  76          $error_msg = false;
  77  
  78          // For output of the Quick Draft dashboard widget.
  79          require_once  ABSPATH . 'wp-admin/includes/dashboard.php';
  80  
  81          if ( ! wp_verify_nonce( $nonce, 'add-post' ) ) {
  82              $error_msg = __( 'Unable to submit this form, please refresh and try again.' );
  83          }
  84  
  85          if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
  86              exit;
  87          }
  88  
  89          if ( $error_msg ) {
  90              return wp_dashboard_quick_press( $error_msg );
  91          }
  92  
  93          $post = get_post( $_REQUEST['post_ID'] );
  94          check_admin_referer( 'add-' . $post->post_type );
  95  
  96          $_POST['comment_status'] = get_default_comment_status( $post->post_type );
  97          $_POST['ping_status']    = get_default_comment_status( $post->post_type, 'pingback' );
  98  
  99          $quickdraft_post_title   = trim( $_POST['post_title'] );
 100          $quickdraft_post_content = trim( $_POST['content'] );
 101  
 102          if ( empty( $quickdraft_post_title ) && empty( $quickdraft_post_content ) ) {
 103              return wp_dashboard_quick_press( __( 'Cannot create a draft post with empty title and content.' ) );
 104          }
 105  
 106          // Wrap Quick Draft content in a Paragraph block.
 107          if (
 108              use_block_editor_for_post_type( $post->post_type ) &&
 109              ! empty( $quickdraft_post_content ) &&
 110              ! str_contains( $quickdraft_post_content, '<!-- wp:paragraph -->' )
 111          ) {
 112              // Note that `edit_post()` reads from the $_POST superglobal by reference.
 113              $_POST['content'] = sprintf(
 114                  '<!-- wp:paragraph -->%s<!-- /wp:paragraph -->',
 115                  str_replace( array( "\r\n", "\r", "\n" ), '<br />', $quickdraft_post_content )
 116              );
 117          }
 118  
 119          edit_post();
 120          wp_dashboard_quick_press( __( 'Draft created successfully.' ), 'success' );
 121          exit;
 122  
 123      case 'postajaxpost':
 124      case 'post':
 125          check_admin_referer( 'add-' . $post_type );
 126          $post_id = 'postajaxpost' === $action ? edit_post() : write_post();
 127          redirect_post( $post_id );
 128          exit;
 129  
 130      case 'edit':
 131          $editing = true;
 132  
 133          if ( empty( $post_id ) ) {
 134              wp_redirect( admin_url( 'post.php' ) );
 135              exit;
 136          }
 137  
 138          if ( ! $post ) {
 139              wp_die( __( 'You attempted to edit an item that does not exist. Perhaps it was deleted?' ), 404 );
 140          }
 141  
 142          if ( ! $post_type_object ) {
 143              wp_die( __( 'Invalid post type.' ), 400 );
 144          }
 145  
 146          if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ), true ) ) {
 147              wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ), 403 );
 148          }
 149  
 150          if ( ! current_user_can( 'edit_post', $post_id ) ) {
 151              wp_die( __( 'Sorry, you are not allowed to edit this item.' ), 403 );
 152          }
 153  
 154          if ( 'trash' === $post->post_status ) {
 155              wp_die( __( 'You cannot edit this item because it is in the Trash. Please restore it and try again.' ), 409 );
 156          }
 157  
 158          if ( ! empty( $_GET['get-post-lock'] ) ) {
 159              check_admin_referer( 'lock-post_' . $post_id );
 160              wp_set_post_lock( $post_id );
 161              wp_redirect( get_edit_post_link( $post_id, 'url' ) );
 162              exit;
 163          }
 164  
 165          $post_type = $post->post_type;
 166          if ( 'post' === $post_type ) {
 167              $parent_file   = 'edit.php';
 168              $submenu_file  = 'edit.php';
 169              $post_new_file = 'post-new.php';
 170          } elseif ( 'attachment' === $post_type ) {
 171              $parent_file   = 'upload.php';
 172              $submenu_file  = 'upload.php';
 173              $post_new_file = 'media-new.php';
 174          } else {
 175              if ( $post_type_object->show_in_menu && true !== $post_type_object->show_in_menu ) {
 176                  $parent_file = $post_type_object->show_in_menu;
 177              } else {
 178                  $parent_file = "edit.php?post_type=$post_type";
 179              }
 180              $submenu_file  = "edit.php?post_type=$post_type";
 181              $post_new_file = "post-new.php?post_type=$post_type";
 182          }
 183  
 184          $title = $post_type_object->labels->edit_item;
 185  
 186          /**
 187           * Allows replacement of the editor.
 188           *
 189           * @since 4.9.0
 190           *
 191           * @param bool    $replace Whether to replace the editor. Default false.
 192           * @param WP_Post $post    Post object.
 193           */
 194          if ( true === apply_filters( 'replace_editor', false, $post ) ) {
 195              break;
 196          }
 197  
 198          if ( use_block_editor_for_post( $post ) ) {
 199              require  ABSPATH . 'wp-admin/edit-form-blocks.php';
 200              break;
 201          }
 202  
 203          if ( ! wp_check_post_lock( $post->ID ) ) {
 204              $active_post_lock = wp_set_post_lock( $post->ID );
 205  
 206              if ( 'attachment' !== $post_type ) {
 207                  wp_enqueue_script( 'autosave' );
 208              }
 209          }
 210  
 211          $post = get_post( $post_id, OBJECT, 'edit' );
 212  
 213          if ( post_type_supports( $post_type, 'comments' ) ) {
 214              wp_enqueue_script( 'admin-comments' );
 215              enqueue_comment_hotkeys_js();
 216          }
 217  
 218          require  ABSPATH . 'wp-admin/edit-form-advanced.php';
 219  
 220          break;
 221  
 222      case 'editattachment':
 223          check_admin_referer( 'update-post_' . $post_id );
 224  
 225          // Don't let these be changed.
 226          unset( $_POST['guid'] );
 227          $_POST['post_type'] = 'attachment';
 228  
 229          // Update the thumbnail filename.
 230          $newmeta          = wp_get_attachment_metadata( $post_id, true );
 231          $newmeta['thumb'] = wp_basename( $_POST['thumb'] );
 232  
 233          wp_update_attachment_metadata( $post_id, $newmeta );
 234  
 235          // Intentional fall-through to trigger the edit_post() call.
 236      case 'editpost':
 237          check_admin_referer( 'update-post_' . $post_id );
 238  
 239          $post_id = edit_post();
 240  
 241          // Session cookie flag that the post was saved.
 242          if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
 243              setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
 244          }
 245  
 246          redirect_post( $post_id ); // Send user on their way while we keep working.
 247  
 248          exit;
 249  
 250      case 'trash':
 251          check_admin_referer( 'trash-post_' . $post_id );
 252  
 253          if ( ! $post ) {
 254              wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ), 410 );
 255          }
 256  
 257          if ( ! $post_type_object ) {
 258              wp_die( __( 'Invalid post type.' ), 400 );
 259          }
 260  
 261          if ( ! current_user_can( 'delete_post', $post_id ) ) {
 262              wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ), 403 );
 263          }
 264  
 265          $user_id = wp_check_post_lock( $post_id );
 266          if ( $user_id ) {
 267              $user = get_userdata( $user_id );
 268              /* translators: %s: User's display name. */
 269              wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ), 409 );
 270          }
 271  
 272          if ( ! wp_trash_post( $post_id ) ) {
 273              wp_die( __( 'Error in moving the item to Trash.' ), 500 );
 274          }
 275  
 276          wp_redirect(
 277              add_query_arg(
 278                  array(
 279                      'trashed' => 1,
 280                      'ids'     => $post_id,
 281                  ),
 282                  $sendback
 283              )
 284          );
 285          exit;
 286  
 287      case 'untrash':
 288          check_admin_referer( 'untrash-post_' . $post_id );
 289  
 290          if ( ! $post ) {
 291              wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ), 410 );
 292          }
 293  
 294          if ( ! $post_type_object ) {
 295              wp_die( __( 'Invalid post type.' ), 400 );
 296          }
 297  
 298          if ( ! current_user_can( 'delete_post', $post_id ) ) {
 299              wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ), 403 );
 300          }
 301  
 302          if ( ! wp_untrash_post( $post_id ) ) {
 303              wp_die( __( 'Error in restoring the item from Trash.' ), 500 );
 304          }
 305  
 306          $sendback = add_query_arg(
 307              array(
 308                  'untrashed' => 1,
 309                  'ids'       => $post_id,
 310              ),
 311              $sendback
 312          );
 313          wp_redirect( $sendback );
 314          exit;
 315  
 316      case 'delete':
 317          check_admin_referer( 'delete-post_' . $post_id );
 318  
 319          if ( ! $post ) {
 320              wp_die( __( 'This item has already been deleted.' ), 410 );
 321          }
 322  
 323          if ( ! $post_type_object ) {
 324              wp_die( __( 'Invalid post type.' ), 400 );
 325          }
 326  
 327          if ( ! current_user_can( 'delete_post', $post_id ) ) {
 328              wp_die( __( 'Sorry, you are not allowed to delete this item.' ), 403 );
 329          }
 330  
 331          if ( 'attachment' === $post->post_type ) {
 332              $force = ( ! MEDIA_TRASH );
 333              if ( ! wp_delete_attachment( $post_id, $force ) ) {
 334                  wp_die( __( 'Error in deleting the attachment.' ), 500 );
 335              }
 336          } else {
 337              if ( ! wp_delete_post( $post_id, true ) ) {
 338                  wp_die( __( 'Error in deleting the item.' ), 500 );
 339              }
 340          }
 341  
 342          wp_redirect( add_query_arg( 'deleted', 1, $sendback ) );
 343          exit;
 344  
 345      case 'preview':
 346          check_admin_referer( 'update-post_' . $post_id );
 347  
 348          $url = post_preview();
 349  
 350          wp_redirect( $url );
 351          exit;
 352  
 353      case 'toggle-custom-fields':
 354          check_admin_referer( 'toggle-custom-fields', 'toggle-custom-fields-nonce' );
 355  
 356          $current_user_id = get_current_user_id();
 357          if ( $current_user_id ) {
 358              $enable_custom_fields = (bool) get_user_meta( $current_user_id, 'enable_custom_fields', true );
 359              update_user_meta( $current_user_id, 'enable_custom_fields', ! $enable_custom_fields );
 360          }
 361  
 362          wp_safe_redirect( wp_get_referer() );
 363          exit;
 364  
 365      default:
 366          /**
 367           * Fires for a given custom post action request.
 368           *
 369           * The dynamic portion of the hook name, `$action`, refers to the custom post action.
 370           *
 371           * @since 4.6.0
 372           *
 373           * @param int $post_id Post ID sent with the request.
 374           */
 375          do_action( "post_action_{$action}", $post_id );
 376  
 377          wp_redirect( admin_url( 'edit.php' ) );
 378          exit;
 379  } // End switch.
 380  
 381  require_once  ABSPATH . 'wp-admin/admin-footer.php';


Generated : Sun Jul 19 08:20:17 2026 Cross-referenced by PHPXref