[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> revision.php (source)

   1  <?php
   2  /**
   3   * Post revision functions.
   4   *
   5   * @package WordPress
   6   * @subpackage Post_Revisions
   7   */
   8  
   9  /**
  10   * Determines which fields of posts are to be saved in revisions.
  11   *
  12   * @since 2.6.0
  13   * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter.
  14   * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`.
  15   * @access private
  16   *
  17   * @staticvar array $fields
  18   *
  19   * @param array|WP_Post $post       Optional. A post array or a WP_Post object being processed
  20   *                                  for insertion as a post revision. Default empty array.
  21   * @param bool          $deprecated Not used.
  22   * @return array Array of fields that can be versioned.
  23   */
  24  function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
  25      static $fields = null;
  26  
  27      if ( ! is_array( $post ) ) {
  28          $post = get_post( $post, ARRAY_A );
  29      }
  30  
  31      if ( is_null( $fields ) ) {
  32          // Allow these to be versioned.
  33          $fields = array(
  34              'post_title'   => __( 'Title' ),
  35              'post_content' => __( 'Content' ),
  36              'post_excerpt' => __( 'Excerpt' ),
  37          );
  38      }
  39  
  40      /**
  41       * Filters the list of fields saved in post revisions.
  42       *
  43       * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
  44       *
  45       * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
  46       * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
  47       * and 'post_author'.
  48       *
  49       * @since 2.6.0
  50       * @since 4.5.0 The `$post` parameter was added.
  51       *
  52       * @param array $fields List of fields to revision. Contains 'post_title',
  53       *                      'post_content', and 'post_excerpt' by default.
  54       * @param array $post   A post array being processed for insertion as a post revision.
  55       */
  56      $fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
  57  
  58      // WP uses these internally either in versioning or elsewhere - they cannot be versioned.
  59      foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) {
  60          unset( $fields[ $protect ] );
  61      }
  62  
  63      return $fields;
  64  }
  65  
  66  /**
  67   * Returns a post array ready to be inserted into the posts table as a post revision.
  68   *
  69   * @since 4.5.0
  70   * @access private
  71   *
  72   * @param array|WP_Post $post     Optional. A post array or a WP_Post object to be processed
  73   *                                for insertion as a post revision. Default empty array.
  74   * @param bool          $autosave Optional. Is the revision an autosave? Default false.
  75   * @return array Post array ready to be inserted as a post revision.
  76   */
  77  function _wp_post_revision_data( $post = array(), $autosave = false ) {
  78      if ( ! is_array( $post ) ) {
  79          $post = get_post( $post, ARRAY_A );
  80      }
  81  
  82      $fields = _wp_post_revision_fields( $post );
  83  
  84      $revision_data = array();
  85  
  86      foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) {
  87          $revision_data[ $field ] = $post[ $field ];
  88      }
  89  
  90      $revision_data['post_parent']   = $post['ID'];
  91      $revision_data['post_status']   = 'inherit';
  92      $revision_data['post_type']     = 'revision';
  93      $revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version.
  94      $revision_data['post_date']     = isset( $post['post_modified'] ) ? $post['post_modified'] : '';
  95      $revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : '';
  96  
  97      return $revision_data;
  98  }
  99  
 100  /**
 101   * Creates a revision for the current version of a post.
 102   *
 103   * Typically used immediately after a post update, as every update is a revision,
 104   * and the most recent revision always matches the current post.
 105   *
 106   * @since 2.6.0
 107   *
 108   * @param int $post_id The ID of the post to save as a revision.
 109   * @return int|WP_Error|void Void or 0 if error, new revision ID, if success.
 110   */
 111  function wp_save_post_revision( $post_id ) {
 112      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
 113          return;
 114      }
 115  
 116      $post = get_post( $post_id );
 117      if ( ! $post ) {
 118          return;
 119      }
 120  
 121      if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
 122          return;
 123      }
 124  
 125      if ( 'auto-draft' == $post->post_status ) {
 126          return;
 127      }
 128  
 129      if ( ! wp_revisions_enabled( $post ) ) {
 130          return;
 131      }
 132  
 133      /*
 134       * Compare the proposed update with the last stored revision verifying that
 135       * they are different, unless a plugin tells us to always save regardless.
 136       * If no previous revisions, save one.
 137       */
 138      $revisions = wp_get_post_revisions( $post_id );
 139      if ( $revisions ) {
 140          // Grab the last revision, but not an autosave.
 141          foreach ( $revisions as $revision ) {
 142              if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
 143                  $last_revision = $revision;
 144                  break;
 145              }
 146          }
 147  
 148          /**
 149           * Filters whether the post has changed since the last revision.
 150           *
 151           * By default a revision is saved only if one of the revisioned fields has changed.
 152           * This filter can override that so a revision is saved even if nothing has changed.
 153           *
 154           * @since 3.6.0
 155           *
 156           * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
 157           *                                   Default true.
 158           * @param WP_Post $last_revision     The last revision post object.
 159           * @param WP_Post $post              The post object.
 160           */
 161          if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) {
 162              $post_has_changed = false;
 163  
 164              foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
 165                  if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $last_revision->$field ) ) {
 166                      $post_has_changed = true;
 167                      break;
 168                  }
 169              }
 170  
 171              /**
 172               * Filters whether a post has changed.
 173               *
 174               * By default a revision is saved only if one of the revisioned fields has changed.
 175               * This filter allows for additional checks to determine if there were changes.
 176               *
 177               * @since 4.1.0
 178               *
 179               * @param bool    $post_has_changed Whether the post has changed.
 180               * @param WP_Post $last_revision    The last revision post object.
 181               * @param WP_Post $post             The post object.
 182               */
 183              $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
 184  
 185              // Don't save revision if post unchanged.
 186              if ( ! $post_has_changed ) {
 187                  return;
 188              }
 189          }
 190      }
 191  
 192      $return = _wp_put_post_revision( $post );
 193  
 194      // If a limit for the number of revisions to keep has been set,
 195      // delete the oldest ones.
 196      $revisions_to_keep = wp_revisions_to_keep( $post );
 197  
 198      if ( $revisions_to_keep < 0 ) {
 199          return $return;
 200      }
 201  
 202      $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
 203  
 204      $delete = count( $revisions ) - $revisions_to_keep;
 205  
 206      if ( $delete < 1 ) {
 207          return $return;
 208      }
 209  
 210      $revisions = array_slice( $revisions, 0, $delete );
 211  
 212      for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) {
 213          if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) {
 214              continue;
 215          }
 216  
 217          wp_delete_post_revision( $revisions[ $i ]->ID );
 218      }
 219  
 220      return $return;
 221  }
 222  
 223  /**
 224   * Retrieve the autosaved data of the specified post.
 225   *
 226   * Returns a post object containing the information that was autosaved for the
 227   * specified post. If the optional $user_id is passed, returns the autosave for that user
 228   * otherwise returns the latest autosave.
 229   *
 230   * @since 2.6.0
 231   *
 232   * @param int $post_id The post ID.
 233   * @param int $user_id Optional The post author ID.
 234   * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
 235   */
 236  function wp_get_post_autosave( $post_id, $user_id = 0 ) {
 237      $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
 238  
 239      foreach ( $revisions as $revision ) {
 240          if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
 241              if ( $user_id && $user_id != $revision->post_author ) {
 242                  continue;
 243              }
 244  
 245              return $revision;
 246          }
 247      }
 248  
 249      return false;
 250  }
 251  
 252  /**
 253   * Determines if the specified post is a revision.
 254   *
 255   * @since 2.6.0
 256   *
 257   * @param int|WP_Post $post Post ID or post object.
 258   * @return int|false ID of revision's parent on success, false if not a revision.
 259   */
 260  function wp_is_post_revision( $post ) {
 261      $post = wp_get_post_revision( $post );
 262      if ( ! $post ) {
 263          return false;
 264      }
 265  
 266      return (int) $post->post_parent;
 267  }
 268  
 269  /**
 270   * Determines if the specified post is an autosave.
 271   *
 272   * @since 2.6.0
 273   *
 274   * @param int|WP_Post $post Post ID or post object.
 275   * @return int|false ID of autosave's parent on success, false if not a revision.
 276   */
 277  function wp_is_post_autosave( $post ) {
 278      $post = wp_get_post_revision( $post );
 279      if ( ! $post ) {
 280          return false;
 281      }
 282  
 283      if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) {
 284          return (int) $post->post_parent;
 285      }
 286  
 287      return false;
 288  }
 289  
 290  /**
 291   * Inserts post data into the posts table as a post revision.
 292   *
 293   * @since 2.6.0
 294   * @access private
 295   *
 296   * @param int|WP_Post|array|null $post     Post ID, post object OR post array.
 297   * @param bool                   $autosave Optional. Is the revision an autosave?
 298   * @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
 299   */
 300  function _wp_put_post_revision( $post = null, $autosave = false ) {
 301      if ( is_object( $post ) ) {
 302          $post = get_object_vars( $post );
 303      } elseif ( ! is_array( $post ) ) {
 304          $post = get_post( $post, ARRAY_A );
 305      }
 306  
 307      if ( ! $post || empty( $post['ID'] ) ) {
 308          return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
 309      }
 310  
 311      if ( isset( $post['post_type'] ) && 'revision' == $post['post_type'] ) {
 312          return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
 313      }
 314  
 315      $post = _wp_post_revision_data( $post, $autosave );
 316      $post = wp_slash( $post ); // Since data is from DB.
 317  
 318      $revision_id = wp_insert_post( $post );
 319      if ( is_wp_error( $revision_id ) ) {
 320          return $revision_id;
 321      }
 322  
 323      if ( $revision_id ) {
 324          /**
 325           * Fires once a revision has been saved.
 326           *
 327           * @since 2.6.0
 328           *
 329           * @param int $revision_id Post revision ID.
 330           */
 331          do_action( '_wp_put_post_revision', $revision_id );
 332      }
 333  
 334      return $revision_id;
 335  }
 336  
 337  /**
 338   * Gets a post revision.
 339   *
 340   * @since 2.6.0
 341   *
 342   * @param int|WP_Post $post   The post ID or object.
 343   * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 344   *                            a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
 345   * @param string      $filter Optional sanitation filter. See sanitize_post().
 346   * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 347   */
 348  function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
 349      $revision = get_post( $post, OBJECT, $filter );
 350      if ( ! $revision ) {
 351          return $revision;
 352      }
 353      if ( 'revision' !== $revision->post_type ) {
 354          return null;
 355      }
 356  
 357      if ( OBJECT == $output ) {
 358          return $revision;
 359      } elseif ( ARRAY_A == $output ) {
 360          $_revision = get_object_vars( $revision );
 361          return $_revision;
 362      } elseif ( ARRAY_N == $output ) {
 363          $_revision = array_values( get_object_vars( $revision ) );
 364          return $_revision;
 365      }
 366  
 367      return $revision;
 368  }
 369  
 370  /**
 371   * Restores a post to the specified revision.
 372   *
 373   * Can restore a past revision using all fields of the post revision, or only selected fields.
 374   *
 375   * @since 2.6.0
 376   *
 377   * @param int|WP_Post $revision_id Revision ID or revision object.
 378   * @param array       $fields      Optional. What fields to restore from. Defaults to all.
 379   * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
 380   */
 381  function wp_restore_post_revision( $revision_id, $fields = null ) {
 382      $revision = wp_get_post_revision( $revision_id, ARRAY_A );
 383      if ( ! $revision ) {
 384          return $revision;
 385      }
 386  
 387      if ( ! is_array( $fields ) ) {
 388          $fields = array_keys( _wp_post_revision_fields( $revision ) );
 389      }
 390  
 391      $update = array();
 392      foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) {
 393          $update[ $field ] = $revision[ $field ];
 394      }
 395  
 396      if ( ! $update ) {
 397          return false;
 398      }
 399  
 400      $update['ID'] = $revision['post_parent'];
 401  
 402      $update = wp_slash( $update ); // Since data is from DB.
 403  
 404      $post_id = wp_update_post( $update );
 405      if ( ! $post_id || is_wp_error( $post_id ) ) {
 406          return $post_id;
 407      }
 408  
 409      // Update last edit user.
 410      update_post_meta( $post_id, '_edit_last', get_current_user_id() );
 411  
 412      /**
 413       * Fires after a post revision has been restored.
 414       *
 415       * @since 2.6.0
 416       *
 417       * @param int $post_id     Post ID.
 418       * @param int $revision_id Post revision ID.
 419       */
 420      do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
 421  
 422      return $post_id;
 423  }
 424  
 425  /**
 426   * Deletes a revision.
 427   *
 428   * Deletes the row from the posts table corresponding to the specified revision.
 429   *
 430   * @since 2.6.0
 431   *
 432   * @param int|WP_Post $revision_id Revision ID or revision object.
 433   * @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
 434   */
 435  function wp_delete_post_revision( $revision_id ) {
 436      $revision = wp_get_post_revision( $revision_id );
 437      if ( ! $revision ) {
 438          return $revision;
 439      }
 440  
 441      $delete = wp_delete_post( $revision->ID );
 442      if ( $delete ) {
 443          /**
 444           * Fires once a post revision has been deleted.
 445           *
 446           * @since 2.6.0
 447           *
 448           * @param int     $revision_id Post revision ID.
 449           * @param WP_Post $revision    Post revision object.
 450           */
 451          do_action( 'wp_delete_post_revision', $revision->ID, $revision );
 452      }
 453  
 454      return $delete;
 455  }
 456  
 457  /**
 458   * Returns all revisions of specified post.
 459   *
 460   * @since 2.6.0
 461   *
 462   * @see get_children()
 463   *
 464   * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`.
 465   * @param array|null  $args    Optional. Arguments for retrieving post revisions. Default null.
 466   * @return array An array of revisions, or an empty array if none.
 467   */
 468  function wp_get_post_revisions( $post_id = 0, $args = null ) {
 469      $post = get_post( $post_id );
 470      if ( ! $post || empty( $post->ID ) ) {
 471          return array();
 472      }
 473  
 474      $defaults = array(
 475          'order'         => 'DESC',
 476          'orderby'       => 'date ID',
 477          'check_enabled' => true,
 478      );
 479      $args     = wp_parse_args( $args, $defaults );
 480  
 481      if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) {
 482          return array();
 483      }
 484  
 485      $args = array_merge(
 486          $args,
 487          array(
 488              'post_parent' => $post->ID,
 489              'post_type'   => 'revision',
 490              'post_status' => 'inherit',
 491          )
 492      );
 493  
 494      $revisions = get_children( $args );
 495      if ( ! $revisions ) {
 496          return array();
 497      }
 498  
 499      return $revisions;
 500  }
 501  
 502  /**
 503   * Determine if revisions are enabled for a given post.
 504   *
 505   * @since 3.6.0
 506   *
 507   * @param WP_Post $post The post object.
 508   * @return bool True if number of revisions to keep isn't zero, false otherwise.
 509   */
 510  function wp_revisions_enabled( $post ) {
 511      return wp_revisions_to_keep( $post ) !== 0;
 512  }
 513  
 514  /**
 515   * Determine how many revisions to retain for a given post.
 516   *
 517   * By default, an infinite number of revisions are kept.
 518   *
 519   * The constant WP_POST_REVISIONS can be set in wp-config to specify the limit
 520   * of revisions to keep.
 521   *
 522   * @since 3.6.0
 523   *
 524   * @param WP_Post $post The post object.
 525   * @return int The number of revisions to keep.
 526   */
 527  function wp_revisions_to_keep( $post ) {
 528      $num = WP_POST_REVISIONS;
 529  
 530      if ( true === $num ) {
 531          $num = -1;
 532      } else {
 533          $num = intval( $num );
 534      }
 535  
 536      if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
 537          $num = 0;
 538      }
 539  
 540      /**
 541       * Filters the number of revisions to save for the given post.
 542       *
 543       * Overrides the value of WP_POST_REVISIONS.
 544       *
 545       * @since 3.6.0
 546       *
 547       * @param int     $num  Number of revisions to store.
 548       * @param WP_Post $post Post object.
 549       */
 550      return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
 551  }
 552  
 553  /**
 554   * Sets up the post object for preview based on the post autosave.
 555   *
 556   * @since 2.7.0
 557   * @access private
 558   *
 559   * @param WP_Post $post
 560   * @return WP_Post|false
 561   */
 562  function _set_preview( $post ) {
 563      if ( ! is_object( $post ) ) {
 564          return $post;
 565      }
 566  
 567      $preview = wp_get_post_autosave( $post->ID );
 568      if ( ! is_object( $preview ) ) {
 569          return $post;
 570      }
 571  
 572      $preview = sanitize_post( $preview );
 573  
 574      $post->post_content = $preview->post_content;
 575      $post->post_title   = $preview->post_title;
 576      $post->post_excerpt = $preview->post_excerpt;
 577  
 578      add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
 579      add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
 580  
 581      return $post;
 582  }
 583  
 584  /**
 585   * Filters the latest content for preview from the post autosave.
 586   *
 587   * @since 2.7.0
 588   * @access private
 589   */
 590  function _show_post_preview() {
 591      if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) {
 592          $id = (int) $_GET['preview_id'];
 593  
 594          if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) {
 595              wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 );
 596          }
 597  
 598          add_filter( 'the_preview', '_set_preview' );
 599      }
 600  }
 601  
 602  /**
 603   * Filters terms lookup to set the post format.
 604   *
 605   * @since 3.6.0
 606   * @access private
 607   *
 608   * @param array  $terms
 609   * @param int    $post_id
 610   * @param string $taxonomy
 611   * @return array
 612   */
 613  function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
 614      $post = get_post();
 615      if ( ! $post ) {
 616          return $terms;
 617      }
 618  
 619      if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) {
 620          return $terms;
 621      }
 622  
 623      if ( 'standard' == $_REQUEST['post_format'] ) {
 624          $terms = array();
 625      } else {
 626          $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );
 627          if ( $term ) {
 628              $terms = array( $term ); // Can only have one post format.
 629          }
 630      }
 631  
 632      return $terms;
 633  }
 634  
 635  /**
 636   * Filters post thumbnail lookup to set the post thumbnail.
 637   *
 638   * @since 4.6.0
 639   * @access private
 640   *
 641   * @param null|array|string $value    The value to return - a single metadata value, or an array of values.
 642   * @param int               $post_id  Post ID.
 643   * @param string            $meta_key Meta key.
 644   * @return null|array The default return value or the post thumbnail meta array.
 645   */
 646  function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
 647      $post = get_post();
 648      if ( ! $post ) {
 649          return $value;
 650      }
 651  
 652      if ( empty( $_REQUEST['_thumbnail_id'] ) ||
 653          empty( $_REQUEST['preview_id'] ) ||
 654          $post->ID != $post_id ||
 655          '_thumbnail_id' != $meta_key ||
 656          'revision' == $post->post_type ||
 657          $post_id != $_REQUEST['preview_id'] ) {
 658  
 659          return $value;
 660      }
 661  
 662      $thumbnail_id = intval( $_REQUEST['_thumbnail_id'] );
 663      if ( $thumbnail_id <= 0 ) {
 664          return '';
 665      }
 666  
 667      return strval( $thumbnail_id );
 668  }
 669  
 670  /**
 671   * Gets the post revision version.
 672   *
 673   * @since 3.6.0
 674   * @access private
 675   *
 676   * @param WP_Post $revision
 677   * @return int|false
 678   */
 679  function _wp_get_post_revision_version( $revision ) {
 680      if ( is_object( $revision ) ) {
 681          $revision = get_object_vars( $revision );
 682      } elseif ( ! is_array( $revision ) ) {
 683          return false;
 684      }
 685  
 686      if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) {
 687          return (int) $matches[1];
 688      }
 689  
 690      return 0;
 691  }
 692  
 693  /**
 694   * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
 695   *
 696   * @since 3.6.0
 697   * @access private
 698   *
 699   * @global wpdb $wpdb WordPress database abstraction object.
 700   *
 701   * @param WP_Post $post      Post object
 702   * @param array   $revisions Current revisions of the post
 703   * @return bool true if the revisions were upgraded, false if problems
 704   */
 705  function _wp_upgrade_revisions_of_post( $post, $revisions ) {
 706      global $wpdb;
 707  
 708      // Add post option exclusively.
 709      $lock   = "revision-upgrade-{$post->ID}";
 710      $now    = time();
 711      $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
 712      if ( ! $result ) {
 713          // If we couldn't get a lock, see how old the previous lock is.
 714          $locked = get_option( $lock );
 715          if ( ! $locked ) {
 716              // Can't write to the lock, and can't read the lock.
 717              // Something broken has happened.
 718              return false;
 719          }
 720  
 721          if ( $locked > $now - 3600 ) {
 722              // Lock is not too old: some other process may be upgrading this post. Bail.
 723              return false;
 724          }
 725  
 726          // Lock is too old - update it (below) and continue.
 727      }
 728  
 729      // If we could get a lock, re-"add" the option to fire all the correct filters.
 730      update_option( $lock, $now );
 731  
 732      reset( $revisions );
 733      $add_last = true;
 734  
 735      do {
 736          $this_revision = current( $revisions );
 737          $prev_revision = next( $revisions );
 738  
 739          $this_revision_version = _wp_get_post_revision_version( $this_revision );
 740  
 741          // Something terrible happened.
 742          if ( false === $this_revision_version ) {
 743              continue;
 744          }
 745  
 746          // 1 is the latest revision version, so we're already up to date.
 747          // No need to add a copy of the post as latest revision.
 748          if ( 0 < $this_revision_version ) {
 749              $add_last = false;
 750              continue;
 751          }
 752  
 753          // Always update the revision version.
 754          $update = array(
 755              'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
 756          );
 757  
 758          /*
 759           * If this revision is the oldest revision of the post, i.e. no $prev_revision,
 760           * the correct post_author is probably $post->post_author, but that's only a good guess.
 761           * Update the revision version only and Leave the author as-is.
 762           */
 763          if ( $prev_revision ) {
 764              $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
 765  
 766              // If the previous revision is already up to date, it no longer has the information we need :(
 767              if ( $prev_revision_version < 1 ) {
 768                  $update['post_author'] = $prev_revision->post_author;
 769              }
 770          }
 771  
 772          // Upgrade this revision.
 773          $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
 774  
 775          if ( $result ) {
 776              wp_cache_delete( $this_revision->ID, 'posts' );
 777          }
 778      } while ( $prev_revision );
 779  
 780      delete_option( $lock );
 781  
 782      // Add a copy of the post as latest revision.
 783      if ( $add_last ) {
 784          wp_save_post_revision( $post->ID );
 785      }
 786  
 787      return true;
 788  }


Generated: Sun Apr 5 01:00:03 2020 Cross-referenced by PHPXref 0.7.1