wp/wp-admin/includes/post.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Rename $_POST data from form names to DB post columns.
    10  * Renames `$_POST` data from form names to DB post columns.
    11  *
    11  *
    12  * Manipulates $_POST directly.
    12  * Manipulates `$_POST` directly.
    13  *
    13  *
    14  * @since 2.6.0
    14  * @since 2.6.0
    15  *
    15  *
    16  * @param bool  $update    Are we updating a pre-existing post?
    16  * @param bool       $update    Whether the post already exists.
    17  * @param array $post_data Array of post data. Defaults to the contents of $_POST.
    17  * @param array|null $post_data Optional. The array of post data to process.
       
    18  *                              Defaults to the `$_POST` superglobal.
    18  * @return array|WP_Error Array of post data on success, WP_Error on failure.
    19  * @return array|WP_Error Array of post data on success, WP_Error on failure.
    19  */
    20  */
    20 function _wp_translate_postdata( $update = false, $post_data = null ) {
    21 function _wp_translate_postdata( $update = false, $post_data = null ) {
    21 
    22 
    22 	if ( empty( $post_data ) ) {
    23 	if ( empty( $post_data ) ) {
   202 
   203 
   203 	return $post_data;
   204 	return $post_data;
   204 }
   205 }
   205 
   206 
   206 /**
   207 /**
   207  * Returns only allowed post data fields
   208  * Returns only allowed post data fields.
   208  *
   209  *
   209  * @since 5.0.1
   210  * @since 5.0.1
   210  *
   211  *
   211  * @param array $post_data Array of post data. Defaults to the contents of $_POST.
   212  * @param array|WP_Error|null $post_data The array of post data to process, or an error object.
       
   213  *                                       Defaults to the `$_POST` superglobal.
   212  * @return array|WP_Error Array of post data on success, WP_Error on failure.
   214  * @return array|WP_Error Array of post data on success, WP_Error on failure.
   213  */
   215  */
   214 function _wp_get_allowed_postdata( $post_data = null ) {
   216 function _wp_get_allowed_postdata( $post_data = null ) {
   215 	if ( empty( $post_data ) ) {
   217 	if ( empty( $post_data ) ) {
   216 		$post_data = $_POST;
   218 		$post_data = $_POST;
   223 
   225 
   224 	return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
   226 	return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
   225 }
   227 }
   226 
   228 
   227 /**
   229 /**
   228  * Update an existing post with values provided in $_POST.
   230  * Updates an existing post with values provided in `$_POST`.
   229  *
   231  *
   230  * If post data is passed as an argument, it is treated as an array of data
   232  * If post data is passed as an argument, it is treated as an array of data
   231  * keyed appropriately for turning into a post object.
   233  * keyed appropriately for turning into a post object.
   232  *
   234  *
   233  * If post data is not passed, the $_POST global variable is used instead.
   235  * If post data is not passed, the `$_POST` global variable is used instead.
   234  *
   236  *
   235  * @since 1.5.0
   237  * @since 1.5.0
   236  *
   238  *
   237  * @global wpdb $wpdb WordPress database abstraction object.
   239  * @global wpdb $wpdb WordPress database abstraction object.
   238  *
   240  *
   239  * @param array $post_data Optional. Defaults to the $_POST global.
   241  * @param array|null $post_data Optional. The array of post data to process.
       
   242  *                              Defaults to the `$_POST` superglobal.
   240  * @return int Post ID.
   243  * @return int Post ID.
   241  */
   244  */
   242 function edit_post( $post_data = null ) {
   245 function edit_post( $post_data = null ) {
   243 	global $wpdb;
   246 	global $wpdb;
   244 
   247 
   450 
   453 
   451 	return $post_ID;
   454 	return $post_ID;
   452 }
   455 }
   453 
   456 
   454 /**
   457 /**
   455  * Process the post data for the bulk editing of posts.
   458  * Processes the post data for the bulk editing of posts.
   456  *
   459  *
   457  * Updates all bulk edited posts/pages, adding (but not removing) tags and
   460  * Updates all bulk edited posts/pages, adding (but not removing) tags and
   458  * categories. Skips pages when they would be their own parent or child.
   461  * categories. Skips pages when they would be their own parent or child.
   459  *
   462  *
   460  * @since 2.7.0
   463  * @since 2.7.0
   461  *
   464  *
   462  * @global wpdb $wpdb WordPress database abstraction object.
   465  * @global wpdb $wpdb WordPress database abstraction object.
   463  *
   466  *
   464  * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
   467  * @param array|null $post_data Optional. The array of post data to process.
       
   468  *                              Defaults to the `$_POST` superglobal.
   465  * @return array
   469  * @return array
   466  */
   470  */
   467 function bulk_edit_posts( $post_data = null ) {
   471 function bulk_edit_posts( $post_data = null ) {
   468 	global $wpdb;
   472 	global $wpdb;
   469 
   473 
   638 		}
   642 		}
   639 
   643 
   640 		// Prevent wp_insert_post() from overwriting post format with the old data.
   644 		// Prevent wp_insert_post() from overwriting post format with the old data.
   641 		unset( $post_data['tax_input']['post_format'] );
   645 		unset( $post_data['tax_input']['post_format'] );
   642 
   646 
   643 		$updated[] = wp_update_post( $post_data );
   647 		$post_id = wp_update_post( $post_data );
       
   648 		update_post_meta( $post_id, '_edit_last', get_current_user_id() );
       
   649 		$updated[] = $post_id;
   644 
   650 
   645 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   651 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   646 			if ( 'sticky' === $post_data['sticky'] ) {
   652 			if ( 'sticky' === $post_data['sticky'] ) {
   647 				stick_post( $post_ID );
   653 				stick_post( $post_ID );
   648 			} else {
   654 			} else {
   657 		'locked'  => $locked,
   663 		'locked'  => $locked,
   658 	);
   664 	);
   659 }
   665 }
   660 
   666 
   661 /**
   667 /**
   662  * Default post information to use when populating the "Write Post" form.
   668  * Returns default post information to use when populating the "Write Post" form.
   663  *
   669  *
   664  * @since 2.0.0
   670  * @since 2.0.0
   665  *
   671  *
   666  * @param string $post_type    Optional. A post type string. Default 'post'.
   672  * @param string $post_type    Optional. A post type string. Default 'post'.
   667  * @param bool   $create_in_db Optional. Whether to insert the post into database. Default false.
   673  * @param bool   $create_in_db Optional. Whether to insert the post into database. Default false.
   766  * @since 5.8.0 Added the `$status` parameter.
   772  * @since 5.8.0 Added the `$status` parameter.
   767  *
   773  *
   768  * @global wpdb $wpdb WordPress database abstraction object.
   774  * @global wpdb $wpdb WordPress database abstraction object.
   769  *
   775  *
   770  * @param string $title   Post title.
   776  * @param string $title   Post title.
   771  * @param string $content Optional post content.
   777  * @param string $content Optional. Post content.
   772  * @param string $date    Optional post date.
   778  * @param string $date    Optional. Post date.
   773  * @param string $type    Optional post type.
   779  * @param string $type    Optional. Post type.
   774  * @param string $status  Optional post status.
   780  * @param string $status  Optional. Post status.
   775  * @return int Post ID if post exists, 0 otherwise.
   781  * @return int Post ID if post exists, 0 otherwise.
   776  */
   782  */
   777 function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
   783 function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
   778 	global $wpdb;
   784 	global $wpdb;
   779 
   785 
   817 
   823 
   818 	return 0;
   824 	return 0;
   819 }
   825 }
   820 
   826 
   821 /**
   827 /**
   822  * Creates a new post from the "Write Post" form using $_POST information.
   828  * Creates a new post from the "Write Post" form using `$_POST` information.
   823  *
   829  *
   824  * @since 2.1.0
   830  * @since 2.1.0
   825  *
   831  *
   826  * @global WP_User $current_user
   832  * @global WP_User $current_user
   827  *
   833  *
   915 //
   921 //
   916 // Post Meta.
   922 // Post Meta.
   917 //
   923 //
   918 
   924 
   919 /**
   925 /**
   920  * Add post meta data defined in $_POST superglobal for post with given ID.
   926  * Adds post meta data defined in the `$_POST` superglobal for a post with given ID.
   921  *
   927  *
   922  * @since 1.2.0
   928  * @since 1.2.0
   923  *
   929  *
   924  * @param int $post_ID
   930  * @param int $post_ID
   925  * @return int|bool
   931  * @return int|bool
   958 
   964 
   959 	return false;
   965 	return false;
   960 }
   966 }
   961 
   967 
   962 /**
   968 /**
   963  * Delete post meta data by meta ID.
   969  * Deletes post meta data by meta ID.
   964  *
   970  *
   965  * @since 1.2.0
   971  * @since 1.2.0
   966  *
   972  *
   967  * @param int $mid
   973  * @param int $mid
   968  * @return bool
   974  * @return bool
   970 function delete_meta( $mid ) {
   976 function delete_meta( $mid ) {
   971 	return delete_metadata_by_mid( 'post', $mid );
   977 	return delete_metadata_by_mid( 'post', $mid );
   972 }
   978 }
   973 
   979 
   974 /**
   980 /**
   975  * Get a list of previously defined keys.
   981  * Returns a list of previously defined keys.
   976  *
   982  *
   977  * @since 1.2.0
   983  * @since 1.2.0
   978  *
   984  *
   979  * @global wpdb $wpdb WordPress database abstraction object.
   985  * @global wpdb $wpdb WordPress database abstraction object.
   980  *
   986  *
   981  * @return mixed
   987  * @return string[] Array of meta key names.
   982  */
   988  */
   983 function get_meta_keys() {
   989 function get_meta_keys() {
   984 	global $wpdb;
   990 	global $wpdb;
   985 
   991 
   986 	$keys = $wpdb->get_col(
   992 	$keys = $wpdb->get_col(
   993 
   999 
   994 	return $keys;
  1000 	return $keys;
   995 }
  1001 }
   996 
  1002 
   997 /**
  1003 /**
   998  * Get post meta data by meta ID.
  1004  * Returns post meta data by meta ID.
   999  *
  1005  *
  1000  * @since 2.1.0
  1006  * @since 2.1.0
  1001  *
  1007  *
  1002  * @param int $mid
  1008  * @param int $mid
  1003  * @return object|bool
  1009  * @return object|bool
  1005 function get_post_meta_by_id( $mid ) {
  1011 function get_post_meta_by_id( $mid ) {
  1006 	return get_metadata_by_mid( 'post', $mid );
  1012 	return get_metadata_by_mid( 'post', $mid );
  1007 }
  1013 }
  1008 
  1014 
  1009 /**
  1015 /**
  1010  * Get meta data for the given post ID.
  1016  * Returns meta data for the given post ID.
  1011  *
  1017  *
  1012  * @since 1.2.0
  1018  * @since 1.2.0
  1013  *
  1019  *
  1014  * @global wpdb $wpdb WordPress database abstraction object.
  1020  * @global wpdb $wpdb WordPress database abstraction object.
  1015  *
  1021  *
  1016  * @param int $postid
  1022  * @param int $postid A post ID.
  1017  * @return mixed
  1023  * @return array[] {
       
  1024  *     Array of meta data arrays for the given post ID.
       
  1025  *
       
  1026  *     @type array ...$0 {
       
  1027  *         Associative array of meta data.
       
  1028  *
       
  1029  *         @type string $meta_key   Meta key.
       
  1030  *         @type mixed  $meta_value Meta value.
       
  1031  *         @type string $meta_id    Meta ID as a numeric string.
       
  1032  *         @type string $post_id    Post ID as a numeric string.
       
  1033  *     }
       
  1034  * }
  1018  */
  1035  */
  1019 function has_meta( $postid ) {
  1036 function has_meta( $postid ) {
  1020 	global $wpdb;
  1037 	global $wpdb;
  1021 
  1038 
  1022 	return $wpdb->get_results(
  1039 	return $wpdb->get_results(
  1029 		ARRAY_A
  1046 		ARRAY_A
  1030 	);
  1047 	);
  1031 }
  1048 }
  1032 
  1049 
  1033 /**
  1050 /**
  1034  * Update post meta data by meta ID.
  1051  * Updates post meta data by meta ID.
  1035  *
  1052  *
  1036  * @since 1.2.0
  1053  * @since 1.2.0
  1037  *
  1054  *
  1038  * @param int    $meta_id
  1055  * @param int    $meta_id    Meta ID.
  1039  * @param string $meta_key Expect Slashed
  1056  * @param string $meta_key   Meta key. Expect slashed.
  1040  * @param string $meta_value Expect Slashed
  1057  * @param string $meta_value Meta value. Expect slashed.
  1041  * @return bool
  1058  * @return bool
  1042  */
  1059  */
  1043 function update_meta( $meta_id, $meta_key, $meta_value ) {
  1060 function update_meta( $meta_id, $meta_key, $meta_value ) {
  1044 	$meta_key   = wp_unslash( $meta_key );
  1061 	$meta_key   = wp_unslash( $meta_key );
  1045 	$meta_value = wp_unslash( $meta_value );
  1062 	$meta_value = wp_unslash( $meta_value );
  1050 //
  1067 //
  1051 // Private.
  1068 // Private.
  1052 //
  1069 //
  1053 
  1070 
  1054 /**
  1071 /**
  1055  * Replace hrefs of attachment anchors with up-to-date permalinks.
  1072  * Replaces hrefs of attachment anchors with up-to-date permalinks.
  1056  *
  1073  *
  1057  * @since 2.3.0
  1074  * @since 2.3.0
  1058  * @access private
  1075  * @access private
  1059  *
  1076  *
  1060  * @param int|object $post Post ID or post object.
  1077  * @param int|object $post Post ID or post object.
  1107 		return wp_update_post( $post );
  1124 		return wp_update_post( $post );
  1108 	}
  1125 	}
  1109 }
  1126 }
  1110 
  1127 
  1111 /**
  1128 /**
  1112  * Get all the possible statuses for a post_type
  1129  * Returns all the possible statuses for a post type.
  1113  *
  1130  *
  1114  * @since 2.5.0
  1131  * @since 2.5.0
  1115  *
  1132  *
  1116  * @param string $type The post_type you want the statuses for. Default 'post'.
  1133  * @param string $type The post_type you want the statuses for. Default 'post'.
  1117  * @return string[] An array of all the statuses for the supplied post type.
  1134  * @return string[] An array of all the statuses for the supplied post type.
  1121 
  1138 
  1122 	return array_keys( get_object_vars( $stati ) );
  1139 	return array_keys( get_object_vars( $stati ) );
  1123 }
  1140 }
  1124 
  1141 
  1125 /**
  1142 /**
  1126  * Run the wp query to fetch the posts for listing on the edit posts page
  1143  * Runs the query to fetch the posts for listing on the edit posts page.
  1127  *
  1144  *
  1128  * @since 2.5.0
  1145  * @since 2.5.0
  1129  *
  1146  *
  1130  * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1147  * @param array|false $q Optional. Array of query variables to use to build the query.
       
  1148  *                       Defaults to the `$_GET` superglobal.
  1131  * @return array
  1149  * @return array
  1132  */
  1150  */
  1133 function wp_edit_posts_query( $q = false ) {
  1151 function wp_edit_posts_query( $q = false ) {
  1134 	if ( false === $q ) {
  1152 	if ( false === $q ) {
  1135 		$q = $_GET;
  1153 		$q = $_GET;
  1222 
  1240 
  1223 	return $avail_post_stati;
  1241 	return $avail_post_stati;
  1224 }
  1242 }
  1225 
  1243 
  1226 /**
  1244 /**
  1227  * Get the query variables for the current attachments request.
  1245  * Returns the query variables for the current attachments request.
  1228  *
  1246  *
  1229  * @since 4.2.0
  1247  * @since 4.2.0
  1230  *
  1248  *
  1231  * @param array|false $q Optional. Array of query variables to use to build the query or false
  1249  * @param array|false $q Optional. Array of query variables to use to build the query.
  1232  *                       to use $_GET superglobal. Default false.
  1250  *                       Defaults to the `$_GET` superglobal.
  1233  * @return array The parsed query vars.
  1251  * @return array The parsed query vars.
  1234  */
  1252  */
  1235 function wp_edit_attachments_query_vars( $q = false ) {
  1253 function wp_edit_attachments_query_vars( $q = false ) {
  1236 	if ( false === $q ) {
  1254 	if ( false === $q ) {
  1237 		$q = $_GET;
  1255 		$q = $_GET;
  1294  * Executes a query for attachments. An array of WP_Query arguments
  1312  * Executes a query for attachments. An array of WP_Query arguments
  1295  * can be passed in, which will override the arguments set by this function.
  1313  * can be passed in, which will override the arguments set by this function.
  1296  *
  1314  *
  1297  * @since 2.5.0
  1315  * @since 2.5.0
  1298  *
  1316  *
  1299  * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1317  * @param array|false $q Optional. Array of query variables to use to build the query.
       
  1318  *                       Defaults to the `$_GET` superglobal.
  1300  * @return array
  1319  * @return array
  1301  */
  1320  */
  1302 function wp_edit_attachments_query( $q = false ) {
  1321 function wp_edit_attachments_query( $q = false ) {
  1303 	wp( wp_edit_attachments_query_vars( $q ) );
  1322 	wp( wp_edit_attachments_query_vars( $q ) );
  1304 
  1323 
  1340 	 * @since 3.2.0
  1359 	 * @since 3.2.0
  1341 	 *
  1360 	 *
  1342 	 * @param string[] $classes An array of postbox classes.
  1361 	 * @param string[] $classes An array of postbox classes.
  1343 	 */
  1362 	 */
  1344 	$classes = apply_filters( "postbox_classes_{$screen_id}_{$box_id}", $classes );
  1363 	$classes = apply_filters( "postbox_classes_{$screen_id}_{$box_id}", $classes );
       
  1364 
  1345 	return implode( ' ', $classes );
  1365 	return implode( ' ', $classes );
  1346 }
  1366 }
  1347 
  1367 
  1348 /**
  1368 /**
  1349  * Get a sample permalink based off of the post name.
  1369  * Returns a sample permalink based on the post name.
  1350  *
  1370  *
  1351  * @since 2.5.0
  1371  * @since 2.5.0
  1352  *
  1372  *
  1353  * @param int|WP_Post $id    Post ID or post object.
  1373  * @param int|WP_Post $id    Post ID or post object.
  1354  * @param string      $title Optional. Title to override the post's current title when generating the post name. Default null.
  1374  * @param string|null $title Optional. Title to override the post's current title
  1355  * @param string      $name  Optional. Name to override the post name. Default null.
  1375  *                           when generating the post name. Default null.
       
  1376  * @param string|null $name  Optional. Name to override the post name. Default null.
  1356  * @return array {
  1377  * @return array {
  1357  *     Array containing the sample permalink with placeholder for the post name, and the post name.
  1378  *     Array containing the sample permalink with placeholder for the post name, and the post name.
  1358  *
  1379  *
  1359  *     @type string $0 The permalink with placeholder for the post name.
  1380  *     @type string $0 The permalink with placeholder for the post name.
  1360  *     @type string $1 The post name.
  1381  *     @type string $1 The post name.
  1426 	 *     Array containing the sample permalink with placeholder for the post name, and the post name.
  1447 	 *     Array containing the sample permalink with placeholder for the post name, and the post name.
  1427 	 *
  1448 	 *
  1428 	 *     @type string $0 The permalink with placeholder for the post name.
  1449 	 *     @type string $0 The permalink with placeholder for the post name.
  1429 	 *     @type string $1 The post name.
  1450 	 *     @type string $1 The post name.
  1430 	 * }
  1451 	 * }
  1431 	 * @param int     $post_id   Post ID.
  1452 	 * @param int     $post_id Post ID.
  1432 	 * @param string  $title     Post title.
  1453 	 * @param string  $title   Post title.
  1433 	 * @param string  $name      Post name (slug).
  1454 	 * @param string  $name    Post name (slug).
  1434 	 * @param WP_Post $post      Post object.
  1455 	 * @param WP_Post $post    Post object.
  1435 	 */
  1456 	 */
  1436 	return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
  1457 	return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
  1437 }
  1458 }
  1438 
  1459 
  1439 /**
  1460 /**
  1440  * Returns the HTML of the sample permalink slug editor.
  1461  * Returns the HTML of the sample permalink slug editor.
  1441  *
  1462  *
  1442  * @since 2.5.0
  1463  * @since 2.5.0
  1443  *
  1464  *
  1444  * @param int    $id        Post ID or post object.
  1465  * @param int|WP_Post $id        Post ID or post object.
  1445  * @param string $new_title Optional. New title. Default null.
  1466  * @param string|null $new_title Optional. New title. Default null.
  1446  * @param string $new_slug  Optional. New slug. Default null.
  1467  * @param string|null $new_slug  Optional. New slug. Default null.
  1447  * @return string The HTML of the sample permalink slug editor.
  1468  * @return string The HTML of the sample permalink slug editor.
  1448  */
  1469  */
  1449 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1470 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1450 	$post = get_post( $id );
  1471 	$post = get_post( $id );
  1451 	if ( ! $post ) {
  1472 	if ( ! $post ) {
  1484 
  1505 
  1485 		// Encourage a pretty permalink setting.
  1506 		// Encourage a pretty permalink setting.
  1486 		if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
  1507 		if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
  1487 			&& ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $id )
  1508 			&& ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $id )
  1488 		) {
  1509 		) {
  1489 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __( 'Change Permalinks' ) . "</a></span>\n";
  1510 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small">' . __( 'Change Permalink Structure' ) . "</a></span>\n";
  1490 		}
  1511 		}
  1491 	} else {
  1512 	} else {
  1492 		if ( mb_strlen( $post_name ) > 34 ) {
  1513 		if ( mb_strlen( $post_name ) > 34 ) {
  1493 			$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
  1514 			$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
  1494 		} else {
  1515 		} else {
  1525 /**
  1546 /**
  1526  * Returns HTML for the post thumbnail meta box.
  1547  * Returns HTML for the post thumbnail meta box.
  1527  *
  1548  *
  1528  * @since 2.9.0
  1549  * @since 2.9.0
  1529  *
  1550  *
  1530  * @param int         $thumbnail_id ID of the attachment used for thumbnail
  1551  * @param int|null         $thumbnail_id Optional. Thumbnail attachment ID. Default null.
  1531  * @param int|WP_Post $post         Optional. The post ID or object associated with the thumbnail, defaults to global $post.
  1552  * @param int|WP_Post|null $post         Optional. The post ID or object associated
       
  1553  *                                       with the thumbnail. Defaults to global $post.
  1532  * @return string The post thumbnail HTML.
  1554  * @return string The post thumbnail HTML.
  1533  */
  1555  */
  1534 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1556 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1535 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1557 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1536 
  1558 
  1594 	 */
  1616 	 */
  1595 	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );
  1617 	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );
  1596 }
  1618 }
  1597 
  1619 
  1598 /**
  1620 /**
  1599  * Check to see if the post is currently being edited by another user.
  1621  * Determines whether the post is currently being edited by another user.
  1600  *
  1622  *
  1601  * @since 2.5.0
  1623  * @since 2.5.0
  1602  *
  1624  *
  1603  * @param int|WP_Post $post_id ID or object of the post to check for editing.
  1625  * @param int|WP_Post $post_id ID or object of the post to check for editing.
  1604  * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
  1626  * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
  1632 
  1654 
  1633 	return false;
  1655 	return false;
  1634 }
  1656 }
  1635 
  1657 
  1636 /**
  1658 /**
  1637  * Mark the post as currently being edited by the current user
  1659  * Marks the post as currently being edited by the current user.
  1638  *
  1660  *
  1639  * @since 2.5.0
  1661  * @since 2.5.0
  1640  *
  1662  *
  1641  * @param int|WP_Post $post_id ID or object of the post being edited.
  1663  * @param int|WP_Post $post_id ID or object of the post being edited.
  1642  * @return array|false Array of the lock time and user ID. False if the post does not exist, or
  1664  * @return array|false {
  1643  *                     there is no current user.
  1665  *     Array of the lock time and user ID. False if the post does not exist, or there
       
  1666  *     is no current user.
       
  1667  *
       
  1668  *     @type int $0 The current time as a Unix timestamp.
       
  1669  *     @type int $1 The ID of the current user.
       
  1670  * }
  1644  */
  1671  */
  1645 function wp_set_post_lock( $post_id ) {
  1672 function wp_set_post_lock( $post_id ) {
  1646 	$post = get_post( $post_id );
  1673 	$post = get_post( $post_id );
  1647 	if ( ! $post ) {
  1674 	if ( ! $post ) {
  1648 		return false;
  1675 		return false;
  1822 	</div>
  1849 	</div>
  1823 	<?php
  1850 	<?php
  1824 }
  1851 }
  1825 
  1852 
  1826 /**
  1853 /**
  1827  * Creates autosave data for the specified post from $_POST data.
  1854  * Creates autosave data for the specified post from `$_POST` data.
  1828  *
  1855  *
  1829  * @since 2.6.0
  1856  * @since 2.6.0
  1830  *
  1857  *
  1831  * @param array|int $post_data Associative array containing the post data or int post ID.
  1858  * @param array|int $post_data Associative array containing the post data, or integer post ID.
       
  1859  *                             If a numeric post ID is provided, will use the `$_POST` superglobal.
  1832  * @return int|WP_Error The autosave revision ID. WP_Error or 0 on error.
  1860  * @return int|WP_Error The autosave revision ID. WP_Error or 0 on error.
  1833  */
  1861  */
  1834 function wp_create_post_autosave( $post_data ) {
  1862 function wp_create_post_autosave( $post_data ) {
  1835 	if ( is_numeric( $post_data ) ) {
  1863 	if ( is_numeric( $post_data ) ) {
  1836 		$post_id   = $post_data;
  1864 		$post_id   = $post_data;
  1947 
  1975 
  1948 	return get_preview_post_link( $post, $query_args );
  1976 	return get_preview_post_link( $post, $query_args );
  1949 }
  1977 }
  1950 
  1978 
  1951 /**
  1979 /**
  1952  * Save a post submitted with XHR
  1980  * Saves a post submitted with XHR.
  1953  *
  1981  *
  1954  * Intended for use with heartbeat and autosave.js
  1982  * Intended for use with heartbeat and autosave.js
  1955  *
  1983  *
  1956  * @since 3.9.0
  1984  * @since 3.9.0
  1957  *
  1985  *
  1998 		return wp_create_post_autosave( wp_slash( $post_data ) );
  2026 		return wp_create_post_autosave( wp_slash( $post_data ) );
  1999 	}
  2027 	}
  2000 }
  2028 }
  2001 
  2029 
  2002 /**
  2030 /**
  2003  * Redirect to previous page.
  2031  * Redirects to previous page.
  2004  *
  2032  *
  2005  * @since 2.7.0
  2033  * @since 2.7.0
  2006  *
  2034  *
  2007  * @param int $post_id Optional. Post ID.
  2035  * @param int $post_id Optional. Post ID.
  2008  */
  2036  */
  2112 
  2140 
  2113 	return $clean_terms;
  2141 	return $clean_terms;
  2114 }
  2142 }
  2115 
  2143 
  2116 /**
  2144 /**
  2117  * Return whether the post can be edited in the block editor.
  2145  * Returns whether the post can be edited in the block editor.
  2118  *
  2146  *
  2119  * @since 5.0.0
  2147  * @since 5.0.0
  2120  *
  2148  *
  2121  * @param int|WP_Post $post Post ID or WP_Post object.
  2149  * @param int|WP_Post $post Post ID or WP_Post object.
  2122  * @return bool Whether the post can be edited in the block editor.
  2150  * @return bool Whether the post can be edited in the block editor.
  2146 	 */
  2174 	 */
  2147 	return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
  2175 	return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
  2148 }
  2176 }
  2149 
  2177 
  2150 /**
  2178 /**
  2151  * Return whether a post type is compatible with the block editor.
  2179  * Returns whether a post type is compatible with the block editor.
  2152  *
  2180  *
  2153  * The block editor depends on the REST API, and if the post type is not shown in the
  2181  * The block editor depends on the REST API, and if the post type is not shown in the
  2154  * REST API, then it won't work with the block editor.
  2182  * REST API, then it won't work with the block editor.
  2155  *
  2183  *
  2156  * @since 5.0.0
  2184  * @since 5.0.0
  2207 		'supports'         => 'supports',
  2235 		'supports'         => 'supports',
  2208 		'category'         => 'category',
  2236 		'category'         => 'category',
  2209 		'styles'           => 'styles',
  2237 		'styles'           => 'styles',
  2210 		'textdomain'       => 'textdomain',
  2238 		'textdomain'       => 'textdomain',
  2211 		'parent'           => 'parent',
  2239 		'parent'           => 'parent',
       
  2240 		'ancestor'         => 'ancestor',
  2212 		'keywords'         => 'keywords',
  2241 		'keywords'         => 'keywords',
  2213 		'example'          => 'example',
  2242 		'example'          => 'example',
  2214 		'variations'       => 'variations',
  2243 		'variations'       => 'variations',
  2215 	);
  2244 	);
  2216 
  2245 
  2312 				);
  2341 				);
  2313 			}
  2342 			}
  2314 		}
  2343 		}
  2315 	}
  2344 	}
  2316 
  2345 
  2317 	/**
  2346 	/*
  2318 	 * Sadly we probably can not add this data directly into editor settings.
  2347 	 * Sadly we probably cannot add this data directly into editor settings.
  2319 	 *
  2348 	 *
  2320 	 * Some meta boxes need admin_head to fire for meta box registry.
  2349 	 * Some meta boxes need `admin_head` to fire for meta box registry.
  2321 	 * admin_head fires after admin_enqueue_scripts, which is where we create our
  2350 	 * `admin_head` fires after `admin_enqueue_scripts`, which is where we create
  2322 	 * editor instance.
  2351 	 * our editor instance.
  2323 	 */
  2352 	 */
  2324 	$script = 'window._wpLoadBlockEditor.then( function() {
  2353 	$script = 'window._wpLoadBlockEditor.then( function() {
  2325 		wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' );
  2354 		wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' );
  2326 	} );';
  2355 	} );';
  2327 
  2356 
  2328 	wp_add_inline_script( 'wp-edit-post', $script );
  2357 	wp_add_inline_script( 'wp-edit-post', $script );
  2329 
  2358 
  2330 	/**
  2359 	/*
  2331 	 * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed. Otherwise,
  2360 	 * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed.
  2332 	 * meta boxes will not display because inline scripts for `wp-edit-post` will not be printed again after this point.
  2361 	 * Otherwise, meta boxes will not display because inline scripts for `wp-edit-post`
       
  2362 	 * will not be printed again after this point.
  2333 	 */
  2363 	 */
  2334 	if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
  2364 	if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
  2335 		printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
  2365 		printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
  2336 	}
  2366 	}
  2337 
  2367 
  2338 	/**
  2368 	/*
  2339 	 * If the 'postcustom' meta box is enabled, then we need to perform some
  2369 	 * If the 'postcustom' meta box is enabled, then we need to perform
  2340 	 * extra initialization on it.
  2370 	 * some extra initialization on it.
  2341 	 */
  2371 	 */
  2342 	$enable_custom_fields = (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true );
  2372 	$enable_custom_fields = (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true );
       
  2373 
  2343 	if ( $enable_custom_fields ) {
  2374 	if ( $enable_custom_fields ) {
  2344 		$script = "( function( $ ) {
  2375 		$script = "( function( $ ) {
  2345 			if ( $('#postcustom').length ) {
  2376 			if ( $('#postcustom').length ) {
  2346 				$( '#the-list' ).wpList( {
  2377 				$( '#the-list' ).wpList( {
  2347 					addBefore: function( s ) {
  2378 					addBefore: function( s ) {
  2381 	$current_user = wp_get_current_user();
  2412 	$current_user = wp_get_current_user();
  2382 	$user_id      = $current_user->ID;
  2413 	$user_id      = $current_user->ID;
  2383 	wp_nonce_field( $nonce_action );
  2414 	wp_nonce_field( $nonce_action );
  2384 
  2415 
  2385 	/*
  2416 	/*
  2386 	 * Some meta boxes hook into these actions to add hidden input fields in the classic post form. For backwards
  2417 	 * Some meta boxes hook into these actions to add hidden input fields in the classic post form.
  2387 	 * compatibility, we can capture the output from these actions, and extract the hidden input fields.
  2418 	 * For backward compatibility, we can capture the output from these actions,
       
  2419 	 * and extract the hidden input fields.
  2388 	 */
  2420 	 */
  2389 	ob_start();
  2421 	ob_start();
  2390 	/** This filter is documented in wp-admin/edit-form-advanced.php */
  2422 	/** This filter is documented in wp-admin/edit-form-advanced.php */
  2391 	do_action( 'edit_form_after_title', $post );
  2423 	do_action( 'edit_form_after_title', $post );
  2392 	/** This filter is documented in wp-admin/edit-form-advanced.php */
  2424 	/** This filter is documented in wp-admin/edit-form-advanced.php */
  2421 	wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  2453 	wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  2422 	// Permalink title nonce.
  2454 	// Permalink title nonce.
  2423 	wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
  2455 	wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
  2424 
  2456 
  2425 	/**
  2457 	/**
  2426 	 * Add hidden input fields to the meta box save form.
  2458 	 * Adds hidden input fields to the meta box save form.
  2427 	 *
  2459 	 *
  2428 	 * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to
  2460 	 * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to
  2429 	 * the server when meta boxes are saved.
  2461 	 * the server when meta boxes are saved.
  2430 	 *
  2462 	 *
  2431 	 * @since 5.0.0
  2463 	 * @since 5.0.0
  2432 	 *
  2464 	 *
  2433 	 * @param WP_Post $post The post that is being edited.
  2465 	 * @param WP_Post $post The post that is being edited.
  2434 	 */
  2466 	 */
  2435 	do_action( 'block_editor_meta_box_hidden_fields', $post );
  2467 	do_action( 'block_editor_meta_box_hidden_fields', $post );
  2436 }
  2468 }
       
  2469 
       
  2470 /**
       
  2471  * Disables block editor for wp_navigation type posts so they can be managed via the UI.
       
  2472  *
       
  2473  * @since 5.9.0
       
  2474  * @access private
       
  2475  *
       
  2476  * @param bool   $value Whether the CPT supports block editor or not.
       
  2477  * @param string $post_type Post type.
       
  2478  * @return bool Whether the block editor should be disabled or not.
       
  2479  */
       
  2480 function _disable_block_editor_for_navigation_post_type( $value, $post_type ) {
       
  2481 	if ( 'wp_navigation' === $post_type ) {
       
  2482 		return false;
       
  2483 	}
       
  2484 
       
  2485 	return $value;
       
  2486 }
       
  2487 
       
  2488 /**
       
  2489  * This callback disables the content editor for wp_navigation type posts.
       
  2490  * Content editor cannot handle wp_navigation type posts correctly.
       
  2491  * We cannot disable the "editor" feature in the wp_navigation's CPT definition
       
  2492  * because it disables the ability to save navigation blocks via REST API.
       
  2493  *
       
  2494  * @since 5.9.0
       
  2495  * @access private
       
  2496  *
       
  2497  * @param WP_Post $post An instance of WP_Post class.
       
  2498  */
       
  2499 function _disable_content_editor_for_navigation_post_type( $post ) {
       
  2500 	$post_type = get_post_type( $post );
       
  2501 	if ( 'wp_navigation' !== $post_type ) {
       
  2502 		return;
       
  2503 	}
       
  2504 
       
  2505 	remove_post_type_support( $post_type, 'editor' );
       
  2506 }
       
  2507 
       
  2508 /**
       
  2509  * This callback enables content editor for wp_navigation type posts.
       
  2510  * We need to enable it back because we disable it to hide
       
  2511  * the content editor for wp_navigation type posts.
       
  2512  *
       
  2513  * @since 5.9.0
       
  2514  * @access private
       
  2515  *
       
  2516  * @see _disable_content_editor_for_navigation_post_type
       
  2517  *
       
  2518  * @param WP_Post $post An instance of WP_Post class.
       
  2519  */
       
  2520 function _enable_content_editor_for_navigation_post_type( $post ) {
       
  2521 	$post_type = get_post_type( $post );
       
  2522 	if ( 'wp_navigation' !== $post_type ) {
       
  2523 		return;
       
  2524 	}
       
  2525 
       
  2526 	add_post_type_support( $post_type, 'editor' );
       
  2527 }