[ Index ]

PHP Cross Reference of WordPress

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( dirname( __FILE__ ) . '/admin.php' );
  13  
  14  $parent_file  = 'edit.php';
  15  $submenu_file = 'edit.php';
  16  
  17  wp_reset_vars( array( '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
  32   * @global object  $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      strpos( $sendback, 'post.php' ) !== false ||
  59      strpos( $sendback, 'post-new.php' ) !== false ) {
  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 quickdraft 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          edit_post();
 100          wp_dashboard_quick_press();
 101          exit;
 102  
 103      case 'postajaxpost':
 104      case 'post':
 105          check_admin_referer( 'add-' . $post_type );
 106          $post_id = 'postajaxpost' == $action ? edit_post() : write_post();
 107          redirect_post( $post_id );
 108          exit();
 109  
 110      case 'edit':
 111          $editing = true;
 112  
 113          if ( empty( $post_id ) ) {
 114              wp_redirect( admin_url( 'post.php' ) );
 115              exit();
 116          }
 117  
 118          if ( ! $post ) {
 119              wp_die( __( 'You attempted to edit an item that doesn&#8217;t exist. Perhaps it was deleted?' ) );
 120          }
 121  
 122          if ( ! $post_type_object ) {
 123              wp_die( __( 'Invalid post type.' ) );
 124          }
 125  
 126          if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ) ) ) {
 127              wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
 128          }
 129  
 130          if ( ! current_user_can( 'edit_post', $post_id ) ) {
 131              wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
 132          }
 133  
 134          if ( 'trash' == $post->post_status ) {
 135              wp_die( __( 'You can&#8217;t edit this item because it is in the Trash. Please restore it and try again.' ) );
 136          }
 137  
 138          if ( ! empty( $_GET['get-post-lock'] ) ) {
 139              check_admin_referer( 'lock-post_' . $post_id );
 140              wp_set_post_lock( $post_id );
 141              wp_redirect( get_edit_post_link( $post_id, 'url' ) );
 142              exit();
 143          }
 144  
 145          $post_type = $post->post_type;
 146          if ( 'post' == $post_type ) {
 147              $parent_file   = 'edit.php';
 148              $submenu_file  = 'edit.php';
 149              $post_new_file = 'post-new.php';
 150          } elseif ( 'attachment' == $post_type ) {
 151              $parent_file   = 'upload.php';
 152              $submenu_file  = 'upload.php';
 153              $post_new_file = 'media-new.php';
 154          } else {
 155              if ( isset( $post_type_object ) && $post_type_object->show_in_menu && $post_type_object->show_in_menu !== true ) {
 156                  $parent_file = $post_type_object->show_in_menu;
 157              } else {
 158                  $parent_file = "edit.php?post_type=$post_type";
 159              }
 160              $submenu_file  = "edit.php?post_type=$post_type";
 161              $post_new_file = "post-new.php?post_type=$post_type";
 162          }
 163  
 164          $title = $post_type_object->labels->edit_item;
 165  
 166          /**
 167           * Allows replacement of the editor.
 168           *
 169           * @since 4.9.0
 170           *
 171           * @param boolean      Whether to replace the editor. Default false.
 172           * @param object $post Post object.
 173           */
 174          if ( apply_filters( 'replace_editor', false, $post ) === true ) {
 175              break;
 176          }
 177  
 178          if ( use_block_editor_for_post( $post ) ) {
 179              include( ABSPATH . 'wp-admin/edit-form-blocks.php' );
 180              break;
 181          }
 182  
 183          if ( ! wp_check_post_lock( $post->ID ) ) {
 184              $active_post_lock = wp_set_post_lock( $post->ID );
 185  
 186              if ( 'attachment' !== $post_type ) {
 187                  wp_enqueue_script( 'autosave' );
 188              }
 189          }
 190  
 191          $post = get_post( $post_id, OBJECT, 'edit' );
 192  
 193          if ( post_type_supports( $post_type, 'comments' ) ) {
 194              wp_enqueue_script( 'admin-comments' );
 195              enqueue_comment_hotkeys_js();
 196          }
 197  
 198          include( ABSPATH . 'wp-admin/edit-form-advanced.php' );
 199  
 200          break;
 201  
 202      case 'editattachment':
 203          check_admin_referer( 'update-post_' . $post_id );
 204  
 205          // Don't let these be changed
 206          unset( $_POST['guid'] );
 207          $_POST['post_type'] = 'attachment';
 208  
 209          // Update the thumbnail filename
 210          $newmeta          = wp_get_attachment_metadata( $post_id, true );
 211          $newmeta['thumb'] = wp_basename( $_POST['thumb'] );
 212  
 213          wp_update_attachment_metadata( $post_id, $newmeta );
 214  
 215          // Intentional fall-through to trigger the edit_post() call.
 216      case 'editpost':
 217          check_admin_referer( 'update-post_' . $post_id );
 218  
 219          $post_id = edit_post();
 220  
 221          // Session cookie flag that the post was saved
 222          if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
 223              setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
 224          }
 225  
 226          redirect_post( $post_id ); // Send user on their way while we keep working
 227  
 228          exit();
 229  
 230      case 'trash':
 231          check_admin_referer( 'trash-post_' . $post_id );
 232  
 233          if ( ! $post ) {
 234              wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
 235          }
 236  
 237          if ( ! $post_type_object ) {
 238              wp_die( __( 'Invalid post type.' ) );
 239          }
 240  
 241          if ( ! current_user_can( 'delete_post', $post_id ) ) {
 242              wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
 243          }
 244  
 245          $user_id = wp_check_post_lock( $post_id );
 246          if ( $user_id ) {
 247              $user = get_userdata( $user_id );
 248              /* translators: %s: User's display name. */
 249              wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
 250          }
 251  
 252          if ( ! wp_trash_post( $post_id ) ) {
 253              wp_die( __( 'Error in moving to Trash.' ) );
 254          }
 255  
 256          wp_redirect(
 257              add_query_arg(
 258                  array(
 259                      'trashed' => 1,
 260                      'ids'     => $post_id,
 261                  ),
 262                  $sendback
 263              )
 264          );
 265          exit();
 266  
 267      case 'untrash':
 268          check_admin_referer( 'untrash-post_' . $post_id );
 269  
 270          if ( ! $post ) {
 271              wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
 272          }
 273  
 274          if ( ! $post_type_object ) {
 275              wp_die( __( 'Invalid post type.' ) );
 276          }
 277  
 278          if ( ! current_user_can( 'delete_post', $post_id ) ) {
 279              wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
 280          }
 281  
 282          if ( ! wp_untrash_post( $post_id ) ) {
 283              wp_die( __( 'Error in restoring from Trash.' ) );
 284          }
 285  
 286          wp_redirect( add_query_arg( 'untrashed', 1, $sendback ) );
 287          exit();
 288  
 289      case 'delete':
 290          check_admin_referer( 'delete-post_' . $post_id );
 291  
 292          if ( ! $post ) {
 293              wp_die( __( 'This item has already been deleted.' ) );
 294          }
 295  
 296          if ( ! $post_type_object ) {
 297              wp_die( __( 'Invalid post type.' ) );
 298          }
 299  
 300          if ( ! current_user_can( 'delete_post', $post_id ) ) {
 301              wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
 302          }
 303  
 304          if ( $post->post_type == 'attachment' ) {
 305              $force = ( ! MEDIA_TRASH );
 306              if ( ! wp_delete_attachment( $post_id, $force ) ) {
 307                  wp_die( __( 'Error in deleting.' ) );
 308              }
 309          } else {
 310              if ( ! wp_delete_post( $post_id, true ) ) {
 311                  wp_die( __( 'Error in deleting.' ) );
 312              }
 313          }
 314  
 315          wp_redirect( add_query_arg( 'deleted', 1, $sendback ) );
 316          exit();
 317  
 318      case 'preview':
 319          check_admin_referer( 'update-post_' . $post_id );
 320  
 321          $url = post_preview();
 322  
 323          wp_redirect( $url );
 324          exit();
 325  
 326      case 'toggle-custom-fields':
 327          check_admin_referer( 'toggle-custom-fields' );
 328  
 329          $current_user_id = get_current_user_id();
 330          if ( $current_user_id ) {
 331              $enable_custom_fields = (bool) get_user_meta( $current_user_id, 'enable_custom_fields', true );
 332              update_user_meta( $current_user_id, 'enable_custom_fields', ! $enable_custom_fields );
 333          }
 334  
 335          wp_safe_redirect( wp_get_referer() );
 336          exit();
 337  
 338      default:
 339          /**
 340           * Fires for a given custom post action request.
 341           *
 342           * The dynamic portion of the hook name, `$action`, refers to the custom post action.
 343           *
 344           * @since 4.6.0
 345           *
 346           * @param int $post_id Post ID sent with the request.
 347           */
 348          do_action( "post_action_{$action}", $post_id );
 349  
 350          wp_redirect( admin_url( 'edit.php' ) );
 351          exit();
 352  } // end switch
 353  include( ABSPATH . 'wp-admin/admin-footer.php' );


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1