/server/wordpress/wp-content/plugins/advanced-custom-fields/includes/revisions.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br · PHP · 457 lines · 150 code · 131 blank · 176 comment · 32 complexity · 3500696bd898e62eaffcb028204a060d MD5 · raw file

  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 ealry 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 ealry if is empty
  185. if ( empty( $value ) ) {
  186. return $value;
  187. }
  188. // value has not yet been 'maybe_unserialize'
  189. $value = maybe_unserialize( $value );
  190. // vars
  191. $post_id = $post->ID;
  192. // load field
  193. $field = acf_maybe_get_field( $field_name, $post_id );
  194. // default formatting
  195. if ( is_array( $value ) ) {
  196. $value = implode( ', ', $value );
  197. } elseif ( is_object( $value ) ) {
  198. $value = serialize( $value );
  199. }
  200. // image
  201. if ( $field['type'] == 'image' || $field['type'] == 'file' ) {
  202. $url = wp_get_attachment_url( $value );
  203. $value = $value . ' (' . $url . ')';
  204. }
  205. // return
  206. return $value;
  207. }
  208. /*
  209. * wp_restore_post_revision
  210. *
  211. * This action will copy and paste the metadata from a revision to the post
  212. *
  213. * @type action
  214. * @date 11/08/13
  215. *
  216. * @param $parent_id (int) the destination post
  217. * @return $revision_id (int) the source post
  218. */
  219. function wp_restore_post_revision( $post_id, $revision_id ) {
  220. // copy postmeta from revision to post (restore from revision)
  221. acf_copy_postmeta( $revision_id, $post_id );
  222. // Make sure the latest revision is also updated to match the new $post data
  223. // get latest revision
  224. $revision = acf_get_post_latest_revision( $post_id );
  225. // save
  226. if ( $revision ) {
  227. // copy postmeta from revision to latest revision (potentialy may be the same, but most likely are different)
  228. acf_copy_postmeta( $revision_id, $revision->ID );
  229. }
  230. }
  231. /*
  232. * acf_validate_post_id
  233. *
  234. * This function will modify the $post_id and allow loading values from a revision
  235. *
  236. * @type function
  237. * @date 6/3/17
  238. * @since 5.5.10
  239. *
  240. * @param $post_id (int)
  241. * @param $_post_id (int)
  242. * @return $post_id (int)
  243. */
  244. function acf_validate_post_id( $post_id, $_post_id ) {
  245. // bail early if no preview in URL
  246. if ( ! isset( $_GET['preview'] ) ) {
  247. return $post_id;
  248. }
  249. // bail early if $post_id is not numeric
  250. if ( ! is_numeric( $post_id ) ) {
  251. return $post_id;
  252. }
  253. // vars
  254. $k = $post_id;
  255. $preview_id = 0;
  256. // check cache
  257. if ( isset( $this->cache[ $k ] ) ) {
  258. return $this->cache[ $k ];
  259. }
  260. // validate
  261. if ( isset( $_GET['preview_id'] ) ) {
  262. $preview_id = (int) $_GET['preview_id'];
  263. } elseif ( isset( $_GET['p'] ) ) {
  264. $preview_id = (int) $_GET['p'];
  265. } elseif ( isset( $_GET['page_id'] ) ) {
  266. $preview_id = (int) $_GET['page_id'];
  267. }
  268. // bail early id $preview_id does not match $post_id
  269. if ( $preview_id != $post_id ) {
  270. return $post_id;
  271. }
  272. // attempt find revision
  273. $revision = acf_get_post_latest_revision( $post_id );
  274. // save
  275. if ( $revision && $revision->post_parent == $post_id ) {
  276. $post_id = (int) $revision->ID;
  277. }
  278. // set cache
  279. $this->cache[ $k ] = $post_id;
  280. // return
  281. return $post_id;
  282. }
  283. }
  284. // initialize
  285. acf()->revisions = new acf_revisions();
  286. endif; // class_exists check
  287. /*
  288. * acf_save_post_revision
  289. *
  290. * This function will copy meta from a post to it's latest revision
  291. *
  292. * @type function
  293. * @date 26/09/2016
  294. * @since 5.4.0
  295. *
  296. * @param $post_id (int)
  297. * @return n/a
  298. */
  299. function acf_save_post_revision( $post_id = 0 ) {
  300. // get latest revision
  301. $revision = acf_get_post_latest_revision( $post_id );
  302. // save
  303. if ( $revision ) {
  304. acf_copy_postmeta( $post_id, $revision->ID );
  305. }
  306. }
  307. /*
  308. * acf_get_post_latest_revision
  309. *
  310. * This function will return the latest revision for a given post
  311. *
  312. * @type function
  313. * @date 25/06/2016
  314. * @since 5.3.8
  315. *
  316. * @param $post_id (int)
  317. * @return $post_id (int)
  318. */
  319. function acf_get_post_latest_revision( $post_id ) {
  320. // vars
  321. $revisions = wp_get_post_revisions( $post_id );
  322. // shift off and return first revision (will return null if no revisions)
  323. $revision = array_shift( $revisions );
  324. // return
  325. return $revision;
  326. }