revision.php 21 KB

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