Нет описания

revision.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 array 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 array $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 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 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. Is the revision an autosave?
  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 The post ID or 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 sanitation 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_id 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_id, $fields = null ) {
  345. $revision = wp_get_post_revision( $revision_id, 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_id Revision ID or revision object.
  386. * @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
  387. */
  388. function wp_delete_post_revision( $revision_id ) {
  389. $revision = wp_get_post_revision( $revision_id );
  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_id 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 array An array of revisions, or an empty array if none.
  417. */
  418. function wp_get_post_revisions( $post_id = 0, $args = null ) {
  419. $post = get_post( $post_id );
  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. * Determine if revisions are enabled for a given post.
  448. *
  449. * @since 3.6.0
  450. *
  451. * @param WP_Post $post The post object.
  452. * @return bool True if number of revisions to keep isn't zero, false otherwise.
  453. */
  454. function wp_revisions_enabled( $post ) {
  455. return wp_revisions_to_keep( $post ) !== 0;
  456. }
  457. /**
  458. * Determine how many revisions to retain for a given post.
  459. *
  460. * By default, an infinite number of revisions are kept.
  461. *
  462. * The constant WP_POST_REVISIONS can be set in wp-config to specify the limit
  463. * of revisions to keep.
  464. *
  465. * @since 3.6.0
  466. *
  467. * @param WP_Post $post The post object.
  468. * @return int The number of revisions to keep.
  469. */
  470. function wp_revisions_to_keep( $post ) {
  471. $num = WP_POST_REVISIONS;
  472. if ( true === $num ) {
  473. $num = -1;
  474. } else {
  475. $num = (int) $num;
  476. }
  477. if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
  478. $num = 0;
  479. }
  480. /**
  481. * Filters the number of revisions to save for the given post.
  482. *
  483. * Overrides the value of WP_POST_REVISIONS.
  484. *
  485. * @since 3.6.0
  486. *
  487. * @param int $num Number of revisions to store.
  488. * @param WP_Post $post Post object.
  489. */
  490. $num = apply_filters( 'wp_revisions_to_keep', $num, $post );
  491. /**
  492. * Filters the number of revisions to save for the given post by its post type.
  493. *
  494. * Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter.
  495. *
  496. * The dynamic portion of the hook name, `$post->post_type`, refers to
  497. * the post type slug.
  498. *
  499. * @since 5.8.0
  500. *
  501. * @param int $num Number of revisions to store.
  502. * @param WP_Post $post Post object.
  503. */
  504. $num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post );
  505. return (int) $num;
  506. }
  507. /**
  508. * Sets up the post object for preview based on the post autosave.
  509. *
  510. * @since 2.7.0
  511. * @access private
  512. *
  513. * @param WP_Post $post
  514. * @return WP_Post|false
  515. */
  516. function _set_preview( $post ) {
  517. if ( ! is_object( $post ) ) {
  518. return $post;
  519. }
  520. $preview = wp_get_post_autosave( $post->ID );
  521. if ( ! is_object( $preview ) ) {
  522. return $post;
  523. }
  524. $preview = sanitize_post( $preview );
  525. $post->post_content = $preview->post_content;
  526. $post->post_title = $preview->post_title;
  527. $post->post_excerpt = $preview->post_excerpt;
  528. add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
  529. add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
  530. return $post;
  531. }
  532. /**
  533. * Filters the latest content for preview from the post autosave.
  534. *
  535. * @since 2.7.0
  536. * @access private
  537. */
  538. function _show_post_preview() {
  539. if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) {
  540. $id = (int) $_GET['preview_id'];
  541. if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) {
  542. wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 );
  543. }
  544. add_filter( 'the_preview', '_set_preview' );
  545. }
  546. }
  547. /**
  548. * Filters terms lookup to set the post format.
  549. *
  550. * @since 3.6.0
  551. * @access private
  552. *
  553. * @param array $terms
  554. * @param int $post_id
  555. * @param string $taxonomy
  556. * @return array
  557. */
  558. function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
  559. $post = get_post();
  560. if ( ! $post ) {
  561. return $terms;
  562. }
  563. if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id
  564. || 'post_format' !== $taxonomy || 'revision' === $post->post_type
  565. ) {
  566. return $terms;
  567. }
  568. if ( 'standard' === $_REQUEST['post_format'] ) {
  569. $terms = array();
  570. } else {
  571. $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );
  572. if ( $term ) {
  573. $terms = array( $term ); // Can only have one post format.
  574. }
  575. }
  576. return $terms;
  577. }
  578. /**
  579. * Filters post thumbnail lookup to set the post thumbnail.
  580. *
  581. * @since 4.6.0
  582. * @access private
  583. *
  584. * @param null|array|string $value The value to return - a single metadata value, or an array of values.
  585. * @param int $post_id Post ID.
  586. * @param string $meta_key Meta key.
  587. * @return null|array The default return value or the post thumbnail meta array.
  588. */
  589. function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
  590. $post = get_post();
  591. if ( ! $post ) {
  592. return $value;
  593. }
  594. if ( empty( $_REQUEST['_thumbnail_id'] ) ||
  595. empty( $_REQUEST['preview_id'] ) ||
  596. $post->ID != $post_id ||
  597. '_thumbnail_id' !== $meta_key ||
  598. 'revision' === $post->post_type ||
  599. $post_id != $_REQUEST['preview_id'] ) {
  600. return $value;
  601. }
  602. $thumbnail_id = (int) $_REQUEST['_thumbnail_id'];
  603. if ( $thumbnail_id <= 0 ) {
  604. return '';
  605. }
  606. return (string) $thumbnail_id;
  607. }
  608. /**
  609. * Gets the post revision version.
  610. *
  611. * @since 3.6.0
  612. * @access private
  613. *
  614. * @param WP_Post $revision
  615. * @return int|false
  616. */
  617. function _wp_get_post_revision_version( $revision ) {
  618. if ( is_object( $revision ) ) {
  619. $revision = get_object_vars( $revision );
  620. } elseif ( ! is_array( $revision ) ) {
  621. return false;
  622. }
  623. if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) {
  624. return (int) $matches[1];
  625. }
  626. return 0;
  627. }
  628. /**
  629. * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
  630. *
  631. * @since 3.6.0
  632. * @access private
  633. *
  634. * @global wpdb $wpdb WordPress database abstraction object.
  635. *
  636. * @param WP_Post $post Post object
  637. * @param array $revisions Current revisions of the post
  638. * @return bool true if the revisions were upgraded, false if problems
  639. */
  640. function _wp_upgrade_revisions_of_post( $post, $revisions ) {
  641. global $wpdb;
  642. // Add post option exclusively.
  643. $lock = "revision-upgrade-{$post->ID}";
  644. $now = time();
  645. $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
  646. if ( ! $result ) {
  647. // If we couldn't get a lock, see how old the previous lock is.
  648. $locked = get_option( $lock );
  649. if ( ! $locked ) {
  650. // Can't write to the lock, and can't read the lock.
  651. // Something broken has happened.
  652. return false;
  653. }
  654. if ( $locked > $now - 3600 ) {
  655. // Lock is not too old: some other process may be upgrading this post. Bail.
  656. return false;
  657. }
  658. // Lock is too old - update it (below) and continue.
  659. }
  660. // If we could get a lock, re-"add" the option to fire all the correct filters.
  661. update_option( $lock, $now );
  662. reset( $revisions );
  663. $add_last = true;
  664. do {
  665. $this_revision = current( $revisions );
  666. $prev_revision = next( $revisions );
  667. $this_revision_version = _wp_get_post_revision_version( $this_revision );
  668. // Something terrible happened.
  669. if ( false === $this_revision_version ) {
  670. continue;
  671. }
  672. // 1 is the latest revision version, so we're already up to date.
  673. // No need to add a copy of the post as latest revision.
  674. if ( 0 < $this_revision_version ) {
  675. $add_last = false;
  676. continue;
  677. }
  678. // Always update the revision version.
  679. $update = array(
  680. 'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
  681. );
  682. /*
  683. * If this revision is the oldest revision of the post, i.e. no $prev_revision,
  684. * the correct post_author is probably $post->post_author, but that's only a good guess.
  685. * Update the revision version only and Leave the author as-is.
  686. */
  687. if ( $prev_revision ) {
  688. $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
  689. // If the previous revision is already up to date, it no longer has the information we need :(
  690. if ( $prev_revision_version < 1 ) {
  691. $update['post_author'] = $prev_revision->post_author;
  692. }
  693. }
  694. // Upgrade this revision.
  695. $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
  696. if ( $result ) {
  697. wp_cache_delete( $this_revision->ID, 'posts' );
  698. }
  699. } while ( $prev_revision );
  700. delete_option( $lock );
  701. // Add a copy of the post as latest revision.
  702. if ( $add_last ) {
  703. wp_save_post_revision( $post->ID );
  704. }
  705. return true;
  706. }