wp/wp-admin/includes/post.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
   167 			break;
   167 			break;
   168 		}
   168 		}
   169 	}
   169 	}
   170 
   170 
   171 	if ( ! empty( $post_data['edit_date'] ) ) {
   171 	if ( ! empty( $post_data['edit_date'] ) ) {
   172 		$aa                     = $post_data['aa'];
   172 		$aa = $post_data['aa'];
   173 		$mm                     = $post_data['mm'];
   173 		$mm = $post_data['mm'];
   174 		$jj                     = $post_data['jj'];
   174 		$jj = $post_data['jj'];
   175 		$hh                     = $post_data['hh'];
   175 		$hh = $post_data['hh'];
   176 		$mn                     = $post_data['mn'];
   176 		$mn = $post_data['mn'];
   177 		$ss                     = $post_data['ss'];
   177 		$ss = $post_data['ss'];
   178 		$aa                     = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa;
   178 		$aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa;
   179 		$mm                     = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm;
   179 		$mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm;
   180 		$jj                     = ( $jj > 31 ) ? 31 : $jj;
   180 		$jj = ( $jj > 31 ) ? 31 : $jj;
   181 		$jj                     = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj;
   181 		$jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj;
   182 		$hh                     = ( $hh > 23 ) ? $hh - 24 : $hh;
   182 		$hh = ( $hh > 23 ) ? $hh - 24 : $hh;
   183 		$mn                     = ( $mn > 59 ) ? $mn - 60 : $mn;
   183 		$mn = ( $mn > 59 ) ? $mn - 60 : $mn;
   184 		$ss                     = ( $ss > 59 ) ? $ss - 60 : $ss;
   184 		$ss = ( $ss > 59 ) ? $ss - 60 : $ss;
       
   185 
   185 		$post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss );
   186 		$post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss );
   186 		$valid_date             = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
   187 
       
   188 		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
   187 		if ( ! $valid_date ) {
   189 		if ( ! $valid_date ) {
   188 			return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
   190 			return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
   189 		}
   191 		}
       
   192 
   190 		$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
   193 		$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
   191 	}
   194 	}
   192 
   195 
   193 	if ( isset( $post_data['post_category'] ) ) {
   196 	if ( isset( $post_data['post_category'] ) ) {
   194 		$category_object = get_taxonomy( 'category' );
   197 		$category_object = get_taxonomy( 'category' );
   244 	}
   247 	}
   245 
   248 
   246 	// Clear out any data in internal vars.
   249 	// Clear out any data in internal vars.
   247 	unset( $post_data['filter'] );
   250 	unset( $post_data['filter'] );
   248 
   251 
   249 	$post_ID                     = (int) $post_data['post_ID'];
   252 	$post_ID = (int) $post_data['post_ID'];
   250 	$post                        = get_post( $post_ID );
   253 	$post    = get_post( $post_ID );
       
   254 
   251 	$post_data['post_type']      = $post->post_type;
   255 	$post_data['post_type']      = $post->post_type;
   252 	$post_data['post_mime_type'] = $post->post_mime_type;
   256 	$post_data['post_mime_type'] = $post->post_mime_type;
   253 
   257 
   254 	if ( ! empty( $post_data['post_status'] ) ) {
   258 	if ( ! empty( $post_data['post_status'] ) ) {
   255 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
   259 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
   683 		$post_id = wp_insert_post(
   687 		$post_id = wp_insert_post(
   684 			array(
   688 			array(
   685 				'post_title'  => __( 'Auto Draft' ),
   689 				'post_title'  => __( 'Auto Draft' ),
   686 				'post_type'   => $post_type,
   690 				'post_type'   => $post_type,
   687 				'post_status' => 'auto-draft',
   691 				'post_status' => 'auto-draft',
   688 			)
   692 			),
       
   693 			false,
       
   694 			false
   689 		);
   695 		);
   690 		$post    = get_post( $post_id );
   696 		$post    = get_post( $post_id );
   691 		if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
   697 		if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
   692 			set_post_format( $post, get_option( 'default_post_format' ) );
   698 			set_post_format( $post, get_option( 'default_post_format' ) );
   693 		}
   699 		}
       
   700 		wp_after_insert_post( $post, false, null );
   694 
   701 
   695 		// Schedule auto-draft cleanup.
   702 		// Schedule auto-draft cleanup.
   696 		if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
   703 		if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
   697 			wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
   704 			wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
   698 		}
   705 		}
   754 /**
   761 /**
   755  * Determines if a post exists based on title, content, date and type.
   762  * Determines if a post exists based on title, content, date and type.
   756  *
   763  *
   757  * @since 2.0.0
   764  * @since 2.0.0
   758  * @since 5.2.0 Added the `$type` parameter.
   765  * @since 5.2.0 Added the `$type` parameter.
       
   766  * @since 5.8.0 Added the `$status` parameter.
   759  *
   767  *
   760  * @global wpdb $wpdb WordPress database abstraction object.
   768  * @global wpdb $wpdb WordPress database abstraction object.
   761  *
   769  *
   762  * @param string $title   Post title.
   770  * @param string $title   Post title.
   763  * @param string $content Optional post content.
   771  * @param string $content Optional post content.
   764  * @param string $date    Optional post date.
   772  * @param string $date    Optional post date.
   765  * @param string $type    Optional post type.
   773  * @param string $type    Optional post type.
       
   774  * @param string $status  Optional post status.
   766  * @return int Post ID if post exists, 0 otherwise.
   775  * @return int Post ID if post exists, 0 otherwise.
   767  */
   776  */
   768 function post_exists( $title, $content = '', $date = '', $type = '' ) {
   777 function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
   769 	global $wpdb;
   778 	global $wpdb;
   770 
   779 
   771 	$post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
   780 	$post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
   772 	$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
   781 	$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
   773 	$post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
   782 	$post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
   774 	$post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
   783 	$post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
       
   784 	$post_status  = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
   775 
   785 
   776 	$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
   786 	$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
   777 	$args  = array();
   787 	$args  = array();
   778 
   788 
   779 	if ( ! empty( $date ) ) {
   789 	if ( ! empty( $date ) ) {
   794 	if ( ! empty( $type ) ) {
   804 	if ( ! empty( $type ) ) {
   795 		$query .= ' AND post_type = %s';
   805 		$query .= ' AND post_type = %s';
   796 		$args[] = $post_type;
   806 		$args[] = $post_type;
   797 	}
   807 	}
   798 
   808 
       
   809 	if ( ! empty( $status ) ) {
       
   810 		$query .= ' AND post_status = %s';
       
   811 		$args[] = $post_status;
       
   812 	}
       
   813 
   799 	if ( ! empty( $args ) ) {
   814 	if ( ! empty( $args ) ) {
   800 		return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
   815 		return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
   801 	}
   816 	}
   802 
   817 
   803 	return 0;
   818 	return 0;
   808  *
   823  *
   809  * @since 2.1.0
   824  * @since 2.1.0
   810  *
   825  *
   811  * @global WP_User $current_user
   826  * @global WP_User $current_user
   812  *
   827  *
   813  * @return int|WP_Error
   828  * @return int|WP_Error Post ID on success, WP_Error on failure.
   814  */
   829  */
   815 function wp_write_post() {
   830 function wp_write_post() {
   816 	if ( isset( $_POST['post_type'] ) ) {
   831 	if ( isset( $_POST['post_type'] ) ) {
   817 		$ptype = get_post_type_object( $_POST['post_type'] );
   832 		$ptype = get_post_type_object( $_POST['post_type'] );
   818 	} else {
   833 	} else {
   884 /**
   899 /**
   885  * Calls wp_write_post() and handles the errors.
   900  * Calls wp_write_post() and handles the errors.
   886  *
   901  *
   887  * @since 2.0.0
   902  * @since 2.0.0
   888  *
   903  *
   889  * @return int|null
   904  * @return int|void Post ID on success, void on failure.
   890  */
   905  */
   891 function write_post() {
   906 function write_post() {
   892 	$result = wp_write_post();
   907 	$result = wp_write_post();
   893 	if ( is_wp_error( $result ) ) {
   908 	if ( is_wp_error( $result ) ) {
   894 		wp_die( $result->get_error_message() );
   909 		wp_die( $result->get_error_message() );
  1110 /**
  1125 /**
  1111  * Run the wp query to fetch the posts for listing on the edit posts page
  1126  * Run the wp query to fetch the posts for listing on the edit posts page
  1112  *
  1127  *
  1113  * @since 2.5.0
  1128  * @since 2.5.0
  1114  *
  1129  *
  1115  * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1130  * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1116  * @return array
  1131  * @return array
  1117  */
  1132  */
  1118 function wp_edit_posts_query( $q = false ) {
  1133 function wp_edit_posts_query( $q = false ) {
  1119 	if ( false === $q ) {
  1134 	if ( false === $q ) {
  1120 		$q = $_GET;
  1135 		$q = $_GET;
  1163 	/**
  1178 	/**
  1164 	 * Filters the number of items per page to show for a specific 'per_page' type.
  1179 	 * Filters the number of items per page to show for a specific 'per_page' type.
  1165 	 *
  1180 	 *
  1166 	 * The dynamic portion of the hook name, `$post_type`, refers to the post type.
  1181 	 * The dynamic portion of the hook name, `$post_type`, refers to the post type.
  1167 	 *
  1182 	 *
  1168 	 * Some examples of filter hooks generated here include: 'edit_attachment_per_page',
  1183 	 * Possible hook names include:
  1169 	 * 'edit_post_per_page', 'edit_page_per_page', etc.
  1184 	 *
       
  1185 	 *  - `edit_post_per_page`
       
  1186 	 *  - `edit_page_per_page`
       
  1187 	 *  - `edit_attachment_per_page`
  1170 	 *
  1188 	 *
  1171 	 * @since 3.0.0
  1189 	 * @since 3.0.0
  1172 	 *
  1190 	 *
  1173 	 * @param int $posts_per_page Number of posts to display per page for the given post
  1191 	 * @param int $posts_per_page Number of posts to display per page for the given post
  1174 	 *                            type. Default 20.
  1192 	 *                            type. Default 20.
  1330 /**
  1348 /**
  1331  * Get a sample permalink based off of the post name.
  1349  * Get a sample permalink based off of the post name.
  1332  *
  1350  *
  1333  * @since 2.5.0
  1351  * @since 2.5.0
  1334  *
  1352  *
  1335  * @param int    $id    Post ID or post object.
  1353  * @param int|WP_Post $id    Post ID or post object.
  1336  * @param string $title Optional. Title to override the post's current title when generating the post name. Default null.
  1354  * @param string      $title Optional. Title to override the post's current title when generating the post name. Default null.
  1337  * @param string $name  Optional. Name to override the post name. Default null.
  1355  * @param string      $name  Optional. Name to override the post name. Default null.
  1338  * @return array {
  1356  * @return array {
  1339  *     Array containing the sample permalink with placeholder for the post name, and the post name.
  1357  *     Array containing the sample permalink with placeholder for the post name, and the post name.
  1340  *
  1358  *
  1341  *     @type string $0 The permalink with placeholder for the post name.
  1359  *     @type string $0 The permalink with placeholder for the post name.
  1342  *     @type string $1 The post name.
  1360  *     @type string $1 The post name.
  1352 
  1370 
  1353 	$original_status = $post->post_status;
  1371 	$original_status = $post->post_status;
  1354 	$original_date   = $post->post_date;
  1372 	$original_date   = $post->post_date;
  1355 	$original_name   = $post->post_name;
  1373 	$original_name   = $post->post_name;
  1356 
  1374 
  1357 	// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
  1375 	// Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published.
  1358 	if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) {
  1376 	if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) {
  1359 		$post->post_status = 'publish';
  1377 		$post->post_status = 'publish';
  1360 		$post->post_name   = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
  1378 		$post->post_name   = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
  1361 	}
  1379 	}
  1362 
  1380 
  1534 		/**
  1552 		/**
  1535 		 * Filters the size used to display the post thumbnail image in the 'Featured image' meta box.
  1553 		 * Filters the size used to display the post thumbnail image in the 'Featured image' meta box.
  1536 		 *
  1554 		 *
  1537 		 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
  1555 		 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
  1538 		 * image size is registered, which differs from the 'thumbnail' image size
  1556 		 * image size is registered, which differs from the 'thumbnail' image size
  1539 		 * managed via the Settings > Media screen. See the `$size` parameter description
  1557 		 * managed via the Settings > Media screen.
  1540 		 * for more information on default values.
       
  1541 		 *
  1558 		 *
  1542 		 * @since 4.4.0
  1559 		 * @since 4.4.0
  1543 		 *
  1560 		 *
  1544 		 * @param string|array $size         Post thumbnail image size to display in the meta box. Accepts any valid
  1561 		 * @param string|int[] $size         Requested image size. Can be any registered image size name, or
  1545 		 *                                   image size, or an array of width and height values in pixels (in that order).
  1562 		 *                                   an array of width and height values in pixels (in that order).
  1546 		 *                                   If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,
       
  1547 		 *                                   default is an array with 266 as both the height and width values.
       
  1548 		 * @param int          $thumbnail_id Post thumbnail attachment ID.
  1563 		 * @param int          $thumbnail_id Post thumbnail attachment ID.
  1549 		 * @param WP_Post      $post         The post object associated with the thumbnail.
  1564 		 * @param WP_Post      $post         The post object associated with the thumbnail.
  1550 		 */
  1565 		 */
  1551 		$size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
  1566 		$size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
  1552 
  1567 
  1583 /**
  1598 /**
  1584  * Check to see if the post is currently being edited by another user.
  1599  * Check to see if the post is currently being edited by another user.
  1585  *
  1600  *
  1586  * @since 2.5.0
  1601  * @since 2.5.0
  1587  *
  1602  *
  1588  * @param int $post_id ID of the post to check for editing.
  1603  * @param int|WP_Post $post_id ID or object of the post to check for editing.
  1589  * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
  1604  * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
  1590  *                   the user with lock does not exist, or the post is locked by current user.
  1605  *                   the user with lock does not exist, or the post is locked by current user.
  1591  */
  1606  */
  1592 function wp_check_post_lock( $post_id ) {
  1607 function wp_check_post_lock( $post_id ) {
  1593 	$post = get_post( $post_id );
  1608 	$post = get_post( $post_id );
  1621 /**
  1636 /**
  1622  * Mark the post as currently being edited by the current user
  1637  * Mark the post as currently being edited by the current user
  1623  *
  1638  *
  1624  * @since 2.5.0
  1639  * @since 2.5.0
  1625  *
  1640  *
  1626  * @param int $post_id ID of the post being edited.
  1641  * @param int|WP_Post $post_id ID or object of the post being edited.
  1627  * @return array|false Array of the lock time and user ID. False if the post does not exist, or
  1642  * @return array|false Array of the lock time and user ID. False if the post does not exist, or
  1628  *                     there is no current user.
  1643  *                     there is no current user.
  1629  */
  1644  */
  1630 function wp_set_post_lock( $post_id ) {
  1645 function wp_set_post_lock( $post_id ) {
  1631 	$post = get_post( $post_id );
  1646 	$post = get_post( $post_id );
  1739 		<div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1754 		<div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1740 		<p class="currently-editing wp-tab-first" tabindex="0">
  1755 		<p class="currently-editing wp-tab-first" tabindex="0">
  1741 		<?php
  1756 		<?php
  1742 		if ( $override ) {
  1757 		if ( $override ) {
  1743 			/* translators: %s: User's display name. */
  1758 			/* translators: %s: User's display name. */
  1744 			printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
  1759 			printf( __( '%s is currently editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
  1745 		} else {
  1760 		} else {
  1746 			/* translators: %s: User's display name. */
  1761 			/* translators: %s: User's display name. */
  1747 			printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) );
  1762 			printf( __( '%s is currently editing this post.' ), esc_html( $user->display_name ) );
  1748 		}
  1763 		}
  1749 		?>
  1764 		?>
  1750 		</p>
  1765 		</p>
  1751 		<?php
  1766 		<?php
  1752 		/**
  1767 		/**
  1924 		if ( isset( $_POST['post_format'] ) ) {
  1939 		if ( isset( $_POST['post_format'] ) ) {
  1925 			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
  1940 			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
  1926 		}
  1941 		}
  1927 
  1942 
  1928 		if ( isset( $_POST['_thumbnail_id'] ) ) {
  1943 		if ( isset( $_POST['_thumbnail_id'] ) ) {
  1929 			$query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] );
  1944 			$query_args['_thumbnail_id'] = ( (int) $_POST['_thumbnail_id'] <= 0 ) ? '-1' : (int) $_POST['_thumbnail_id'];
  1930 		}
  1945 		}
  1931 	}
  1946 	}
  1932 
  1947 
  1933 	return get_preview_post_link( $post, $query_args );
  1948 	return get_preview_post_link( $post, $query_args );
  1934 }
  1949 }
  2086 				'hide_empty' => false,
  2101 				'hide_empty' => false,
  2087 			)
  2102 			)
  2088 		);
  2103 		);
  2089 
  2104 
  2090 		if ( ! empty( $_term ) ) {
  2105 		if ( ! empty( $_term ) ) {
  2091 			$clean_terms[] = intval( $_term[0] );
  2106 			$clean_terms[] = (int) $_term[0];
  2092 		} else {
  2107 		} else {
  2093 			// No existing term was found, so pass the string. A new term will be created.
  2108 			// No existing term was found, so pass the string. A new term will be created.
  2094 			$clean_terms[] = $term;
  2109 			$clean_terms[] = $term;
  2095 		}
  2110 		}
  2096 	}
  2111 	}
  2117 	if ( isset( $_GET['meta-box-loader'] ) ) {
  2132 	if ( isset( $_GET['meta-box-loader'] ) ) {
  2118 		check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
  2133 		check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
  2119 		return false;
  2134 		return false;
  2120 	}
  2135 	}
  2121 
  2136 
  2122 	// The posts page can't be edited in the block editor.
       
  2123 	if ( absint( get_option( 'page_for_posts' ) ) === $post->ID && empty( $post->post_content ) ) {
       
  2124 		return false;
       
  2125 	}
       
  2126 
       
  2127 	$use_block_editor = use_block_editor_for_post_type( $post->post_type );
  2137 	$use_block_editor = use_block_editor_for_post_type( $post->post_type );
  2128 
  2138 
  2129 	/**
  2139 	/**
  2130 	 * Filter whether a post is able to be edited in the block editor.
  2140 	 * Filters whether a post is able to be edited in the block editor.
  2131 	 *
  2141 	 *
  2132 	 * @since 5.0.0
  2142 	 * @since 5.0.0
  2133 	 *
  2143 	 *
  2134 	 * @param bool    $use_block_editor Whether the post can be edited or not.
  2144 	 * @param bool    $use_block_editor Whether the post can be edited or not.
  2135 	 * @param WP_Post $post             The post being checked.
  2145 	 * @param WP_Post $post             The post being checked.
  2161 	if ( $post_type_object && ! $post_type_object->show_in_rest ) {
  2171 	if ( $post_type_object && ! $post_type_object->show_in_rest ) {
  2162 		return false;
  2172 		return false;
  2163 	}
  2173 	}
  2164 
  2174 
  2165 	/**
  2175 	/**
  2166 	 * Filter whether a post is able to be edited in the block editor.
  2176 	 * Filters whether a post is able to be edited in the block editor.
  2167 	 *
  2177 	 *
  2168 	 * @since 5.0.0
  2178 	 * @since 5.0.0
  2169 	 *
  2179 	 *
  2170 	 * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
  2180 	 * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
  2171 	 * @param string $post_type         The post type being checked.
  2181 	 * @param string $post_type         The post type being checked.
  2172 	 */
  2182 	 */
  2173 	return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
  2183 	return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
  2174 }
  2184 }
  2175 
  2185 
  2176 /**
  2186 /**
  2177  * Returns all the block categories that will be shown in the block editor.
       
  2178  *
       
  2179  * @since 5.0.0
       
  2180  *
       
  2181  * @param WP_Post $post Post object.
       
  2182  * @return array[] Array of block categories.
       
  2183  */
       
  2184 function get_block_categories( $post ) {
       
  2185 	$default_categories = array(
       
  2186 		array(
       
  2187 			'slug'  => 'text',
       
  2188 			'title' => _x( 'Text', 'block category' ),
       
  2189 			'icon'  => null,
       
  2190 		),
       
  2191 		array(
       
  2192 			'slug'  => 'media',
       
  2193 			'title' => _x( 'Media', 'block category' ),
       
  2194 			'icon'  => null,
       
  2195 		),
       
  2196 		array(
       
  2197 			'slug'  => 'design',
       
  2198 			'title' => _x( 'Design', 'block category' ),
       
  2199 			'icon'  => null,
       
  2200 		),
       
  2201 		array(
       
  2202 			'slug'  => 'widgets',
       
  2203 			'title' => _x( 'Widgets', 'block category' ),
       
  2204 			'icon'  => null,
       
  2205 		),
       
  2206 		array(
       
  2207 			'slug'  => 'embed',
       
  2208 			'title' => _x( 'Embeds', 'block category' ),
       
  2209 			'icon'  => null,
       
  2210 		),
       
  2211 		array(
       
  2212 			'slug'  => 'reusable',
       
  2213 			'title' => _x( 'Reusable Blocks', 'block category' ),
       
  2214 			'icon'  => null,
       
  2215 		),
       
  2216 	);
       
  2217 
       
  2218 	/**
       
  2219 	 * Filter the default array of block categories.
       
  2220 	 *
       
  2221 	 * @since 5.0.0
       
  2222 	 *
       
  2223 	 * @param array[] $default_categories Array of block categories.
       
  2224 	 * @param WP_Post $post               Post being loaded.
       
  2225 	 */
       
  2226 	return apply_filters( 'block_categories', $default_categories, $post );
       
  2227 }
       
  2228 
       
  2229 /**
       
  2230  * Prepares server-registered blocks for the block editor.
  2187  * Prepares server-registered blocks for the block editor.
  2231  *
  2188  *
  2232  * Returns an associative array of registered block data keyed by block name. Data includes properties
  2189  * Returns an associative array of registered block data keyed by block name. Data includes properties
  2233  * of a block relevant for client registration.
  2190  * of a block relevant for client registration.
  2234  *
  2191  *
  2238  */
  2195  */
  2239 function get_block_editor_server_block_settings() {
  2196 function get_block_editor_server_block_settings() {
  2240 	$block_registry = WP_Block_Type_Registry::get_instance();
  2197 	$block_registry = WP_Block_Type_Registry::get_instance();
  2241 	$blocks         = array();
  2198 	$blocks         = array();
  2242 	$fields_to_pick = array(
  2199 	$fields_to_pick = array(
       
  2200 		'api_version'      => 'apiVersion',
  2243 		'title'            => 'title',
  2201 		'title'            => 'title',
  2244 		'description'      => 'description',
  2202 		'description'      => 'description',
  2245 		'icon'             => 'icon',
  2203 		'icon'             => 'icon',
  2246 		'category'         => 'category',
       
  2247 		'keywords'         => 'keywords',
       
  2248 		'parent'           => 'parent',
       
  2249 		'supports'         => 'supports',
       
  2250 		'attributes'       => 'attributes',
  2204 		'attributes'       => 'attributes',
  2251 		'provides_context' => 'providesContext',
  2205 		'provides_context' => 'providesContext',
  2252 		'uses_context'     => 'usesContext',
  2206 		'uses_context'     => 'usesContext',
       
  2207 		'supports'         => 'supports',
       
  2208 		'category'         => 'category',
  2253 		'styles'           => 'styles',
  2209 		'styles'           => 'styles',
  2254 		'textdomain'       => 'textdomain',
  2210 		'textdomain'       => 'textdomain',
       
  2211 		'parent'           => 'parent',
       
  2212 		'keywords'         => 'keywords',
  2255 		'example'          => 'example',
  2213 		'example'          => 'example',
       
  2214 		'variations'       => 'variations',
  2256 	);
  2215 	);
  2257 
  2216 
  2258 	foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
  2217 	foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
  2259 		foreach ( $fields_to_pick as $field => $key ) {
  2218 		foreach ( $fields_to_pick as $field => $key ) {
  2260 			if ( ! isset( $block_type->{ $field } ) ) {
  2219 			if ( ! isset( $block_type->{ $field } ) ) {
  2300 	// Render meta boxes.
  2259 	// Render meta boxes.
  2301 	?>
  2260 	?>
  2302 	<form class="metabox-base-form">
  2261 	<form class="metabox-base-form">
  2303 	<?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
  2262 	<?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
  2304 	</form>
  2263 	</form>
  2305 	<form id="toggle-custom-fields-form" method="post" action="<?php echo esc_attr( admin_url( 'post.php' ) ); ?>">
  2264 	<form id="toggle-custom-fields-form" method="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>">
  2306 		<?php wp_nonce_field( 'toggle-custom-fields' ); ?>
  2265 		<?php wp_nonce_field( 'toggle-custom-fields', 'toggle-custom-fields-nonce' ); ?>
  2307 		<input type="hidden" name="action" value="toggle-custom-fields" />
  2266 		<input type="hidden" name="action" value="toggle-custom-fields" />
  2308 	</form>
  2267 	</form>
  2309 	<?php foreach ( $locations as $location ) : ?>
  2268 	<?php foreach ( $locations as $location ) : ?>
  2310 		<form class="metabox-location-<?php echo esc_attr( $location ); ?>" onsubmit="return false;">
  2269 		<form class="metabox-location-<?php echo esc_attr( $location ); ?>" onsubmit="return false;">
  2311 			<div id="poststuff" class="sidebar-open">
  2270 			<div id="poststuff" class="sidebar-open">