28 } |
28 } |
29 |
29 |
30 $ptype = get_post_type_object( $post_data['post_type'] ); |
30 $ptype = get_post_type_object( $post_data['post_type'] ); |
31 |
31 |
32 if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { |
32 if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { |
33 if ( 'page' == $post_data['post_type'] ) { |
33 if ( 'page' === $post_data['post_type'] ) { |
34 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
34 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
35 } else { |
35 } else { |
36 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
36 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
37 } |
37 } |
38 } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { |
38 } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { |
39 if ( 'page' == $post_data['post_type'] ) { |
39 if ( 'page' === $post_data['post_type'] ) { |
40 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
40 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
41 } else { |
41 } else { |
42 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
42 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
43 } |
43 } |
44 } |
44 } |
73 |
73 |
74 if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) |
74 if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) |
75 && ! current_user_can( $ptype->cap->edit_others_posts ) ) { |
75 && ! current_user_can( $ptype->cap->edit_others_posts ) ) { |
76 |
76 |
77 if ( $update ) { |
77 if ( $update ) { |
78 if ( 'page' == $post_data['post_type'] ) { |
78 if ( 'page' === $post_data['post_type'] ) { |
79 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
79 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
80 } else { |
80 } else { |
81 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
81 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
82 } |
82 } |
83 } else { |
83 } else { |
84 if ( 'page' == $post_data['post_type'] ) { |
84 if ( 'page' === $post_data['post_type'] ) { |
85 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
85 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
86 } else { |
86 } else { |
87 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
87 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
88 } |
88 } |
89 } |
89 } |
90 } |
90 } |
91 |
91 |
92 if ( ! empty( $post_data['post_status'] ) ) { |
92 if ( ! empty( $post_data['post_status'] ) ) { |
93 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
93 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
94 |
94 |
95 // No longer an auto-draft |
95 // No longer an auto-draft. |
96 if ( 'auto-draft' === $post_data['post_status'] ) { |
96 if ( 'auto-draft' === $post_data['post_status'] ) { |
97 $post_data['post_status'] = 'draft'; |
97 $post_data['post_status'] = 'draft'; |
98 } |
98 } |
99 |
99 |
100 if ( ! get_post_status_object( $post_data['post_status'] ) ) { |
100 if ( ! get_post_status_object( $post_data['post_status'] ) ) { |
101 unset( $post_data['post_status'] ); |
101 unset( $post_data['post_status'] ); |
102 } |
102 } |
103 } |
103 } |
104 |
104 |
105 // What to do based on which button they pressed |
105 // What to do based on which button they pressed. |
106 if ( isset( $post_data['saveasdraft'] ) && '' != $post_data['saveasdraft'] ) { |
106 if ( isset( $post_data['saveasdraft'] ) && '' !== $post_data['saveasdraft'] ) { |
107 $post_data['post_status'] = 'draft'; |
107 $post_data['post_status'] = 'draft'; |
108 } |
108 } |
109 if ( isset( $post_data['saveasprivate'] ) && '' != $post_data['saveasprivate'] ) { |
109 if ( isset( $post_data['saveasprivate'] ) && '' !== $post_data['saveasprivate'] ) { |
110 $post_data['post_status'] = 'private'; |
110 $post_data['post_status'] = 'private'; |
111 } |
111 } |
112 if ( isset( $post_data['publish'] ) && ( '' != $post_data['publish'] ) && ( ! isset( $post_data['post_status'] ) || $post_data['post_status'] != 'private' ) ) { |
112 if ( isset( $post_data['publish'] ) && ( '' !== $post_data['publish'] ) |
|
113 && ( ! isset( $post_data['post_status'] ) || 'private' !== $post_data['post_status'] ) |
|
114 ) { |
113 $post_data['post_status'] = 'publish'; |
115 $post_data['post_status'] = 'publish'; |
114 } |
116 } |
115 if ( isset( $post_data['advanced'] ) && '' != $post_data['advanced'] ) { |
117 if ( isset( $post_data['advanced'] ) && '' !== $post_data['advanced'] ) { |
116 $post_data['post_status'] = 'draft'; |
118 $post_data['post_status'] = 'draft'; |
117 } |
119 } |
118 if ( isset( $post_data['pending'] ) && '' != $post_data['pending'] ) { |
120 if ( isset( $post_data['pending'] ) && '' !== $post_data['pending'] ) { |
119 $post_data['post_status'] = 'pending'; |
121 $post_data['post_status'] = 'pending'; |
120 } |
122 } |
121 |
123 |
122 if ( isset( $post_data['ID'] ) ) { |
124 if ( isset( $post_data['ID'] ) ) { |
123 $post_id = $post_data['ID']; |
125 $post_id = $post_data['ID']; |
124 } else { |
126 } else { |
125 $post_id = false; |
127 $post_id = false; |
126 } |
128 } |
127 $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; |
129 $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; |
128 |
130 |
129 if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) { |
131 if ( isset( $post_data['post_status'] ) && 'private' === $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) { |
130 $post_data['post_status'] = $previous_status ? $previous_status : 'pending'; |
132 $post_data['post_status'] = $previous_status ? $previous_status : 'pending'; |
131 } |
133 } |
132 |
134 |
133 $published_statuses = array( 'publish', 'future' ); |
135 $published_statuses = array( 'publish', 'future' ); |
134 |
136 |
135 // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published. |
137 // Posts 'submitted for approval' are submitted to $_POST the same as if they were being published. |
136 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. |
138 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. |
137 if ( isset( $post_data['post_status'] ) && ( in_array( $post_data['post_status'], $published_statuses ) && ! current_user_can( $ptype->cap->publish_posts ) ) ) { |
139 if ( isset( $post_data['post_status'] ) |
138 if ( ! in_array( $previous_status, $published_statuses ) || ! current_user_can( 'edit_post', $post_id ) ) { |
140 && ( in_array( $post_data['post_status'], $published_statuses, true ) |
|
141 && ! current_user_can( $ptype->cap->publish_posts ) ) |
|
142 ) { |
|
143 if ( ! in_array( $previous_status, $published_statuses, true ) || ! current_user_can( 'edit_post', $post_id ) ) { |
139 $post_data['post_status'] = 'pending'; |
144 $post_data['post_status'] = 'pending'; |
140 } |
145 } |
141 } |
146 } |
142 |
147 |
143 if ( ! isset( $post_data['post_status'] ) ) { |
148 if ( ! isset( $post_data['post_status'] ) ) { |
168 $mm = $post_data['mm']; |
173 $mm = $post_data['mm']; |
169 $jj = $post_data['jj']; |
174 $jj = $post_data['jj']; |
170 $hh = $post_data['hh']; |
175 $hh = $post_data['hh']; |
171 $mn = $post_data['mn']; |
176 $mn = $post_data['mn']; |
172 $ss = $post_data['ss']; |
177 $ss = $post_data['ss']; |
173 $aa = ( $aa <= 0 ) ? date( 'Y' ) : $aa; |
178 $aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa; |
174 $mm = ( $mm <= 0 ) ? date( 'n' ) : $mm; |
179 $mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm; |
175 $jj = ( $jj > 31 ) ? 31 : $jj; |
180 $jj = ( $jj > 31 ) ? 31 : $jj; |
176 $jj = ( $jj <= 0 ) ? date( 'j' ) : $jj; |
181 $jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj; |
177 $hh = ( $hh > 23 ) ? $hh - 24 : $hh; |
182 $hh = ( $hh > 23 ) ? $hh - 24 : $hh; |
178 $mn = ( $mn > 59 ) ? $mn - 60 : $mn; |
183 $mn = ( $mn > 59 ) ? $mn - 60 : $mn; |
179 $ss = ( $ss > 59 ) ? $ss - 60 : $ss; |
184 $ss = ( $ss > 59 ) ? $ss - 60 : $ss; |
180 $post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss ); |
185 $post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss ); |
181 $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); |
186 $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); |
199 * Returns only allowed post data fields |
204 * Returns only allowed post data fields |
200 * |
205 * |
201 * @since 5.0.1 |
206 * @since 5.0.1 |
202 * |
207 * |
203 * @param array $post_data Array of post data. Defaults to the contents of $_POST. |
208 * @param array $post_data Array of post data. Defaults to the contents of $_POST. |
204 * @return object|bool WP_Error on failure, true on success. |
209 * @return array|WP_Error Array of post data on success, WP_Error on failure. |
205 */ |
210 */ |
206 function _wp_get_allowed_postdata( $post_data = null ) { |
211 function _wp_get_allowed_postdata( $post_data = null ) { |
207 if ( empty( $post_data ) ) { |
212 if ( empty( $post_data ) ) { |
208 $post_data = $_POST; |
213 $post_data = $_POST; |
209 } |
214 } |
210 |
215 |
211 // Pass through errors |
216 // Pass through errors. |
212 if ( is_wp_error( $post_data ) ) { |
217 if ( is_wp_error( $post_data ) ) { |
213 return $post_data; |
218 return $post_data; |
214 } |
219 } |
215 |
220 |
216 return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) ); |
221 return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) ); |
217 } |
222 } |
218 |
223 |
219 /** |
224 /** |
220 * Update an existing post with values provided in $_POST. |
225 * Update an existing post with values provided in $_POST. |
221 * |
226 * |
|
227 * If post data is passed as an argument, it is treated as an array of data |
|
228 * keyed appropriately for turning into a post object. |
|
229 * |
|
230 * If post data is not passed, the $_POST global variable is used instead. |
|
231 * |
222 * @since 1.5.0 |
232 * @since 1.5.0 |
223 * |
233 * |
224 * @global wpdb $wpdb WordPress database abstraction object. |
234 * @global wpdb $wpdb WordPress database abstraction object. |
225 * |
235 * |
226 * @param array $post_data Optional. |
236 * @param array $post_data Optional. Defaults to the $_POST global. |
227 * @return int Post ID. |
237 * @return int Post ID. |
228 */ |
238 */ |
229 function edit_post( $post_data = null ) { |
239 function edit_post( $post_data = null ) { |
230 global $wpdb; |
240 global $wpdb; |
231 |
241 |
242 $post_data['post_mime_type'] = $post->post_mime_type; |
252 $post_data['post_mime_type'] = $post->post_mime_type; |
243 |
253 |
244 if ( ! empty( $post_data['post_status'] ) ) { |
254 if ( ! empty( $post_data['post_status'] ) ) { |
245 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
255 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
246 |
256 |
247 if ( 'inherit' == $post_data['post_status'] ) { |
257 if ( 'inherit' === $post_data['post_status'] ) { |
248 unset( $post_data['post_status'] ); |
258 unset( $post_data['post_status'] ); |
249 } |
259 } |
250 } |
260 } |
251 |
261 |
252 $ptype = get_post_type_object( $post_data['post_type'] ); |
262 $ptype = get_post_type_object( $post_data['post_type'] ); |
253 if ( ! current_user_can( 'edit_post', $post_ID ) ) { |
263 if ( ! current_user_can( 'edit_post', $post_ID ) ) { |
254 if ( 'page' == $post_data['post_type'] ) { |
264 if ( 'page' === $post_data['post_type'] ) { |
255 wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); |
265 wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); |
256 } else { |
266 } else { |
257 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
267 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
258 } |
268 } |
259 } |
269 } |
368 } |
380 } |
369 delete_meta( $key ); |
381 delete_meta( $key ); |
370 } |
382 } |
371 } |
383 } |
372 |
384 |
373 // Attachment stuff |
385 // Attachment stuff. |
374 if ( 'attachment' == $post_data['post_type'] ) { |
386 if ( 'attachment' === $post_data['post_type'] ) { |
375 if ( isset( $post_data['_wp_attachment_image_alt'] ) ) { |
387 if ( isset( $post_data['_wp_attachment_image_alt'] ) ) { |
376 $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); |
388 $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); |
377 if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) { |
389 |
|
390 if ( get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) !== $image_alt ) { |
378 $image_alt = wp_strip_all_tags( $image_alt, true ); |
391 $image_alt = wp_strip_all_tags( $image_alt, true ); |
379 // update_meta expects slashed. |
392 |
|
393 // update_post_meta() expects slashed. |
380 update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); |
394 update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); |
381 } |
395 } |
382 } |
396 } |
383 |
397 |
384 $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); |
398 $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); |
401 add_meta( $post_ID ); |
415 add_meta( $post_ID ); |
402 |
416 |
403 update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); |
417 update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); |
404 |
418 |
405 $success = wp_update_post( $translated ); |
419 $success = wp_update_post( $translated ); |
406 // If the save failed, see if we can sanity check the main fields and try again |
420 |
|
421 // If the save failed, see if we can sanity check the main fields and try again. |
407 if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { |
422 if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { |
408 $fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
423 $fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
409 |
424 |
410 foreach ( $fields as $field ) { |
425 foreach ( $fields as $field ) { |
411 if ( isset( $translated[ $field ] ) ) { |
426 if ( isset( $translated[ $field ] ) ) { |
529 $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
544 $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
530 } |
545 } |
531 } |
546 } |
532 } |
547 } |
533 |
548 |
534 if ( isset( $post_data['post_parent'] ) && ( $parent = (int) $post_data['post_parent'] ) ) { |
549 if ( isset( $post_data['post_parent'] ) && (int) $post_data['post_parent'] ) { |
|
550 $parent = (int) $post_data['post_parent']; |
535 $pages = $wpdb->get_results( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'" ); |
551 $pages = $wpdb->get_results( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'" ); |
536 $children = array(); |
552 $children = array(); |
537 |
553 |
538 for ( $i = 0; $i < 50 && $parent > 0; $i++ ) { |
554 for ( $i = 0; $i < 50 && $parent > 0; $i++ ) { |
539 $children[] = $parent; |
555 $children[] = $parent; |
540 |
556 |
541 foreach ( $pages as $page ) { |
557 foreach ( $pages as $page ) { |
542 if ( $page->ID == $parent ) { |
558 if ( (int) $page->ID === $parent ) { |
543 $parent = $page->post_parent; |
559 $parent = (int) $page->post_parent; |
544 break; |
560 break; |
545 } |
561 } |
546 } |
562 } |
547 } |
563 } |
548 } |
564 } |
549 |
565 |
550 $updated = $skipped = $locked = array(); |
566 $updated = array(); |
|
567 $skipped = array(); |
|
568 $locked = array(); |
551 $shared_post_data = $post_data; |
569 $shared_post_data = $post_data; |
552 |
570 |
553 foreach ( $post_IDs as $post_ID ) { |
571 foreach ( $post_IDs as $post_ID ) { |
554 // Start with fresh post data with each iteration. |
572 // Start with fresh post data with each iteration. |
555 $post_data = $shared_post_data; |
573 $post_data = $shared_post_data; |
556 |
574 |
557 $post_type_object = get_post_type_object( get_post_type( $post_ID ) ); |
575 $post_type_object = get_post_type_object( get_post_type( $post_ID ) ); |
558 |
576 |
559 if ( ! isset( $post_type_object ) || ( isset( $children ) && in_array( $post_ID, $children ) ) || ! current_user_can( 'edit_post', $post_ID ) ) { |
577 if ( ! isset( $post_type_object ) |
|
578 || ( isset( $children ) && in_array( $post_ID, $children, true ) ) |
|
579 || ! current_user_can( 'edit_post', $post_ID ) |
|
580 ) { |
560 $skipped[] = $post_ID; |
581 $skipped[] = $post_ID; |
561 continue; |
582 continue; |
562 } |
583 } |
563 |
584 |
564 if ( wp_check_post_lock( $post_ID ) ) { |
585 if ( wp_check_post_lock( $post_ID ) ) { |
896 $metavalue = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : ''; |
917 $metavalue = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : ''; |
897 if ( is_string( $metavalue ) ) { |
918 if ( is_string( $metavalue ) ) { |
898 $metavalue = trim( $metavalue ); |
919 $metavalue = trim( $metavalue ); |
899 } |
920 } |
900 |
921 |
901 if ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) { |
922 if ( ( ( '#NONE#' !== $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) { |
902 /* |
923 /* |
903 * We have a key/value pair. If both the select and the input |
924 * We have a key/value pair. If both the select and the input |
904 * for the key have data, the input takes precedence. |
925 * for the key have data, the input takes precedence. |
905 */ |
926 */ |
906 if ( '#NONE#' != $metakeyselect ) { |
927 if ( '#NONE#' !== $metakeyselect ) { |
907 $metakey = $metakeyselect; |
928 $metakey = $metakeyselect; |
908 } |
929 } |
909 |
930 |
910 if ( $metakeyinput ) { |
931 if ( $metakeyinput ) { |
911 $metakey = $metakeyinput; // default |
932 $metakey = $metakeyinput; // Default. |
912 } |
933 } |
913 |
934 |
914 if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) { |
935 if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) { |
915 return false; |
936 return false; |
916 } |
937 } |
1027 function _fix_attachment_links( $post ) { |
1048 function _fix_attachment_links( $post ) { |
1028 $post = get_post( $post, ARRAY_A ); |
1049 $post = get_post( $post, ARRAY_A ); |
1029 $content = $post['post_content']; |
1050 $content = $post['post_content']; |
1030 |
1051 |
1031 // Don't run if no pretty permalinks or post is not published, scheduled, or privately published. |
1052 // Don't run if no pretty permalinks or post is not published, scheduled, or privately published. |
1032 if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) ) { |
1053 if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) { |
1033 return; |
1054 return; |
1034 } |
1055 } |
1035 |
1056 |
1036 // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero) |
1057 // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero). |
1037 if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) { |
1058 if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) { |
1038 return; |
1059 return; |
1039 } |
1060 } |
1040 |
1061 |
1041 $site_url = get_bloginfo( 'url' ); |
1062 $site_url = get_bloginfo( 'url' ); |
1042 $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // remove the http(s) |
1063 $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s). |
1043 $replace = ''; |
1064 $replace = ''; |
1044 |
1065 |
1045 foreach ( $link_matches[1] as $key => $value ) { |
1066 foreach ( $link_matches[1] as $key => $value ) { |
1046 if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' ) |
1067 if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' ) |
1047 || ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match ) |
1068 || ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match ) |
1048 || ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) { |
1069 || ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) { |
1049 continue; |
1070 continue; |
1050 } |
1071 } |
1051 |
1072 |
1052 $quote = $url_match[1]; // the quote (single or double) |
1073 $quote = $url_match[1]; // The quote (single or double). |
1053 $url_id = (int) $url_match[2]; |
1074 $url_id = (int) $url_match[2]; |
1054 $rel_id = (int) $rel_match[1]; |
1075 $rel_id = (int) $rel_match[1]; |
1055 |
1076 |
1056 if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) { |
1077 if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) { |
1057 continue; |
1078 continue; |
1100 } |
1121 } |
1101 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; |
1122 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; |
1102 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; |
1123 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; |
1103 $post_stati = get_post_stati(); |
1124 $post_stati = get_post_stati(); |
1104 |
1125 |
1105 if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types() ) ) { |
1126 if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types(), true ) ) { |
1106 $post_type = $q['post_type']; |
1127 $post_type = $q['post_type']; |
1107 } else { |
1128 } else { |
1108 $post_type = 'post'; |
1129 $post_type = 'post'; |
1109 } |
1130 } |
1110 |
1131 |
1111 $avail_post_stati = get_available_post_statuses( $post_type ); |
1132 $avail_post_stati = get_available_post_statuses( $post_type ); |
1112 $post_status = ''; |
1133 $post_status = ''; |
1113 $perm = ''; |
1134 $perm = ''; |
1114 |
1135 |
1115 if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_stati ) ) { |
1136 if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_stati, true ) ) { |
1116 $post_status = $q['post_status']; |
1137 $post_status = $q['post_status']; |
1117 $perm = 'readable'; |
1138 $perm = 'readable'; |
1118 } |
1139 } |
1119 |
1140 |
1120 $orderby = ''; |
1141 $orderby = ''; |
1121 |
1142 |
1122 if ( isset( $q['orderby'] ) ) { |
1143 if ( isset( $q['orderby'] ) ) { |
1123 $orderby = $q['orderby']; |
1144 $orderby = $q['orderby']; |
1124 } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) { |
1145 } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ), true ) ) { |
1125 $orderby = 'modified'; |
1146 $orderby = 'modified'; |
1126 } |
1147 } |
1127 |
1148 |
1128 $order = ''; |
1149 $order = ''; |
1129 |
1150 |
1130 if ( isset( $q['order'] ) ) { |
1151 if ( isset( $q['order'] ) ) { |
1131 $order = $q['order']; |
1152 $order = $q['order']; |
1132 } elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) { |
1153 } elseif ( isset( $q['post_status'] ) && 'pending' === $q['post_status'] ) { |
1133 $order = 'ASC'; |
1154 $order = 'ASC'; |
1134 } |
1155 } |
1135 |
1156 |
1136 $per_page = "edit_{$post_type}_per_page"; |
1157 $per_page = "edit_{$post_type}_per_page"; |
1137 $posts_per_page = (int) get_user_option( $per_page ); |
1158 $posts_per_page = (int) get_user_option( $per_page ); |
1227 if ( isset( $q['post_mime_type'] ) && ! array_intersect( (array) $q['post_mime_type'], array_keys( $post_mime_types ) ) ) { |
1248 if ( isset( $q['post_mime_type'] ) && ! array_intersect( (array) $q['post_mime_type'], array_keys( $post_mime_types ) ) ) { |
1228 unset( $q['post_mime_type'] ); |
1249 unset( $q['post_mime_type'] ); |
1229 } |
1250 } |
1230 |
1251 |
1231 foreach ( array_keys( $post_mime_types ) as $type ) { |
1252 foreach ( array_keys( $post_mime_types ) as $type ) { |
1232 if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) { |
1253 if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" === $q['attachment-filter'] ) { |
1233 $q['post_mime_type'] = $type; |
1254 $q['post_mime_type'] = $type; |
1234 break; |
1255 break; |
1235 } |
1256 } |
1236 } |
1257 } |
1237 |
1258 |
1238 if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) { |
1259 if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' === $q['attachment-filter'] ) ) { |
1239 $q['post_parent'] = 0; |
1260 $q['post_parent'] = 0; |
1240 } |
1261 } |
1241 |
1262 |
1242 if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' == $q['attachment-filter'] ) ) { |
1263 if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' === $q['attachment-filter'] ) ) { |
1243 $q['author'] = get_current_user_id(); |
1264 $q['author'] = get_current_user_id(); |
1244 } |
1265 } |
1245 |
1266 |
1246 // Filter query clauses to include filenames. |
1267 // Filter query clauses to include filenames. |
1247 if ( isset( $q['s'] ) ) { |
1268 if ( isset( $q['s'] ) ) { |
1279 * @return string Space-separated string of class names. |
1300 * @return string Space-separated string of class names. |
1280 */ |
1301 */ |
1281 function postbox_classes( $box_id, $screen_id ) { |
1302 function postbox_classes( $box_id, $screen_id ) { |
1282 if ( isset( $_GET['edit'] ) && $_GET['edit'] == $box_id ) { |
1303 if ( isset( $_GET['edit'] ) && $_GET['edit'] == $box_id ) { |
1283 $classes = array( '' ); |
1304 $classes = array( '' ); |
1284 } elseif ( $closed = get_user_option( 'closedpostboxes_' . $screen_id ) ) { |
1305 } elseif ( get_user_option( 'closedpostboxes_' . $screen_id ) ) { |
|
1306 $closed = get_user_option( 'closedpostboxes_' . $screen_id ); |
1285 if ( ! is_array( $closed ) ) { |
1307 if ( ! is_array( $closed ) ) { |
1286 $classes = array( '' ); |
1308 $classes = array( '' ); |
1287 } else { |
1309 } else { |
1288 $classes = in_array( $box_id, $closed ) ? array( 'closed' ) : array( '' ); |
1310 $classes = in_array( $box_id, $closed, true ) ? array( 'closed' ) : array( '' ); |
1289 } |
1311 } |
1290 } else { |
1312 } else { |
1291 $classes = array( '' ); |
1313 $classes = array( '' ); |
1292 } |
1314 } |
1293 |
1315 |
1311 * @since 2.5.0 |
1333 * @since 2.5.0 |
1312 * |
1334 * |
1313 * @param int $id Post ID or post object. |
1335 * @param int $id Post ID or post object. |
1314 * @param string $title Optional. Title to override the post's current title when generating the post name. Default null. |
1336 * @param string $title Optional. Title to override the post's current title when generating the post name. Default null. |
1315 * @param string $name Optional. Name to override the post name. Default null. |
1337 * @param string $name Optional. Name to override the post name. Default null. |
1316 * @return array Array containing the sample permalink with placeholder for the post name, and the post name. |
1338 * @return array { |
|
1339 * Array containing the sample permalink with placeholder for the post name, and the post name. |
|
1340 * |
|
1341 * @type string $0 The permalink with placeholder for the post name. |
|
1342 * @type string $1 The post name. |
|
1343 * } |
1317 */ |
1344 */ |
1318 function get_sample_permalink( $id, $title = null, $name = null ) { |
1345 function get_sample_permalink( $id, $title = null, $name = null ) { |
1319 $post = get_post( $id ); |
1346 $post = get_post( $id ); |
1320 if ( ! $post ) { |
1347 if ( ! $post ) { |
1321 return array( '', '' ); |
1348 return array( '', '' ); |
1326 $original_status = $post->post_status; |
1353 $original_status = $post->post_status; |
1327 $original_date = $post->post_date; |
1354 $original_date = $post->post_date; |
1328 $original_name = $post->post_name; |
1355 $original_name = $post->post_name; |
1329 |
1356 |
1330 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. |
1357 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. |
1331 if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) { |
1358 if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) { |
1332 $post->post_status = 'publish'; |
1359 $post->post_status = 'publish'; |
1333 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); |
1360 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); |
1334 } |
1361 } |
1335 |
1362 |
1336 // If the user wants to set a new name -- override the current one |
1363 // If the user wants to set a new name -- override the current one. |
1337 // Note: if empty name is supplied -- use the title instead, see #6072 |
1364 // Note: if empty name is supplied -- use the title instead, see #6072. |
1338 if ( ! is_null( $name ) ) { |
1365 if ( ! is_null( $name ) ) { |
1339 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); |
1366 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); |
1340 } |
1367 } |
1341 |
1368 |
1342 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); |
1369 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); |
1343 |
1370 |
1344 $post->filter = 'sample'; |
1371 $post->filter = 'sample'; |
1345 |
1372 |
1346 $permalink = get_permalink( $post, true ); |
1373 $permalink = get_permalink( $post, true ); |
1347 |
1374 |
1348 // Replace custom post_type Token with generic pagename token for ease of use. |
1375 // Replace custom post_type token with generic pagename token for ease of use. |
1349 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); |
1376 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); |
1350 |
1377 |
1351 // Handle page hierarchy |
1378 // Handle page hierarchy. |
1352 if ( $ptype->hierarchical ) { |
1379 if ( $ptype->hierarchical ) { |
1353 $uri = get_page_uri( $post ); |
1380 $uri = get_page_uri( $post ); |
1354 if ( $uri ) { |
1381 if ( $uri ) { |
1355 $uri = untrailingslashit( $uri ); |
1382 $uri = untrailingslashit( $uri ); |
1356 $uri = strrev( stristr( strrev( $uri ), '/' ) ); |
1383 $uri = strrev( stristr( strrev( $uri ), '/' ) ); |
1413 $preview_target = " target='wp-preview-{$post->ID}'"; |
1445 $preview_target = " target='wp-preview-{$post->ID}'"; |
1414 } else { |
1446 } else { |
1415 if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { |
1447 if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { |
1416 $view_link = get_permalink( $post ); |
1448 $view_link = get_permalink( $post ); |
1417 } else { |
1449 } else { |
1418 // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set |
1450 // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set. |
1419 $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); |
1451 $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); |
1420 } |
1452 } |
1421 } |
1453 } |
1422 } |
1454 } |
1423 |
1455 |
1424 // Permalinks without a post/page name placeholder don't have anything to edit |
1456 // Permalinks without a post/page name placeholder don't have anything to edit. |
1425 if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { |
1457 if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { |
1426 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
1458 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
1427 |
1459 |
1428 if ( false !== $view_link ) { |
1460 if ( false !== $view_link ) { |
1429 $display_link = urldecode( $view_link ); |
1461 $display_link = urldecode( $view_link ); |
1430 $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n"; |
1462 $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n"; |
1431 } else { |
1463 } else { |
1432 $return .= '<span id="sample-permalink">' . $permalink . "</span>\n"; |
1464 $return .= '<span id="sample-permalink">' . $permalink . "</span>\n"; |
1433 } |
1465 } |
1434 |
1466 |
1435 // Encourage a pretty permalink setting |
1467 // Encourage a pretty permalink setting. |
1436 if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && ! ( 'page' == get_option( 'show_on_front' ) && $id == get_option( 'page_on_front' ) ) ) { |
1468 if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) |
|
1469 && ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $id ) |
|
1470 ) { |
1437 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __( 'Change Permalinks' ) . "</a></span>\n"; |
1471 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __( 'Change Permalinks' ) . "</a></span>\n"; |
1438 } |
1472 } |
1439 } else { |
1473 } else { |
1440 if ( mb_strlen( $post_name ) > 34 ) { |
1474 if ( mb_strlen( $post_name ) > 34 ) { |
1441 $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 ); |
1475 $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 ); |
1610 |
1648 |
1611 /** |
1649 /** |
1612 * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
1650 * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
1613 * |
1651 * |
1614 * @since 2.8.5 |
1652 * @since 2.8.5 |
1615 * @return none |
|
1616 */ |
1653 */ |
1617 function _admin_notice_post_locked() { |
1654 function _admin_notice_post_locked() { |
1618 if ( ! $post = get_post() ) { |
1655 $post = get_post(); |
|
1656 if ( ! $post ) { |
1619 return; |
1657 return; |
1620 } |
1658 } |
1621 |
1659 |
1622 $user = null; |
1660 $user = null; |
1623 if ( $user_id = wp_check_post_lock( $post->ID ) ) { |
1661 $user_id = wp_check_post_lock( $post->ID ); |
|
1662 if ( $user_id ) { |
1624 $user = get_userdata( $user_id ); |
1663 $user = get_userdata( $user_id ); |
1625 } |
1664 } |
1626 |
1665 |
1627 if ( $user ) { |
1666 if ( $user ) { |
1628 |
|
1629 /** |
1667 /** |
1630 * Filters whether to show the post locked dialog. |
1668 * Filters whether to show the post locked dialog. |
1631 * |
1669 * |
1632 * Returning a falsey value to the filter will short-circuit displaying the dialog. |
1670 * Returning false from the filter will prevent the dialog from being displayed. |
1633 * |
1671 * |
1634 * @since 3.6.0 |
1672 * @since 3.6.0 |
1635 * |
1673 * |
1636 * @param bool $display Whether to display the dialog. Default true. |
1674 * @param bool $display Whether to display the dialog. Default true. |
1637 * @param WP_Post $post Post object. |
1675 * @param WP_Post $post Post object. |
1638 * @param WP_User|bool $user WP_User object on success, false otherwise. |
1676 * @param WP_User $user The user with the lock for the post. |
1639 */ |
1677 */ |
1640 if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) { |
1678 if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) { |
1641 return; |
1679 return; |
1642 } |
1680 } |
1643 |
1681 |
1644 $locked = true; |
1682 $locked = true; |
1645 } else { |
1683 } else { |
1646 $locked = false; |
1684 $locked = false; |
1647 } |
1685 } |
1648 |
1686 |
1649 if ( $locked && ( $sendback = wp_get_referer() ) && |
1687 $sendback = wp_get_referer(); |
1650 false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) { |
1688 if ( $locked && $sendback && false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) { |
1651 |
1689 |
1652 $sendback_text = __( 'Go back' ); |
1690 $sendback_text = __( 'Go back' ); |
1653 } else { |
1691 } else { |
1654 $sendback = admin_url( 'edit.php' ); |
1692 $sendback = admin_url( 'edit.php' ); |
1655 |
1693 |
1656 if ( 'post' != $post->post_type ) { |
1694 if ( 'post' !== $post->post_type ) { |
1657 $sendback = add_query_arg( 'post_type', $post->post_type, $sendback ); |
1695 $sendback = add_query_arg( 'post_type', $post->post_type, $sendback ); |
1658 } |
1696 } |
1659 |
1697 |
1660 $sendback_text = get_post_type_object( $post->post_type )->labels->all_items; |
1698 $sendback_text = get_post_type_object( $post->post_type )->labels->all_items; |
1661 } |
1699 } |
1682 $preview_link = get_preview_post_link( $post->ID, $query_args ); |
1720 $preview_link = get_preview_post_link( $post->ID, $query_args ); |
1683 |
1721 |
1684 /** |
1722 /** |
1685 * Filters whether to allow the post lock to be overridden. |
1723 * Filters whether to allow the post lock to be overridden. |
1686 * |
1724 * |
1687 * Returning a falsey value to the filter will disable the ability |
1725 * Returning false from the filter will disable the ability |
1688 * to override the post lock. |
1726 * to override the post lock. |
1689 * |
1727 * |
1690 * @since 3.6.0 |
1728 * @since 3.6.0 |
1691 * |
1729 * |
1692 * @param bool $override Whether to allow overriding post locks. Default true. |
1730 * @param bool $override Whether to allow the post lock to be overridden. Default true. |
1693 * @param WP_Post $post Post object. |
1731 * @param WP_Post $post Post object. |
1694 * @param WP_User $user User object. |
1732 * @param WP_User $user The user with the lock for the post. |
1695 */ |
1733 */ |
1696 $override = apply_filters( 'override_post_lock', true, $post, $user ); |
1734 $override = apply_filters( 'override_post_lock', true, $post, $user ); |
1697 $tab_last = $override ? '' : ' wp-tab-last'; |
1735 $tab_last = $override ? '' : ' wp-tab-last'; |
1698 |
1736 |
1699 ?> |
1737 ?> |
1700 <div class="post-locked-message"> |
1738 <div class="post-locked-message"> |
1701 <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div> |
1739 <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div> |
1702 <p class="currently-editing wp-tab-first" tabindex="0"> |
1740 <p class="currently-editing wp-tab-first" tabindex="0"> |
1703 <?php |
1741 <?php |
1704 if ( $override ) { |
1742 if ( $override ) { |
1705 /* translators: %s: user's display name */ |
1743 /* translators: %s: User's display name. */ |
1706 printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) ); |
1744 printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) ); |
1707 } else { |
1745 } else { |
1708 /* translators: %s: user's display name */ |
1746 /* translators: %s: User's display name. */ |
1709 printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) ); |
1747 printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) ); |
1710 } |
1748 } |
1711 ?> |
1749 ?> |
1712 </p> |
1750 </p> |
1713 <?php |
1751 <?php |
1714 /** |
1752 /** |
1715 * Fires inside the post locked dialog before the buttons are displayed. |
1753 * Fires inside the post locked dialog before the buttons are displayed. |
1716 * |
1754 * |
1717 * @since 3.6.0 |
1755 * @since 3.6.0 |
|
1756 * @since 5.4.0 The $user parameter was added. |
1718 * |
1757 * |
1719 * @param WP_Post $post Post object. |
1758 * @param WP_Post $post Post object. |
|
1759 * @param WP_User $user The user with the lock for the post. |
1720 */ |
1760 */ |
1721 do_action( 'post_locked_dialog', $post ); |
1761 do_action( 'post_locked_dialog', $post, $user ); |
1722 ?> |
1762 ?> |
1723 <p> |
1763 <p> |
1724 <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a> |
1764 <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a> |
1725 <?php if ( $preview_link ) { ?> |
1765 <?php if ( $preview_link ) { ?> |
1726 <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e( 'Preview' ); ?></a> |
1766 <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e( 'Preview' ); ?></a> |
1727 <?php |
1767 <?php |
1728 } |
1768 } |
1729 |
1769 |
1730 // Allow plugins to prevent some users overriding the post lock |
1770 // Allow plugins to prevent some users overriding the post lock. |
1731 if ( $override ) { |
1771 if ( $override ) { |
1732 ?> |
1772 ?> |
1733 <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> |
1773 <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> |
1734 <?php |
1774 <?php |
1735 } |
1775 } |
1791 $post_data = _wp_get_allowed_postdata( $post_data ); |
1831 $post_data = _wp_get_allowed_postdata( $post_data ); |
1792 |
1832 |
1793 $post_author = get_current_user_id(); |
1833 $post_author = get_current_user_id(); |
1794 |
1834 |
1795 // Store one autosave per author. If there is already an autosave, overwrite it. |
1835 // Store one autosave per author. If there is already an autosave, overwrite it. |
1796 if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { |
1836 $old_autosave = wp_get_post_autosave( $post_id, $post_author ); |
|
1837 if ( $old_autosave ) { |
1797 $new_autosave = _wp_post_revision_data( $post_data, true ); |
1838 $new_autosave = _wp_post_revision_data( $post_data, true ); |
1798 $new_autosave['ID'] = $old_autosave->ID; |
1839 $new_autosave['ID'] = $old_autosave->ID; |
1799 $new_autosave['post_author'] = $post_author; |
1840 $new_autosave['post_author'] = $post_author; |
1800 |
1841 |
|
1842 $post = get_post( $post_id ); |
|
1843 |
1801 // If the new autosave has the same content as the post, delete the autosave. |
1844 // If the new autosave has the same content as the post, delete the autosave. |
1802 $post = get_post( $post_id ); |
|
1803 $autosave_is_different = false; |
1845 $autosave_is_different = false; |
1804 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { |
1846 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { |
1805 if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { |
1847 if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) { |
1806 $autosave_is_different = true; |
1848 $autosave_is_different = true; |
1807 break; |
1849 break; |
1808 } |
1850 } |
1809 } |
1851 } |
1810 |
1852 |
1842 function post_preview() { |
1884 function post_preview() { |
1843 |
1885 |
1844 $post_ID = (int) $_POST['post_ID']; |
1886 $post_ID = (int) $_POST['post_ID']; |
1845 $_POST['ID'] = $post_ID; |
1887 $_POST['ID'] = $post_ID; |
1846 |
1888 |
1847 if ( ! $post = get_post( $post_ID ) ) { |
1889 $post = get_post( $post_ID ); |
|
1890 if ( ! $post ) { |
1848 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
1891 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
1849 } |
1892 } |
1850 |
1893 |
1851 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
1894 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
1852 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
1895 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
1853 } |
1896 } |
1854 |
1897 |
1855 $is_autosave = false; |
1898 $is_autosave = false; |
1856 |
1899 |
1857 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) { |
1900 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author |
|
1901 && ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status ) |
|
1902 ) { |
1858 $saved_post_id = edit_post(); |
1903 $saved_post_id = edit_post(); |
1859 } else { |
1904 } else { |
1860 $is_autosave = true; |
1905 $is_autosave = true; |
1861 |
1906 |
1862 if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] ) { |
1907 if ( isset( $_POST['post_status'] ) && 'auto-draft' === $_POST['post_status'] ) { |
1863 $_POST['post_status'] = 'draft'; |
1908 $_POST['post_status'] = 'draft'; |
1864 } |
1909 } |
1865 |
1910 |
1866 $saved_post_id = wp_create_post_autosave( $post->ID ); |
1911 $saved_post_id = wp_create_post_autosave( $post->ID ); |
1867 } |
1912 } |
1898 * @param array $post_data Associative array of the submitted post data. |
1943 * @param array $post_data Associative array of the submitted post data. |
1899 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success. |
1944 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success. |
1900 * The ID can be the draft post_id or the autosave revision post_id. |
1945 * The ID can be the draft post_id or the autosave revision post_id. |
1901 */ |
1946 */ |
1902 function wp_autosave( $post_data ) { |
1947 function wp_autosave( $post_data ) { |
1903 // Back-compat |
1948 // Back-compat. |
1904 if ( ! defined( 'DOING_AUTOSAVE' ) ) { |
1949 if ( ! defined( 'DOING_AUTOSAVE' ) ) { |
1905 define( 'DOING_AUTOSAVE', true ); |
1950 define( 'DOING_AUTOSAVE', true ); |
1906 } |
1951 } |
1907 |
1952 |
1908 $post_id = (int) $post_data['post_id']; |
1953 $post_id = (int) $post_data['post_id']; |
1909 $post_data['ID'] = $post_data['post_ID'] = $post_id; |
1954 $post_data['ID'] = $post_id; |
|
1955 $post_data['post_ID'] = $post_id; |
1910 |
1956 |
1911 if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { |
1957 if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { |
1912 return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); |
1958 return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); |
1913 } |
1959 } |
1914 |
1960 |
1916 |
1962 |
1917 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
1963 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
1918 return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) ); |
1964 return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) ); |
1919 } |
1965 } |
1920 |
1966 |
1921 if ( 'auto-draft' == $post->post_status ) { |
1967 if ( 'auto-draft' === $post->post_status ) { |
1922 $post_data['post_status'] = 'draft'; |
1968 $post_data['post_status'] = 'draft'; |
1923 } |
1969 } |
1924 |
1970 |
1925 if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) ) { |
1971 if ( 'page' !== $post_data['post_type'] && ! empty( $post_data['catslist'] ) ) { |
1926 $post_data['post_category'] = explode( ',', $post_data['catslist'] ); |
1972 $post_data['post_category'] = explode( ',', $post_data['catslist'] ); |
1927 } |
1973 } |
1928 |
1974 |
1929 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) { |
1975 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author |
1930 // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked |
1976 && ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status ) |
|
1977 ) { |
|
1978 // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked. |
1931 return edit_post( wp_slash( $post_data ) ); |
1979 return edit_post( wp_slash( $post_data ) ); |
1932 } else { |
1980 } else { |
1933 // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. |
1981 // Non-drafts or other users' drafts are not overwritten. |
|
1982 // The autosave is stored in a special post revision for each user. |
1934 return wp_create_post_autosave( wp_slash( $post_data ) ); |
1983 return wp_create_post_autosave( wp_slash( $post_data ) ); |
1935 } |
1984 } |
1936 } |
1985 } |
1937 |
1986 |
1938 /** |
1987 /** |
1939 * Redirect to previous page. |
1988 * Redirect to previous page. |
|
1989 * |
|
1990 * @since 2.7.0 |
1940 * |
1991 * |
1941 * @param int $post_id Optional. Post ID. |
1992 * @param int $post_id Optional. Post ID. |
1942 */ |
1993 */ |
1943 function redirect_post( $post_id = '' ) { |
1994 function redirect_post( $post_id = '' ) { |
1944 if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) { |
1995 if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) { |
1987 /** |
2038 /** |
1988 * Sanitizes POST values from a checkbox taxonomy metabox. |
2039 * Sanitizes POST values from a checkbox taxonomy metabox. |
1989 * |
2040 * |
1990 * @since 5.1.0 |
2041 * @since 5.1.0 |
1991 * |
2042 * |
1992 * @param mixed $terms Raw term data from the 'tax_input' field. |
2043 * @param string $taxonomy The taxonomy name. |
1993 * @return array |
2044 * @param array $terms Raw term data from the 'tax_input' field. |
|
2045 * @return int[] Array of sanitized term IDs. |
1994 */ |
2046 */ |
1995 function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonomy, $terms ) { |
2047 function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonomy, $terms ) { |
1996 return array_map( 'intval', $terms ); |
2048 return array_map( 'intval', $terms ); |
1997 } |
2049 } |
1998 |
2050 |
1999 /** |
2051 /** |
2000 * Sanitizes POST values from an input taxonomy metabox. |
2052 * Sanitizes POST values from an input taxonomy metabox. |
2001 * |
2053 * |
2002 * @since 5.1.0 |
2054 * @since 5.1.0 |
2003 * |
2055 * |
2004 * @param mixed $terms Raw term data from the 'tax_input' field. |
2056 * @param string $taxonomy The taxonomy name. |
|
2057 * @param array|string $terms Raw term data from the 'tax_input' field. |
2005 * @return array |
2058 * @return array |
2006 */ |
2059 */ |
2007 function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) { |
2060 function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) { |
2008 /* |
2061 /* |
2009 * Assume that a 'tax_input' string is a comma-separated list of term names. |
2062 * Assume that a 'tax_input' string is a comma-separated list of term names. |
2124 * Returns all the block categories that will be shown in the block editor. |
2177 * Returns all the block categories that will be shown in the block editor. |
2125 * |
2178 * |
2126 * @since 5.0.0 |
2179 * @since 5.0.0 |
2127 * |
2180 * |
2128 * @param WP_Post $post Post object. |
2181 * @param WP_Post $post Post object. |
2129 * @return array Array of block categories. |
2182 * @return array[] Array of block categories. |
2130 */ |
2183 */ |
2131 function get_block_categories( $post ) { |
2184 function get_block_categories( $post ) { |
2132 $default_categories = array( |
2185 $default_categories = array( |
2133 array( |
2186 array( |
2134 'slug' => 'common', |
2187 'slug' => 'text', |
2135 'title' => __( 'Common Blocks' ), |
2188 'title' => _x( 'Text', 'block category' ), |
2136 'icon' => null, |
2189 'icon' => null, |
2137 ), |
2190 ), |
2138 array( |
2191 array( |
2139 'slug' => 'formatting', |
2192 'slug' => 'media', |
2140 'title' => __( 'Formatting' ), |
2193 'title' => _x( 'Media', 'block category' ), |
2141 'icon' => null, |
2194 'icon' => null, |
2142 ), |
2195 ), |
2143 array( |
2196 array( |
2144 'slug' => 'layout', |
2197 'slug' => 'design', |
2145 'title' => __( 'Layout Elements' ), |
2198 'title' => _x( 'Design', 'block category' ), |
2146 'icon' => null, |
2199 'icon' => null, |
2147 ), |
2200 ), |
2148 array( |
2201 array( |
2149 'slug' => 'widgets', |
2202 'slug' => 'widgets', |
2150 'title' => __( 'Widgets' ), |
2203 'title' => _x( 'Widgets', 'block category' ), |
2151 'icon' => null, |
2204 'icon' => null, |
2152 ), |
2205 ), |
2153 array( |
2206 array( |
2154 'slug' => 'embed', |
2207 'slug' => 'embed', |
2155 'title' => __( 'Embeds' ), |
2208 'title' => _x( 'Embeds', 'block category' ), |
2156 'icon' => null, |
2209 'icon' => null, |
2157 ), |
2210 ), |
2158 array( |
2211 array( |
2159 'slug' => 'reusable', |
2212 'slug' => 'reusable', |
2160 'title' => __( 'Reusable Blocks' ), |
2213 'title' => _x( 'Reusable Blocks', 'block category' ), |
2161 'icon' => null, |
2214 'icon' => null, |
2162 ), |
2215 ), |
2163 ); |
2216 ); |
2164 |
2217 |
2165 /** |
2218 /** |
2166 * Filter the default array of block categories. |
2219 * Filter the default array of block categories. |
2167 * |
2220 * |
2168 * @since 5.0.0 |
2221 * @since 5.0.0 |
2169 * |
2222 * |
2170 * @param array $default_categories Array of block categories. |
2223 * @param array[] $default_categories Array of block categories. |
2171 * @param WP_Post $post Post being loaded. |
2224 * @param WP_Post $post Post being loaded. |
2172 */ |
2225 */ |
2173 return apply_filters( 'block_categories', $default_categories, $post ); |
2226 return apply_filters( 'block_categories', $default_categories, $post ); |
2174 } |
2227 } |
2175 |
2228 |
2184 * @return array An associative array of registered block data. |
2237 * @return array An associative array of registered block data. |
2185 */ |
2238 */ |
2186 function get_block_editor_server_block_settings() { |
2239 function get_block_editor_server_block_settings() { |
2187 $block_registry = WP_Block_Type_Registry::get_instance(); |
2240 $block_registry = WP_Block_Type_Registry::get_instance(); |
2188 $blocks = array(); |
2241 $blocks = array(); |
2189 $keys_to_pick = array( 'title', 'description', 'icon', 'category', 'keywords', 'supports', 'attributes' ); |
2242 $fields_to_pick = array( |
|
2243 'title' => 'title', |
|
2244 'description' => 'description', |
|
2245 'icon' => 'icon', |
|
2246 'category' => 'category', |
|
2247 'keywords' => 'keywords', |
|
2248 'parent' => 'parent', |
|
2249 'supports' => 'supports', |
|
2250 'attributes' => 'attributes', |
|
2251 'provides_context' => 'providesContext', |
|
2252 'uses_context' => 'usesContext', |
|
2253 'styles' => 'styles', |
|
2254 'textdomain' => 'textdomain', |
|
2255 'example' => 'example', |
|
2256 ); |
2190 |
2257 |
2191 foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
2258 foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
2192 foreach ( $keys_to_pick as $key ) { |
2259 foreach ( $fields_to_pick as $field => $key ) { |
2193 if ( ! isset( $block_type->{ $key } ) ) { |
2260 if ( ! isset( $block_type->{ $field } ) ) { |
2194 continue; |
2261 continue; |
2195 } |
2262 } |
2196 |
2263 |
2197 if ( ! isset( $blocks[ $block_name ] ) ) { |
2264 if ( ! isset( $blocks[ $block_name ] ) ) { |
2198 $blocks[ $block_name ] = array(); |
2265 $blocks[ $block_name ] = array(); |
2199 } |
2266 } |
2200 |
2267 |
2201 $blocks[ $block_name ][ $key ] = $block_type->{ $key }; |
2268 $blocks[ $block_name ][ $key ] = $block_type->{ $field }; |
2202 } |
2269 } |
2203 } |
2270 } |
2204 |
2271 |
2205 return $blocks; |
2272 return $blocks; |
2206 } |
2273 } |