revision.php 24 KB

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