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