_wp_upgrade_revisions_of_post

Upgrade the revisions author, add the current post as a revision and set the revisions version to 1.

Description

_wp_upgrade_revisions_of_post( (WP_Post) $post, (array) $revisions ); 

Parameters (2)

0. $post (WP_Post)
Post object
1. $revisions (array)
Current revisions of the post

Usage

  1. if ( !function_exists( '_wp_upgrade_revisions_of_post' ) ) { 
  2. require_once ABSPATH . WPINC . '/revision.php'; 
  3.  
  4. // Post object 
  5. $post = null; 
  6.  
  7. // Current revisions of the post 
  8. $revisions = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _wp_upgrade_revisions_of_post($post, $revisions); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/revision.php  
  1. function _wp_upgrade_revisions_of_post( $post, $revisions ) { 
  2. global $wpdb; 
  3.  
  4. // Add post option exclusively 
  5. $lock = "revision-upgrade-{$post->ID}"; 
  6. $now = time(); 
  7. $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) ); 
  8. if ( ! $result ) { 
  9. // If we couldn't get a lock, see how old the previous lock is 
  10. $locked = get_option( $lock ); 
  11. if ( ! $locked ) { 
  12. // Can't write to the lock, and can't read the lock. 
  13. // Something broken has happened 
  14. return false; 
  15.  
  16. if ( $locked > $now - 3600 ) { 
  17. // Lock is not too old: some other process may be upgrading this post. Bail. 
  18. return false; 
  19.  
  20. // Lock is too old - update it (below) and continue 
  21.  
  22. // If we could get a lock, re-"add" the option to fire all the correct filters. 
  23. update_option( $lock, $now ); 
  24.  
  25. reset( $revisions ); 
  26. $add_last = true; 
  27.  
  28. do { 
  29. $this_revision = current( $revisions ); 
  30. $prev_revision = next( $revisions ); 
  31.  
  32. $this_revision_version = _wp_get_post_revision_version( $this_revision ); 
  33.  
  34. // Something terrible happened 
  35. if ( false === $this_revision_version ) 
  36. continue; 
  37.  
  38. // 1 is the latest revision version, so we're already up to date. 
  39. // No need to add a copy of the post as latest revision. 
  40. if ( 0 < $this_revision_version ) { 
  41. $add_last = false; 
  42. continue; 
  43.  
  44. // Always update the revision version 
  45. $update = array( 
  46. 'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),  
  47. ); 
  48.  
  49. // If this revision is the oldest revision of the post, i.e. no $prev_revision,  
  50. // the correct post_author is probably $post->post_author, but that's only a good guess. 
  51. // Update the revision version only and Leave the author as-is. 
  52. if ( $prev_revision ) { 
  53. $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); 
  54.  
  55. // If the previous revision is already up to date, it no longer has the information we need :( 
  56. if ( $prev_revision_version < 1 ) 
  57. $update['post_author'] = $prev_revision->post_author; 
  58.  
  59. // Upgrade this revision 
  60. $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); 
  61.  
  62. if ( $result ) 
  63. wp_cache_delete( $this_revision->ID, 'posts' ); 
  64.  
  65. } while ( $prev_revision ); 
  66.  
  67. delete_option( $lock ); 
  68.  
  69. // Add a copy of the post as latest revision. 
  70. if ( $add_last ) 
  71. wp_save_post_revision( $post->ID ); 
  72.  
  73. return true;