author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Post revision functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Post_Revisions |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Determines which fields of posts are to be saved in revisions. |
|
11 |
* |
|
12 |
* @since 2.6.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`. |
0 | 15 |
* @access private |
16 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* @param array|WP_Post $post Optional. A post array or a WP_Post object being processed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* for insertion as a post revision. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* @param bool $deprecated Not used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* @return array Array of fields that can be versioned. |
0 | 21 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
function _wp_post_revision_fields( $post = array(), $deprecated = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
static $fields = null; |
0 | 24 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
if ( ! is_array( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
$post = get_post( $post, ARRAY_A ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
if ( is_null( $fields ) ) { |
16 | 30 |
// Allow these to be versioned. |
0 | 31 |
$fields = array( |
9 | 32 |
'post_title' => __( 'Title' ), |
0 | 33 |
'post_content' => __( 'Content' ), |
34 |
'post_excerpt' => __( 'Excerpt' ), |
|
35 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
} |
0 | 37 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* Filters the list of fields saved in post revisions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
* Included by default: 'post_title', 'post_content' and 'post_excerpt'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
* Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* 'post_date_gmt', 'post_status', 'post_type', 'comment_count', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* and 'post_author'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* @since 4.5.0 The `$post` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* @param array $fields List of fields to revision. Contains 'post_title', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* 'post_content', and 'post_excerpt' by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @param array $post A post array being processed for insertion as a post revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
$fields = apply_filters( '_wp_post_revision_fields', $fields, $post ); |
0 | 55 |
|
16 | 56 |
// WP uses these internally either in versioning or elsewhere - they cannot be versioned. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
unset( $fields[ $protect ] ); |
0 | 59 |
} |
60 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
return $fields; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
} |
0 | 63 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* Returns a post array ready to be inserted into the posts table as a post revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* @param array|WP_Post $post Optional. A post array or a WP_Post object to be processed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* for insertion as a post revision. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* @param bool $autosave Optional. Is the revision an autosave? Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* @return array Post array ready to be inserted as a post revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
function _wp_post_revision_data( $post = array(), $autosave = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
if ( ! is_array( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
$post = get_post( $post, ARRAY_A ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
} |
0 | 79 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
$fields = _wp_post_revision_fields( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
$revision_data = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
$revision_data[ $field ] = $post[ $field ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
$revision_data['post_parent'] = $post['ID']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
$revision_data['post_status'] = 'inherit'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
$revision_data['post_type'] = 'revision'; |
16 | 91 |
$revision_data['post_name'] = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
$revision_data['post_date'] = isset( $post['post_modified'] ) ? $post['post_modified'] : ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
$revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
return $revision_data; |
0 | 96 |
} |
97 |
||
98 |
/** |
|
5 | 99 |
* Creates a revision for the current version of a post. |
0 | 100 |
* |
5 | 101 |
* Typically used immediately after a post update, as every update is a revision, |
102 |
* and the most recent revision always matches the current post. |
|
0 | 103 |
* |
104 |
* @since 2.6.0 |
|
105 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
* @param int $post_id The ID of the post to save as a revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
* @return int|WP_Error|void Void or 0 if error, new revision ID, if success. |
0 | 108 |
*/ |
109 |
function wp_save_post_revision( $post_id ) { |
|
9 | 110 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
0 | 111 |
return; |
9 | 112 |
} |
0 | 113 |
|
16 | 114 |
$post = get_post( $post_id ); |
115 |
if ( ! $post ) { |
|
0 | 116 |
return; |
9 | 117 |
} |
0 | 118 |
|
9 | 119 |
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
0 | 120 |
return; |
9 | 121 |
} |
0 | 122 |
|
16 | 123 |
if ( 'auto-draft' === $post->post_status ) { |
0 | 124 |
return; |
9 | 125 |
} |
0 | 126 |
|
9 | 127 |
if ( ! wp_revisions_enabled( $post ) ) { |
0 | 128 |
return; |
9 | 129 |
} |
0 | 130 |
|
16 | 131 |
/* |
132 |
* Compare the proposed update with the last stored revision verifying that |
|
133 |
* they are different, unless a plugin tells us to always save regardless. |
|
134 |
* If no previous revisions, save one. |
|
135 |
*/ |
|
136 |
$revisions = wp_get_post_revisions( $post_id ); |
|
137 |
if ( $revisions ) { |
|
138 |
// Grab the last revision, but not an autosave. |
|
0 | 139 |
foreach ( $revisions as $revision ) { |
140 |
if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) { |
|
141 |
$last_revision = $revision; |
|
142 |
break; |
|
143 |
} |
|
144 |
} |
|
145 |
||
5 | 146 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
* Filters whether the post has changed since the last revision. |
5 | 148 |
* |
149 |
* By default a revision is saved only if one of the revisioned fields has changed. |
|
150 |
* This filter can override that so a revision is saved even if nothing has changed. |
|
151 |
* |
|
152 |
* @since 3.6.0 |
|
153 |
* |
|
154 |
* @param bool $check_for_changes Whether to check for changes before saving a new revision. |
|
155 |
* Default true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* @param WP_Post $last_revision The last revision post object. |
5 | 157 |
* @param WP_Post $post The post object. |
158 |
*/ |
|
16 | 159 |
if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) { |
0 | 160 |
$post_has_changed = false; |
161 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { |
16 | 163 |
if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $last_revision->$field ) ) { |
0 | 164 |
$post_has_changed = true; |
165 |
break; |
|
166 |
} |
|
167 |
} |
|
5 | 168 |
|
169 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
* Filters whether a post has changed. |
5 | 171 |
* |
172 |
* By default a revision is saved only if one of the revisioned fields has changed. |
|
173 |
* This filter allows for additional checks to determine if there were changes. |
|
174 |
* |
|
175 |
* @since 4.1.0 |
|
176 |
* |
|
177 |
* @param bool $post_has_changed Whether the post has changed. |
|
178 |
* @param WP_Post $last_revision The last revision post object. |
|
179 |
* @param WP_Post $post The post object. |
|
180 |
*/ |
|
181 |
$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post ); |
|
182 |
||
16 | 183 |
// Don't save revision if post unchanged. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
if ( ! $post_has_changed ) { |
0 | 185 |
return; |
5 | 186 |
} |
0 | 187 |
} |
188 |
} |
|
189 |
||
190 |
$return = _wp_put_post_revision( $post ); |
|
191 |
||
5 | 192 |
// If a limit for the number of revisions to keep has been set, |
193 |
// delete the oldest ones. |
|
0 | 194 |
$revisions_to_keep = wp_revisions_to_keep( $post ); |
195 |
||
9 | 196 |
if ( $revisions_to_keep < 0 ) { |
0 | 197 |
return $return; |
9 | 198 |
} |
0 | 199 |
|
200 |
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); |
|
201 |
||
9 | 202 |
$delete = count( $revisions ) - $revisions_to_keep; |
0 | 203 |
|
9 | 204 |
if ( $delete < 1 ) { |
0 | 205 |
return $return; |
9 | 206 |
} |
0 | 207 |
|
208 |
$revisions = array_slice( $revisions, 0, $delete ); |
|
209 |
||
9 | 210 |
for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) { |
211 |
if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) { |
|
0 | 212 |
continue; |
9 | 213 |
} |
0 | 214 |
|
215 |
wp_delete_post_revision( $revisions[ $i ]->ID ); |
|
216 |
} |
|
217 |
||
218 |
return $return; |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Retrieve the autosaved data of the specified post. |
|
223 |
* |
|
16 | 224 |
* Returns a post object with the information that was autosaved for the specified post. |
225 |
* If the optional $user_id is passed, returns the autosave for that user, otherwise |
|
226 |
* returns the latest autosave. |
|
0 | 227 |
* |
228 |
* @since 2.6.0 |
|
229 |
* |
|
230 |
* @param int $post_id The post ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
* @param int $user_id Optional The post author ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* @return WP_Post|false The autosaved data or false on failure or when no autosave exists. |
0 | 233 |
*/ |
234 |
function wp_get_post_autosave( $post_id, $user_id = 0 ) { |
|
16 | 235 |
global $wpdb; |
236 |
||
237 |
$autosave_name = $post_id . '-autosave-v1'; |
|
238 |
$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null; |
|
0 | 239 |
|
16 | 240 |
// Construct the autosave query. |
241 |
$autosave_query = " |
|
242 |
SELECT * |
|
243 |
FROM $wpdb->posts |
|
244 |
WHERE post_parent = %d |
|
245 |
AND post_type = 'revision' |
|
246 |
AND post_status = 'inherit' |
|
247 |
AND post_name = %s " . $user_id_query . ' |
|
248 |
ORDER BY post_date DESC |
|
249 |
LIMIT 1'; |
|
0 | 250 |
|
16 | 251 |
$autosave = $wpdb->get_results( |
252 |
$wpdb->prepare( |
|
253 |
$autosave_query, |
|
254 |
$post_id, |
|
255 |
$autosave_name |
|
256 |
) |
|
257 |
); |
|
258 |
||
259 |
if ( ! $autosave ) { |
|
260 |
return false; |
|
0 | 261 |
} |
262 |
||
16 | 263 |
return get_post( $autosave[0] ); |
0 | 264 |
} |
265 |
||
266 |
/** |
|
267 |
* Determines if the specified post is a revision. |
|
268 |
* |
|
269 |
* @since 2.6.0 |
|
270 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
* @param int|WP_Post $post Post ID or post object. |
16 | 272 |
* @return int|false ID of revision's parent on success, false if not a revision. |
0 | 273 |
*/ |
274 |
function wp_is_post_revision( $post ) { |
|
16 | 275 |
$post = wp_get_post_revision( $post ); |
276 |
if ( ! $post ) { |
|
0 | 277 |
return false; |
9 | 278 |
} |
0 | 279 |
|
280 |
return (int) $post->post_parent; |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Determines if the specified post is an autosave. |
|
285 |
* |
|
286 |
* @since 2.6.0 |
|
287 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
* @param int|WP_Post $post Post ID or post object. |
16 | 289 |
* @return int|false ID of autosave's parent on success, false if not a revision. |
0 | 290 |
*/ |
291 |
function wp_is_post_autosave( $post ) { |
|
16 | 292 |
$post = wp_get_post_revision( $post ); |
293 |
if ( ! $post ) { |
|
0 | 294 |
return false; |
9 | 295 |
} |
0 | 296 |
|
9 | 297 |
if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) { |
0 | 298 |
return (int) $post->post_parent; |
9 | 299 |
} |
0 | 300 |
|
301 |
return false; |
|
302 |
} |
|
303 |
||
304 |
/** |
|
305 |
* Inserts post data into the posts table as a post revision. |
|
306 |
* |
|
307 |
* @since 2.6.0 |
|
308 |
* @access private |
|
309 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* @param int|WP_Post|array|null $post Post ID, post object OR post array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
* @param bool $autosave Optional. Is the revision an autosave? |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
* @return int|WP_Error WP_Error or 0 if error, new revision ID if success. |
0 | 313 |
*/ |
314 |
function _wp_put_post_revision( $post = null, $autosave = false ) { |
|
9 | 315 |
if ( is_object( $post ) ) { |
0 | 316 |
$post = get_object_vars( $post ); |
9 | 317 |
} elseif ( ! is_array( $post ) ) { |
318 |
$post = get_post( $post, ARRAY_A ); |
|
319 |
} |
|
0 | 320 |
|
9 | 321 |
if ( ! $post || empty( $post['ID'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
9 | 323 |
} |
0 | 324 |
|
16 | 325 |
if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) { |
0 | 326 |
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
9 | 327 |
} |
0 | 328 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
$post = _wp_post_revision_data( $post, $autosave ); |
16 | 330 |
$post = wp_slash( $post ); // Since data is from DB. |
0 | 331 |
|
332 |
$revision_id = wp_insert_post( $post ); |
|
9 | 333 |
if ( is_wp_error( $revision_id ) ) { |
0 | 334 |
return $revision_id; |
9 | 335 |
} |
0 | 336 |
|
5 | 337 |
if ( $revision_id ) { |
338 |
/** |
|
339 |
* Fires once a revision has been saved. |
|
340 |
* |
|
341 |
* @since 2.6.0 |
|
342 |
* |
|
343 |
* @param int $revision_id Post revision ID. |
|
344 |
*/ |
|
0 | 345 |
do_action( '_wp_put_post_revision', $revision_id ); |
5 | 346 |
} |
0 | 347 |
|
348 |
return $revision_id; |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Gets a post revision. |
|
353 |
* |
|
354 |
* @since 2.6.0 |
|
355 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
* @param int|WP_Post $post The post ID or object. |
16 | 357 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
358 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
359 |
* respectively. Default OBJECT. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
* @param string $filter Optional sanitation filter. See sanitize_post(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 362 |
*/ |
9 | 363 |
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { |
16 | 364 |
$revision = get_post( $post, OBJECT, $filter ); |
365 |
if ( ! $revision ) { |
|
0 | 366 |
return $revision; |
9 | 367 |
} |
368 |
if ( 'revision' !== $revision->post_type ) { |
|
5 | 369 |
return null; |
9 | 370 |
} |
0 | 371 |
|
16 | 372 |
if ( OBJECT == $output ) { |
0 | 373 |
return $revision; |
16 | 374 |
} elseif ( ARRAY_A == $output ) { |
9 | 375 |
$_revision = get_object_vars( $revision ); |
0 | 376 |
return $_revision; |
16 | 377 |
} elseif ( ARRAY_N == $output ) { |
9 | 378 |
$_revision = array_values( get_object_vars( $revision ) ); |
0 | 379 |
return $_revision; |
380 |
} |
|
381 |
||
382 |
return $revision; |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Restores a post to the specified revision. |
|
387 |
* |
|
388 |
* Can restore a past revision using all fields of the post revision, or only selected fields. |
|
389 |
* |
|
390 |
* @since 2.6.0 |
|
391 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* @param int|WP_Post $revision_id Revision ID or revision object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* @param array $fields Optional. What fields to restore from. Defaults to all. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
* @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. |
0 | 395 |
*/ |
396 |
function wp_restore_post_revision( $revision_id, $fields = null ) { |
|
16 | 397 |
$revision = wp_get_post_revision( $revision_id, ARRAY_A ); |
398 |
if ( ! $revision ) { |
|
0 | 399 |
return $revision; |
9 | 400 |
} |
0 | 401 |
|
9 | 402 |
if ( ! is_array( $fields ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
$fields = array_keys( _wp_post_revision_fields( $revision ) ); |
9 | 404 |
} |
0 | 405 |
|
406 |
$update = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
9 | 408 |
$update[ $field ] = $revision[ $field ]; |
0 | 409 |
} |
410 |
||
9 | 411 |
if ( ! $update ) { |
0 | 412 |
return false; |
9 | 413 |
} |
0 | 414 |
|
415 |
$update['ID'] = $revision['post_parent']; |
|
416 |
||
16 | 417 |
$update = wp_slash( $update ); // Since data is from DB. |
0 | 418 |
|
419 |
$post_id = wp_update_post( $update ); |
|
9 | 420 |
if ( ! $post_id || is_wp_error( $post_id ) ) { |
0 | 421 |
return $post_id; |
9 | 422 |
} |
0 | 423 |
|
16 | 424 |
// Update last edit user. |
0 | 425 |
update_post_meta( $post_id, '_edit_last', get_current_user_id() ); |
426 |
||
5 | 427 |
/** |
428 |
* Fires after a post revision has been restored. |
|
429 |
* |
|
430 |
* @since 2.6.0 |
|
431 |
* |
|
432 |
* @param int $post_id Post ID. |
|
433 |
* @param int $revision_id Post revision ID. |
|
434 |
*/ |
|
0 | 435 |
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); |
436 |
||
437 |
return $post_id; |
|
438 |
} |
|
439 |
||
440 |
/** |
|
441 |
* Deletes a revision. |
|
442 |
* |
|
443 |
* Deletes the row from the posts table corresponding to the specified revision. |
|
444 |
* |
|
445 |
* @since 2.6.0 |
|
446 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
* @param int|WP_Post $revision_id Revision ID or revision object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
* @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success. |
0 | 449 |
*/ |
450 |
function wp_delete_post_revision( $revision_id ) { |
|
16 | 451 |
$revision = wp_get_post_revision( $revision_id ); |
452 |
if ( ! $revision ) { |
|
0 | 453 |
return $revision; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
} |
0 | 455 |
|
456 |
$delete = wp_delete_post( $revision->ID ); |
|
5 | 457 |
if ( $delete ) { |
458 |
/** |
|
459 |
* Fires once a post revision has been deleted. |
|
460 |
* |
|
461 |
* @since 2.6.0 |
|
462 |
* |
|
16 | 463 |
* @param int $revision_id Post revision ID. |
464 |
* @param WP_Post $revision Post revision object. |
|
5 | 465 |
*/ |
0 | 466 |
do_action( 'wp_delete_post_revision', $revision->ID, $revision ); |
5 | 467 |
} |
0 | 468 |
|
469 |
return $delete; |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* Returns all revisions of specified post. |
|
474 |
* |
|
475 |
* @since 2.6.0 |
|
476 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
* @see get_children() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
* @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
* @param array|null $args Optional. Arguments for retrieving post revisions. Default null. |
0 | 481 |
* @return array An array of revisions, or an empty array if none. |
482 |
*/ |
|
483 |
function wp_get_post_revisions( $post_id = 0, $args = null ) { |
|
484 |
$post = get_post( $post_id ); |
|
9 | 485 |
if ( ! $post || empty( $post->ID ) ) { |
0 | 486 |
return array(); |
9 | 487 |
} |
0 | 488 |
|
9 | 489 |
$defaults = array( |
490 |
'order' => 'DESC', |
|
491 |
'orderby' => 'date ID', |
|
492 |
'check_enabled' => true, |
|
493 |
); |
|
494 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 495 |
|
9 | 496 |
if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) { |
0 | 497 |
return array(); |
9 | 498 |
} |
0 | 499 |
|
9 | 500 |
$args = array_merge( |
501 |
$args, |
|
502 |
array( |
|
503 |
'post_parent' => $post->ID, |
|
504 |
'post_type' => 'revision', |
|
505 |
'post_status' => 'inherit', |
|
506 |
) |
|
507 |
); |
|
0 | 508 |
|
16 | 509 |
$revisions = get_children( $args ); |
510 |
if ( ! $revisions ) { |
|
0 | 511 |
return array(); |
9 | 512 |
} |
0 | 513 |
|
514 |
return $revisions; |
|
515 |
} |
|
516 |
||
517 |
/** |
|
518 |
* Determine if revisions are enabled for a given post. |
|
519 |
* |
|
520 |
* @since 3.6.0 |
|
521 |
* |
|
5 | 522 |
* @param WP_Post $post The post object. |
0 | 523 |
* @return bool True if number of revisions to keep isn't zero, false otherwise. |
524 |
*/ |
|
525 |
function wp_revisions_enabled( $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
return wp_revisions_to_keep( $post ) !== 0; |
0 | 527 |
} |
528 |
||
529 |
/** |
|
530 |
* Determine how many revisions to retain for a given post. |
|
5 | 531 |
* |
532 |
* By default, an infinite number of revisions are kept. |
|
533 |
* |
|
534 |
* The constant WP_POST_REVISIONS can be set in wp-config to specify the limit |
|
535 |
* of revisions to keep. |
|
0 | 536 |
* |
537 |
* @since 3.6.0 |
|
538 |
* |
|
5 | 539 |
* @param WP_Post $post The post object. |
0 | 540 |
* @return int The number of revisions to keep. |
541 |
*/ |
|
542 |
function wp_revisions_to_keep( $post ) { |
|
543 |
$num = WP_POST_REVISIONS; |
|
544 |
||
9 | 545 |
if ( true === $num ) { |
0 | 546 |
$num = -1; |
9 | 547 |
} else { |
0 | 548 |
$num = intval( $num ); |
9 | 549 |
} |
0 | 550 |
|
9 | 551 |
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
0 | 552 |
$num = 0; |
9 | 553 |
} |
0 | 554 |
|
5 | 555 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* Filters the number of revisions to save for the given post. |
5 | 557 |
* |
558 |
* Overrides the value of WP_POST_REVISIONS. |
|
559 |
* |
|
560 |
* @since 3.6.0 |
|
561 |
* |
|
562 |
* @param int $num Number of revisions to store. |
|
563 |
* @param WP_Post $post Post object. |
|
564 |
*/ |
|
0 | 565 |
return (int) apply_filters( 'wp_revisions_to_keep', $num, $post ); |
566 |
} |
|
567 |
||
568 |
/** |
|
569 |
* Sets up the post object for preview based on the post autosave. |
|
570 |
* |
|
571 |
* @since 2.7.0 |
|
572 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @param WP_Post $post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
* @return WP_Post|false |
0 | 576 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
function _set_preview( $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
if ( ! is_object( $post ) ) { |
0 | 579 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
} |
0 | 581 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
$preview = wp_get_post_autosave( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
if ( ! is_object( $preview ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
return $post; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
} |
0 | 586 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
$preview = sanitize_post( $preview ); |
0 | 588 |
|
589 |
$post->post_content = $preview->post_content; |
|
9 | 590 |
$post->post_title = $preview->post_title; |
0 | 591 |
$post->post_excerpt = $preview->post_excerpt; |
592 |
||
593 |
add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); |
0 | 595 |
|
596 |
return $post; |
|
597 |
} |
|
598 |
||
599 |
/** |
|
600 |
* Filters the latest content for preview from the post autosave. |
|
601 |
* |
|
602 |
* @since 2.7.0 |
|
603 |
* @access private |
|
604 |
*/ |
|
605 |
function _show_post_preview() { |
|
9 | 606 |
if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) { |
0 | 607 |
$id = (int) $_GET['preview_id']; |
608 |
||
9 | 609 |
if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) { |
610 |
wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 ); |
|
611 |
} |
|
0 | 612 |
|
9 | 613 |
add_filter( 'the_preview', '_set_preview' ); |
0 | 614 |
} |
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* Filters terms lookup to set the post format. |
|
619 |
* |
|
620 |
* @since 3.6.0 |
|
621 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
* @param array $terms |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
* @param int $post_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
* @param string $taxonomy |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
* @return array |
0 | 627 |
*/ |
628 |
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { |
|
16 | 629 |
$post = get_post(); |
630 |
if ( ! $post ) { |
|
0 | 631 |
return $terms; |
9 | 632 |
} |
0 | 633 |
|
16 | 634 |
if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id |
635 |
|| 'post_format' !== $taxonomy || 'revision' === $post->post_type |
|
636 |
) { |
|
0 | 637 |
return $terms; |
9 | 638 |
} |
0 | 639 |
|
16 | 640 |
if ( 'standard' === $_REQUEST['post_format'] ) { |
0 | 641 |
$terms = array(); |
16 | 642 |
} else { |
643 |
$term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ); |
|
644 |
if ( $term ) { |
|
645 |
$terms = array( $term ); // Can only have one post format. |
|
646 |
} |
|
9 | 647 |
} |
0 | 648 |
|
649 |
return $terms; |
|
650 |
} |
|
651 |
||
652 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* Filters post thumbnail lookup to set the post thumbnail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
* @param null|array|string $value The value to return - a single metadata value, or an array of values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* @return null|array The default return value or the post thumbnail meta array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) { |
16 | 664 |
$post = get_post(); |
665 |
if ( ! $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
if ( empty( $_REQUEST['_thumbnail_id'] ) || |
9 | 670 |
empty( $_REQUEST['preview_id'] ) || |
671 |
$post->ID != $post_id || |
|
16 | 672 |
'_thumbnail_id' !== $meta_key || |
673 |
'revision' === $post->post_type || |
|
9 | 674 |
$post_id != $_REQUEST['preview_id'] ) { |
675 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
678 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
$thumbnail_id = intval( $_REQUEST['_thumbnail_id'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
680 |
if ( $thumbnail_id <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
684 |
return strval( $thumbnail_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
686 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
/** |
0 | 688 |
* Gets the post revision version. |
689 |
* |
|
690 |
* @since 3.6.0 |
|
691 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
693 |
* @param WP_Post $revision |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
* @return int|false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
*/ |
0 | 696 |
function _wp_get_post_revision_version( $revision ) { |
9 | 697 |
if ( is_object( $revision ) ) { |
0 | 698 |
$revision = get_object_vars( $revision ); |
9 | 699 |
} elseif ( ! is_array( $revision ) ) { |
0 | 700 |
return false; |
9 | 701 |
} |
0 | 702 |
|
9 | 703 |
if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) { |
0 | 704 |
return (int) $matches[1]; |
9 | 705 |
} |
0 | 706 |
|
707 |
return 0; |
|
708 |
} |
|
709 |
||
710 |
/** |
|
711 |
* Upgrade the revisions author, add the current post as a revision and set the revisions version to 1 |
|
712 |
* |
|
713 |
* @since 3.6.0 |
|
714 |
* @access private |
|
715 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
* @param WP_Post $post Post object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
* @param array $revisions Current revisions of the post |
0 | 720 |
* @return bool true if the revisions were upgraded, false if problems |
721 |
*/ |
|
722 |
function _wp_upgrade_revisions_of_post( $post, $revisions ) { |
|
723 |
global $wpdb; |
|
724 |
||
16 | 725 |
// Add post option exclusively. |
9 | 726 |
$lock = "revision-upgrade-{$post->ID}"; |
727 |
$now = time(); |
|
0 | 728 |
$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) ); |
729 |
if ( ! $result ) { |
|
16 | 730 |
// If we couldn't get a lock, see how old the previous lock is. |
0 | 731 |
$locked = get_option( $lock ); |
732 |
if ( ! $locked ) { |
|
733 |
// Can't write to the lock, and can't read the lock. |
|
16 | 734 |
// Something broken has happened. |
0 | 735 |
return false; |
736 |
} |
|
737 |
||
738 |
if ( $locked > $now - 3600 ) { |
|
16 | 739 |
// Lock is not too old: some other process may be upgrading this post. Bail. |
0 | 740 |
return false; |
741 |
} |
|
742 |
||
16 | 743 |
// Lock is too old - update it (below) and continue. |
0 | 744 |
} |
745 |
||
746 |
// If we could get a lock, re-"add" the option to fire all the correct filters. |
|
747 |
update_option( $lock, $now ); |
|
748 |
||
749 |
reset( $revisions ); |
|
750 |
$add_last = true; |
|
751 |
||
752 |
do { |
|
753 |
$this_revision = current( $revisions ); |
|
754 |
$prev_revision = next( $revisions ); |
|
755 |
||
756 |
$this_revision_version = _wp_get_post_revision_version( $this_revision ); |
|
757 |
||
16 | 758 |
// Something terrible happened. |
9 | 759 |
if ( false === $this_revision_version ) { |
0 | 760 |
continue; |
9 | 761 |
} |
0 | 762 |
|
763 |
// 1 is the latest revision version, so we're already up to date. |
|
764 |
// No need to add a copy of the post as latest revision. |
|
765 |
if ( 0 < $this_revision_version ) { |
|
766 |
$add_last = false; |
|
767 |
continue; |
|
768 |
} |
|
769 |
||
16 | 770 |
// Always update the revision version. |
0 | 771 |
$update = array( |
772 |
'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ), |
|
773 |
); |
|
774 |
||
16 | 775 |
/* |
776 |
* If this revision is the oldest revision of the post, i.e. no $prev_revision, |
|
777 |
* the correct post_author is probably $post->post_author, but that's only a good guess. |
|
778 |
* Update the revision version only and Leave the author as-is. |
|
779 |
*/ |
|
0 | 780 |
if ( $prev_revision ) { |
781 |
$prev_revision_version = _wp_get_post_revision_version( $prev_revision ); |
|
782 |
||
783 |
// If the previous revision is already up to date, it no longer has the information we need :( |
|
9 | 784 |
if ( $prev_revision_version < 1 ) { |
0 | 785 |
$update['post_author'] = $prev_revision->post_author; |
9 | 786 |
} |
0 | 787 |
} |
788 |
||
16 | 789 |
// Upgrade this revision. |
0 | 790 |
$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); |
791 |
||
9 | 792 |
if ( $result ) { |
0 | 793 |
wp_cache_delete( $this_revision->ID, 'posts' ); |
9 | 794 |
} |
0 | 795 |
} while ( $prev_revision ); |
796 |
||
797 |
delete_option( $lock ); |
|
798 |
||
799 |
// Add a copy of the post as latest revision. |
|
9 | 800 |
if ( $add_last ) { |
0 | 801 |
wp_save_post_revision( $post->ID ); |
9 | 802 |
} |
0 | 803 |
|
804 |
return true; |
|
805 |
} |