wp/wp-includes/post.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
   175 		'customize_changeset',
   175 		'customize_changeset',
   176 		array(
   176 		array(
   177 			'labels'           => array(
   177 			'labels'           => array(
   178 				'name'               => _x( 'Changesets', 'post type general name' ),
   178 				'name'               => _x( 'Changesets', 'post type general name' ),
   179 				'singular_name'      => _x( 'Changeset', 'post type singular name' ),
   179 				'singular_name'      => _x( 'Changeset', 'post type singular name' ),
   180 				'menu_name'          => _x( 'Changesets', 'admin menu' ),
       
   181 				'name_admin_bar'     => _x( 'Changeset', 'add new on admin bar' ),
       
   182 				'add_new'            => _x( 'Add New', 'Customize Changeset' ),
   180 				'add_new'            => _x( 'Add New', 'Customize Changeset' ),
   183 				'add_new_item'       => __( 'Add New Changeset' ),
   181 				'add_new_item'       => __( 'Add New Changeset' ),
   184 				'new_item'           => __( 'New Changeset' ),
   182 				'new_item'           => __( 'New Changeset' ),
   185 				'edit_item'          => __( 'Edit Changeset' ),
   183 				'edit_item'          => __( 'Edit Changeset' ),
   186 				'view_item'          => __( 'View Changeset' ),
   184 				'view_item'          => __( 'View Changeset' ),
   257 
   255 
   258 	register_post_type(
   256 	register_post_type(
   259 		'wp_block',
   257 		'wp_block',
   260 		array(
   258 		array(
   261 			'labels'                => array(
   259 			'labels'                => array(
   262 				'name'                     => _x( 'Blocks', 'post type general name' ),
   260 				'name'                     => _x( 'Reusable blocks', 'post type general name' ),
   263 				'singular_name'            => _x( 'Block', 'post type singular name' ),
   261 				'singular_name'            => _x( 'Reusable block', 'post type singular name' ),
   264 				'menu_name'                => _x( 'Blocks', 'admin menu' ),
   262 				'add_new'                  => _x( 'Add New', 'Reusable block' ),
   265 				'name_admin_bar'           => _x( 'Block', 'add new on admin bar' ),
   263 				'add_new_item'             => __( 'Add new Reusable block' ),
   266 				'add_new'                  => _x( 'Add New', 'Block' ),
   264 				'new_item'                 => __( 'New Reusable block' ),
   267 				'add_new_item'             => __( 'Add New Block' ),
   265 				'edit_item'                => __( 'Edit Reusable block' ),
   268 				'new_item'                 => __( 'New Block' ),
   266 				'view_item'                => __( 'View Reusable block' ),
   269 				'edit_item'                => __( 'Edit Block' ),
   267 				'all_items'                => __( 'All Reusable blocks' ),
   270 				'view_item'                => __( 'View Block' ),
   268 				'search_items'             => __( 'Search Reusable blocks' ),
   271 				'all_items'                => __( 'All Blocks' ),
   269 				'not_found'                => __( 'No reusable blocks found.' ),
   272 				'search_items'             => __( 'Search Blocks' ),
   270 				'not_found_in_trash'       => __( 'No reusable blocks found in Trash.' ),
   273 				'not_found'                => __( 'No blocks found.' ),
   271 				'filter_items_list'        => __( 'Filter reusable blocks list' ),
   274 				'not_found_in_trash'       => __( 'No blocks found in Trash.' ),
   272 				'items_list_navigation'    => __( 'Reusable blocks list navigation' ),
   275 				'filter_items_list'        => __( 'Filter blocks list' ),
   273 				'items_list'               => __( 'Reusable blocks list' ),
   276 				'items_list_navigation'    => __( 'Blocks list navigation' ),
   274 				'item_published'           => __( 'Reusable block published.' ),
   277 				'items_list'               => __( 'Blocks list' ),
   275 				'item_published_privately' => __( 'Reusable block published privately.' ),
   278 				'item_published'           => __( 'Block published.' ),
   276 				'item_reverted_to_draft'   => __( 'Reusable block reverted to draft.' ),
   279 				'item_published_privately' => __( 'Block published privately.' ),
   277 				'item_scheduled'           => __( 'Reusable block scheduled.' ),
   280 				'item_reverted_to_draft'   => __( 'Block reverted to draft.' ),
   278 				'item_updated'             => __( 'Reusable block updated.' ),
   281 				'item_scheduled'           => __( 'Block scheduled.' ),
       
   282 				'item_updated'             => __( 'Block updated.' ),
       
   283 			),
   279 			),
   284 			'public'                => false,
   280 			'public'                => false,
   285 			'_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
   281 			'_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
   286 			'show_ui'               => true,
   282 			'show_ui'               => true,
   287 			'show_in_menu'          => false,
   283 			'show_in_menu'          => false,
   303 			),
   299 			),
   304 			'map_meta_cap'          => true,
   300 			'map_meta_cap'          => true,
   305 			'supports'              => array(
   301 			'supports'              => array(
   306 				'title',
   302 				'title',
   307 				'editor',
   303 				'editor',
       
   304 				'revisions',
       
   305 			),
       
   306 		)
       
   307 	);
       
   308 
       
   309 	register_post_type(
       
   310 		'wp_template',
       
   311 		array(
       
   312 			'labels'                => array(
       
   313 				'name'                  => __( 'Templates' ),
       
   314 				'singular_name'         => __( 'Template' ),
       
   315 				'add_new'               => _x( 'Add New', 'Template' ),
       
   316 				'add_new_item'          => __( 'Add New Template' ),
       
   317 				'new_item'              => __( 'New Template' ),
       
   318 				'edit_item'             => __( 'Edit Template' ),
       
   319 				'view_item'             => __( 'View Template' ),
       
   320 				'all_items'             => __( 'All Templates' ),
       
   321 				'search_items'          => __( 'Search Templates' ),
       
   322 				'parent_item_colon'     => __( 'Parent Template:' ),
       
   323 				'not_found'             => __( 'No templates found.' ),
       
   324 				'not_found_in_trash'    => __( 'No templates found in Trash.' ),
       
   325 				'archives'              => __( 'Template archives' ),
       
   326 				'insert_into_item'      => __( 'Insert into template' ),
       
   327 				'uploaded_to_this_item' => __( 'Uploaded to this template' ),
       
   328 				'filter_items_list'     => __( 'Filter templates list' ),
       
   329 				'items_list_navigation' => __( 'Templates list navigation' ),
       
   330 				'items_list'            => __( 'Templates list' ),
       
   331 			),
       
   332 			'description'           => __( 'Templates to include in your theme.' ),
       
   333 			'public'                => false,
       
   334 			'_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
       
   335 			'has_archive'           => false,
       
   336 			'show_ui'               => false,
       
   337 			'show_in_menu'          => false,
       
   338 			'show_in_rest'          => true,
       
   339 			'rewrite'               => false,
       
   340 			'rest_base'             => 'templates',
       
   341 			'rest_controller_class' => 'WP_REST_Templates_Controller',
       
   342 			'capability_type'       => array( 'template', 'templates' ),
       
   343 			'capabilities'          => array(
       
   344 				'create_posts'           => 'edit_theme_options',
       
   345 				'delete_posts'           => 'edit_theme_options',
       
   346 				'delete_others_posts'    => 'edit_theme_options',
       
   347 				'delete_private_posts'   => 'edit_theme_options',
       
   348 				'delete_published_posts' => 'edit_theme_options',
       
   349 				'edit_posts'             => 'edit_theme_options',
       
   350 				'edit_others_posts'      => 'edit_theme_options',
       
   351 				'edit_private_posts'     => 'edit_theme_options',
       
   352 				'edit_published_posts'   => 'edit_theme_options',
       
   353 				'publish_posts'          => 'edit_theme_options',
       
   354 				'read'                   => 'edit_theme_options',
       
   355 				'read_private_posts'     => 'edit_theme_options',
       
   356 			),
       
   357 			'map_meta_cap'          => true,
       
   358 			'supports'              => array(
       
   359 				'title',
       
   360 				'slug',
       
   361 				'excerpt',
       
   362 				'editor',
       
   363 				'revisions',
   308 			),
   364 			),
   309 		)
   365 		)
   310 	);
   366 	);
   311 
   367 
   312 	register_post_status(
   368 	register_post_status(
   675 
   731 
   676 	foreach ( $children as $key => $child ) {
   732 	foreach ( $children as $key => $child ) {
   677 		$kids[ $child->ID ] = $children[ $key ];
   733 		$kids[ $child->ID ] = $children[ $key ];
   678 	}
   734 	}
   679 
   735 
   680 	if ( OBJECT == $output ) {
   736 	if ( OBJECT === $output ) {
   681 		return $kids;
   737 		return $kids;
   682 	} elseif ( ARRAY_A == $output ) {
   738 	} elseif ( ARRAY_A === $output ) {
   683 		$weeuns = array();
   739 		$weeuns = array();
   684 		foreach ( (array) $kids as $kid ) {
   740 		foreach ( (array) $kids as $kid ) {
   685 			$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
   741 			$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
   686 		}
   742 		}
   687 		return $weeuns;
   743 		return $weeuns;
   688 	} elseif ( ARRAY_N == $output ) {
   744 	} elseif ( ARRAY_N === $output ) {
   689 		$babes = array();
   745 		$babes = array();
   690 		foreach ( (array) $kids as $kid ) {
   746 		foreach ( (array) $kids as $kid ) {
   691 			$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
   747 			$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
   692 		}
   748 		}
   693 		return $babes;
   749 		return $babes;
   749  *
   805  *
   750  * @since 1.5.1
   806  * @since 1.5.1
   751  *
   807  *
   752  * @global WP_Post $post Global post object.
   808  * @global WP_Post $post Global post object.
   753  *
   809  *
   754  * @param int|WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
   810  * @param int|WP_Post|null $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey
       
   811  *                                 values return the current global post inside the loop. A numerically valid post
       
   812  *                                 ID that points to a non-existent post returns `null`. Defaults to global $post.
   755  * @param string           $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
   813  * @param string           $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
   756  *                                 correspond to a WP_Post object, an associative array, or a numeric array,
   814  *                                 correspond to a WP_Post object, an associative array, or a numeric array,
   757  *                                 respectively. Default OBJECT.
   815  *                                 respectively. Default OBJECT.
   758  * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
   816  * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
   759  *                                 or 'display'. Default 'raw'.
   817  *                                 or 'display'. Default 'raw'.
   784 		return null;
   842 		return null;
   785 	}
   843 	}
   786 
   844 
   787 	$_post = $_post->filter( $filter );
   845 	$_post = $_post->filter( $filter );
   788 
   846 
   789 	if ( ARRAY_A == $output ) {
   847 	if ( ARRAY_A === $output ) {
   790 		return $_post->to_array();
   848 		return $_post->to_array();
   791 	} elseif ( ARRAY_N == $output ) {
   849 	} elseif ( ARRAY_N === $output ) {
   792 		return array_values( $_post->to_array() );
   850 		return array_values( $_post->to_array() );
   793 	}
   851 	}
   794 
   852 
   795 	return $_post;
   853 	return $_post;
   796 }
   854 }
   797 
   855 
   798 /**
   856 /**
   799  * Retrieve ancestors of a post.
   857  * Retrieves the IDs of the ancestors of a post.
   800  *
   858  *
   801  * @since 2.5.0
   859  * @since 2.5.0
   802  *
   860  *
   803  * @param int|WP_Post $post Post ID or post object.
   861  * @param int|WP_Post $post Post ID or post object.
   804  * @return int[] Ancestor IDs or empty array if none are found.
   862  * @return int[] Array of ancestor IDs or empty array if there are none.
   805  */
   863  */
   806 function get_post_ancestors( $post ) {
   864 function get_post_ancestors( $post ) {
   807 	$post = get_post( $post );
   865 	$post = get_post( $post );
   808 
   866 
   809 	if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) {
   867 	if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) {
   899 
   957 
   900 	if ( ! is_object( $post ) ) {
   958 	if ( ! is_object( $post ) ) {
   901 		return false;
   959 		return false;
   902 	}
   960 	}
   903 
   961 
   904 	if ( 'attachment' === $post->post_type ) {
   962 	$post_status = $post->post_status;
   905 		if ( 'private' === $post->post_status ) {
   963 
   906 			return 'private';
   964 	if (
   907 		}
   965 		'attachment' === $post->post_type &&
   908 
   966 		'inherit' === $post_status
   909 		// Unattached attachments are assumed to be published.
   967 	) {
   910 		if ( ( 'inherit' === $post->post_status ) && ( 0 == $post->post_parent ) ) {
   968 		if (
   911 			return 'publish';
   969 			0 === $post->post_parent ||
   912 		}
   970 			! get_post( $post->post_parent ) ||
   913 
   971 			$post->ID === $post->post_parent
   914 		// Inherit status from the parent.
   972 		) {
   915 		if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
   973 			// Unattached attachments with inherit status are assumed to be published.
   916 			$parent_post_status = get_post_status( $post->post_parent );
   974 			$post_status = 'publish';
   917 			if ( 'trash' === $parent_post_status ) {
   975 		} elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
   918 				return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
   976 			// Get parent status prior to trashing.
   919 			} else {
   977 			$post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
   920 				return $parent_post_status;
   978 			if ( ! $post_status ) {
       
   979 				// Assume publish as above.
       
   980 				$post_status = 'publish';
   921 			}
   981 			}
   922 		}
   982 		} else {
       
   983 			$post_status = get_post_status( $post->post_parent );
       
   984 		}
       
   985 	} elseif (
       
   986 		'attachment' === $post->post_type &&
       
   987 		! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
       
   988 	) {
       
   989 		/*
       
   990 		 * Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
       
   991 		 * This is to match the logic in wp_insert_post().
       
   992 		 *
       
   993 		 * Note: 'inherit' is excluded from this check as it is resolved to the parent post's
       
   994 		 * status in the logic block above.
       
   995 		 */
       
   996 		$post_status = 'publish';
   923 	}
   997 	}
   924 
   998 
   925 	/**
   999 	/**
   926 	 * Filters the post status.
  1000 	 * Filters the post status.
   927 	 *
  1001 	 *
   928 	 * @since 4.4.0
  1002 	 * @since 4.4.0
       
  1003 	 * @since 5.7.0 The attachment post type is now passed through this filter.
   929 	 *
  1004 	 *
   930 	 * @param string  $post_status The post status.
  1005 	 * @param string  $post_status The post status.
   931 	 * @param WP_Post $post        The post object.
  1006 	 * @param WP_Post $post        The post object.
   932 	 */
  1007 	 */
   933 	return apply_filters( 'get_post_status', $post->post_status, $post );
  1008 	return apply_filters( 'get_post_status', $post_status, $post );
   934 }
  1009 }
   935 
  1010 
   936 /**
  1011 /**
   937  * Retrieve all of the WordPress supported post statuses.
  1012  * Retrieve all of the WordPress supported post statuses.
   938  *
  1013  *
  1292  * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing
  1367  * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing
  1293  *              screen and post editing screen.
  1368  *              screen and post editing screen.
  1294  * @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`.
  1369  * @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`.
  1295  * @since 4.7.0 Introduced `show_in_rest`, `rest_base` and `rest_controller_class`
  1370  * @since 4.7.0 Introduced `show_in_rest`, `rest_base` and `rest_controller_class`
  1296  *              arguments to register the post type in REST API.
  1371  *              arguments to register the post type in REST API.
       
  1372  * @since 5.0.0 The `template` and `template_lock` arguments were added.
  1297  * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature.
  1373  * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature.
  1298  * .
  1374  *
  1299  * @global array $wp_post_types List of post types.
  1375  * @global array $wp_post_types List of post types.
  1300  *
  1376  *
  1301  * @param string       $post_type Post type key. Must not exceed 20 characters and may
  1377  * @param string       $post_type Post type key. Must not exceed 20 characters and may
  1302  *                                only contain lowercase alphanumeric characters, dashes,
  1378  *                                only contain lowercase alphanumeric characters, dashes,
  1303  *                                and underscores. See sanitize_key().
  1379  *                                and underscores. See sanitize_key().
  1304  * @param array|string $args {
  1380  * @param array|string $args {
  1305  *     Array or string of arguments for registering a post type.
  1381  *     Array or string of arguments for registering a post type.
  1306  *
  1382  *
  1307  *     @type string      $label                 Name of the post type shown in the menu. Usually plural.
  1383  *     @type string       $label                 Name of the post type shown in the menu. Usually plural.
  1308  *                                              Default is value of $labels['name'].
  1384  *                                               Default is value of $labels['name'].
  1309  *     @type array       $labels                An array of labels for this post type. If not set, post
  1385  *     @type string[]     $labels                An array of labels for this post type. If not set, post
  1310  *                                              labels are inherited for non-hierarchical types and page
  1386  *                                               labels are inherited for non-hierarchical types and page
  1311  *                                              labels for hierarchical ones. See get_post_type_labels() for a full
  1387  *                                               labels for hierarchical ones. See get_post_type_labels() for a full
  1312  *                                              list of supported labels.
  1388  *                                               list of supported labels.
  1313  *     @type string      $description           A short descriptive summary of what the post type is.
  1389  *     @type string       $description           A short descriptive summary of what the post type is.
  1314  *                                              Default empty.
  1390  *                                               Default empty.
  1315  *     @type bool        $public                Whether a post type is intended for use publicly either via
  1391  *     @type bool         $public                Whether a post type is intended for use publicly either via
  1316  *                                              the admin interface or by front-end users. While the default
  1392  *                                               the admin interface or by front-end users. While the default
  1317  *                                              settings of $exclude_from_search, $publicly_queryable, $show_ui,
  1393  *                                               settings of $exclude_from_search, $publicly_queryable, $show_ui,
  1318  *                                              and $show_in_nav_menus are inherited from public, each does not
  1394  *                                               and $show_in_nav_menus are inherited from public, each does not
  1319  *                                              rely on this relationship and controls a very specific intention.
  1395  *                                               rely on this relationship and controls a very specific intention.
  1320  *                                              Default false.
  1396  *                                               Default false.
  1321  *     @type bool        $hierarchical          Whether the post type is hierarchical (e.g. page). Default false.
  1397  *     @type bool         $hierarchical          Whether the post type is hierarchical (e.g. page). Default false.
  1322  *     @type bool        $exclude_from_search   Whether to exclude posts with this post type from front end search
  1398  *     @type bool         $exclude_from_search   Whether to exclude posts with this post type from front end search
  1323  *                                              results. Default is the opposite value of $public.
  1399  *                                               results. Default is the opposite value of $public.
  1324  *     @type bool        $publicly_queryable    Whether queries can be performed on the front end for the post type
  1400  *     @type bool         $publicly_queryable    Whether queries can be performed on the front end for the post type
  1325  *                                              as part of parse_request(). Endpoints would include:
  1401  *                                               as part of parse_request(). Endpoints would include:
  1326  *                                              * ?post_type={post_type_key}
  1402  *                                               * ?post_type={post_type_key}
  1327  *                                              * ?{post_type_key}={single_post_slug}
  1403  *                                               * ?{post_type_key}={single_post_slug}
  1328  *                                              * ?{post_type_query_var}={single_post_slug}
  1404  *                                               * ?{post_type_query_var}={single_post_slug}
  1329  *                                              If not set, the default is inherited from $public.
  1405  *                                               If not set, the default is inherited from $public.
  1330  *     @type bool        $show_ui               Whether to generate and allow a UI for managing this post type in the
  1406  *     @type bool         $show_ui               Whether to generate and allow a UI for managing this post type in the
  1331  *                                              admin. Default is value of $public.
  1407  *                                               admin. Default is value of $public.
  1332  *     @type bool|string $show_in_menu          Where to show the post type in the admin menu. To work, $show_ui
  1408  *     @type bool|string  $show_in_menu          Where to show the post type in the admin menu. To work, $show_ui
  1333  *                                              must be true. If true, the post type is shown in its own top level
  1409  *                                               must be true. If true, the post type is shown in its own top level
  1334  *                                              menu. If false, no menu is shown. If a string of an existing top
  1410  *                                               menu. If false, no menu is shown. If a string of an existing top
  1335  *                                              level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post
  1411  *                                               level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post
  1336  *                                              type will be placed as a sub-menu of that.
  1412  *                                               type will be placed as a sub-menu of that.
  1337  *                                              Default is value of $show_ui.
  1413  *                                               Default is value of $show_ui.
  1338  *     @type bool        $show_in_nav_menus     Makes this post type available for selection in navigation menus.
  1414  *     @type bool         $show_in_nav_menus     Makes this post type available for selection in navigation menus.
  1339  *                                              Default is value of $public.
  1415  *                                               Default is value of $public.
  1340  *     @type bool        $show_in_admin_bar     Makes this post type available via the admin bar. Default is value
  1416  *     @type bool         $show_in_admin_bar     Makes this post type available via the admin bar. Default is value
  1341  *                                              of $show_in_menu.
  1417  *                                               of $show_in_menu.
  1342  *     @type bool        $show_in_rest          Whether to include the post type in the REST API. Set this to true
  1418  *     @type bool         $show_in_rest          Whether to include the post type in the REST API. Set this to true
  1343  *                                              for the post type to be available in the block editor.
  1419  *                                               for the post type to be available in the block editor.
  1344  *     @type string      $rest_base             To change the base url of REST API route. Default is $post_type.
  1420  *     @type string       $rest_base             To change the base url of REST API route. Default is $post_type.
  1345  *     @type string      $rest_controller_class REST API Controller class name. Default is 'WP_REST_Posts_Controller'.
  1421  *     @type string       $rest_controller_class REST API Controller class name. Default is 'WP_REST_Posts_Controller'.
  1346  *     @type int         $menu_position         The position in the menu order the post type should appear. To work,
  1422  *     @type int          $menu_position         The position in the menu order the post type should appear. To work,
  1347  *                                              $show_in_menu must be true. Default null (at the bottom).
  1423  *                                               $show_in_menu must be true. Default null (at the bottom).
  1348  *     @type string      $menu_icon             The url to the icon to be used for this menu. Pass a base64-encoded
  1424  *     @type string       $menu_icon             The url to the icon to be used for this menu. Pass a base64-encoded
  1349  *                                              SVG using a data URI, which will be colored to match the color scheme
  1425  *                                               SVG using a data URI, which will be colored to match the color scheme
  1350  *                                              -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
  1426  *                                               -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
  1351  *                                              of a Dashicons helper class to use a font icon, e.g.
  1427  *                                               of a Dashicons helper class to use a font icon, e.g.
  1352  *                                              'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
  1428  *                                               'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
  1353  *                                              so an icon can be added via CSS. Defaults to use the posts icon.
  1429  *                                               so an icon can be added via CSS. Defaults to use the posts icon.
  1354  *     @type string      $capability_type       The string to use to build the read, edit, and delete capabilities.
  1430  *     @type string       $capability_type       The string to use to build the read, edit, and delete capabilities.
  1355  *                                              May be passed as an array to allow for alternative plurals when using
  1431  *                                               May be passed as an array to allow for alternative plurals when using
  1356  *                                              this argument as a base to construct the capabilities, e.g.
  1432  *                                               this argument as a base to construct the capabilities, e.g.
  1357  *                                              array('story', 'stories'). Default 'post'.
  1433  *                                               array('story', 'stories'). Default 'post'.
  1358  *     @type array       $capabilities          Array of capabilities for this post type. $capability_type is used
  1434  *     @type string[]     $capabilities          Array of capabilities for this post type. $capability_type is used
  1359  *                                              as a base to construct capabilities by default.
  1435  *                                               as a base to construct capabilities by default.
  1360  *                                              See get_post_type_capabilities().
  1436  *                                               See get_post_type_capabilities().
  1361  *     @type bool        $map_meta_cap          Whether to use the internal default meta capability handling.
  1437  *     @type bool         $map_meta_cap          Whether to use the internal default meta capability handling.
  1362  *                                              Default false.
  1438  *                                               Default false.
  1363  *     @type array       $supports              Core feature(s) the post type supports. Serves as an alias for calling
  1439  *     @type array        $supports              Core feature(s) the post type supports. Serves as an alias for calling
  1364  *                                              add_post_type_support() directly. Core features include 'title',
  1440  *                                               add_post_type_support() directly. Core features include 'title',
  1365  *                                              'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',
  1441  *                                               'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',
  1366  *                                              'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.
  1442  *                                               'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.
  1367  *                                              Additionally, the 'revisions' feature dictates whether the post type
  1443  *                                               Additionally, the 'revisions' feature dictates whether the post type
  1368  *                                              will store revisions, and the 'comments' feature dictates whether the
  1444  *                                               will store revisions, and the 'comments' feature dictates whether the
  1369  *                                              comments count will show on the edit screen. A feature can also be
  1445  *                                               comments count will show on the edit screen. A feature can also be
  1370  *                                              specified as an array of arguments to provide additional information
  1446  *                                               specified as an array of arguments to provide additional information
  1371  *                                              about supporting that feature.
  1447  *                                               about supporting that feature.
  1372  *                                              Example: `array( 'my_feature', array( 'field' => 'value' ) )`.
  1448  *                                               Example: `array( 'my_feature', array( 'field' => 'value' ) )`.
  1373  *                                              Default is an array containing 'title' and 'editor'.
  1449  *                                               Default is an array containing 'title' and 'editor'.
  1374  *     @type callable    $register_meta_box_cb  Provide a callback function that sets up the meta boxes for the
  1450  *     @type callable     $register_meta_box_cb  Provide a callback function that sets up the meta boxes for the
  1375  *                                              edit form. Do remove_meta_box() and add_meta_box() calls in the
  1451  *                                               edit form. Do remove_meta_box() and add_meta_box() calls in the
  1376  *                                              callback. Default null.
  1452  *                                               callback. Default null.
  1377  *     @type array       $taxonomies            An array of taxonomy identifiers that will be registered for the
  1453  *     @type string[]     $taxonomies            An array of taxonomy identifiers that will be registered for the
  1378  *                                              post type. Taxonomies can be registered later with register_taxonomy()
  1454  *                                               post type. Taxonomies can be registered later with register_taxonomy()
  1379  *                                              or register_taxonomy_for_object_type().
  1455  *                                               or register_taxonomy_for_object_type().
  1380  *                                              Default empty array.
  1456  *                                               Default empty array.
  1381  *     @type bool|string $has_archive           Whether there should be post type archives, or if a string, the
  1457  *     @type bool|string  $has_archive           Whether there should be post type archives, or if a string, the
  1382  *                                              archive slug to use. Will generate the proper rewrite rules if
  1458  *                                               archive slug to use. Will generate the proper rewrite rules if
  1383  *                                              $rewrite is enabled. Default false.
  1459  *                                               $rewrite is enabled. Default false.
  1384  *     @type bool|array  $rewrite              {
  1460  *     @type bool|array   $rewrite               {
  1385  *         Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
  1461  *         Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
  1386  *         Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
  1462  *         Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
  1387  *         passed with any of these keys:
  1463  *         passed with any of these keys:
  1388  *
  1464  *
  1389  *         @type string $slug       Customize the permastruct slug. Defaults to $post_type key.
  1465  *         @type string $slug       Customize the permastruct slug. Defaults to $post_type key.
  1390  *         @type bool   $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
  1466  *         @type bool   $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
  1391  *                                  Default true.
  1467  *                                  Default true.
  1392  *         @type bool   $feeds      Whether the feed permastruct should be built for this post type.
  1468  *         @type bool   $feeds      Whether the feed permastruct should be built for this post type.
  1393  *                                  Default is value of $has_archive.
  1469  *                                  Default is value of $has_archive.
  1394  *         @type bool   $pages      Whether the permastruct should provide for pagination. Default true.
  1470  *         @type bool   $pages      Whether the permastruct should provide for pagination. Default true.
  1395  *         @type const  $ep_mask    Endpoint mask to assign. If not specified and permalink_epmask is set,
  1471  *         @type int    $ep_mask    Endpoint mask to assign. If not specified and permalink_epmask is set,
  1396  *                                  inherits from $permalink_epmask. If not specified and permalink_epmask
  1472  *                                  inherits from $permalink_epmask. If not specified and permalink_epmask
  1397  *                                  is not set, defaults to EP_PERMALINK.
  1473  *                                  is not set, defaults to EP_PERMALINK.
  1398  *     }
  1474  *     }
  1399  *     @type string|bool $query_var             Sets the query_var key for this post type. Defaults to $post_type
  1475  *     @type string|bool  $query_var             Sets the query_var key for this post type. Defaults to $post_type
  1400  *                                              key. If false, a post type cannot be loaded at
  1476  *                                               key. If false, a post type cannot be loaded at
  1401  *                                              ?{query_var}={post_slug}. If specified as a string, the query
  1477  *                                               ?{query_var}={post_slug}. If specified as a string, the query
  1402  *                                              ?{query_var_string}={post_slug} will be valid.
  1478  *                                               ?{query_var_string}={post_slug} will be valid.
  1403  *     @type bool        $can_export            Whether to allow this post type to be exported. Default true.
  1479  *     @type bool         $can_export            Whether to allow this post type to be exported. Default true.
  1404  *     @type bool        $delete_with_user      Whether to delete posts of this type when deleting a user. If true,
  1480  *     @type bool         $delete_with_user      Whether to delete posts of this type when deleting a user.
  1405  *                                              posts of this type belonging to the user will be moved to Trash
  1481  *                                               * If true, posts of this type belonging to the user will be moved
  1406  *                                              when then user is deleted. If false, posts of this type belonging
  1482  *                                                 to Trash when the user is deleted.
  1407  *                                              to the user will *not* be trashed or deleted. If not set (the default),
  1483  *                                               * If false, posts of this type belonging to the user will *not*
  1408  *                                              posts are trashed if post_type_supports('author'). Otherwise posts
  1484  *                                                 be trashed or deleted.
  1409  *                                              are not trashed or deleted. Default null.
  1485  *                                               * If not set (the default), posts are trashed if post type supports
  1410  *     @type bool        $_builtin              FOR INTERNAL USE ONLY! True if this post type is a native or
  1486  *                                                 the 'author' feature. Otherwise posts are not trashed or deleted.
  1411  *                                              "built-in" post_type. Default false.
  1487  *                                               Default null.
  1412  *     @type string      $_edit_link            FOR INTERNAL USE ONLY! URL segment to use for edit link of
  1488  *     @type array        $template              Array of blocks to use as the default initial state for an editor
  1413  *                                              this post type. Default 'post.php?post=%d'.
  1489  *                                               session. Each item should be an array containing block name and
       
  1490  *                                               optional attributes. Default empty array.
       
  1491  *     @type string|false $template_lock         Whether the block template should be locked if $template is set.
       
  1492  *                                               * If set to 'all', the user is unable to insert new blocks,
       
  1493  *                                                 move existing blocks and delete blocks.
       
  1494  *                                               * If set to 'insert', the user is able to move existing blocks
       
  1495  *                                                 but is unable to insert new blocks and delete blocks.
       
  1496  *                                               Default false.
       
  1497  *     @type bool         $_builtin              FOR INTERNAL USE ONLY! True if this post type is a native or
       
  1498  *                                               "built-in" post_type. Default false.
       
  1499  *     @type string       $_edit_link            FOR INTERNAL USE ONLY! URL segment to use for edit link of
       
  1500  *                                               this post type. Default 'post.php?post=%d'.
  1414  * }
  1501  * }
  1415  * @return WP_Post_Type|WP_Error The registered post type object on success,
  1502  * @return WP_Post_Type|WP_Error The registered post type object on success,
  1416  *                               WP_Error object on failure.
  1503  *                               WP_Error object on failure.
  1417  */
  1504  */
  1418 function register_post_type( $post_type, $args = array() ) {
  1505 function register_post_type( $post_type, $args = array() ) {
  1462  * @since 4.5.0
  1549  * @since 4.5.0
  1463  *
  1550  *
  1464  * @global array $wp_post_types List of post types.
  1551  * @global array $wp_post_types List of post types.
  1465  *
  1552  *
  1466  * @param string $post_type Post type to unregister.
  1553  * @param string $post_type Post type to unregister.
  1467  * @return bool|WP_Error True on success, WP_Error on failure or if the post type doesn't exist.
  1554  * @return true|WP_Error True on success, WP_Error on failure or if the post type doesn't exist.
  1468  */
  1555  */
  1469 function unregister_post_type( $post_type ) {
  1556 function unregister_post_type( $post_type ) {
  1470 	global $wp_post_types;
  1557 	global $wp_post_types;
  1471 
  1558 
  1472 	if ( ! post_type_exists( $post_type ) ) {
  1559 	if ( ! post_type_exists( $post_type ) ) {
  1658  * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'.
  1745  * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'.
  1659  * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'.
  1746  * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'.
  1660  * - `menu_name` - Label for the menu name. Default is the same as `name`.
  1747  * - `menu_name` - Label for the menu name. Default is the same as `name`.
  1661  * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' /
  1748  * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' /
  1662  *                       'Filter pages list'.
  1749  *                       'Filter pages list'.
       
  1750  * - `filter_by_date` - Label for the date filter in list tables. Default is 'Filter by date'.
  1663  * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' /
  1751  * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' /
  1664  *                           'Pages list navigation'.
  1752  *                           'Pages list navigation'.
  1665  * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'.
  1753  * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'.
  1666  * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.'
  1754  * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.'
  1667  * - `item_published_privately` - Label used when an item is published with private visibility.
  1755  * - `item_published_privately` - Label used when an item is published with private visibility.
  1669  * - `item_reverted_to_draft` - Label used when an item is switched to a draft.
  1757  * - `item_reverted_to_draft` - Label used when an item is switched to a draft.
  1670  *                            Default is 'Post reverted to draft.' / 'Page reverted to draft.'
  1758  *                            Default is 'Post reverted to draft.' / 'Page reverted to draft.'
  1671  * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' /
  1759  * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' /
  1672  *                    'Page scheduled.'
  1760  *                    'Page scheduled.'
  1673  * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.'
  1761  * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.'
       
  1762  * - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'.
       
  1763  * - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' /
       
  1764  *                             'A link to a page.'
  1674  *
  1765  *
  1675  * Above, the first default value is for non-hierarchical post types (like posts)
  1766  * Above, the first default value is for non-hierarchical post types (like posts)
  1676  * and the second one is for hierarchical post types (like pages).
  1767  * and the second one is for hierarchical post types (like pages).
  1677  *
  1768  *
  1678  * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter.
  1769  * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter.
  1684  *              `items_list_navigation`, and `items_list` labels.
  1775  *              `items_list_navigation`, and `items_list` labels.
  1685  * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
  1776  * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
  1686  * @since 4.7.0 Added the `view_items` and `attributes` labels.
  1777  * @since 4.7.0 Added the `view_items` and `attributes` labels.
  1687  * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`,
  1778  * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`,
  1688  *              `item_scheduled`, and `item_updated` labels.
  1779  *              `item_scheduled`, and `item_updated` labels.
       
  1780  * @since 5.7.0 Added the `filter_by_date` label.
       
  1781  * @since 5.8.0 Added the `item_link` and `item_link_description` labels.
  1689  *
  1782  *
  1690  * @access private
  1783  * @access private
  1691  *
  1784  *
  1692  * @param object|WP_Post_Type $post_type_object Post type object.
  1785  * @param object|WP_Post_Type $post_type_object Post type object.
  1693  * @return object Object with all the labels as member variables.
  1786  * @return object Object with all the labels as member variables.
  1694  */
  1787  */
  1695 function get_post_type_labels( $post_type_object ) {
  1788 function get_post_type_labels( $post_type_object ) {
  1696 	$nohier_vs_hier_defaults              = array(
  1789 	$nohier_vs_hier_defaults = array(
  1697 		'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
  1790 		'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
  1698 		'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
  1791 		'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
  1699 		'add_new'                  => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
  1792 		'add_new'                  => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
  1700 		'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
  1793 		'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
  1701 		'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
  1794 		'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
  1714 		'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
  1807 		'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
  1715 		'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
  1808 		'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
  1716 		'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
  1809 		'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
  1717 		'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
  1810 		'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
  1718 		'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
  1811 		'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
       
  1812 		'filter_by_date'           => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
  1719 		'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
  1813 		'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
  1720 		'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
  1814 		'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
  1721 		'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
  1815 		'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
  1722 		'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
  1816 		'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
  1723 		'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
  1817 		'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
  1724 		'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
  1818 		'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
  1725 		'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
  1819 		'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
       
  1820 		'item_link'                => array(
       
  1821 			_x( 'Post Link', 'navigation link block title' ),
       
  1822 			_x( 'Page Link', 'navigation link block title' ),
       
  1823 		),
       
  1824 		'item_link_description'    => array(
       
  1825 			_x( 'A link to a post.', 'navigation link block description' ),
       
  1826 			_x( 'A link to a page.', 'navigation link block description' ),
       
  1827 		),
  1726 	);
  1828 	);
       
  1829 
  1727 	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1830 	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1728 
  1831 
  1729 	$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1832 	$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1730 
  1833 
  1731 	$post_type = $post_type_object->name;
  1834 	$post_type = $post_type_object->name;
  1735 	/**
  1838 	/**
  1736 	 * Filters the labels of a specific post type.
  1839 	 * Filters the labels of a specific post type.
  1737 	 *
  1840 	 *
  1738 	 * The dynamic portion of the hook name, `$post_type`, refers to
  1841 	 * The dynamic portion of the hook name, `$post_type`, refers to
  1739 	 * the post type slug.
  1842 	 * the post type slug.
       
  1843 	 *
       
  1844 	 * Possible hook names include:
       
  1845 	 *
       
  1846 	 *  - `post_type_labels_post`
       
  1847 	 *  - `post_type_labels_page`
       
  1848 	 *  - `post_type_labels_attachment`
  1740 	 *
  1849 	 *
  1741 	 * @since 3.5.0
  1850 	 * @since 3.5.0
  1742 	 *
  1851 	 *
  1743 	 * @see get_post_type_labels() for the full list of labels.
  1852 	 * @see get_post_type_labels() for the full list of labels.
  1744 	 *
  1853 	 *
  1985 		if ( ! $post_type ) {
  2094 		if ( ! $post_type ) {
  1986 			return false;
  2095 			return false;
  1987 		}
  2096 		}
  1988 	}
  2097 	}
  1989 
  2098 
       
  2099 	if ( ! is_object( $post_type ) ) {
       
  2100 		return false;
       
  2101 	}
       
  2102 
  1990 	return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
  2103 	return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
  1991 }
  2104 }
  1992 
  2105 
  1993 /**
  2106 /**
       
  2107  * Determine whether a post status is considered "viewable".
       
  2108  *
       
  2109  * For built-in post statuses such as publish and private, the 'public' value will be evaluted.
       
  2110  * For all others, the 'publicly_queryable' value will be used.
       
  2111  *
       
  2112  * @since 5.7.0
       
  2113  *
       
  2114  * @param string|stdClass $post_status Post status name or object.
       
  2115  * @return bool Whether the post status should be considered viewable.
       
  2116  */
       
  2117 function is_post_status_viewable( $post_status ) {
       
  2118 	if ( is_scalar( $post_status ) ) {
       
  2119 		$post_status = get_post_status_object( $post_status );
       
  2120 		if ( ! $post_status ) {
       
  2121 			return false;
       
  2122 		}
       
  2123 	}
       
  2124 
       
  2125 	if (
       
  2126 		! is_object( $post_status ) ||
       
  2127 		$post_status->internal ||
       
  2128 		$post_status->protected
       
  2129 	) {
       
  2130 		return false;
       
  2131 	}
       
  2132 
       
  2133 	return $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
       
  2134 }
       
  2135 
       
  2136 /**
       
  2137  * Determine whether a post is publicly viewable.
       
  2138  *
       
  2139  * Posts are considered publicly viewable if both the post status and post type
       
  2140  * are viewable.
       
  2141  *
       
  2142  * @since 5.7.0
       
  2143  *
       
  2144  * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
       
  2145  * @return bool Whether the post is publicly viewable.
       
  2146  */
       
  2147 function is_post_publicly_viewable( $post = null ) {
       
  2148 	$post = get_post( $post );
       
  2149 
       
  2150 	if ( ! $post ) {
       
  2151 		return false;
       
  2152 	}
       
  2153 
       
  2154 	$post_type   = get_post_type( $post );
       
  2155 	$post_status = get_post_status( $post );
       
  2156 
       
  2157 	return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
       
  2158 }
       
  2159 
       
  2160 /**
  1994  * Retrieves an array of the latest posts, or posts matching the given criteria.
  2161  * Retrieves an array of the latest posts, or posts matching the given criteria.
  1995  *
  2162  *
       
  2163  * For more information on the accepted arguments, see the
       
  2164  * {@link https://developer.wordpress.org/reference/classes/wp_query/
       
  2165  * WP_Query} documentation in the Developer Handbook.
       
  2166  *
       
  2167  * The `$ignore_sticky_posts` and `$no_found_rows` arguments are ignored by
       
  2168  * this function and both are set to `true`.
       
  2169  *
  1996  * The defaults are as follows:
  2170  * The defaults are as follows:
  1997  *
  2171  *
  1998  * @since 1.2.0
  2172  * @since 1.2.0
  1999  *
  2173  *
       
  2174  * @see WP_Query
  2000  * @see WP_Query::parse_query()
  2175  * @see WP_Query::parse_query()
  2001  *
  2176  *
  2002  * @param array $args {
  2177  * @param array $args {
  2003  *     Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all
  2178  *     Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all
  2004  *     available arguments.
  2179  *     available arguments.
  2005  *
  2180  *
  2006  *     @type int        $numberposts      Total number of posts to retrieve. Is an alias of $posts_per_page
  2181  *     @type int        $numberposts      Total number of posts to retrieve. Is an alias of `$posts_per_page`
  2007  *                                        in WP_Query. Accepts -1 for all. Default 5.
  2182  *                                        in WP_Query. Accepts -1 for all. Default 5.
  2008  *     @type int|string $category         Category ID or comma-separated list of IDs (this or any children).
  2183  *     @type int|string $category         Category ID or comma-separated list of IDs (this or any children).
  2009  *                                        Is an alias of $cat in WP_Query. Default 0.
  2184  *                                        Is an alias of `$cat` in WP_Query. Default 0.
  2010  *     @type array      $include          An array of post IDs to retrieve, sticky posts will be included.
  2185  *     @type int[]      $include          An array of post IDs to retrieve, sticky posts will be included.
  2011  *                                        Is an alias of $post__in in WP_Query. Default empty array.
  2186  *                                        Is an alias of `$post__in` in WP_Query. Default empty array.
  2012  *     @type array      $exclude          An array of post IDs not to retrieve. Default empty array.
  2187  *     @type int[]      $exclude          An array of post IDs not to retrieve. Default empty array.
  2013  *     @type bool       $suppress_filters Whether to suppress filters. Default true.
  2188  *     @type bool       $suppress_filters Whether to suppress filters. Default true.
  2014  * }
  2189  * }
  2015  * @return WP_Post[]|int[] Array of post objects or post IDs.
  2190  * @return WP_Post[]|int[] Array of post objects or post IDs.
  2016  */
  2191  */
  2017 function get_posts( $args = null ) {
  2192 function get_posts( $args = null ) {
  2115  *
  2290  *
  2116  * @param int    $post_id Post ID.
  2291  * @param int    $post_id Post ID.
  2117  * @param string $key     Optional. The meta key to retrieve. By default,
  2292  * @param string $key     Optional. The meta key to retrieve. By default,
  2118  *                        returns data for all keys. Default empty.
  2293  *                        returns data for all keys. Default empty.
  2119  * @param bool   $single  Optional. Whether to return a single value.
  2294  * @param bool   $single  Optional. Whether to return a single value.
  2120  *                        This parameter has no effect if $key is not specified.
  2295  *                        This parameter has no effect if `$key` is not specified.
  2121  *                        Default false.
  2296  *                        Default false.
  2122  * @return mixed An array if $single is false. The value of the meta field
  2297  * @return mixed An array of values if `$single` is false.
  2123  *               if $single is true. False for an invalid $post_id.
  2298  *               The value of the meta field if `$single` is true.
       
  2299  *               False for an invalid `$post_id` (non-numeric, zero, or negative value).
       
  2300  *               An empty string if a valid but non-existing post ID is passed.
  2124  */
  2301  */
  2125 function get_post_meta( $post_id, $key = '', $single = false ) {
  2302 function get_post_meta( $post_id, $key = '', $single = false ) {
  2126 	return get_metadata( 'post', $post_id, $key, $single );
  2303 	return get_metadata( 'post', $post_id, $key, $single );
  2127 }
  2304 }
  2128 
  2305 
  2309 	 */
  2486 	 */
  2310 	return apply_filters( 'is_sticky', $is_sticky, $post_id );
  2487 	return apply_filters( 'is_sticky', $is_sticky, $post_id );
  2311 }
  2488 }
  2312 
  2489 
  2313 /**
  2490 /**
  2314  * Sanitize every post field.
  2491  * Sanitizes every post field.
  2315  *
  2492  *
  2316  * If the context is 'raw', then the post object or array will get minimal
  2493  * If the context is 'raw', then the post object or array will get minimal
  2317  * sanitization of the integer fields.
  2494  * sanitization of the integer fields.
  2318  *
  2495  *
  2319  * @since 2.3.0
  2496  * @since 2.3.0
  2320  *
  2497  *
  2321  * @see sanitize_post_field()
  2498  * @see sanitize_post_field()
  2322  *
  2499  *
  2323  * @param object|WP_Post|array $post    The Post Object or Array
  2500  * @param object|WP_Post|array $post    The post object or array
  2324  * @param string               $context Optional. How to sanitize post fields.
  2501  * @param string               $context Optional. How to sanitize post fields.
  2325  *                                      Accepts 'raw', 'edit', 'db', or 'display'.
  2502  *                                      Accepts 'raw', 'edit', 'db', 'display',
  2326  *                                      Default 'display'.
  2503  *                                      'attribute', or 'js'. Default 'display'.
  2327  * @return object|WP_Post|array The now sanitized Post Object or Array (will be the
  2504  * @return object|WP_Post|array The now sanitized post object or array (will be the
  2328  *                              same type as $post).
  2505  *                              same type as `$post`).
  2329  */
  2506  */
  2330 function sanitize_post( $post, $context = 'display' ) {
  2507 function sanitize_post( $post, $context = 'display' ) {
  2331 	if ( is_object( $post ) ) {
  2508 	if ( is_object( $post ) ) {
  2332 		// Check if post already filtered for this context.
  2509 		// Check if post already filtered for this context.
  2333 		if ( isset( $post->filter ) && $context == $post->filter ) {
  2510 		if ( isset( $post->filter ) && $context == $post->filter ) {
  2493 			 *
  2670 			 *
  2494 			 * @since 2.3.0
  2671 			 * @since 2.3.0
  2495 			 *
  2672 			 *
  2496 			 * @param mixed  $value   Value of the prefixed post field.
  2673 			 * @param mixed  $value   Value of the prefixed post field.
  2497 			 * @param int    $post_id Post ID.
  2674 			 * @param int    $post_id Post ID.
  2498 			 * @param string $context Context for how to sanitize the field. Possible
  2675 			 * @param string $context Context for how to sanitize the field.
  2499 			 *                        values include 'edit', 'display',
  2676 			 *                        Accepts 'raw', 'edit', 'db', 'display',
  2500 			 *                        'attribute' and 'js'.
  2677 			 *                        'attribute', or 'js'. Default 'display'.
  2501 			 */
  2678 			 */
  2502 			$value = apply_filters( "{$field}", $value, $post_id, $context );
  2679 			$value = apply_filters( "{$field}", $value, $post_id, $context );
  2503 		} else {
  2680 		} else {
  2504 			$value = apply_filters( "post_{$field}", $value, $post_id, $context );
  2681 			$value = apply_filters( "post_{$field}", $value, $post_id, $context );
  2505 		}
  2682 		}
  2509 		} elseif ( 'js' === $context ) {
  2686 		} elseif ( 'js' === $context ) {
  2510 			$value = esc_js( $value );
  2687 			$value = esc_js( $value );
  2511 		}
  2688 		}
  2512 	}
  2689 	}
  2513 
  2690 
       
  2691 	// Restore the type for integer fields after esc_attr().
       
  2692 	if ( in_array( $field, $int_fields, true ) ) {
       
  2693 		$value = (int) $value;
       
  2694 	}
       
  2695 
  2514 	return $value;
  2696 	return $value;
  2515 }
  2697 }
  2516 
  2698 
  2517 /**
  2699 /**
  2518  * Make a post sticky.
  2700  * Make a post sticky.
  2524  * @param int $post_id Post ID.
  2706  * @param int $post_id Post ID.
  2525  */
  2707  */
  2526 function stick_post( $post_id ) {
  2708 function stick_post( $post_id ) {
  2527 	$post_id  = (int) $post_id;
  2709 	$post_id  = (int) $post_id;
  2528 	$stickies = get_option( 'sticky_posts' );
  2710 	$stickies = get_option( 'sticky_posts' );
       
  2711 	$updated  = false;
  2529 
  2712 
  2530 	if ( ! is_array( $stickies ) ) {
  2713 	if ( ! is_array( $stickies ) ) {
  2531 		$stickies = array();
  2714 		$stickies = array( $post_id );
  2532 	}
  2715 	} else {
  2533 
  2716 		$stickies = array_unique( array_map( 'intval', $stickies ) );
  2534 	$stickies = array_map( 'intval', $stickies );
  2717 	}
  2535 
  2718 
  2536 	if ( ! in_array( $post_id, $stickies, true ) ) {
  2719 	if ( ! in_array( $post_id, $stickies, true ) ) {
  2537 		$stickies[] = $post_id;
  2720 		$stickies[] = $post_id;
  2538 	}
  2721 		$updated    = update_option( 'sticky_posts', array_values( $stickies ) );
  2539 
  2722 	}
  2540 	$updated = update_option( 'sticky_posts', $stickies );
       
  2541 
  2723 
  2542 	if ( $updated ) {
  2724 	if ( $updated ) {
  2543 		/**
  2725 		/**
  2544 		 * Fires once a post has been added to the sticky list.
  2726 		 * Fires once a post has been added to the sticky list.
  2545 		 *
  2727 		 *
  2566 
  2748 
  2567 	if ( ! is_array( $stickies ) ) {
  2749 	if ( ! is_array( $stickies ) ) {
  2568 		return;
  2750 		return;
  2569 	}
  2751 	}
  2570 
  2752 
  2571 	$stickies = array_map( 'intval', $stickies );
  2753 	$stickies = array_values( array_unique( array_map( 'intval', $stickies ) ) );
  2572 
  2754 
  2573 	if ( ! in_array( $post_id, $stickies, true ) ) {
  2755 	if ( ! in_array( $post_id, $stickies, true ) ) {
  2574 		return;
  2756 		return;
  2575 	}
  2757 	}
  2576 
  2758 
  2708  *
  2890  *
  2709  * @since 2.5.0
  2891  * @since 2.5.0
  2710  *
  2892  *
  2711  * @global wpdb $wpdb WordPress database abstraction object.
  2893  * @global wpdb $wpdb WordPress database abstraction object.
  2712  *
  2894  *
  2713  * @param string|array $mime_type Optional. Array or comma-separated list of
  2895  * @param string|string[] $mime_type Optional. Array or comma-separated list of
  2714  *                                MIME patterns. Default empty.
  2896  *                                   MIME patterns. Default empty.
  2715  * @return object An object containing the attachment counts by mime type.
  2897  * @return object An object containing the attachment counts by mime type.
  2716  */
  2898  */
  2717 function wp_count_attachments( $mime_type = '' ) {
  2899 function wp_count_attachments( $mime_type = '' ) {
  2718 	global $wpdb;
  2900 	global $wpdb;
  2719 
  2901 
  2729 	/**
  2911 	/**
  2730 	 * Modify returned attachment counts by mime type.
  2912 	 * Modify returned attachment counts by mime type.
  2731 	 *
  2913 	 *
  2732 	 * @since 3.7.0
  2914 	 * @since 3.7.0
  2733 	 *
  2915 	 *
  2734 	 * @param object $counts    An object containing the attachment counts by
  2916 	 * @param object          $counts    An object containing the attachment counts by
  2735 	 *                          mime type.
  2917 	 *                                   mime type.
  2736 	 * @param string $mime_type The mime type pattern used to filter the attachments
  2918 	 * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
  2737 	 *                          counted.
       
  2738 	 */
  2919 	 */
  2739 	return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
  2920 	return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
  2740 }
  2921 }
  2741 
  2922 
  2742 /**
  2923 /**
  2850  * list. If the real_mime_types is a string, it is also comma separated to
  3031  * list. If the real_mime_types is a string, it is also comma separated to
  2851  * create the list.
  3032  * create the list.
  2852  *
  3033  *
  2853  * @since 2.5.0
  3034  * @since 2.5.0
  2854  *
  3035  *
  2855  * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*)
  3036  * @param string|string[] $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*)
  2856  *                                          or flash (same as *flash*).
  3037  *                                             or flash (same as *flash*).
  2857  * @param string|array $real_mime_types     Real post mime type values.
  3038  * @param string|string[] $real_mime_types     Real post mime type values.
  2858  * @return array array(wildcard=>array(real types)).
  3039  * @return array array(wildcard=>array(real types)).
  2859  */
  3040  */
  2860 function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
  3041 function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
  2861 	$matches = array();
  3042 	$matches = array();
  2862 	if ( is_string( $wildcard_mime_types ) ) {
  3043 	if ( is_string( $wildcard_mime_types ) ) {
  2902 /**
  3083 /**
  2903  * Convert MIME types into SQL.
  3084  * Convert MIME types into SQL.
  2904  *
  3085  *
  2905  * @since 2.5.0
  3086  * @since 2.5.0
  2906  *
  3087  *
  2907  * @param string|array $post_mime_types List of mime types or comma separated string
  3088  * @param string|string[] $post_mime_types List of mime types or comma separated string
  2908  *                                      of mime types.
  3089  *                                         of mime types.
  2909  * @param string       $table_alias     Optional. Specify a table alias, if needed.
  3090  * @param string          $table_alias     Optional. Specify a table alias, if needed.
  2910  *                                      Default empty.
  3091  *                                         Default empty.
  2911  * @return string The SQL AND clause for mime searching.
  3092  * @return string The SQL AND clause for mime searching.
  2912  */
  3093  */
  2913 function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
  3094 function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
  2914 	$where     = '';
  3095 	$where     = '';
  2915 	$wildcards = array( '', '%', '%/%' );
  3096 	$wildcards = array( '', '%', '%/%' );
  2950 			$wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
  3131 			$wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
  2951 		}
  3132 		}
  2952 	}
  3133 	}
  2953 
  3134 
  2954 	if ( ! empty( $wheres ) ) {
  3135 	if ( ! empty( $wheres ) ) {
  2955 		$where = ' AND (' . join( ' OR ', $wheres ) . ') ';
  3136 		$where = ' AND (' . implode( ' OR ', $wheres ) . ') ';
  2956 	}
  3137 	}
  2957 
  3138 
  2958 	return $where;
  3139 	return $where;
  2959 }
  3140 }
  2960 
  3141 
  3185 	 *
  3366 	 *
  3186 	 * @param bool|null $trash Whether to go forward with trashing.
  3367 	 * @param bool|null $trash Whether to go forward with trashing.
  3187 	 * @param WP_Post   $post  Post object.
  3368 	 * @param WP_Post   $post  Post object.
  3188 	 */
  3369 	 */
  3189 	$check = apply_filters( 'pre_trash_post', null, $post );
  3370 	$check = apply_filters( 'pre_trash_post', null, $post );
       
  3371 
  3190 	if ( null !== $check ) {
  3372 	if ( null !== $check ) {
  3191 		return $check;
  3373 		return $check;
  3192 	}
  3374 	}
  3193 
  3375 
  3194 	/**
  3376 	/**
  3227 
  3409 
  3228 	return $post;
  3410 	return $post;
  3229 }
  3411 }
  3230 
  3412 
  3231 /**
  3413 /**
  3232  * Restore a post or page from the Trash.
  3414  * Restores a post from the Trash.
  3233  *
  3415  *
  3234  * @since 2.9.0
  3416  * @since 2.9.0
  3235  *
  3417  * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for
  3236  * @param int $post_id Optional. Post ID. Default is ID of the global $post.
  3418  *              attachments which are returned to their original 'inherit' status.
       
  3419  *
       
  3420  * @param int $post_id Optional. Post ID. Default is ID of the global `$post`.
  3237  * @return WP_Post|false|null Post data on success, false or null on failure.
  3421  * @return WP_Post|false|null Post data on success, false or null on failure.
  3238  */
  3422  */
  3239 function wp_untrash_post( $post_id = 0 ) {
  3423 function wp_untrash_post( $post_id = 0 ) {
  3240 	$post = get_post( $post_id );
  3424 	$post = get_post( $post_id );
  3241 
  3425 
  3242 	if ( ! $post ) {
  3426 	if ( ! $post ) {
  3243 		return $post;
  3427 		return $post;
  3244 	}
  3428 	}
  3245 
  3429 
       
  3430 	$post_id = $post->ID;
       
  3431 
  3246 	if ( 'trash' !== $post->post_status ) {
  3432 	if ( 'trash' !== $post->post_status ) {
  3247 		return false;
  3433 		return false;
  3248 	}
  3434 	}
  3249 
  3435 
       
  3436 	$previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
       
  3437 
  3250 	/**
  3438 	/**
  3251 	 * Filters whether a post untrashing should take place.
  3439 	 * Filters whether a post untrashing should take place.
  3252 	 *
  3440 	 *
  3253 	 * @since 4.9.0
  3441 	 * @since 4.9.0
  3254 	 *
  3442 	 * @since 5.6.0 The `$previous_status` parameter was added.
  3255 	 * @param bool|null $untrash Whether to go forward with untrashing.
  3443 	 *
  3256 	 * @param WP_Post   $post    Post object.
  3444 	 * @param bool|null $untrash         Whether to go forward with untrashing.
       
  3445 	 * @param WP_Post   $post            Post object.
       
  3446 	 * @param string    $previous_status The status of the post at the point where it was trashed.
  3257 	 */
  3447 	 */
  3258 	$check = apply_filters( 'pre_untrash_post', null, $post );
  3448 	$check = apply_filters( 'pre_untrash_post', null, $post, $previous_status );
  3259 	if ( null !== $check ) {
  3449 	if ( null !== $check ) {
  3260 		return $check;
  3450 		return $check;
  3261 	}
  3451 	}
  3262 
  3452 
  3263 	/**
  3453 	/**
  3264 	 * Fires before a post is restored from the Trash.
  3454 	 * Fires before a post is restored from the Trash.
  3265 	 *
  3455 	 *
  3266 	 * @since 2.9.0
  3456 	 * @since 2.9.0
  3267 	 *
  3457 	 * @since 5.6.0 The `$previous_status` parameter was added.
  3268 	 * @param int $post_id Post ID.
  3458 	 *
       
  3459 	 * @param int    $post_id         Post ID.
       
  3460 	 * @param string $previous_status The status of the post at the point where it was trashed.
  3269 	 */
  3461 	 */
  3270 	do_action( 'untrash_post', $post_id );
  3462 	do_action( 'untrash_post', $post_id, $previous_status );
  3271 
  3463 
  3272 	$post_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
  3464 	$new_status = ( 'attachment' === $post->post_type ) ? 'inherit' : 'draft';
       
  3465 
       
  3466 	/**
       
  3467 	 * Filters the status that a post gets assigned when it is restored from the trash (untrashed).
       
  3468 	 *
       
  3469 	 * By default posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status`
       
  3470 	 * in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()`
       
  3471 	 * function is available for this.
       
  3472 	 *
       
  3473 	 * Prior to WordPress 5.6.0, restored posts were always assigned their original status.
       
  3474 	 *
       
  3475 	 * @since 5.6.0
       
  3476 	 *
       
  3477 	 * @param string $new_status      The new status of the post being restored.
       
  3478 	 * @param int    $post_id         The ID of the post being restored.
       
  3479 	 * @param string $previous_status The status of the post at the point where it was trashed.
       
  3480 	 */
       
  3481 	$post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status );
  3273 
  3482 
  3274 	delete_post_meta( $post_id, '_wp_trash_meta_status' );
  3483 	delete_post_meta( $post_id, '_wp_trash_meta_status' );
  3275 	delete_post_meta( $post_id, '_wp_trash_meta_time' );
  3484 	delete_post_meta( $post_id, '_wp_trash_meta_time' );
  3276 
  3485 
  3277 	$post_updated = wp_update_post(
  3486 	$post_updated = wp_update_post(
  3289 
  3498 
  3290 	/**
  3499 	/**
  3291 	 * Fires after a post is restored from the Trash.
  3500 	 * Fires after a post is restored from the Trash.
  3292 	 *
  3501 	 *
  3293 	 * @since 2.9.0
  3502 	 * @since 2.9.0
  3294 	 *
  3503 	 * @since 5.6.0 The `$previous_status` parameter was added.
  3295 	 * @param int $post_id Post ID.
  3504 	 *
       
  3505 	 * @param int    $post_id         Post ID.
       
  3506 	 * @param string $previous_status The status of the post at the point where it was trashed.
  3296 	 */
  3507 	 */
  3297 	do_action( 'untrashed_post', $post_id );
  3508 	do_action( 'untrashed_post', $post_id, $previous_status );
  3298 
  3509 
  3299 	return $post;
  3510 	return $post;
  3300 }
  3511 }
  3301 
  3512 
  3302 /**
  3513 /**
  3311  */
  3522  */
  3312 function wp_trash_post_comments( $post = null ) {
  3523 function wp_trash_post_comments( $post = null ) {
  3313 	global $wpdb;
  3524 	global $wpdb;
  3314 
  3525 
  3315 	$post = get_post( $post );
  3526 	$post = get_post( $post );
  3316 	if ( empty( $post ) ) {
  3527 
       
  3528 	if ( ! $post ) {
  3317 		return;
  3529 		return;
  3318 	}
  3530 	}
  3319 
  3531 
  3320 	$post_id = $post->ID;
  3532 	$post_id = $post->ID;
  3321 
  3533 
  3327 	 * @param int $post_id Post ID.
  3539 	 * @param int $post_id Post ID.
  3328 	 */
  3540 	 */
  3329 	do_action( 'trash_post_comments', $post_id );
  3541 	do_action( 'trash_post_comments', $post_id );
  3330 
  3542 
  3331 	$comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) );
  3543 	$comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) );
  3332 	if ( empty( $comments ) ) {
  3544 
       
  3545 	if ( ! $comments ) {
  3333 		return;
  3546 		return;
  3334 	}
  3547 	}
  3335 
  3548 
  3336 	// Cache current status for each comment.
  3549 	// Cache current status for each comment.
  3337 	$statuses = array();
  3550 	$statuses = array();
  3370  */
  3583  */
  3371 function wp_untrash_post_comments( $post = null ) {
  3584 function wp_untrash_post_comments( $post = null ) {
  3372 	global $wpdb;
  3585 	global $wpdb;
  3373 
  3586 
  3374 	$post = get_post( $post );
  3587 	$post = get_post( $post );
  3375 	if ( empty( $post ) ) {
  3588 
       
  3589 	if ( ! $post ) {
  3376 		return;
  3590 		return;
  3377 	}
  3591 	}
  3378 
  3592 
  3379 	$post_id = $post->ID;
  3593 	$post_id = $post->ID;
  3380 
  3594 
  3381 	$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );
  3595 	$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );
  3382 
  3596 
  3383 	if ( empty( $statuses ) ) {
  3597 	if ( ! $statuses ) {
  3384 		return true;
  3598 		return true;
  3385 	}
  3599 	}
  3386 
  3600 
  3387 	/**
  3601 	/**
  3388 	 * Fires before comments are restored for a post from the Trash.
  3602 	 * Fires before comments are restored for a post from the Trash.
  3474 /**
  3688 /**
  3475  * Retrieves the terms for a post.
  3689  * Retrieves the terms for a post.
  3476  *
  3690  *
  3477  * @since 2.8.0
  3691  * @since 2.8.0
  3478  *
  3692  *
  3479  * @param int          $post_id  Optional. The Post ID. Does not default to the ID of the
  3693  * @param int             $post_id  Optional. The Post ID. Does not default to the ID of the
  3480  *                               global $post. Default 0.
  3694  *                                  global $post. Default 0.
  3481  * @param string|array $taxonomy Optional. The taxonomy slug or array of slugs for which
  3695  * @param string|string[] $taxonomy Optional. The taxonomy slug or array of slugs for which
  3482  *                               to retrieve terms. Default 'post_tag'.
  3696  *                                  to retrieve terms. Default 'post_tag'.
  3483  * @param array        $args     {
  3697  * @param array           $args     {
  3484  *     Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments.
  3698  *     Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments.
  3485  *
  3699  *
  3486  *     @type string $fields Term fields to retrieve. Default 'all'.
  3700  *     @type string $fields Term fields to retrieve. Default 'all'.
  3487  * }
  3701  * }
  3488  * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found.
  3702  * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found.
  3539 	$parsed_args = wp_parse_args( $args, $defaults );
  3753 	$parsed_args = wp_parse_args( $args, $defaults );
  3540 
  3754 
  3541 	$results = get_posts( $parsed_args );
  3755 	$results = get_posts( $parsed_args );
  3542 
  3756 
  3543 	// Backward compatibility. Prior to 3.1 expected posts to be returned in array.
  3757 	// Backward compatibility. Prior to 3.1 expected posts to be returned in array.
  3544 	if ( ARRAY_A == $output ) {
  3758 	if ( ARRAY_A === $output ) {
  3545 		foreach ( $results as $key => $result ) {
  3759 		foreach ( $results as $key => $result ) {
  3546 			$results[ $key ] = get_object_vars( $result );
  3760 			$results[ $key ] = get_object_vars( $result );
  3547 		}
  3761 		}
  3548 		return $results ? $results : array();
  3762 		return $results ? $results : array();
  3549 	}
  3763 	}
  3560  * You can set the post date manually, by setting the values for 'post_date'
  3774  * You can set the post date manually, by setting the values for 'post_date'
  3561  * and 'post_date_gmt' keys. You can close the comments or open the comments by
  3775  * and 'post_date_gmt' keys. You can close the comments or open the comments by
  3562  * setting the value for 'comment_status' key.
  3776  * setting the value for 'comment_status' key.
  3563  *
  3777  *
  3564  * @since 1.0.0
  3778  * @since 1.0.0
       
  3779  * @since 2.6.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
  3565  * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
  3780  * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
  3566  * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.
  3781  * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.
       
  3782  * @since 5.6.0 Added the `$fire_after_hooks` parameter.
  3567  *
  3783  *
  3568  * @see sanitize_post()
  3784  * @see sanitize_post()
  3569  * @global wpdb $wpdb WordPress database abstraction object.
  3785  * @global wpdb $wpdb WordPress database abstraction object.
  3570  *
  3786  *
  3571  * @param array $postarr {
  3787  * @param array $postarr {
  3601  *                                         timezone. Default is the current time.
  3817  *                                         timezone. Default is the current time.
  3602  *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
  3818  *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
  3603  *     @type int    $menu_order            The order the post should be displayed in. Default 0.
  3819  *     @type int    $menu_order            The order the post should be displayed in. Default 0.
  3604  *     @type string $post_mime_type        The mime type of the post. Default empty.
  3820  *     @type string $post_mime_type        The mime type of the post. Default empty.
  3605  *     @type string $guid                  Global Unique ID for referencing the post. Default empty.
  3821  *     @type string $guid                  Global Unique ID for referencing the post. Default empty.
  3606  *     @type array  $post_category         Array of category IDs.
  3822  *     @type int    $import_id             The post ID to be used when inserting a new post.
       
  3823  *                                         If specified, must not match any existing post ID. Default 0.
       
  3824  *     @type int[]  $post_category         Array of category IDs.
  3607  *                                         Defaults to value of the 'default_category' option.
  3825  *                                         Defaults to value of the 'default_category' option.
  3608  *     @type array  $tags_input            Array of tag names, slugs, or IDs. Default empty.
  3826  *     @type array  $tags_input            Array of tag names, slugs, or IDs. Default empty.
  3609  *     @type array  $tax_input             Array of taxonomy terms keyed by their taxonomy name. Default empty.
  3827  *     @type array  $tax_input             Array of taxonomy terms keyed by their taxonomy name. Default empty.
  3610  *     @type array  $meta_input            Array of post meta values keyed by their post meta key. Default empty.
  3828  *     @type array  $meta_input            Array of post meta values keyed by their post meta key. Default empty.
  3611  * }
  3829  * }
  3612  * @param bool  $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  3830  * @param bool  $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
       
  3831  * @param bool  $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
  3613  * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
  3832  * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
  3614  */
  3833  */
  3615 function wp_insert_post( $postarr, $wp_error = false ) {
  3834 function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) {
  3616 	global $wpdb;
  3835 	global $wpdb;
  3617 
  3836 
  3618 	// Capture original pre-sanitized array for passing into filters.
  3837 	// Capture original pre-sanitized array for passing into filters.
  3619 	$unsanitized_postarr = $postarr;
  3838 	$unsanitized_postarr = $postarr;
  3620 
  3839 
  3636 		'post_parent'           => 0,
  3855 		'post_parent'           => 0,
  3637 		'menu_order'            => 0,
  3856 		'menu_order'            => 0,
  3638 		'guid'                  => '',
  3857 		'guid'                  => '',
  3639 		'import_id'             => 0,
  3858 		'import_id'             => 0,
  3640 		'context'               => '',
  3859 		'context'               => '',
       
  3860 		'post_date'             => '',
       
  3861 		'post_date_gmt'         => '',
  3641 	);
  3862 	);
  3642 
  3863 
  3643 	$postarr = wp_parse_args( $postarr, $defaults );
  3864 	$postarr = wp_parse_args( $postarr, $defaults );
  3644 
  3865 
  3645 	unset( $postarr['filter'] );
  3866 	unset( $postarr['filter'] );
  3667 
  3888 
  3668 		$guid            = get_post_field( 'guid', $post_ID );
  3889 		$guid            = get_post_field( 'guid', $post_ID );
  3669 		$previous_status = get_post_field( 'post_status', $post_ID );
  3890 		$previous_status = get_post_field( 'post_status', $post_ID );
  3670 	} else {
  3891 	} else {
  3671 		$previous_status = 'new';
  3892 		$previous_status = 'new';
       
  3893 		$post_before     = null;
  3672 	}
  3894 	}
  3673 
  3895 
  3674 	$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
  3896 	$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
  3675 
  3897 
  3676 	$post_title   = $postarr['post_title'];
  3898 	$post_title   = $postarr['post_title'];
  3768 			$post_name = sanitize_title( $post_name );
  3990 			$post_name = sanitize_title( $post_name );
  3769 		}
  3991 		}
  3770 	}
  3992 	}
  3771 
  3993 
  3772 	/*
  3994 	/*
  3773 	 * If the post date is empty (due to having been new or a draft) and status
  3995 	 * Resolve the post date from any provided post date or post date GMT strings;
  3774 	 * is not 'draft' or 'pending', set date to now.
  3996 	 * if none are provided, the date will be set to now.
  3775 	 */
  3997 	 */
  3776 	if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' === $postarr['post_date'] ) {
  3998 	$post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
  3777 		if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
  3999 	if ( ! $post_date ) {
  3778 			$post_date = current_time( 'mysql' );
       
  3779 		} else {
       
  3780 			$post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
       
  3781 		}
       
  3782 	} else {
       
  3783 		$post_date = $postarr['post_date'];
       
  3784 	}
       
  3785 
       
  3786 	// Validate the date.
       
  3787 	$mm         = substr( $post_date, 5, 2 );
       
  3788 	$jj         = substr( $post_date, 8, 2 );
       
  3789 	$aa         = substr( $post_date, 0, 4 );
       
  3790 	$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
       
  3791 	if ( ! $valid_date ) {
       
  3792 		if ( $wp_error ) {
  4000 		if ( $wp_error ) {
  3793 			return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
  4001 			return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
  3794 		} else {
  4002 		} else {
  3795 			return 0;
  4003 			return 0;
  3796 		}
  4004 		}
  4048 		foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
  4256 		foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
  4049 
  4257 
  4050 			if ( ! empty( $tax_object->default_term ) ) {
  4258 			if ( ! empty( $tax_object->default_term ) ) {
  4051 
  4259 
  4052 				// Filter out empty terms.
  4260 				// Filter out empty terms.
  4053 				if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
  4261 				if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
  4054 					$postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
  4262 					$postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
  4055 				}
  4263 				}
  4056 
  4264 
  4057 				// Passed custom taxonomy list overwrites the existing list if not empty.
  4265 				// Passed custom taxonomy list overwrites the existing list if not empty.
  4058 				$terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) );
  4266 				$terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) );
  4126 				$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
  4334 				$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
  4127 			}
  4335 			}
  4128 		}
  4336 		}
  4129 
  4337 
  4130 		if ( $thumbnail_support ) {
  4338 		if ( $thumbnail_support ) {
  4131 			$thumbnail_id = intval( $postarr['_thumbnail_id'] );
  4339 			$thumbnail_id = (int) $postarr['_thumbnail_id'];
  4132 			if ( -1 === $thumbnail_id ) {
  4340 			if ( -1 === $thumbnail_id ) {
  4133 				delete_post_thumbnail( $post_ID );
  4341 				delete_post_thumbnail( $post_ID );
  4134 			} else {
  4342 			} else {
  4135 				set_post_thumbnail( $post_ID, $thumbnail_id );
  4343 				set_post_thumbnail( $post_ID, $thumbnail_id );
  4136 			}
  4344 			}
  4268 	 * @param WP_Post $post    Post object.
  4476 	 * @param WP_Post $post    Post object.
  4269 	 * @param bool    $update  Whether this is an existing post being updated.
  4477 	 * @param bool    $update  Whether this is an existing post being updated.
  4270 	 */
  4478 	 */
  4271 	do_action( 'wp_insert_post', $post_ID, $post, $update );
  4479 	do_action( 'wp_insert_post', $post_ID, $post, $update );
  4272 
  4480 
       
  4481 	if ( $fire_after_hooks ) {
       
  4482 		wp_after_insert_post( $post, $update, $post_before );
       
  4483 	}
       
  4484 
  4273 	return $post_ID;
  4485 	return $post_ID;
  4274 }
  4486 }
  4275 
  4487 
  4276 /**
  4488 /**
  4277  * Update a post with new post data.
  4489  * Update a post with new post data.
  4278  *
  4490  *
  4279  * The date does not have to be set for drafts. You can set the date and it will
  4491  * The date does not have to be set for drafts. You can set the date and it will
  4280  * not be overridden.
  4492  * not be overridden.
  4281  *
  4493  *
  4282  * @since 1.0.0
  4494  * @since 1.0.0
  4283  *
  4495  * @since 3.5.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
  4284  * @param array|object $postarr  Optional. Post data. Arrays are expected to be escaped,
  4496  * @since 5.6.0 Added the `$fire_after_hooks` parameter.
  4285  *                               objects are not. Default array.
  4497  *
  4286  * @param bool         $wp_error Optional. Allow return of WP_Error on failure. Default false.
  4498  * @param array|object $postarr          Optional. Post data. Arrays are expected to be escaped,
       
  4499  *                                       objects are not. See wp_insert_post() for accepted arguments.
       
  4500  *                                       Default array.
       
  4501  * @param bool         $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
       
  4502  * @param bool         $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
  4287  * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
  4503  * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
  4288  */
  4504  */
  4289 function wp_update_post( $postarr = array(), $wp_error = false ) {
  4505 function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) {
  4290 	if ( is_object( $postarr ) ) {
  4506 	if ( is_object( $postarr ) ) {
  4291 		// Non-escaped post was passed.
  4507 		// Non-escaped post was passed.
  4292 		$postarr = get_object_vars( $postarr );
  4508 		$postarr = get_object_vars( $postarr );
  4293 		$postarr = wp_slash( $postarr );
  4509 		$postarr = wp_slash( $postarr );
  4294 	}
  4510 	}
  4349 		if ( $postarr['tags_input'] === $tag_names ) {
  4565 		if ( $postarr['tags_input'] === $tag_names ) {
  4350 			unset( $postarr['tags_input'] );
  4566 			unset( $postarr['tags_input'] );
  4351 		}
  4567 		}
  4352 	}
  4568 	}
  4353 
  4569 
  4354 	return wp_insert_post( $postarr, $wp_error );
  4570 	return wp_insert_post( $postarr, $wp_error, $fire_after_hooks );
  4355 }
  4571 }
  4356 
  4572 
  4357 /**
  4573 /**
  4358  * Publish a post by transitioning the post status.
  4574  * Publish a post by transitioning the post status.
  4359  *
  4575  *
  4365  */
  4581  */
  4366 function wp_publish_post( $post ) {
  4582 function wp_publish_post( $post ) {
  4367 	global $wpdb;
  4583 	global $wpdb;
  4368 
  4584 
  4369 	$post = get_post( $post );
  4585 	$post = get_post( $post );
       
  4586 
  4370 	if ( ! $post ) {
  4587 	if ( ! $post ) {
  4371 		return;
  4588 		return;
  4372 	}
  4589 	}
  4373 
  4590 
  4374 	if ( 'publish' === $post->post_status ) {
  4591 	if ( 'publish' === $post->post_status ) {
  4375 		return;
  4592 		return;
       
  4593 	}
       
  4594 
       
  4595 	$post_before = get_post( $post->ID );
       
  4596 
       
  4597 	// Ensure at least one term is applied for taxonomies with a default term.
       
  4598 	foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
       
  4599 		// Skip taxonomy if no default term is set.
       
  4600 		if (
       
  4601 			'category' !== $taxonomy &&
       
  4602 			empty( $tax_object->default_term )
       
  4603 		) {
       
  4604 			continue;
       
  4605 		}
       
  4606 
       
  4607 		// Do not modify previously set terms.
       
  4608 		if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
       
  4609 			continue;
       
  4610 		}
       
  4611 
       
  4612 		if ( 'category' === $taxonomy ) {
       
  4613 			$default_term_id = (int) get_option( 'default_category', 0 );
       
  4614 		} else {
       
  4615 			$default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
       
  4616 		}
       
  4617 
       
  4618 		if ( ! $default_term_id ) {
       
  4619 			continue;
       
  4620 		}
       
  4621 		wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
  4376 	}
  4622 	}
  4377 
  4623 
  4378 	$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
  4624 	$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
  4379 
  4625 
  4380 	clean_post_cache( $post->ID );
  4626 	clean_post_cache( $post->ID );
  4395 	/** This action is documented in wp-includes/post.php */
  4641 	/** This action is documented in wp-includes/post.php */
  4396 	do_action( 'save_post', $post->ID, $post, true );
  4642 	do_action( 'save_post', $post->ID, $post, true );
  4397 
  4643 
  4398 	/** This action is documented in wp-includes/post.php */
  4644 	/** This action is documented in wp-includes/post.php */
  4399 	do_action( 'wp_insert_post', $post->ID, $post, true );
  4645 	do_action( 'wp_insert_post', $post->ID, $post, true );
       
  4646 
       
  4647 	wp_after_insert_post( $post, true, $post_before );
  4400 }
  4648 }
  4401 
  4649 
  4402 /**
  4650 /**
  4403  * Publish future post and make sure post ID has future post status.
  4651  * Publish future post and make sure post ID has future post status.
  4404  *
  4652  *
  4410  * @param int|WP_Post $post_id Post ID or post object.
  4658  * @param int|WP_Post $post_id Post ID or post object.
  4411  */
  4659  */
  4412 function check_and_publish_future_post( $post_id ) {
  4660 function check_and_publish_future_post( $post_id ) {
  4413 	$post = get_post( $post_id );
  4661 	$post = get_post( $post_id );
  4414 
  4662 
  4415 	if ( empty( $post ) ) {
  4663 	if ( ! $post ) {
  4416 		return;
  4664 		return;
  4417 	}
  4665 	}
  4418 
  4666 
  4419 	if ( 'future' !== $post->post_status ) {
  4667 	if ( 'future' !== $post->post_status ) {
  4420 		return;
  4668 		return;
  4429 		return;
  4677 		return;
  4430 	}
  4678 	}
  4431 
  4679 
  4432 	// wp_publish_post() returns no meaningful value.
  4680 	// wp_publish_post() returns no meaningful value.
  4433 	wp_publish_post( $post_id );
  4681 	wp_publish_post( $post_id );
       
  4682 }
       
  4683 
       
  4684 /**
       
  4685  * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
       
  4686  * If post_date is not provided, this first checks post_date_gmt if provided,
       
  4687  * then falls back to use the current time.
       
  4688  *
       
  4689  * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
       
  4690  * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
       
  4691  *
       
  4692  * @since 5.7.0
       
  4693  *
       
  4694  * @param string $post_date     The date in mysql format.
       
  4695  * @param string $post_date_gmt The GMT date in mysql format.
       
  4696  * @return string|false A valid Gregorian-calendar date string, or false on failure.
       
  4697  */
       
  4698 function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
       
  4699 	// If the date is empty, set the date to now.
       
  4700 	if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
       
  4701 		if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
       
  4702 			$post_date = current_time( 'mysql' );
       
  4703 		} else {
       
  4704 			$post_date = get_date_from_gmt( $post_date_gmt );
       
  4705 		}
       
  4706 	}
       
  4707 
       
  4708 	// Validate the date.
       
  4709 	$month = substr( $post_date, 5, 2 );
       
  4710 	$day   = substr( $post_date, 8, 2 );
       
  4711 	$year  = substr( $post_date, 0, 4 );
       
  4712 
       
  4713 	$valid_date = wp_checkdate( $month, $day, $year, $post_date );
       
  4714 
       
  4715 	if ( ! $valid_date ) {
       
  4716 		return false;
       
  4717 	}
       
  4718 	return $post_date;
  4434 }
  4719 }
  4435 
  4720 
  4436 /**
  4721 /**
  4437  * Computes a unique slug for the post, when given the desired slug and some post details.
  4722  * Computes a unique slug for the post, when given the desired slug and some post details.
  4438  *
  4723  *
  4556 		$post = get_post( $post_ID );
  4841 		$post = get_post( $post_ID );
  4557 
  4842 
  4558 		// Prevent new post slugs that could result in URLs that conflict with date archives.
  4843 		// Prevent new post slugs that could result in URLs that conflict with date archives.
  4559 		$conflicts_with_date_archive = false;
  4844 		$conflicts_with_date_archive = false;
  4560 		if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) {
  4845 		if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) {
  4561 			$slug_num = intval( $slug );
  4846 			$slug_num = (int) $slug;
  4562 
  4847 
  4563 			if ( $slug_num ) {
  4848 			if ( $slug_num ) {
  4564 				$permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
  4849 				$permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
  4565 				$postname_index = array_search( '%postname%', $permastructs, true );
  4850 				$postname_index = array_search( '%postname%', $permastructs, true );
  4566 
  4851 
  4734  *
  5019  *
  4735  * @since 2.1.0
  5020  * @since 2.1.0
  4736  *
  5021  *
  4737  * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
  5022  * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
  4738  *                                   of the global $post. Default 0.
  5023  *                                   of the global $post. Default 0.
  4739  * @param array|int $post_categories Optional. List of category IDs, or the ID of a single category.
  5024  * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category.
  4740  *                                   Default empty array.
  5025  *                                   Default empty array.
  4741  * @param bool      $append          If true, don't delete existing categories, just add on.
  5026  * @param bool      $append          If true, don't delete existing categories, just add on.
  4742  *                                   If false, replace the categories with the new categories.
  5027  *                                   If false, replace the categories with the new categories.
  4743  * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
  5028  * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
  4744  */
  5029  */
  4813 	do_action( 'transition_post_status', $new_status, $old_status, $post );
  5098 	do_action( 'transition_post_status', $new_status, $old_status, $post );
  4814 
  5099 
  4815 	/**
  5100 	/**
  4816 	 * Fires when a post is transitioned from one status to another.
  5101 	 * Fires when a post is transitioned from one status to another.
  4817 	 *
  5102 	 *
  4818 	 * The dynamic portions of the hook name, `$new_status` and `$old status`,
  5103 	 * The dynamic portions of the hook name, `$new_status` and `$old_status`,
  4819 	 * refer to the old and new post statuses, respectively.
  5104 	 * refer to the old and new post statuses, respectively.
  4820 	 *
  5105 	 *
  4821 	 * @since 2.3.0
  5106 	 * @since 2.3.0
  4822 	 *
  5107 	 *
  4823 	 * @param WP_Post $post Post object.
  5108 	 * @param WP_Post $post Post object.
  4827 	/**
  5112 	/**
  4828 	 * Fires when a post is transitioned from one status to another.
  5113 	 * Fires when a post is transitioned from one status to another.
  4829 	 *
  5114 	 *
  4830 	 * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
  5115 	 * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
  4831 	 * refer to the new post status and post type, respectively.
  5116 	 * refer to the new post status and post type, respectively.
       
  5117 	 *
       
  5118 	 * Possible hook names include:
       
  5119 	 *
       
  5120 	 *  - `draft_post`
       
  5121 	 *  - `future_post`
       
  5122 	 *  - `pending_post`
       
  5123 	 *  - `private_post`
       
  5124 	 *  - `publish_post`
       
  5125 	 *  - `trash_post`
       
  5126 	 *  - `draft_page`
       
  5127 	 *  - `future_page`
       
  5128 	 *  - `pending_page`
       
  5129 	 *  - `private_page`
       
  5130 	 *  - `publish_page`
       
  5131 	 *  - `trash_page`
       
  5132 	 *  - `publish_attachment`
       
  5133 	 *  - `trash_attachment`
  4832 	 *
  5134 	 *
  4833 	 * Please note: When this action is hooked using a particular post status (like
  5135 	 * Please note: When this action is hooked using a particular post status (like
  4834 	 * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
  5136 	 * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
  4835 	 * first transitioned to that status from something else, as well as upon
  5137 	 * first transitioned to that status from something else, as well as upon
  4836 	 * subsequent post updates (old and new status are both the same).
  5138 	 * subsequent post updates (old and new status are both the same).
  4844 	 * @param WP_Post $post    Post object.
  5146 	 * @param WP_Post $post    Post object.
  4845 	 */
  5147 	 */
  4846 	do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
  5148 	do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
  4847 }
  5149 }
  4848 
  5150 
       
  5151 /**
       
  5152  * Fires actions after a post, its terms and meta data has been saved.
       
  5153  *
       
  5154  * @since 5.6.0
       
  5155  *
       
  5156  * @param int|WP_Post  $post        The post ID or object that has been saved.
       
  5157  * @param bool         $update      Whether this is an existing post being updated.
       
  5158  * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
       
  5159  *                                  to the update for updated posts.
       
  5160  */
       
  5161 function wp_after_insert_post( $post, $update, $post_before ) {
       
  5162 	$post = get_post( $post );
       
  5163 	if ( ! $post ) {
       
  5164 		return;
       
  5165 	}
       
  5166 
       
  5167 	$post_id = $post->ID;
       
  5168 
       
  5169 	/**
       
  5170 	 * Fires once a post, its terms and meta data has been saved.
       
  5171 	 *
       
  5172 	 * @since 5.6.0
       
  5173 	 *
       
  5174 	 * @param int          $post_id     Post ID.
       
  5175 	 * @param WP_Post      $post        Post object.
       
  5176 	 * @param bool         $update      Whether this is an existing post being updated.
       
  5177 	 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
       
  5178 	 *                                  to the update for updated posts.
       
  5179 	 */
       
  5180 	do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before );
       
  5181 }
       
  5182 
  4849 //
  5183 //
  4850 // Comment, trackback, and pingback functions.
  5184 // Comment, trackback, and pingback functions.
  4851 //
  5185 //
  4852 
  5186 
  4853 /**
  5187 /**
  4865  */
  5199  */
  4866 function add_ping( $post_id, $uri ) {
  5200 function add_ping( $post_id, $uri ) {
  4867 	global $wpdb;
  5201 	global $wpdb;
  4868 
  5202 
  4869 	$post = get_post( $post_id );
  5203 	$post = get_post( $post_id );
       
  5204 
  4870 	if ( ! $post ) {
  5205 	if ( ! $post ) {
  4871 		return false;
  5206 		return false;
  4872 	}
  5207 	}
  4873 
  5208 
  4874 	$pung = trim( $post->pinged );
  5209 	$pung = trim( $post->pinged );
  4937  * @since 1.5.0
  5272  * @since 1.5.0
  4938  *
  5273  *
  4939  * @since 4.7.0 `$post_id` can be a WP_Post object.
  5274  * @since 4.7.0 `$post_id` can be a WP_Post object.
  4940  *
  5275  *
  4941  * @param int|WP_Post $post_id Post ID or object.
  5276  * @param int|WP_Post $post_id Post ID or object.
  4942  * @return bool|string[] Array of URLs already pinged for the given post, false if the post is not found.
  5277  * @return string[]|false Array of URLs already pinged for the given post, false if the post is not found.
  4943  */
  5278  */
  4944 function get_pung( $post_id ) {
  5279 function get_pung( $post_id ) {
  4945 	$post = get_post( $post_id );
  5280 	$post = get_post( $post_id );
       
  5281 
  4946 	if ( ! $post ) {
  5282 	if ( ! $post ) {
  4947 		return false;
  5283 		return false;
  4948 	}
  5284 	}
  4949 
  5285 
  4950 	$pung = trim( $post->pinged );
  5286 	$pung = trim( $post->pinged );
  5227  */
  5563  */
  5228 function get_page_children( $page_id, $pages ) {
  5564 function get_page_children( $page_id, $pages ) {
  5229 	// Build a hash of ID -> children.
  5565 	// Build a hash of ID -> children.
  5230 	$children = array();
  5566 	$children = array();
  5231 	foreach ( (array) $pages as $page ) {
  5567 	foreach ( (array) $pages as $page ) {
  5232 		$children[ intval( $page->post_parent ) ][] = $page;
  5568 		$children[ (int) $page->post_parent ][] = $page;
  5233 	}
  5569 	}
  5234 
  5570 
  5235 	$page_list = array();
  5571 	$page_list = array();
  5236 
  5572 
  5237 	// Start the search by looking at immediate children.
  5573 	// Start the search by looking at immediate children.
  5271 		return array();
  5607 		return array();
  5272 	}
  5608 	}
  5273 
  5609 
  5274 	$children = array();
  5610 	$children = array();
  5275 	foreach ( (array) $pages as $p ) {
  5611 	foreach ( (array) $pages as $p ) {
  5276 		$parent_id                = intval( $p->post_parent );
  5612 		$parent_id                = (int) $p->post_parent;
  5277 		$children[ $parent_id ][] = $p;
  5613 		$children[ $parent_id ][] = $p;
  5278 	}
  5614 	}
  5279 
  5615 
  5280 	$result = array();
  5616 	$result = array();
  5281 	_page_traverse_name( $page_id, $children, $result );
  5617 	_page_traverse_name( $page_id, $children, $result );
  5345 	 */
  5681 	 */
  5346 	return apply_filters( 'get_page_uri', $uri, $page );
  5682 	return apply_filters( 'get_page_uri', $uri, $page );
  5347 }
  5683 }
  5348 
  5684 
  5349 /**
  5685 /**
  5350  * Retrieve a list of pages (or hierarchical post type items).
  5686  * Retrieve an array of pages (or hierarchical post type items).
  5351  *
  5687  *
  5352  * @global wpdb $wpdb WordPress database abstraction object.
  5688  * @global wpdb $wpdb WordPress database abstraction object.
  5353  *
  5689  *
  5354  * @since 1.5.0
  5690  * @since 1.5.0
  5355  *
  5691  *
  5366  *                                      'post_' can be omitted for any values that start with it.
  5702  *                                      'post_' can be omitted for any values that start with it.
  5367  *                                      Default 'post_title'.
  5703  *                                      Default 'post_title'.
  5368  *     @type bool         $hierarchical Whether to return pages hierarchically. If false in conjunction with
  5704  *     @type bool         $hierarchical Whether to return pages hierarchically. If false in conjunction with
  5369  *                                      `$child_of` also being false, both arguments will be disregarded.
  5705  *                                      `$child_of` also being false, both arguments will be disregarded.
  5370  *                                      Default true.
  5706  *                                      Default true.
  5371  *     @type array        $exclude      Array of page IDs to exclude. Default empty array.
  5707  *     @type int[]        $exclude      Array of page IDs to exclude. Default empty array.
  5372  *     @type array        $include      Array of page IDs to include. Cannot be used with `$child_of`,
  5708  *     @type int[]        $include      Array of page IDs to include. Cannot be used with `$child_of`,
  5373  *                                      `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
  5709  *                                      `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
  5374  *                                      Default empty array.
  5710  *                                      Default empty array.
  5375  *     @type string       $meta_key     Only include pages with this meta key. Default empty.
  5711  *     @type string       $meta_key     Only include pages with this meta key. Default empty.
  5376  *     @type string       $meta_value   Only include pages with this meta value. Requires `$meta_key`.
  5712  *     @type string       $meta_value   Only include pages with this meta value. Requires `$meta_key`.
  5377  *                                      Default empty.
  5713  *                                      Default empty.
  5378  *     @type string       $authors      A comma-separated list of author IDs. Default empty.
  5714  *     @type string       $authors      A comma-separated list of author IDs. Default empty.
  5379  *     @type int          $parent       Page ID to return direct children of. Default -1, or no restriction.
  5715  *     @type int          $parent       Page ID to return direct children of. Default -1, or no restriction.
  5380  *     @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude.
  5716  *     @type string|int[] $exclude_tree Comma-separated string or array of page IDs to exclude.
  5381  *                                      Default empty array.
  5717  *                                      Default empty array.
  5382  *     @type int          $number       The number of pages to return. Default 0, or all pages.
  5718  *     @type int          $number       The number of pages to return. Default 0, or all pages.
  5383  *     @type int          $offset       The number of pages to skip before returning. Requires `$number`.
  5719  *     @type int          $offset       The number of pages to skip before returning. Requires `$number`.
  5384  *                                      Default 0.
  5720  *                                      Default 0.
  5385  *     @type string       $post_type    The post type to query. Default 'page'.
  5721  *     @type string       $post_type    The post type to query. Default 'page'.
  5386  *     @type string|array $post_status  A comma-separated list or array of post statuses to include.
  5722  *     @type string|array $post_status  A comma-separated list or array of post statuses to include.
  5387  *                                      Default 'publish'.
  5723  *                                      Default 'publish'.
  5388  * }
  5724  * }
  5389  * @return array|false List of pages matching defaults or `$args`.
  5725  * @return WP_Post[]|int[]|false Array of pages (or hierarchical post type items). Boolean false if the
       
  5726  *                               specified post type is not hierarchical or the specified status is not
       
  5727  *                               supported by the post type.
  5390  */
  5728  */
  5391 function get_pages( $args = array() ) {
  5729 function get_pages( $args = array() ) {
  5392 	global $wpdb;
  5730 	global $wpdb;
  5393 
  5731 
  5394 	$defaults = array(
  5732 	$defaults = array(
  5444 	$last_changed = wp_cache_get_last_changed( 'posts' );
  5782 	$last_changed = wp_cache_get_last_changed( 'posts' );
  5445 
  5783 
  5446 	$cache_key = "get_pages:$key:$last_changed";
  5784 	$cache_key = "get_pages:$key:$last_changed";
  5447 	$cache     = wp_cache_get( $cache_key, 'posts' );
  5785 	$cache     = wp_cache_get( $cache_key, 'posts' );
  5448 	if ( false !== $cache ) {
  5786 	if ( false !== $cache ) {
       
  5787 		_prime_post_caches( $cache, false, false );
       
  5788 
  5449 		// Convert to WP_Post instances.
  5789 		// Convert to WP_Post instances.
  5450 		$pages = array_map( 'get_post', $cache );
  5790 		$pages = array_map( 'get_post', $cache );
  5451 		/** This filter is documented in wp-includes/post.php */
  5791 		/** This filter is documented in wp-includes/post.php */
  5452 		$pages = apply_filters( 'get_pages', $pages, $parsed_args );
  5792 		$pages = apply_filters( 'get_pages', $pages, $parsed_args );
       
  5793 
  5453 		return $pages;
  5794 		return $pages;
  5454 	}
  5795 	}
  5455 
  5796 
  5456 	$inclusions = '';
  5797 	$inclusions = '';
  5457 	if ( ! empty( $parsed_args['include'] ) ) {
  5798 	if ( ! empty( $parsed_args['include'] ) ) {
  5480 		$post_authors = wp_parse_list( $parsed_args['authors'] );
  5821 		$post_authors = wp_parse_list( $parsed_args['authors'] );
  5481 
  5822 
  5482 		if ( ! empty( $post_authors ) ) {
  5823 		if ( ! empty( $post_authors ) ) {
  5483 			foreach ( $post_authors as $post_author ) {
  5824 			foreach ( $post_authors as $post_author ) {
  5484 				// Do we have an author id or an author login?
  5825 				// Do we have an author id or an author login?
  5485 				if ( 0 == intval( $post_author ) ) {
  5826 				if ( 0 == (int) $post_author ) {
  5486 					$post_author = get_user_by( 'login', $post_author );
  5827 					$post_author = get_user_by( 'login', $post_author );
  5487 					if ( empty( $post_author ) ) {
  5828 					if ( empty( $post_author ) ) {
  5488 						continue;
  5829 						continue;
  5489 					}
  5830 					}
  5490 					if ( empty( $post_author->ID ) ) {
  5831 					if ( empty( $post_author->ID ) ) {
  5608 	if ( empty( $pages ) ) {
  5949 	if ( empty( $pages ) ) {
  5609 		wp_cache_set( $cache_key, array(), 'posts' );
  5950 		wp_cache_set( $cache_key, array(), 'posts' );
  5610 
  5951 
  5611 		/** This filter is documented in wp-includes/post.php */
  5952 		/** This filter is documented in wp-includes/post.php */
  5612 		$pages = apply_filters( 'get_pages', array(), $parsed_args );
  5953 		$pages = apply_filters( 'get_pages', array(), $parsed_args );
       
  5954 
  5613 		return $pages;
  5955 		return $pages;
  5614 	}
  5956 	}
  5615 
  5957 
  5616 	// Sanitize before caching so it'll only get done once.
  5958 	// Sanitize before caching so it'll only get done once.
  5617 	$num_pages = count( $pages );
  5959 	$num_pages = count( $pages );
  5712  * comments are allowed. You can close them manually or keep them open by
  6054  * comments are allowed. You can close them manually or keep them open by
  5713  * setting the value for the 'comment_status' key.
  6055  * setting the value for the 'comment_status' key.
  5714  *
  6056  *
  5715  * @since 2.0.0
  6057  * @since 2.0.0
  5716  * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
  6058  * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
       
  6059  * @since 5.6.0 Added the `$fire_after_hooks` parameter.
  5717  *
  6060  *
  5718  * @see wp_insert_post()
  6061  * @see wp_insert_post()
  5719  *
  6062  *
  5720  * @param string|array $args     Arguments for inserting an attachment.
  6063  * @param string|array $args             Arguments for inserting an attachment.
  5721  * @param string       $file     Optional. Filename.
  6064  * @param string|false $file             Optional. Filename.
  5722  * @param int          $parent   Optional. Parent post ID.
  6065  * @param int          $parent           Optional. Parent post ID.
  5723  * @param bool         $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  6066  * @param bool         $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
       
  6067  * @param bool         $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
  5724  * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
  6068  * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
  5725  */
  6069  */
  5726 function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) {
  6070 function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) {
  5727 	$defaults = array(
  6071 	$defaults = array(
  5728 		'file'        => $file,
  6072 		'file'        => $file,
  5729 		'post_parent' => 0,
  6073 		'post_parent' => 0,
  5730 	);
  6074 	);
  5731 
  6075 
  5735 		$data['post_parent'] = $parent;
  6079 		$data['post_parent'] = $parent;
  5736 	}
  6080 	}
  5737 
  6081 
  5738 	$data['post_type'] = 'attachment';
  6082 	$data['post_type'] = 'attachment';
  5739 
  6083 
  5740 	return wp_insert_post( $data, $wp_error );
  6084 	return wp_insert_post( $data, $wp_error, $fire_after_hooks );
  5741 }
  6085 }
  5742 
  6086 
  5743 /**
  6087 /**
  5744  * Trash or delete an attachment.
  6088  * Trash or delete an attachment.
  5745  *
  6089  *
  5798 	$meta         = wp_get_attachment_metadata( $post_id );
  6142 	$meta         = wp_get_attachment_metadata( $post_id );
  5799 	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
  6143 	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
  5800 	$file         = get_attached_file( $post_id );
  6144 	$file         = get_attached_file( $post_id );
  5801 
  6145 
  5802 	if ( is_multisite() ) {
  6146 	if ( is_multisite() ) {
  5803 		delete_transient( 'dirsize_cache' );
  6147 		clean_dirsize_cache( $file );
  5804 	}
  6148 	}
  5805 
  6149 
  5806 	/**
  6150 	/**
  5807 	 * Fires before an attachment is deleted, at the start of wp_delete_attachment().
  6151 	 * Fires before an attachment is deleted, at the start of wp_delete_attachment().
  5808 	 *
  6152 	 *
  5852 
  6196 
  5853 /**
  6197 /**
  5854  * Deletes all files that belong to the given attachment.
  6198  * Deletes all files that belong to the given attachment.
  5855  *
  6199  *
  5856  * @since 4.9.7
  6200  * @since 4.9.7
       
  6201  *
       
  6202  * @global wpdb $wpdb WordPress database abstraction object.
  5857  *
  6203  *
  5858  * @param int    $post_id      Attachment ID.
  6204  * @param int    $post_id      Attachment ID.
  5859  * @param array  $meta         The attachment's meta data.
  6205  * @param array  $meta         The attachment's meta data.
  5860  * @param array  $backup_sizes The meta data for the attachment's backup images.
  6206  * @param array  $backup_sizes The meta data for the attachment's backup images.
  5861  * @param string $file         Absolute path to the attachment's file.
  6207  * @param string $file         Absolute path to the attachment's file.
  5958  * }
  6304  * }
  5959  */
  6305  */
  5960 function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
  6306 function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
  5961 	$attachment_id = (int) $attachment_id;
  6307 	$attachment_id = (int) $attachment_id;
  5962 
  6308 
  5963 	$post = get_post( $attachment_id );
  6309 	if ( ! $attachment_id ) {
  5964 	if ( ! $post ) {
  6310 		$post = get_post();
       
  6311 
       
  6312 		if ( ! $post ) {
       
  6313 			return false;
       
  6314 		}
       
  6315 
       
  6316 		$attachment_id = $post->ID;
       
  6317 	}
       
  6318 
       
  6319 	$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
       
  6320 
       
  6321 	if ( ! $data ) {
  5965 		return false;
  6322 		return false;
  5966 	}
  6323 	}
  5967 
       
  5968 	$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
       
  5969 
  6324 
  5970 	if ( $unfiltered ) {
  6325 	if ( $unfiltered ) {
  5971 		return $data;
  6326 		return $data;
  5972 	}
  6327 	}
  5973 
  6328 
  5974 	/**
  6329 	/**
  5975 	 * Filters the attachment meta data.
  6330 	 * Filters the attachment meta data.
  5976 	 *
  6331 	 *
  5977 	 * @since 2.1.0
  6332 	 * @since 2.1.0
  5978 	 *
  6333 	 *
  5979 	 * @param array|bool $data          Array of meta data for the given attachment, or false
  6334 	 * @param array $data          Array of meta data for the given attachment.
  5980 	 *                                  if the object does not exist.
  6335 	 * @param int   $attachment_id Attachment post ID.
  5981 	 * @param int        $attachment_id Attachment post ID.
       
  5982 	 */
  6336 	 */
  5983 	return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
  6337 	return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
  5984 }
  6338 }
  5985 
  6339 
  5986 /**
  6340 /**
  5987  * Updates metadata for an attachment.
  6341  * Updates metadata for an attachment.
  5988  *
  6342  *
  5989  * @since 2.1.0
  6343  * @since 2.1.0
  5990  *
  6344  *
  5991  * @param int   $attachment_id Attachment post ID.
  6345  * @param int   $attachment_id Attachment post ID.
  5992  * @param array $data          Attachment meta data.
  6346  * @param array $data          Attachment meta data.
  5993  * @return int|bool False if $post is invalid.
  6347  * @return int|false False if $post is invalid.
  5994  */
  6348  */
  5995 function wp_update_attachment_metadata( $attachment_id, $data ) {
  6349 function wp_update_attachment_metadata( $attachment_id, $data ) {
  5996 	$attachment_id = (int) $attachment_id;
  6350 	$attachment_id = (int) $attachment_id;
  5997 
  6351 
  5998 	$post = get_post( $attachment_id );
  6352 	$post = get_post( $attachment_id );
       
  6353 
  5999 	if ( ! $post ) {
  6354 	if ( ! $post ) {
  6000 		return false;
  6355 		return false;
  6001 	}
  6356 	}
  6002 
  6357 
  6003 	/**
  6358 	/**
  6025  *
  6380  *
  6026  * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
  6381  * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
  6027  * @return string|false Attachment URL, otherwise false.
  6382  * @return string|false Attachment URL, otherwise false.
  6028  */
  6383  */
  6029 function wp_get_attachment_url( $attachment_id = 0 ) {
  6384 function wp_get_attachment_url( $attachment_id = 0 ) {
       
  6385 	global $pagenow;
       
  6386 
  6030 	$attachment_id = (int) $attachment_id;
  6387 	$attachment_id = (int) $attachment_id;
  6031 
  6388 
  6032 	$post = get_post( $attachment_id );
  6389 	$post = get_post( $attachment_id );
       
  6390 
  6033 	if ( ! $post ) {
  6391 	if ( ! $post ) {
  6034 		return false;
  6392 		return false;
  6035 	}
  6393 	}
  6036 
  6394 
  6037 	if ( 'attachment' !== $post->post_type ) {
  6395 	if ( 'attachment' !== $post->post_type ) {
  6061 
  6419 
  6062 	/*
  6420 	/*
  6063 	 * If any of the above options failed, Fallback on the GUID as used pre-2.7,
  6421 	 * If any of the above options failed, Fallback on the GUID as used pre-2.7,
  6064 	 * not recommended to rely upon this.
  6422 	 * not recommended to rely upon this.
  6065 	 */
  6423 	 */
  6066 	if ( empty( $url ) ) {
  6424 	if ( ! $url ) {
  6067 		$url = get_the_guid( $post->ID );
  6425 		$url = get_the_guid( $post->ID );
  6068 	}
  6426 	}
  6069 
  6427 
  6070 	// On SSL front end, URLs should be HTTPS.
  6428 	// On SSL front end, URLs should be HTTPS.
  6071 	if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) {
  6429 	if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
  6072 		$url = set_url_scheme( $url );
  6430 		$url = set_url_scheme( $url );
  6073 	}
  6431 	}
  6074 
  6432 
  6075 	/**
  6433 	/**
  6076 	 * Filters the attachment URL.
  6434 	 * Filters the attachment URL.
  6080 	 * @param string $url           URL for the given attachment.
  6438 	 * @param string $url           URL for the given attachment.
  6081 	 * @param int    $attachment_id Attachment post ID.
  6439 	 * @param int    $attachment_id Attachment post ID.
  6082 	 */
  6440 	 */
  6083 	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
  6441 	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
  6084 
  6442 
  6085 	if ( empty( $url ) ) {
  6443 	if ( ! $url ) {
  6086 		return false;
  6444 		return false;
  6087 	}
  6445 	}
  6088 
  6446 
  6089 	return $url;
  6447 	return $url;
  6090 }
  6448 }
  6093  * Retrieves the caption for an attachment.
  6451  * Retrieves the caption for an attachment.
  6094  *
  6452  *
  6095  * @since 4.6.0
  6453  * @since 4.6.0
  6096  *
  6454  *
  6097  * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
  6455  * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
  6098  * @return string|false False on failure. Attachment caption on success.
  6456  * @return string|false Attachment caption on success, false on failure.
  6099  */
  6457  */
  6100 function wp_get_attachment_caption( $post_id = 0 ) {
  6458 function wp_get_attachment_caption( $post_id = 0 ) {
  6101 	$post_id = (int) $post_id;
  6459 	$post_id = (int) $post_id;
  6102 	$post    = get_post( $post_id );
  6460 	$post    = get_post( $post_id );
       
  6461 
  6103 	if ( ! $post ) {
  6462 	if ( ! $post ) {
  6104 		return false;
  6463 		return false;
  6105 	}
  6464 	}
  6106 
  6465 
  6107 	if ( 'attachment' !== $post->post_type ) {
  6466 	if ( 'attachment' !== $post->post_type ) {
  6125  * Retrieve thumbnail for an attachment.
  6484  * Retrieve thumbnail for an attachment.
  6126  *
  6485  *
  6127  * @since 2.1.0
  6486  * @since 2.1.0
  6128  *
  6487  *
  6129  * @param int $post_id Optional. Attachment ID. Default 0.
  6488  * @param int $post_id Optional. Attachment ID. Default 0.
  6130  * @return string|false False on failure. Thumbnail file path on success.
  6489  * @return string|false Thumbnail file path on success, false on failure.
  6131  */
  6490  */
  6132 function wp_get_attachment_thumb_file( $post_id = 0 ) {
  6491 function wp_get_attachment_thumb_file( $post_id = 0 ) {
  6133 	$post_id = (int) $post_id;
  6492 	$post_id = (int) $post_id;
  6134 	$post    = get_post( $post_id );
  6493 	$post    = get_post( $post_id );
       
  6494 
  6135 	if ( ! $post ) {
  6495 	if ( ! $post ) {
  6136 		return false;
  6496 		return false;
  6137 	}
  6497 	}
  6138 
  6498 
  6139 	$imagedata = wp_get_attachment_metadata( $post->ID );
  6499 	$imagedata = wp_get_attachment_metadata( $post->ID );
  6164  * Retrieve URL for an attachment thumbnail.
  6524  * Retrieve URL for an attachment thumbnail.
  6165  *
  6525  *
  6166  * @since 2.1.0
  6526  * @since 2.1.0
  6167  *
  6527  *
  6168  * @param int $post_id Optional. Attachment ID. Default 0.
  6528  * @param int $post_id Optional. Attachment ID. Default 0.
  6169  * @return string|false False on failure. Thumbnail URL on success.
  6529  * @return string|false Thumbnail URL on success, false on failure.
  6170  */
  6530  */
  6171 function wp_get_attachment_thumb_url( $post_id = 0 ) {
  6531 function wp_get_attachment_thumb_url( $post_id = 0 ) {
  6172 	$post_id = (int) $post_id;
  6532 	$post_id = (int) $post_id;
  6173 	$post    = get_post( $post_id );
  6533 	$post    = get_post( $post_id );
       
  6534 
  6174 	if ( ! $post ) {
  6535 	if ( ! $post ) {
  6175 		return false;
  6536 		return false;
  6176 	}
  6537 	}
  6177 
  6538 
  6178 	$url = wp_get_attachment_url( $post->ID );
  6539 	$url = wp_get_attachment_url( $post->ID );
  6212  * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
  6573  * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
  6213  * @return bool True if one of the accepted types, false otherwise.
  6574  * @return bool True if one of the accepted types, false otherwise.
  6214  */
  6575  */
  6215 function wp_attachment_is( $type, $post = null ) {
  6576 function wp_attachment_is( $type, $post = null ) {
  6216 	$post = get_post( $post );
  6577 	$post = get_post( $post );
       
  6578 
  6217 	if ( ! $post ) {
  6579 	if ( ! $post ) {
  6218 		return false;
  6580 		return false;
  6219 	}
  6581 	}
  6220 
  6582 
  6221 	$file = get_attached_file( $post->ID );
  6583 	$file = get_attached_file( $post->ID );
       
  6584 
  6222 	if ( ! $file ) {
  6585 	if ( ! $file ) {
  6223 		return false;
  6586 		return false;
  6224 	}
  6587 	}
  6225 
  6588 
  6226 	if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
  6589 	if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
  6227 		return true;
  6590 		return true;
  6228 	}
  6591 	}
  6229 
  6592 
  6230 	$check = wp_check_filetype( $file );
  6593 	$check = wp_check_filetype( $file );
       
  6594 
  6231 	if ( empty( $check['ext'] ) ) {
  6595 	if ( empty( $check['ext'] ) ) {
  6232 		return false;
  6596 		return false;
  6233 	}
  6597 	}
  6234 
  6598 
  6235 	$ext = $check['ext'];
  6599 	$ext = $check['ext'];
  6238 		return $type === $ext;
  6602 		return $type === $ext;
  6239 	}
  6603 	}
  6240 
  6604 
  6241 	switch ( $type ) {
  6605 	switch ( $type ) {
  6242 		case 'image':
  6606 		case 'image':
  6243 			$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
  6607 			$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
  6244 			return in_array( $ext, $image_exts, true );
  6608 			return in_array( $ext, $image_exts, true );
  6245 
  6609 
  6246 		case 'audio':
  6610 		case 'audio':
  6247 			return in_array( $ext, wp_get_audio_extensions(), true );
  6611 			return in_array( $ext, wp_get_audio_extensions(), true );
  6248 
  6612 
  6796 	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  7160 	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  6797 		return;
  7161 		return;
  6798 	}
  7162 	}
  6799 
  7163 
  6800 	$post = get_post( $post );
  7164 	$post = get_post( $post );
  6801 	if ( empty( $post ) ) {
  7165 
       
  7166 	if ( ! $post ) {
  6802 		return;
  7167 		return;
  6803 	}
  7168 	}
  6804 
  7169 
  6805 	wp_cache_delete( $post->ID, 'posts' );
  7170 	wp_cache_delete( $post->ID, 'posts' );
  6806 	wp_cache_delete( $post->ID, 'post_meta' );
  7171 	wp_cache_delete( $post->ID, 'post_meta' );
  7093 	if ( ! $post_parent ) {
  7458 	if ( ! $post_parent ) {
  7094 		return 0;
  7459 		return 0;
  7095 	}
  7460 	}
  7096 
  7461 
  7097 	// New post can't cause a loop.
  7462 	// New post can't cause a loop.
  7098 	if ( empty( $post_ID ) ) {
  7463 	if ( ! $post_ID ) {
  7099 		return $post_parent;
  7464 		return $post_parent;
  7100 	}
  7465 	}
  7101 
  7466 
  7102 	// Can't be its own parent.
  7467 	// Can't be its own parent.
  7103 	if ( $post_parent == $post_ID ) {
  7468 	if ( $post_parent == $post_ID ) {
  7206 		foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
  7571 		foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
  7207 			// Term cache should already be primed by `update_post_term_cache()`.
  7572 			// Term cache should already be primed by `update_post_term_cache()`.
  7208 			$terms = get_object_term_cache( $post->ID, $taxonomy );
  7573 			$terms = get_object_term_cache( $post->ID, $taxonomy );
  7209 			if ( false !== $terms ) {
  7574 			if ( false !== $terms ) {
  7210 				foreach ( $terms as $term ) {
  7575 				foreach ( $terms as $term ) {
  7211 					if ( ! isset( $term_ids[ $term->term_id ] ) ) {
  7576 					if ( ! in_array( $term->term_id, $term_ids, true ) ) {
  7212 						$term_ids[] = $term->term_id;
  7577 						$term_ids[] = $term->term_id;
  7213 					}
  7578 					}
  7214 				}
  7579 				}
  7215 			}
  7580 			}
  7216 		}
  7581 		}
  7242 		wp_update_term_count( $tt_ids, $taxonomy );
  7607 		wp_update_term_count( $tt_ids, $taxonomy );
  7243 	}
  7608 	}
  7244 }
  7609 }
  7245 
  7610 
  7246 /**
  7611 /**
  7247  * Adds any posts from the given IDs to the cache that do not already exist in cache
  7612  * Adds any posts from the given IDs to the cache that do not already exist in cache.
  7248  *
  7613  *
  7249  * @since 3.4.0
  7614  * @since 3.4.0
  7250  * @access private
  7615  * @access private
  7251  *
  7616  *
  7252  * @see update_post_caches()
  7617  * @see update_post_caches()
  7260 function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
  7625 function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
  7261 	global $wpdb;
  7626 	global $wpdb;
  7262 
  7627 
  7263 	$non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
  7628 	$non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
  7264 	if ( ! empty( $non_cached_ids ) ) {
  7629 	if ( ! empty( $non_cached_ids ) ) {
  7265 		$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ',', $non_cached_ids ) ) );
  7630 		$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
  7266 
  7631 
  7267 		update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
  7632 		update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
  7268 	}
  7633 	}
  7269 }
  7634 }
  7270 
  7635 
  7278  *
  7643  *
  7279  * @since 4.5.0
  7644  * @since 4.5.0
  7280  * @access private
  7645  * @access private
  7281  *
  7646  *
  7282  * @param string $post_name Slug.
  7647  * @param string $post_name Slug.
  7283  * @param string $post_ID   Optional. Post ID that should be ignored. Default 0.
  7648  * @param int    $post_ID   Optional. Post ID that should be ignored. Default 0.
  7284  */
  7649  */
  7285 function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
  7650 function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
  7286 	$trashed_posts_with_desired_slug = get_posts(
  7651 	$trashed_posts_with_desired_slug = get_posts(
  7287 		array(
  7652 		array(
  7288 			'name'         => $post_name,
  7653 			'name'         => $post_name,
  7328 	clean_post_cache( $post->ID );
  7693 	clean_post_cache( $post->ID );
  7329 	return $post_name;
  7694 	return $post_name;
  7330 }
  7695 }
  7331 
  7696 
  7332 /**
  7697 /**
  7333  * Filter the SQL clauses of an attachment query to include filenames.
  7698  * Filters the SQL clauses of an attachment query to include filenames.
  7334  *
  7699  *
  7335  * @since 4.7.0
  7700  * @since 4.7.0
  7336  * @access private
  7701  * @access private
  7337  *
  7702  *
  7338  * @global wpdb $wpdb WordPress database abstraction object.
  7703  * @global wpdb $wpdb WordPress database abstraction object.
  7442 		return false;
  7807 		return false;
  7443 	}
  7808 	}
  7444 
  7809 
  7445 	$image_url = wp_get_attachment_url( $attachment_id );
  7810 	$image_url = wp_get_attachment_url( $attachment_id );
  7446 
  7811 
  7447 	if ( empty( $image_url ) ) {
  7812 	if ( ! $image_url ) {
  7448 		return false;
  7813 		return false;
  7449 	}
  7814 	}
  7450 
  7815 
  7451 	$image_meta = wp_get_attachment_metadata( $attachment_id );
  7816 	$image_meta = wp_get_attachment_metadata( $attachment_id );
  7452 
  7817 
  7464 	 * @param string $original_image_url URL to original image.
  7829 	 * @param string $original_image_url URL to original image.
  7465 	 * @param int    $attachment_id      Attachment ID.
  7830 	 * @param int    $attachment_id      Attachment ID.
  7466 	 */
  7831 	 */
  7467 	return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id );
  7832 	return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id );
  7468 }
  7833 }
       
  7834 
       
  7835 /**
       
  7836  * Filter callback which sets the status of an untrashed post to its previous status.
       
  7837  *
       
  7838  * This can be used as a callback on the `wp_untrash_post_status` filter.
       
  7839  *
       
  7840  * @since 5.6.0
       
  7841  *
       
  7842  * @param string $new_status      The new status of the post being restored.
       
  7843  * @param int    $post_id         The ID of the post being restored.
       
  7844  * @param string $previous_status The status of the post at the point where it was trashed.
       
  7845  * @return string The new status of the post.
       
  7846  */
       
  7847 function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) {
       
  7848 	return $previous_status;
       
  7849 }