acf_revisions

Revisions.

Defined (1)

The class is defined in the following location(s).

/core/controllers/revisions.php  
  1. class acf_revisions 
  2.  
  3. /** 
  4. * __construct 
  5. * A good place to add actions / filters 
  6. * @type function 
  7. * @date 11/08/13 
  8. * @param N/A 
  9. * @return N/A 
  10. */ 
  11.  
  12. function __construct() 
  13. // actions  
  14. add_action('wp_restore_post_revision', array($this, 'wp_restore_post_revision'), 10, 2 ); 
  15.  
  16.  
  17. // filters 
  18. add_filter('_wp_post_revision_fields', array($this, 'wp_post_revision_fields') ); 
  19. add_filter('wp_save_post_revision_check_for_changes', array($this, 'force_save_revision'), 10, 3); 
  20.  
  21.  
  22. /** 
  23. * force_save_revision 
  24. * This filter will return false and force WP to save a revision. This is required due to 
  25. * WP checking only post_title, post_excerpt and post_content values, not custom fields. 
  26. * @type filter 
  27. * @date 19/09/13 
  28. * @param $return (boolean) defaults to true 
  29. * @param $last_revision (object) the last revision that WP will compare against 
  30. * @param $post (object) the $post that WP will compare against 
  31. * @return $return (boolean) 
  32. */ 
  33.  
  34. function force_save_revision( $return, $last_revision, $post ) 
  35. // preview hack 
  36. if( isset($_POST['acf_has_changed']) && $_POST['acf_has_changed'] == '1' ) 
  37. $return = false; 
  38.  
  39.  
  40. // return 
  41. return $return; 
  42.  
  43.  
  44. /** 
  45. * wp_post_revision_fields 
  46. * This filter will add the ACF fields to the returned array 
  47. * Versions 3.5 and 3.6 of WP feature different uses of the revisions filters, so there are 
  48. * some hacks to allow both versions to work correctly 
  49. * @type filter 
  50. * @date 11/08/13 
  51. * @param $post_id (int) 
  52. * @return $post_id (int) 
  53. */ 
  54.  
  55. function wp_post_revision_fields( $return ) { 
  56.  
  57.  
  58. //globals 
  59. global $post, $pagenow; 
  60.  
  61.  
  62. // validate 
  63. $allowed = false; 
  64.  
  65.  
  66. // Normal revisions page 
  67. if( $pagenow == 'revision.php' ) 
  68. $allowed = true; 
  69.  
  70.  
  71. // WP 3.6 AJAX revision 
  72. if( $pagenow == 'admin-ajax.php' && isset($_POST['action']) && $_POST['action'] == 'get-revision-diffs' ) 
  73. $allowed = true; 
  74.  
  75.  
  76. // bail 
  77. if( !$allowed )  
  78. return $return; 
  79.  
  80.  
  81. // vars 
  82. $post_id = 0; 
  83.  
  84.  
  85. // determine $post_id 
  86. if( isset($_POST['post_id']) ) 
  87. $post_id = $_POST['post_id']; 
  88. elseif( isset($post->ID) ) 
  89. $post_id = $post->ID; 
  90. else 
  91. return $return; 
  92.  
  93.  
  94. // get field objects 
  95. $fields = get_field_objects( $post_id, array('format_value' => false ) ); 
  96.  
  97.  
  98. if( $fields ) 
  99. foreach( $fields as $field ) 
  100. // dud field? 
  101. if( !$field || !isset($field['name']) || !$field['name'] ) 
  102. continue; 
  103.  
  104.  
  105. // Add field key / label 
  106. $return[ $field['name'] ] = $field['label']; 
  107.  
  108.  
  109. // load value 
  110. add_filter('_wp_post_revision_field_' . $field['name'], array($this, 'wp_post_revision_field'), 10, 4); 
  111.  
  112.  
  113. // WP 3.5: left vs right 
  114. // Add a value of the revision ID (as there is no way to determine this within the '_wp_post_revision_field_' filter!) 
  115. if( isset($_GET['action'], $_GET['left'], $_GET['right']) && $_GET['action'] == 'diff' ) 
  116. global $left_revision, $right_revision; 
  117.  
  118. $left_revision->$field['name'] = 'revision_id=' . $_GET['left']; 
  119. $right_revision->$field['name'] = 'revision_id=' . $_GET['right']; 
  120.  
  121.  
  122.  
  123. return $return; 
  124.  
  125.  
  126.  
  127. /** 
  128. * wp_post_revision_field 
  129. * This filter will load the value for the given field and return it for rendering 
  130. * @type filter 
  131. * @date 11/08/13 
  132. * @param $value (mixed) should be false as it has not yet been loaded 
  133. * @param $field_name (string) The name of the field 
  134. * @param $post (mixed) Holds the $post object to load from - in WP 3.5, this is not passed! 
  135. * @param $direction (string) to / from - not used 
  136. * @return $value (string) 
  137. */ 
  138.  
  139. function wp_post_revision_field( $value, $field_name, $post = null, $direction = false) 
  140. // vars 
  141. $post_id = 0; 
  142.  
  143.  
  144. // determine $post_id 
  145. if( isset($post->ID) ) 
  146. // WP 3.6 
  147. $post_id = $post->ID; 
  148. elseif( isset($_GET['revision']) ) 
  149. // WP 3.5 
  150. $post_id = (int) $_GET['revision']; 
  151. elseif( strpos($value, 'revision_id=') !== false ) 
  152. // WP 3.5 (left vs right) 
  153. $post_id = (int) str_replace('revision_id=', '', $value); 
  154.  
  155.  
  156. // load field 
  157. $field = get_field_object($field_name, $post_id, array('format_value' => false )); 
  158. $value = $field['value']; 
  159.  
  160.  
  161. // default formatting 
  162. if( is_array($value) ) 
  163. $value = implode(', ', $value); 
  164.  
  165.  
  166. // format 
  167. if( $value ) 
  168. // image? 
  169. if( $field['type'] == 'image' || $field['type'] == 'file' ) 
  170. $url = wp_get_attachment_url($value); 
  171. $value = $value . ' (' . $url . ')'; 
  172.  
  173.  
  174. // return 
  175. return $value; 
  176.  
  177.  
  178. /** 
  179. * wp_restore_post_revision 
  180. * This action will copy and paste the metadata from a revision to the post 
  181. * @type action 
  182. * @date 11/08/13 
  183. * @param $parent_id (int) the destination post 
  184. * @return $revision_id (int) the source post 
  185. */ 
  186.  
  187. function wp_restore_post_revision( $post_id, $revision_id ) { 
  188.  
  189. // global 
  190. global $wpdb; 
  191.  
  192.  
  193. // vars 
  194. $fields = array(); 
  195.  
  196.  
  197. // get field from postmeta 
  198. $rows = $wpdb->get_results( $wpdb->prepare( 
  199. "SELECT * FROM $wpdb->postmeta WHERE post_id=%d",  
  200. $revision_id 
  201. ), ARRAY_A); 
  202.  
  203.  
  204. // populate $fields 
  205. if( $rows ) 
  206. foreach( $rows as $row ) 
  207. // meta_key must start with '_' 
  208. if( substr($row['meta_key'], 0, 1) !== '_' ) 
  209. continue; 
  210.  
  211.  
  212. // meta_value must start with 'field_' 
  213. if( substr($row['meta_value'], 0, 6) !== 'field_' ) 
  214. continue; 
  215.  
  216.  
  217. // this is an ACF field, append to $fields 
  218. $fields[] = substr($row['meta_key'], 1); 
  219.  
  220.  
  221.  
  222. // save data 
  223. if( $rows ) 
  224. foreach( $rows as $row ) 
  225. if( in_array($row['meta_key'], $fields) ) 
  226. update_post_meta( $post_id, $row['meta_key'], $row['meta_value'] ); 
  227.  
  228.  
  229.