7 */ |
7 */ |
8 |
8 |
9 /** |
9 /** |
10 * Determines which fields of posts are to be saved in revisions. |
10 * Determines which fields of posts are to be saved in revisions. |
11 * |
11 * |
12 * Does two things. If passed a post *array*, it will return a post array ready |
12 * @since 2.6.0 |
13 * to be inserted into the posts table as a post revision. Otherwise, returns |
13 * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter. |
14 * an array whose keys are the post fields to be saved for post revisions. |
14 * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`. |
15 * |
15 * @access private |
16 * @since 2.6.0 |
16 * |
17 * @access private |
17 * @staticvar array $fields |
18 * |
18 * |
19 * @param array $post Optional a post array to be processed for insertion as a post revision. |
19 * @param array|WP_Post $post Optional. A post array or a WP_Post object being processed |
20 * @param bool $autosave optional Is the revision an autosave? |
20 * for insertion as a post revision. Default empty array. |
21 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned. |
21 * @param bool $deprecated Not used. |
22 */ |
22 * @return array Array of fields that can be versioned. |
23 function _wp_post_revision_fields( $post = null, $autosave = false ) { |
23 */ |
24 static $fields = false; |
24 function _wp_post_revision_fields( $post = array(), $deprecated = false ) { |
25 |
25 static $fields = null; |
26 if ( !$fields ) { |
26 |
|
27 if ( ! is_array( $post ) ) { |
|
28 $post = get_post( $post, ARRAY_A ); |
|
29 } |
|
30 |
|
31 if ( is_null( $fields ) ) { |
27 // Allow these to be versioned |
32 // Allow these to be versioned |
28 $fields = array( |
33 $fields = array( |
29 'post_title' => __( 'Title' ), |
34 'post_title' => __( 'Title' ), |
30 'post_content' => __( 'Content' ), |
35 'post_content' => __( 'Content' ), |
31 'post_excerpt' => __( 'Excerpt' ), |
36 'post_excerpt' => __( 'Excerpt' ), |
32 ); |
37 ); |
33 |
38 } |
34 /** |
39 |
35 * Filter the list of fields saved in post revisions. |
40 /** |
36 * |
41 * Filters the list of fields saved in post revisions. |
37 * Included by default: 'post_title', 'post_content' and 'post_excerpt'. |
42 * |
38 * |
43 * Included by default: 'post_title', 'post_content' and 'post_excerpt'. |
39 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date', |
44 * |
40 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count', |
45 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date', |
41 * and 'post_author'. |
46 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count', |
42 * |
47 * and 'post_author'. |
43 * @since 2.6.0 |
48 * |
44 * |
49 * @since 2.6.0 |
45 * @param array $fields List of fields to revision. Contains 'post_title', |
50 * @since 4.5.0 The `$post` parameter was added. |
46 * 'post_content', and 'post_excerpt' by default. |
51 * |
47 */ |
52 * @param array $fields List of fields to revision. Contains 'post_title', |
48 $fields = apply_filters( '_wp_post_revision_fields', $fields ); |
53 * 'post_content', and 'post_excerpt' by default. |
49 |
54 * @param array $post A post array being processed for insertion as a post revision. |
50 // WP uses these internally either in versioning or elsewhere - they cannot be versioned |
55 */ |
51 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) |
56 $fields = apply_filters( '_wp_post_revision_fields', $fields, $post ); |
52 unset( $fields[$protect] ); |
57 |
53 } |
58 // WP uses these internally either in versioning or elsewhere - they cannot be versioned |
54 |
59 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) { |
55 if ( !is_array($post) ) |
60 unset( $fields[ $protect ] ); |
56 return $fields; |
61 } |
57 |
62 |
58 $return = array(); |
63 |
59 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) |
64 return $fields; |
60 $return[$field] = $post[$field]; |
65 } |
61 |
66 |
62 $return['post_parent'] = $post['ID']; |
67 /** |
63 $return['post_status'] = 'inherit'; |
68 * Returns a post array ready to be inserted into the posts table as a post revision. |
64 $return['post_type'] = 'revision'; |
69 * |
65 $return['post_name'] = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version |
70 * @since 4.5.0 |
66 $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : ''; |
71 * @access private |
67 $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : ''; |
72 * |
68 |
73 * @param array|WP_Post $post Optional. A post array or a WP_Post object to be processed |
69 return $return; |
74 * for insertion as a post revision. Default empty array. |
|
75 * @param bool $autosave Optional. Is the revision an autosave? Default false. |
|
76 * @return array Post array ready to be inserted as a post revision. |
|
77 */ |
|
78 function _wp_post_revision_data( $post = array(), $autosave = false ) { |
|
79 if ( ! is_array( $post ) ) { |
|
80 $post = get_post( $post, ARRAY_A ); |
|
81 } |
|
82 |
|
83 $fields = _wp_post_revision_fields( $post ); |
|
84 |
|
85 $revision_data = array(); |
|
86 |
|
87 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) { |
|
88 $revision_data[ $field ] = $post[ $field ]; |
|
89 } |
|
90 |
|
91 $revision_data['post_parent'] = $post['ID']; |
|
92 $revision_data['post_status'] = 'inherit'; |
|
93 $revision_data['post_type'] = 'revision'; |
|
94 $revision_data['post_name'] = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version |
|
95 $revision_data['post_date'] = isset( $post['post_modified'] ) ? $post['post_modified'] : ''; |
|
96 $revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : ''; |
|
97 |
|
98 return $revision_data; |
70 } |
99 } |
71 |
100 |
72 /** |
101 /** |
73 * Creates a revision for the current version of a post. |
102 * Creates a revision for the current version of a post. |
74 * |
103 * |
75 * Typically used immediately after a post update, as every update is a revision, |
104 * Typically used immediately after a post update, as every update is a revision, |
76 * and the most recent revision always matches the current post. |
105 * and the most recent revision always matches the current post. |
77 * |
106 * |
78 * @since 2.6.0 |
107 * @since 2.6.0 |
79 * |
108 * |
80 * @param int $post_id The ID of the post to save as a revision. |
109 * @param int $post_id The ID of the post to save as a revision. |
81 * @return null|int Null or 0 if error, new revision ID, if success. |
110 * @return int|WP_Error|void Void or 0 if error, new revision ID, if success. |
82 */ |
111 */ |
83 function wp_save_post_revision( $post_id ) { |
112 function wp_save_post_revision( $post_id ) { |
84 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
113 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
85 return; |
114 return; |
86 |
115 |
107 break; |
136 break; |
108 } |
137 } |
109 } |
138 } |
110 |
139 |
111 /** |
140 /** |
112 * Filter whether the post has changed since the last revision. |
141 * Filters whether the post has changed since the last revision. |
113 * |
142 * |
114 * By default a revision is saved only if one of the revisioned fields has changed. |
143 * By default a revision is saved only if one of the revisioned fields has changed. |
115 * This filter can override that so a revision is saved even if nothing has changed. |
144 * This filter can override that so a revision is saved even if nothing has changed. |
116 * |
145 * |
117 * @since 3.6.0 |
146 * @since 3.6.0 |
118 * |
147 * |
119 * @param bool $check_for_changes Whether to check for changes before saving a new revision. |
148 * @param bool $check_for_changes Whether to check for changes before saving a new revision. |
120 * Default true. |
149 * Default true. |
121 * @param WP_Post $last_revision The the last revision post object. |
150 * @param WP_Post $last_revision The last revision post object. |
122 * @param WP_Post $post The post object. |
151 * @param WP_Post $post The post object. |
123 * |
152 * |
124 */ |
153 */ |
125 if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) { |
154 if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) { |
126 $post_has_changed = false; |
155 $post_has_changed = false; |
127 |
156 |
128 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { |
157 foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { |
129 if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) { |
158 if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) { |
130 $post_has_changed = true; |
159 $post_has_changed = true; |
131 break; |
160 break; |
132 } |
161 } |
133 } |
162 } |
134 |
163 |
135 /** |
164 /** |
136 * Filter whether a post has changed. |
165 * Filters whether a post has changed. |
137 * |
166 * |
138 * By default a revision is saved only if one of the revisioned fields has changed. |
167 * By default a revision is saved only if one of the revisioned fields has changed. |
139 * This filter allows for additional checks to determine if there were changes. |
168 * This filter allows for additional checks to determine if there were changes. |
140 * |
169 * |
141 * @since 4.1.0 |
170 * @since 4.1.0 |
247 * Inserts post data into the posts table as a post revision. |
276 * Inserts post data into the posts table as a post revision. |
248 * |
277 * |
249 * @since 2.6.0 |
278 * @since 2.6.0 |
250 * @access private |
279 * @access private |
251 * |
280 * |
252 * @param int|object|array $post Post ID, post object OR post array. |
281 * @param int|WP_Post|array|null $post Post ID, post object OR post array. |
253 * @param bool $autosave Optional. Is the revision an autosave? |
282 * @param bool $autosave Optional. Is the revision an autosave? |
254 * @return mixed WP_Error or 0 if error, new revision ID if success. |
283 * @return int|WP_Error WP_Error or 0 if error, new revision ID if success. |
255 */ |
284 */ |
256 function _wp_put_post_revision( $post = null, $autosave = false ) { |
285 function _wp_put_post_revision( $post = null, $autosave = false ) { |
257 if ( is_object($post) ) |
286 if ( is_object($post) ) |
258 $post = get_object_vars( $post ); |
287 $post = get_object_vars( $post ); |
259 elseif ( !is_array($post) ) |
288 elseif ( !is_array($post) ) |
260 $post = get_post($post, ARRAY_A); |
289 $post = get_post($post, ARRAY_A); |
261 |
290 |
262 if ( ! $post || empty($post['ID']) ) |
291 if ( ! $post || empty($post['ID']) ) |
263 return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) ); |
292 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
264 |
293 |
265 if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) |
294 if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) |
266 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
295 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
267 |
296 |
268 $post = _wp_post_revision_fields( $post, $autosave ); |
297 $post = _wp_post_revision_data( $post, $autosave ); |
269 $post = wp_slash($post); //since data is from db |
298 $post = wp_slash($post); //since data is from db |
270 |
299 |
271 $revision_id = wp_insert_post( $post ); |
300 $revision_id = wp_insert_post( $post ); |
272 if ( is_wp_error($revision_id) ) |
301 if ( is_wp_error($revision_id) ) |
273 return $revision_id; |
302 return $revision_id; |
320 * |
350 * |
321 * Can restore a past revision using all fields of the post revision, or only selected fields. |
351 * Can restore a past revision using all fields of the post revision, or only selected fields. |
322 * |
352 * |
323 * @since 2.6.0 |
353 * @since 2.6.0 |
324 * |
354 * |
325 * @param int|object $revision_id Revision ID or revision object. |
355 * @param int|WP_Post $revision_id Revision ID or revision object. |
326 * @param array $fields Optional. What fields to restore from. Defaults to all. |
356 * @param array $fields Optional. What fields to restore from. Defaults to all. |
327 * @return mixed Null if error, false if no fields to restore, (int) post ID if success. |
357 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. |
328 */ |
358 */ |
329 function wp_restore_post_revision( $revision_id, $fields = null ) { |
359 function wp_restore_post_revision( $revision_id, $fields = null ) { |
330 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) |
360 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) |
331 return $revision; |
361 return $revision; |
332 |
362 |
333 if ( !is_array( $fields ) ) |
363 if ( !is_array( $fields ) ) |
334 $fields = array_keys( _wp_post_revision_fields() ); |
364 $fields = array_keys( _wp_post_revision_fields( $revision ) ); |
335 |
365 |
336 $update = array(); |
366 $update = array(); |
337 foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
367 foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
338 $update[$field] = $revision[$field]; |
368 $update[$field] = $revision[$field]; |
339 } |
369 } |
340 |
370 |
341 if ( !$update ) |
371 if ( !$update ) |
342 return false; |
372 return false; |
547 |
578 |
548 return $terms; |
579 return $terms; |
549 } |
580 } |
550 |
581 |
551 /** |
582 /** |
|
583 * Filters post thumbnail lookup to set the post thumbnail. |
|
584 * |
|
585 * @since 4.6.0 |
|
586 * @access private |
|
587 * |
|
588 * @param null|array|string $value The value to return - a single metadata value, or an array of values. |
|
589 * @param int $post_id Post ID. |
|
590 * @param string $meta_key Meta key. |
|
591 * @return null|array The default return value or the post thumbnail meta array. |
|
592 */ |
|
593 function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) { |
|
594 if ( ! $post = get_post() ) { |
|
595 return $value; |
|
596 } |
|
597 |
|
598 if ( empty( $_REQUEST['_thumbnail_id'] ) || |
|
599 empty( $_REQUEST['preview_id'] ) || |
|
600 $post->ID != $post_id || |
|
601 '_thumbnail_id' != $meta_key || |
|
602 'revision' == $post->post_type || |
|
603 $post_id != $_REQUEST['preview_id'] |
|
604 ) { |
|
605 return $value; |
|
606 } |
|
607 |
|
608 $thumbnail_id = intval( $_REQUEST['_thumbnail_id'] ); |
|
609 if ( $thumbnail_id <= 0 ) { |
|
610 return ''; |
|
611 } |
|
612 |
|
613 return strval( $thumbnail_id ); |
|
614 } |
|
615 |
|
616 /** |
552 * Gets the post revision version. |
617 * Gets the post revision version. |
553 * |
618 * |
554 * @since 3.6.0 |
619 * @since 3.6.0 |
555 * @access private |
620 * @access private |
556 */ |
621 * |
|
622 * @param WP_Post $revision |
|
623 * @return int|false |
|
624 */ |
557 function _wp_get_post_revision_version( $revision ) { |
625 function _wp_get_post_revision_version( $revision ) { |
558 if ( is_object( $revision ) ) |
626 if ( is_object( $revision ) ) |
559 $revision = get_object_vars( $revision ); |
627 $revision = get_object_vars( $revision ); |
560 elseif ( !is_array( $revision ) ) |
628 elseif ( !is_array( $revision ) ) |
561 return false; |
629 return false; |