/wp-includes/revision.php

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