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