[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Wed Oct 23 08:20:01 2019 Cross-referenced by PHPXref 0.7