98 $post_id = $post_data['ID']; |
108 $post_id = $post_data['ID']; |
99 else |
109 else |
100 $post_id = false; |
110 $post_id = false; |
101 $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; |
111 $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; |
102 |
112 |
|
113 if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) { |
|
114 $post_data['post_status'] = $previous_status ? $previous_status : 'pending'; |
|
115 } |
|
116 |
103 $published_statuses = array( 'publish', 'future' ); |
117 $published_statuses = array( 'publish', 'future' ); |
104 |
118 |
105 // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published. |
119 // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published. |
106 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. |
120 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. |
107 if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) ) |
121 if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) ) |
108 if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) ) |
122 if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) ) |
109 $post_data['post_status'] = 'pending'; |
123 $post_data['post_status'] = 'pending'; |
110 |
124 |
111 if ( ! isset($post_data['post_status']) ) |
125 if ( ! isset( $post_data['post_status'] ) ) { |
112 $post_data['post_status'] = $previous_status; |
126 $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status; |
|
127 } |
|
128 |
|
129 if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) { |
|
130 unset( $post_data['post_password'] ); |
|
131 } |
113 |
132 |
114 if (!isset( $post_data['comment_status'] )) |
133 if (!isset( $post_data['comment_status'] )) |
115 $post_data['comment_status'] = 'closed'; |
134 $post_data['comment_status'] = 'closed'; |
116 |
135 |
117 if (!isset( $post_data['ping_status'] )) |
136 if (!isset( $post_data['ping_status'] )) |
262 if ( 'attachment' == $post_data['post_type'] ) { |
301 if ( 'attachment' == $post_data['post_type'] ) { |
263 if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) { |
302 if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) { |
264 $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); |
303 $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); |
265 if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) { |
304 if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) { |
266 $image_alt = wp_strip_all_tags( $image_alt, true ); |
305 $image_alt = wp_strip_all_tags( $image_alt, true ); |
267 // update_meta expects slashed |
306 // update_meta expects slashed. |
268 update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); |
307 update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); |
269 } |
308 } |
270 } |
309 } |
271 |
310 |
272 $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); |
311 $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); |
|
312 |
273 /** This filter is documented in wp-admin/includes/media.php */ |
313 /** This filter is documented in wp-admin/includes/media.php */ |
274 $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data ); |
314 $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data ); |
275 } |
315 } |
276 |
316 |
|
317 // Convert taxonomy input to term IDs, to avoid ambiguity. |
|
318 if ( isset( $post_data['tax_input'] ) ) { |
|
319 foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) { |
|
320 // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary. |
|
321 if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|
322 continue; |
|
323 } |
|
324 |
|
325 /* |
|
326 * Assume that a 'tax_input' string is a comma-separated list of term names. |
|
327 * Some languages may use a character other than a comma as a delimiter, so we standardize on |
|
328 * commas before parsing the list. |
|
329 */ |
|
330 if ( ! is_array( $terms ) ) { |
|
331 $comma = _x( ',', 'tag delimiter' ); |
|
332 if ( ',' !== $comma ) { |
|
333 $terms = str_replace( $comma, ',', $terms ); |
|
334 } |
|
335 $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
|
336 } |
|
337 |
|
338 $clean_terms = array(); |
|
339 foreach ( $terms as $term ) { |
|
340 // Empty terms are invalid input. |
|
341 if ( empty( $term ) ) { |
|
342 continue; |
|
343 } |
|
344 |
|
345 $_term = get_terms( $taxonomy, array( |
|
346 'name' => $term, |
|
347 'fields' => 'ids', |
|
348 'hide_empty' => false, |
|
349 ) ); |
|
350 |
|
351 if ( ! empty( $_term ) ) { |
|
352 $clean_terms[] = intval( $_term[0] ); |
|
353 } else { |
|
354 // No existing term was found, so pass the string. A new term will be created. |
|
355 $clean_terms[] = $term; |
|
356 } |
|
357 } |
|
358 |
|
359 $post_data['tax_input'][ $taxonomy ] = $clean_terms; |
|
360 } |
|
361 } |
|
362 |
277 add_meta( $post_ID ); |
363 add_meta( $post_ID ); |
278 |
364 |
279 update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); |
365 update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); |
280 |
366 |
281 wp_update_post( $post_data ); |
367 $success = wp_update_post( $post_data ); |
|
368 // If the save failed, see if we can sanity check the main fields and try again |
|
369 if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { |
|
370 $fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
|
371 |
|
372 foreach( $fields as $field ) { |
|
373 if ( isset( $post_data[ $field ] ) ) { |
|
374 $post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] ); |
|
375 } |
|
376 } |
|
377 |
|
378 wp_update_post( $post_data ); |
|
379 } |
282 |
380 |
283 // Now that we have an ID we can fix any attachment anchor hrefs |
381 // Now that we have an ID we can fix any attachment anchor hrefs |
284 _fix_attachment_links( $post_ID ); |
382 _fix_attachment_links( $post_ID ); |
285 |
383 |
286 wp_set_post_lock( $post_ID ); |
384 wp_set_post_lock( $post_ID ); |
420 $cats = (array) wp_get_post_categories($post_ID); |
531 $cats = (array) wp_get_post_categories($post_ID); |
421 $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) ); |
532 $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) ); |
422 unset( $post_data['tax_input']['category'] ); |
533 unset( $post_data['tax_input']['category'] ); |
423 } |
534 } |
424 |
535 |
|
536 $post_data['post_type'] = $post->post_type; |
425 $post_data['post_mime_type'] = $post->post_mime_type; |
537 $post_data['post_mime_type'] = $post->post_mime_type; |
426 $post_data['guid'] = $post->guid; |
538 $post_data['guid'] = $post->guid; |
427 |
539 |
|
540 foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) { |
|
541 if ( ! isset( $post_data[ $field ] ) ) { |
|
542 $post_data[ $field ] = $post->$field; |
|
543 } |
|
544 } |
|
545 |
428 $post_data['ID'] = $post_ID; |
546 $post_data['ID'] = $post_ID; |
|
547 $post_data['post_ID'] = $post_ID; |
|
548 |
|
549 $post_data = _wp_translate_postdata( true, $post_data ); |
|
550 if ( is_wp_error( $post_data ) ) { |
|
551 $skipped[] = $post_ID; |
|
552 continue; |
|
553 } |
|
554 |
429 $updated[] = wp_update_post( $post_data ); |
555 $updated[] = wp_update_post( $post_data ); |
430 |
556 |
431 if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { |
557 if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { |
432 if ( 'sticky' == $post_data['sticky'] ) |
558 if ( 'sticky' == $post_data['sticky'] ) |
433 stick_post( $post_ID ); |
559 stick_post( $post_ID ); |
489 $post->post_parent = 0; |
614 $post->post_parent = 0; |
490 $post->menu_order = 0; |
615 $post->menu_order = 0; |
491 $post = new WP_Post( $post ); |
616 $post = new WP_Post( $post ); |
492 } |
617 } |
493 |
618 |
|
619 /** |
|
620 * Filter the default post content initially used in the "Write Post" form. |
|
621 * |
|
622 * @since 1.5.0 |
|
623 * |
|
624 * @param string $post_content Default post content. |
|
625 * @param WP_Post $post Post object. |
|
626 */ |
494 $post->post_content = apply_filters( 'default_content', $post_content, $post ); |
627 $post->post_content = apply_filters( 'default_content', $post_content, $post ); |
495 $post->post_title = apply_filters( 'default_title', $post_title, $post ); |
628 |
|
629 /** |
|
630 * Filter the default post title initially used in the "Write Post" form. |
|
631 * |
|
632 * @since 1.5.0 |
|
633 * |
|
634 * @param string $post_title Default post title. |
|
635 * @param WP_Post $post Post object. |
|
636 */ |
|
637 $post->post_title = apply_filters( 'default_title', $post_title, $post ); |
|
638 |
|
639 /** |
|
640 * Filter the default post excerpt initially used in the "Write Post" form. |
|
641 * |
|
642 * @since 1.5.0 |
|
643 * |
|
644 * @param string $post_excerpt Default post excerpt. |
|
645 * @param WP_Post $post Post object. |
|
646 */ |
496 $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post ); |
647 $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post ); |
497 $post->post_name = ''; |
|
498 |
648 |
499 return $post; |
649 return $post; |
500 } |
650 } |
501 |
651 |
502 /** |
652 /** |
630 // |
777 // |
631 // Post Meta |
778 // Post Meta |
632 // |
779 // |
633 |
780 |
634 /** |
781 /** |
635 * {@internal Missing Short Description}} |
782 * Add post meta data defined in $_POST superglobal for post with given ID. |
636 * |
783 * |
637 * @since 1.2.0 |
784 * @since 1.2.0 |
638 * |
785 * |
639 * @param unknown_type $post_ID |
786 * @param int $post_ID |
640 * @return unknown |
787 * @return int|bool |
641 */ |
788 */ |
642 function add_meta( $post_ID ) { |
789 function add_meta( $post_ID ) { |
643 global $wpdb; |
|
644 $post_ID = (int) $post_ID; |
790 $post_ID = (int) $post_ID; |
645 |
791 |
646 $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : ''; |
792 $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : ''; |
647 $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : ''; |
793 $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : ''; |
648 $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : ''; |
794 $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : ''; |
649 if ( is_string( $metavalue ) ) |
795 if ( is_string( $metavalue ) ) |
650 $metavalue = trim( $metavalue ); |
796 $metavalue = trim( $metavalue ); |
651 |
797 |
652 if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) { |
798 if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) { |
653 // We have a key/value pair. If both the select and the |
799 /* |
654 // input for the key have data, the input takes precedence: |
800 * We have a key/value pair. If both the select and the input |
655 |
801 * for the key have data, the input takes precedence. |
|
802 */ |
656 if ( '#NONE#' != $metakeyselect ) |
803 if ( '#NONE#' != $metakeyselect ) |
657 $metakey = $metakeyselect; |
804 $metakey = $metakeyselect; |
658 |
805 |
659 if ( $metakeyinput ) |
806 if ( $metakeyinput ) |
660 $metakey = $metakeyinput; // default |
807 $metakey = $metakeyinput; // default |
700 |
847 |
701 return $keys; |
848 return $keys; |
702 } |
849 } |
703 |
850 |
704 /** |
851 /** |
705 * {@internal Missing Short Description}} |
852 * Get post meta data by meta ID. |
706 * |
853 * |
707 * @since 2.1.0 |
854 * @since 2.1.0 |
708 * |
855 * |
709 * @param unknown_type $mid |
856 * @param int $mid |
710 * @return unknown |
857 * @return object|bool |
711 */ |
858 */ |
712 function get_post_meta_by_id( $mid ) { |
859 function get_post_meta_by_id( $mid ) { |
713 return get_metadata_by_mid( 'post', $mid ); |
860 return get_metadata_by_mid( 'post', $mid ); |
714 } |
861 } |
715 |
862 |
716 /** |
863 /** |
717 * {@internal Missing Short Description}} |
864 * Get meta data for the given post ID. |
718 * |
|
719 * Some postmeta stuff. |
|
720 * |
865 * |
721 * @since 1.2.0 |
866 * @since 1.2.0 |
722 * |
867 * |
723 * @param unknown_type $postid |
868 * @param int $postid |
724 * @return unknown |
869 * @return mixed |
725 */ |
870 */ |
726 function has_meta( $postid ) { |
871 function has_meta( $postid ) { |
727 global $wpdb; |
872 global $wpdb; |
728 |
873 |
729 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id |
874 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id |
730 FROM $wpdb->postmeta WHERE post_id = %d |
875 FROM $wpdb->postmeta WHERE post_id = %d |
731 ORDER BY meta_key,meta_id", $postid), ARRAY_A ); |
876 ORDER BY meta_key,meta_id", $postid), ARRAY_A ); |
732 } |
877 } |
733 |
878 |
734 /** |
879 /** |
735 * {@internal Missing Short Description}} |
880 * Update post meta data by meta ID. |
736 * |
881 * |
737 * @since 1.2.0 |
882 * @since 1.2.0 |
738 * |
883 * |
739 * @param unknown_type $meta_id |
884 * @param int $meta_id |
740 * @param unknown_type $meta_key Expect Slashed |
885 * @param string $meta_key Expect Slashed |
741 * @param unknown_type $meta_value Expect Slashed |
886 * @param string $meta_value Expect Slashed |
742 * @return unknown |
887 * @return bool |
743 */ |
888 */ |
744 function update_meta( $meta_id, $meta_key, $meta_value ) { |
889 function update_meta( $meta_id, $meta_key, $meta_value ) { |
745 $meta_key = wp_unslash( $meta_key ); |
890 $meta_key = wp_unslash( $meta_key ); |
746 $meta_value = wp_unslash( $meta_value ); |
891 $meta_value = wp_unslash( $meta_value ); |
747 |
892 |
881 if ( isset($q['order']) ) |
999 if ( isset($q['order']) ) |
882 $order = $q['order']; |
1000 $order = $q['order']; |
883 elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] ) |
1001 elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] ) |
884 $order = 'ASC'; |
1002 $order = 'ASC'; |
885 |
1003 |
886 $per_page = 'edit_' . $post_type . '_per_page'; |
1004 $per_page = "edit_{$post_type}_per_page"; |
887 $posts_per_page = (int) get_user_option( $per_page ); |
1005 $posts_per_page = (int) get_user_option( $per_page ); |
888 if ( empty( $posts_per_page ) || $posts_per_page < 1 ) |
1006 if ( empty( $posts_per_page ) || $posts_per_page < 1 ) |
889 $posts_per_page = 20; |
1007 $posts_per_page = 20; |
890 |
1008 |
891 $posts_per_page = apply_filters( $per_page, $posts_per_page ); |
1009 /** |
|
1010 * Filter the number of items per page to show for a specific 'per_page' type. |
|
1011 * |
|
1012 * The dynamic portion of the hook name, `$post_type`, refers to the post type. |
|
1013 * |
|
1014 * Some examples of filter hooks generated here include: 'edit_attachment_per_page', |
|
1015 * 'edit_post_per_page', 'edit_page_per_page', etc. |
|
1016 * |
|
1017 * @since 3.0.0 |
|
1018 * |
|
1019 * @param int $posts_per_page Number of posts to display per page for the given post |
|
1020 * type. Default 20. |
|
1021 */ |
|
1022 $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page ); |
|
1023 |
|
1024 /** |
|
1025 * Filter the number of posts displayed per page when specifically listing "posts". |
|
1026 * |
|
1027 * @since 2.8.0 |
|
1028 * |
|
1029 * @param int $posts_per_page Number of posts to be displayed. Default 20. |
|
1030 * @param string $post_type The post type. |
|
1031 */ |
892 $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); |
1032 $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); |
893 |
1033 |
894 $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page'); |
1034 $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page'); |
895 |
1035 |
896 // Hierarchical types require special args. |
1036 // Hierarchical types require special args. |
897 if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) { |
1037 if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) { |
898 $query['orderby'] = 'menu_order title'; |
1038 $query['orderby'] = 'menu_order title'; |
899 $query['order'] = 'asc'; |
1039 $query['order'] = 'asc'; |
900 $query['posts_per_page'] = -1; |
1040 $query['posts_per_page'] = -1; |
901 $query['posts_per_archive_page'] = -1; |
1041 $query['posts_per_archive_page'] = -1; |
|
1042 $query['fields'] = 'id=>parent'; |
902 } |
1043 } |
903 |
1044 |
904 if ( ! empty( $q['show_sticky'] ) ) |
1045 if ( ! empty( $q['show_sticky'] ) ) |
905 $query['post__in'] = (array) get_option( 'sticky_posts' ); |
1046 $query['post__in'] = (array) get_option( 'sticky_posts' ); |
906 |
1047 |
908 |
1049 |
909 return $avail_post_stati; |
1050 return $avail_post_stati; |
910 } |
1051 } |
911 |
1052 |
912 /** |
1053 /** |
913 * {@internal Missing Short Description}} |
1054 * Get all available post MIME types for a given post type. |
914 * |
1055 * |
915 * @since 2.5.0 |
1056 * @since 2.5.0 |
916 * |
1057 * |
917 * @param unknown_type $type |
1058 * @param string $type |
918 * @return unknown |
1059 * @return mixed |
919 */ |
1060 */ |
920 function get_available_post_mime_types($type = 'attachment') { |
1061 function get_available_post_mime_types($type = 'attachment') { |
921 global $wpdb; |
1062 global $wpdb; |
922 |
1063 |
923 $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type)); |
1064 $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type)); |
924 return $types; |
1065 return $types; |
925 } |
1066 } |
926 |
1067 |
927 /** |
1068 /** |
928 * Executes a query for attachments. An array of WP_Query arguments |
1069 * Get the query variables for the current attachments request. |
929 * can be passed in, which will override the arguments set by this function. |
1070 * |
930 * |
1071 * @since 4.2.0 |
931 * @since 2.5.0 |
1072 * |
932 * @uses apply_filters() Calls 'upload_per_page' on posts_per_page argument |
1073 * @param array|false $q Optional. Array of query variables to use to build the query or false |
933 * |
1074 * to use $_GET superglobal. Default false. |
934 * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal. |
1075 * @return array The parsed query vars. |
935 * @return array |
1076 */ |
936 */ |
1077 function wp_edit_attachments_query_vars( $q = false ) { |
937 function wp_edit_attachments_query( $q = false ) { |
1078 if ( false === $q ) { |
938 if ( false === $q ) |
|
939 $q = $_GET; |
1079 $q = $_GET; |
940 |
1080 } |
941 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; |
1081 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; |
942 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; |
1082 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; |
943 $q['post_type'] = 'attachment'; |
1083 $q['post_type'] = 'attachment'; |
944 $post_type = get_post_type_object( 'attachment' ); |
1084 $post_type = get_post_type_object( 'attachment' ); |
945 $states = 'inherit'; |
1085 $states = 'inherit'; |
946 if ( current_user_can( $post_type->cap->read_private_posts ) ) |
1086 if ( current_user_can( $post_type->cap->read_private_posts ) ) { |
947 $states .= ',private'; |
1087 $states .= ',private'; |
|
1088 } |
948 |
1089 |
949 $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states; |
1090 $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states; |
|
1091 $q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' == $q['attachment-filter'] ? 'trash' : $states; |
|
1092 |
950 $media_per_page = (int) get_user_option( 'upload_per_page' ); |
1093 $media_per_page = (int) get_user_option( 'upload_per_page' ); |
951 if ( empty( $media_per_page ) || $media_per_page < 1 ) |
1094 if ( empty( $media_per_page ) || $media_per_page < 1 ) { |
952 $media_per_page = 20; |
1095 $media_per_page = 20; |
|
1096 } |
|
1097 |
|
1098 /** |
|
1099 * Filter the number of items to list per page when listing media items. |
|
1100 * |
|
1101 * @since 2.9.0 |
|
1102 * |
|
1103 * @param int $media_per_page Number of media to list. Default 20. |
|
1104 */ |
953 $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page ); |
1105 $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page ); |
954 |
1106 |
955 $post_mime_types = get_post_mime_types(); |
1107 $post_mime_types = get_post_mime_types(); |
956 $avail_post_mime_types = get_available_post_mime_types('attachment'); |
1108 if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) { |
957 |
|
958 if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) |
|
959 unset($q['post_mime_type']); |
1109 unset($q['post_mime_type']); |
960 |
1110 } |
961 if ( isset($q['detached']) ) |
1111 |
962 add_filter('posts_where', '_edit_attachments_query_helper'); |
1112 foreach( array_keys( $post_mime_types ) as $type ) { |
963 |
1113 if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) { |
964 wp( $q ); |
1114 $q['post_mime_type'] = $type; |
965 |
1115 break; |
966 if ( isset($q['detached']) ) |
1116 } |
967 remove_filter('posts_where', '_edit_attachments_query_helper'); |
1117 } |
968 |
1118 |
969 return array($post_mime_types, $avail_post_mime_types); |
1119 if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) { |
970 } |
1120 $q['post_parent'] = 0; |
971 |
1121 } |
972 function _edit_attachments_query_helper($where) { |
1122 |
973 global $wpdb; |
1123 return $q; |
974 return $where .= " AND {$wpdb->posts}.post_parent < 1"; |
1124 } |
|
1125 |
|
1126 /** |
|
1127 * Executes a query for attachments. An array of WP_Query arguments |
|
1128 * can be passed in, which will override the arguments set by this function. |
|
1129 * |
|
1130 * @since 2.5.0 |
|
1131 * |
|
1132 * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal. |
|
1133 * @return array |
|
1134 */ |
|
1135 function wp_edit_attachments_query( $q = false ) { |
|
1136 wp( wp_edit_attachments_query_vars( $q ) ); |
|
1137 |
|
1138 $post_mime_types = get_post_mime_types(); |
|
1139 $avail_post_mime_types = get_available_post_mime_types( 'attachment' ); |
|
1140 |
|
1141 return array( $post_mime_types, $avail_post_mime_types ); |
975 } |
1142 } |
976 |
1143 |
977 /** |
1144 /** |
978 * Returns the list of classes to be used by a metabox |
1145 * Returns the list of classes to be used by a metabox |
979 * |
1146 * |
980 * @uses get_user_option() |
|
981 * @since 2.5.0 |
1147 * @since 2.5.0 |
982 * |
1148 * |
983 * @param unknown_type $id |
1149 * @param string $id |
984 * @param unknown_type $page |
1150 * @param string $page |
985 * @return unknown |
1151 * @return string |
986 */ |
1152 */ |
987 function postbox_classes( $id, $page ) { |
1153 function postbox_classes( $id, $page ) { |
988 if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) { |
1154 if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) { |
989 $classes = array( '' ); |
1155 $classes = array( '' ); |
990 } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) { |
1156 } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) { |
995 } |
1161 } |
996 } else { |
1162 } else { |
997 $classes = array( '' ); |
1163 $classes = array( '' ); |
998 } |
1164 } |
999 |
1165 |
|
1166 /** |
|
1167 * Filter the postbox classes for a specific screen and screen ID combo. |
|
1168 * |
|
1169 * The dynamic portions of the hook name, `$page` and `$id`, refer to |
|
1170 * the screen and screen ID, respectively. |
|
1171 * |
|
1172 * @since 3.2.0 |
|
1173 * |
|
1174 * @param array $classes An array of postbox classes. |
|
1175 */ |
1000 $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes ); |
1176 $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes ); |
1001 return implode( ' ', $classes ); |
1177 return implode( ' ', $classes ); |
1002 } |
1178 } |
1003 |
1179 |
1004 /** |
1180 /** |
1005 * {@internal Missing Short Description}} |
1181 * Get a sample permalink based off of the post name. |
1006 * |
1182 * |
1007 * @since 2.5.0 |
1183 * @since 2.5.0 |
1008 * |
1184 * |
1009 * @param int|object $id Post ID or post object. |
1185 * @param int $id Post ID or post object. |
1010 * @param string $title (optional) Title |
1186 * @param string $title Optional. Title. Default null. |
1011 * @param string $name (optional) Name |
1187 * @param string $name Optional. Name. Default null. |
1012 * @return array With two entries of type string |
1188 * @return array Array with two entries of type string. |
1013 */ |
1189 */ |
1014 function get_sample_permalink($id, $title = null, $name = null) { |
1190 function get_sample_permalink($id, $title = null, $name = null) { |
1015 $post = get_post( $id ); |
1191 $post = get_post( $id ); |
1016 if ( ! $post ) |
1192 if ( ! $post ) |
1017 return array( '', '' ); |
1193 return array( '', '' ); |
1043 $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink); |
1219 $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink); |
1044 |
1220 |
1045 // Handle page hierarchy |
1221 // Handle page hierarchy |
1046 if ( $ptype->hierarchical ) { |
1222 if ( $ptype->hierarchical ) { |
1047 $uri = get_page_uri($post); |
1223 $uri = get_page_uri($post); |
1048 $uri = untrailingslashit($uri); |
1224 if ( $uri ) { |
1049 $uri = strrev( stristr( strrev( $uri ), '/' ) ); |
1225 $uri = untrailingslashit($uri); |
1050 $uri = untrailingslashit($uri); |
1226 $uri = strrev( stristr( strrev( $uri ), '/' ) ); |
|
1227 $uri = untrailingslashit($uri); |
|
1228 } |
|
1229 |
|
1230 /** This filter is documented in wp-admin/edit-tag-form.php */ |
1051 $uri = apply_filters( 'editable_slug', $uri ); |
1231 $uri = apply_filters( 'editable_slug', $uri ); |
1052 if ( !empty($uri) ) |
1232 if ( !empty($uri) ) |
1053 $uri .= '/'; |
1233 $uri .= '/'; |
1054 $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); |
1234 $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); |
1055 } |
1235 } |
1056 |
1236 |
1057 $permalink = array($permalink, apply_filters('editable_slug', $post->post_name)); |
1237 /** This filter is documented in wp-admin/edit-tag-form.php */ |
|
1238 $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name ) ); |
1058 $post->post_status = $original_status; |
1239 $post->post_status = $original_status; |
1059 $post->post_date = $original_date; |
1240 $post->post_date = $original_date; |
1060 $post->post_name = $original_name; |
1241 $post->post_name = $original_name; |
1061 unset($post->filter); |
1242 unset($post->filter); |
1062 |
1243 |
1066 /** |
1247 /** |
1067 * Returns the HTML of the sample permalink slug editor. |
1248 * Returns the HTML of the sample permalink slug editor. |
1068 * |
1249 * |
1069 * @since 2.5.0 |
1250 * @since 2.5.0 |
1070 * |
1251 * |
1071 * @param int|object $id Post ID or post object. |
1252 * @param int $id Post ID or post object. |
1072 * @param string $new_title Optional. New title. |
1253 * @param string $new_title Optional. New title. Default null. |
1073 * @param string $new_slug Optional. New slug. |
1254 * @param string $new_slug Optional. New slug. Default null. |
1074 * @return string The HTML of the sample permalink slug editor. |
1255 * @return string The HTML of the sample permalink slug editor. |
1075 */ |
1256 */ |
1076 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { |
1257 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { |
1077 $post = get_post( $id ); |
1258 $post = get_post( $id ); |
1078 if ( ! $post ) |
1259 if ( ! $post ) |
1079 return ''; |
1260 return ''; |
1080 |
1261 |
1081 list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); |
1262 list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); |
1082 |
1263 |
|
1264 if ( current_user_can( 'read_post', $post->ID ) ) { |
|
1265 $ptype = get_post_type_object( $post->post_type ); |
|
1266 $view_post = $ptype->labels->view_item; |
|
1267 } |
|
1268 |
1083 if ( 'publish' == get_post_status( $post ) ) { |
1269 if ( 'publish' == get_post_status( $post ) ) { |
1084 $ptype = get_post_type_object($post->post_type); |
|
1085 $view_post = $ptype->labels->view_item; |
|
1086 $title = __('Click to edit this part of the permalink'); |
1270 $title = __('Click to edit this part of the permalink'); |
1087 } else { |
1271 } else { |
1088 $title = __('Temporary permalink. Click to edit this part.'); |
1272 $title = __('Temporary permalink. Click to edit this part.'); |
1089 } |
1273 } |
1090 |
1274 |
1091 if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) { |
1275 if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { |
1092 $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n"; |
1276 $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n"; |
1093 if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) |
1277 if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) { |
1094 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; |
1278 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; |
1095 if ( isset( $view_post ) ) |
1279 } |
1096 $return .= "<span id='view-post-btn'><a href='$permalink' class='button button-small'>$view_post</a></span>\n"; |
1280 } else { |
1097 |
1281 if ( function_exists( 'mb_strlen' ) ) { |
1098 $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug); |
1282 if ( mb_strlen( $post_name ) > 30 ) { |
1099 |
1283 $post_name_abridged = mb_substr( $post_name, 0, 14 ) . '…' . mb_substr( $post_name, -14 ); |
1100 return $return; |
1284 } else { |
1101 } |
1285 $post_name_abridged = $post_name; |
1102 |
1286 } |
1103 if ( function_exists('mb_strlen') ) { |
|
1104 if ( mb_strlen($post_name) > 30 ) { |
|
1105 $post_name_abridged = mb_substr($post_name, 0, 14). '…' . mb_substr($post_name, -14); |
|
1106 } else { |
1287 } else { |
1107 $post_name_abridged = $post_name; |
1288 if ( strlen( $post_name ) > 30 ) { |
1108 } |
1289 $post_name_abridged = substr( $post_name, 0, 14 ) . '…' . substr( $post_name, -14 ); |
1109 } else { |
1290 } else { |
1110 if ( strlen($post_name) > 30 ) { |
1291 $post_name_abridged = $post_name; |
1111 $post_name_abridged = substr($post_name, 0, 14). '…' . substr($post_name, -14); |
1292 } |
|
1293 } |
|
1294 |
|
1295 $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>'; |
|
1296 $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) ); |
|
1297 $pretty_permalink = str_replace( array( '%pagename%', '%postname%' ), $post_name, urldecode( $permalink ) ); |
|
1298 |
|
1299 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
|
1300 $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n"; |
|
1301 $return .= '‎'; // Fix bi-directional text display defect in RTL languages. |
|
1302 $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __( 'Edit' ) . "</a></span>\n"; |
|
1303 $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n"; |
|
1304 } |
|
1305 |
|
1306 if ( isset( $view_post ) ) { |
|
1307 if( 'draft' == $post->post_status ) { |
|
1308 $preview_link = set_url_scheme( get_permalink( $post->ID ) ); |
|
1309 /** This filter is documented in wp-admin/includes/meta-boxes.php */ |
|
1310 $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ); |
|
1311 $return .= "<span id='view-post-btn'><a href='" . esc_url( $preview_link ) . "' class='button button-small' target='wp-preview-{$post->ID}'>$view_post</a></span>\n"; |
1112 } else { |
1312 } else { |
1113 $post_name_abridged = $post_name; |
1313 if ( empty( $pretty_permalink ) ) { |
1114 } |
1314 $pretty_permalink = $permalink; |
1115 } |
1315 } |
1116 |
1316 |
1117 $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>'; |
1317 $return .= "<span id='view-post-btn'><a href='" . $pretty_permalink . "' class='button button-small'>$view_post</a></span>\n"; |
1118 $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink); |
1318 } |
1119 $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink); |
1319 } |
1120 $return = '<strong>' . __('Permalink:') . "</strong>\n"; |
1320 |
1121 $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n"; |
1321 /** |
1122 $return .= '‎'; // Fix bi-directional text display defect in RTL languages. |
1322 * Filter the sample permalink HTML markup. |
1123 $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n"; |
1323 * |
1124 $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n"; |
1324 * @since 2.9.0 |
1125 if ( isset($view_post) ) |
1325 * |
1126 $return .= "<span id='view-post-btn'><a href='$view_link' class='button button-small'>$view_post</a></span>\n"; |
1326 * @param string $return Sample permalink HTML markup. |
1127 |
1327 * @param int|WP_Post $id Post object or ID. |
1128 $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug); |
1328 * @param string $new_title New sample permalink title. |
|
1329 * @param string $new_slug New sample permalink slug. |
|
1330 */ |
|
1331 $return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug ); |
1129 |
1332 |
1130 return $return; |
1333 return $return; |
1131 } |
1334 } |
1132 |
1335 |
1133 /** |
1336 /** |
1330 * |
1585 * |
1331 * @package WordPress |
1586 * @package WordPress |
1332 * @subpackage Post_Revisions |
1587 * @subpackage Post_Revisions |
1333 * @since 2.6.0 |
1588 * @since 2.6.0 |
1334 * |
1589 * |
1335 * @uses _wp_translate_postdata() |
1590 * @param mixed $post_data Associative array containing the post data or int post ID. |
1336 * @uses _wp_post_revision_fields() |
1591 * @return mixed The autosave revision ID. WP_Error or 0 on error. |
1337 * |
1592 */ |
1338 * @return unknown |
1593 function wp_create_post_autosave( $post_data ) { |
1339 */ |
1594 if ( is_numeric( $post_data ) ) { |
1340 function wp_create_post_autosave( $post_id ) { |
1595 $post_id = $post_data; |
1341 $translated = _wp_translate_postdata( true ); |
1596 $post_data = &$_POST; |
1342 if ( is_wp_error( $translated ) ) |
1597 } else { |
1343 return $translated; |
1598 $post_id = (int) $post_data['post_ID']; |
|
1599 } |
|
1600 |
|
1601 $post_data = _wp_translate_postdata( true, $post_data ); |
|
1602 if ( is_wp_error( $post_data ) ) |
|
1603 return $post_data; |
1344 |
1604 |
1345 $post_author = get_current_user_id(); |
1605 $post_author = get_current_user_id(); |
1346 |
1606 |
1347 // Store one autosave per author. If there is already an autosave, overwrite it. |
1607 // Store one autosave per author. If there is already an autosave, overwrite it. |
1348 if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { |
1608 if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { |
1349 $new_autosave = _wp_post_revision_fields( $_POST, true ); |
1609 $new_autosave = _wp_post_revision_fields( $post_data, true ); |
1350 $new_autosave['ID'] = $old_autosave->ID; |
1610 $new_autosave['ID'] = $old_autosave->ID; |
1351 $new_autosave['post_author'] = $post_author; |
1611 $new_autosave['post_author'] = $post_author; |
1352 |
1612 |
1353 // If the new autosave is the same content as the post, delete the old autosave. |
1613 // If the new autosave has the same content as the post, delete the autosave. |
1354 $post = get_post( $post_id ); |
1614 $post = get_post( $post_id ); |
1355 $autosave_is_different = false; |
1615 $autosave_is_different = false; |
1356 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { |
1616 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) { |
1357 if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { |
1617 if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { |
1358 $autosave_is_different = true; |
1618 $autosave_is_different = true; |
1359 break; |
1619 break; |
1360 } |
1620 } |
1361 } |
1621 } |
1362 |
1622 |
1363 if ( ! $autosave_is_different ) { |
1623 if ( ! $autosave_is_different ) { |
1364 wp_delete_post_revision( $old_autosave->ID ); |
1624 wp_delete_post_revision( $old_autosave->ID ); |
1365 return; |
1625 return 0; |
1366 } |
1626 } |
|
1627 |
|
1628 /** |
|
1629 * Fires before an autosave is stored. |
|
1630 * |
|
1631 * @since 4.1.0 |
|
1632 * |
|
1633 * @param array $new_autosave Post array - the autosave that is about to be saved. |
|
1634 */ |
|
1635 do_action( 'wp_creating_autosave', $new_autosave ); |
1367 |
1636 |
1368 return wp_update_post( $new_autosave ); |
1637 return wp_update_post( $new_autosave ); |
1369 } |
1638 } |
1370 |
1639 |
1371 // _wp_put_post_revision() expects unescaped. |
1640 // _wp_put_post_revision() expects unescaped. |
1372 $post_data = wp_unslash( $_POST ); |
1641 $post_data = wp_unslash( $post_data ); |
1373 |
1642 |
1374 // Otherwise create the new autosave as a special post revision |
1643 // Otherwise create the new autosave as a special post revision |
1375 return _wp_put_post_revision( $post_data, true ); |
1644 return _wp_put_post_revision( $post_data, true ); |
1376 } |
1645 } |
1377 |
1646 |
1379 * Save draft or manually autosave for showing preview. |
1648 * Save draft or manually autosave for showing preview. |
1380 * |
1649 * |
1381 * @package WordPress |
1650 * @package WordPress |
1382 * @since 2.7.0 |
1651 * @since 2.7.0 |
1383 * |
1652 * |
1384 * @uses get_post_status() |
|
1385 * @uses edit_post() |
|
1386 * @uses get_post() |
|
1387 * @uses current_user_can() |
|
1388 * @uses wp_die() |
|
1389 * @uses wp_create_post_autosave() |
|
1390 * @uses add_query_arg() |
|
1391 * @uses wp_create_nonce() |
|
1392 * |
|
1393 * @return str URL to redirect to show the preview |
1653 * @return str URL to redirect to show the preview |
1394 */ |
1654 */ |
1395 function post_preview() { |
1655 function post_preview() { |
1396 |
1656 |
1397 $post_ID = (int) $_POST['post_ID']; |
1657 $post_ID = (int) $_POST['post_ID']; |
1398 $status = get_post_status( $post_ID ); |
|
1399 if ( 'auto-draft' == $status ) |
|
1400 wp_die( __('Preview not available. Please save as a draft first.') ); |
|
1401 |
|
1402 if ( isset($_POST['catslist']) ) |
|
1403 $_POST['post_category'] = explode(",", $_POST['catslist']); |
|
1404 |
|
1405 if ( isset($_POST['tags_input']) ) |
|
1406 $_POST['tags_input'] = explode(",", $_POST['tags_input']); |
|
1407 |
|
1408 if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) ) |
|
1409 unset($_POST['post_category']); |
|
1410 |
|
1411 $_POST['ID'] = $post_ID; |
1658 $_POST['ID'] = $post_ID; |
1412 $post = get_post($post_ID); |
1659 |
1413 |
1660 if ( ! $post = get_post( $post_ID ) ) { |
1414 if ( 'page' == $post->post_type ) { |
1661 wp_die( __( 'You are not allowed to edit this post.' ) ); |
1415 if ( ! current_user_can('edit_page', $post_ID) ) |
1662 } |
1416 wp_die( __('You are not allowed to edit this page.') ); |
1663 |
|
1664 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
|
1665 wp_die( __( 'You are not allowed to edit this post.' ) ); |
|
1666 } |
|
1667 |
|
1668 $is_autosave = false; |
|
1669 |
|
1670 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) { |
|
1671 $saved_post_id = edit_post(); |
1417 } else { |
1672 } else { |
1418 if ( ! current_user_can('edit_post', $post_ID) ) |
1673 $is_autosave = true; |
1419 wp_die( __('You are not allowed to edit this post.') ); |
1674 |
1420 } |
1675 if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] ) |
1421 |
1676 $_POST['post_status'] = 'draft'; |
1422 $user_id = get_current_user_id(); |
1677 |
1423 $locked = wp_check_post_lock( $post->ID ); |
1678 $saved_post_id = wp_create_post_autosave( $post->ID ); |
1424 if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) { |
1679 } |
1425 $id = edit_post(); |
1680 |
1426 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision. |
1681 if ( is_wp_error( $saved_post_id ) ) |
1427 $id = wp_create_post_autosave( $post->ID ); |
1682 wp_die( $saved_post_id->get_error_message() ); |
1428 if ( ! is_wp_error($id) ) |
1683 |
1429 $id = $post->ID; |
1684 $query_args = array( 'preview' => 'true' ); |
1430 } |
1685 |
1431 |
1686 if ( $is_autosave && $saved_post_id ) { |
1432 if ( is_wp_error($id) ) |
1687 $query_args['preview_id'] = $post->ID; |
1433 wp_die( $id->get_error_message() ); |
1688 $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID ); |
1434 |
1689 |
1435 if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) { |
1690 if ( isset( $_POST['post_format'] ) ) |
1436 $url = add_query_arg( 'preview', 'true', get_permalink($id) ); |
1691 $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] ); |
|
1692 } |
|
1693 |
|
1694 $url = add_query_arg( $query_args, get_permalink( $post->ID ) ); |
|
1695 |
|
1696 /** This filter is documented in wp-admin/includes/meta-boxes.php */ |
|
1697 return apply_filters( 'preview_post_link', $url, $post ); |
|
1698 } |
|
1699 |
|
1700 /** |
|
1701 * Save a post submitted with XHR |
|
1702 * |
|
1703 * Intended for use with heartbeat and autosave.js |
|
1704 * |
|
1705 * @since 3.9.0 |
|
1706 * |
|
1707 * @param array $post_data Associative array of the submitted post data. |
|
1708 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success. |
|
1709 * Te ID can be the draft post_id or the autosave revision post_id. |
|
1710 */ |
|
1711 function wp_autosave( $post_data ) { |
|
1712 // Back-compat |
|
1713 if ( ! defined( 'DOING_AUTOSAVE' ) ) |
|
1714 define( 'DOING_AUTOSAVE', true ); |
|
1715 |
|
1716 $post_id = (int) $post_data['post_id']; |
|
1717 $post_data['ID'] = $post_data['post_ID'] = $post_id; |
|
1718 |
|
1719 if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { |
|
1720 return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); |
|
1721 } |
|
1722 |
|
1723 $post = get_post( $post_id ); |
|
1724 |
|
1725 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
|
1726 return new WP_Error( 'edit_posts', __( 'You are not allowed to edit this item.' ) ); |
|
1727 } |
|
1728 |
|
1729 if ( 'auto-draft' == $post->post_status ) |
|
1730 $post_data['post_status'] = 'draft'; |
|
1731 |
|
1732 if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) ) |
|
1733 $post_data['post_category'] = explode( ',', $post_data['catslist'] ); |
|
1734 |
|
1735 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) { |
|
1736 // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked |
|
1737 return edit_post( wp_slash( $post_data ) ); |
1437 } else { |
1738 } else { |
1438 $nonce = wp_create_nonce('post_preview_' . $id); |
1739 // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. |
1439 $args = array( |
1740 return wp_create_post_autosave( wp_slash( $post_data ) ); |
1440 'preview' => 'true', |
1741 } |
1441 'preview_id' => $id, |
1742 } |
1442 'preview_nonce' => $nonce, |
|
1443 ); |
|
1444 |
|
1445 if ( isset( $_POST['post_format'] ) ) |
|
1446 $args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] ); |
|
1447 |
|
1448 $url = add_query_arg( $args, get_permalink($id) ); |
|
1449 } |
|
1450 |
|
1451 return apply_filters( 'preview_post_link', $url ); |
|
1452 } |
|