author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Post Administration API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Rename $_POST data from form names to DB post columns. |
|
11 |
* |
|
12 |
* Manipulates $_POST directly. |
|
13 |
* |
|
14 |
* @since 2.6.0 |
|
15 |
* |
|
16 |
* @param bool $update Are we updating a pre-existing post? |
|
17 |
* @param array $post_data Array of post data. Defaults to the contents of $_POST. |
|
18 |
* @return object|bool WP_Error on failure, true on success. |
|
19 |
*/ |
|
20 |
function _wp_translate_postdata( $update = false, $post_data = null ) { |
|
21 |
||
22 |
if ( empty($post_data) ) |
|
23 |
$post_data = &$_POST; |
|
24 |
||
25 |
if ( $update ) |
|
26 |
$post_data['ID'] = (int) $post_data['post_ID']; |
|
27 |
||
28 |
$ptype = get_post_type_object( $post_data['post_type'] ); |
|
29 |
||
30 |
if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { |
|
31 |
if ( 'page' == $post_data['post_type'] ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
0 | 33 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
0 | 35 |
} elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { |
36 |
if ( 'page' == $post_data['post_type'] ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
0 | 38 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
0 | 40 |
} |
41 |
||
42 |
if ( isset( $post_data['content'] ) ) |
|
43 |
$post_data['post_content'] = $post_data['content']; |
|
44 |
||
45 |
if ( isset( $post_data['excerpt'] ) ) |
|
46 |
$post_data['post_excerpt'] = $post_data['excerpt']; |
|
47 |
||
48 |
if ( isset( $post_data['parent_id'] ) ) |
|
49 |
$post_data['post_parent'] = (int) $post_data['parent_id']; |
|
50 |
||
51 |
if ( isset($post_data['trackback_url']) ) |
|
52 |
$post_data['to_ping'] = $post_data['trackback_url']; |
|
53 |
||
54 |
$post_data['user_ID'] = get_current_user_id(); |
|
55 |
||
56 |
if (!empty ( $post_data['post_author_override'] ) ) { |
|
57 |
$post_data['post_author'] = (int) $post_data['post_author_override']; |
|
58 |
} else { |
|
59 |
if (!empty ( $post_data['post_author'] ) ) { |
|
60 |
$post_data['post_author'] = (int) $post_data['post_author']; |
|
61 |
} else { |
|
62 |
$post_data['post_author'] = (int) $post_data['user_ID']; |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) |
|
67 |
&& ! current_user_can( $ptype->cap->edit_others_posts ) ) { |
|
68 |
if ( $update ) { |
|
69 |
if ( 'page' == $post_data['post_type'] ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
0 | 71 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
0 | 73 |
} else { |
74 |
if ( 'page' == $post_data['post_type'] ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
0 | 76 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
0 | 78 |
} |
79 |
} |
|
80 |
||
5 | 81 |
if ( ! empty( $post_data['post_status'] ) ) { |
0 | 82 |
$post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
83 |
||
5 | 84 |
// No longer an auto-draft |
85 |
if ( 'auto-draft' === $post_data['post_status'] ) { |
|
86 |
$post_data['post_status'] = 'draft'; |
|
87 |
} |
|
88 |
||
89 |
if ( ! get_post_status_object( $post_data['post_status'] ) ) { |
|
90 |
unset( $post_data['post_status'] ); |
|
91 |
} |
|
92 |
} |
|
93 |
||
0 | 94 |
// What to do based on which button they pressed |
95 |
if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] ) |
|
96 |
$post_data['post_status'] = 'draft'; |
|
97 |
if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] ) |
|
98 |
$post_data['post_status'] = 'private'; |
|
99 |
if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) ) |
|
100 |
$post_data['post_status'] = 'publish'; |
|
101 |
if ( isset($post_data['advanced']) && '' != $post_data['advanced'] ) |
|
102 |
$post_data['post_status'] = 'draft'; |
|
103 |
if ( isset($post_data['pending']) && '' != $post_data['pending'] ) |
|
104 |
$post_data['post_status'] = 'pending'; |
|
105 |
||
106 |
if ( isset( $post_data['ID'] ) ) |
|
107 |
$post_id = $post_data['ID']; |
|
108 |
else |
|
109 |
$post_id = false; |
|
110 |
$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; |
|
111 |
||
5 | 112 |
if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) { |
113 |
$post_data['post_status'] = $previous_status ? $previous_status : 'pending'; |
|
114 |
} |
|
115 |
||
0 | 116 |
$published_statuses = array( 'publish', 'future' ); |
117 |
||
118 |
// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published. |
|
119 |
// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. |
|
120 |
if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) ) |
|
121 |
if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) ) |
|
122 |
$post_data['post_status'] = 'pending'; |
|
123 |
||
5 | 124 |
if ( ! isset( $post_data['post_status'] ) ) { |
125 |
$post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status; |
|
126 |
} |
|
127 |
||
128 |
if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) { |
|
129 |
unset( $post_data['post_password'] ); |
|
130 |
} |
|
0 | 131 |
|
132 |
if (!isset( $post_data['comment_status'] )) |
|
133 |
$post_data['comment_status'] = 'closed'; |
|
134 |
||
135 |
if (!isset( $post_data['ping_status'] )) |
|
136 |
$post_data['ping_status'] = 'closed'; |
|
137 |
||
138 |
foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) { |
|
139 |
if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) { |
|
140 |
$post_data['edit_date'] = '1'; |
|
141 |
break; |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
if ( !empty( $post_data['edit_date'] ) ) { |
|
146 |
$aa = $post_data['aa']; |
|
147 |
$mm = $post_data['mm']; |
|
148 |
$jj = $post_data['jj']; |
|
149 |
$hh = $post_data['hh']; |
|
150 |
$mn = $post_data['mn']; |
|
151 |
$ss = $post_data['ss']; |
|
152 |
$aa = ($aa <= 0 ) ? date('Y') : $aa; |
|
153 |
$mm = ($mm <= 0 ) ? date('n') : $mm; |
|
154 |
$jj = ($jj > 31 ) ? 31 : $jj; |
|
155 |
$jj = ($jj <= 0 ) ? date('j') : $jj; |
|
156 |
$hh = ($hh > 23 ) ? $hh -24 : $hh; |
|
157 |
$mn = ($mn > 59 ) ? $mn -60 : $mn; |
|
158 |
$ss = ($ss > 59 ) ? $ss -60 : $ss; |
|
159 |
$post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss ); |
|
160 |
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); |
|
161 |
if ( !$valid_date ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
0 | 163 |
} |
164 |
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); |
|
165 |
} |
|
166 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
if ( isset( $post_data['post_category'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
$category_object = get_taxonomy( 'category' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
if ( ! current_user_can( $category_object->cap->assign_terms ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
unset( $post_data['post_category'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
|
0 | 174 |
return $post_data; |
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Update an existing post with values provided in $_POST. |
|
179 |
* |
|
180 |
* @since 1.5.0 |
|
181 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* |
0 | 184 |
* @param array $post_data Optional. |
185 |
* @return int Post ID. |
|
186 |
*/ |
|
187 |
function edit_post( $post_data = null ) { |
|
5 | 188 |
global $wpdb; |
0 | 189 |
|
190 |
if ( empty($post_data) ) |
|
191 |
$post_data = &$_POST; |
|
192 |
||
193 |
// Clear out any data in internal vars. |
|
194 |
unset( $post_data['filter'] ); |
|
195 |
||
196 |
$post_ID = (int) $post_data['post_ID']; |
|
197 |
$post = get_post( $post_ID ); |
|
198 |
$post_data['post_type'] = $post->post_type; |
|
199 |
$post_data['post_mime_type'] = $post->post_mime_type; |
|
200 |
||
5 | 201 |
if ( ! empty( $post_data['post_status'] ) ) { |
202 |
$post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
|
203 |
||
204 |
if ( 'inherit' == $post_data['post_status'] ) { |
|
205 |
unset( $post_data['post_status'] ); |
|
206 |
} |
|
207 |
} |
|
208 |
||
0 | 209 |
$ptype = get_post_type_object($post_data['post_type']); |
210 |
if ( !current_user_can( 'edit_post', $post_ID ) ) { |
|
211 |
if ( 'page' == $post_data['post_type'] ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
wp_die( __('Sorry, you are not allowed to edit this page.' )); |
0 | 213 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
wp_die( __('Sorry, you are not allowed to edit this post.' )); |
0 | 215 |
} |
216 |
||
217 |
if ( post_type_supports( $ptype->name, 'revisions' ) ) { |
|
218 |
$revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) ); |
|
219 |
$revision = current( $revisions ); |
|
220 |
||
221 |
// Check if the revisions have been upgraded |
|
222 |
if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) |
|
223 |
_wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) ); |
|
224 |
} |
|
225 |
||
226 |
if ( isset($post_data['visibility']) ) { |
|
227 |
switch ( $post_data['visibility'] ) { |
|
228 |
case 'public' : |
|
229 |
$post_data['post_password'] = ''; |
|
230 |
break; |
|
231 |
case 'password' : |
|
232 |
unset( $post_data['sticky'] ); |
|
233 |
break; |
|
234 |
case 'private' : |
|
235 |
$post_data['post_status'] = 'private'; |
|
236 |
$post_data['post_password'] = ''; |
|
237 |
unset( $post_data['sticky'] ); |
|
238 |
break; |
|
239 |
} |
|
240 |
} |
|
241 |
||
5 | 242 |
$post_data = _wp_translate_postdata( true, $post_data ); |
243 |
if ( is_wp_error($post_data) ) |
|
244 |
wp_die( $post_data->get_error_message() ); |
|
245 |
||
0 | 246 |
// Post Formats |
247 |
if ( isset( $post_data['post_format'] ) ) |
|
248 |
set_post_format( $post_ID, $post_data['post_format'] ); |
|
249 |
||
250 |
$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' ); |
|
251 |
foreach ( $format_meta_urls as $format_meta_url ) { |
|
252 |
$keyed = '_format_' . $format_meta_url; |
|
253 |
if ( isset( $post_data[ $keyed ] ) ) |
|
254 |
update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) ); |
|
255 |
} |
|
256 |
||
257 |
$format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' ); |
|
258 |
||
259 |
foreach ( $format_keys as $key ) { |
|
260 |
$keyed = '_format_' . $key; |
|
261 |
if ( isset( $post_data[ $keyed ] ) ) { |
|
262 |
if ( current_user_can( 'unfiltered_html' ) ) |
|
263 |
update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] ); |
|
264 |
else |
|
265 |
update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) ); |
|
266 |
} |
|
267 |
} |
|
268 |
||
5 | 269 |
if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) { |
270 |
$id3data = wp_get_attachment_metadata( $post_ID ); |
|
271 |
if ( ! is_array( $id3data ) ) { |
|
272 |
$id3data = array(); |
|
273 |
} |
|
274 |
||
275 |
foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) { |
|
276 |
if ( isset( $post_data[ 'id3_' . $key ] ) ) { |
|
277 |
$id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) ); |
|
278 |
} |
|
279 |
} |
|
280 |
wp_update_attachment_metadata( $post_ID, $id3data ); |
|
281 |
} |
|
282 |
||
0 | 283 |
// Meta Stuff |
284 |
if ( isset($post_data['meta']) && $post_data['meta'] ) { |
|
285 |
foreach ( $post_data['meta'] as $key => $value ) { |
|
286 |
if ( !$meta = get_post_meta_by_id( $key ) ) |
|
287 |
continue; |
|
288 |
if ( $meta->post_id != $post_ID ) |
|
289 |
continue; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
continue; |
0 | 292 |
if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) |
293 |
continue; |
|
294 |
update_meta( $key, $value['key'], $value['value'] ); |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) { |
|
299 |
foreach ( $post_data['deletemeta'] as $key => $value ) { |
|
300 |
if ( !$meta = get_post_meta_by_id( $key ) ) |
|
301 |
continue; |
|
302 |
if ( $meta->post_id != $post_ID ) |
|
303 |
continue; |
|
304 |
if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) |
|
305 |
continue; |
|
306 |
delete_meta( $key ); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
// Attachment stuff |
|
311 |
if ( 'attachment' == $post_data['post_type'] ) { |
|
312 |
if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) { |
|
313 |
$image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); |
|
314 |
if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) { |
|
315 |
$image_alt = wp_strip_all_tags( $image_alt, true ); |
|
5 | 316 |
// update_meta expects slashed. |
0 | 317 |
update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); |
318 |
} |
|
319 |
} |
|
320 |
||
321 |
$attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); |
|
5 | 322 |
|
0 | 323 |
/** This filter is documented in wp-admin/includes/media.php */ |
324 |
$post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data ); |
|
325 |
} |
|
326 |
||
5 | 327 |
// Convert taxonomy input to term IDs, to avoid ambiguity. |
328 |
if ( isset( $post_data['tax_input'] ) ) { |
|
329 |
foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) { |
|
330 |
// Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary. |
|
331 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|
332 |
continue; |
|
333 |
} |
|
334 |
||
335 |
/* |
|
336 |
* Assume that a 'tax_input' string is a comma-separated list of term names. |
|
337 |
* Some languages may use a character other than a comma as a delimiter, so we standardize on |
|
338 |
* commas before parsing the list. |
|
339 |
*/ |
|
340 |
if ( ! is_array( $terms ) ) { |
|
341 |
$comma = _x( ',', 'tag delimiter' ); |
|
342 |
if ( ',' !== $comma ) { |
|
343 |
$terms = str_replace( $comma, ',', $terms ); |
|
344 |
} |
|
345 |
$terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
|
346 |
} |
|
347 |
||
348 |
$clean_terms = array(); |
|
349 |
foreach ( $terms as $term ) { |
|
350 |
// Empty terms are invalid input. |
|
351 |
if ( empty( $term ) ) { |
|
352 |
continue; |
|
353 |
} |
|
354 |
||
355 |
$_term = get_terms( $taxonomy, array( |
|
356 |
'name' => $term, |
|
357 |
'fields' => 'ids', |
|
358 |
'hide_empty' => false, |
|
359 |
) ); |
|
360 |
||
361 |
if ( ! empty( $_term ) ) { |
|
362 |
$clean_terms[] = intval( $_term[0] ); |
|
363 |
} else { |
|
364 |
// No existing term was found, so pass the string. A new term will be created. |
|
365 |
$clean_terms[] = $term; |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
$post_data['tax_input'][ $taxonomy ] = $clean_terms; |
|
370 |
} |
|
371 |
} |
|
372 |
||
0 | 373 |
add_meta( $post_ID ); |
374 |
||
375 |
update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); |
|
376 |
||
5 | 377 |
$success = wp_update_post( $post_data ); |
378 |
// If the save failed, see if we can sanity check the main fields and try again |
|
379 |
if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { |
|
380 |
$fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
|
381 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
foreach ( $fields as $field ) { |
5 | 383 |
if ( isset( $post_data[ $field ] ) ) { |
384 |
$post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] ); |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
wp_update_post( $post_data ); |
|
389 |
} |
|
0 | 390 |
|
391 |
// Now that we have an ID we can fix any attachment anchor hrefs |
|
392 |
_fix_attachment_links( $post_ID ); |
|
393 |
||
394 |
wp_set_post_lock( $post_ID ); |
|
395 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) { |
0 | 397 |
if ( ! empty( $post_data['sticky'] ) ) |
398 |
stick_post( $post_ID ); |
|
399 |
else |
|
400 |
unstick_post( $post_ID ); |
|
401 |
} |
|
402 |
||
403 |
return $post_ID; |
|
404 |
} |
|
405 |
||
406 |
/** |
|
407 |
* Process the post data for the bulk editing of posts. |
|
408 |
* |
|
409 |
* Updates all bulk edited posts/pages, adding (but not removing) tags and |
|
410 |
* categories. Skips pages when they would be their own parent or child. |
|
411 |
* |
|
412 |
* @since 2.7.0 |
|
413 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* |
0 | 416 |
* @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal. |
417 |
* @return array |
|
418 |
*/ |
|
419 |
function bulk_edit_posts( $post_data = null ) { |
|
420 |
global $wpdb; |
|
421 |
||
422 |
if ( empty($post_data) ) |
|
423 |
$post_data = &$_POST; |
|
424 |
||
425 |
if ( isset($post_data['post_type']) ) |
|
426 |
$ptype = get_post_type_object($post_data['post_type']); |
|
427 |
else |
|
428 |
$ptype = get_post_type_object('post'); |
|
429 |
||
430 |
if ( !current_user_can( $ptype->cap->edit_posts ) ) { |
|
431 |
if ( 'page' == $ptype->name ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
wp_die( __('Sorry, you are not allowed to edit pages.')); |
0 | 433 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
wp_die( __('Sorry, you are not allowed to edit posts.')); |
0 | 435 |
} |
436 |
||
437 |
if ( -1 == $post_data['_status'] ) { |
|
438 |
$post_data['post_status'] = null; |
|
439 |
unset($post_data['post_status']); |
|
440 |
} else { |
|
441 |
$post_data['post_status'] = $post_data['_status']; |
|
442 |
} |
|
443 |
unset($post_data['_status']); |
|
444 |
||
5 | 445 |
if ( ! empty( $post_data['post_status'] ) ) { |
446 |
$post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
|
447 |
||
448 |
if ( 'inherit' == $post_data['post_status'] ) { |
|
449 |
unset( $post_data['post_status'] ); |
|
450 |
} |
|
451 |
} |
|
452 |
||
0 | 453 |
$post_IDs = array_map( 'intval', (array) $post_data['post'] ); |
454 |
||
455 |
$reset = array( |
|
456 |
'post_author', 'post_status', 'post_password', |
|
457 |
'post_parent', 'page_template', 'comment_status', |
|
458 |
'ping_status', 'keep_private', 'tax_input', |
|
459 |
'post_category', 'sticky', 'post_format', |
|
460 |
); |
|
461 |
||
462 |
foreach ( $reset as $field ) { |
|
463 |
if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) ) |
|
464 |
unset($post_data[$field]); |
|
465 |
} |
|
466 |
||
467 |
if ( isset($post_data['post_category']) ) { |
|
468 |
if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) ) |
|
469 |
$new_cats = array_map( 'absint', $post_data['post_category'] ); |
|
470 |
else |
|
471 |
unset($post_data['post_category']); |
|
472 |
} |
|
473 |
||
474 |
$tax_input = array(); |
|
475 |
if ( isset($post_data['tax_input'])) { |
|
476 |
foreach ( $post_data['tax_input'] as $tax_name => $terms ) { |
|
477 |
if ( empty($terms) ) |
|
478 |
continue; |
|
479 |
if ( is_taxonomy_hierarchical( $tax_name ) ) { |
|
480 |
$tax_input[ $tax_name ] = array_map( 'absint', $terms ); |
|
481 |
} else { |
|
482 |
$comma = _x( ',', 'tag delimiter' ); |
|
483 |
if ( ',' !== $comma ) |
|
484 |
$terms = str_replace( $comma, ',', $terms ); |
|
485 |
$tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) { |
|
491 |
$pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'"); |
|
492 |
$children = array(); |
|
493 |
||
494 |
for ( $i = 0; $i < 50 && $parent > 0; $i++ ) { |
|
495 |
$children[] = $parent; |
|
496 |
||
497 |
foreach ( $pages as $page ) { |
|
498 |
if ( $page->ID == $parent ) { |
|
499 |
$parent = $page->post_parent; |
|
500 |
break; |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
$updated = $skipped = $locked = array(); |
|
5 | 507 |
$shared_post_data = $post_data; |
508 |
||
0 | 509 |
foreach ( $post_IDs as $post_ID ) { |
5 | 510 |
// Start with fresh post data with each iteration. |
511 |
$post_data = $shared_post_data; |
|
512 |
||
0 | 513 |
$post_type_object = get_post_type_object( get_post_type( $post_ID ) ); |
514 |
||
515 |
if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) { |
|
516 |
$skipped[] = $post_ID; |
|
517 |
continue; |
|
518 |
} |
|
519 |
||
520 |
if ( wp_check_post_lock( $post_ID ) ) { |
|
521 |
$locked[] = $post_ID; |
|
522 |
continue; |
|
523 |
} |
|
524 |
||
525 |
$post = get_post( $post_ID ); |
|
526 |
$tax_names = get_object_taxonomies( $post ); |
|
527 |
foreach ( $tax_names as $tax_name ) { |
|
528 |
$taxonomy_obj = get_taxonomy($tax_name); |
|
529 |
if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) |
|
530 |
$new_terms = $tax_input[$tax_name]; |
|
531 |
else |
|
532 |
$new_terms = array(); |
|
533 |
||
534 |
if ( $taxonomy_obj->hierarchical ) |
|
535 |
$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') ); |
|
536 |
else |
|
537 |
$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') ); |
|
538 |
||
539 |
$post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms ); |
|
540 |
} |
|
541 |
||
542 |
if ( isset($new_cats) && in_array( 'category', $tax_names ) ) { |
|
543 |
$cats = (array) wp_get_post_categories($post_ID); |
|
544 |
$post_data['post_category'] = array_unique( array_merge($cats, $new_cats) ); |
|
545 |
unset( $post_data['tax_input']['category'] ); |
|
546 |
} |
|
547 |
||
5 | 548 |
$post_data['post_type'] = $post->post_type; |
0 | 549 |
$post_data['post_mime_type'] = $post->post_mime_type; |
550 |
$post_data['guid'] = $post->guid; |
|
551 |
||
5 | 552 |
foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) { |
553 |
if ( ! isset( $post_data[ $field ] ) ) { |
|
554 |
$post_data[ $field ] = $post->$field; |
|
555 |
} |
|
556 |
} |
|
557 |
||
0 | 558 |
$post_data['ID'] = $post_ID; |
5 | 559 |
$post_data['post_ID'] = $post_ID; |
560 |
||
561 |
$post_data = _wp_translate_postdata( true, $post_data ); |
|
562 |
if ( is_wp_error( $post_data ) ) { |
|
563 |
$skipped[] = $post_ID; |
|
564 |
continue; |
|
565 |
} |
|
566 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
if ( isset( $post_data['post_format'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
set_post_format( $post_ID, $post_data['post_format'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
569 |
unset( $post_data['tax_input']['post_format'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
|
0 | 572 |
$updated[] = wp_update_post( $post_data ); |
573 |
||
574 |
if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { |
|
575 |
if ( 'sticky' == $post_data['sticky'] ) |
|
576 |
stick_post( $post_ID ); |
|
577 |
else |
|
578 |
unstick_post( $post_ID ); |
|
579 |
} |
|
580 |
} |
|
581 |
||
582 |
return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked ); |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* Default post information to use when populating the "Write Post" form. |
|
587 |
* |
|
588 |
* @since 2.0.0 |
|
589 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
* @param string $post_type Optional. A post type string. Default 'post'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
* @param bool $create_in_db Optional. Whether to insert the post into database. Default false. |
0 | 592 |
* @return WP_Post Post object containing all the default post data as attributes |
593 |
*/ |
|
594 |
function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) { |
|
595 |
$post_title = ''; |
|
596 |
if ( !empty( $_REQUEST['post_title'] ) ) |
|
597 |
$post_title = esc_html( wp_unslash( $_REQUEST['post_title'] )); |
|
598 |
||
599 |
$post_content = ''; |
|
600 |
if ( !empty( $_REQUEST['content'] ) ) |
|
601 |
$post_content = esc_html( wp_unslash( $_REQUEST['content'] )); |
|
602 |
||
603 |
$post_excerpt = ''; |
|
604 |
if ( !empty( $_REQUEST['excerpt'] ) ) |
|
605 |
$post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] )); |
|
606 |
||
607 |
if ( $create_in_db ) { |
|
608 |
$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) ); |
|
609 |
$post = get_post( $post_id ); |
|
610 |
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) |
|
611 |
set_post_format( $post, get_option( 'default_post_format' ) ); |
|
612 |
} else { |
|
613 |
$post = new stdClass; |
|
614 |
$post->ID = 0; |
|
615 |
$post->post_author = ''; |
|
616 |
$post->post_date = ''; |
|
617 |
$post->post_date_gmt = ''; |
|
618 |
$post->post_password = ''; |
|
5 | 619 |
$post->post_name = ''; |
0 | 620 |
$post->post_type = $post_type; |
621 |
$post->post_status = 'draft'; |
|
622 |
$post->to_ping = ''; |
|
623 |
$post->pinged = ''; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
$post->comment_status = get_default_comment_status( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
$post->ping_status = get_default_comment_status( $post_type, 'pingback' ); |
0 | 626 |
$post->post_pingback = get_option( 'default_pingback_flag' ); |
627 |
$post->post_category = get_option( 'default_category' ); |
|
628 |
$post->page_template = 'default'; |
|
629 |
$post->post_parent = 0; |
|
630 |
$post->menu_order = 0; |
|
631 |
$post = new WP_Post( $post ); |
|
632 |
} |
|
633 |
||
5 | 634 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
635 |
* Filters the default post content initially used in the "Write Post" form. |
5 | 636 |
* |
637 |
* @since 1.5.0 |
|
638 |
* |
|
639 |
* @param string $post_content Default post content. |
|
640 |
* @param WP_Post $post Post object. |
|
641 |
*/ |
|
0 | 642 |
$post->post_content = apply_filters( 'default_content', $post_content, $post ); |
5 | 643 |
|
644 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* Filters the default post title initially used in the "Write Post" form. |
5 | 646 |
* |
647 |
* @since 1.5.0 |
|
648 |
* |
|
649 |
* @param string $post_title Default post title. |
|
650 |
* @param WP_Post $post Post object. |
|
651 |
*/ |
|
652 |
$post->post_title = apply_filters( 'default_title', $post_title, $post ); |
|
653 |
||
654 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* Filters the default post excerpt initially used in the "Write Post" form. |
5 | 656 |
* |
657 |
* @since 1.5.0 |
|
658 |
* |
|
659 |
* @param string $post_excerpt Default post excerpt. |
|
660 |
* @param WP_Post $post Post object. |
|
661 |
*/ |
|
0 | 662 |
$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post ); |
663 |
||
664 |
return $post; |
|
665 |
} |
|
666 |
||
667 |
/** |
|
668 |
* Determine if a post exists based on title, content, and date |
|
669 |
* |
|
670 |
* @since 2.0.0 |
|
671 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
* |
0 | 674 |
* @param string $title Post title |
675 |
* @param string $content Optional post content |
|
676 |
* @param string $date Optional post date |
|
677 |
* @return int Post ID if post exists, 0 otherwise. |
|
678 |
*/ |
|
679 |
function post_exists($title, $content = '', $date = '') { |
|
680 |
global $wpdb; |
|
681 |
||
682 |
$post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); |
|
683 |
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); |
|
684 |
$post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) ); |
|
685 |
||
686 |
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; |
|
687 |
$args = array(); |
|
688 |
||
689 |
if ( !empty ( $date ) ) { |
|
690 |
$query .= ' AND post_date = %s'; |
|
691 |
$args[] = $post_date; |
|
692 |
} |
|
693 |
||
694 |
if ( !empty ( $title ) ) { |
|
695 |
$query .= ' AND post_title = %s'; |
|
696 |
$args[] = $post_title; |
|
697 |
} |
|
698 |
||
699 |
if ( !empty ( $content ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
$query .= ' AND post_content = %s'; |
0 | 701 |
$args[] = $post_content; |
702 |
} |
|
703 |
||
704 |
if ( !empty ( $args ) ) |
|
705 |
return (int) $wpdb->get_var( $wpdb->prepare($query, $args) ); |
|
706 |
||
707 |
return 0; |
|
708 |
} |
|
709 |
||
710 |
/** |
|
711 |
* Creates a new post from the "Write Post" form using $_POST information. |
|
712 |
* |
|
713 |
* @since 2.1.0 |
|
714 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
* @global WP_User $current_user |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
* |
5 | 717 |
* @return int|WP_Error |
0 | 718 |
*/ |
719 |
function wp_write_post() { |
|
720 |
if ( isset($_POST['post_type']) ) |
|
721 |
$ptype = get_post_type_object($_POST['post_type']); |
|
722 |
else |
|
723 |
$ptype = get_post_type_object('post'); |
|
724 |
||
725 |
if ( !current_user_can( $ptype->cap->edit_posts ) ) { |
|
726 |
if ( 'page' == $ptype->name ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) ); |
0 | 728 |
else |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) ); |
0 | 730 |
} |
731 |
||
732 |
$_POST['post_mime_type'] = ''; |
|
733 |
||
734 |
// Clear out any data in internal vars. |
|
735 |
unset( $_POST['filter'] ); |
|
736 |
||
737 |
// Edit don't write if we have a post id. |
|
738 |
if ( isset( $_POST['post_ID'] ) ) |
|
739 |
return edit_post(); |
|
740 |
||
741 |
if ( isset($_POST['visibility']) ) { |
|
742 |
switch ( $_POST['visibility'] ) { |
|
743 |
case 'public' : |
|
744 |
$_POST['post_password'] = ''; |
|
745 |
break; |
|
746 |
case 'password' : |
|
747 |
unset( $_POST['sticky'] ); |
|
748 |
break; |
|
749 |
case 'private' : |
|
750 |
$_POST['post_status'] = 'private'; |
|
751 |
$_POST['post_password'] = ''; |
|
752 |
unset( $_POST['sticky'] ); |
|
753 |
break; |
|
754 |
} |
|
755 |
} |
|
756 |
||
5 | 757 |
$translated = _wp_translate_postdata( false ); |
758 |
if ( is_wp_error($translated) ) |
|
759 |
return $translated; |
|
760 |
||
0 | 761 |
// Create the post. |
762 |
$post_ID = wp_insert_post( $_POST ); |
|
763 |
if ( is_wp_error( $post_ID ) ) |
|
764 |
return $post_ID; |
|
765 |
||
766 |
if ( empty($post_ID) ) |
|
767 |
return 0; |
|
768 |
||
769 |
add_meta( $post_ID ); |
|
770 |
||
771 |
add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID ); |
|
772 |
||
773 |
// Now that we have an ID we can fix any attachment anchor hrefs |
|
774 |
_fix_attachment_links( $post_ID ); |
|
775 |
||
776 |
wp_set_post_lock( $post_ID ); |
|
777 |
||
778 |
return $post_ID; |
|
779 |
} |
|
780 |
||
781 |
/** |
|
782 |
* Calls wp_write_post() and handles the errors. |
|
783 |
* |
|
784 |
* @since 2.0.0 |
|
5 | 785 |
* |
786 |
* @return int|null |
|
0 | 787 |
*/ |
788 |
function write_post() { |
|
789 |
$result = wp_write_post(); |
|
790 |
if ( is_wp_error( $result ) ) |
|
791 |
wp_die( $result->get_error_message() ); |
|
792 |
else |
|
793 |
return $result; |
|
794 |
} |
|
795 |
||
796 |
// |
|
797 |
// Post Meta |
|
798 |
// |
|
799 |
||
800 |
/** |
|
5 | 801 |
* Add post meta data defined in $_POST superglobal for post with given ID. |
0 | 802 |
* |
803 |
* @since 1.2.0 |
|
804 |
* |
|
5 | 805 |
* @param int $post_ID |
806 |
* @return int|bool |
|
0 | 807 |
*/ |
808 |
function add_meta( $post_ID ) { |
|
809 |
$post_ID = (int) $post_ID; |
|
810 |
||
811 |
$metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : ''; |
|
812 |
$metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : ''; |
|
813 |
$metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : ''; |
|
814 |
if ( is_string( $metavalue ) ) |
|
815 |
$metavalue = trim( $metavalue ); |
|
816 |
||
817 |
if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) { |
|
5 | 818 |
/* |
819 |
* We have a key/value pair. If both the select and the input |
|
820 |
* for the key have data, the input takes precedence. |
|
821 |
*/ |
|
0 | 822 |
if ( '#NONE#' != $metakeyselect ) |
823 |
$metakey = $metakeyselect; |
|
824 |
||
825 |
if ( $metakeyinput ) |
|
826 |
$metakey = $metakeyinput; // default |
|
827 |
||
828 |
if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) |
|
829 |
return false; |
|
830 |
||
831 |
$metakey = wp_slash( $metakey ); |
|
832 |
||
833 |
return add_post_meta( $post_ID, $metakey, $metavalue ); |
|
834 |
} |
|
835 |
||
836 |
return false; |
|
837 |
} // add_meta |
|
838 |
||
839 |
/** |
|
5 | 840 |
* Delete post meta data by meta ID. |
0 | 841 |
* |
842 |
* @since 1.2.0 |
|
843 |
* |
|
5 | 844 |
* @param int $mid |
845 |
* @return bool |
|
0 | 846 |
*/ |
847 |
function delete_meta( $mid ) { |
|
848 |
return delete_metadata_by_mid( 'post' , $mid ); |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Get a list of previously defined keys. |
|
853 |
* |
|
854 |
* @since 1.2.0 |
|
855 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
* |
5 | 858 |
* @return mixed |
0 | 859 |
*/ |
860 |
function get_meta_keys() { |
|
861 |
global $wpdb; |
|
862 |
||
863 |
$keys = $wpdb->get_col( " |
|
864 |
SELECT meta_key |
|
865 |
FROM $wpdb->postmeta |
|
866 |
GROUP BY meta_key |
|
867 |
ORDER BY meta_key" ); |
|
868 |
||
869 |
return $keys; |
|
870 |
} |
|
871 |
||
872 |
/** |
|
5 | 873 |
* Get post meta data by meta ID. |
0 | 874 |
* |
875 |
* @since 2.1.0 |
|
876 |
* |
|
5 | 877 |
* @param int $mid |
878 |
* @return object|bool |
|
0 | 879 |
*/ |
880 |
function get_post_meta_by_id( $mid ) { |
|
881 |
return get_metadata_by_mid( 'post', $mid ); |
|
882 |
} |
|
883 |
||
884 |
/** |
|
5 | 885 |
* Get meta data for the given post ID. |
0 | 886 |
* |
887 |
* @since 1.2.0 |
|
888 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
889 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
* |
5 | 891 |
* @param int $postid |
892 |
* @return mixed |
|
0 | 893 |
*/ |
894 |
function has_meta( $postid ) { |
|
895 |
global $wpdb; |
|
896 |
||
897 |
return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id |
|
898 |
FROM $wpdb->postmeta WHERE post_id = %d |
|
899 |
ORDER BY meta_key,meta_id", $postid), ARRAY_A ); |
|
900 |
} |
|
901 |
||
902 |
/** |
|
5 | 903 |
* Update post meta data by meta ID. |
0 | 904 |
* |
905 |
* @since 1.2.0 |
|
906 |
* |
|
5 | 907 |
* @param int $meta_id |
908 |
* @param string $meta_key Expect Slashed |
|
909 |
* @param string $meta_value Expect Slashed |
|
910 |
* @return bool |
|
0 | 911 |
*/ |
912 |
function update_meta( $meta_id, $meta_key, $meta_value ) { |
|
913 |
$meta_key = wp_unslash( $meta_key ); |
|
914 |
$meta_value = wp_unslash( $meta_value ); |
|
915 |
||
916 |
return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key ); |
|
917 |
} |
|
918 |
||
919 |
// |
|
920 |
// Private |
|
921 |
// |
|
922 |
||
923 |
/** |
|
924 |
* Replace hrefs of attachment anchors with up-to-date permalinks. |
|
925 |
* |
|
926 |
* @since 2.3.0 |
|
927 |
* @access private |
|
928 |
* |
|
929 |
* @param int|object $post Post ID or post object. |
|
930 |
* @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success. |
|
931 |
*/ |
|
932 |
function _fix_attachment_links( $post ) { |
|
933 |
$post = get_post( $post, ARRAY_A ); |
|
934 |
$content = $post['post_content']; |
|
935 |
||
936 |
// Don't run if no pretty permalinks or post is not published, scheduled, or privately published. |
|
937 |
if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) ) |
|
938 |
return; |
|
939 |
||
940 |
// Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero) |
|
941 |
if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) |
|
942 |
return; |
|
943 |
||
944 |
$site_url = get_bloginfo('url'); |
|
945 |
$site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s) |
|
946 |
$replace = ''; |
|
947 |
||
948 |
foreach ( $link_matches[1] as $key => $value ) { |
|
949 |
if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-') |
|
950 |
|| !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match ) |
|
951 |
|| !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) |
|
952 |
continue; |
|
953 |
||
954 |
$quote = $url_match[1]; // the quote (single or double) |
|
955 |
$url_id = (int) $url_match[2]; |
|
956 |
$rel_id = (int) $rel_match[1]; |
|
957 |
||
958 |
if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false ) |
|
959 |
continue; |
|
960 |
||
961 |
$link = $link_matches[0][$key]; |
|
962 |
$replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link ); |
|
963 |
||
964 |
$content = str_replace( $link, $replace, $content ); |
|
965 |
} |
|
966 |
||
967 |
if ( $replace ) { |
|
968 |
$post['post_content'] = $content; |
|
969 |
// Escape data pulled from DB. |
|
970 |
$post = add_magic_quotes($post); |
|
971 |
||
972 |
return wp_update_post($post); |
|
973 |
} |
|
974 |
} |
|
975 |
||
976 |
/** |
|
977 |
* Get all the possible statuses for a post_type |
|
978 |
* |
|
979 |
* @since 2.5.0 |
|
980 |
* |
|
981 |
* @param string $type The post_type you want the statuses for |
|
982 |
* @return array As array of all the statuses for the supplied post type |
|
983 |
*/ |
|
984 |
function get_available_post_statuses($type = 'post') { |
|
985 |
$stati = wp_count_posts($type); |
|
986 |
||
987 |
return array_keys(get_object_vars($stati)); |
|
988 |
} |
|
989 |
||
990 |
/** |
|
991 |
* Run the wp query to fetch the posts for listing on the edit posts page |
|
992 |
* |
|
993 |
* @since 2.5.0 |
|
994 |
* |
|
995 |
* @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal. |
|
996 |
* @return array |
|
997 |
*/ |
|
998 |
function wp_edit_posts_query( $q = false ) { |
|
999 |
if ( false === $q ) |
|
1000 |
$q = $_GET; |
|
1001 |
$q['m'] = isset($q['m']) ? (int) $q['m'] : 0; |
|
1002 |
$q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0; |
|
1003 |
$post_stati = get_post_stati(); |
|
1004 |
||
1005 |
if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) ) |
|
1006 |
$post_type = $q['post_type']; |
|
1007 |
else |
|
1008 |
$post_type = 'post'; |
|
1009 |
||
1010 |
$avail_post_stati = get_available_post_statuses($post_type); |
|
1011 |
||
1012 |
if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) { |
|
1013 |
$post_status = $q['post_status']; |
|
1014 |
$perm = 'readable'; |
|
1015 |
} |
|
1016 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1017 |
if ( isset( $q['orderby'] ) ) { |
0 | 1018 |
$orderby = $q['orderby']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
} elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) { |
0 | 1020 |
$orderby = 'modified'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1021 |
} |
0 | 1022 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1023 |
if ( isset( $q['order'] ) ) { |
0 | 1024 |
$order = $q['order']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1025 |
} elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) { |
0 | 1026 |
$order = 'ASC'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1027 |
} |
0 | 1028 |
|
5 | 1029 |
$per_page = "edit_{$post_type}_per_page"; |
0 | 1030 |
$posts_per_page = (int) get_user_option( $per_page ); |
1031 |
if ( empty( $posts_per_page ) || $posts_per_page < 1 ) |
|
1032 |
$posts_per_page = 20; |
|
1033 |
||
5 | 1034 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1035 |
* Filters the number of items per page to show for a specific 'per_page' type. |
5 | 1036 |
* |
1037 |
* The dynamic portion of the hook name, `$post_type`, refers to the post type. |
|
1038 |
* |
|
1039 |
* Some examples of filter hooks generated here include: 'edit_attachment_per_page', |
|
1040 |
* 'edit_post_per_page', 'edit_page_per_page', etc. |
|
1041 |
* |
|
1042 |
* @since 3.0.0 |
|
1043 |
* |
|
1044 |
* @param int $posts_per_page Number of posts to display per page for the given post |
|
1045 |
* type. Default 20. |
|
1046 |
*/ |
|
1047 |
$posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page ); |
|
1048 |
||
1049 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1050 |
* Filters the number of posts displayed per page when specifically listing "posts". |
5 | 1051 |
* |
1052 |
* @since 2.8.0 |
|
1053 |
* |
|
1054 |
* @param int $posts_per_page Number of posts to be displayed. Default 20. |
|
1055 |
* @param string $post_type The post type. |
|
1056 |
*/ |
|
0 | 1057 |
$posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); |
1058 |
||
1059 |
$query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page'); |
|
1060 |
||
1061 |
// Hierarchical types require special args. |
|
1062 |
if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) { |
|
1063 |
$query['orderby'] = 'menu_order title'; |
|
1064 |
$query['order'] = 'asc'; |
|
1065 |
$query['posts_per_page'] = -1; |
|
1066 |
$query['posts_per_archive_page'] = -1; |
|
5 | 1067 |
$query['fields'] = 'id=>parent'; |
0 | 1068 |
} |
1069 |
||
1070 |
if ( ! empty( $q['show_sticky'] ) ) |
|
1071 |
$query['post__in'] = (array) get_option( 'sticky_posts' ); |
|
1072 |
||
1073 |
wp( $query ); |
|
1074 |
||
1075 |
return $avail_post_stati; |
|
1076 |
} |
|
1077 |
||
1078 |
/** |
|
5 | 1079 |
* Get all available post MIME types for a given post type. |
0 | 1080 |
* |
1081 |
* @since 2.5.0 |
|
1082 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1083 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1084 |
* |
5 | 1085 |
* @param string $type |
1086 |
* @return mixed |
|
0 | 1087 |
*/ |
1088 |
function get_available_post_mime_types($type = 'attachment') { |
|
1089 |
global $wpdb; |
|
1090 |
||
1091 |
$types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type)); |
|
1092 |
return $types; |
|
1093 |
} |
|
1094 |
||
1095 |
/** |
|
5 | 1096 |
* Get the query variables for the current attachments request. |
0 | 1097 |
* |
5 | 1098 |
* @since 4.2.0 |
0 | 1099 |
* |
5 | 1100 |
* @param array|false $q Optional. Array of query variables to use to build the query or false |
1101 |
* to use $_GET superglobal. Default false. |
|
1102 |
* @return array The parsed query vars. |
|
0 | 1103 |
*/ |
5 | 1104 |
function wp_edit_attachments_query_vars( $q = false ) { |
1105 |
if ( false === $q ) { |
|
0 | 1106 |
$q = $_GET; |
5 | 1107 |
} |
0 | 1108 |
$q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; |
1109 |
$q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; |
|
1110 |
$q['post_type'] = 'attachment'; |
|
1111 |
$post_type = get_post_type_object( 'attachment' ); |
|
1112 |
$states = 'inherit'; |
|
5 | 1113 |
if ( current_user_can( $post_type->cap->read_private_posts ) ) { |
0 | 1114 |
$states .= ',private'; |
5 | 1115 |
} |
0 | 1116 |
|
1117 |
$q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states; |
|
5 | 1118 |
$q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' == $q['attachment-filter'] ? 'trash' : $states; |
1119 |
||
0 | 1120 |
$media_per_page = (int) get_user_option( 'upload_per_page' ); |
5 | 1121 |
if ( empty( $media_per_page ) || $media_per_page < 1 ) { |
0 | 1122 |
$media_per_page = 20; |
5 | 1123 |
} |
1124 |
||
1125 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
* Filters the number of items to list per page when listing media items. |
5 | 1127 |
* |
1128 |
* @since 2.9.0 |
|
1129 |
* |
|
1130 |
* @param int $media_per_page Number of media to list. Default 20. |
|
1131 |
*/ |
|
0 | 1132 |
$q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page ); |
1133 |
||
1134 |
$post_mime_types = get_post_mime_types(); |
|
5 | 1135 |
if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) { |
0 | 1136 |
unset($q['post_mime_type']); |
5 | 1137 |
} |
0 | 1138 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
foreach ( array_keys( $post_mime_types ) as $type ) { |
5 | 1140 |
if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) { |
1141 |
$q['post_mime_type'] = $type; |
|
1142 |
break; |
|
1143 |
} |
|
1144 |
} |
|
0 | 1145 |
|
5 | 1146 |
if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) { |
1147 |
$q['post_parent'] = 0; |
|
1148 |
} |
|
0 | 1149 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' == $q['attachment-filter'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
$q['author'] = get_current_user_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
// Filter query clauses to include filenames. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
if ( isset( $q['s'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
|
5 | 1159 |
return $q; |
0 | 1160 |
} |
1161 |
||
5 | 1162 |
/** |
1163 |
* Executes a query for attachments. An array of WP_Query arguments |
|
1164 |
* can be passed in, which will override the arguments set by this function. |
|
1165 |
* |
|
1166 |
* @since 2.5.0 |
|
1167 |
* |
|
1168 |
* @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal. |
|
1169 |
* @return array |
|
1170 |
*/ |
|
1171 |
function wp_edit_attachments_query( $q = false ) { |
|
1172 |
wp( wp_edit_attachments_query_vars( $q ) ); |
|
1173 |
||
1174 |
$post_mime_types = get_post_mime_types(); |
|
1175 |
$avail_post_mime_types = get_available_post_mime_types( 'attachment' ); |
|
1176 |
||
1177 |
return array( $post_mime_types, $avail_post_mime_types ); |
|
0 | 1178 |
} |
1179 |
||
1180 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
* Returns the list of classes to be used by a meta box. |
0 | 1182 |
* |
1183 |
* @since 2.5.0 |
|
1184 |
* |
|
5 | 1185 |
* @param string $id |
1186 |
* @param string $page |
|
1187 |
* @return string |
|
0 | 1188 |
*/ |
1189 |
function postbox_classes( $id, $page ) { |
|
1190 |
if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) { |
|
1191 |
$classes = array( '' ); |
|
1192 |
} elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) { |
|
1193 |
if ( !is_array( $closed ) ) { |
|
1194 |
$classes = array( '' ); |
|
1195 |
} else { |
|
1196 |
$classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' ); |
|
1197 |
} |
|
1198 |
} else { |
|
1199 |
$classes = array( '' ); |
|
1200 |
} |
|
1201 |
||
5 | 1202 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
* Filters the postbox classes for a specific screen and screen ID combo. |
5 | 1204 |
* |
1205 |
* The dynamic portions of the hook name, `$page` and `$id`, refer to |
|
1206 |
* the screen and screen ID, respectively. |
|
1207 |
* |
|
1208 |
* @since 3.2.0 |
|
1209 |
* |
|
1210 |
* @param array $classes An array of postbox classes. |
|
1211 |
*/ |
|
0 | 1212 |
$classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes ); |
1213 |
return implode( ' ', $classes ); |
|
1214 |
} |
|
1215 |
||
1216 |
/** |
|
5 | 1217 |
* Get a sample permalink based off of the post name. |
0 | 1218 |
* |
1219 |
* @since 2.5.0 |
|
1220 |
* |
|
5 | 1221 |
* @param int $id Post ID or post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
* @param string $title Optional. Title to override the post's current title when generating the post name. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
* @param string $name Optional. Name to override the post name. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1224 |
* @return array Array containing the sample permalink with placeholder for the post name, and the post name. |
0 | 1225 |
*/ |
1226 |
function get_sample_permalink($id, $title = null, $name = null) { |
|
1227 |
$post = get_post( $id ); |
|
1228 |
if ( ! $post ) |
|
1229 |
return array( '', '' ); |
|
1230 |
||
1231 |
$ptype = get_post_type_object($post->post_type); |
|
1232 |
||
1233 |
$original_status = $post->post_status; |
|
1234 |
$original_date = $post->post_date; |
|
1235 |
$original_name = $post->post_name; |
|
1236 |
||
1237 |
// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. |
|
5 | 1238 |
if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) { |
0 | 1239 |
$post->post_status = 'publish'; |
1240 |
$post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID); |
|
1241 |
} |
|
1242 |
||
1243 |
// If the user wants to set a new name -- override the current one |
|
1244 |
// Note: if empty name is supplied -- use the title instead, see #6072 |
|
1245 |
if ( !is_null($name) ) |
|
1246 |
$post->post_name = sanitize_title($name ? $name : $title, $post->ID); |
|
1247 |
||
1248 |
$post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent); |
|
1249 |
||
1250 |
$post->filter = 'sample'; |
|
1251 |
||
1252 |
$permalink = get_permalink($post, true); |
|
1253 |
||
1254 |
// Replace custom post_type Token with generic pagename token for ease of use. |
|
1255 |
$permalink = str_replace("%$post->post_type%", '%pagename%', $permalink); |
|
1256 |
||
1257 |
// Handle page hierarchy |
|
1258 |
if ( $ptype->hierarchical ) { |
|
1259 |
$uri = get_page_uri($post); |
|
5 | 1260 |
if ( $uri ) { |
1261 |
$uri = untrailingslashit($uri); |
|
1262 |
$uri = strrev( stristr( strrev( $uri ), '/' ) ); |
|
1263 |
$uri = untrailingslashit($uri); |
|
1264 |
} |
|
1265 |
||
1266 |
/** This filter is documented in wp-admin/edit-tag-form.php */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
$uri = apply_filters( 'editable_slug', $uri, $post ); |
0 | 1268 |
if ( !empty($uri) ) |
1269 |
$uri .= '/'; |
|
1270 |
$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); |
|
1271 |
} |
|
1272 |
||
5 | 1273 |
/** This filter is documented in wp-admin/edit-tag-form.php */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
$permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) ); |
0 | 1275 |
$post->post_status = $original_status; |
1276 |
$post->post_date = $original_date; |
|
1277 |
$post->post_name = $original_name; |
|
1278 |
unset($post->filter); |
|
1279 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
* Filters the sample permalink. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1285 |
* @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
* @param string $title Post title. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
* @param string $name Post name (slug). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
* @param WP_Post $post Post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post ); |
0 | 1292 |
} |
1293 |
||
1294 |
/** |
|
1295 |
* Returns the HTML of the sample permalink slug editor. |
|
1296 |
* |
|
1297 |
* @since 2.5.0 |
|
1298 |
* |
|
5 | 1299 |
* @param int $id Post ID or post object. |
1300 |
* @param string $new_title Optional. New title. Default null. |
|
1301 |
* @param string $new_slug Optional. New slug. Default null. |
|
0 | 1302 |
* @return string The HTML of the sample permalink slug editor. |
1303 |
*/ |
|
1304 |
function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { |
|
1305 |
$post = get_post( $id ); |
|
1306 |
if ( ! $post ) |
|
1307 |
return ''; |
|
1308 |
||
1309 |
list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); |
|
1310 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1311 |
$view_link = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
$preview_target = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
|
5 | 1314 |
if ( current_user_can( 'read_post', $post->ID ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1315 |
if ( 'draft' === $post->post_status || empty( $post->post_name ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
$view_link = get_preview_post_link( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
$preview_target = " target='wp-preview-{$post->ID}'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1319 |
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1320 |
$view_link = get_permalink( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
// Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
} |
5 | 1326 |
} |
1327 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
// Permalinks without a post/page name placeholder don't have anything to edit |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1329 |
if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1330 |
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
0 | 1331 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1332 |
if ( false !== $view_link ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
$display_link = urldecode( $view_link ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1334 |
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
$return .= '<span id="sample-permalink">' . $permalink . "</span>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
// Encourage a pretty permalink setting |
5 | 1340 |
if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) { |
0 | 1341 |
$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; |
5 | 1342 |
} |
1343 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
if ( mb_strlen( $post_name ) > 34 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 ); |
5 | 1346 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1347 |
$post_name_abridged = $post_name; |
5 | 1348 |
} |
0 | 1349 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
$post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) ); |
0 | 1352 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
$return .= '‎'; // Fix bi-directional text display defect in RTL languages. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
$return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
$return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n"; |
0 | 1358 |
} |
1359 |
||
5 | 1360 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
* Filters the sample permalink HTML markup. |
5 | 1362 |
* |
1363 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
* @since 4.4.0 Added `$post` parameter. |
5 | 1365 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* @param string $return Sample permalink HTML markup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
* @param string $new_title New sample permalink title. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
* @param string $new_slug New sample permalink slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
* @param WP_Post $post Post object. |
5 | 1371 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
$return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post ); |
0 | 1373 |
|
1374 |
return $return; |
|
1375 |
} |
|
1376 |
||
1377 |
/** |
|
1378 |
* Output HTML for the post thumbnail meta-box. |
|
1379 |
* |
|
1380 |
* @since 2.9.0 |
|
1381 |
* |
|
1382 |
* @param int $thumbnail_id ID of the attachment used for thumbnail |
|
1383 |
* @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post. |
|
1384 |
* @return string html |
|
1385 |
*/ |
|
1386 |
function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
$_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
0 | 1388 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
$post_type_object = get_post_type_object( $post->post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
$set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
$upload_iframe_src = get_upload_iframe_src( 'image', $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
$content = sprintf( $set_thumbnail_link, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
esc_url( $upload_iframe_src ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
'', // Empty when there's no featured image set, `aria-describedby` attribute otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
esc_html( $post_type_object->labels->set_featured_image ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
); |
0 | 1399 |
|
1400 |
if ( $thumbnail_id && get_post( $thumbnail_id ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
$size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
* Filters the size used to display the post thumbnail image in the 'Featured Image' meta box. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
* Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
* image size is registered, which differs from the 'thumbnail' image size |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
* managed via the Settings > Media screen. See the `$size` parameter description |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
* for more information on default values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
* @param string|array $size Post thumbnail image size to display in the meta box. Accepts any valid |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
* image size, or an array of width and height values in pixels (in that order). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
* If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
* default is an array with 266 as both the height and width values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
* @param int $thumbnail_id Post thumbnail attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
* @param WP_Post $post The post object associated with the thumbnail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
$size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
if ( ! empty( $thumbnail_html ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
$content = sprintf( $set_thumbnail_link, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
esc_url( $upload_iframe_src ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
' aria-describedby="set-post-thumbnail-desc"', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
$thumbnail_html |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
$content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>'; |
0 | 1432 |
} |
1433 |
} |
|
1434 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
$content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
|
5 | 1437 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
* Filters the admin post thumbnail HTML markup to return. |
5 | 1439 |
* |
1440 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
* @since 3.5.0 Added the `$post_id` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1442 |
* @since 4.6.0 Added the `$thumbnail_id` parameter. |
5 | 1443 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1444 |
* @param string $content Admin post thumbnail HTML markup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
* @param int $thumbnail_id Thumbnail ID. |
5 | 1447 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1448 |
return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id ); |
0 | 1449 |
} |
1450 |
||
1451 |
/** |
|
1452 |
* Check to see if the post is currently being edited by another user. |
|
1453 |
* |
|
1454 |
* @since 2.5.0 |
|
1455 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1456 |
* @param int $post_id ID of the post to check for editing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1457 |
* @return int|false ID of the user with lock. False if the post does not exist, post is not locked, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
* the user with lock does not exist, or the post is locked by current user. |
0 | 1459 |
*/ |
1460 |
function wp_check_post_lock( $post_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
if ( ! $post = get_post( $post_id ) ) { |
0 | 1462 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1463 |
} |
0 | 1464 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1465 |
if ( ! $lock = get_post_meta( $post->ID, '_edit_lock', true ) ) { |
0 | 1466 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1467 |
} |
0 | 1468 |
|
1469 |
$lock = explode( ':', $lock ); |
|
1470 |
$time = $lock[0]; |
|
1471 |
$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); |
|
1472 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1473 |
if ( ! get_userdata( $user ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1474 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1475 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1476 |
|
5 | 1477 |
/** This filter is documented in wp-admin/includes/ajax-actions.php */ |
1478 |
$time_window = apply_filters( 'wp_check_post_lock_window', 150 ); |
|
0 | 1479 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1480 |
if ( $time && $time > time() - $time_window && $user != get_current_user_id() ) { |
0 | 1481 |
return $user; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1482 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1483 |
|
0 | 1484 |
return false; |
1485 |
} |
|
1486 |
||
1487 |
/** |
|
1488 |
* Mark the post as currently being edited by the current user |
|
1489 |
* |
|
1490 |
* @since 2.5.0 |
|
1491 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
* @param int $post_id ID of the post being edited. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1493 |
* @return array|false Array of the lock time and user ID. False if the post does not exist, or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1494 |
* there is no current user. |
0 | 1495 |
*/ |
1496 |
function wp_set_post_lock( $post_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1497 |
if ( ! $post = get_post( $post_id ) ) { |
0 | 1498 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1499 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1501 |
if ( 0 == ( $user_id = get_current_user_id() ) ) { |
0 | 1502 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1503 |
} |
0 | 1504 |
|
1505 |
$now = time(); |
|
1506 |
$lock = "$now:$user_id"; |
|
1507 |
||
1508 |
update_post_meta( $post->ID, '_edit_lock', $lock ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1509 |
|
0 | 1510 |
return array( $now, $user_id ); |
1511 |
} |
|
1512 |
||
1513 |
/** |
|
1514 |
* Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
|
1515 |
* |
|
1516 |
* @since 2.8.5 |
|
1517 |
* @return none |
|
1518 |
*/ |
|
1519 |
function _admin_notice_post_locked() { |
|
1520 |
if ( ! $post = get_post() ) |
|
1521 |
return; |
|
1522 |
||
1523 |
$user = null; |
|
1524 |
if ( $user_id = wp_check_post_lock( $post->ID ) ) |
|
1525 |
$user = get_userdata( $user_id ); |
|
1526 |
||
1527 |
if ( $user ) { |
|
5 | 1528 |
|
1529 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
* Filters whether to show the post locked dialog. |
5 | 1531 |
* |
1532 |
* Returning a falsey value to the filter will short-circuit displaying the dialog. |
|
1533 |
* |
|
1534 |
* @since 3.6.0 |
|
1535 |
* |
|
1536 |
* @param bool $display Whether to display the dialog. Default true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
* @param WP_Post $post Post object. |
5 | 1538 |
* @param WP_User|bool $user WP_User object on success, false otherwise. |
1539 |
*/ |
|
0 | 1540 |
if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) |
1541 |
return; |
|
1542 |
||
1543 |
$locked = true; |
|
1544 |
} else { |
|
1545 |
$locked = false; |
|
1546 |
} |
|
1547 |
||
1548 |
if ( $locked && ( $sendback = wp_get_referer() ) && |
|
1549 |
false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) { |
|
1550 |
||
1551 |
$sendback_text = __('Go back'); |
|
1552 |
} else { |
|
1553 |
$sendback = admin_url( 'edit.php' ); |
|
1554 |
||
1555 |
if ( 'post' != $post->post_type ) |
|
1556 |
$sendback = add_query_arg( 'post_type', $post->post_type, $sendback ); |
|
1557 |
||
1558 |
$sendback_text = get_post_type_object( $post->post_type )->labels->all_items; |
|
1559 |
} |
|
1560 |
||
1561 |
$hidden = $locked ? '' : ' hidden'; |
|
1562 |
||
1563 |
?> |
|
1564 |
<div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>"> |
|
1565 |
<div class="notification-dialog-background"></div> |
|
1566 |
<div class="notification-dialog"> |
|
1567 |
<?php |
|
1568 |
||
1569 |
if ( $locked ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1570 |
$query_args = array(); |
0 | 1571 |
if ( get_post_type_object( $post->post_type )->public ) { |
1572 |
if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) { |
|
1573 |
// Latest content is in autosave |
|
1574 |
$nonce = wp_create_nonce( 'post_preview_' . $post->ID ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
$query_args['preview_id'] = $post->ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1576 |
$query_args['preview_nonce'] = $nonce; |
0 | 1577 |
} |
1578 |
} |
|
1579 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
$preview_link = get_preview_post_link( $post->ID, $query_args ); |
5 | 1581 |
|
1582 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
* Filters whether to allow the post lock to be overridden. |
5 | 1584 |
* |
1585 |
* Returning a falsey value to the filter will disable the ability |
|
1586 |
* to override the post lock. |
|
1587 |
* |
|
1588 |
* @since 3.6.0 |
|
1589 |
* |
|
1590 |
* @param bool $override Whether to allow overriding post locks. Default true. |
|
1591 |
* @param WP_Post $post Post object. |
|
1592 |
* @param WP_User $user User object. |
|
1593 |
*/ |
|
0 | 1594 |
$override = apply_filters( 'override_post_lock', true, $post, $user ); |
1595 |
$tab_last = $override ? '' : ' wp-tab-last'; |
|
1596 |
||
1597 |
?> |
|
1598 |
<div class="post-locked-message"> |
|
1599 |
<div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div> |
|
1600 |
<p class="currently-editing wp-tab-first" tabindex="0"> |
|
1601 |
<?php |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
if ( $override ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1603 |
/* translators: %s: user's display name */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1604 |
printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1605 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1606 |
/* translators: %s: user's display name */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1607 |
printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1608 |
} |
0 | 1609 |
?> |
1610 |
</p> |
|
5 | 1611 |
<?php |
1612 |
/** |
|
1613 |
* Fires inside the post locked dialog before the buttons are displayed. |
|
1614 |
* |
|
1615 |
* @since 3.6.0 |
|
1616 |
* |
|
1617 |
* @param WP_Post $post Post object. |
|
1618 |
*/ |
|
1619 |
do_action( 'post_locked_dialog', $post ); |
|
1620 |
?> |
|
0 | 1621 |
<p> |
1622 |
<a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a> |
|
1623 |
<?php if ( $preview_link ) { ?> |
|
1624 |
<a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a> |
|
1625 |
<?php |
|
1626 |
} |
|
1627 |
||
1628 |
// Allow plugins to prevent some users overriding the post lock |
|
1629 |
if ( $override ) { |
|
1630 |
?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1631 |
<a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e('Take over'); ?></a> |
0 | 1632 |
<?php |
1633 |
} |
|
1634 |
||
1635 |
?> |
|
1636 |
</p> |
|
1637 |
</div> |
|
1638 |
<?php |
|
1639 |
} else { |
|
1640 |
?> |
|
1641 |
<div class="post-taken-over"> |
|
1642 |
<div class="post-locked-avatar"></div> |
|
1643 |
<p class="wp-tab-first" tabindex="0"> |
|
5 | 1644 |
<span class="currently-editing"></span><br /> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1645 |
<span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision…' ); ?></span> |
0 | 1646 |
<span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span> |
1647 |
</p> |
|
5 | 1648 |
<?php |
1649 |
/** |
|
1650 |
* Fires inside the dialog displayed when a user has lost the post lock. |
|
1651 |
* |
|
1652 |
* @since 3.6.0 |
|
1653 |
* |
|
1654 |
* @param WP_Post $post Post object. |
|
1655 |
*/ |
|
1656 |
do_action( 'post_lock_lost_dialog', $post ); |
|
1657 |
?> |
|
0 | 1658 |
<p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p> |
1659 |
</div> |
|
1660 |
<?php |
|
1661 |
} |
|
1662 |
||
1663 |
?> |
|
1664 |
</div> |
|
1665 |
</div> |
|
1666 |
<?php |
|
1667 |
} |
|
1668 |
||
1669 |
/** |
|
1670 |
* Creates autosave data for the specified post from $_POST data. |
|
1671 |
* |
|
1672 |
* @since 2.6.0 |
|
1673 |
* |
|
5 | 1674 |
* @param mixed $post_data Associative array containing the post data or int post ID. |
1675 |
* @return mixed The autosave revision ID. WP_Error or 0 on error. |
|
0 | 1676 |
*/ |
5 | 1677 |
function wp_create_post_autosave( $post_data ) { |
1678 |
if ( is_numeric( $post_data ) ) { |
|
1679 |
$post_id = $post_data; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1680 |
$post_data = $_POST; |
5 | 1681 |
} else { |
1682 |
$post_id = (int) $post_data['post_ID']; |
|
1683 |
} |
|
1684 |
||
1685 |
$post_data = _wp_translate_postdata( true, $post_data ); |
|
1686 |
if ( is_wp_error( $post_data ) ) |
|
1687 |
return $post_data; |
|
0 | 1688 |
|
1689 |
$post_author = get_current_user_id(); |
|
1690 |
||
1691 |
// Store one autosave per author. If there is already an autosave, overwrite it. |
|
1692 |
if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
$new_autosave = _wp_post_revision_data( $post_data, true ); |
0 | 1694 |
$new_autosave['ID'] = $old_autosave->ID; |
1695 |
$new_autosave['post_author'] = $post_author; |
|
1696 |
||
5 | 1697 |
// If the new autosave has the same content as the post, delete the autosave. |
0 | 1698 |
$post = get_post( $post_id ); |
1699 |
$autosave_is_different = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { |
0 | 1701 |
if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { |
1702 |
$autosave_is_different = true; |
|
1703 |
break; |
|
1704 |
} |
|
1705 |
} |
|
1706 |
||
1707 |
if ( ! $autosave_is_different ) { |
|
1708 |
wp_delete_post_revision( $old_autosave->ID ); |
|
5 | 1709 |
return 0; |
0 | 1710 |
} |
1711 |
||
5 | 1712 |
/** |
1713 |
* Fires before an autosave is stored. |
|
1714 |
* |
|
1715 |
* @since 4.1.0 |
|
1716 |
* |
|
1717 |
* @param array $new_autosave Post array - the autosave that is about to be saved. |
|
1718 |
*/ |
|
1719 |
do_action( 'wp_creating_autosave', $new_autosave ); |
|
1720 |
||
0 | 1721 |
return wp_update_post( $new_autosave ); |
1722 |
} |
|
1723 |
||
1724 |
// _wp_put_post_revision() expects unescaped. |
|
5 | 1725 |
$post_data = wp_unslash( $post_data ); |
0 | 1726 |
|
1727 |
// Otherwise create the new autosave as a special post revision |
|
1728 |
return _wp_put_post_revision( $post_data, true ); |
|
1729 |
} |
|
1730 |
||
1731 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1732 |
* Saves a draft or manually autosaves for the purpose of showing a post preview. |
0 | 1733 |
* |
1734 |
* @since 2.7.0 |
|
1735 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1736 |
* @return string URL to redirect to show the preview. |
0 | 1737 |
*/ |
1738 |
function post_preview() { |
|
1739 |
||
1740 |
$post_ID = (int) $_POST['post_ID']; |
|
5 | 1741 |
$_POST['ID'] = $post_ID; |
1742 |
||
1743 |
if ( ! $post = get_post( $post_ID ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1744 |
wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
5 | 1745 |
} |
0 | 1746 |
|
5 | 1747 |
if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
5 | 1749 |
} |
0 | 1750 |
|
5 | 1751 |
$is_autosave = false; |
1752 |
||
1753 |
if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) { |
|
1754 |
$saved_post_id = edit_post(); |
|
1755 |
} else { |
|
1756 |
$is_autosave = true; |
|
0 | 1757 |
|
5 | 1758 |
if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] ) |
1759 |
$_POST['post_status'] = 'draft'; |
|
0 | 1760 |
|
5 | 1761 |
$saved_post_id = wp_create_post_autosave( $post->ID ); |
1762 |
} |
|
1763 |
||
1764 |
if ( is_wp_error( $saved_post_id ) ) |
|
1765 |
wp_die( $saved_post_id->get_error_message() ); |
|
0 | 1766 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1767 |
$query_args = array(); |
5 | 1768 |
|
1769 |
if ( $is_autosave && $saved_post_id ) { |
|
1770 |
$query_args['preview_id'] = $post->ID; |
|
1771 |
$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID ); |
|
1772 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
if ( isset( $_POST['post_format'] ) ) { |
5 | 1774 |
$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1776 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1777 |
if ( isset( $_POST['_thumbnail_id'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1778 |
$query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1779 |
} |
0 | 1780 |
} |
1781 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1782 |
return get_preview_post_link( $post, $query_args ); |
5 | 1783 |
} |
1784 |
||
1785 |
/** |
|
1786 |
* Save a post submitted with XHR |
|
1787 |
* |
|
1788 |
* Intended for use with heartbeat and autosave.js |
|
1789 |
* |
|
1790 |
* @since 3.9.0 |
|
1791 |
* |
|
1792 |
* @param array $post_data Associative array of the submitted post data. |
|
1793 |
* @return mixed The value 0 or WP_Error on failure. The saved post ID on success. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1794 |
* The ID can be the draft post_id or the autosave revision post_id. |
5 | 1795 |
*/ |
1796 |
function wp_autosave( $post_data ) { |
|
1797 |
// Back-compat |
|
1798 |
if ( ! defined( 'DOING_AUTOSAVE' ) ) |
|
1799 |
define( 'DOING_AUTOSAVE', true ); |
|
1800 |
||
1801 |
$post_id = (int) $post_data['post_id']; |
|
1802 |
$post_data['ID'] = $post_data['post_ID'] = $post_id; |
|
1803 |
||
1804 |
if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { |
|
1805 |
return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); |
|
0 | 1806 |
} |
1807 |
||
5 | 1808 |
$post = get_post( $post_id ); |
0 | 1809 |
|
5 | 1810 |
if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1811 |
return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) ); |
0 | 1812 |
} |
1813 |
||
5 | 1814 |
if ( 'auto-draft' == $post->post_status ) |
1815 |
$post_data['post_status'] = 'draft'; |
|
1816 |
||
1817 |
if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) ) |
|
1818 |
$post_data['post_category'] = explode( ',', $post_data['catslist'] ); |
|
1819 |
||
1820 |
if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) { |
|
1821 |
// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked |
|
1822 |
return edit_post( wp_slash( $post_data ) ); |
|
1823 |
} else { |
|
1824 |
// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. |
|
1825 |
return wp_create_post_autosave( wp_slash( $post_data ) ); |
|
1826 |
} |
|
0 | 1827 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1828 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1829 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1830 |
* Redirect to previous page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1831 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1832 |
* @param int $post_id Optional. Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1833 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1834 |
function redirect_post($post_id = '') { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1835 |
if ( isset($_POST['save']) || isset($_POST['publish']) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1836 |
$status = get_post_status( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1837 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1838 |
if ( isset( $_POST['publish'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1839 |
switch ( $status ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1840 |
case 'pending': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1841 |
$message = 8; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1842 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1843 |
case 'future': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1844 |
$message = 9; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1845 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
default: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1847 |
$message = 6; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1848 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1849 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1850 |
$message = 'draft' == $status ? 10 : 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1851 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1852 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1853 |
$location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1854 |
} elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1855 |
$location = add_query_arg( 'message', 2, wp_get_referer() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1856 |
$location = explode('#', $location); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1857 |
$location = $location[0] . '#postcustom'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1858 |
} elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1859 |
$location = add_query_arg( 'message', 3, wp_get_referer() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1860 |
$location = explode('#', $location); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1861 |
$location = $location[0] . '#postcustom'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1862 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1863 |
$location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1864 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1865 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1866 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1867 |
* Filters the post redirect destination URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1868 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1869 |
* @since 2.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1870 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1871 |
* @param string $location The destination URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1872 |
* @param int $post_id The post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1873 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1874 |
wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1875 |
exit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1876 |
} |