[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Edit Posts Administration Screen.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once( dirname( __FILE__ ) . '/admin.php' );
  11  
  12  if ( ! $typenow ) {
  13      wp_die( __( 'Invalid post type.' ) );
  14  }
  15  
  16  if ( ! in_array( $typenow, get_post_types( array( 'show_ui' => true ) ) ) ) {
  17      wp_die( __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
  18  }
  19  
  20  if ( 'attachment' === $typenow ) {
  21      if ( wp_redirect( admin_url( 'upload.php' ) ) ) {
  22          exit;
  23      }
  24  }
  25  
  26  /**
  27   * @global string       $post_type
  28   * @global WP_Post_Type $post_type_object
  29   */
  30  global $post_type, $post_type_object;
  31  
  32  $post_type        = $typenow;
  33  $post_type_object = get_post_type_object( $post_type );
  34  
  35  if ( ! $post_type_object ) {
  36      wp_die( __( 'Invalid post type.' ) );
  37  }
  38  
  39  if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
  40      wp_die(
  41          '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
  42          '<p>' . __( 'Sorry, you are not allowed to edit posts in this post type.' ) . '</p>',
  43          403
  44      );
  45  }
  46  
  47  $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
  48  $pagenum       = $wp_list_table->get_pagenum();
  49  
  50  // Back-compat for viewing comments of an entry
  51  foreach ( array( 'p', 'attachment_id', 'page_id' ) as $_redirect ) {
  52      if ( ! empty( $_REQUEST[ $_redirect ] ) ) {
  53          wp_redirect( admin_url( 'edit-comments.php?p=' . absint( $_REQUEST[ $_redirect ] ) ) );
  54          exit;
  55      }
  56  }
  57  unset( $_redirect );
  58  
  59  if ( 'post' != $post_type ) {
  60      $parent_file   = "edit.php?post_type=$post_type";
  61      $submenu_file  = "edit.php?post_type=$post_type";
  62      $post_new_file = "post-new.php?post_type=$post_type";
  63  } else {
  64      $parent_file   = 'edit.php';
  65      $submenu_file  = 'edit.php';
  66      $post_new_file = 'post-new.php';
  67  }
  68  
  69  $doaction = $wp_list_table->current_action();
  70  
  71  if ( $doaction ) {
  72      check_admin_referer( 'bulk-posts' );
  73  
  74      $sendback = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'locked', 'ids' ), wp_get_referer() );
  75      if ( ! $sendback ) {
  76          $sendback = admin_url( $parent_file );
  77      }
  78      $sendback = add_query_arg( 'paged', $pagenum, $sendback );
  79      if ( strpos( $sendback, 'post.php' ) !== false ) {
  80          $sendback = admin_url( $post_new_file );
  81      }
  82  
  83      if ( 'delete_all' == $doaction ) {
  84          // Prepare for deletion of all posts with a specified post status (i.e. Empty trash).
  85          $post_status = preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['post_status'] );
  86          // Validate the post status exists.
  87          if ( get_post_status_object( $post_status ) ) {
  88              $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type=%s AND post_status = %s", $post_type, $post_status ) );
  89          }
  90          $doaction = 'delete';
  91      } elseif ( isset( $_REQUEST['media'] ) ) {
  92          $post_ids = $_REQUEST['media'];
  93      } elseif ( isset( $_REQUEST['ids'] ) ) {
  94          $post_ids = explode( ',', $_REQUEST['ids'] );
  95      } elseif ( ! empty( $_REQUEST['post'] ) ) {
  96          $post_ids = array_map( 'intval', $_REQUEST['post'] );
  97      }
  98  
  99      if ( ! isset( $post_ids ) ) {
 100          wp_redirect( $sendback );
 101          exit;
 102      }
 103  
 104      switch ( $doaction ) {
 105          case 'trash':
 106              $trashed = 0;
 107              $locked  = 0;
 108  
 109              foreach ( (array) $post_ids as $post_id ) {
 110                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 111                      wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
 112                  }
 113  
 114                  if ( wp_check_post_lock( $post_id ) ) {
 115                      $locked++;
 116                      continue;
 117                  }
 118  
 119                  if ( ! wp_trash_post( $post_id ) ) {
 120                      wp_die( __( 'Error in moving to Trash.' ) );
 121                  }
 122  
 123                  $trashed++;
 124              }
 125  
 126              $sendback = add_query_arg(
 127                  array(
 128                      'trashed' => $trashed,
 129                      'ids'     => join( ',', $post_ids ),
 130                      'locked'  => $locked,
 131                  ),
 132                  $sendback
 133              );
 134              break;
 135          case 'untrash':
 136              $untrashed = 0;
 137              foreach ( (array) $post_ids as $post_id ) {
 138                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 139                      wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
 140                  }
 141  
 142                  if ( ! wp_untrash_post( $post_id ) ) {
 143                      wp_die( __( 'Error in restoring from Trash.' ) );
 144                  }
 145  
 146                  $untrashed++;
 147              }
 148              $sendback = add_query_arg( 'untrashed', $untrashed, $sendback );
 149              break;
 150          case 'delete':
 151              $deleted = 0;
 152              foreach ( (array) $post_ids as $post_id ) {
 153                  $post_del = get_post( $post_id );
 154  
 155                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 156                      wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
 157                  }
 158  
 159                  if ( $post_del->post_type == 'attachment' ) {
 160                      if ( ! wp_delete_attachment( $post_id ) ) {
 161                          wp_die( __( 'Error in deleting.' ) );
 162                      }
 163                  } else {
 164                      if ( ! wp_delete_post( $post_id ) ) {
 165                          wp_die( __( 'Error in deleting.' ) );
 166                      }
 167                  }
 168                  $deleted++;
 169              }
 170              $sendback = add_query_arg( 'deleted', $deleted, $sendback );
 171              break;
 172          case 'edit':
 173              if ( isset( $_REQUEST['bulk_edit'] ) ) {
 174                  $done = bulk_edit_posts( $_REQUEST );
 175  
 176                  if ( is_array( $done ) ) {
 177                      $done['updated'] = count( $done['updated'] );
 178                      $done['skipped'] = count( $done['skipped'] );
 179                      $done['locked']  = count( $done['locked'] );
 180                      $sendback        = add_query_arg( $done, $sendback );
 181                  }
 182              }
 183              break;
 184          default:
 185              /** This action is documented in wp-admin/edit-comments.php */
 186              $sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $doaction, $post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 187              break;
 188      }
 189  
 190      $sendback = remove_query_arg( array( 'action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view' ), $sendback );
 191  
 192      wp_redirect( $sendback );
 193      exit();
 194  } elseif ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
 195      wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
 196      exit;
 197  }
 198  
 199  $wp_list_table->prepare_items();
 200  
 201  wp_enqueue_script( 'inline-edit-post' );
 202  wp_enqueue_script( 'heartbeat' );
 203  
 204  if ( 'wp_block' === $post_type ) {
 205      wp_enqueue_script( 'wp-list-reusable-blocks' );
 206      wp_enqueue_style( 'wp-list-reusable-blocks' );
 207  }
 208  
 209  $title = $post_type_object->labels->name;
 210  
 211  if ( 'post' == $post_type ) {
 212      get_current_screen()->add_help_tab(
 213          array(
 214              'id'      => 'overview',
 215              'title'   => __( 'Overview' ),
 216              'content' =>
 217                      '<p>' . __( 'This screen provides access to all of your posts. You can customize the display of this screen to suit your workflow.' ) . '</p>',
 218          )
 219      );
 220      get_current_screen()->add_help_tab(
 221          array(
 222              'id'      => 'screen-content',
 223              'title'   => __( 'Screen Content' ),
 224              'content' =>
 225                      '<p>' . __( 'You can customize the display of this screen&#8217;s contents in a number of ways:' ) . '</p>' .
 226                      '<ul>' .
 227                          '<li>' . __( 'You can hide/display columns based on your needs and decide how many posts to list per screen using the Screen Options tab.' ) . '</li>' .
 228                          '<li>' . __( 'You can filter the list of posts by post status using the text links above the posts list to only show posts with that status. The default view is to show all posts.' ) . '</li>' .
 229                          '<li>' . __( 'You can view posts in a simple title list or with an excerpt using the Screen Options tab.' ) . '</li>' .
 230                          '<li>' . __( 'You can refine the list to show only posts in a specific category or from a specific month by using the dropdown menus above the posts list. Click the Filter button after making your selection. You also can refine the list by clicking on the post author, category or tag in the posts list.' ) . '</li>' .
 231                      '</ul>',
 232          )
 233      );
 234      get_current_screen()->add_help_tab(
 235          array(
 236              'id'      => 'action-links',
 237              'title'   => __( 'Available Actions' ),
 238              'content' =>
 239                      '<p>' . __( 'Hovering over a row in the posts list will display action links that allow you to manage your post. You can perform the following actions:' ) . '</p>' .
 240                      '<ul>' .
 241                          '<li>' . __( '<strong>Edit</strong> takes you to the editing screen for that post. You can also reach that screen by clicking on the post title.' ) . '</li>' .
 242                          '<li>' . __( '<strong>Quick Edit</strong> provides inline access to the metadata of your post, allowing you to update post details without leaving this screen.' ) . '</li>' .
 243                          '<li>' . __( '<strong>Trash</strong> removes your post from this list and places it in the trash, from which you can permanently delete it.' ) . '</li>' .
 244                          '<li>' . __( '<strong>Preview</strong> will show you what your draft post will look like if you publish it. View will take you to your live site to view the post. Which link is available depends on your post&#8217;s status.' ) . '</li>' .
 245                      '</ul>',
 246          )
 247      );
 248      get_current_screen()->add_help_tab(
 249          array(
 250              'id'      => 'bulk-actions',
 251              'title'   => __( 'Bulk Actions' ),
 252              'content' =>
 253                      '<p>' . __( 'You can also edit or move multiple posts to the trash at once. Select the posts you want to act on using the checkboxes, then select the action you want to take from the Bulk Actions menu and click Apply.' ) . '</p>' .
 254                              '<p>' . __( 'When using Bulk Edit, you can change the metadata (categories, author, etc.) for all selected posts at once. To remove a post from the grouping, just click the x next to its name in the Bulk Edit area that appears.' ) . '</p>',
 255          )
 256      );
 257  
 258      get_current_screen()->set_help_sidebar(
 259          '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 260          '<p>' . __( '<a href="https://codex.wordpress.org/Posts_Screen">Documentation on Managing Posts</a>' ) . '</p>' .
 261          '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
 262      );
 263  
 264  } elseif ( 'page' == $post_type ) {
 265      get_current_screen()->add_help_tab(
 266          array(
 267              'id'      => 'overview',
 268              'title'   => __( 'Overview' ),
 269              'content' =>
 270                      '<p>' . __( 'Pages are similar to posts in that they have a title, body text, and associated metadata, but they are different in that they are not part of the chronological blog stream, kind of like permanent posts. Pages are not categorized or tagged, but can have a hierarchy. You can nest pages under other pages by making one the &#8220;Parent&#8221; of the other, creating a group of pages.' ) . '</p>',
 271          )
 272      );
 273      get_current_screen()->add_help_tab(
 274          array(
 275              'id'      => 'managing-pages',
 276              'title'   => __( 'Managing Pages' ),
 277              'content' =>
 278                      '<p>' . __( 'Managing pages is very similar to managing posts, and the screens can be customized in the same way.' ) . '</p>' .
 279                      '<p>' . __( 'You can also perform the same types of actions, including narrowing the list by using the filters, acting on a page using the action links that appear when you hover over a row, or using the Bulk Actions menu to edit the metadata for multiple pages at once.' ) . '</p>',
 280          )
 281      );
 282  
 283      get_current_screen()->set_help_sidebar(
 284          '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 285          '<p>' . __( '<a href="https://codex.wordpress.org/Pages_Screen">Documentation on Managing Pages</a>' ) . '</p>' .
 286          '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
 287      );
 288  
 289  }
 290  
 291  get_current_screen()->set_screen_reader_content(
 292      array(
 293          'heading_views'      => $post_type_object->labels->filter_items_list,
 294          'heading_pagination' => $post_type_object->labels->items_list_navigation,
 295          'heading_list'       => $post_type_object->labels->items_list,
 296      )
 297  );
 298  
 299  add_screen_option(
 300      'per_page',
 301      array(
 302          'default' => 20,
 303          'option'  => 'edit_' . $post_type . '_per_page',
 304      )
 305  );
 306  
 307  $bulk_counts = array(
 308      'updated'   => isset( $_REQUEST['updated'] ) ? absint( $_REQUEST['updated'] ) : 0,
 309      'locked'    => isset( $_REQUEST['locked'] ) ? absint( $_REQUEST['locked'] ) : 0,
 310      'deleted'   => isset( $_REQUEST['deleted'] ) ? absint( $_REQUEST['deleted'] ) : 0,
 311      'trashed'   => isset( $_REQUEST['trashed'] ) ? absint( $_REQUEST['trashed'] ) : 0,
 312      'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0,
 313  );
 314  
 315  $bulk_messages             = array();
 316  $bulk_messages['post']     = array(
 317      'updated'   => _n( '%s post updated.', '%s posts updated.', $bulk_counts['updated'] ),
 318      'locked'    => ( 1 == $bulk_counts['locked'] ) ? __( '1 post not updated, somebody is editing it.' ) :
 319                      _n( '%s post not updated, somebody is editing it.', '%s posts not updated, somebody is editing them.', $bulk_counts['locked'] ),
 320      'deleted'   => _n( '%s post permanently deleted.', '%s posts permanently deleted.', $bulk_counts['deleted'] ),
 321      'trashed'   => _n( '%s post moved to the Trash.', '%s posts moved to the Trash.', $bulk_counts['trashed'] ),
 322      'untrashed' => _n( '%s post restored from the Trash.', '%s posts restored from the Trash.', $bulk_counts['untrashed'] ),
 323  );
 324  $bulk_messages['page']     = array(
 325      'updated'   => _n( '%s page updated.', '%s pages updated.', $bulk_counts['updated'] ),
 326      'locked'    => ( 1 == $bulk_counts['locked'] ) ? __( '1 page not updated, somebody is editing it.' ) :
 327                      _n( '%s page not updated, somebody is editing it.', '%s pages not updated, somebody is editing them.', $bulk_counts['locked'] ),
 328      'deleted'   => _n( '%s page permanently deleted.', '%s pages permanently deleted.', $bulk_counts['deleted'] ),
 329      'trashed'   => _n( '%s page moved to the Trash.', '%s pages moved to the Trash.', $bulk_counts['trashed'] ),
 330      'untrashed' => _n( '%s page restored from the Trash.', '%s pages restored from the Trash.', $bulk_counts['untrashed'] ),
 331  );
 332  $bulk_messages['wp_block'] = array(
 333      'updated'   => _n( '%s block updated.', '%s blocks updated.', $bulk_counts['updated'] ),
 334      'locked'    => ( 1 == $bulk_counts['locked'] ) ? __( '1 block not updated, somebody is editing it.' ) : _n( '%s block not updated, somebody is editing it.', '%s blocks not updated, somebody is editing them.', $bulk_counts['locked'] ),
 335      'deleted'   => _n( '%s block permanently deleted.', '%s blocks permanently deleted.', $bulk_counts['deleted'] ),
 336      'trashed'   => _n( '%s block moved to the Trash.', '%s blocks moved to the Trash.', $bulk_counts['trashed'] ),
 337      'untrashed' => _n( '%s block restored from the Trash.', '%s blocks restored from the Trash.', $bulk_counts['untrashed'] ),
 338  );
 339  
 340  /**
 341   * Filters the bulk action updated messages.
 342   *
 343   * By default, custom post types use the messages for the 'post' post type.
 344   *
 345   * @since 3.7.0
 346   *
 347   * @param array[] $bulk_messages Arrays of messages, each keyed by the corresponding post type. Messages are
 348   *                               keyed with 'updated', 'locked', 'deleted', 'trashed', and 'untrashed'.
 349   * @param int[]   $bulk_counts   Array of item counts for each message, used to build internationalized strings.
 350   */
 351  $bulk_messages = apply_filters( 'bulk_post_updated_messages', $bulk_messages, $bulk_counts );
 352  $bulk_counts   = array_filter( $bulk_counts );
 353  
 354  require_once( ABSPATH . 'wp-admin/admin-header.php' );
 355  ?>
 356  <div class="wrap">
 357  <h1 class="wp-heading-inline">
 358  <?php
 359  echo esc_html( $post_type_object->labels->name );
 360  ?>
 361  </h1>
 362  
 363  <?php
 364  if ( current_user_can( $post_type_object->cap->create_posts ) ) {
 365      echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
 366  }
 367  
 368  if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
 369      /* translators: %s: search keywords */
 370      printf( ' <span class="subtitle">' . __( 'Search results for &#8220;%s&#8221;' ) . '</span>', get_search_query() );
 371  }
 372  ?>
 373  
 374  <hr class="wp-header-end">
 375  
 376  <?php
 377  // If we have a bulk message to issue:
 378  $messages = array();
 379  foreach ( $bulk_counts as $message => $count ) {
 380      if ( isset( $bulk_messages[ $post_type ][ $message ] ) ) {
 381          $messages[] = sprintf( $bulk_messages[ $post_type ][ $message ], number_format_i18n( $count ) );
 382      } elseif ( isset( $bulk_messages['post'][ $message ] ) ) {
 383          $messages[] = sprintf( $bulk_messages['post'][ $message ], number_format_i18n( $count ) );
 384      }
 385  
 386      if ( $message == 'trashed' && isset( $_REQUEST['ids'] ) ) {
 387          $ids        = preg_replace( '/[^0-9,]/', '', $_REQUEST['ids'] );
 388          $messages[] = '<a href="' . esc_url( wp_nonce_url( "edit.php?post_type=$post_type&doaction=undo&action=untrash&ids=$ids", 'bulk-posts' ) ) . '">' . __( 'Undo' ) . '</a>';
 389      }
 390  }
 391  
 392  if ( $messages ) {
 393      echo '<div id="message" class="updated notice is-dismissible"><p>' . join( ' ', $messages ) . '</p></div>';
 394  }
 395  unset( $messages );
 396  
 397  $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'locked', 'skipped', 'updated', 'deleted', 'trashed', 'untrashed' ), $_SERVER['REQUEST_URI'] );
 398  ?>
 399  
 400  <?php $wp_list_table->views(); ?>
 401  
 402  <form id="posts-filter" method="get">
 403  
 404  <?php $wp_list_table->search_box( $post_type_object->labels->search_items, 'post' ); ?>
 405  
 406  <input type="hidden" name="post_status" class="post_status_page" value="<?php echo ! empty( $_REQUEST['post_status'] ) ? esc_attr( $_REQUEST['post_status'] ) : 'all'; ?>" />
 407  <input type="hidden" name="post_type" class="post_type_page" value="<?php echo $post_type; ?>" />
 408  
 409  <?php if ( ! empty( $_REQUEST['author'] ) ) { ?>
 410  <input type="hidden" name="author" value="<?php echo esc_attr( $_REQUEST['author'] ); ?>" />
 411  <?php } ?>
 412  
 413  <?php if ( ! empty( $_REQUEST['show_sticky'] ) ) { ?>
 414  <input type="hidden" name="show_sticky" value="1" />
 415  <?php } ?>
 416  
 417  <?php $wp_list_table->display(); ?>
 418  
 419  </form>
 420  
 421  <?php
 422  if ( $wp_list_table->has_items() ) {
 423      $wp_list_table->inline_edit();
 424  }
 425  ?>
 426  
 427  <div id="ajax-response"></div>
 428  <br class="clear" />
 429  </div>
 430  
 431  <?php
 432  include( ABSPATH . 'wp-admin/admin-footer.php' );


Generated: Mon Jul 22 01:00:03 2019 Cross-referenced by PHPXref 0.7.1