[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Mon Jul 15 08:20:02 2024 Cross-referenced by PHPXref