revisions.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'acf_revisions' ) ) :
  6. class acf_revisions {
  7. // vars
  8. var $cache = array();
  9. /*
  10. * __construct
  11. *
  12. * A good place to add actions / filters
  13. *
  14. * @type function
  15. * @date 11/08/13
  16. *
  17. * @param N/A
  18. * @return N/A
  19. */
  20. function __construct() {
  21. // actions
  22. add_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 );
  23. // filters
  24. add_filter( 'wp_save_post_revision_check_for_changes', array( $this, 'wp_save_post_revision_check_for_changes' ), 10, 3 );
  25. add_filter( '_wp_post_revision_fields', array( $this, 'wp_preview_post_fields' ), 10, 2 );
  26. add_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ), 10, 2 );
  27. add_filter( 'acf/validate_post_id', array( $this, 'acf_validate_post_id' ), 10, 2 );
  28. }
  29. /*
  30. * wp_preview_post_fields
  31. *
  32. * This function is used to trick WP into thinking that one of the $post's fields has changed and
  33. * will allow an autosave to be updated.
  34. * Fixes an odd bug causing the preview page to render the non autosave post data on every odd attempt
  35. *
  36. * @type function
  37. * @date 21/10/2014
  38. * @since 5.1.0
  39. *
  40. * @param $fields (array)
  41. * @return $fields
  42. */
  43. function wp_preview_post_fields( $fields ) {
  44. // bail early if not previewing a post
  45. if ( acf_maybe_get_POST( 'wp-preview' ) !== 'dopreview' ) {
  46. return $fields;
  47. }
  48. // add to fields if ACF has changed
  49. if ( acf_maybe_get_POST( '_acf_changed' ) ) {
  50. $fields['_acf_changed'] = 'different than 1';
  51. }
  52. // return
  53. return $fields;
  54. }
  55. /*
  56. * wp_save_post_revision_check_for_changes
  57. *
  58. * This filter will return false and force WP to save a revision. This is required due to
  59. * WP checking only post_title, post_excerpt and post_content values, not custom fields.
  60. *
  61. * @type filter
  62. * @date 19/09/13
  63. *
  64. * @param $return (boolean) defaults to true
  65. * @param $last_revision (object) the last revision that WP will compare against
  66. * @param $post (object) the $post that WP will compare against
  67. * @return $return (boolean)
  68. */
  69. function wp_save_post_revision_check_for_changes( $return, $last_revision, $post ) {
  70. // if acf has changed, return false and prevent WP from performing 'compare' logic
  71. if ( acf_maybe_get_POST( '_acf_changed' ) ) {
  72. return false;
  73. }
  74. // return
  75. return $return;
  76. }
  77. /*
  78. * wp_post_revision_fields
  79. *
  80. * This filter will add the ACF fields to the returned array
  81. * Versions 3.5 and 3.6 of WP feature different uses of the revisions filters, so there are
  82. * some hacks to allow both versions to work correctly
  83. *
  84. * @type filter
  85. * @date 11/08/13
  86. *
  87. * @param $post_id (int)
  88. * @return $post_id (int)
  89. */
  90. function wp_post_revision_fields( $fields, $post = null ) {
  91. // validate page
  92. if ( acf_is_screen( 'revision' ) || acf_is_ajax( 'get-revision-diffs' ) ) {
  93. // bail early if is restoring
  94. if ( acf_maybe_get_GET( 'action' ) === 'restore' ) {
  95. return $fields;
  96. }
  97. // allow
  98. } else {
  99. // bail early (most likely saving a post)
  100. return $fields;
  101. }
  102. // vars
  103. $append = array();
  104. $order = array();
  105. $post_id = acf_maybe_get( $post, 'ID' );
  106. // compatibility with WP < 4.5 (test)
  107. if ( ! $post_id ) {
  108. global $post;
  109. $post_id = $post->ID;
  110. }
  111. // get all postmeta
  112. $meta = get_post_meta( $post_id );
  113. // bail early if no meta
  114. if ( ! $meta ) {
  115. return $fields;
  116. }
  117. // loop
  118. foreach ( $meta as $name => $value ) {
  119. // attempt to find key value
  120. $key = acf_maybe_get( $meta, '_' . $name );
  121. // bail early if no key
  122. if ( ! $key ) {
  123. continue;
  124. }
  125. // update vars
  126. $value = $value[0];
  127. $key = $key[0];
  128. // Load field.
  129. $field = acf_get_field( $key );
  130. if ( ! $field ) {
  131. continue;
  132. }
  133. // get field
  134. $field_title = $field['label'] . ' (' . $name . ')';
  135. $field_order = $field['menu_order'];
  136. $ancestors = acf_get_field_ancestors( $field );
  137. // ancestors
  138. if ( ! empty( $ancestors ) ) {
  139. // vars
  140. $count = count( $ancestors );
  141. $oldest = acf_get_field( $ancestors[ $count - 1 ] );
  142. // update vars
  143. $field_title = str_repeat( '- ', $count ) . $field_title;
  144. $field_order = $oldest['menu_order'] . '.1';
  145. }
  146. // append
  147. $append[ $name ] = $field_title;
  148. $order[ $name ] = $field_order;
  149. // hook into specific revision field filter and return local value
  150. add_filter( "_wp_post_revision_field_{$name}", array( $this, 'wp_post_revision_field' ), 10, 4 );
  151. }
  152. // append
  153. if ( ! empty( $append ) ) {
  154. // vars
  155. $prefix = '_';
  156. // add prefix
  157. $append = acf_add_array_key_prefix( $append, $prefix );
  158. $order = acf_add_array_key_prefix( $order, $prefix );
  159. // sort by name (orders sub field values correctly)
  160. array_multisort( $order, $append );
  161. // remove prefix
  162. $append = acf_remove_array_key_prefix( $append, $prefix );
  163. // append
  164. $fields = $fields + $append;
  165. }
  166. // return
  167. return $fields;
  168. }
  169. /*
  170. * wp_post_revision_field
  171. *
  172. * This filter will load the value for the given field and return it for rendering
  173. *
  174. * @type filter
  175. * @date 11/08/13
  176. *
  177. * @param $value (mixed) should be false as it has not yet been loaded
  178. * @param $field_name (string) The name of the field
  179. * @param $post (mixed) Holds the $post object to load from - in WP 3.5, this is not passed!
  180. * @param $direction (string) to / from - not used
  181. * @return $value (string)
  182. */
  183. function wp_post_revision_field( $value, $field_name, $post = null, $direction = false ) {
  184. // bail early if is empty.
  185. if ( empty( $value ) ) {
  186. return $value;
  187. }
  188. $value = maybe_unserialize( $value );
  189. $post_id = $post->ID;
  190. // load field.
  191. $field = acf_maybe_get_field( $field_name, $post_id );
  192. // default formatting.
  193. if ( is_array( $value ) ) {
  194. $value = implode( ', ', $value );
  195. } elseif ( is_object( $value ) ) {
  196. $value = serialize( $value );
  197. }
  198. // image.
  199. if ( is_array( $field ) && isset( $field['type'] ) && ( $field['type'] === 'image' || $field['type'] === 'file' ) ) {
  200. $url = wp_get_attachment_url( $value );
  201. $value = $value . ' (' . $url . ')';
  202. }
  203. return $value;
  204. }
  205. /*
  206. * wp_restore_post_revision
  207. *
  208. * This action will copy and paste the metadata from a revision to the post
  209. *
  210. * @type action
  211. * @date 11/08/13
  212. *
  213. * @param $parent_id (int) the destination post
  214. * @return $revision_id (int) the source post
  215. */
  216. function wp_restore_post_revision( $post_id, $revision_id ) {
  217. // copy postmeta from revision to post (restore from revision)
  218. acf_copy_postmeta( $revision_id, $post_id );
  219. // Make sure the latest revision is also updated to match the new $post data
  220. // get latest revision
  221. $revision = acf_get_post_latest_revision( $post_id );
  222. // save
  223. if ( $revision ) {
  224. // copy postmeta from revision to latest revision (potentialy may be the same, but most likely are different)
  225. acf_copy_postmeta( $revision_id, $revision->ID );
  226. }
  227. }
  228. /*
  229. * acf_validate_post_id
  230. *
  231. * This function will modify the $post_id and allow loading values from a revision
  232. *
  233. * @type function
  234. * @date 6/3/17
  235. * @since 5.5.10
  236. *
  237. * @param $post_id (int)
  238. * @param $_post_id (int)
  239. * @return $post_id (int)
  240. */
  241. function acf_validate_post_id( $post_id, $_post_id ) {
  242. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  243. // bail early if no preview in URL
  244. if ( ! isset( $_GET['preview'] ) ) {
  245. return $post_id;
  246. }
  247. // bail early if $post_id is not numeric
  248. if ( ! is_numeric( $post_id ) ) {
  249. return $post_id;
  250. }
  251. // vars
  252. $k = $post_id;
  253. $preview_id = 0;
  254. // check cache
  255. if ( isset( $this->cache[ $k ] ) ) {
  256. return $this->cache[ $k ];
  257. }
  258. // validate
  259. if ( isset( $_GET['preview_id'] ) ) {
  260. $preview_id = (int) $_GET['preview_id'];
  261. } elseif ( isset( $_GET['p'] ) ) {
  262. $preview_id = (int) $_GET['p'];
  263. } elseif ( isset( $_GET['page_id'] ) ) {
  264. $preview_id = (int) $_GET['page_id'];
  265. }
  266. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  267. // bail early id $preview_id does not match $post_id
  268. if ( $preview_id != $post_id ) {
  269. return $post_id;
  270. }
  271. // attempt find revision
  272. $revision = acf_get_post_latest_revision( $post_id );
  273. // save
  274. if ( $revision && $revision->post_parent == $post_id ) {
  275. $post_id = (int) $revision->ID;
  276. }
  277. // set cache
  278. $this->cache[ $k ] = $post_id;
  279. // return
  280. return $post_id;
  281. }
  282. }
  283. // initialize
  284. acf()->revisions = new acf_revisions();
  285. endif; // class_exists check
  286. /*
  287. * acf_save_post_revision
  288. *
  289. * This function will copy meta from a post to it's latest revision
  290. *
  291. * @type function
  292. * @date 26/09/2016
  293. * @since 5.4.0
  294. *
  295. * @param $post_id (int)
  296. * @return n/a
  297. */
  298. function acf_save_post_revision( $post_id = 0 ) {
  299. // get latest revision
  300. $revision = acf_get_post_latest_revision( $post_id );
  301. // save
  302. if ( $revision ) {
  303. acf_copy_postmeta( $post_id, $revision->ID );
  304. }
  305. }
  306. /*
  307. * acf_get_post_latest_revision
  308. *
  309. * This function will return the latest revision for a given post
  310. *
  311. * @type function
  312. * @date 25/06/2016
  313. * @since 5.3.8
  314. *
  315. * @param $post_id (int)
  316. * @return $post_id (int)
  317. */
  318. function acf_get_post_latest_revision( $post_id ) {
  319. // vars
  320. $revisions = wp_get_post_revisions( $post_id );
  321. // shift off and return first revision (will return null if no revisions)
  322. $revision = array_shift( $revisions );
  323. // return
  324. return $revision;
  325. }