[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Post Administration API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Renames `$_POST` data from form names to DB post columns.
  11   *
  12   * Manipulates `$_POST` directly.
  13   *
  14   * @since 2.6.0
  15   *
  16   * @param bool       $update    Whether the post already exists.
  17   * @param array|null $post_data Optional. The array of post data to process.
  18   *                              Defaults to the `$_POST` superglobal.
  19   * @return array|WP_Error Array of post data on success, WP_Error on failure.
  20   */
  21  function _wp_translate_postdata( $update = false, $post_data = null ) {
  22  
  23      if ( empty( $post_data ) ) {
  24          $post_data = &$_POST;
  25      }
  26  
  27      if ( $update ) {
  28          $post_data['ID'] = (int) $post_data['post_ID'];
  29      }
  30  
  31      $ptype = get_post_type_object( $post_data['post_type'] );
  32  
  33      if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
  34          if ( 'page' === $post_data['post_type'] ) {
  35              return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
  36          } else {
  37              return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
  38          }
  39      } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
  40          if ( 'page' === $post_data['post_type'] ) {
  41              return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
  42          } else {
  43              return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
  44          }
  45      }
  46  
  47      if ( isset( $post_data['content'] ) ) {
  48          $post_data['post_content'] = $post_data['content'];
  49      }
  50  
  51      if ( isset( $post_data['excerpt'] ) ) {
  52          $post_data['post_excerpt'] = $post_data['excerpt'];
  53      }
  54  
  55      if ( isset( $post_data['parent_id'] ) ) {
  56          $post_data['post_parent'] = (int) $post_data['parent_id'];
  57      }
  58  
  59      if ( isset( $post_data['trackback_url'] ) ) {
  60          $post_data['to_ping'] = $post_data['trackback_url'];
  61      }
  62  
  63      $post_data['user_ID'] = get_current_user_id();
  64  
  65      if ( ! empty( $post_data['post_author_override'] ) ) {
  66          $post_data['post_author'] = (int) $post_data['post_author_override'];
  67      } else {
  68          if ( ! empty( $post_data['post_author'] ) ) {
  69              $post_data['post_author'] = (int) $post_data['post_author'];
  70          } else {
  71              $post_data['post_author'] = (int) $post_data['user_ID'];
  72          }
  73      }
  74  
  75      if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
  76          && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
  77  
  78          if ( $update ) {
  79              if ( 'page' === $post_data['post_type'] ) {
  80                  return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
  81              } else {
  82                  return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
  83              }
  84          } else {
  85              if ( 'page' === $post_data['post_type'] ) {
  86                  return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
  87              } else {
  88                  return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
  89              }
  90          }
  91      }
  92  
  93      if ( ! empty( $post_data['post_status'] ) ) {
  94          $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  95  
  96          // No longer an auto-draft.
  97          if ( 'auto-draft' === $post_data['post_status'] ) {
  98              $post_data['post_status'] = 'draft';
  99          }
 100  
 101          if ( ! get_post_status_object( $post_data['post_status'] ) ) {
 102              unset( $post_data['post_status'] );
 103          }
 104      }
 105  
 106      // What to do based on which button they pressed.
 107      if ( isset( $post_data['saveasdraft'] ) && '' !== $post_data['saveasdraft'] ) {
 108          $post_data['post_status'] = 'draft';
 109      }
 110      if ( isset( $post_data['saveasprivate'] ) && '' !== $post_data['saveasprivate'] ) {
 111          $post_data['post_status'] = 'private';
 112      }
 113      if ( isset( $post_data['publish'] ) && ( '' !== $post_data['publish'] )
 114          && ( ! isset( $post_data['post_status'] ) || 'private' !== $post_data['post_status'] )
 115      ) {
 116          $post_data['post_status'] = 'publish';
 117      }
 118      if ( isset( $post_data['advanced'] ) && '' !== $post_data['advanced'] ) {
 119          $post_data['post_status'] = 'draft';
 120      }
 121      if ( isset( $post_data['pending'] ) && '' !== $post_data['pending'] ) {
 122          $post_data['post_status'] = 'pending';
 123      }
 124  
 125      if ( isset( $post_data['ID'] ) ) {
 126          $post_id = $post_data['ID'];
 127      } else {
 128          $post_id = false;
 129      }
 130      $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
 131  
 132      if ( isset( $post_data['post_status'] ) && 'private' === $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) {
 133          $post_data['post_status'] = $previous_status ? $previous_status : 'pending';
 134      }
 135  
 136      $published_statuses = array( 'publish', 'future' );
 137  
 138      // Posts 'submitted for approval' are submitted to $_POST the same as if they were being published.
 139      // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
 140      if ( isset( $post_data['post_status'] )
 141          && ( in_array( $post_data['post_status'], $published_statuses, true )
 142          && ! current_user_can( $ptype->cap->publish_posts ) )
 143      ) {
 144          if ( ! in_array( $previous_status, $published_statuses, true ) || ! current_user_can( 'edit_post', $post_id ) ) {
 145              $post_data['post_status'] = 'pending';
 146          }
 147      }
 148  
 149      if ( ! isset( $post_data['post_status'] ) ) {
 150          $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status;
 151      }
 152  
 153      if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) {
 154          unset( $post_data['post_password'] );
 155      }
 156  
 157      if ( ! isset( $post_data['comment_status'] ) ) {
 158          $post_data['comment_status'] = 'closed';
 159      }
 160  
 161      if ( ! isset( $post_data['ping_status'] ) ) {
 162          $post_data['ping_status'] = 'closed';
 163      }
 164  
 165      foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
 166          if ( ! empty( $post_data[ 'hidden_' . $timeunit ] ) && $post_data[ 'hidden_' . $timeunit ] != $post_data[ $timeunit ] ) {
 167              $post_data['edit_date'] = '1';
 168              break;
 169          }
 170      }
 171  
 172      if ( ! empty( $post_data['edit_date'] ) ) {
 173          $aa = $post_data['aa'];
 174          $mm = $post_data['mm'];
 175          $jj = $post_data['jj'];
 176          $hh = $post_data['hh'];
 177          $mn = $post_data['mn'];
 178          $ss = $post_data['ss'];
 179          $aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa;
 180          $mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm;
 181          $jj = ( $jj > 31 ) ? 31 : $jj;
 182          $jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj;
 183          $hh = ( $hh > 23 ) ? $hh - 24 : $hh;
 184          $mn = ( $mn > 59 ) ? $mn - 60 : $mn;
 185          $ss = ( $ss > 59 ) ? $ss - 60 : $ss;
 186  
 187          $post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss );
 188  
 189          $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
 190          if ( ! $valid_date ) {
 191              return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
 192          }
 193  
 194          $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
 195      }
 196  
 197      if ( isset( $post_data['post_category'] ) ) {
 198          $category_object = get_taxonomy( 'category' );
 199          if ( ! current_user_can( $category_object->cap->assign_terms ) ) {
 200              unset( $post_data['post_category'] );
 201          }
 202      }
 203  
 204      return $post_data;
 205  }
 206  
 207  /**
 208   * Returns only allowed post data fields.
 209   *
 210   * @since 5.0.1
 211   *
 212   * @param array|WP_Error|null $post_data The array of post data to process, or an error object.
 213   *                                       Defaults to the `$_POST` superglobal.
 214   * @return array|WP_Error Array of post data on success, WP_Error on failure.
 215   */
 216  function _wp_get_allowed_postdata( $post_data = null ) {
 217      if ( empty( $post_data ) ) {
 218          $post_data = $_POST;
 219      }
 220  
 221      // Pass through errors.
 222      if ( is_wp_error( $post_data ) ) {
 223          return $post_data;
 224      }
 225  
 226      return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
 227  }
 228  
 229  /**
 230   * Updates an existing post with values provided in `$_POST`.
 231   *
 232   * If post data is passed as an argument, it is treated as an array of data
 233   * keyed appropriately for turning into a post object.
 234   *
 235   * If post data is not passed, the `$_POST` global variable is used instead.
 236   *
 237   * @since 1.5.0
 238   *
 239   * @global wpdb $wpdb WordPress database abstraction object.
 240   *
 241   * @param array|null $post_data Optional. The array of post data to process.
 242   *                              Defaults to the `$_POST` superglobal.
 243   * @return int Post ID.
 244   */
 245  function edit_post( $post_data = null ) {
 246      global $wpdb;
 247  
 248      if ( empty( $post_data ) ) {
 249          $post_data = &$_POST;
 250      }
 251  
 252      // Clear out any data in internal vars.
 253      unset( $post_data['filter'] );
 254  
 255      $post_ID = (int) $post_data['post_ID'];
 256      $post    = get_post( $post_ID );
 257  
 258      $post_data['post_type']      = $post->post_type;
 259      $post_data['post_mime_type'] = $post->post_mime_type;
 260  
 261      if ( ! empty( $post_data['post_status'] ) ) {
 262          $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
 263  
 264          if ( 'inherit' === $post_data['post_status'] ) {
 265              unset( $post_data['post_status'] );
 266          }
 267      }
 268  
 269      $ptype = get_post_type_object( $post_data['post_type'] );
 270      if ( ! current_user_can( 'edit_post', $post_ID ) ) {
 271          if ( 'page' === $post_data['post_type'] ) {
 272              wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
 273          } else {
 274              wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
 275          }
 276      }
 277  
 278      if ( post_type_supports( $ptype->name, 'revisions' ) ) {
 279          $revisions = wp_get_post_revisions(
 280              $post_ID,
 281              array(
 282                  'order'          => 'ASC',
 283                  'posts_per_page' => 1,
 284              )
 285          );
 286          $revision  = current( $revisions );
 287  
 288          // Check if the revisions have been upgraded.
 289          if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) {
 290              _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
 291          }
 292      }
 293  
 294      if ( isset( $post_data['visibility'] ) ) {
 295          switch ( $post_data['visibility'] ) {
 296              case 'public':
 297                  $post_data['post_password'] = '';
 298                  break;
 299              case 'password':
 300                  unset( $post_data['sticky'] );
 301                  break;
 302              case 'private':
 303                  $post_data['post_status']   = 'private';
 304                  $post_data['post_password'] = '';
 305                  unset( $post_data['sticky'] );
 306                  break;
 307          }
 308      }
 309  
 310      $post_data = _wp_translate_postdata( true, $post_data );
 311      if ( is_wp_error( $post_data ) ) {
 312          wp_die( $post_data->get_error_message() );
 313      }
 314      $translated = _wp_get_allowed_postdata( $post_data );
 315  
 316      // Post formats.
 317      if ( isset( $post_data['post_format'] ) ) {
 318          set_post_format( $post_ID, $post_data['post_format'] );
 319      }
 320  
 321      $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
 322      foreach ( $format_meta_urls as $format_meta_url ) {
 323          $keyed = '_format_' . $format_meta_url;
 324          if ( isset( $post_data[ $keyed ] ) ) {
 325              update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
 326          }
 327      }
 328  
 329      $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );
 330  
 331      foreach ( $format_keys as $key ) {
 332          $keyed = '_format_' . $key;
 333          if ( isset( $post_data[ $keyed ] ) ) {
 334              if ( current_user_can( 'unfiltered_html' ) ) {
 335                  update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
 336              } else {
 337                  update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
 338              }
 339          }
 340      }
 341  
 342      if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
 343          $id3data = wp_get_attachment_metadata( $post_ID );
 344          if ( ! is_array( $id3data ) ) {
 345              $id3data = array();
 346          }
 347  
 348          foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
 349              if ( isset( $post_data[ 'id3_' . $key ] ) ) {
 350                  $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
 351              }
 352          }
 353          wp_update_attachment_metadata( $post_ID, $id3data );
 354      }
 355  
 356      // Meta stuff.
 357      if ( isset( $post_data['meta'] ) && $post_data['meta'] ) {
 358          foreach ( $post_data['meta'] as $key => $value ) {
 359              $meta = get_post_meta_by_id( $key );
 360              if ( ! $meta ) {
 361                  continue;
 362              }
 363              if ( $meta->post_id != $post_ID ) {
 364                  continue;
 365              }
 366              if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) {
 367                  continue;
 368              }
 369              if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) {
 370                  continue;
 371              }
 372              update_meta( $key, $value['key'], $value['value'] );
 373          }
 374      }
 375  
 376      if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) {
 377          foreach ( $post_data['deletemeta'] as $key => $value ) {
 378              $meta = get_post_meta_by_id( $key );
 379              if ( ! $meta ) {
 380                  continue;
 381              }
 382              if ( $meta->post_id != $post_ID ) {
 383                  continue;
 384              }
 385              if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) {
 386                  continue;
 387              }
 388              delete_meta( $key );
 389          }
 390      }
 391  
 392      // Attachment stuff.
 393      if ( 'attachment' === $post_data['post_type'] ) {
 394          if ( isset( $post_data['_wp_attachment_image_alt'] ) ) {
 395              $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
 396  
 397              if ( get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) !== $image_alt ) {
 398                  $image_alt = wp_strip_all_tags( $image_alt, true );
 399  
 400                  // update_post_meta() expects slashed.
 401                  update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
 402              }
 403          }
 404  
 405          $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
 406  
 407          /** This filter is documented in wp-admin/includes/media.php */
 408          $translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
 409      }
 410  
 411      // Convert taxonomy input to term IDs, to avoid ambiguity.
 412      if ( isset( $post_data['tax_input'] ) ) {
 413          foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
 414              $tax_object = get_taxonomy( $taxonomy );
 415  
 416              if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
 417                  $translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
 418              }
 419          }
 420      }
 421  
 422      add_meta( $post_ID );
 423  
 424      update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
 425  
 426      $success = wp_update_post( $translated );
 427  
 428      // If the save failed, see if we can sanity check the main fields and try again.
 429      if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
 430          $fields = array( 'post_title', 'post_content', 'post_excerpt' );
 431  
 432          foreach ( $fields as $field ) {
 433              if ( isset( $translated[ $field ] ) ) {
 434                  $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] );
 435              }
 436          }
 437  
 438          wp_update_post( $translated );
 439      }
 440  
 441      // Now that we have an ID we can fix any attachment anchor hrefs.
 442      _fix_attachment_links( $post_ID );
 443  
 444      wp_set_post_lock( $post_ID );
 445  
 446      if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
 447          if ( ! empty( $post_data['sticky'] ) ) {
 448              stick_post( $post_ID );
 449          } else {
 450              unstick_post( $post_ID );
 451          }
 452      }
 453  
 454      return $post_ID;
 455  }
 456  
 457  /**
 458   * Processes the post data for the bulk editing of posts.
 459   *
 460   * Updates all bulk edited posts/pages, adding (but not removing) tags and
 461   * categories. Skips pages when they would be their own parent or child.
 462   *
 463   * @since 2.7.0
 464   *
 465   * @global wpdb $wpdb WordPress database abstraction object.
 466   *
 467   * @param array|null $post_data Optional. The array of post data to process.
 468   *                              Defaults to the `$_POST` superglobal.
 469   * @return array
 470   */
 471  function bulk_edit_posts( $post_data = null ) {
 472      global $wpdb;
 473  
 474      if ( empty( $post_data ) ) {
 475          $post_data = &$_POST;
 476      }
 477  
 478      if ( isset( $post_data['post_type'] ) ) {
 479          $ptype = get_post_type_object( $post_data['post_type'] );
 480      } else {
 481          $ptype = get_post_type_object( 'post' );
 482      }
 483  
 484      if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
 485          if ( 'page' === $ptype->name ) {
 486              wp_die( __( 'Sorry, you are not allowed to edit pages.' ) );
 487          } else {
 488              wp_die( __( 'Sorry, you are not allowed to edit posts.' ) );
 489          }
 490      }
 491  
 492      if ( -1 == $post_data['_status'] ) {
 493          $post_data['post_status'] = null;
 494          unset( $post_data['post_status'] );
 495      } else {
 496          $post_data['post_status'] = $post_data['_status'];
 497      }
 498      unset( $post_data['_status'] );
 499  
 500      if ( ! empty( $post_data['post_status'] ) ) {
 501          $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
 502  
 503          if ( 'inherit' === $post_data['post_status'] ) {
 504              unset( $post_data['post_status'] );
 505          }
 506      }
 507  
 508      $post_IDs = array_map( 'intval', (array) $post_data['post'] );
 509  
 510      $reset = array(
 511          'post_author',
 512          'post_status',
 513          'post_password',
 514          'post_parent',
 515          'page_template',
 516          'comment_status',
 517          'ping_status',
 518          'keep_private',
 519          'tax_input',
 520          'post_category',
 521          'sticky',
 522          'post_format',
 523      );
 524  
 525      foreach ( $reset as $field ) {
 526          if ( isset( $post_data[ $field ] ) && ( '' === $post_data[ $field ] || -1 == $post_data[ $field ] ) ) {
 527              unset( $post_data[ $field ] );
 528          }
 529      }
 530  
 531      if ( isset( $post_data['post_category'] ) ) {
 532          if ( is_array( $post_data['post_category'] ) && ! empty( $post_data['post_category'] ) ) {
 533              $new_cats = array_map( 'absint', $post_data['post_category'] );
 534          } else {
 535              unset( $post_data['post_category'] );
 536          }
 537      }
 538  
 539      $tax_input = array();
 540      if ( isset( $post_data['tax_input'] ) ) {
 541          foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
 542              if ( empty( $terms ) ) {
 543                  continue;
 544              }
 545              if ( is_taxonomy_hierarchical( $tax_name ) ) {
 546                  $tax_input[ $tax_name ] = array_map( 'absint', $terms );
 547              } else {
 548                  $comma = _x( ',', 'tag delimiter' );
 549                  if ( ',' !== $comma ) {
 550                      $terms = str_replace( $comma, ',', $terms );
 551                  }
 552                  $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
 553              }
 554          }
 555      }
 556  
 557      if ( isset( $post_data['post_parent'] ) && (int) $post_data['post_parent'] ) {
 558          $parent   = (int) $post_data['post_parent'];
 559          $pages    = $wpdb->get_results( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'" );
 560          $children = array();
 561  
 562          for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
 563              $children[] = $parent;
 564  
 565              foreach ( $pages as $page ) {
 566                  if ( (int) $page->ID === $parent ) {
 567                      $parent = (int) $page->post_parent;
 568                      break;
 569                  }
 570              }
 571          }
 572      }
 573  
 574      $updated          = array();
 575      $skipped          = array();
 576      $locked           = array();
 577      $shared_post_data = $post_data;
 578  
 579      foreach ( $post_IDs as $post_ID ) {
 580          // Start with fresh post data with each iteration.
 581          $post_data = $shared_post_data;
 582  
 583          $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
 584  
 585          if ( ! isset( $post_type_object )
 586              || ( isset( $children ) && in_array( $post_ID, $children, true ) )
 587              || ! current_user_can( 'edit_post', $post_ID )
 588          ) {
 589              $skipped[] = $post_ID;
 590              continue;
 591          }
 592  
 593          if ( wp_check_post_lock( $post_ID ) ) {
 594              $locked[] = $post_ID;
 595              continue;
 596          }
 597  
 598          $post      = get_post( $post_ID );
 599          $tax_names = get_object_taxonomies( $post );
 600          foreach ( $tax_names as $tax_name ) {
 601              $taxonomy_obj = get_taxonomy( $tax_name );
 602              if ( isset( $tax_input[ $tax_name ] ) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
 603                  $new_terms = $tax_input[ $tax_name ];
 604              } else {
 605                  $new_terms = array();
 606              }
 607  
 608              if ( $taxonomy_obj->hierarchical ) {
 609                  $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'ids' ) );
 610              } else {
 611                  $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'names' ) );
 612              }
 613  
 614              $post_data['tax_input'][ $tax_name ] = array_merge( $current_terms, $new_terms );
 615          }
 616  
 617          if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
 618              $cats                       = (array) wp_get_post_categories( $post_ID );
 619              $post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
 620              unset( $post_data['tax_input']['category'] );
 621          }
 622  
 623          $post_data['post_ID']        = $post_ID;
 624          $post_data['post_type']      = $post->post_type;
 625          $post_data['post_mime_type'] = $post->post_mime_type;
 626  
 627          foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
 628              if ( ! isset( $post_data[ $field ] ) ) {
 629                  $post_data[ $field ] = $post->$field;
 630              }
 631          }
 632  
 633          $post_data = _wp_translate_postdata( true, $post_data );
 634          if ( is_wp_error( $post_data ) ) {
 635              $skipped[] = $post_ID;
 636              continue;
 637          }
 638          $post_data = _wp_get_allowed_postdata( $post_data );
 639  
 640          if ( isset( $shared_post_data['post_format'] ) ) {
 641              set_post_format( $post_ID, $shared_post_data['post_format'] );
 642          }
 643  
 644          // Prevent wp_insert_post() from overwriting post format with the old data.
 645          unset( $post_data['tax_input']['post_format'] );
 646  
 647          $post_id = wp_update_post( $post_data );
 648          update_post_meta( $post_id, '_edit_last', get_current_user_id() );
 649          $updated[] = $post_id;
 650  
 651          if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
 652              if ( 'sticky' === $post_data['sticky'] ) {
 653                  stick_post( $post_ID );
 654              } else {
 655                  unstick_post( $post_ID );
 656              }
 657          }
 658      }
 659  
 660      return array(
 661          'updated' => $updated,
 662          'skipped' => $skipped,
 663          'locked'  => $locked,
 664      );
 665  }
 666  
 667  /**
 668   * Returns default post information to use when populating the "Write Post" form.
 669   *
 670   * @since 2.0.0
 671   *
 672   * @param string $post_type    Optional. A post type string. Default 'post'.
 673   * @param bool   $create_in_db Optional. Whether to insert the post into database. Default false.
 674   * @return WP_Post Post object containing all the default post data as attributes
 675   */
 676  function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
 677      $post_title = '';
 678      if ( ! empty( $_REQUEST['post_title'] ) ) {
 679          $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ) );
 680      }
 681  
 682      $post_content = '';
 683      if ( ! empty( $_REQUEST['content'] ) ) {
 684          $post_content = esc_html( wp_unslash( $_REQUEST['content'] ) );
 685      }
 686  
 687      $post_excerpt = '';
 688      if ( ! empty( $_REQUEST['excerpt'] ) ) {
 689          $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) );
 690      }
 691  
 692      if ( $create_in_db ) {
 693          $post_id = wp_insert_post(
 694              array(
 695                  'post_title'  => __( 'Auto Draft' ),
 696                  'post_type'   => $post_type,
 697                  'post_status' => 'auto-draft',
 698              ),
 699              false,
 700              false
 701          );
 702          $post    = get_post( $post_id );
 703          if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
 704              set_post_format( $post, get_option( 'default_post_format' ) );
 705          }
 706          wp_after_insert_post( $post, false, null );
 707  
 708          // Schedule auto-draft cleanup.
 709          if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
 710              wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
 711          }
 712      } else {
 713          $post                 = new stdClass;
 714          $post->ID             = 0;
 715          $post->post_author    = '';
 716          $post->post_date      = '';
 717          $post->post_date_gmt  = '';
 718          $post->post_password  = '';
 719          $post->post_name      = '';
 720          $post->post_type      = $post_type;
 721          $post->post_status    = 'draft';
 722          $post->to_ping        = '';
 723          $post->pinged         = '';
 724          $post->comment_status = get_default_comment_status( $post_type );
 725          $post->ping_status    = get_default_comment_status( $post_type, 'pingback' );
 726          $post->post_pingback  = get_option( 'default_pingback_flag' );
 727          $post->post_category  = get_option( 'default_category' );
 728          $post->page_template  = 'default';
 729          $post->post_parent    = 0;
 730          $post->menu_order     = 0;
 731          $post                 = new WP_Post( $post );
 732      }
 733  
 734      /**
 735       * Filters the default post content initially used in the "Write Post" form.
 736       *
 737       * @since 1.5.0
 738       *
 739       * @param string  $post_content Default post content.
 740       * @param WP_Post $post         Post object.
 741       */
 742      $post->post_content = (string) apply_filters( 'default_content', $post_content, $post );
 743  
 744      /**
 745       * Filters the default post title initially used in the "Write Post" form.
 746       *
 747       * @since 1.5.0
 748       *
 749       * @param string  $post_title Default post title.
 750       * @param WP_Post $post       Post object.
 751       */
 752      $post->post_title = (string) apply_filters( 'default_title', $post_title, $post );
 753  
 754      /**
 755       * Filters the default post excerpt initially used in the "Write Post" form.
 756       *
 757       * @since 1.5.0
 758       *
 759       * @param string  $post_excerpt Default post excerpt.
 760       * @param WP_Post $post         Post object.
 761       */
 762      $post->post_excerpt = (string) apply_filters( 'default_excerpt', $post_excerpt, $post );
 763  
 764      return $post;
 765  }
 766  
 767  /**
 768   * Determines if a post exists based on title, content, date and type.
 769   *
 770   * @since 2.0.0
 771   * @since 5.2.0 Added the `$type` parameter.
 772   * @since 5.8.0 Added the `$status` parameter.
 773   *
 774   * @global wpdb $wpdb WordPress database abstraction object.
 775   *
 776   * @param string $title   Post title.
 777   * @param string $content Optional. Post content.
 778   * @param string $date    Optional. Post date.
 779   * @param string $type    Optional. Post type.
 780   * @param string $status  Optional. Post status.
 781   * @return int Post ID if post exists, 0 otherwise.
 782   */
 783  function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
 784      global $wpdb;
 785  
 786      $post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
 787      $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
 788      $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
 789      $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
 790      $post_status  = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
 791  
 792      $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
 793      $args  = array();
 794  
 795      if ( ! empty( $date ) ) {
 796          $query .= ' AND post_date = %s';
 797          $args[] = $post_date;
 798      }
 799  
 800      if ( ! empty( $title ) ) {
 801          $query .= ' AND post_title = %s';
 802          $args[] = $post_title;
 803      }
 804  
 805      if ( ! empty( $content ) ) {
 806          $query .= ' AND post_content = %s';
 807          $args[] = $post_content;
 808      }
 809  
 810      if ( ! empty( $type ) ) {
 811          $query .= ' AND post_type = %s';
 812          $args[] = $post_type;
 813      }
 814  
 815      if ( ! empty( $status ) ) {
 816          $query .= ' AND post_status = %s';
 817          $args[] = $post_status;
 818      }
 819  
 820      if ( ! empty( $args ) ) {
 821          return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
 822      }
 823  
 824      return 0;
 825  }
 826  
 827  /**
 828   * Creates a new post from the "Write Post" form using `$_POST` information.
 829   *
 830   * @since 2.1.0
 831   *
 832   * @global WP_User $current_user
 833   *
 834   * @return int|WP_Error Post ID on success, WP_Error on failure.
 835   */
 836  function wp_write_post() {
 837      if ( isset( $_POST['post_type'] ) ) {
 838          $ptype = get_post_type_object( $_POST['post_type'] );
 839      } else {
 840          $ptype = get_post_type_object( 'post' );
 841      }
 842  
 843      if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
 844          if ( 'page' === $ptype->name ) {
 845              return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
 846          } else {
 847              return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
 848          }
 849      }
 850  
 851      $_POST['post_mime_type'] = '';
 852  
 853      // Clear out any data in internal vars.
 854      unset( $_POST['filter'] );
 855  
 856      // Edit, don't write, if we have a post ID.
 857      if ( isset( $_POST['post_ID'] ) ) {
 858          return edit_post();
 859      }
 860  
 861      if ( isset( $_POST['visibility'] ) ) {
 862          switch ( $_POST['visibility'] ) {
 863              case 'public':
 864                  $_POST['post_password'] = '';
 865                  break;
 866              case 'password':
 867                  unset( $_POST['sticky'] );
 868                  break;
 869              case 'private':
 870                  $_POST['post_status']   = 'private';
 871                  $_POST['post_password'] = '';
 872                  unset( $_POST['sticky'] );
 873                  break;
 874          }
 875      }
 876  
 877      $translated = _wp_translate_postdata( false );
 878      if ( is_wp_error( $translated ) ) {
 879          return $translated;
 880      }
 881      $translated = _wp_get_allowed_postdata( $translated );
 882  
 883      // Create the post.
 884      $post_ID = wp_insert_post( $translated );
 885      if ( is_wp_error( $post_ID ) ) {
 886          return $post_ID;
 887      }
 888  
 889      if ( empty( $post_ID ) ) {
 890          return 0;
 891      }
 892  
 893      add_meta( $post_ID );
 894  
 895      add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
 896  
 897      // Now that we have an ID we can fix any attachment anchor hrefs.
 898      _fix_attachment_links( $post_ID );
 899  
 900      wp_set_post_lock( $post_ID );
 901  
 902      return $post_ID;
 903  }
 904  
 905  /**
 906   * Calls wp_write_post() and handles the errors.
 907   *
 908   * @since 2.0.0
 909   *
 910   * @return int|void Post ID on success, void on failure.
 911   */
 912  function write_post() {
 913      $result = wp_write_post();
 914      if ( is_wp_error( $result ) ) {
 915          wp_die( $result->get_error_message() );
 916      } else {
 917          return $result;
 918      }
 919  }
 920  
 921  //
 922  // Post Meta.
 923  //
 924  
 925  /**
 926   * Adds post meta data defined in the `$_POST` superglobal for a post with given ID.
 927   *
 928   * @since 1.2.0
 929   *
 930   * @param int $post_ID
 931   * @return int|bool
 932   */
 933  function add_meta( $post_ID ) {
 934      $post_ID = (int) $post_ID;
 935  
 936      $metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
 937      $metakeyinput  = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
 938      $metavalue     = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : '';
 939      if ( is_string( $metavalue ) ) {
 940          $metavalue = trim( $metavalue );
 941      }
 942  
 943      if ( ( ( '#NONE#' !== $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) {
 944          /*
 945           * We have a key/value pair. If both the select and the input
 946           * for the key have data, the input takes precedence.
 947           */
 948          if ( '#NONE#' !== $metakeyselect ) {
 949              $metakey = $metakeyselect;
 950          }
 951  
 952          if ( $metakeyinput ) {
 953              $metakey = $metakeyinput; // Default.
 954          }
 955  
 956          if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) {
 957              return false;
 958          }
 959  
 960          $metakey = wp_slash( $metakey );
 961  
 962          return add_post_meta( $post_ID, $metakey, $metavalue );
 963      }
 964  
 965      return false;
 966  }
 967  
 968  /**
 969   * Deletes post meta data by meta ID.
 970   *
 971   * @since 1.2.0
 972   *
 973   * @param int $mid
 974   * @return bool
 975   */
 976  function delete_meta( $mid ) {
 977      return delete_metadata_by_mid( 'post', $mid );
 978  }
 979  
 980  /**
 981   * Returns a list of previously defined keys.
 982   *
 983   * @since 1.2.0
 984   *
 985   * @global wpdb $wpdb WordPress database abstraction object.
 986   *
 987   * @return string[] Array of meta key names.
 988   */
 989  function get_meta_keys() {
 990      global $wpdb;
 991  
 992      $keys = $wpdb->get_col(
 993          "
 994              SELECT meta_key
 995              FROM $wpdb->postmeta
 996              GROUP BY meta_key
 997              ORDER BY meta_key"
 998      );
 999  
1000      return $keys;
1001  }
1002  
1003  /**
1004   * Returns post meta data by meta ID.
1005   *
1006   * @since 2.1.0
1007   *
1008   * @param int $mid
1009   * @return object|bool
1010   */
1011  function get_post_meta_by_id( $mid ) {
1012      return get_metadata_by_mid( 'post', $mid );
1013  }
1014  
1015  /**
1016   * Returns meta data for the given post ID.
1017   *
1018   * @since 1.2.0
1019   *
1020   * @global wpdb $wpdb WordPress database abstraction object.
1021   *
1022   * @param int $postid A post ID.
1023   * @return array[] {
1024   *     Array of meta data arrays for the given post ID.
1025   *
1026   *     @type array ...$0 {
1027   *         Associative array of meta data.
1028   *
1029   *         @type string $meta_key   Meta key.
1030   *         @type mixed  $meta_value Meta value.
1031   *         @type string $meta_id    Meta ID as a numeric string.
1032   *         @type string $post_id    Post ID as a numeric string.
1033   *     }
1034   * }
1035   */
1036  function has_meta( $postid ) {
1037      global $wpdb;
1038  
1039      return $wpdb->get_results(
1040          $wpdb->prepare(
1041              "SELECT meta_key, meta_value, meta_id, post_id
1042              FROM $wpdb->postmeta WHERE post_id = %d
1043              ORDER BY meta_key,meta_id",
1044              $postid
1045          ),
1046          ARRAY_A
1047      );
1048  }
1049  
1050  /**
1051   * Updates post meta data by meta ID.
1052   *
1053   * @since 1.2.0
1054   *
1055   * @param int    $meta_id    Meta ID.
1056   * @param string $meta_key   Meta key. Expect slashed.
1057   * @param string $meta_value Meta value. Expect slashed.
1058   * @return bool
1059   */
1060  function update_meta( $meta_id, $meta_key, $meta_value ) {
1061      $meta_key   = wp_unslash( $meta_key );
1062      $meta_value = wp_unslash( $meta_value );
1063  
1064      return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
1065  }
1066  
1067  //
1068  // Private.
1069  //
1070  
1071  /**
1072   * Replaces hrefs of attachment anchors with up-to-date permalinks.
1073   *
1074   * @since 2.3.0
1075   * @access private
1076   *
1077   * @param int|object $post Post ID or post object.
1078   * @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.
1079   */
1080  function _fix_attachment_links( $post ) {
1081      $post    = get_post( $post, ARRAY_A );
1082      $content = $post['post_content'];
1083  
1084      // Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
1085      if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) {
1086          return;
1087      }
1088  
1089      // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero).
1090      if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) {
1091          return;
1092      }
1093  
1094      $site_url = get_bloginfo( 'url' );
1095      $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s).
1096      $replace  = '';
1097  
1098      foreach ( $link_matches[1] as $key => $value ) {
1099          if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' )
1100              || ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
1101              || ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) {
1102                  continue;
1103          }
1104  
1105          $quote  = $url_match[1]; // The quote (single or double).
1106          $url_id = (int) $url_match[2];
1107          $rel_id = (int) $rel_match[1];
1108  
1109          if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) {
1110              continue;
1111          }
1112  
1113          $link    = $link_matches[0][ $key ];
1114          $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
1115  
1116          $content = str_replace( $link, $replace, $content );
1117      }
1118  
1119      if ( $replace ) {
1120          $post['post_content'] = $content;
1121          // Escape data pulled from DB.
1122          $post = add_magic_quotes( $post );
1123  
1124          return wp_update_post( $post );
1125      }
1126  }
1127  
1128  /**
1129   * Returns all the possible statuses for a post type.
1130   *
1131   * @since 2.5.0
1132   *
1133   * @param string $type The post_type you want the statuses for. Default 'post'.
1134   * @return string[] An array of all the statuses for the supplied post type.
1135   */
1136  function get_available_post_statuses( $type = 'post' ) {
1137      $stati = wp_count_posts( $type );
1138  
1139      return array_keys( get_object_vars( $stati ) );
1140  }
1141  
1142  /**
1143   * Runs the query to fetch the posts for listing on the edit posts page.
1144   *
1145   * @since 2.5.0
1146   *
1147   * @param array|false $q Optional. Array of query variables to use to build the query.
1148   *                       Defaults to the `$_GET` superglobal.
1149   * @return array
1150   */
1151  function wp_edit_posts_query( $q = false ) {
1152      if ( false === $q ) {
1153          $q = $_GET;
1154      }
1155      $q['m']     = isset( $q['m'] ) ? (int) $q['m'] : 0;
1156      $q['cat']   = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
1157      $post_stati = get_post_stati();
1158  
1159      if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types(), true ) ) {
1160          $post_type = $q['post_type'];
1161      } else {
1162          $post_type = 'post';
1163      }
1164  
1165      $avail_post_stati = get_available_post_statuses( $post_type );
1166      $post_status      = '';
1167      $perm             = '';
1168  
1169      if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_stati, true ) ) {
1170          $post_status = $q['post_status'];
1171          $perm        = 'readable';
1172      }
1173  
1174      $orderby = '';
1175  
1176      if ( isset( $q['orderby'] ) ) {
1177          $orderby = $q['orderby'];
1178      } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ), true ) ) {
1179          $orderby = 'modified';
1180      }
1181  
1182      $order = '';
1183  
1184      if ( isset( $q['order'] ) ) {
1185          $order = $q['order'];
1186      } elseif ( isset( $q['post_status'] ) && 'pending' === $q['post_status'] ) {
1187          $order = 'ASC';
1188      }
1189  
1190      $per_page       = "edit_{$post_type}_per_page";
1191      $posts_per_page = (int) get_user_option( $per_page );
1192      if ( empty( $posts_per_page ) || $posts_per_page < 1 ) {
1193          $posts_per_page = 20;
1194      }
1195  
1196      /**
1197       * Filters the number of items per page to show for a specific 'per_page' type.
1198       *
1199       * The dynamic portion of the hook name, `$post_type`, refers to the post type.
1200       *
1201       * Possible hook names include:
1202       *
1203       *  - `edit_post_per_page`
1204       *  - `edit_page_per_page`
1205       *  - `edit_attachment_per_page`
1206       *
1207       * @since 3.0.0
1208       *
1209       * @param int $posts_per_page Number of posts to display per page for the given post
1210       *                            type. Default 20.
1211       */
1212      $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page );
1213  
1214      /**
1215       * Filters the number of posts displayed per page when specifically listing "posts".
1216       *
1217       * @since 2.8.0
1218       *
1219       * @param int    $posts_per_page Number of posts to be displayed. Default 20.
1220       * @param string $post_type      The post type.
1221       */
1222      $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
1223  
1224      $query = compact( 'post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page' );
1225  
1226      // Hierarchical types require special args.
1227      if ( is_post_type_hierarchical( $post_type ) && empty( $orderby ) ) {
1228          $query['orderby']                = 'menu_order title';
1229          $query['order']                  = 'asc';
1230          $query['posts_per_page']         = -1;
1231          $query['posts_per_archive_page'] = -1;
1232          $query['fields']                 = 'id=>parent';
1233      }
1234  
1235      if ( ! empty( $q['show_sticky'] ) ) {
1236          $query['post__in'] = (array) get_option( 'sticky_posts' );
1237      }
1238  
1239      wp( $query );
1240  
1241      return $avail_post_stati;
1242  }
1243  
1244  /**
1245   * Returns the query variables for the current attachments request.
1246   *
1247   * @since 4.2.0
1248   *
1249   * @param array|false $q Optional. Array of query variables to use to build the query.
1250   *                       Defaults to the `$_GET` superglobal.
1251   * @return array The parsed query vars.
1252   */
1253  function wp_edit_attachments_query_vars( $q = false ) {
1254      if ( false === $q ) {
1255          $q = $_GET;
1256      }
1257      $q['m']         = isset( $q['m'] ) ? (int) $q['m'] : 0;
1258      $q['cat']       = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
1259      $q['post_type'] = 'attachment';
1260      $post_type      = get_post_type_object( 'attachment' );
1261      $states         = 'inherit';
1262      if ( current_user_can( $post_type->cap->read_private_posts ) ) {
1263          $states .= ',private';
1264      }
1265  
1266      $q['post_status'] = isset( $q['status'] ) && 'trash' === $q['status'] ? 'trash' : $states;
1267      $q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' === $q['attachment-filter'] ? 'trash' : $states;
1268  
1269      $media_per_page = (int) get_user_option( 'upload_per_page' );
1270      if ( empty( $media_per_page ) || $media_per_page < 1 ) {
1271          $media_per_page = 20;
1272      }
1273  
1274      /**
1275       * Filters the number of items to list per page when listing media items.
1276       *
1277       * @since 2.9.0
1278       *
1279       * @param int $media_per_page Number of media to list. Default 20.
1280       */
1281      $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
1282  
1283      $post_mime_types = get_post_mime_types();
1284      if ( isset( $q['post_mime_type'] ) && ! array_intersect( (array) $q['post_mime_type'], array_keys( $post_mime_types ) ) ) {
1285          unset( $q['post_mime_type'] );
1286      }
1287  
1288      foreach ( array_keys( $post_mime_types ) as $type ) {
1289          if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" === $q['attachment-filter'] ) {
1290              $q['post_mime_type'] = $type;
1291              break;
1292          }
1293      }
1294  
1295      if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' === $q['attachment-filter'] ) ) {
1296          $q['post_parent'] = 0;
1297      }
1298  
1299      if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' === $q['attachment-filter'] ) ) {
1300          $q['author'] = get_current_user_id();
1301      }
1302  
1303      // Filter query clauses to include filenames.
1304      if ( isset( $q['s'] ) ) {
1305          add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
1306      }
1307  
1308      return $q;
1309  }
1310  
1311  /**
1312   * Executes a query for attachments. An array of WP_Query arguments
1313   * can be passed in, which will override the arguments set by this function.
1314   *
1315   * @since 2.5.0
1316   *
1317   * @param array|false $q Optional. Array of query variables to use to build the query.
1318   *                       Defaults to the `$_GET` superglobal.
1319   * @return array
1320   */
1321  function wp_edit_attachments_query( $q = false ) {
1322      wp( wp_edit_attachments_query_vars( $q ) );
1323  
1324      $post_mime_types       = get_post_mime_types();
1325      $avail_post_mime_types = get_available_post_mime_types( 'attachment' );
1326  
1327      return array( $post_mime_types, $avail_post_mime_types );
1328  }
1329  
1330  /**
1331   * Returns the list of classes to be used by a meta box.
1332   *
1333   * @since 2.5.0
1334   *
1335   * @param string $box_id    Meta box ID (used in the 'id' attribute for the meta box).
1336   * @param string $screen_id The screen on which the meta box is shown.
1337   * @return string Space-separated string of class names.
1338   */
1339  function postbox_classes( $box_id, $screen_id ) {
1340      if ( isset( $_GET['edit'] ) && $_GET['edit'] == $box_id ) {
1341          $classes = array( '' );
1342      } elseif ( get_user_option( 'closedpostboxes_' . $screen_id ) ) {
1343          $closed = get_user_option( 'closedpostboxes_' . $screen_id );
1344          if ( ! is_array( $closed ) ) {
1345              $classes = array( '' );
1346          } else {
1347              $classes = in_array( $box_id, $closed, true ) ? array( 'closed' ) : array( '' );
1348          }
1349      } else {
1350          $classes = array( '' );
1351      }
1352  
1353      /**
1354       * Filters the postbox classes for a specific screen and box ID combo.
1355       *
1356       * The dynamic portions of the hook name, `$screen_id` and `$box_id`, refer to
1357       * the screen ID and meta box ID, respectively.
1358       *
1359       * @since 3.2.0
1360       *
1361       * @param string[] $classes An array of postbox classes.
1362       */
1363      $classes = apply_filters( "postbox_classes_{$screen_id}_{$box_id}", $classes );
1364  
1365      return implode( ' ', $classes );
1366  }
1367  
1368  /**
1369   * Returns a sample permalink based on the post name.
1370   *
1371   * @since 2.5.0
1372   *
1373   * @param int|WP_Post $id    Post ID or post object.
1374   * @param string|null $title Optional. Title to override the post's current title
1375   *                           when generating the post name. Default null.
1376   * @param string|null $name  Optional. Name to override the post name. Default null.
1377   * @return array {
1378   *     Array containing the sample permalink with placeholder for the post name, and the post name.
1379   *
1380   *     @type string $0 The permalink with placeholder for the post name.
1381   *     @type string $1 The post name.
1382   * }
1383   */
1384  function get_sample_permalink( $id, $title = null, $name = null ) {
1385      $post = get_post( $id );
1386      if ( ! $post ) {
1387          return array( '', '' );
1388      }
1389  
1390      $ptype = get_post_type_object( $post->post_type );
1391  
1392      $original_status = $post->post_status;
1393      $original_date   = $post->post_date;
1394      $original_name   = $post->post_name;
1395  
1396      // Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published.
1397      if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) {
1398          $post->post_status = 'publish';
1399          $post->post_name   = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
1400      }
1401  
1402      // If the user wants to set a new name -- override the current one.
1403      // Note: if empty name is supplied -- use the title instead, see #6072.
1404      if ( ! is_null( $name ) ) {
1405          $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
1406      }
1407  
1408      $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
1409  
1410      $post->filter = 'sample';
1411  
1412      $permalink = get_permalink( $post, true );
1413  
1414      // Replace custom post_type token with generic pagename token for ease of use.
1415      $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
1416  
1417      // Handle page hierarchy.
1418      if ( $ptype->hierarchical ) {
1419          $uri = get_page_uri( $post );
1420          if ( $uri ) {
1421              $uri = untrailingslashit( $uri );
1422              $uri = strrev( stristr( strrev( $uri ), '/' ) );
1423              $uri = untrailingslashit( $uri );
1424          }
1425  
1426          /** This filter is documented in wp-admin/edit-tag-form.php */
1427          $uri = apply_filters( 'editable_slug', $uri, $post );
1428          if ( ! empty( $uri ) ) {
1429              $uri .= '/';
1430          }
1431          $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
1432      }
1433  
1434      /** This filter is documented in wp-admin/edit-tag-form.php */
1435      $permalink         = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
1436      $post->post_status = $original_status;
1437      $post->post_date   = $original_date;
1438      $post->post_name   = $original_name;
1439      unset( $post->filter );
1440  
1441      /**
1442       * Filters the sample permalink.
1443       *
1444       * @since 4.4.0
1445       *
1446       * @param array   $permalink {
1447       *     Array containing the sample permalink with placeholder for the post name, and the post name.
1448       *
1449       *     @type string $0 The permalink with placeholder for the post name.
1450       *     @type string $1 The post name.
1451       * }
1452       * @param int     $post_id Post ID.
1453       * @param string  $title   Post title.
1454       * @param string  $name    Post name (slug).
1455       * @param WP_Post $post    Post object.
1456       */
1457      return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
1458  }
1459  
1460  /**
1461   * Returns the HTML of the sample permalink slug editor.
1462   *
1463   * @since 2.5.0
1464   *
1465   * @param int|WP_Post $id        Post ID or post object.
1466   * @param string|null $new_title Optional. New title. Default null.
1467   * @param string|null $new_slug  Optional. New slug. Default null.
1468   * @return string The HTML of the sample permalink slug editor.
1469   */
1470  function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
1471      $post = get_post( $id );
1472      if ( ! $post ) {
1473          return '';
1474      }
1475  
1476      list($permalink, $post_name) = get_sample_permalink( $post->ID, $new_title, $new_slug );
1477  
1478      $view_link      = false;
1479      $preview_target = '';
1480  
1481      if ( current_user_can( 'read_post', $post->ID ) ) {
1482          if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
1483              $view_link      = get_preview_post_link( $post );
1484              $preview_target = " target='wp-preview-{$post->ID}'";
1485          } else {
1486              if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
1487                  $view_link = get_permalink( $post );
1488              } else {
1489                  // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.
1490                  $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
1491              }
1492          }
1493      }
1494  
1495      // Permalinks without a post/page name placeholder don't have anything to edit.
1496      if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
1497          $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1498  
1499          if ( false !== $view_link ) {
1500              $display_link = urldecode( $view_link );
1501              $return      .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";
1502          } else {
1503              $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
1504          }
1505  
1506          // Encourage a pretty permalink setting.
1507          if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
1508              && ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $id )
1509          ) {
1510              $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small">' . __( 'Change Permalink Structure' ) . "</a></span>\n";
1511          }
1512      } else {
1513          if ( mb_strlen( $post_name ) > 34 ) {
1514              $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
1515          } else {
1516              $post_name_abridged = $post_name;
1517          }
1518  
1519          $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
1520          $display_link   = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
1521  
1522          $return  = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1523          $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
1524          $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
1525          $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n";
1526          $return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n";
1527      }
1528  
1529      /**
1530       * Filters the sample permalink HTML markup.
1531       *
1532       * @since 2.9.0
1533       * @since 4.4.0 Added `$post` parameter.
1534       *
1535       * @param string  $return    Sample permalink HTML markup.
1536       * @param int     $post_id   Post ID.
1537       * @param string  $new_title New sample permalink title.
1538       * @param string  $new_slug  New sample permalink slug.
1539       * @param WP_Post $post      Post object.
1540       */
1541      $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
1542  
1543      return $return;
1544  }
1545  
1546  /**
1547   * Returns HTML for the post thumbnail meta box.
1548   *
1549   * @since 2.9.0
1550   *
1551   * @param int|null         $thumbnail_id Optional. Thumbnail attachment ID. Default null.
1552   * @param int|WP_Post|null $post         Optional. The post ID or object associated
1553   *                                       with the thumbnail. Defaults to global $post.
1554   * @return string The post thumbnail HTML.
1555   */
1556  function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
1557      $_wp_additional_image_sizes = wp_get_additional_image_sizes();
1558  
1559      $post               = get_post( $post );
1560      $post_type_object   = get_post_type_object( $post->post_type );
1561      $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>';
1562      $upload_iframe_src  = get_upload_iframe_src( 'image', $post->ID );
1563  
1564      $content = sprintf(
1565          $set_thumbnail_link,
1566          esc_url( $upload_iframe_src ),
1567          '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise.
1568          esc_html( $post_type_object->labels->set_featured_image )
1569      );
1570  
1571      if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
1572          $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 );
1573  
1574          /**
1575           * Filters the size used to display the post thumbnail image in the 'Featured image' meta box.
1576           *
1577           * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
1578           * image size is registered, which differs from the 'thumbnail' image size
1579           * managed via the Settings > Media screen.
1580           *
1581           * @since 4.4.0
1582           *
1583           * @param string|int[] $size         Requested image size. Can be any registered image size name, or
1584           *                                   an array of width and height values in pixels (in that order).
1585           * @param int          $thumbnail_id Post thumbnail attachment ID.
1586           * @param WP_Post      $post         The post object associated with the thumbnail.
1587           */
1588          $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
1589  
1590          $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
1591  
1592          if ( ! empty( $thumbnail_html ) ) {
1593              $content  = sprintf(
1594                  $set_thumbnail_link,
1595                  esc_url( $upload_iframe_src ),
1596                  ' aria-describedby="set-post-thumbnail-desc"',
1597                  $thumbnail_html
1598              );
1599              $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
1600              $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
1601          }
1602      }
1603  
1604      $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
1605  
1606      /**
1607       * Filters the admin post thumbnail HTML markup to return.
1608       *
1609       * @since 2.9.0
1610       * @since 3.5.0 Added the `$post_id` parameter.
1611       * @since 4.6.0 Added the `$thumbnail_id` parameter.
1612       *
1613       * @param string   $content      Admin post thumbnail HTML markup.
1614       * @param int      $post_id      Post ID.
1615       * @param int|null $thumbnail_id Thumbnail attachment ID, or null if there isn't one.
1616       */
1617      return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );
1618  }
1619  
1620  /**
1621   * Determines whether the post is currently being edited by another user.
1622   *
1623   * @since 2.5.0
1624   *
1625   * @param int|WP_Post $post_id ID or object of the post to check for editing.
1626   * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
1627   *                   the user with lock does not exist, or the post is locked by current user.
1628   */
1629  function wp_check_post_lock( $post_id ) {
1630      $post = get_post( $post_id );
1631      if ( ! $post ) {
1632          return false;
1633      }
1634  
1635      $lock = get_post_meta( $post->ID, '_edit_lock', true );
1636      if ( ! $lock ) {
1637          return false;
1638      }
1639  
1640      $lock = explode( ':', $lock );
1641      $time = $lock[0];
1642      $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
1643  
1644      if ( ! get_userdata( $user ) ) {
1645          return false;
1646      }
1647  
1648      /** This filter is documented in wp-admin/includes/ajax-actions.php */
1649      $time_window = apply_filters( 'wp_check_post_lock_window', 150 );
1650  
1651      if ( $time && $time > time() - $time_window && get_current_user_id() != $user ) {
1652          return $user;
1653      }
1654  
1655      return false;
1656  }
1657  
1658  /**
1659   * Marks the post as currently being edited by the current user.
1660   *
1661   * @since 2.5.0
1662   *
1663   * @param int|WP_Post $post_id ID or object of the post being edited.
1664   * @return array|false {
1665   *     Array of the lock time and user ID. False if the post does not exist, or there
1666   *     is no current user.
1667   *
1668   *     @type int $0 The current time as a Unix timestamp.
1669   *     @type int $1 The ID of the current user.
1670   * }
1671   */
1672  function wp_set_post_lock( $post_id ) {
1673      $post = get_post( $post_id );
1674      if ( ! $post ) {
1675          return false;
1676      }
1677  
1678      $user_id = get_current_user_id();
1679      if ( 0 == $user_id ) {
1680          return false;
1681      }
1682  
1683      $now  = time();
1684      $lock = "$now:$user_id";
1685  
1686      update_post_meta( $post->ID, '_edit_lock', $lock );
1687  
1688      return array( $now, $user_id );
1689  }
1690  
1691  /**
1692   * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
1693   *
1694   * @since 2.8.5
1695   */
1696  function _admin_notice_post_locked() {
1697      $post = get_post();
1698      if ( ! $post ) {
1699          return;
1700      }
1701  
1702      $user    = null;
1703      $user_id = wp_check_post_lock( $post->ID );
1704      if ( $user_id ) {
1705          $user = get_userdata( $user_id );
1706      }
1707  
1708      if ( $user ) {
1709          /**
1710           * Filters whether to show the post locked dialog.
1711           *
1712           * Returning false from the filter will prevent the dialog from being displayed.
1713           *
1714           * @since 3.6.0
1715           *
1716           * @param bool    $display Whether to display the dialog. Default true.
1717           * @param WP_Post $post    Post object.
1718           * @param WP_User $user    The user with the lock for the post.
1719           */
1720          if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) {
1721              return;
1722          }
1723  
1724          $locked = true;
1725      } else {
1726          $locked = false;
1727      }
1728  
1729      $sendback = wp_get_referer();
1730      if ( $locked && $sendback && false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) {
1731  
1732          $sendback_text = __( 'Go back' );
1733      } else {
1734          $sendback = admin_url( 'edit.php' );
1735  
1736          if ( 'post' !== $post->post_type ) {
1737              $sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
1738          }
1739  
1740          $sendback_text = get_post_type_object( $post->post_type )->labels->all_items;
1741      }
1742  
1743      $hidden = $locked ? '' : ' hidden';
1744  
1745      ?>
1746      <div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>">
1747      <div class="notification-dialog-background"></div>
1748      <div class="notification-dialog">
1749      <?php
1750  
1751      if ( $locked ) {
1752          $query_args = array();
1753          if ( get_post_type_object( $post->post_type )->public ) {
1754              if ( 'publish' === $post->post_status || $user->ID != $post->post_author ) {
1755                  // Latest content is in autosave.
1756                  $nonce                       = wp_create_nonce( 'post_preview_' . $post->ID );
1757                  $query_args['preview_id']    = $post->ID;
1758                  $query_args['preview_nonce'] = $nonce;
1759              }
1760          }
1761  
1762          $preview_link = get_preview_post_link( $post->ID, $query_args );
1763  
1764          /**
1765           * Filters whether to allow the post lock to be overridden.
1766           *
1767           * Returning false from the filter will disable the ability
1768           * to override the post lock.
1769           *
1770           * @since 3.6.0
1771           *
1772           * @param bool    $override Whether to allow the post lock to be overridden. Default true.
1773           * @param WP_Post $post     Post object.
1774           * @param WP_User $user     The user with the lock for the post.
1775           */
1776          $override = apply_filters( 'override_post_lock', true, $post, $user );
1777          $tab_last = $override ? '' : ' wp-tab-last';
1778  
1779          ?>
1780          <div class="post-locked-message">
1781          <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
1782          <p class="currently-editing wp-tab-first" tabindex="0">
1783          <?php
1784          if ( $override ) {
1785              /* translators: %s: User's display name. */
1786              printf( __( '%s is currently editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
1787          } else {
1788              /* translators: %s: User's display name. */
1789              printf( __( '%s is currently editing this post.' ), esc_html( $user->display_name ) );
1790          }
1791          ?>
1792          </p>
1793          <?php
1794          /**
1795           * Fires inside the post locked dialog before the buttons are displayed.
1796           *
1797           * @since 3.6.0
1798           * @since 5.4.0 The $user parameter was added.
1799           *
1800           * @param WP_Post $post Post object.
1801           * @param WP_User $user The user with the lock for the post.
1802           */
1803          do_action( 'post_locked_dialog', $post, $user );
1804          ?>
1805          <p>
1806          <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
1807          <?php if ( $preview_link ) { ?>
1808          <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e( 'Preview' ); ?></a>
1809              <?php
1810          }
1811  
1812          // Allow plugins to prevent some users overriding the post lock.
1813          if ( $override ) {
1814              ?>
1815      <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e( 'Take over' ); ?></a>
1816              <?php
1817          }
1818  
1819          ?>
1820          </p>
1821          </div>
1822          <?php
1823      } else {
1824          ?>
1825          <div class="post-taken-over">
1826              <div class="post-locked-avatar"></div>
1827              <p class="wp-tab-first" tabindex="0">
1828              <span class="currently-editing"></span><br />
1829              <span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision&hellip;' ); ?></span>
1830              <span class="locked-saved hidden"><?php _e( 'Your latest changes were saved as a revision.' ); ?></span>
1831              </p>
1832              <?php
1833              /**
1834               * Fires inside the dialog displayed when a user has lost the post lock.
1835               *
1836               * @since 3.6.0
1837               *
1838               * @param WP_Post $post Post object.
1839               */
1840              do_action( 'post_lock_lost_dialog', $post );
1841              ?>
1842              <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
1843          </div>
1844          <?php
1845      }
1846  
1847      ?>
1848      </div>
1849      </div>
1850      <?php
1851  }
1852  
1853  /**
1854   * Creates autosave data for the specified post from `$_POST` data.
1855   *
1856   * @since 2.6.0
1857   *
1858   * @param array|int $post_data Associative array containing the post data, or integer post ID.
1859   *                             If a numeric post ID is provided, will use the `$_POST` superglobal.
1860   * @return int|WP_Error The autosave revision ID. WP_Error or 0 on error.
1861   */
1862  function wp_create_post_autosave( $post_data ) {
1863      if ( is_numeric( $post_data ) ) {
1864          $post_id   = $post_data;
1865          $post_data = $_POST;
1866      } else {
1867          $post_id = (int) $post_data['post_ID'];
1868      }
1869  
1870      $post_data = _wp_translate_postdata( true, $post_data );
1871      if ( is_wp_error( $post_data ) ) {
1872          return $post_data;
1873      }
1874      $post_data = _wp_get_allowed_postdata( $post_data );
1875  
1876      $post_author = get_current_user_id();
1877  
1878      // Store one autosave per author. If there is already an autosave, overwrite it.
1879      $old_autosave = wp_get_post_autosave( $post_id, $post_author );
1880      if ( $old_autosave ) {
1881          $new_autosave                = _wp_post_revision_data( $post_data, true );
1882          $new_autosave['ID']          = $old_autosave->ID;
1883          $new_autosave['post_author'] = $post_author;
1884  
1885          $post = get_post( $post_id );
1886  
1887          // If the new autosave has the same content as the post, delete the autosave.
1888          $autosave_is_different = false;
1889          foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
1890              if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
1891                  $autosave_is_different = true;
1892                  break;
1893              }
1894          }
1895  
1896          if ( ! $autosave_is_different ) {
1897              wp_delete_post_revision( $old_autosave->ID );
1898              return 0;
1899          }
1900  
1901          /**
1902           * Fires before an autosave is stored.
1903           *
1904           * @since 4.1.0
1905           *
1906           * @param array $new_autosave Post array - the autosave that is about to be saved.
1907           */
1908          do_action( 'wp_creating_autosave', $new_autosave );
1909  
1910          return wp_update_post( $new_autosave );
1911      }
1912  
1913      // _wp_put_post_revision() expects unescaped.
1914      $post_data = wp_unslash( $post_data );
1915  
1916      // Otherwise create the new autosave as a special post revision.
1917      return _wp_put_post_revision( $post_data, true );
1918  }
1919  
1920  /**
1921   * Saves a draft or manually autosaves for the purpose of showing a post preview.
1922   *
1923   * @since 2.7.0
1924   *
1925   * @return string URL to redirect to show the preview.
1926   */
1927  function post_preview() {
1928  
1929      $post_ID     = (int) $_POST['post_ID'];
1930      $_POST['ID'] = $post_ID;
1931  
1932      $post = get_post( $post_ID );
1933      if ( ! $post ) {
1934          wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
1935      }
1936  
1937      if ( ! current_user_can( 'edit_post', $post->ID ) ) {
1938          wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
1939      }
1940  
1941      $is_autosave = false;
1942  
1943      if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author
1944          && ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status )
1945      ) {
1946          $saved_post_id = edit_post();
1947      } else {
1948          $is_autosave = true;
1949  
1950          if ( isset( $_POST['post_status'] ) && 'auto-draft' === $_POST['post_status'] ) {
1951              $_POST['post_status'] = 'draft';
1952          }
1953  
1954          $saved_post_id = wp_create_post_autosave( $post->ID );
1955      }
1956  
1957      if ( is_wp_error( $saved_post_id ) ) {
1958          wp_die( $saved_post_id->get_error_message() );
1959      }
1960  
1961      $query_args = array();
1962  
1963      if ( $is_autosave && $saved_post_id ) {
1964          $query_args['preview_id']    = $post->ID;
1965          $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
1966  
1967          if ( isset( $_POST['post_format'] ) ) {
1968              $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
1969          }
1970  
1971          if ( isset( $_POST['_thumbnail_id'] ) ) {
1972              $query_args['_thumbnail_id'] = ( (int) $_POST['_thumbnail_id'] <= 0 ) ? '-1' : (int) $_POST['_thumbnail_id'];
1973          }
1974      }
1975  
1976      return get_preview_post_link( $post, $query_args );
1977  }
1978  
1979  /**
1980   * Saves a post submitted with XHR.
1981   *
1982   * Intended for use with heartbeat and autosave.js
1983   *
1984   * @since 3.9.0
1985   *
1986   * @param array $post_data Associative array of the submitted post data.
1987   * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
1988   *               The ID can be the draft post_id or the autosave revision post_id.
1989   */
1990  function wp_autosave( $post_data ) {
1991      // Back-compat.
1992      if ( ! defined( 'DOING_AUTOSAVE' ) ) {
1993          define( 'DOING_AUTOSAVE', true );
1994      }
1995  
1996      $post_id              = (int) $post_data['post_id'];
1997      $post_data['ID']      = $post_id;
1998      $post_data['post_ID'] = $post_id;
1999  
2000      if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) {
2001          return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) );
2002      }
2003  
2004      $post = get_post( $post_id );
2005  
2006      if ( ! current_user_can( 'edit_post', $post->ID ) ) {
2007          return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) );
2008      }
2009  
2010      if ( 'auto-draft' === $post->post_status ) {
2011          $post_data['post_status'] = 'draft';
2012      }
2013  
2014      if ( 'page' !== $post_data['post_type'] && ! empty( $post_data['catslist'] ) ) {
2015          $post_data['post_category'] = explode( ',', $post_data['catslist'] );
2016      }
2017  
2018      if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author
2019          && ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status )
2020      ) {
2021          // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked.
2022          return edit_post( wp_slash( $post_data ) );
2023      } else {
2024          // Non-drafts or other users' drafts are not overwritten.
2025          // The autosave is stored in a special post revision for each user.
2026          return wp_create_post_autosave( wp_slash( $post_data ) );
2027      }
2028  }
2029  
2030  /**
2031   * Redirects to previous page.
2032   *
2033   * @since 2.7.0
2034   *
2035   * @param int $post_id Optional. Post ID.
2036   */
2037  function redirect_post( $post_id = '' ) {
2038      if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {
2039          $status = get_post_status( $post_id );
2040  
2041          if ( isset( $_POST['publish'] ) ) {
2042              switch ( $status ) {
2043                  case 'pending':
2044                      $message = 8;
2045                      break;
2046                  case 'future':
2047                      $message = 9;
2048                      break;
2049                  default:
2050                      $message = 6;
2051              }
2052          } else {
2053              $message = 'draft' === $status ? 10 : 1;
2054          }
2055  
2056          $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
2057      } elseif ( isset( $_POST['addmeta'] ) && $_POST['addmeta'] ) {
2058          $location = add_query_arg( 'message', 2, wp_get_referer() );
2059          $location = explode( '#', $location );
2060          $location = $location[0] . '#postcustom';
2061      } elseif ( isset( $_POST['deletemeta'] ) && $_POST['deletemeta'] ) {
2062          $location = add_query_arg( 'message', 3, wp_get_referer() );
2063          $location = explode( '#', $location );
2064          $location = $location[0] . '#postcustom';
2065      } else {
2066          $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
2067      }
2068  
2069      /**
2070       * Filters the post redirect destination URL.
2071       *
2072       * @since 2.9.0
2073       *
2074       * @param string $location The destination URL.
2075       * @param int    $post_id  The post ID.
2076       */
2077      wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
2078      exit;
2079  }
2080  
2081  /**
2082   * Sanitizes POST values from a checkbox taxonomy metabox.
2083   *
2084   * @since 5.1.0
2085   *
2086   * @param string $taxonomy The taxonomy name.
2087   * @param array  $terms    Raw term data from the 'tax_input' field.
2088   * @return int[] Array of sanitized term IDs.
2089   */
2090  function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonomy, $terms ) {
2091      return array_map( 'intval', $terms );
2092  }
2093  
2094  /**
2095   * Sanitizes POST values from an input taxonomy metabox.
2096   *
2097   * @since 5.1.0
2098   *
2099   * @param string       $taxonomy The taxonomy name.
2100   * @param array|string $terms    Raw term data from the 'tax_input' field.
2101   * @return array
2102   */
2103  function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) {
2104      /*
2105       * Assume that a 'tax_input' string is a comma-separated list of term names.
2106       * Some languages may use a character other than a comma as a delimiter, so we standardize on
2107       * commas before parsing the list.
2108       */
2109      if ( ! is_array( $terms ) ) {
2110          $comma = _x( ',', 'tag delimiter' );
2111          if ( ',' !== $comma ) {
2112              $terms = str_replace( $comma, ',', $terms );
2113          }
2114          $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
2115      }
2116  
2117      $clean_terms = array();
2118      foreach ( $terms as $term ) {
2119          // Empty terms are invalid input.
2120          if ( empty( $term ) ) {
2121              continue;
2122          }
2123  
2124          $_term = get_terms(
2125              array(
2126                  'taxonomy'   => $taxonomy,
2127                  'name'       => $term,
2128                  'fields'     => 'ids',
2129                  'hide_empty' => false,
2130              )
2131          );
2132  
2133          if ( ! empty( $_term ) ) {
2134              $clean_terms[] = (int) $_term[0];
2135          } else {
2136              // No existing term was found, so pass the string. A new term will be created.
2137              $clean_terms[] = $term;
2138          }
2139      }
2140  
2141      return $clean_terms;
2142  }
2143  
2144  /**
2145   * Returns whether the post can be edited in the block editor.
2146   *
2147   * @since 5.0.0
2148   *
2149   * @param int|WP_Post $post Post ID or WP_Post object.
2150   * @return bool Whether the post can be edited in the block editor.
2151   */
2152  function use_block_editor_for_post( $post ) {
2153      $post = get_post( $post );
2154  
2155      if ( ! $post ) {
2156          return false;
2157      }
2158  
2159      // We're in the meta box loader, so don't use the block editor.
2160      if ( isset( $_GET['meta-box-loader'] ) ) {
2161          check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
2162          return false;
2163      }
2164  
2165      $use_block_editor = use_block_editor_for_post_type( $post->post_type );
2166  
2167      /**
2168       * Filters whether a post is able to be edited in the block editor.
2169       *
2170       * @since 5.0.0
2171       *
2172       * @param bool    $use_block_editor Whether the post can be edited or not.
2173       * @param WP_Post $post             The post being checked.
2174       */
2175      return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
2176  }
2177  
2178  /**
2179   * Returns whether a post type is compatible with the block editor.
2180   *
2181   * The block editor depends on the REST API, and if the post type is not shown in the
2182   * REST API, then it won't work with the block editor.
2183   *
2184   * @since 5.0.0
2185   *
2186   * @param string $post_type The post type.
2187   * @return bool Whether the post type can be edited with the block editor.
2188   */
2189  function use_block_editor_for_post_type( $post_type ) {
2190      if ( ! post_type_exists( $post_type ) ) {
2191          return false;
2192      }
2193  
2194      if ( ! post_type_supports( $post_type, 'editor' ) ) {
2195          return false;
2196      }
2197  
2198      $post_type_object = get_post_type_object( $post_type );
2199      if ( $post_type_object && ! $post_type_object->show_in_rest ) {
2200          return false;
2201      }
2202  
2203      /**
2204       * Filters whether a post is able to be edited in the block editor.
2205       *
2206       * @since 5.0.0
2207       *
2208       * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
2209       * @param string $post_type         The post type being checked.
2210       */
2211      return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
2212  }
2213  
2214  /**
2215   * Prepares server-registered blocks for the block editor.
2216   *
2217   * Returns an associative array of registered block data keyed by block name. Data includes properties
2218   * of a block relevant for client registration.
2219   *
2220   * @since 5.0.0
2221   *
2222   * @return array An associative array of registered block data.
2223   */
2224  function get_block_editor_server_block_settings() {
2225      $block_registry = WP_Block_Type_Registry::get_instance();
2226      $blocks         = array();
2227      $fields_to_pick = array(
2228          'api_version'      => 'apiVersion',
2229          'title'            => 'title',
2230          'description'      => 'description',
2231          'icon'             => 'icon',
2232          'attributes'       => 'attributes',
2233          'provides_context' => 'providesContext',
2234          'uses_context'     => 'usesContext',
2235          'supports'         => 'supports',
2236          'category'         => 'category',
2237          'styles'           => 'styles',
2238          'textdomain'       => 'textdomain',
2239          'parent'           => 'parent',
2240          'ancestor'         => 'ancestor',
2241          'keywords'         => 'keywords',
2242          'example'          => 'example',
2243          'variations'       => 'variations',
2244      );
2245  
2246      foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
2247          foreach ( $fields_to_pick as $field => $key ) {
2248              if ( ! isset( $block_type->{ $field } ) ) {
2249                  continue;
2250              }
2251  
2252              if ( ! isset( $blocks[ $block_name ] ) ) {
2253                  $blocks[ $block_name ] = array();
2254              }
2255  
2256              $blocks[ $block_name ][ $key ] = $block_type->{ $field };
2257          }
2258      }
2259  
2260      return $blocks;
2261  }
2262  
2263  /**
2264   * Renders the meta boxes forms.
2265   *
2266   * @since 5.0.0
2267   */
2268  function the_block_editor_meta_boxes() {
2269      global $post, $current_screen, $wp_meta_boxes;
2270  
2271      // Handle meta box state.
2272      $_original_meta_boxes = $wp_meta_boxes;
2273  
2274      /**
2275       * Fires right before the meta boxes are rendered.
2276       *
2277       * This allows for the filtering of meta box data, that should already be
2278       * present by this point. Do not use as a means of adding meta box data.
2279       *
2280       * @since 5.0.0
2281       *
2282       * @param array $wp_meta_boxes Global meta box state.
2283       */
2284      $wp_meta_boxes = apply_filters( 'filter_block_editor_meta_boxes', $wp_meta_boxes );
2285      $locations     = array( 'side', 'normal', 'advanced' );
2286      $priorities    = array( 'high', 'sorted', 'core', 'default', 'low' );
2287  
2288      // Render meta boxes.
2289      ?>
2290      <form class="metabox-base-form">
2291      <?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
2292      </form>
2293      <form id="toggle-custom-fields-form" method="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>">
2294          <?php wp_nonce_field( 'toggle-custom-fields', 'toggle-custom-fields-nonce' ); ?>
2295          <input type="hidden" name="action" value="toggle-custom-fields" />
2296      </form>
2297      <?php foreach ( $locations as $location ) : ?>
2298          <form class="metabox-location-<?php echo esc_attr( $location ); ?>" onsubmit="return false;">
2299              <div id="poststuff" class="sidebar-open">
2300                  <div id="postbox-container-2" class="postbox-container">
2301                      <?php
2302                      do_meta_boxes(
2303                          $current_screen,
2304                          $location,
2305                          $post
2306                      );
2307                      ?>
2308                  </div>
2309              </div>
2310          </form>
2311      <?php endforeach; ?>
2312      <?php
2313  
2314      $meta_boxes_per_location = array();
2315      foreach ( $locations as $location ) {
2316          $meta_boxes_per_location[ $location ] = array();
2317  
2318          if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ] ) ) {
2319              continue;
2320          }
2321  
2322          foreach ( $priorities as $priority ) {
2323              if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ] ) ) {
2324                  continue;
2325              }
2326  
2327              $meta_boxes = (array) $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ];
2328              foreach ( $meta_boxes as $meta_box ) {
2329                  if ( false == $meta_box || ! $meta_box['title'] ) {
2330                      continue;
2331                  }
2332  
2333                  // If a meta box is just here for back compat, don't show it in the block editor.
2334                  if ( isset( $meta_box['args']['__back_compat_meta_box'] ) && $meta_box['args']['__back_compat_meta_box'] ) {
2335                      continue;
2336                  }
2337  
2338                  $meta_boxes_per_location[ $location ][] = array(
2339                      'id'    => $meta_box['id'],
2340                      'title' => $meta_box['title'],
2341                  );
2342              }
2343          }
2344      }
2345  
2346      /*
2347       * Sadly we probably cannot add this data directly into editor settings.
2348       *
2349       * Some meta boxes need `admin_head` to fire for meta box registry.
2350       * `admin_head` fires after `admin_enqueue_scripts`, which is where we create
2351       * our editor instance.
2352       */
2353      $script = 'window._wpLoadBlockEditor.then( function() {
2354          wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' );
2355      } );';
2356  
2357      wp_add_inline_script( 'wp-edit-post', $script );
2358  
2359      /*
2360       * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed.
2361       * Otherwise, meta boxes will not display because inline scripts for `wp-edit-post`
2362       * will not be printed again after this point.
2363       */
2364      if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
2365          printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
2366      }
2367  
2368      /*
2369       * If the 'postcustom' meta box is enabled, then we need to perform
2370       * some extra initialization on it.
2371       */
2372      $enable_custom_fields = (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true );
2373  
2374      if ( $enable_custom_fields ) {
2375          $script = "( function( $ ) {
2376              if ( $('#postcustom').length ) {
2377                  $( '#the-list' ).wpList( {
2378                      addBefore: function( s ) {
2379                          s.data += '&post_id=$post->ID';
2380                          return s;
2381                      },
2382                      addAfter: function() {
2383                          $('table#list-table').show();
2384                      }
2385                  });
2386              }
2387          } )( jQuery );";
2388          wp_enqueue_script( 'wp-lists' );
2389          wp_add_inline_script( 'wp-lists', $script );
2390      }
2391  
2392      // Reset meta box data.
2393      $wp_meta_boxes = $_original_meta_boxes;
2394  }
2395  
2396  /**
2397   * Renders the hidden form required for the meta boxes form.
2398   *
2399   * @since 5.0.0
2400   *
2401   * @param WP_Post $post Current post object.
2402   */
2403  function the_block_editor_meta_box_post_form_hidden_fields( $post ) {
2404      $form_extra = '';
2405      if ( 'auto-draft' === $post->post_status ) {
2406          $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
2407      }
2408      $form_action  = 'editpost';
2409      $nonce_action = 'update-post_' . $post->ID;
2410      $form_extra  .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />";
2411      $referer      = wp_get_referer();
2412      $current_user = wp_get_current_user();
2413      $user_id      = $current_user->ID;
2414      wp_nonce_field( $nonce_action );
2415  
2416      /*
2417       * Some meta boxes hook into these actions to add hidden input fields in the classic post form.
2418       * For backward compatibility, we can capture the output from these actions,
2419       * and extract the hidden input fields.
2420       */
2421      ob_start();
2422      /** This filter is documented in wp-admin/edit-form-advanced.php */
2423      do_action( 'edit_form_after_title', $post );
2424      /** This filter is documented in wp-admin/edit-form-advanced.php */
2425      do_action( 'edit_form_advanced', $post );
2426      $classic_output = ob_get_clean();
2427  
2428      $classic_elements = wp_html_split( $classic_output );
2429      $hidden_inputs    = '';
2430      foreach ( $classic_elements as $element ) {
2431          if ( 0 !== strpos( $element, '<input ' ) ) {
2432              continue;
2433          }
2434  
2435          if ( preg_match( '/\stype=[\'"]hidden[\'"]\s/', $element ) ) {
2436              echo $element;
2437          }
2438      }
2439      ?>
2440      <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
2441      <input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" />
2442      <input type="hidden" id="originalaction" name="originalaction" value="<?php echo esc_attr( $form_action ); ?>" />
2443      <input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
2444      <input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status ); ?>" />
2445      <input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>" />
2446  
2447      <?php
2448      if ( 'draft' !== get_post_status( $post ) ) {
2449          wp_original_referer_field( true, 'previous' );
2450      }
2451      echo $form_extra;
2452      wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
2453      wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
2454      // Permalink title nonce.
2455      wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
2456  
2457      /**
2458       * Adds hidden input fields to the meta box save form.
2459       *
2460       * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to
2461       * the server when meta boxes are saved.
2462       *
2463       * @since 5.0.0
2464       *
2465       * @param WP_Post $post The post that is being edited.
2466       */
2467      do_action( 'block_editor_meta_box_hidden_fields', $post );
2468  }
2469  
2470  /**
2471   * Disables block editor for wp_navigation type posts so they can be managed via the UI.
2472   *
2473   * @since 5.9.0
2474   * @access private
2475   *
2476   * @param bool   $value Whether the CPT supports block editor or not.
2477   * @param string $post_type Post type.
2478   * @return bool Whether the block editor should be disabled or not.
2479   */
2480  function _disable_block_editor_for_navigation_post_type( $value, $post_type ) {
2481      if ( 'wp_navigation' === $post_type ) {
2482          return false;
2483      }
2484  
2485      return $value;
2486  }
2487  
2488  /**
2489   * This callback disables the content editor for wp_navigation type posts.
2490   * Content editor cannot handle wp_navigation type posts correctly.
2491   * We cannot disable the "editor" feature in the wp_navigation's CPT definition
2492   * because it disables the ability to save navigation blocks via REST API.
2493   *
2494   * @since 5.9.0
2495   * @access private
2496   *
2497   * @param WP_Post $post An instance of WP_Post class.
2498   */
2499  function _disable_content_editor_for_navigation_post_type( $post ) {
2500      $post_type = get_post_type( $post );
2501      if ( 'wp_navigation' !== $post_type ) {
2502          return;
2503      }
2504  
2505      remove_post_type_support( $post_type, 'editor' );
2506  }
2507  
2508  /**
2509   * This callback enables content editor for wp_navigation type posts.
2510   * We need to enable it back because we disable it to hide
2511   * the content editor for wp_navigation type posts.
2512   *
2513   * @since 5.9.0
2514   * @access private
2515   *
2516   * @see _disable_content_editor_for_navigation_post_type
2517   *
2518   * @param WP_Post $post An instance of WP_Post class.
2519   */
2520  function _enable_content_editor_for_navigation_post_type( $post ) {
2521      $post_type = get_post_type( $post );
2522      if ( 'wp_navigation' !== $post_type ) {
2523          return;
2524      }
2525  
2526      add_post_type_support( $post_type, 'editor' );
2527  }


Generated: Tue Dec 24 01:00:02 2024 Cross-referenced by PHPXref 0.7.1