28 |
27 |
29 $ptype = get_post_type_object( $post_data['post_type'] ); |
28 $ptype = get_post_type_object( $post_data['post_type'] ); |
30 |
29 |
31 if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { |
30 if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { |
32 if ( 'page' == $post_data['post_type'] ) |
31 if ( 'page' == $post_data['post_type'] ) |
33 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) ); |
32 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
34 else |
33 else |
35 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) ); |
34 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
36 } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { |
35 } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { |
37 if ( 'page' == $post_data['post_type'] ) |
36 if ( 'page' == $post_data['post_type'] ) |
38 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) ); |
37 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
39 else |
38 else |
40 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) ); |
39 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
41 } |
40 } |
42 |
41 |
43 if ( isset( $post_data['content'] ) ) |
42 if ( isset( $post_data['content'] ) ) |
44 $post_data['post_content'] = $post_data['content']; |
43 $post_data['post_content'] = $post_data['content']; |
45 |
44 |
66 |
65 |
67 if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) |
66 if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) |
68 && ! current_user_can( $ptype->cap->edit_others_posts ) ) { |
67 && ! current_user_can( $ptype->cap->edit_others_posts ) ) { |
69 if ( $update ) { |
68 if ( $update ) { |
70 if ( 'page' == $post_data['post_type'] ) |
69 if ( 'page' == $post_data['post_type'] ) |
71 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) ); |
70 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); |
72 else |
71 else |
73 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) ); |
72 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); |
74 } else { |
73 } else { |
75 if ( 'page' == $post_data['post_type'] ) |
74 if ( 'page' == $post_data['post_type'] ) |
76 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) ); |
75 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); |
77 else |
76 else |
78 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) ); |
77 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); |
79 } |
78 } |
80 } |
79 } |
81 |
80 |
82 if ( ! empty( $post_data['post_status'] ) ) { |
81 if ( ! empty( $post_data['post_status'] ) ) { |
83 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
82 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); |
158 $mn = ($mn > 59 ) ? $mn -60 : $mn; |
157 $mn = ($mn > 59 ) ? $mn -60 : $mn; |
159 $ss = ($ss > 59 ) ? $ss -60 : $ss; |
158 $ss = ($ss > 59 ) ? $ss -60 : $ss; |
160 $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss ); |
159 $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss ); |
161 $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); |
160 $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); |
162 if ( !$valid_date ) { |
161 if ( !$valid_date ) { |
163 return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) ); |
162 return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
164 } |
163 } |
165 $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); |
164 $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); |
166 } |
165 } |
167 |
166 |
|
167 if ( isset( $post_data['post_category'] ) ) { |
|
168 $category_object = get_taxonomy( 'category' ); |
|
169 if ( ! current_user_can( $category_object->cap->assign_terms ) ) { |
|
170 unset( $post_data['post_category'] ); |
|
171 } |
|
172 } |
|
173 |
168 return $post_data; |
174 return $post_data; |
169 } |
175 } |
170 |
176 |
171 /** |
177 /** |
172 * Update an existing post with values provided in $_POST. |
178 * Update an existing post with values provided in $_POST. |
173 * |
179 * |
174 * @since 1.5.0 |
180 * @since 1.5.0 |
|
181 * |
|
182 * @global wpdb $wpdb WordPress database abstraction object. |
175 * |
183 * |
176 * @param array $post_data Optional. |
184 * @param array $post_data Optional. |
177 * @return int Post ID. |
185 * @return int Post ID. |
178 */ |
186 */ |
179 function edit_post( $post_data = null ) { |
187 function edit_post( $post_data = null ) { |
550 if ( is_wp_error( $post_data ) ) { |
562 if ( is_wp_error( $post_data ) ) { |
551 $skipped[] = $post_ID; |
563 $skipped[] = $post_ID; |
552 continue; |
564 continue; |
553 } |
565 } |
554 |
566 |
|
567 if ( isset( $post_data['post_format'] ) ) { |
|
568 set_post_format( $post_ID, $post_data['post_format'] ); |
|
569 unset( $post_data['tax_input']['post_format'] ); |
|
570 } |
|
571 |
555 $updated[] = wp_update_post( $post_data ); |
572 $updated[] = wp_update_post( $post_data ); |
556 |
573 |
557 if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { |
574 if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { |
558 if ( 'sticky' == $post_data['sticky'] ) |
575 if ( 'sticky' == $post_data['sticky'] ) |
559 stick_post( $post_ID ); |
576 stick_post( $post_ID ); |
560 else |
577 else |
561 unstick_post( $post_ID ); |
578 unstick_post( $post_ID ); |
562 } |
579 } |
563 |
|
564 if ( isset( $post_data['post_format'] ) ) |
|
565 set_post_format( $post_ID, $post_data['post_format'] ); |
|
566 } |
580 } |
567 |
581 |
568 return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked ); |
582 return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked ); |
569 } |
583 } |
570 |
584 |
571 /** |
585 /** |
572 * Default post information to use when populating the "Write Post" form. |
586 * Default post information to use when populating the "Write Post" form. |
573 * |
587 * |
574 * @since 2.0.0 |
588 * @since 2.0.0 |
575 * |
589 * |
576 * @param string $post_type A post type string, defaults to 'post'. |
590 * @param string $post_type Optional. A post type string. Default 'post'. |
|
591 * @param bool $create_in_db Optional. Whether to insert the post into database. Default false. |
577 * @return WP_Post Post object containing all the default post data as attributes |
592 * @return WP_Post Post object containing all the default post data as attributes |
578 */ |
593 */ |
579 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) { |
594 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) { |
580 $post_title = ''; |
595 $post_title = ''; |
581 if ( !empty( $_REQUEST['post_title'] ) ) |
596 if ( !empty( $_REQUEST['post_title'] ) ) |
604 $post->post_name = ''; |
619 $post->post_name = ''; |
605 $post->post_type = $post_type; |
620 $post->post_type = $post_type; |
606 $post->post_status = 'draft'; |
621 $post->post_status = 'draft'; |
607 $post->to_ping = ''; |
622 $post->to_ping = ''; |
608 $post->pinged = ''; |
623 $post->pinged = ''; |
609 $post->comment_status = get_option( 'default_comment_status' ); |
624 $post->comment_status = get_default_comment_status( $post_type ); |
610 $post->ping_status = get_option( 'default_ping_status' ); |
625 $post->ping_status = get_default_comment_status( $post_type, 'pingback' ); |
611 $post->post_pingback = get_option( 'default_pingback_flag' ); |
626 $post->post_pingback = get_option( 'default_pingback_flag' ); |
612 $post->post_category = get_option( 'default_category' ); |
627 $post->post_category = get_option( 'default_category' ); |
613 $post->page_template = 'default'; |
628 $post->page_template = 'default'; |
614 $post->post_parent = 0; |
629 $post->post_parent = 0; |
615 $post->menu_order = 0; |
630 $post->menu_order = 0; |
616 $post = new WP_Post( $post ); |
631 $post = new WP_Post( $post ); |
617 } |
632 } |
618 |
633 |
619 /** |
634 /** |
620 * Filter the default post content initially used in the "Write Post" form. |
635 * Filters the default post content initially used in the "Write Post" form. |
621 * |
636 * |
622 * @since 1.5.0 |
637 * @since 1.5.0 |
623 * |
638 * |
624 * @param string $post_content Default post content. |
639 * @param string $post_content Default post content. |
625 * @param WP_Post $post Post object. |
640 * @param WP_Post $post Post object. |
626 */ |
641 */ |
627 $post->post_content = apply_filters( 'default_content', $post_content, $post ); |
642 $post->post_content = apply_filters( 'default_content', $post_content, $post ); |
628 |
643 |
629 /** |
644 /** |
630 * Filter the default post title initially used in the "Write Post" form. |
645 * Filters the default post title initially used in the "Write Post" form. |
631 * |
646 * |
632 * @since 1.5.0 |
647 * @since 1.5.0 |
633 * |
648 * |
634 * @param string $post_title Default post title. |
649 * @param string $post_title Default post title. |
635 * @param WP_Post $post Post object. |
650 * @param WP_Post $post Post object. |
636 */ |
651 */ |
637 $post->post_title = apply_filters( 'default_title', $post_title, $post ); |
652 $post->post_title = apply_filters( 'default_title', $post_title, $post ); |
638 |
653 |
639 /** |
654 /** |
640 * Filter the default post excerpt initially used in the "Write Post" form. |
655 * Filters the default post excerpt initially used in the "Write Post" form. |
641 * |
656 * |
642 * @since 1.5.0 |
657 * @since 1.5.0 |
643 * |
658 * |
644 * @param string $post_excerpt Default post excerpt. |
659 * @param string $post_excerpt Default post excerpt. |
645 * @param WP_Post $post Post object. |
660 * @param WP_Post $post Post object. |
989 if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) { |
1012 if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) { |
990 $post_status = $q['post_status']; |
1013 $post_status = $q['post_status']; |
991 $perm = 'readable'; |
1014 $perm = 'readable'; |
992 } |
1015 } |
993 |
1016 |
994 if ( isset($q['orderby']) ) |
1017 if ( isset( $q['orderby'] ) ) { |
995 $orderby = $q['orderby']; |
1018 $orderby = $q['orderby']; |
996 elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) ) |
1019 } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) { |
997 $orderby = 'modified'; |
1020 $orderby = 'modified'; |
998 |
1021 } |
999 if ( isset($q['order']) ) |
1022 |
|
1023 if ( isset( $q['order'] ) ) { |
1000 $order = $q['order']; |
1024 $order = $q['order']; |
1001 elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] ) |
1025 } elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) { |
1002 $order = 'ASC'; |
1026 $order = 'ASC'; |
|
1027 } |
1003 |
1028 |
1004 $per_page = "edit_{$post_type}_per_page"; |
1029 $per_page = "edit_{$post_type}_per_page"; |
1005 $posts_per_page = (int) get_user_option( $per_page ); |
1030 $posts_per_page = (int) get_user_option( $per_page ); |
1006 if ( empty( $posts_per_page ) || $posts_per_page < 1 ) |
1031 if ( empty( $posts_per_page ) || $posts_per_page < 1 ) |
1007 $posts_per_page = 20; |
1032 $posts_per_page = 20; |
1008 |
1033 |
1009 /** |
1034 /** |
1010 * Filter the number of items per page to show for a specific 'per_page' type. |
1035 * Filters the number of items per page to show for a specific 'per_page' type. |
1011 * |
1036 * |
1012 * The dynamic portion of the hook name, `$post_type`, refers to the post type. |
1037 * The dynamic portion of the hook name, `$post_type`, refers to the post type. |
1013 * |
1038 * |
1014 * Some examples of filter hooks generated here include: 'edit_attachment_per_page', |
1039 * Some examples of filter hooks generated here include: 'edit_attachment_per_page', |
1015 * 'edit_post_per_page', 'edit_page_per_page', etc. |
1040 * 'edit_post_per_page', 'edit_page_per_page', etc. |
1226 $uri = strrev( stristr( strrev( $uri ), '/' ) ); |
1262 $uri = strrev( stristr( strrev( $uri ), '/' ) ); |
1227 $uri = untrailingslashit($uri); |
1263 $uri = untrailingslashit($uri); |
1228 } |
1264 } |
1229 |
1265 |
1230 /** This filter is documented in wp-admin/edit-tag-form.php */ |
1266 /** This filter is documented in wp-admin/edit-tag-form.php */ |
1231 $uri = apply_filters( 'editable_slug', $uri ); |
1267 $uri = apply_filters( 'editable_slug', $uri, $post ); |
1232 if ( !empty($uri) ) |
1268 if ( !empty($uri) ) |
1233 $uri .= '/'; |
1269 $uri .= '/'; |
1234 $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); |
1270 $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); |
1235 } |
1271 } |
1236 |
1272 |
1237 /** This filter is documented in wp-admin/edit-tag-form.php */ |
1273 /** This filter is documented in wp-admin/edit-tag-form.php */ |
1238 $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name ) ); |
1274 $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) ); |
1239 $post->post_status = $original_status; |
1275 $post->post_status = $original_status; |
1240 $post->post_date = $original_date; |
1276 $post->post_date = $original_date; |
1241 $post->post_name = $original_name; |
1277 $post->post_name = $original_name; |
1242 unset($post->filter); |
1278 unset($post->filter); |
1243 |
1279 |
1244 return $permalink; |
1280 /** |
|
1281 * Filters the sample permalink. |
|
1282 * |
|
1283 * @since 4.4.0 |
|
1284 * |
|
1285 * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. |
|
1286 * @param int $post_id Post ID. |
|
1287 * @param string $title Post title. |
|
1288 * @param string $name Post name (slug). |
|
1289 * @param WP_Post $post Post object. |
|
1290 */ |
|
1291 return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post ); |
1245 } |
1292 } |
1246 |
1293 |
1247 /** |
1294 /** |
1248 * Returns the HTML of the sample permalink slug editor. |
1295 * Returns the HTML of the sample permalink slug editor. |
1249 * |
1296 * |
1259 if ( ! $post ) |
1306 if ( ! $post ) |
1260 return ''; |
1307 return ''; |
1261 |
1308 |
1262 list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); |
1309 list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); |
1263 |
1310 |
|
1311 $view_link = false; |
|
1312 $preview_target = ''; |
|
1313 |
1264 if ( current_user_can( 'read_post', $post->ID ) ) { |
1314 if ( current_user_can( 'read_post', $post->ID ) ) { |
1265 $ptype = get_post_type_object( $post->post_type ); |
1315 if ( 'draft' === $post->post_status || empty( $post->post_name ) ) { |
1266 $view_post = $ptype->labels->view_item; |
1316 $view_link = get_preview_post_link( $post ); |
1267 } |
1317 $preview_target = " target='wp-preview-{$post->ID}'"; |
1268 |
1318 } else { |
1269 if ( 'publish' == get_post_status( $post ) ) { |
1319 if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { |
1270 $title = __('Click to edit this part of the permalink'); |
1320 $view_link = get_permalink( $post ); |
1271 } else { |
1321 } else { |
1272 $title = __('Temporary permalink. Click to edit this part.'); |
1322 // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set |
1273 } |
1323 $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); |
1274 |
1324 } |
|
1325 } |
|
1326 } |
|
1327 |
|
1328 // Permalinks without a post/page name placeholder don't have anything to edit |
1275 if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { |
1329 if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { |
1276 $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n"; |
1330 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
|
1331 |
|
1332 if ( false !== $view_link ) { |
|
1333 $display_link = urldecode( $view_link ); |
|
1334 $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n"; |
|
1335 } else { |
|
1336 $return .= '<span id="sample-permalink">' . $permalink . "</span>\n"; |
|
1337 } |
|
1338 |
|
1339 // Encourage a pretty permalink setting |
1277 if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) { |
1340 if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) { |
1278 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; |
1341 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; |
1279 } |
1342 } |
1280 } else { |
1343 } else { |
1281 if ( function_exists( 'mb_strlen' ) ) { |
1344 if ( mb_strlen( $post_name ) > 34 ) { |
1282 if ( mb_strlen( $post_name ) > 30 ) { |
1345 $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 ); |
1283 $post_name_abridged = mb_substr( $post_name, 0, 14 ) . '…' . mb_substr( $post_name, -14 ); |
|
1284 } else { |
|
1285 $post_name_abridged = $post_name; |
|
1286 } |
|
1287 } else { |
1346 } else { |
1288 if ( strlen( $post_name ) > 30 ) { |
1347 $post_name_abridged = $post_name; |
1289 $post_name_abridged = substr( $post_name, 0, 14 ) . '…' . substr( $post_name, -14 ); |
1348 } |
1290 } else { |
1349 |
1291 $post_name_abridged = $post_name; |
1350 $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>'; |
1292 } |
1351 $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) ); |
1293 } |
1352 |
1294 |
1353 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; |
1295 $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>'; |
1354 $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n"; |
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. |
1355 $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"; |
1356 $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n"; |
1303 $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n"; |
1357 $return .= '<span id="editable-post-name-full">' . esc_html( $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"; |
|
1312 } else { |
|
1313 if ( empty( $pretty_permalink ) ) { |
|
1314 $pretty_permalink = $permalink; |
|
1315 } |
|
1316 |
|
1317 $return .= "<span id='view-post-btn'><a href='" . $pretty_permalink . "' class='button button-small'>$view_post</a></span>\n"; |
|
1318 } |
|
1319 } |
1358 } |
1320 |
1359 |
1321 /** |
1360 /** |
1322 * Filter the sample permalink HTML markup. |
1361 * Filters the sample permalink HTML markup. |
1323 * |
1362 * |
1324 * @since 2.9.0 |
1363 * @since 2.9.0 |
1325 * |
1364 * @since 4.4.0 Added `$post` parameter. |
1326 * @param string $return Sample permalink HTML markup. |
1365 * |
1327 * @param int|WP_Post $id Post object or ID. |
1366 * @param string $return Sample permalink HTML markup. |
1328 * @param string $new_title New sample permalink title. |
1367 * @param int $post_id Post ID. |
1329 * @param string $new_slug New sample permalink slug. |
1368 * @param string $new_title New sample permalink title. |
|
1369 * @param string $new_slug New sample permalink slug. |
|
1370 * @param WP_Post $post Post object. |
1330 */ |
1371 */ |
1331 $return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug ); |
1372 $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post ); |
1332 |
1373 |
1333 return $return; |
1374 return $return; |
1334 } |
1375 } |
1335 |
1376 |
1336 /** |
1377 /** |
1341 * @param int $thumbnail_id ID of the attachment used for thumbnail |
1382 * @param int $thumbnail_id ID of the attachment used for thumbnail |
1342 * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post. |
1383 * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post. |
1343 * @return string html |
1384 * @return string html |
1344 */ |
1385 */ |
1345 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { |
1386 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { |
1346 global $content_width, $_wp_additional_image_sizes; |
1387 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
1347 |
1388 |
1348 $post = get_post( $post ); |
1389 $post = get_post( $post ); |
1349 |
1390 $post_type_object = get_post_type_object( $post->post_type ); |
1350 $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) ); |
1391 $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>'; |
1351 $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>'; |
1392 $upload_iframe_src = get_upload_iframe_src( 'image', $post->ID ); |
1352 $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) ); |
1393 |
|
1394 $content = sprintf( $set_thumbnail_link, |
|
1395 esc_url( $upload_iframe_src ), |
|
1396 '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise. |
|
1397 esc_html( $post_type_object->labels->set_featured_image ) |
|
1398 ); |
1353 |
1399 |
1354 if ( $thumbnail_id && get_post( $thumbnail_id ) ) { |
1400 if ( $thumbnail_id && get_post( $thumbnail_id ) ) { |
1355 $old_content_width = $content_width; |
1401 $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 ); |
1356 $content_width = 266; |
1402 |
1357 if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) ) |
1403 /** |
1358 $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) ); |
1404 * Filters the size used to display the post thumbnail image in the 'Featured Image' meta box. |
1359 else |
1405 * |
1360 $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' ); |
1406 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' |
1361 if ( !empty( $thumbnail_html ) ) { |
1407 * image size is registered, which differs from the 'thumbnail' image size |
1362 $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID ); |
1408 * managed via the Settings > Media screen. See the `$size` parameter description |
1363 $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html ); |
1409 * for more information on default values. |
1364 $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>'; |
1410 * |
1365 } |
1411 * @since 4.4.0 |
1366 $content_width = $old_content_width; |
1412 * |
1367 } |
1413 * @param string|array $size Post thumbnail image size to display in the meta box. Accepts any valid |
|
1414 * image size, or an array of width and height values in pixels (in that order). |
|
1415 * If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise, |
|
1416 * default is an array with 266 as both the height and width values. |
|
1417 * @param int $thumbnail_id Post thumbnail attachment ID. |
|
1418 * @param WP_Post $post The post object associated with the thumbnail. |
|
1419 */ |
|
1420 $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post ); |
|
1421 |
|
1422 $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size ); |
|
1423 |
|
1424 if ( ! empty( $thumbnail_html ) ) { |
|
1425 $content = sprintf( $set_thumbnail_link, |
|
1426 esc_url( $upload_iframe_src ), |
|
1427 ' aria-describedby="set-post-thumbnail-desc"', |
|
1428 $thumbnail_html |
|
1429 ); |
|
1430 $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>'; |
|
1431 $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>'; |
|
1432 } |
|
1433 } |
|
1434 |
|
1435 $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />'; |
1368 |
1436 |
1369 /** |
1437 /** |
1370 * Filter the admin post thumbnail HTML markup to return. |
1438 * Filters the admin post thumbnail HTML markup to return. |
1371 * |
1439 * |
1372 * @since 2.9.0 |
1440 * @since 2.9.0 |
1373 * |
1441 * @since 3.5.0 Added the `$post_id` parameter. |
1374 * @param string $content Admin post thumbnail HTML markup. |
1442 * @since 4.6.0 Added the `$thumbnail_id` parameter. |
1375 * @param int $post_id Post ID. |
1443 * |
|
1444 * @param string $content Admin post thumbnail HTML markup. |
|
1445 * @param int $post_id Post ID. |
|
1446 * @param int $thumbnail_id Thumbnail ID. |
1376 */ |
1447 */ |
1377 return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID ); |
1448 return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id ); |
1378 } |
1449 } |
1379 |
1450 |
1380 /** |
1451 /** |
1381 * Check to see if the post is currently being edited by another user. |
1452 * Check to see if the post is currently being edited by another user. |
1382 * |
1453 * |
1383 * @since 2.5.0 |
1454 * @since 2.5.0 |
1384 * |
1455 * |
1385 * @param int $post_id ID of the post to check for editing |
1456 * @param int $post_id ID of the post to check for editing. |
1386 * @return integer False: not locked or locked by current user. Int: user ID of user with lock. |
1457 * @return int|false ID of the user with lock. False if the post does not exist, post is not locked, |
|
1458 * the user with lock does not exist, or the post is locked by current user. |
1387 */ |
1459 */ |
1388 function wp_check_post_lock( $post_id ) { |
1460 function wp_check_post_lock( $post_id ) { |
1389 if ( !$post = get_post( $post_id ) ) |
1461 if ( ! $post = get_post( $post_id ) ) { |
1390 return false; |
1462 return false; |
1391 |
1463 } |
1392 if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) ) |
1464 |
|
1465 if ( ! $lock = get_post_meta( $post->ID, '_edit_lock', true ) ) { |
1393 return false; |
1466 return false; |
|
1467 } |
1394 |
1468 |
1395 $lock = explode( ':', $lock ); |
1469 $lock = explode( ':', $lock ); |
1396 $time = $lock[0]; |
1470 $time = $lock[0]; |
1397 $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); |
1471 $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); |
1398 |
1472 |
|
1473 if ( ! get_userdata( $user ) ) { |
|
1474 return false; |
|
1475 } |
|
1476 |
1399 /** This filter is documented in wp-admin/includes/ajax-actions.php */ |
1477 /** This filter is documented in wp-admin/includes/ajax-actions.php */ |
1400 $time_window = apply_filters( 'wp_check_post_lock_window', 150 ); |
1478 $time_window = apply_filters( 'wp_check_post_lock_window', 150 ); |
1401 |
1479 |
1402 if ( $time && $time > time() - $time_window && $user != get_current_user_id() ) |
1480 if ( $time && $time > time() - $time_window && $user != get_current_user_id() ) { |
1403 return $user; |
1481 return $user; |
|
1482 } |
|
1483 |
1404 return false; |
1484 return false; |
1405 } |
1485 } |
1406 |
1486 |
1407 /** |
1487 /** |
1408 * Mark the post as currently being edited by the current user |
1488 * Mark the post as currently being edited by the current user |
1409 * |
1489 * |
1410 * @since 2.5.0 |
1490 * @since 2.5.0 |
1411 * |
1491 * |
1412 * @param int $post_id ID of the post to being edited |
1492 * @param int $post_id ID of the post being edited. |
1413 * @return bool|array Returns false if the post doesn't exist of there is no current user, or |
1493 * @return array|false Array of the lock time and user ID. False if the post does not exist, or |
1414 * an array of the lock time and the user ID. |
1494 * there is no current user. |
1415 */ |
1495 */ |
1416 function wp_set_post_lock( $post_id ) { |
1496 function wp_set_post_lock( $post_id ) { |
1417 if ( !$post = get_post( $post_id ) ) |
1497 if ( ! $post = get_post( $post_id ) ) { |
1418 return false; |
1498 return false; |
1419 if ( 0 == ($user_id = get_current_user_id()) ) |
1499 } |
|
1500 |
|
1501 if ( 0 == ( $user_id = get_current_user_id() ) ) { |
1420 return false; |
1502 return false; |
|
1503 } |
1421 |
1504 |
1422 $now = time(); |
1505 $now = time(); |
1423 $lock = "$now:$user_id"; |
1506 $lock = "$now:$user_id"; |
1424 |
1507 |
1425 update_post_meta( $post->ID, '_edit_lock', $lock ); |
1508 update_post_meta( $post->ID, '_edit_lock', $lock ); |
|
1509 |
1426 return array( $now, $user_id ); |
1510 return array( $now, $user_id ); |
1427 } |
1511 } |
1428 |
1512 |
1429 /** |
1513 /** |
1430 * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
1514 * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
1480 <div class="notification-dialog-background"></div> |
1565 <div class="notification-dialog-background"></div> |
1481 <div class="notification-dialog"> |
1566 <div class="notification-dialog"> |
1482 <?php |
1567 <?php |
1483 |
1568 |
1484 if ( $locked ) { |
1569 if ( $locked ) { |
|
1570 $query_args = array(); |
1485 if ( get_post_type_object( $post->post_type )->public ) { |
1571 if ( get_post_type_object( $post->post_type )->public ) { |
1486 $preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ); |
|
1487 |
|
1488 if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) { |
1572 if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) { |
1489 // Latest content is in autosave |
1573 // Latest content is in autosave |
1490 $nonce = wp_create_nonce( 'post_preview_' . $post->ID ); |
1574 $nonce = wp_create_nonce( 'post_preview_' . $post->ID ); |
1491 $preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link ); |
1575 $query_args['preview_id'] = $post->ID; |
|
1576 $query_args['preview_nonce'] = $nonce; |
1492 } |
1577 } |
1493 } else { |
1578 } |
1494 $preview_link = ''; |
1579 |
1495 } |
1580 $preview_link = get_preview_post_link( $post->ID, $query_args ); |
1496 |
|
1497 /** This filter is documented in wp-admin/includes/meta-boxes.php */ |
|
1498 $preview_link = apply_filters( 'preview_post_link', $preview_link, $post ); |
|
1499 |
1581 |
1500 /** |
1582 /** |
1501 * Filter whether to allow the post lock to be overridden. |
1583 * Filters whether to allow the post lock to be overridden. |
1502 * |
1584 * |
1503 * Returning a falsey value to the filter will disable the ability |
1585 * Returning a falsey value to the filter will disable the ability |
1504 * to override the post lock. |
1586 * to override the post lock. |
1505 * |
1587 * |
1506 * @since 3.6.0 |
1588 * @since 3.6.0 |
1604 |
1688 |
1605 $post_author = get_current_user_id(); |
1689 $post_author = get_current_user_id(); |
1606 |
1690 |
1607 // Store one autosave per author. If there is already an autosave, overwrite it. |
1691 // Store one autosave per author. If there is already an autosave, overwrite it. |
1608 if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { |
1692 if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { |
1609 $new_autosave = _wp_post_revision_fields( $post_data, true ); |
1693 $new_autosave = _wp_post_revision_data( $post_data, true ); |
1610 $new_autosave['ID'] = $old_autosave->ID; |
1694 $new_autosave['ID'] = $old_autosave->ID; |
1611 $new_autosave['post_author'] = $post_author; |
1695 $new_autosave['post_author'] = $post_author; |
1612 |
1696 |
1613 // If the new autosave has the same content as the post, delete the autosave. |
1697 // If the new autosave has the same content as the post, delete the autosave. |
1614 $post = get_post( $post_id ); |
1698 $post = get_post( $post_id ); |
1615 $autosave_is_different = false; |
1699 $autosave_is_different = false; |
1616 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) { |
1700 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { |
1617 if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { |
1701 if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { |
1618 $autosave_is_different = true; |
1702 $autosave_is_different = true; |
1619 break; |
1703 break; |
1620 } |
1704 } |
1621 } |
1705 } |
1643 // Otherwise create the new autosave as a special post revision |
1727 // Otherwise create the new autosave as a special post revision |
1644 return _wp_put_post_revision( $post_data, true ); |
1728 return _wp_put_post_revision( $post_data, true ); |
1645 } |
1729 } |
1646 |
1730 |
1647 /** |
1731 /** |
1648 * Save draft or manually autosave for showing preview. |
1732 * Saves a draft or manually autosaves for the purpose of showing a post preview. |
1649 * |
1733 * |
1650 * @package WordPress |
|
1651 * @since 2.7.0 |
1734 * @since 2.7.0 |
1652 * |
1735 * |
1653 * @return str URL to redirect to show the preview |
1736 * @return string URL to redirect to show the preview. |
1654 */ |
1737 */ |
1655 function post_preview() { |
1738 function post_preview() { |
1656 |
1739 |
1657 $post_ID = (int) $_POST['post_ID']; |
1740 $post_ID = (int) $_POST['post_ID']; |
1658 $_POST['ID'] = $post_ID; |
1741 $_POST['ID'] = $post_ID; |
1659 |
1742 |
1660 if ( ! $post = get_post( $post_ID ) ) { |
1743 if ( ! $post = get_post( $post_ID ) ) { |
1661 wp_die( __( 'You are not allowed to edit this post.' ) ); |
1744 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
1662 } |
1745 } |
1663 |
1746 |
1664 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
1747 if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
1665 wp_die( __( 'You are not allowed to edit this post.' ) ); |
1748 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); |
1666 } |
1749 } |
1667 |
1750 |
1668 $is_autosave = false; |
1751 $is_autosave = false; |
1669 |
1752 |
1670 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) { |
1753 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) { |
1738 } else { |
1823 } else { |
1739 // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. |
1824 // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. |
1740 return wp_create_post_autosave( wp_slash( $post_data ) ); |
1825 return wp_create_post_autosave( wp_slash( $post_data ) ); |
1741 } |
1826 } |
1742 } |
1827 } |
|
1828 |
|
1829 /** |
|
1830 * Redirect to previous page. |
|
1831 * |
|
1832 * @param int $post_id Optional. Post ID. |
|
1833 */ |
|
1834 function redirect_post($post_id = '') { |
|
1835 if ( isset($_POST['save']) || isset($_POST['publish']) ) { |
|
1836 $status = get_post_status( $post_id ); |
|
1837 |
|
1838 if ( isset( $_POST['publish'] ) ) { |
|
1839 switch ( $status ) { |
|
1840 case 'pending': |
|
1841 $message = 8; |
|
1842 break; |
|
1843 case 'future': |
|
1844 $message = 9; |
|
1845 break; |
|
1846 default: |
|
1847 $message = 6; |
|
1848 } |
|
1849 } else { |
|
1850 $message = 'draft' == $status ? 10 : 1; |
|
1851 } |
|
1852 |
|
1853 $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) ); |
|
1854 } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) { |
|
1855 $location = add_query_arg( 'message', 2, wp_get_referer() ); |
|
1856 $location = explode('#', $location); |
|
1857 $location = $location[0] . '#postcustom'; |
|
1858 } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) { |
|
1859 $location = add_query_arg( 'message', 3, wp_get_referer() ); |
|
1860 $location = explode('#', $location); |
|
1861 $location = $location[0] . '#postcustom'; |
|
1862 } else { |
|
1863 $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) ); |
|
1864 } |
|
1865 |
|
1866 /** |
|
1867 * Filters the post redirect destination URL. |
|
1868 * |
|
1869 * @since 2.9.0 |
|
1870 * |
|
1871 * @param string $location The destination URL. |
|
1872 * @param int $post_id The post ID. |
|
1873 */ |
|
1874 wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) ); |
|
1875 exit; |
|
1876 } |