[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> post.php (source)

   1  <?php
   2  /**
   3   * Core Post API
   4   *
   5   * @package WordPress
   6   * @subpackage Post
   7   */
   8  
   9  //
  10  // Post Type registration.
  11  //
  12  
  13  /**
  14   * Creates the initial post types when 'init' action is fired.
  15   *
  16   * See {@see 'init'}.
  17   *
  18   * @since 2.9.0
  19   */
  20  function create_initial_post_types() {
  21      WP_Post_Type::reset_default_labels();
  22  
  23      register_post_type(
  24          'post',
  25          array(
  26              'labels'                => array(
  27                  'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
  28              ),
  29              'public'                => true,
  30              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
  31              '_edit_link'            => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  32              'capability_type'       => 'post',
  33              'map_meta_cap'          => true,
  34              'menu_position'         => 5,
  35              'menu_icon'             => 'dashicons-admin-post',
  36              'hierarchical'          => false,
  37              'rewrite'               => false,
  38              'query_var'             => false,
  39              'delete_with_user'      => true,
  40              'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
  41              'show_in_rest'          => true,
  42              'rest_base'             => 'posts',
  43              'rest_controller_class' => 'WP_REST_Posts_Controller',
  44          )
  45      );
  46  
  47      register_post_type(
  48          'page',
  49          array(
  50              'labels'                => array(
  51                  'name_admin_bar' => _x( 'Page', 'add new from admin bar' ),
  52              ),
  53              'public'                => true,
  54              'publicly_queryable'    => false,
  55              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
  56              '_edit_link'            => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  57              'capability_type'       => 'page',
  58              'map_meta_cap'          => true,
  59              'menu_position'         => 20,
  60              'menu_icon'             => 'dashicons-admin-page',
  61              'hierarchical'          => true,
  62              'rewrite'               => false,
  63              'query_var'             => false,
  64              'delete_with_user'      => true,
  65              'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  66              'show_in_rest'          => true,
  67              'rest_base'             => 'pages',
  68              'rest_controller_class' => 'WP_REST_Posts_Controller',
  69          )
  70      );
  71  
  72      register_post_type(
  73          'attachment',
  74          array(
  75              'labels'                => array(
  76                  'name'           => _x( 'Media', 'post type general name' ),
  77                  'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
  78                  'add_new'        => __( 'Add New Media File' ),
  79                  'edit_item'      => __( 'Edit Media' ),
  80                  'view_item'      => ( '1' === get_option( 'wp_attachment_pages_enabled' ) ) ? __( 'View Attachment Page' ) : __( 'View Media File' ),
  81                  'attributes'     => __( 'Attachment Attributes' ),
  82              ),
  83              'public'                => true,
  84              'show_ui'               => true,
  85              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
  86              '_edit_link'            => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  87              'capability_type'       => 'post',
  88              'capabilities'          => array(
  89                  'create_posts' => 'upload_files',
  90              ),
  91              'map_meta_cap'          => true,
  92              'menu_icon'             => 'dashicons-admin-media',
  93              'hierarchical'          => false,
  94              'rewrite'               => false,
  95              'query_var'             => false,
  96              'show_in_nav_menus'     => false,
  97              'delete_with_user'      => true,
  98              'supports'              => array( 'title', 'author', 'comments' ),
  99              'show_in_rest'          => true,
 100              'rest_base'             => 'media',
 101              'rest_controller_class' => 'WP_REST_Attachments_Controller',
 102          )
 103      );
 104      add_post_type_support( 'attachment:audio', 'thumbnail' );
 105      add_post_type_support( 'attachment:video', 'thumbnail' );
 106  
 107      register_post_type(
 108          'revision',
 109          array(
 110              'labels'           => array(
 111                  'name'          => __( 'Revisions' ),
 112                  'singular_name' => __( 'Revision' ),
 113              ),
 114              'public'           => false,
 115              '_builtin'         => true, /* internal use only. don't use this when registering your own post type. */
 116              '_edit_link'       => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
 117              'capability_type'  => 'post',
 118              'map_meta_cap'     => true,
 119              'hierarchical'     => false,
 120              'rewrite'          => false,
 121              'query_var'        => false,
 122              'can_export'       => false,
 123              'delete_with_user' => true,
 124              'supports'         => array( 'author' ),
 125          )
 126      );
 127  
 128      register_post_type(
 129          'nav_menu_item',
 130          array(
 131              'labels'                => array(
 132                  'name'          => __( 'Navigation Menu Items' ),
 133                  'singular_name' => __( 'Navigation Menu Item' ),
 134              ),
 135              'public'                => false,
 136              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
 137              'hierarchical'          => false,
 138              'rewrite'               => false,
 139              'delete_with_user'      => false,
 140              'query_var'             => false,
 141              'map_meta_cap'          => true,
 142              'capability_type'       => array( 'edit_theme_options', 'edit_theme_options' ),
 143              'capabilities'          => array(
 144                  // Meta Capabilities.
 145                  'edit_post'              => 'edit_post',
 146                  'read_post'              => 'read_post',
 147                  'delete_post'            => 'delete_post',
 148                  // Primitive Capabilities.
 149                  'edit_posts'             => 'edit_theme_options',
 150                  'edit_others_posts'      => 'edit_theme_options',
 151                  'delete_posts'           => 'edit_theme_options',
 152                  'publish_posts'          => 'edit_theme_options',
 153                  'read_private_posts'     => 'edit_theme_options',
 154                  'read'                   => 'read',
 155                  'delete_private_posts'   => 'edit_theme_options',
 156                  'delete_published_posts' => 'edit_theme_options',
 157                  'delete_others_posts'    => 'edit_theme_options',
 158                  'edit_private_posts'     => 'edit_theme_options',
 159                  'edit_published_posts'   => 'edit_theme_options',
 160              ),
 161              'show_in_rest'          => true,
 162              'rest_base'             => 'menu-items',
 163              'rest_controller_class' => 'WP_REST_Menu_Items_Controller',
 164          )
 165      );
 166  
 167      register_post_type(
 168          'custom_css',
 169          array(
 170              'labels'           => array(
 171                  'name'          => __( 'Custom CSS' ),
 172                  'singular_name' => __( 'Custom CSS' ),
 173              ),
 174              'public'           => false,
 175              'hierarchical'     => false,
 176              'rewrite'          => false,
 177              'query_var'        => false,
 178              'delete_with_user' => false,
 179              'can_export'       => true,
 180              '_builtin'         => true, /* internal use only. don't use this when registering your own post type. */
 181              'supports'         => array( 'title', 'revisions' ),
 182              'capabilities'     => array(
 183                  'delete_posts'           => 'edit_theme_options',
 184                  'delete_post'            => 'edit_theme_options',
 185                  'delete_published_posts' => 'edit_theme_options',
 186                  'delete_private_posts'   => 'edit_theme_options',
 187                  'delete_others_posts'    => 'edit_theme_options',
 188                  'edit_post'              => 'edit_css',
 189                  'edit_posts'             => 'edit_css',
 190                  'edit_others_posts'      => 'edit_css',
 191                  'edit_published_posts'   => 'edit_css',
 192                  'read_post'              => 'read',
 193                  'read_private_posts'     => 'read',
 194                  'publish_posts'          => 'edit_theme_options',
 195              ),
 196          )
 197      );
 198  
 199      register_post_type(
 200          'customize_changeset',
 201          array(
 202              'labels'           => array(
 203                  'name'               => _x( 'Changesets', 'post type general name' ),
 204                  'singular_name'      => _x( 'Changeset', 'post type singular name' ),
 205                  'add_new'            => __( 'Add New Changeset' ),
 206                  'add_new_item'       => __( 'Add New Changeset' ),
 207                  'new_item'           => __( 'New Changeset' ),
 208                  'edit_item'          => __( 'Edit Changeset' ),
 209                  'view_item'          => __( 'View Changeset' ),
 210                  'all_items'          => __( 'All Changesets' ),
 211                  'search_items'       => __( 'Search Changesets' ),
 212                  'not_found'          => __( 'No changesets found.' ),
 213                  'not_found_in_trash' => __( 'No changesets found in Trash.' ),
 214              ),
 215              'public'           => false,
 216              '_builtin'         => true, /* internal use only. don't use this when registering your own post type. */
 217              'map_meta_cap'     => true,
 218              'hierarchical'     => false,
 219              'rewrite'          => false,
 220              'query_var'        => false,
 221              'can_export'       => false,
 222              'delete_with_user' => false,
 223              'supports'         => array( 'title', 'author' ),
 224              'capability_type'  => 'customize_changeset',
 225              'capabilities'     => array(
 226                  'create_posts'           => 'customize',
 227                  'delete_others_posts'    => 'customize',
 228                  'delete_post'            => 'customize',
 229                  'delete_posts'           => 'customize',
 230                  'delete_private_posts'   => 'customize',
 231                  'delete_published_posts' => 'customize',
 232                  'edit_others_posts'      => 'customize',
 233                  'edit_post'              => 'customize',
 234                  'edit_posts'             => 'customize',
 235                  'edit_private_posts'     => 'customize',
 236                  'edit_published_posts'   => 'do_not_allow',
 237                  'publish_posts'          => 'customize',
 238                  'read'                   => 'read',
 239                  'read_post'              => 'customize',
 240                  'read_private_posts'     => 'customize',
 241              ),
 242          )
 243      );
 244  
 245      register_post_type(
 246          'oembed_cache',
 247          array(
 248              'labels'           => array(
 249                  'name'          => __( 'oEmbed Responses' ),
 250                  'singular_name' => __( 'oEmbed Response' ),
 251              ),
 252              'public'           => false,
 253              'hierarchical'     => false,
 254              'rewrite'          => false,
 255              'query_var'        => false,
 256              'delete_with_user' => false,
 257              'can_export'       => false,
 258              '_builtin'         => true, /* internal use only. don't use this when registering your own post type. */
 259              'supports'         => array(),
 260          )
 261      );
 262  
 263      register_post_type(
 264          'user_request',
 265          array(
 266              'labels'           => array(
 267                  'name'          => __( 'User Requests' ),
 268                  'singular_name' => __( 'User Request' ),
 269              ),
 270              'public'           => false,
 271              '_builtin'         => true, /* internal use only. don't use this when registering your own post type. */
 272              'hierarchical'     => false,
 273              'rewrite'          => false,
 274              'query_var'        => false,
 275              'can_export'       => false,
 276              'delete_with_user' => false,
 277              'supports'         => array(),
 278          )
 279      );
 280  
 281      register_post_type(
 282          'wp_block',
 283          array(
 284              'labels'                => array(
 285                  'name'                     => _x( 'Patterns', 'post type general name' ),
 286                  'singular_name'            => _x( 'Pattern', 'post type singular name' ),
 287                  'add_new'                  => __( 'Add New Pattern' ),
 288                  'add_new_item'             => __( 'Add New Pattern' ),
 289                  'new_item'                 => __( 'New Pattern' ),
 290                  'edit_item'                => __( 'Edit Block Pattern' ),
 291                  'view_item'                => __( 'View Pattern' ),
 292                  'view_items'               => __( 'View Patterns' ),
 293                  'all_items'                => __( 'All Patterns' ),
 294                  'search_items'             => __( 'Search Patterns' ),
 295                  'not_found'                => __( 'No patterns found.' ),
 296                  'not_found_in_trash'       => __( 'No patterns found in Trash.' ),
 297                  'filter_items_list'        => __( 'Filter patterns list' ),
 298                  'items_list_navigation'    => __( 'Patterns list navigation' ),
 299                  'items_list'               => __( 'Patterns list' ),
 300                  'item_published'           => __( 'Pattern published.' ),
 301                  'item_published_privately' => __( 'Pattern published privately.' ),
 302                  'item_reverted_to_draft'   => __( 'Pattern reverted to draft.' ),
 303                  'item_scheduled'           => __( 'Pattern scheduled.' ),
 304                  'item_updated'             => __( 'Pattern updated.' ),
 305              ),
 306              'public'                => false,
 307              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
 308              'show_ui'               => true,
 309              'show_in_menu'          => false,
 310              'rewrite'               => false,
 311              'show_in_rest'          => true,
 312              'rest_base'             => 'blocks',
 313              'rest_controller_class' => 'WP_REST_Blocks_Controller',
 314              'capability_type'       => 'block',
 315              'capabilities'          => array(
 316                  // You need to be able to edit posts, in order to read blocks in their raw form.
 317                  'read'                   => 'edit_posts',
 318                  // You need to be able to publish posts, in order to create blocks.
 319                  'create_posts'           => 'publish_posts',
 320                  'edit_posts'             => 'edit_posts',
 321                  'edit_published_posts'   => 'edit_published_posts',
 322                  'delete_published_posts' => 'delete_published_posts',
 323                  // Enables trashing draft posts as well.
 324                  'delete_posts'           => 'delete_posts',
 325                  'edit_others_posts'      => 'edit_others_posts',
 326                  'delete_others_posts'    => 'delete_others_posts',
 327              ),
 328              'map_meta_cap'          => true,
 329              'supports'              => array(
 330                  'title',
 331                  'excerpt',
 332                  'editor',
 333                  'revisions',
 334                  'custom-fields',
 335              ),
 336          )
 337      );
 338  
 339      $template_edit_link = 'site-editor.php?' . build_query(
 340          array(
 341              'postType' => '%s',
 342              'postId'   => '%s',
 343              'canvas'   => 'edit',
 344          )
 345      );
 346  
 347      register_post_type(
 348          'wp_template',
 349          array(
 350              'labels'                          => array(
 351                  'name'                  => _x( 'Templates', 'post type general name' ),
 352                  'singular_name'         => _x( 'Template', 'post type singular name' ),
 353                  'add_new'               => __( 'Add New Template' ),
 354                  'add_new_item'          => __( 'Add New Template' ),
 355                  'new_item'              => __( 'New Template' ),
 356                  'edit_item'             => __( 'Edit Template' ),
 357                  'view_item'             => __( 'View Template' ),
 358                  'all_items'             => __( 'Templates' ),
 359                  'search_items'          => __( 'Search Templates' ),
 360                  'parent_item_colon'     => __( 'Parent Template:' ),
 361                  'not_found'             => __( 'No templates found.' ),
 362                  'not_found_in_trash'    => __( 'No templates found in Trash.' ),
 363                  'archives'              => __( 'Template archives' ),
 364                  'insert_into_item'      => __( 'Insert into template' ),
 365                  'uploaded_to_this_item' => __( 'Uploaded to this template' ),
 366                  'filter_items_list'     => __( 'Filter templates list' ),
 367                  'items_list_navigation' => __( 'Templates list navigation' ),
 368                  'items_list'            => __( 'Templates list' ),
 369                  'item_updated'          => __( 'Template updated.' ),
 370              ),
 371              'description'                     => __( 'Templates to include in your theme.' ),
 372              'public'                          => false,
 373              '_builtin'                        => true, /* internal use only. don't use this when registering your own post type. */
 374              '_edit_link'                      => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
 375              'has_archive'                     => false,
 376              'show_ui'                         => false,
 377              'show_in_menu'                    => false,
 378              'show_in_rest'                    => true,
 379              'rewrite'                         => false,
 380              'rest_base'                       => 'templates',
 381              'rest_controller_class'           => 'WP_REST_Templates_Controller',
 382              'autosave_rest_controller_class'  => 'WP_REST_Template_Autosaves_Controller',
 383              'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller',
 384              'late_route_registration'         => true,
 385              'capability_type'                 => array( 'template', 'templates' ),
 386              'capabilities'                    => array(
 387                  'create_posts'           => 'edit_theme_options',
 388                  'delete_posts'           => 'edit_theme_options',
 389                  'delete_others_posts'    => 'edit_theme_options',
 390                  'delete_private_posts'   => 'edit_theme_options',
 391                  'delete_published_posts' => 'edit_theme_options',
 392                  'edit_posts'             => 'edit_theme_options',
 393                  'edit_others_posts'      => 'edit_theme_options',
 394                  'edit_private_posts'     => 'edit_theme_options',
 395                  'edit_published_posts'   => 'edit_theme_options',
 396                  'publish_posts'          => 'edit_theme_options',
 397                  'read'                   => 'edit_theme_options',
 398                  'read_private_posts'     => 'edit_theme_options',
 399              ),
 400              'map_meta_cap'                    => true,
 401              'supports'                        => array(
 402                  'title',
 403                  'slug',
 404                  'excerpt',
 405                  'editor',
 406                  'revisions',
 407                  'author',
 408              ),
 409          )
 410      );
 411  
 412      register_post_type(
 413          'wp_template_part',
 414          array(
 415              'labels'                          => array(
 416                  'name'                  => _x( 'Template Parts', 'post type general name' ),
 417                  'singular_name'         => _x( 'Template Part', 'post type singular name' ),
 418                  'add_new'               => __( 'Add New Template Part' ),
 419                  'add_new_item'          => __( 'Add New Template Part' ),
 420                  'new_item'              => __( 'New Template Part' ),
 421                  'edit_item'             => __( 'Edit Template Part' ),
 422                  'view_item'             => __( 'View Template Part' ),
 423                  'all_items'             => __( 'Template Parts' ),
 424                  'search_items'          => __( 'Search Template Parts' ),
 425                  'parent_item_colon'     => __( 'Parent Template Part:' ),
 426                  'not_found'             => __( 'No template parts found.' ),
 427                  'not_found_in_trash'    => __( 'No template parts found in Trash.' ),
 428                  'archives'              => __( 'Template part archives' ),
 429                  'insert_into_item'      => __( 'Insert into template part' ),
 430                  'uploaded_to_this_item' => __( 'Uploaded to this template part' ),
 431                  'filter_items_list'     => __( 'Filter template parts list' ),
 432                  'items_list_navigation' => __( 'Template parts list navigation' ),
 433                  'items_list'            => __( 'Template parts list' ),
 434                  'item_updated'          => __( 'Template part updated.' ),
 435              ),
 436              'description'                     => __( 'Template parts to include in your templates.' ),
 437              'public'                          => false,
 438              '_builtin'                        => true, /* internal use only. don't use this when registering your own post type. */
 439              '_edit_link'                      => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
 440              'has_archive'                     => false,
 441              'show_ui'                         => false,
 442              'show_in_menu'                    => false,
 443              'show_in_rest'                    => true,
 444              'rewrite'                         => false,
 445              'rest_base'                       => 'template-parts',
 446              'rest_controller_class'           => 'WP_REST_Templates_Controller',
 447              'autosave_rest_controller_class'  => 'WP_REST_Template_Autosaves_Controller',
 448              'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller',
 449              'late_route_registration'         => true,
 450              'map_meta_cap'                    => true,
 451              'capabilities'                    => array(
 452                  'create_posts'           => 'edit_theme_options',
 453                  'delete_posts'           => 'edit_theme_options',
 454                  'delete_others_posts'    => 'edit_theme_options',
 455                  'delete_private_posts'   => 'edit_theme_options',
 456                  'delete_published_posts' => 'edit_theme_options',
 457                  'edit_posts'             => 'edit_theme_options',
 458                  'edit_others_posts'      => 'edit_theme_options',
 459                  'edit_private_posts'     => 'edit_theme_options',
 460                  'edit_published_posts'   => 'edit_theme_options',
 461                  'publish_posts'          => 'edit_theme_options',
 462                  'read'                   => 'edit_theme_options',
 463                  'read_private_posts'     => 'edit_theme_options',
 464              ),
 465              'supports'                        => array(
 466                  'title',
 467                  'slug',
 468                  'excerpt',
 469                  'editor',
 470                  'revisions',
 471                  'author',
 472              ),
 473          )
 474      );
 475  
 476      register_post_type(
 477          'wp_global_styles',
 478          array(
 479              'label'                           => _x( 'Global Styles', 'post type general name' ),
 480              'description'                     => __( 'Global styles to include in themes.' ),
 481              'public'                          => false,
 482              '_builtin'                        => true, /* internal use only. don't use this when registering your own post type. */
 483              '_edit_link'                      => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
 484              'show_ui'                         => false,
 485              'show_in_rest'                    => true,
 486              'rewrite'                         => false,
 487              'rest_base'                       => 'global-styles',
 488              'rest_controller_class'           => 'WP_REST_Global_Styles_Controller',
 489              'revisions_rest_controller_class' => 'WP_REST_Global_Styles_Revisions_Controller',
 490              'late_route_registration'         => true,
 491              'capabilities'                    => array(
 492                  'read'                   => 'edit_posts',
 493                  'create_posts'           => 'edit_theme_options',
 494                  'edit_posts'             => 'edit_theme_options',
 495                  'edit_published_posts'   => 'edit_theme_options',
 496                  'delete_published_posts' => 'edit_theme_options',
 497                  'edit_others_posts'      => 'edit_theme_options',
 498                  'delete_others_posts'    => 'edit_theme_options',
 499              ),
 500              'map_meta_cap'                    => true,
 501              'supports'                        => array(
 502                  'title',
 503                  'editor',
 504                  'revisions',
 505              ),
 506          )
 507      );
 508      // Disable autosave endpoints for global styles.
 509      remove_post_type_support( 'wp_global_styles', 'autosave' );
 510  
 511      $navigation_post_edit_link = 'site-editor.php?' . build_query(
 512          array(
 513              'postId'   => '%s',
 514              'postType' => 'wp_navigation',
 515              'canvas'   => 'edit',
 516          )
 517      );
 518  
 519      register_post_type(
 520          'wp_navigation',
 521          array(
 522              'labels'                => array(
 523                  'name'                  => _x( 'Navigation Menus', 'post type general name' ),
 524                  'singular_name'         => _x( 'Navigation Menu', 'post type singular name' ),
 525                  'add_new'               => __( 'Add New Navigation Menu' ),
 526                  'add_new_item'          => __( 'Add New Navigation Menu' ),
 527                  'new_item'              => __( 'New Navigation Menu' ),
 528                  'edit_item'             => __( 'Edit Navigation Menu' ),
 529                  'view_item'             => __( 'View Navigation Menu' ),
 530                  'all_items'             => __( 'Navigation Menus' ),
 531                  'search_items'          => __( 'Search Navigation Menus' ),
 532                  'parent_item_colon'     => __( 'Parent Navigation Menu:' ),
 533                  'not_found'             => __( 'No Navigation Menu found.' ),
 534                  'not_found_in_trash'    => __( 'No Navigation Menu found in Trash.' ),
 535                  'archives'              => __( 'Navigation Menu archives' ),
 536                  'insert_into_item'      => __( 'Insert into Navigation Menu' ),
 537                  'uploaded_to_this_item' => __( 'Uploaded to this Navigation Menu' ),
 538                  'filter_items_list'     => __( 'Filter Navigation Menu list' ),
 539                  'items_list_navigation' => __( 'Navigation Menus list navigation' ),
 540                  'items_list'            => __( 'Navigation Menus list' ),
 541              ),
 542              'description'           => __( 'Navigation menus that can be inserted into your site.' ),
 543              'public'                => false,
 544              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
 545              '_edit_link'            => $navigation_post_edit_link, /* internal use only. don't use this when registering your own post type. */
 546              'has_archive'           => false,
 547              'show_ui'               => true,
 548              'show_in_menu'          => false,
 549              'show_in_admin_bar'     => false,
 550              'show_in_rest'          => true,
 551              'rewrite'               => false,
 552              'map_meta_cap'          => true,
 553              'capabilities'          => array(
 554                  'edit_others_posts'      => 'edit_theme_options',
 555                  'delete_posts'           => 'edit_theme_options',
 556                  'publish_posts'          => 'edit_theme_options',
 557                  'create_posts'           => 'edit_theme_options',
 558                  'read_private_posts'     => 'edit_theme_options',
 559                  'delete_private_posts'   => 'edit_theme_options',
 560                  'delete_published_posts' => 'edit_theme_options',
 561                  'delete_others_posts'    => 'edit_theme_options',
 562                  'edit_private_posts'     => 'edit_theme_options',
 563                  'edit_published_posts'   => 'edit_theme_options',
 564                  'edit_posts'             => 'edit_theme_options',
 565              ),
 566              'rest_base'             => 'navigation',
 567              'rest_controller_class' => 'WP_REST_Posts_Controller',
 568              'supports'              => array(
 569                  'title',
 570                  'editor',
 571                  'revisions',
 572              ),
 573          )
 574      );
 575  
 576      register_post_type(
 577          'wp_font_family',
 578          array(
 579              'labels'                => array(
 580                  'name'          => __( 'Font Families' ),
 581                  'singular_name' => __( 'Font Family' ),
 582              ),
 583              'public'                => false,
 584              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
 585              'hierarchical'          => false,
 586              'capabilities'          => array(
 587                  'read'                   => 'edit_theme_options',
 588                  'read_private_posts'     => 'edit_theme_options',
 589                  'create_posts'           => 'edit_theme_options',
 590                  'publish_posts'          => 'edit_theme_options',
 591                  'edit_posts'             => 'edit_theme_options',
 592                  'edit_others_posts'      => 'edit_theme_options',
 593                  'edit_published_posts'   => 'edit_theme_options',
 594                  'delete_posts'           => 'edit_theme_options',
 595                  'delete_others_posts'    => 'edit_theme_options',
 596                  'delete_published_posts' => 'edit_theme_options',
 597              ),
 598              'map_meta_cap'          => true,
 599              'query_var'             => false,
 600              'rewrite'               => false,
 601              'show_in_rest'          => true,
 602              'rest_base'             => 'font-families',
 603              'rest_controller_class' => 'WP_REST_Font_Families_Controller',
 604              'supports'              => array( 'title' ),
 605          )
 606      );
 607  
 608      register_post_type(
 609          'wp_font_face',
 610          array(
 611              'labels'                => array(
 612                  'name'          => __( 'Font Faces' ),
 613                  'singular_name' => __( 'Font Face' ),
 614              ),
 615              'public'                => false,
 616              '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
 617              'hierarchical'          => false,
 618              'capabilities'          => array(
 619                  'read'                   => 'edit_theme_options',
 620                  'read_private_posts'     => 'edit_theme_options',
 621                  'create_posts'           => 'edit_theme_options',
 622                  'publish_posts'          => 'edit_theme_options',
 623                  'edit_posts'             => 'edit_theme_options',
 624                  'edit_others_posts'      => 'edit_theme_options',
 625                  'edit_published_posts'   => 'edit_theme_options',
 626                  'delete_posts'           => 'edit_theme_options',
 627                  'delete_others_posts'    => 'edit_theme_options',
 628                  'delete_published_posts' => 'edit_theme_options',
 629              ),
 630              'map_meta_cap'          => true,
 631              'query_var'             => false,
 632              'rewrite'               => false,
 633              'show_in_rest'          => true,
 634              'rest_base'             => 'font-families/(?P<font_family_id>[\d]+)/font-faces',
 635              'rest_controller_class' => 'WP_REST_Font_Faces_Controller',
 636              'supports'              => array( 'title' ),
 637          )
 638      );
 639  
 640      register_post_status(
 641          'publish',
 642          array(
 643              'label'       => _x( 'Published', 'post status' ),
 644              'public'      => true,
 645              '_builtin'    => true, /* internal use only. */
 646              /* translators: %s: Number of published posts. */
 647              'label_count' => _n_noop(
 648                  'Published <span class="count">(%s)</span>',
 649                  'Published <span class="count">(%s)</span>'
 650              ),
 651          )
 652      );
 653  
 654      register_post_status(
 655          'future',
 656          array(
 657              'label'       => _x( 'Scheduled', 'post status' ),
 658              'protected'   => true,
 659              '_builtin'    => true, /* internal use only. */
 660              /* translators: %s: Number of scheduled posts. */
 661              'label_count' => _n_noop(
 662                  'Scheduled <span class="count">(%s)</span>',
 663                  'Scheduled <span class="count">(%s)</span>'
 664              ),
 665          )
 666      );
 667  
 668      register_post_status(
 669          'draft',
 670          array(
 671              'label'         => _x( 'Draft', 'post status' ),
 672              'protected'     => true,
 673              '_builtin'      => true, /* internal use only. */
 674              /* translators: %s: Number of draft posts. */
 675              'label_count'   => _n_noop(
 676                  'Draft <span class="count">(%s)</span>',
 677                  'Drafts <span class="count">(%s)</span>'
 678              ),
 679              'date_floating' => true,
 680          )
 681      );
 682  
 683      register_post_status(
 684          'pending',
 685          array(
 686              'label'         => _x( 'Pending', 'post status' ),
 687              'protected'     => true,
 688              '_builtin'      => true, /* internal use only. */
 689              /* translators: %s: Number of pending posts. */
 690              'label_count'   => _n_noop(
 691                  'Pending <span class="count">(%s)</span>',
 692                  'Pending <span class="count">(%s)</span>'
 693              ),
 694              'date_floating' => true,
 695          )
 696      );
 697  
 698      register_post_status(
 699          'private',
 700          array(
 701              'label'       => _x( 'Private', 'post status' ),
 702              'private'     => true,
 703              '_builtin'    => true, /* internal use only. */
 704              /* translators: %s: Number of private posts. */
 705              'label_count' => _n_noop(
 706                  'Private <span class="count">(%s)</span>',
 707                  'Private <span class="count">(%s)</span>'
 708              ),
 709          )
 710      );
 711  
 712      register_post_status(
 713          'trash',
 714          array(
 715              'label'                     => _x( 'Trash', 'post status' ),
 716              'internal'                  => true,
 717              '_builtin'                  => true, /* internal use only. */
 718              /* translators: %s: Number of trashed posts. */
 719              'label_count'               => _n_noop(
 720                  'Trash <span class="count">(%s)</span>',
 721                  'Trash <span class="count">(%s)</span>'
 722              ),
 723              'show_in_admin_status_list' => true,
 724          )
 725      );
 726  
 727      register_post_status(
 728          'auto-draft',
 729          array(
 730              'label'         => 'auto-draft',
 731              'internal'      => true,
 732              '_builtin'      => true, /* internal use only. */
 733              'date_floating' => true,
 734          )
 735      );
 736  
 737      register_post_status(
 738          'inherit',
 739          array(
 740              'label'               => 'inherit',
 741              'internal'            => true,
 742              '_builtin'            => true, /* internal use only. */
 743              'exclude_from_search' => false,
 744          )
 745      );
 746  
 747      register_post_status(
 748          'request-pending',
 749          array(
 750              'label'               => _x( 'Pending', 'request status' ),
 751              'internal'            => true,
 752              '_builtin'            => true, /* internal use only. */
 753              /* translators: %s: Number of pending requests. */
 754              'label_count'         => _n_noop(
 755                  'Pending <span class="count">(%s)</span>',
 756                  'Pending <span class="count">(%s)</span>'
 757              ),
 758              'exclude_from_search' => false,
 759          )
 760      );
 761  
 762      register_post_status(
 763          'request-confirmed',
 764          array(
 765              'label'               => _x( 'Confirmed', 'request status' ),
 766              'internal'            => true,
 767              '_builtin'            => true, /* internal use only. */
 768              /* translators: %s: Number of confirmed requests. */
 769              'label_count'         => _n_noop(
 770                  'Confirmed <span class="count">(%s)</span>',
 771                  'Confirmed <span class="count">(%s)</span>'
 772              ),
 773              'exclude_from_search' => false,
 774          )
 775      );
 776  
 777      register_post_status(
 778          'request-failed',
 779          array(
 780              'label'               => _x( 'Failed', 'request status' ),
 781              'internal'            => true,
 782              '_builtin'            => true, /* internal use only. */
 783              /* translators: %s: Number of failed requests. */
 784              'label_count'         => _n_noop(
 785                  'Failed <span class="count">(%s)</span>',
 786                  'Failed <span class="count">(%s)</span>'
 787              ),
 788              'exclude_from_search' => false,
 789          )
 790      );
 791  
 792      register_post_status(
 793          'request-completed',
 794          array(
 795              'label'               => _x( 'Completed', 'request status' ),
 796              'internal'            => true,
 797              '_builtin'            => true, /* internal use only. */
 798              /* translators: %s: Number of completed requests. */
 799              'label_count'         => _n_noop(
 800                  'Completed <span class="count">(%s)</span>',
 801                  'Completed <span class="count">(%s)</span>'
 802              ),
 803              'exclude_from_search' => false,
 804          )
 805      );
 806  }
 807  
 808  /**
 809   * Retrieves attached file path based on attachment ID.
 810   *
 811   * By default the path will go through the {@see 'get_attached_file'} filter, but
 812   * passing `true` to the `$unfiltered` argument will return the file path unfiltered.
 813   *
 814   * The function works by retrieving the `_wp_attached_file` post meta value.
 815   * This is a convenience function to prevent looking up the meta name and provide
 816   * a mechanism for sending the attached filename through a filter.
 817   *
 818   * @since 2.0.0
 819   *
 820   * @param int  $attachment_id Attachment ID.
 821   * @param bool $unfiltered    Optional. Whether to skip the {@see 'get_attached_file'} filter.
 822   *                            Default false.
 823   * @return string|false The file path to where the attached file should be, false otherwise.
 824   */
 825  function get_attached_file( $attachment_id, $unfiltered = false ) {
 826      $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
 827  
 828      // If the file is relative, prepend upload dir.
 829      if ( $file && ! str_starts_with( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
 830          $uploads = wp_get_upload_dir();
 831          if ( false === $uploads['error'] ) {
 832              $file = $uploads['basedir'] . "/$file";
 833          }
 834      }
 835  
 836      if ( $unfiltered ) {
 837          return $file;
 838      }
 839  
 840      /**
 841       * Filters the attached file based on the given ID.
 842       *
 843       * @since 2.1.0
 844       *
 845       * @param string|false $file          The file path to where the attached file should be, false otherwise.
 846       * @param int          $attachment_id Attachment ID.
 847       */
 848      return apply_filters( 'get_attached_file', $file, $attachment_id );
 849  }
 850  
 851  /**
 852   * Updates attachment file path based on attachment ID.
 853   *
 854   * Used to update the file path of the attachment, which uses post meta name
 855   * `_wp_attached_file` to store the path of the attachment.
 856   *
 857   * @since 2.1.0
 858   *
 859   * @param int    $attachment_id Attachment ID.
 860   * @param string $file          File path for the attachment.
 861   * @return int|bool Meta ID if the `_wp_attached_file` key didn't exist for the attachment.
 862   *                  True on successful update, false on failure or if the `$file` value passed
 863   *                  to the function is the same as the one that is already in the database.
 864   */
 865  function update_attached_file( $attachment_id, $file ) {
 866      if ( ! get_post( $attachment_id ) ) {
 867          return false;
 868      }
 869  
 870      /**
 871       * Filters the path to the attached file to update.
 872       *
 873       * @since 2.1.0
 874       *
 875       * @param string $file          Path to the attached file to update.
 876       * @param int    $attachment_id Attachment ID.
 877       */
 878      $file = apply_filters( 'update_attached_file', $file, $attachment_id );
 879  
 880      $file = _wp_relative_upload_path( $file );
 881      if ( $file ) {
 882          return update_post_meta( $attachment_id, '_wp_attached_file', $file );
 883      } else {
 884          return delete_post_meta( $attachment_id, '_wp_attached_file' );
 885      }
 886  }
 887  
 888  /**
 889   * Returns relative path to an uploaded file.
 890   *
 891   * The path is relative to the current upload dir.
 892   *
 893   * @since 2.9.0
 894   * @access private
 895   *
 896   * @param string $path Full path to the file.
 897   * @return string Relative path on success, unchanged path on failure.
 898   */
 899  function _wp_relative_upload_path( $path ) {
 900      $new_path = $path;
 901  
 902      $uploads = wp_get_upload_dir();
 903      if ( str_starts_with( $new_path, $uploads['basedir'] ) ) {
 904              $new_path = str_replace( $uploads['basedir'], '', $new_path );
 905              $new_path = ltrim( $new_path, '/' );
 906      }
 907  
 908      /**
 909       * Filters the relative path to an uploaded file.
 910       *
 911       * @since 2.9.0
 912       *
 913       * @param string $new_path Relative path to the file.
 914       * @param string $path     Full path to the file.
 915       */
 916      return apply_filters( '_wp_relative_upload_path', $new_path, $path );
 917  }
 918  
 919  /**
 920   * Retrieves all children of the post parent ID.
 921   *
 922   * Normally, without any enhancements, the children would apply to pages. In the
 923   * context of the inner workings of WordPress, pages, posts, and attachments
 924   * share the same table, so therefore the functionality could apply to any one
 925   * of them. It is then noted that while this function does not work on posts, it
 926   * does not mean that it won't work on posts. It is recommended that you know
 927   * what context you wish to retrieve the children of.
 928   *
 929   * Attachments may also be made the child of a post, so if that is an accurate
 930   * statement (which needs to be verified), it would then be possible to get
 931   * all of the attachments for a post. Attachments have since changed since
 932   * version 2.5, so this is most likely inaccurate, but serves generally as an
 933   * example of what is possible.
 934   *
 935   * The arguments listed as defaults are for this function and also of the
 936   * get_posts() function. The arguments are combined with the get_children defaults
 937   * and are then passed to the get_posts() function, which accepts additional arguments.
 938   * You can replace the defaults in this function, listed below and the additional
 939   * arguments listed in the get_posts() function.
 940   *
 941   * The 'post_parent' is the most important argument and important attention
 942   * needs to be paid to the $args parameter. If you pass either an object or an
 943   * integer (number), then just the 'post_parent' is grabbed and everything else
 944   * is lost. If you don't specify any arguments, then it is assumed that you are
 945   * in The Loop and the post parent will be grabbed for from the current post.
 946   *
 947   * The 'post_parent' argument is the ID to get the children. The 'numberposts'
 948   * is the amount of posts to retrieve that has a default of '-1', which is
 949   * used to get all of the posts. Giving a number higher than 0 will only
 950   * retrieve that amount of posts.
 951   *
 952   * The 'post_type' and 'post_status' arguments can be used to choose what
 953   * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
 954   * post types are 'post', 'pages', and 'attachments'. The 'post_status'
 955   * argument will accept any post status within the write administration panels.
 956   *
 957   * @since 2.0.0
 958   *
 959   * @see get_posts()
 960   * @todo Check validity of description.
 961   *
 962   * @global WP_Post $post Global post object.
 963   *
 964   * @param mixed  $args   Optional. User defined arguments for replacing the defaults. Default empty.
 965   * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 966   *                       correspond to a WP_Post object, an associative array, or a numeric array,
 967   *                       respectively. Default OBJECT.
 968   * @return WP_Post[]|array[]|int[] Array of post objects, arrays, or IDs, depending on `$output`.
 969   */
 970  function get_children( $args = '', $output = OBJECT ) {
 971      $kids = array();
 972      if ( empty( $args ) ) {
 973          if ( isset( $GLOBALS['post'] ) ) {
 974              $args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent );
 975          } else {
 976              return $kids;
 977          }
 978      } elseif ( is_object( $args ) ) {
 979          $args = array( 'post_parent' => (int) $args->post_parent );
 980      } elseif ( is_numeric( $args ) ) {
 981          $args = array( 'post_parent' => (int) $args );
 982      }
 983  
 984      $defaults = array(
 985          'numberposts' => -1,
 986          'post_type'   => 'any',
 987          'post_status' => 'any',
 988          'post_parent' => 0,
 989      );
 990  
 991      $parsed_args = wp_parse_args( $args, $defaults );
 992  
 993      $children = get_posts( $parsed_args );
 994  
 995      if ( ! $children ) {
 996          return $kids;
 997      }
 998  
 999      if ( ! empty( $parsed_args['fields'] ) ) {
1000          return $children;
1001      }
1002  
1003      update_post_cache( $children );
1004  
1005      foreach ( $children as $key => $child ) {
1006          $kids[ $child->ID ] = $children[ $key ];
1007      }
1008  
1009      if ( OBJECT === $output ) {
1010          return $kids;
1011      } elseif ( ARRAY_A === $output ) {
1012          $weeuns = array();
1013          foreach ( (array) $kids as $kid ) {
1014              $weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
1015          }
1016          return $weeuns;
1017      } elseif ( ARRAY_N === $output ) {
1018          $babes = array();
1019          foreach ( (array) $kids as $kid ) {
1020              $babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
1021          }
1022          return $babes;
1023      } else {
1024          return $kids;
1025      }
1026  }
1027  
1028  /**
1029   * Gets extended entry info (<!--more-->).
1030   *
1031   * There should not be any space after the second dash and before the word
1032   * 'more'. There can be text or space(s) after the word 'more', but won't be
1033   * referenced.
1034   *
1035   * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
1036   * the `<!--more-->`. The 'extended' key has the content after the
1037   * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text.
1038   *
1039   * @since 1.0.0
1040   *
1041   * @param string $post Post content.
1042   * @return string[] {
1043   *     Extended entry info.
1044   *
1045   *     @type string $main      Content before the more tag.
1046   *     @type string $extended  Content after the more tag.
1047   *     @type string $more_text Custom read more text, or empty string.
1048   * }
1049   */
1050  function get_extended( $post ) {
1051      // Match the new style more links.
1052      if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) {
1053          list($main, $extended) = explode( $matches[0], $post, 2 );
1054          $more_text             = $matches[1];
1055      } else {
1056          $main      = $post;
1057          $extended  = '';
1058          $more_text = '';
1059      }
1060  
1061      // Leading and trailing whitespace.
1062      $main      = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main );
1063      $extended  = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended );
1064      $more_text = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $more_text );
1065  
1066      return array(
1067          'main'      => $main,
1068          'extended'  => $extended,
1069          'more_text' => $more_text,
1070      );
1071  }
1072  
1073  /**
1074   * Retrieves post data given a post ID or post object.
1075   *
1076   * See sanitize_post() for optional $filter values. Also, the parameter
1077   * `$post`, must be given as a variable, since it is passed by reference.
1078   *
1079   * @since 1.5.1
1080   *
1081   * @global WP_Post $post Global post object.
1082   *
1083   * @param int|WP_Post|null $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values
1084   *                                 return the current global post inside the loop. A numerically valid post ID that
1085   *                                 points to a non-existent post returns `null`. Defaults to global $post.
1086   * @param string           $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
1087   *                                 correspond to a WP_Post object, an associative array, or a numeric array,
1088   *                                 respectively. Default OBJECT.
1089   * @param string           $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
1090   *                                 or 'display'. Default 'raw'.
1091   * @return WP_Post|array|null Type corresponding to $output on success or null on failure.
1092   *                            When $output is OBJECT, a `WP_Post` instance is returned.
1093   */
1094  function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
1095      if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
1096          $post = $GLOBALS['post'];
1097      }
1098  
1099      if ( $post instanceof WP_Post ) {
1100          $_post = $post;
1101      } elseif ( is_object( $post ) ) {
1102          if ( empty( $post->filter ) ) {
1103              $_post = sanitize_post( $post, 'raw' );
1104              $_post = new WP_Post( $_post );
1105          } elseif ( 'raw' === $post->filter ) {
1106              $_post = new WP_Post( $post );
1107          } else {
1108              $_post = WP_Post::get_instance( $post->ID );
1109          }
1110      } else {
1111          $_post = WP_Post::get_instance( $post );
1112      }
1113  
1114      if ( ! $_post ) {
1115          return null;
1116      }
1117  
1118      $_post = $_post->filter( $filter );
1119  
1120      if ( ARRAY_A === $output ) {
1121          return $_post->to_array();
1122      } elseif ( ARRAY_N === $output ) {
1123          return array_values( $_post->to_array() );
1124      }
1125  
1126      return $_post;
1127  }
1128  
1129  /**
1130   * Retrieves the IDs of the ancestors of a post.
1131   *
1132   * @since 2.5.0
1133   *
1134   * @param int|WP_Post $post Post ID or post object.
1135   * @return int[] Array of ancestor IDs or empty array if there are none.
1136   */
1137  function get_post_ancestors( $post ) {
1138      $post = get_post( $post );
1139  
1140      if ( ! $post || empty( $post->post_parent ) || $post->post_parent === $post->ID ) {
1141          return array();
1142      }
1143  
1144      $ancestors = array();
1145  
1146      $id          = $post->post_parent;
1147      $ancestors[] = $id;
1148  
1149      while ( $ancestor = get_post( $id ) ) {
1150          // Loop detection: If the ancestor has been seen before, break.
1151          if ( empty( $ancestor->post_parent ) || $ancestor->post_parent === $post->ID
1152              || in_array( $ancestor->post_parent, $ancestors, true )
1153          ) {
1154              break;
1155          }
1156  
1157          $id          = $ancestor->post_parent;
1158          $ancestors[] = $id;
1159      }
1160  
1161      return $ancestors;
1162  }
1163  
1164  /**
1165   * Retrieves data from a post field based on Post ID.
1166   *
1167   * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
1168   * etc and based off of the post object property or key names.
1169   *
1170   * The context values are based off of the taxonomy filter functions and
1171   * supported values are found within those functions.
1172   *
1173   * @since 2.3.0
1174   * @since 4.5.0 The `$post` parameter was made optional.
1175   *
1176   * @see sanitize_post_field()
1177   *
1178   * @param string      $field   Post field name.
1179   * @param int|WP_Post $post    Optional. Post ID or post object. Defaults to global $post.
1180   * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
1181   *                             or 'display'. Default 'display'.
1182   * @return string The value of the post field on success, empty string on failure.
1183   */
1184  function get_post_field( $field, $post = null, $context = 'display' ) {
1185      $post = get_post( $post );
1186  
1187      if ( ! $post ) {
1188          return '';
1189      }
1190  
1191      if ( ! isset( $post->$field ) ) {
1192          return '';
1193      }
1194  
1195      return sanitize_post_field( $field, $post->$field, $post->ID, $context );
1196  }
1197  
1198  /**
1199   * Retrieves the mime type of an attachment based on the ID.
1200   *
1201   * This function can be used with any post type, but it makes more sense with
1202   * attachments.
1203   *
1204   * @since 2.0.0
1205   *
1206   * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
1207   * @return string|false The mime type on success, false on failure.
1208   */
1209  function get_post_mime_type( $post = null ) {
1210      $post = get_post( $post );
1211  
1212      if ( is_object( $post ) ) {
1213          return $post->post_mime_type;
1214      }
1215  
1216      return false;
1217  }
1218  
1219  /**
1220   * Retrieves the post status based on the post ID.
1221   *
1222   * If the post ID is of an attachment, then the parent post status will be given
1223   * instead.
1224   *
1225   * @since 2.0.0
1226   *
1227   * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
1228   * @return string|false Post status on success, false on failure.
1229   */
1230  function get_post_status( $post = null ) {
1231      // Normalize the post object if necessary, skip normalization if called from get_sample_permalink().
1232      if ( ! $post instanceof WP_Post || ! isset( $post->filter ) || 'sample' !== $post->filter ) {
1233          $post = get_post( $post );
1234      }
1235  
1236      if ( ! is_object( $post ) ) {
1237          return false;
1238      }
1239  
1240      $post_status = $post->post_status;
1241  
1242      if (
1243          'attachment' === $post->post_type &&
1244          'inherit' === $post_status
1245      ) {
1246          if (
1247              0 === $post->post_parent ||
1248              ! get_post( $post->post_parent ) ||
1249              $post->ID === $post->post_parent
1250          ) {
1251              // Unattached attachments with inherit status are assumed to be published.
1252              $post_status = 'publish';
1253          } elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
1254              // Get parent status prior to trashing.
1255              $post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
1256  
1257              if ( ! $post_status ) {
1258                  // Assume publish as above.
1259                  $post_status = 'publish';
1260              }
1261          } else {
1262              $post_status = get_post_status( $post->post_parent );
1263          }
1264      } elseif (
1265          'attachment' === $post->post_type &&
1266          ! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
1267      ) {
1268          /*
1269           * Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
1270           * This is to match the logic in wp_insert_post().
1271           *
1272           * Note: 'inherit' is excluded from this check as it is resolved to the parent post's
1273           * status in the logic block above.
1274           */
1275          $post_status = 'publish';
1276      }
1277  
1278      /**
1279       * Filters the post status.
1280       *
1281       * @since 4.4.0
1282       * @since 5.7.0 The attachment post type is now passed through this filter.
1283       *
1284       * @param string  $post_status The post status.
1285       * @param WP_Post $post        The post object.
1286       */
1287      return apply_filters( 'get_post_status', $post_status, $post );
1288  }
1289  
1290  /**
1291   * Retrieves all of the WordPress supported post statuses.
1292   *
1293   * Posts have a limited set of valid status values, this provides the
1294   * post_status values and descriptions.
1295   *
1296   * @since 2.5.0
1297   *
1298   * @return string[] Array of post status labels keyed by their status.
1299   */
1300  function get_post_statuses() {
1301      $status = array(
1302          'draft'   => __( 'Draft' ),
1303          'pending' => __( 'Pending Review' ),
1304          'private' => __( 'Private' ),
1305          'publish' => __( 'Published' ),
1306      );
1307  
1308      return $status;
1309  }
1310  
1311  /**
1312   * Retrieves all of the WordPress support page statuses.
1313   *
1314   * Pages have a limited set of valid status values, this provides the
1315   * post_status values and descriptions.
1316   *
1317   * @since 2.5.0
1318   *
1319   * @return string[] Array of page status labels keyed by their status.
1320   */
1321  function get_page_statuses() {
1322      $status = array(
1323          'draft'   => __( 'Draft' ),
1324          'private' => __( 'Private' ),
1325          'publish' => __( 'Published' ),
1326      );
1327  
1328      return $status;
1329  }
1330  
1331  /**
1332   * Returns statuses for privacy requests.
1333   *
1334   * @since 4.9.6
1335   * @access private
1336   *
1337   * @return string[] Array of privacy request status labels keyed by their status.
1338   */
1339  function _wp_privacy_statuses() {
1340      return array(
1341          'request-pending'   => _x( 'Pending', 'request status' ),      // Pending confirmation from user.
1342          'request-confirmed' => _x( 'Confirmed', 'request status' ),    // User has confirmed the action.
1343          'request-failed'    => _x( 'Failed', 'request status' ),       // User failed to confirm the action.
1344          'request-completed' => _x( 'Completed', 'request status' ),    // Admin has handled the request.
1345      );
1346  }
1347  
1348  /**
1349   * Registers a post status. Do not use before init.
1350   *
1351   * A simple function for creating or modifying a post status based on the
1352   * parameters given. The function will accept an array (second optional
1353   * parameter), along with a string for the post status name.
1354   *
1355   * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
1356   *
1357   * @since 3.0.0
1358   *
1359   * @global stdClass[] $wp_post_statuses Inserts new post status object into the list
1360   *
1361   * @param string       $post_status Name of the post status.
1362   * @param array|string $args {
1363   *     Optional. Array or string of post status arguments.
1364   *
1365   *     @type bool|string $label                     A descriptive name for the post status marked
1366   *                                                  for translation. Defaults to value of $post_status.
1367   *     @type array|false $label_count               Nooped plural text from _n_noop() to provide the singular
1368   *                                                  and plural forms of the label for counts. Default false
1369   *                                                  which means the `$label` argument will be used for both
1370   *                                                  the singular and plural forms of this label.
1371   *     @type bool        $exclude_from_search       Whether to exclude posts with this post status
1372   *                                                  from search results. Default is value of $internal.
1373   *     @type bool        $_builtin                  Whether the status is built-in. Core-use only.
1374   *                                                  Default false.
1375   *     @type bool        $public                    Whether posts of this status should be shown
1376   *                                                  in the front end of the site. Default false.
1377   *     @type bool        $internal                  Whether the status is for internal use only.
1378   *                                                  Default false.
1379   *     @type bool        $protected                 Whether posts with this status should be protected.
1380   *                                                  Default false.
1381   *     @type bool        $private                   Whether posts with this status should be private.
1382   *                                                  Default false.
1383   *     @type bool        $publicly_queryable        Whether posts with this status should be publicly-
1384   *                                                  queryable. Default is value of $public.
1385   *     @type bool        $show_in_admin_all_list    Whether to include posts in the edit listing for
1386   *                                                  their post type. Default is the opposite value
1387   *                                                  of $internal.
1388   *     @type bool        $show_in_admin_status_list Show in the list of statuses with post counts at
1389   *                                                  the top of the edit listings,
1390   *                                                  e.g. All (12) | Published (9) | My Custom Status (2)
1391   *                                                  Default is the opposite value of $internal.
1392   *     @type bool        $date_floating             Whether the post has a floating creation date.
1393   *                                                  Default to false.
1394   * }
1395   * @return object
1396   */
1397  function register_post_status( $post_status, $args = array() ) {
1398      global $wp_post_statuses;
1399  
1400      if ( ! is_array( $wp_post_statuses ) ) {
1401          $wp_post_statuses = array();
1402      }
1403  
1404      // Args prefixed with an underscore are reserved for internal use.
1405      $defaults = array(
1406          'label'                     => false,
1407          'label_count'               => false,
1408          'exclude_from_search'       => null,
1409          '_builtin'                  => false,
1410          'public'                    => null,
1411          'internal'                  => null,
1412          'protected'                 => null,
1413          'private'                   => null,
1414          'publicly_queryable'        => null,
1415          'show_in_admin_status_list' => null,
1416          'show_in_admin_all_list'    => null,
1417          'date_floating'             => null,
1418      );
1419      $args     = wp_parse_args( $args, $defaults );
1420      $args     = (object) $args;
1421  
1422      $post_status = sanitize_key( $post_status );
1423      $args->name  = $post_status;
1424  
1425      // Set various defaults.
1426      if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) {
1427          $args->internal = true;
1428      }
1429  
1430      if ( null === $args->public ) {
1431          $args->public = false;
1432      }
1433  
1434      if ( null === $args->private ) {
1435          $args->private = false;
1436      }
1437  
1438      if ( null === $args->protected ) {
1439          $args->protected = false;
1440      }
1441  
1442      if ( null === $args->internal ) {
1443          $args->internal = false;
1444      }
1445  
1446      if ( null === $args->publicly_queryable ) {
1447          $args->publicly_queryable = $args->public;
1448      }
1449  
1450      if ( null === $args->exclude_from_search ) {
1451          $args->exclude_from_search = $args->internal;
1452      }
1453  
1454      if ( null === $args->show_in_admin_all_list ) {
1455          $args->show_in_admin_all_list = ! $args->internal;
1456      }
1457  
1458      if ( null === $args->show_in_admin_status_list ) {
1459          $args->show_in_admin_status_list = ! $args->internal;
1460      }
1461  
1462      if ( null === $args->date_floating ) {
1463          $args->date_floating = false;
1464      }
1465  
1466      if ( false === $args->label ) {
1467          $args->label = $post_status;
1468      }
1469  
1470      if ( false === $args->label_count ) {
1471          // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural
1472          $args->label_count = _n_noop( $args->label, $args->label );
1473      }
1474  
1475      $wp_post_statuses[ $post_status ] = $args;
1476  
1477      return $args;
1478  }
1479  
1480  /**
1481   * Retrieves a post status object by name.
1482   *
1483   * @since 3.0.0
1484   *
1485   * @global stdClass[] $wp_post_statuses List of post statuses.
1486   *
1487   * @see register_post_status()
1488   *
1489   * @param string $post_status The name of a registered post status.
1490   * @return stdClass|null A post status object.
1491   */
1492  function get_post_status_object( $post_status ) {
1493      global $wp_post_statuses;
1494  
1495      if ( empty( $wp_post_statuses[ $post_status ] ) ) {
1496          return null;
1497      }
1498  
1499      return $wp_post_statuses[ $post_status ];
1500  }
1501  
1502  /**
1503   * Gets a list of post statuses.
1504   *
1505   * @since 3.0.0
1506   *
1507   * @global stdClass[] $wp_post_statuses List of post statuses.
1508   *
1509   * @see register_post_status()
1510   *
1511   * @param array|string $args     Optional. Array or string of post status arguments to compare against
1512   *                               properties of the global `$wp_post_statuses objects`. Default empty array.
1513   * @param string       $output   Optional. The type of output to return, either 'names' or 'objects'. Default 'names'.
1514   * @param string       $operator Optional. The logical operation to perform. 'or' means only one element
1515   *                               from the array needs to match; 'and' means all elements must match.
1516   *                               Default 'and'.
1517   * @return string[]|stdClass[] A list of post status names or objects.
1518   */
1519  function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
1520      global $wp_post_statuses;
1521  
1522      $field = ( 'names' === $output ) ? 'name' : false;
1523  
1524      return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field );
1525  }
1526  
1527  /**
1528   * Determines whether the post type is hierarchical.
1529   *
1530   * A false return value might also mean that the post type does not exist.
1531   *
1532   * @since 3.0.0
1533   *
1534   * @see get_post_type_object()
1535   *
1536   * @param string $post_type Post type name
1537   * @return bool Whether post type is hierarchical.
1538   */
1539  function is_post_type_hierarchical( $post_type ) {
1540      if ( ! post_type_exists( $post_type ) ) {
1541          return false;
1542      }
1543  
1544      $post_type = get_post_type_object( $post_type );
1545      return $post_type->hierarchical;
1546  }
1547  
1548  /**
1549   * Determines whether a post type is registered.
1550   *
1551   * For more information on this and similar theme functions, check out
1552   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1553   * Conditional Tags} article in the Theme Developer Handbook.
1554   *
1555   * @since 3.0.0
1556   *
1557   * @see get_post_type_object()
1558   *
1559   * @param string $post_type Post type name.
1560   * @return bool Whether post type is registered.
1561   */
1562  function post_type_exists( $post_type ) {
1563      return (bool) get_post_type_object( $post_type );
1564  }
1565  
1566  /**
1567   * Retrieves the post type of the current post or of a given post.
1568   *
1569   * @since 2.1.0
1570   *
1571   * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post.
1572   * @return string|false          Post type on success, false on failure.
1573   */
1574  function get_post_type( $post = null ) {
1575      $post = get_post( $post );
1576      if ( $post ) {
1577          return $post->post_type;
1578      }
1579  
1580      return false;
1581  }
1582  
1583  /**
1584   * Retrieves a post type object by name.
1585   *
1586   * @since 3.0.0
1587   * @since 4.6.0 Object returned is now an instance of `WP_Post_Type`.
1588   *
1589   * @global array $wp_post_types List of post types.
1590   *
1591   * @see register_post_type()
1592   *
1593   * @param string $post_type The name of a registered post type.
1594   * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise.
1595   */
1596  function get_post_type_object( $post_type ) {
1597      global $wp_post_types;
1598  
1599      if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) {
1600          return null;
1601      }
1602  
1603      return $wp_post_types[ $post_type ];
1604  }
1605  
1606  /**
1607   * Gets a list of all registered post type objects.
1608   *
1609   * @since 2.9.0
1610   *
1611   * @global array $wp_post_types List of post types.
1612   *
1613   * @see register_post_type() for accepted arguments.
1614   *
1615   * @param array|string $args     Optional. An array of key => value arguments to match against
1616   *                               the post type objects. Default empty array.
1617   * @param string       $output   Optional. The type of output to return. Either 'names'
1618   *                               or 'objects'. Default 'names'.
1619   * @param string       $operator Optional. The logical operation to perform. 'or' means only one
1620   *                               element from the array needs to match; 'and' means all elements
1621   *                               must match; 'not' means no elements may match. Default 'and'.
1622   * @return string[]|WP_Post_Type[] An array of post type names or objects.
1623   */
1624  function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
1625      global $wp_post_types;
1626  
1627      $field = ( 'names' === $output ) ? 'name' : false;
1628  
1629      return wp_filter_object_list( $wp_post_types, $args, $operator, $field );
1630  }
1631  
1632  /**
1633   * Registers a post type.
1634   *
1635   * Note: Post type registrations should not be hooked before the
1636   * {@see 'init'} action. Also, any taxonomy connections should be
1637   * registered via the `$taxonomies` argument to ensure consistency
1638   * when hooks such as {@see 'parse_query'} or {@see 'pre_get_posts'}
1639   * are used.
1640   *
1641   * Post types can support any number of built-in core features such
1642   * as meta boxes, custom fields, post thumbnails, post statuses,
1643   * comments, and more. See the `$supports` argument for a complete
1644   * list of supported features.
1645   *
1646   * @since 2.9.0
1647   * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen.
1648   * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing
1649   *              screen and post editing screen.
1650   * @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`.
1651   * @since 4.7.0 Introduced `show_in_rest`, `rest_base` and `rest_controller_class`
1652   *              arguments to register the post type in REST API.
1653   * @since 5.0.0 The `template` and `template_lock` arguments were added.
1654   * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature.
1655   * @since 5.9.0 The `rest_namespace` argument was added.
1656   *
1657   * @global array $wp_post_types List of post types.
1658   *
1659   * @param string       $post_type Post type key. Must not exceed 20 characters and may only contain
1660   *                                lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().
1661   * @param array|string $args {
1662   *     Array or string of arguments for registering a post type.
1663   *
1664   *     @type string       $label                           Name of the post type shown in the menu. Usually plural.
1665   *                                                         Default is value of $labels['name'].
1666   *     @type string[]     $labels                          An array of labels for this post type. If not set, post
1667   *                                                         labels are inherited for non-hierarchical types and page
1668   *                                                         labels for hierarchical ones. See get_post_type_labels() for a full
1669   *                                                         list of supported labels.
1670   *     @type string       $description                     A short descriptive summary of what the post type is.
1671   *                                                         Default empty.
1672   *     @type bool         $public                          Whether a post type is intended for use publicly either via
1673   *                                                         the admin interface or by front-end users. While the default
1674   *                                                         settings of $exclude_from_search, $publicly_queryable, $show_ui,
1675   *                                                         and $show_in_nav_menus are inherited from $public, each does not
1676   *                                                         rely on this relationship and controls a very specific intention.
1677   *                                                         Default false.
1678   *     @type bool         $hierarchical                    Whether the post type is hierarchical (e.g. page). Default false.
1679   *     @type bool         $exclude_from_search             Whether to exclude posts with this post type from front end search
1680   *                                                         results. Default is the opposite value of $public.
1681   *     @type bool         $publicly_queryable              Whether queries can be performed on the front end for the post type
1682   *                                                         as part of parse_request(). Endpoints would include:
1683   *                                                          * ?post_type={post_type_key}
1684   *                                                          * ?{post_type_key}={single_post_slug}
1685   *                                                          * ?{post_type_query_var}={single_post_slug}
1686   *                                                         If not set, the default is inherited from $public.
1687   *     @type bool         $show_ui                         Whether to generate and allow a UI for managing this post type in the
1688   *                                                         admin. Default is value of $public.
1689   *     @type bool|string  $show_in_menu                    Where to show the post type in the admin menu. To work, $show_ui
1690   *                                                         must be true. If true, the post type is shown in its own top level
1691   *                                                         menu. If false, no menu is shown. If a string of an existing top
1692   *                                                         level menu ('tools.php' or 'edit.php?post_type=page', for example), the
1693   *                                                         post type will be placed as a sub-menu of that.
1694   *                                                         Default is value of $show_ui.
1695   *     @type bool         $show_in_nav_menus               Makes this post type available for selection in navigation menus.
1696   *                                                         Default is value of $public.
1697   *     @type bool         $show_in_admin_bar               Makes this post type available via the admin bar. Default is value
1698   *                                                         of $show_in_menu.
1699   *     @type bool         $show_in_rest                    Whether to include the post type in the REST API. Set this to true
1700   *                                                         for the post type to be available in the block editor.
1701   *     @type string       $rest_base                       To change the base URL of REST API route. Default is $post_type.
1702   *     @type string       $rest_namespace                  To change the namespace URL of REST API route. Default is wp/v2.
1703   *     @type string       $rest_controller_class           REST API controller class name. Default is 'WP_REST_Posts_Controller'.
1704   *     @type string|bool  $autosave_rest_controller_class  REST API controller class name. Default is 'WP_REST_Autosaves_Controller'.
1705   *     @type string|bool  $revisions_rest_controller_class REST API controller class name. Default is 'WP_REST_Revisions_Controller'.
1706   *     @type bool         $late_route_registration         A flag to direct the REST API controllers for autosave / revisions
1707   *                                                         should be registered before/after the post type controller.
1708   *     @type int          $menu_position                   The position in the menu order the post type should appear. To work,
1709   *                                                         $show_in_menu must be true. Default null (at the bottom).
1710   *     @type string       $menu_icon                       The URL to the icon to be used for this menu. Pass a base64-encoded
1711   *                                                         SVG using a data URI, which will be colored to match the color scheme
1712   *                                                         -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
1713   *                                                         of a Dashicons helper class to use a font icon, e.g.
1714   *                                                        'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
1715   *                                                         so an icon can be added via CSS. Defaults to use the posts icon.
1716   *     @type string|array $capability_type                 The string to use to build the read, edit, and delete capabilities.
1717   *                                                         May be passed as an array to allow for alternative plurals when using
1718   *                                                         this argument as a base to construct the capabilities, e.g.
1719   *                                                         array('story', 'stories'). Default 'post'.
1720   *     @type string[]     $capabilities                    Array of capabilities for this post type. $capability_type is used
1721   *                                                         as a base to construct capabilities by default.
1722   *                                                         See get_post_type_capabilities().
1723   *     @type bool         $map_meta_cap                    Whether to use the internal default meta capability handling.
1724   *                                                         Default false.
1725   *     @type array|false  $supports                        Core feature(s) the post type supports. Serves as an alias for calling
1726   *                                                         add_post_type_support() directly. Core features include 'title',
1727   *                                                         'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',
1728   *                                                         'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.
1729   *                                                         Additionally, the 'revisions' feature dictates whether the post type
1730   *                                                         will store revisions, the 'autosave' feature dictates whether the post type
1731   *                                                         will be autosaved, and the 'comments' feature dictates whether the
1732   *                                                         comments count will show on the edit screen. For backward compatibility reasons,
1733   *                                                         adding 'editor' support implies 'autosave' support too. A feature can also be
1734   *                                                         specified as an array of arguments to provide additional information
1735   *                                                         about supporting that feature.
1736   *                                                         Example: `array( 'my_feature', array( 'field' => 'value' ) )`.
1737   *                                                         If false, no features will be added.
1738   *                                                         Default is an array containing 'title' and 'editor'.
1739   *     @type callable     $register_meta_box_cb            Provide a callback function that sets up the meta boxes for the
1740   *                                                         edit form. Do remove_meta_box() and add_meta_box() calls in the
1741   *                                                         callback. Default null.
1742   *     @type string[]     $taxonomies                      An array of taxonomy identifiers that will be registered for the
1743   *                                                         post type. Taxonomies can be registered later with register_taxonomy()
1744   *                                                         or register_taxonomy_for_object_type().
1745   *                                                         Default empty array.
1746   *     @type bool|string  $has_archive                     Whether there should be post type archives, or if a string, the
1747   *                                                         archive slug to use. Will generate the proper rewrite rules if
1748   *                                                         $rewrite is enabled. Default false.
1749   *     @type bool|array   $rewrite                         {
1750   *         Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
1751   *         Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
1752   *         passed with any of these keys:
1753   *
1754   *         @type string $slug       Customize the permastruct slug. Defaults to $post_type key.
1755   *         @type bool   $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
1756   *                                  Default true.
1757   *         @type bool   $feeds      Whether the feed permastruct should be built for this post type.
1758   *                                  Default is value of $has_archive.
1759   *         @type bool   $pages      Whether the permastruct should provide for pagination. Default true.
1760   *         @type int    $ep_mask    Endpoint mask to assign. If not specified and permalink_epmask is set,
1761   *                                  inherits from $permalink_epmask. If not specified and permalink_epmask
1762   *                                  is not set, defaults to EP_PERMALINK.
1763   *     }
1764   *     @type string|bool  $query_var                      Sets the query_var key for this post type. Defaults to $post_type
1765   *                                                        key. If false, a post type cannot be loaded at
1766   *                                                        ?{query_var}={post_slug}. If specified as a string, the query
1767   *                                                        ?{query_var_string}={post_slug} will be valid.
1768   *     @type bool         $can_export                     Whether to allow this post type to be exported. Default true.
1769   *     @type bool         $delete_with_user               Whether to delete posts of this type when deleting a user.
1770   *                                                          * If true, posts of this type belonging to the user will be moved
1771   *                                                            to Trash when the user is deleted.
1772   *                                                          * If false, posts of this type belonging to the user will *not*
1773   *                                                            be trashed or deleted.
1774   *                                                          * If not set (the default), posts are trashed if post type supports
1775   *                                                            the 'author' feature. Otherwise posts are not trashed or deleted.
1776   *                                                        Default null.
1777   *     @type array        $template                       Array of blocks to use as the default initial state for an editor
1778   *                                                        session. Each item should be an array containing block name and
1779   *                                                        optional attributes. Default empty array.
1780   *     @type string|false $template_lock                  Whether the block template should be locked if $template is set.
1781   *                                                        * If set to 'all', the user is unable to insert new blocks,
1782   *                                                          move existing blocks and delete blocks.
1783   *                                                       * If set to 'insert', the user is able to move existing blocks
1784   *                                                         but is unable to insert new blocks and delete blocks.
1785   *                                                         Default false.
1786   *     @type bool         $_builtin                     FOR INTERNAL USE ONLY! True if this post type is a native or
1787   *                                                      "built-in" post_type. Default false.
1788   *     @type string       $_edit_link                   FOR INTERNAL USE ONLY! URL segment to use for edit link of
1789   *                                                      this post type. Default 'post.php?post=%d'.
1790   * }
1791   * @return WP_Post_Type|WP_Error The registered post type object on success,
1792   *                               WP_Error object on failure.
1793   */
1794  function register_post_type( $post_type, $args = array() ) {
1795      global $wp_post_types;
1796  
1797      if ( ! is_array( $wp_post_types ) ) {
1798          $wp_post_types = array();
1799      }
1800  
1801      // Sanitize post type name.
1802      $post_type = sanitize_key( $post_type );
1803  
1804      if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
1805          _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' );
1806          return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
1807      }
1808  
1809      $post_type_object = new WP_Post_Type( $post_type, $args );
1810      $post_type_object->add_supports();
1811      $post_type_object->add_rewrite_rules();
1812      $post_type_object->register_meta_boxes();
1813  
1814      $wp_post_types[ $post_type ] = $post_type_object;
1815  
1816      $post_type_object->add_hooks();
1817      $post_type_object->register_taxonomies();
1818  
1819      /**
1820       * Fires after a post type is registered.
1821       *
1822       * @since 3.3.0
1823       * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
1824       *
1825       * @param string       $post_type        Post type.
1826       * @param WP_Post_Type $post_type_object Arguments used to register the post type.
1827       */
1828      do_action( 'registered_post_type', $post_type, $post_type_object );
1829  
1830      /**
1831       * Fires after a specific post type is registered.
1832       *
1833       * The dynamic portion of the filter name, `$post_type`, refers to the post type key.
1834       *
1835       * Possible hook names include:
1836       *
1837       *  - `registered_post_type_post`
1838       *  - `registered_post_type_page`
1839       *
1840       * @since 6.0.0
1841       *
1842       * @param string       $post_type        Post type.
1843       * @param WP_Post_Type $post_type_object Arguments used to register the post type.
1844       */
1845      do_action( "registered_post_type_{$post_type}", $post_type, $post_type_object );
1846  
1847      return $post_type_object;
1848  }
1849  
1850  /**
1851   * Unregisters a post type.
1852   *
1853   * Cannot be used to unregister built-in post types.
1854   *
1855   * @since 4.5.0
1856   *
1857   * @global array $wp_post_types List of post types.
1858   *
1859   * @param string $post_type Post type to unregister.
1860   * @return true|WP_Error True on success, WP_Error on failure or if the post type doesn't exist.
1861   */
1862  function unregister_post_type( $post_type ) {
1863      global $wp_post_types;
1864  
1865      if ( ! post_type_exists( $post_type ) ) {
1866          return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) );
1867      }
1868  
1869      $post_type_object = get_post_type_object( $post_type );
1870  
1871      // Do not allow unregistering internal post types.
1872      if ( $post_type_object->_builtin ) {
1873          return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) );
1874      }
1875  
1876      $post_type_object->remove_supports();
1877      $post_type_object->remove_rewrite_rules();
1878      $post_type_object->unregister_meta_boxes();
1879      $post_type_object->remove_hooks();
1880      $post_type_object->unregister_taxonomies();
1881  
1882      unset( $wp_post_types[ $post_type ] );
1883  
1884      /**
1885       * Fires after a post type was unregistered.
1886       *
1887       * @since 4.5.0
1888       *
1889       * @param string $post_type Post type key.
1890       */
1891      do_action( 'unregistered_post_type', $post_type );
1892  
1893      return true;
1894  }
1895  
1896  /**
1897   * Builds an object with all post type capabilities out of a post type object
1898   *
1899   * Post type capabilities use the 'capability_type' argument as a base, if the
1900   * capability is not set in the 'capabilities' argument array or if the
1901   * 'capabilities' argument is not supplied.
1902   *
1903   * The capability_type argument can optionally be registered as an array, with
1904   * the first value being singular and the second plural, e.g. array('story, 'stories')
1905   * Otherwise, an 's' will be added to the value for the plural form. After
1906   * registration, capability_type will always be a string of the singular value.
1907   *
1908   * By default, eight keys are accepted as part of the capabilities array:
1909   *
1910   * - edit_post, read_post, and delete_post are meta capabilities, which are then
1911   *   generally mapped to corresponding primitive capabilities depending on the
1912   *   context, which would be the post being edited/read/deleted and the user or
1913   *   role being checked. Thus these capabilities would generally not be granted
1914   *   directly to users or roles.
1915   *
1916   * - edit_posts - Controls whether objects of this post type can be edited.
1917   * - edit_others_posts - Controls whether objects of this type owned by other users
1918   *   can be edited. If the post type does not support an author, then this will
1919   *   behave like edit_posts.
1920   * - delete_posts - Controls whether objects of this post type can be deleted.
1921   * - publish_posts - Controls publishing objects of this post type.
1922   * - read_private_posts - Controls whether private objects can be read.
1923   *
1924   * These five primitive capabilities are checked in core in various locations.
1925   * There are also six other primitive capabilities which are not referenced
1926   * directly in core, except in map_meta_cap(), which takes the three aforementioned
1927   * meta capabilities and translates them into one or more primitive capabilities
1928   * that must then be checked against the user or role, depending on the context.
1929   *
1930   * - read - Controls whether objects of this post type can be read.
1931   * - delete_private_posts - Controls whether private objects can be deleted.
1932   * - delete_published_posts - Controls whether published objects can be deleted.
1933   * - delete_others_posts - Controls whether objects owned by other users can be
1934   *   can be deleted. If the post type does not support an author, then this will
1935   *   behave like delete_posts.
1936   * - edit_private_posts - Controls whether private objects can be edited.
1937   * - edit_published_posts - Controls whether published objects can be edited.
1938   *
1939   * These additional capabilities are only used in map_meta_cap(). Thus, they are
1940   * only assigned by default if the post type is registered with the 'map_meta_cap'
1941   * argument set to true (default is false).
1942   *
1943   * @since 3.0.0
1944   * @since 5.4.0 'delete_posts' is included in default capabilities.
1945   *
1946   * @see register_post_type()
1947   * @see map_meta_cap()
1948   *
1949   * @param object $args Post type registration arguments.
1950   * @return object Object with all the capabilities as member variables.
1951   */
1952  function get_post_type_capabilities( $args ) {
1953      if ( ! is_array( $args->capability_type ) ) {
1954          $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
1955      }
1956  
1957      // Singular base for meta capabilities, plural base for primitive capabilities.
1958      list( $singular_base, $plural_base ) = $args->capability_type;
1959  
1960      $default_capabilities = array(
1961          // Meta capabilities.
1962          'edit_post'          => 'edit_' . $singular_base,
1963          'read_post'          => 'read_' . $singular_base,
1964          'delete_post'        => 'delete_' . $singular_base,
1965          // Primitive capabilities used outside of map_meta_cap():
1966          'edit_posts'         => 'edit_' . $plural_base,
1967          'edit_others_posts'  => 'edit_others_' . $plural_base,
1968          'delete_posts'       => 'delete_' . $plural_base,
1969          'publish_posts'      => 'publish_' . $plural_base,
1970          'read_private_posts' => 'read_private_' . $plural_base,
1971      );
1972  
1973      // Primitive capabilities used within map_meta_cap():
1974      if ( $args->map_meta_cap ) {
1975          $default_capabilities_for_mapping = array(
1976              'read'                   => 'read',
1977              'delete_private_posts'   => 'delete_private_' . $plural_base,
1978              'delete_published_posts' => 'delete_published_' . $plural_base,
1979              'delete_others_posts'    => 'delete_others_' . $plural_base,
1980              'edit_private_posts'     => 'edit_private_' . $plural_base,
1981              'edit_published_posts'   => 'edit_published_' . $plural_base,
1982          );
1983          $default_capabilities             = array_merge( $default_capabilities, $default_capabilities_for_mapping );
1984      }
1985  
1986      $capabilities = array_merge( $default_capabilities, $args->capabilities );
1987  
1988      // Post creation capability simply maps to edit_posts by default:
1989      if ( ! isset( $capabilities['create_posts'] ) ) {
1990          $capabilities['create_posts'] = $capabilities['edit_posts'];
1991      }
1992  
1993      // Remember meta capabilities for future reference.
1994      if ( $args->map_meta_cap ) {
1995          _post_type_meta_capabilities( $capabilities );
1996      }
1997  
1998      return (object) $capabilities;
1999  }
2000  
2001  /**
2002   * Stores or returns a list of post type meta caps for map_meta_cap().
2003   *
2004   * @since 3.1.0
2005   * @access private
2006   *
2007   * @global array $post_type_meta_caps Used to store meta capabilities.
2008   *
2009   * @param string[] $capabilities Post type meta capabilities.
2010   */
2011  function _post_type_meta_capabilities( $capabilities = null ) {
2012      global $post_type_meta_caps;
2013  
2014      foreach ( $capabilities as $core => $custom ) {
2015          if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) {
2016              $post_type_meta_caps[ $custom ] = $core;
2017          }
2018      }
2019  }
2020  
2021  /**
2022   * Builds an object with all post type labels out of a post type object.
2023   *
2024   * Accepted keys of the label array in the post type object:
2025   *
2026   * - `name` - General name for the post type, usually plural. The same and overridden
2027   *          by `$post_type_object->label`. Default is 'Posts' / 'Pages'.
2028   * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'.
2029   * - `add_new` - Label for adding a new item. Default is 'Add New' / 'Add New'.
2030   * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'.
2031   * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'.
2032   * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'.
2033   * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'.
2034   * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'.
2035   * - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'.
2036   * - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'.
2037   * - `not_found_in_trash` - Label used when no items are in the Trash. Default is 'No posts found in Trash' /
2038   *                        'No pages found in Trash'.
2039   * - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical
2040   *                       post types. Default is 'Parent Page:'.
2041   * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'.
2042   * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'.
2043   * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'.
2044   * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'.
2045   * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' /
2046   *                           'Uploaded to this page'.
2047   * - `featured_image` - Label for the featured image meta box title. Default is 'Featured image'.
2048   * - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'.
2049   * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'.
2050   * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'.
2051   * - `menu_name` - Label for the menu name. Default is the same as `name`.
2052   * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' /
2053   *                       'Filter pages list'.
2054   * - `filter_by_date` - Label for the date filter in list tables. Default is 'Filter by date'.
2055   * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' /
2056   *                           'Pages list navigation'.
2057   * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'.
2058   * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.'
2059   * - `item_published_privately` - Label used when an item is published with private visibility.
2060   *                              Default is 'Post published privately.' / 'Page published privately.'
2061   * - `item_reverted_to_draft` - Label used when an item is switched to a draft.
2062   *                            Default is 'Post reverted to draft.' / 'Page reverted to draft.'
2063   * - `item_trashed` - Label used when an item is moved to Trash. Default is 'Post trashed.' / 'Page trashed.'
2064   * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' /
2065   *                    'Page scheduled.'
2066   * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.'
2067   * - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'.
2068   * - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' /
2069   *                             'A link to a page.'
2070   *
2071   * Above, the first default value is for non-hierarchical post types (like posts)
2072   * and the second one is for hierarchical post types (like pages).
2073   *
2074   * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter.
2075   *
2076   * @since 3.0.0
2077   * @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`,
2078   *              and `use_featured_image` labels.
2079   * @since 4.4.0 Added the `archives`, `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`,
2080   *              `items_list_navigation`, and `items_list` labels.
2081   * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
2082   * @since 4.7.0 Added the `view_items` and `attributes` labels.
2083   * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`,
2084   *              `item_scheduled`, and `item_updated` labels.
2085   * @since 5.7.0 Added the `filter_by_date` label.
2086   * @since 5.8.0 Added the `item_link` and `item_link_description` labels.
2087   * @since 6.3.0 Added the `item_trashed` label.
2088   * @since 6.4.0 Changed default values for the `add_new` label to include the type of content.
2089   *              This matches `add_new_item` and provides more context for better accessibility.
2090   * @since 6.6.0 Added the `template_name` label.
2091   * @since 6.7.0 Restored pre-6.4.0 defaults for the `add_new` label and updated documentation.
2092   *              Updated core usage to reference `add_new_item`.
2093   *
2094   * @access private
2095   *
2096   * @param object|WP_Post_Type $post_type_object Post type object.
2097   * @return object Object with all the labels as member variables.
2098   */
2099  function get_post_type_labels( $post_type_object ) {
2100      $nohier_vs_hier_defaults = WP_Post_Type::get_default_labels();
2101  
2102      $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
2103  
2104      $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
2105  
2106      if ( ! isset( $post_type_object->labels->template_name ) && isset( $post_type_object->labels->singular_name ) ) {
2107              /* translators: %s: Post type name. */
2108              $labels->template_name = sprintf( __( 'Single item: %s' ), $post_type_object->labels->singular_name );
2109      }
2110  
2111      $post_type = $post_type_object->name;
2112  
2113      $default_labels = clone $labels;
2114  
2115      /**
2116       * Filters the labels of a specific post type.
2117       *
2118       * The dynamic portion of the hook name, `$post_type`, refers to
2119       * the post type slug.
2120       *
2121       * Possible hook names include:
2122       *
2123       *  - `post_type_labels_post`
2124       *  - `post_type_labels_page`
2125       *  - `post_type_labels_attachment`
2126       *
2127       * @since 3.5.0
2128       *
2129       * @see get_post_type_labels() for the full list of labels.
2130       *
2131       * @param object $labels Object with labels for the post type as member variables.
2132       */
2133      $labels = apply_filters( "post_type_labels_{$post_type}", $labels );
2134  
2135      // Ensure that the filtered labels contain all required default values.
2136      $labels = (object) array_merge( (array) $default_labels, (array) $labels );
2137  
2138      return $labels;
2139  }
2140  
2141  /**
2142   * Builds an object with custom-something object (post type, taxonomy) labels
2143   * out of a custom-something object
2144   *
2145   * @since 3.0.0
2146   * @access private
2147   *
2148   * @param object $data_object             A custom-something object.
2149   * @param array  $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels.
2150   * @return object Object containing labels for the given custom-something object.
2151   */
2152  function _get_custom_object_labels( $data_object, $nohier_vs_hier_defaults ) {
2153      $data_object->labels = (array) $data_object->labels;
2154  
2155      if ( isset( $data_object->label ) && empty( $data_object->labels['name'] ) ) {
2156          $data_object->labels['name'] = $data_object->label;
2157      }
2158  
2159      if ( ! isset( $data_object->labels['singular_name'] ) && isset( $data_object->labels['name'] ) ) {
2160          $data_object->labels['singular_name'] = $data_object->labels['name'];
2161      }
2162  
2163      if ( ! isset( $data_object->labels['name_admin_bar'] ) ) {
2164          $data_object->labels['name_admin_bar'] =
2165              isset( $data_object->labels['singular_name'] )
2166              ? $data_object->labels['singular_name']
2167              : $data_object->name;
2168      }
2169  
2170      if ( ! isset( $data_object->labels['menu_name'] ) && isset( $data_object->labels['name'] ) ) {
2171          $data_object->labels['menu_name'] = $data_object->labels['name'];
2172      }
2173  
2174      if ( ! isset( $data_object->labels['all_items'] ) && isset( $data_object->labels['menu_name'] ) ) {
2175          $data_object->labels['all_items'] = $data_object->labels['menu_name'];
2176      }
2177  
2178      if ( ! isset( $data_object->labels['archives'] ) && isset( $data_object->labels['all_items'] ) ) {
2179          $data_object->labels['archives'] = $data_object->labels['all_items'];
2180      }
2181  
2182      $defaults = array();
2183      foreach ( $nohier_vs_hier_defaults as $key => $value ) {
2184          $defaults[ $key ] = $data_object->hierarchical ? $value[1] : $value[0];
2185      }
2186  
2187      $labels              = array_merge( $defaults, $data_object->labels );
2188      $data_object->labels = (object) $data_object->labels;
2189  
2190      return (object) $labels;
2191  }
2192  
2193  /**
2194   * Adds submenus for post types.
2195   *
2196   * @access private
2197   * @since 3.1.0
2198   */
2199  function _add_post_type_submenus() {
2200      foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
2201          $ptype_obj = get_post_type_object( $ptype );
2202          // Sub-menus only.
2203          if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) {
2204              continue;
2205          }
2206          add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
2207      }
2208  }
2209  
2210  /**
2211   * Registers support of certain features for a post type.
2212   *
2213   * All core features are directly associated with a functional area of the edit
2214   * screen, such as the editor or a meta box. Features include: 'title', 'editor',
2215   * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',
2216   * 'thumbnail', 'custom-fields', and 'post-formats'.
2217   *
2218   * Additionally, the 'revisions' feature dictates whether the post type will
2219   * store revisions, the 'autosave' feature dictates whether the post type
2220   * will be autosaved, and the 'comments' feature dictates whether the comments
2221   * count will show on the edit screen.
2222   *
2223   * A third, optional parameter can also be passed along with a feature to provide
2224   * additional information about supporting that feature.
2225   *
2226   * Example usage:
2227   *
2228   *     add_post_type_support( 'my_post_type', 'comments' );
2229   *     add_post_type_support( 'my_post_type', array(
2230   *         'author', 'excerpt',
2231   *     ) );
2232   *     add_post_type_support( 'my_post_type', 'my_feature', array(
2233   *         'field' => 'value',
2234   *     ) );
2235   *
2236   * @since 3.0.0
2237   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
2238   *              by adding it to the function signature.
2239   *
2240   * @global array $_wp_post_type_features
2241   *
2242   * @param string       $post_type The post type for which to add the feature.
2243   * @param string|array $feature   The feature being added, accepts an array of
2244   *                                feature strings or a single string.
2245   * @param mixed        ...$args   Optional extra arguments to pass along with certain features.
2246   */
2247  function add_post_type_support( $post_type, $feature, ...$args ) {
2248      global $_wp_post_type_features;
2249  
2250      $features = (array) $feature;
2251      foreach ( $features as $feature ) {
2252          if ( $args ) {
2253              $_wp_post_type_features[ $post_type ][ $feature ] = $args;
2254          } else {
2255              $_wp_post_type_features[ $post_type ][ $feature ] = true;
2256          }
2257      }
2258  }
2259  
2260  /**
2261   * Removes support for a feature from a post type.
2262   *
2263   * @since 3.0.0
2264   *
2265   * @global array $_wp_post_type_features
2266   *
2267   * @param string $post_type The post type for which to remove the feature.
2268   * @param string $feature   The feature being removed.
2269   */
2270  function remove_post_type_support( $post_type, $feature ) {
2271      global $_wp_post_type_features;
2272  
2273      unset( $_wp_post_type_features[ $post_type ][ $feature ] );
2274  }
2275  
2276  /**
2277   * Gets all the post type features
2278   *
2279   * @since 3.4.0
2280   *
2281   * @global array $_wp_post_type_features
2282   *
2283   * @param string $post_type The post type.
2284   * @return array Post type supports list.
2285   */
2286  function get_all_post_type_supports( $post_type ) {
2287      global $_wp_post_type_features;
2288  
2289      if ( isset( $_wp_post_type_features[ $post_type ] ) ) {
2290          return $_wp_post_type_features[ $post_type ];
2291      }
2292  
2293      return array();
2294  }
2295  
2296  /**
2297   * Checks a post type's support for a given feature.
2298   *
2299   * @since 3.0.0
2300   *
2301   * @global array $_wp_post_type_features
2302   *
2303   * @param string $post_type The post type being checked.
2304   * @param string $feature   The feature being checked.
2305   * @return bool Whether the post type supports the given feature.
2306   */
2307  function post_type_supports( $post_type, $feature ) {
2308      global $_wp_post_type_features;
2309  
2310      return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) );
2311  }
2312  
2313  /**
2314   * Retrieves a list of post type names that support a specific feature.
2315   *
2316   * @since 4.5.0
2317   *
2318   * @global array $_wp_post_type_features Post type features
2319   *
2320   * @param array|string $feature  Single feature or an array of features the post types should support.
2321   * @param string       $operator Optional. The logical operation to perform. 'or' means
2322   *                               only one element from the array needs to match; 'and'
2323   *                               means all elements must match; 'not' means no elements may
2324   *                               match. Default 'and'.
2325   * @return string[] A list of post type names.
2326   */
2327  function get_post_types_by_support( $feature, $operator = 'and' ) {
2328      global $_wp_post_type_features;
2329  
2330      $features = array_fill_keys( (array) $feature, true );
2331  
2332      return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
2333  }
2334  
2335  /**
2336   * Updates the post type for the post ID.
2337   *
2338   * The page or post cache will be cleaned for the post ID.
2339   *
2340   * @since 2.5.0
2341   *
2342   * @global wpdb $wpdb WordPress database abstraction object.
2343   *
2344   * @param int    $post_id   Optional. Post ID to change post type. Default 0.
2345   * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to
2346   *                          name a few. Default 'post'.
2347   * @return int|false Amount of rows changed. Should be 1 for success and 0 for failure.
2348   */
2349  function set_post_type( $post_id = 0, $post_type = 'post' ) {
2350      global $wpdb;
2351  
2352      $post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' );
2353      $return    = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) );
2354  
2355      clean_post_cache( $post_id );
2356  
2357      return $return;
2358  }
2359  
2360  /**
2361   * Determines whether a post type is considered "viewable".
2362   *
2363   * For built-in post types such as posts and pages, the 'public' value will be evaluated.
2364   * For all others, the 'publicly_queryable' value will be used.
2365   *
2366   * @since 4.4.0
2367   * @since 4.5.0 Added the ability to pass a post type name in addition to object.
2368   * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
2369   * @since 5.9.0 Added `is_post_type_viewable` hook to filter the result.
2370   *
2371   * @param string|WP_Post_Type $post_type Post type name or object.
2372   * @return bool Whether the post type should be considered viewable.
2373   */
2374  function is_post_type_viewable( $post_type ) {
2375      if ( is_scalar( $post_type ) ) {
2376          $post_type = get_post_type_object( $post_type );
2377  
2378          if ( ! $post_type ) {
2379              return false;
2380          }
2381      }
2382  
2383      if ( ! is_object( $post_type ) ) {
2384          return false;
2385      }
2386  
2387      $is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
2388  
2389      /**
2390       * Filters whether a post type is considered "viewable".
2391       *
2392       * The returned filtered value must be a boolean type to ensure
2393       * `is_post_type_viewable()` only returns a boolean. This strictness
2394       * is by design to maintain backwards-compatibility and guard against
2395       * potential type errors in PHP 8.1+. Non-boolean values (even falsey
2396       * and truthy values) will result in the function returning false.
2397       *
2398       * @since 5.9.0
2399       *
2400       * @param bool         $is_viewable Whether the post type is "viewable" (strict type).
2401       * @param WP_Post_Type $post_type   Post type object.
2402       */
2403      return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type );
2404  }
2405  
2406  /**
2407   * Determines whether a post status is considered "viewable".
2408   *
2409   * For built-in post statuses such as publish and private, the 'public' value will be evaluated.
2410   * For all others, the 'publicly_queryable' value will be used.
2411   *
2412   * @since 5.7.0
2413   * @since 5.9.0 Added `is_post_status_viewable` hook to filter the result.
2414   *
2415   * @param string|stdClass $post_status Post status name or object.
2416   * @return bool Whether the post status should be considered viewable.
2417   */
2418  function is_post_status_viewable( $post_status ) {
2419      if ( is_scalar( $post_status ) ) {
2420          $post_status = get_post_status_object( $post_status );
2421  
2422          if ( ! $post_status ) {
2423              return false;
2424          }
2425      }
2426  
2427      if (
2428          ! is_object( $post_status ) ||
2429          $post_status->internal ||
2430          $post_status->protected
2431      ) {
2432          return false;
2433      }
2434  
2435      $is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
2436  
2437      /**
2438       * Filters whether a post status is considered "viewable".
2439       *
2440       * The returned filtered value must be a boolean type to ensure
2441       * `is_post_status_viewable()` only returns a boolean. This strictness
2442       * is by design to maintain backwards-compatibility and guard against
2443       * potential type errors in PHP 8.1+. Non-boolean values (even falsey
2444       * and truthy values) will result in the function returning false.
2445       *
2446       * @since 5.9.0
2447       *
2448       * @param bool     $is_viewable Whether the post status is "viewable" (strict type).
2449       * @param stdClass $post_status Post status object.
2450       */
2451      return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status );
2452  }
2453  
2454  /**
2455   * Determines whether a post is publicly viewable.
2456   *
2457   * Posts are considered publicly viewable if both the post status and post type
2458   * are viewable.
2459   *
2460   * @since 5.7.0
2461   *
2462   * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
2463   * @return bool Whether the post is publicly viewable.
2464   */
2465  function is_post_publicly_viewable( $post = null ) {
2466      $post = get_post( $post );
2467  
2468      if ( ! $post ) {
2469          return false;
2470      }
2471  
2472      $post_type   = get_post_type( $post );
2473      $post_status = get_post_status( $post );
2474  
2475      return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
2476  }
2477  
2478  /**
2479   * Determines whether a post is embeddable.
2480   *
2481   * @since 6.8.0
2482   *
2483   * @param int|WP_Post|null $post Optional. Post ID or `WP_Post` object. Defaults to global $post.
2484   * @return bool Whether the post should be considered embeddable.
2485   */
2486  function is_post_embeddable( $post = null ) {
2487      $post = get_post( $post );
2488  
2489      if ( ! $post ) {
2490          return false;
2491      }
2492  
2493      $post_type = get_post_type_object( $post->post_type );
2494  
2495      if ( ! $post_type ) {
2496          return false;
2497      }
2498  
2499      $is_embeddable = $post_type->embeddable;
2500  
2501      /**
2502       * Filter whether a post is embeddable.
2503       *
2504       * @since 6.8.0
2505       *
2506       * @param bool    $is_embeddable Whether the post is embeddable.
2507       * @param WP_Post $post          Post object.
2508       */
2509      return apply_filters( 'is_post_embeddable', $is_embeddable, $post );
2510  }
2511  
2512  /**
2513   * Retrieves an array of the latest posts, or posts matching the given criteria.
2514   *
2515   * For more information on the accepted arguments, see the
2516   * {@link https://developer.wordpress.org/reference/classes/wp_query/
2517   * WP_Query} documentation in the Developer Handbook.
2518   *
2519   * The `$ignore_sticky_posts` and `$no_found_rows` arguments are ignored by
2520   * this function and both are set to `true`.
2521   *
2522   * The defaults are as follows:
2523   *
2524   * @since 1.2.0
2525   *
2526   * @see WP_Query
2527   * @see WP_Query::parse_query()
2528   *
2529   * @param array $args {
2530   *     Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.
2531   *
2532   *     @type int        $numberposts      Total number of posts to retrieve. Is an alias of `$posts_per_page`
2533   *                                        in WP_Query. Accepts -1 for all. Default 5.
2534   *     @type int|string $category         Category ID or comma-separated list of IDs (this or any children).
2535   *                                        Is an alias of `$cat` in WP_Query. Default 0.
2536   *     @type int[]      $include          An array of post IDs to retrieve, sticky posts will be included.
2537   *                                        Is an alias of `$post__in` in WP_Query. Default empty array.
2538   *     @type int[]      $exclude          An array of post IDs not to retrieve. Default empty array.
2539   *     @type bool       $suppress_filters Whether to suppress filters. Default true.
2540   * }
2541   * @return WP_Post[]|int[] Array of post objects or post IDs.
2542   */
2543  function get_posts( $args = null ) {
2544      $defaults = array(
2545          'numberposts'      => 5,
2546          'category'         => 0,
2547          'orderby'          => 'date',
2548          'order'            => 'DESC',
2549          'include'          => array(),
2550          'exclude'          => array(),
2551          'meta_key'         => '',
2552          'meta_value'       => '',
2553          'post_type'        => 'post',
2554          'suppress_filters' => true,
2555      );
2556  
2557      $parsed_args = wp_parse_args( $args, $defaults );
2558      if ( empty( $parsed_args['post_status'] ) ) {
2559          $parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
2560      }
2561      if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
2562          $parsed_args['posts_per_page'] = $parsed_args['numberposts'];
2563      }
2564      if ( ! empty( $parsed_args['category'] ) ) {
2565          $parsed_args['cat'] = $parsed_args['category'];
2566      }
2567      if ( ! empty( $parsed_args['include'] ) ) {
2568          $incposts                      = wp_parse_id_list( $parsed_args['include'] );
2569          $parsed_args['posts_per_page'] = count( $incposts );  // Only the number of posts included.
2570          $parsed_args['post__in']       = $incposts;
2571      } elseif ( ! empty( $parsed_args['exclude'] ) ) {
2572          $parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
2573      }
2574  
2575      $parsed_args['ignore_sticky_posts'] = true;
2576      $parsed_args['no_found_rows']       = true;
2577  
2578      $get_posts = new WP_Query();
2579      return $get_posts->query( $parsed_args );
2580  }
2581  
2582  //
2583  // Post meta functions.
2584  //
2585  
2586  /**
2587   * Adds a meta field to the given post.
2588   *
2589   * Post meta data is called "Custom Fields" on the Administration Screen.
2590   *
2591   * @since 1.5.0
2592   *
2593   * @param int    $post_id    Post ID.
2594   * @param string $meta_key   Metadata name.
2595   * @param mixed  $meta_value Metadata value. Arrays and objects are stored as serialized data and
2596   *                           will be returned as the same type when retrieved. Other data types will
2597   *                           be stored as strings in the database:
2598   *                           - false is stored and retrieved as an empty string ('')
2599   *                           - true is stored and retrieved as '1'
2600   *                           - numbers (both integer and float) are stored and retrieved as strings
2601   *                           Must be serializable if non-scalar.
2602   * @param bool   $unique     Optional. Whether the same key should not be added.
2603   *                           Default false.
2604   * @return int|false Meta ID on success, false on failure.
2605   */
2606  function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
2607      // Make sure meta is added to the post, not a revision.
2608      $the_post = wp_is_post_revision( $post_id );
2609      if ( $the_post ) {
2610          $post_id = $the_post;
2611      }
2612  
2613      return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
2614  }
2615  
2616  /**
2617   * Deletes a post meta field for the given post ID.
2618   *
2619   * You can match based on the key, or key and value. Removing based on key and
2620   * value, will keep from removing duplicate metadata with the same key. It also
2621   * allows removing all metadata matching the key, if needed.
2622   *
2623   * @since 1.5.0
2624   *
2625   * @param int    $post_id    Post ID.
2626   * @param string $meta_key   Metadata name.
2627   * @param mixed  $meta_value Optional. Metadata value. If provided,
2628   *                           rows will only be removed that match the value.
2629   *                           Must be serializable if non-scalar. Default empty.
2630   * @return bool True on success, false on failure.
2631   */
2632  function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
2633      // Make sure meta is deleted from the post, not from a revision.
2634      $the_post = wp_is_post_revision( $post_id );
2635      if ( $the_post ) {
2636          $post_id = $the_post;
2637      }
2638  
2639      return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
2640  }
2641  
2642  /**
2643   * Retrieves a post meta field for the given post ID.
2644   *
2645   * @since 1.5.0
2646   *
2647   * @param int    $post_id Post ID.
2648   * @param string $key     Optional. The meta key to retrieve. By default,
2649   *                        returns data for all keys. Default empty.
2650   * @param bool   $single  Optional. Whether to return a single value.
2651   *                        This parameter has no effect if `$key` is not specified.
2652   *                        Default false.
2653   * @return mixed An array of values if `$single` is false.
2654   *               The value of the meta field if `$single` is true.
2655   *               False for an invalid `$post_id` (non-numeric, zero, or negative value).
2656   *               An empty array if a valid but non-existing post ID is passed and `$single` is false.
2657   *               An empty string if a valid but non-existing post ID is passed and `$single` is true.
2658   *               Note: Non-serialized values are returned as strings:
2659   *               - false values are returned as empty strings ('')
2660   *               - true values are returned as '1'
2661   *               - numbers (both integer and float) are returned as strings
2662   *               Arrays and objects retain their original type.
2663   */
2664  function get_post_meta( $post_id, $key = '', $single = false ) {
2665      return get_metadata( 'post', $post_id, $key, $single );
2666  }
2667  
2668  /**
2669   * Updates a post meta field based on the given post ID.
2670   *
2671   * Use the `$prev_value` parameter to differentiate between meta fields with the
2672   * same key and post ID.
2673   *
2674   * If the meta field for the post does not exist, it will be added and its ID returned.
2675   *
2676   * Can be used in place of add_post_meta().
2677   *
2678   * @since 1.5.0
2679   *
2680   * @param int    $post_id    Post ID.
2681   * @param string $meta_key   Metadata key.
2682   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
2683   * @param mixed  $prev_value Optional. Previous value to check before updating.
2684   *                           If specified, only update existing metadata entries with
2685   *                           this value. Otherwise, update all entries. Default empty.
2686   * @return int|bool Meta ID if the key didn't exist, true on successful update,
2687   *                  false on failure or if the value passed to the function
2688   *                  is the same as the one that is already in the database.
2689   */
2690  function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
2691      // Make sure meta is updated for the post, not for a revision.
2692      $the_post = wp_is_post_revision( $post_id );
2693      if ( $the_post ) {
2694          $post_id = $the_post;
2695      }
2696  
2697      return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
2698  }
2699  
2700  /**
2701   * Deletes everything from post meta matching the given meta key.
2702   *
2703   * @since 2.3.0
2704   *
2705   * @param string $post_meta_key Key to search for when deleting.
2706   * @return bool Whether the post meta key was deleted from the database.
2707   */
2708  function delete_post_meta_by_key( $post_meta_key ) {
2709      return delete_metadata( 'post', null, $post_meta_key, '', true );
2710  }
2711  
2712  /**
2713   * Registers a meta key for posts.
2714   *
2715   * @since 4.9.8
2716   *
2717   * @param string $post_type Post type to register a meta key for. Pass an empty string
2718   *                          to register the meta key across all existing post types.
2719   * @param string $meta_key  The meta key to register.
2720   * @param array  $args      Data used to describe the meta key when registered. See
2721   *                          {@see register_meta()} for a list of supported arguments.
2722   * @return bool True if the meta key was successfully registered, false if not.
2723   */
2724  function register_post_meta( $post_type, $meta_key, array $args ) {
2725      $args['object_subtype'] = $post_type;
2726  
2727      return register_meta( 'post', $meta_key, $args );
2728  }
2729  
2730  /**
2731   * Unregisters a meta key for posts.
2732   *
2733   * @since 4.9.8
2734   *
2735   * @param string $post_type Post type the meta key is currently registered for. Pass
2736   *                          an empty string if the meta key is registered across all
2737   *                          existing post types.
2738   * @param string $meta_key  The meta key to unregister.
2739   * @return bool True on success, false if the meta key was not previously registered.
2740   */
2741  function unregister_post_meta( $post_type, $meta_key ) {
2742      return unregister_meta_key( 'post', $meta_key, $post_type );
2743  }
2744  
2745  /**
2746   * Retrieves post meta fields, based on post ID.
2747   *
2748   * The post meta fields are retrieved from the cache where possible,
2749   * so the function is optimized to be called more than once.
2750   *
2751   * @since 1.2.0
2752   *
2753   * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
2754   * @return mixed An array of values.
2755   *               False for an invalid `$post_id` (non-numeric, zero, or negative value).
2756   *               An empty string if a valid but non-existing post ID is passed.
2757   */
2758  function get_post_custom( $post_id = 0 ) {
2759      $post_id = absint( $post_id );
2760  
2761      if ( ! $post_id ) {
2762          $post_id = get_the_ID();
2763      }
2764  
2765      return get_post_meta( $post_id );
2766  }
2767  
2768  /**
2769   * Retrieves meta field names for a post.
2770   *
2771   * If there are no meta fields, then nothing (null) will be returned.
2772   *
2773   * @since 1.2.0
2774   *
2775   * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
2776   * @return array|void Array of the keys, if retrieved.
2777   */
2778  function get_post_custom_keys( $post_id = 0 ) {
2779      $custom = get_post_custom( $post_id );
2780  
2781      if ( ! is_array( $custom ) ) {
2782          return;
2783      }
2784  
2785      $keys = array_keys( $custom );
2786      if ( $keys ) {
2787          return $keys;
2788      }
2789  }
2790  
2791  /**
2792   * Retrieves values for a custom post field.
2793   *
2794   * The parameters must not be considered optional. All of the post meta fields
2795   * will be retrieved and only the meta field key values returned.
2796   *
2797   * @since 1.2.0
2798   *
2799   * @param string $key     Optional. Meta field key. Default empty.
2800   * @param int    $post_id Optional. Post ID. Default is the ID of the global `$post`.
2801   * @return array|null Meta field values.
2802   */
2803  function get_post_custom_values( $key = '', $post_id = 0 ) {
2804      if ( ! $key ) {
2805          return null;
2806      }
2807  
2808      $custom = get_post_custom( $post_id );
2809  
2810      return isset( $custom[ $key ] ) ? $custom[ $key ] : null;
2811  }
2812  
2813  /**
2814   * Determines whether a post is sticky.
2815   *
2816   * Sticky posts should remain at the top of The Loop. If the post ID is not
2817   * given, then The Loop ID for the current post will be used.
2818   *
2819   * For more information on this and similar theme functions, check out
2820   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
2821   * Conditional Tags} article in the Theme Developer Handbook.
2822   *
2823   * @since 2.7.0
2824   *
2825   * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
2826   * @return bool Whether post is sticky.
2827   */
2828  function is_sticky( $post_id = 0 ) {
2829      $post_id = absint( $post_id );
2830  
2831      if ( ! $post_id ) {
2832          $post_id = get_the_ID();
2833      }
2834  
2835      $stickies = get_option( 'sticky_posts' );
2836  
2837      if ( is_array( $stickies ) ) {
2838          $stickies  = array_map( 'intval', $stickies );
2839          $is_sticky = in_array( $post_id, $stickies, true );
2840      } else {
2841          $is_sticky = false;
2842      }
2843  
2844      /**
2845       * Filters whether a post is sticky.
2846       *
2847       * @since 5.3.0
2848       *
2849       * @param bool $is_sticky Whether a post is sticky.
2850       * @param int  $post_id   Post ID.
2851       */
2852      return apply_filters( 'is_sticky', $is_sticky, $post_id );
2853  }
2854  
2855  /**
2856   * Sanitizes every post field.
2857   *
2858   * If the context is 'raw', then the post object or array will get minimal
2859   * sanitization of the integer fields.
2860   *
2861   * @since 2.3.0
2862   *
2863   * @see sanitize_post_field()
2864   *
2865   * @param object|WP_Post|array $post    The post object or array
2866   * @param string               $context Optional. How to sanitize post fields.
2867   *                                      Accepts 'raw', 'edit', 'db', 'display',
2868   *                                      'attribute', or 'js'. Default 'display'.
2869   * @return object|WP_Post|array The now sanitized post object or array (will be the
2870   *                              same type as `$post`).
2871   */
2872  function sanitize_post( $post, $context = 'display' ) {
2873      if ( is_object( $post ) ) {
2874          // Check if post already filtered for this context.
2875          if ( isset( $post->filter ) && $context === $post->filter ) {
2876              return $post;
2877          }
2878          if ( ! isset( $post->ID ) ) {
2879              $post->ID = 0;
2880          }
2881          foreach ( array_keys( get_object_vars( $post ) ) as $field ) {
2882              $post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context );
2883          }
2884          $post->filter = $context;
2885      } elseif ( is_array( $post ) ) {
2886          // Check if post already filtered for this context.
2887          if ( isset( $post['filter'] ) && $context === $post['filter'] ) {
2888              return $post;
2889          }
2890          if ( ! isset( $post['ID'] ) ) {
2891              $post['ID'] = 0;
2892          }
2893          foreach ( array_keys( $post ) as $field ) {
2894              $post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context );
2895          }
2896          $post['filter'] = $context;
2897      }
2898      return $post;
2899  }
2900  
2901  /**
2902   * Sanitizes a post field based on context.
2903   *
2904   * Possible context values are:  'raw', 'edit', 'db', 'display', 'attribute' and
2905   * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
2906   * are treated like 'display' when calling filters.
2907   *
2908   * @since 2.3.0
2909   * @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'.
2910   *
2911   * @param string $field   The Post Object field name.
2912   * @param mixed  $value   The Post Object value.
2913   * @param int    $post_id Post ID.
2914   * @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit',
2915   *                        'db', 'display', 'attribute' and 'js'. Default 'display'.
2916   * @return mixed Sanitized value.
2917   */
2918  function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
2919      $int_fields = array( 'ID', 'post_parent', 'menu_order' );
2920      if ( in_array( $field, $int_fields, true ) ) {
2921          $value = (int) $value;
2922      }
2923  
2924      // Fields which contain arrays of integers.
2925      $array_int_fields = array( 'ancestors' );
2926      if ( in_array( $field, $array_int_fields, true ) ) {
2927          $value = array_map( 'absint', $value );
2928          return $value;
2929      }
2930  
2931      if ( 'raw' === $context ) {
2932          return $value;
2933      }
2934  
2935      $prefixed = false;
2936      if ( str_contains( $field, 'post_' ) ) {
2937          $prefixed        = true;
2938          $field_no_prefix = str_replace( 'post_', '', $field );
2939      }
2940  
2941      if ( 'edit' === $context ) {
2942          $format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' );
2943  
2944          if ( $prefixed ) {
2945  
2946              /**
2947               * Filters the value of a specific post field to edit.
2948               *
2949               * The dynamic portion of the hook name, `$field`, refers to the post
2950               * field name. Possible filter names include:
2951               *
2952               *  - `edit_post_author`
2953               *  - `edit_post_date`
2954               *  - `edit_post_date_gmt`
2955               *  - `edit_post_content`
2956               *  - `edit_post_title`
2957               *  - `edit_post_excerpt`
2958               *  - `edit_post_status`
2959               *  - `edit_post_password`
2960               *  - `edit_post_name`
2961               *  - `edit_post_modified`
2962               *  - `edit_post_modified_gmt`
2963               *  - `edit_post_content_filtered`
2964               *  - `edit_post_parent`
2965               *  - `edit_post_type`
2966               *  - `edit_post_mime_type`
2967               *
2968               * @since 2.3.0
2969               *
2970               * @param mixed $value   Value of the post field.
2971               * @param int   $post_id Post ID.
2972               */
2973              $value = apply_filters( "edit_{$field}", $value, $post_id );
2974  
2975              /**
2976               * Filters the value of a specific post field to edit.
2977               *
2978               * Only applied to post fields with a name which is prefixed with `post_`.
2979               *
2980               * The dynamic portion of the hook name, `$field_no_prefix`, refers to the
2981               * post field name minus the `post_` prefix. Possible filter names include:
2982               *
2983               *  - `author_edit_pre`
2984               *  - `date_edit_pre`
2985               *  - `date_gmt_edit_pre`
2986               *  - `content_edit_pre`
2987               *  - `title_edit_pre`
2988               *  - `excerpt_edit_pre`
2989               *  - `status_edit_pre`
2990               *  - `password_edit_pre`
2991               *  - `name_edit_pre`
2992               *  - `modified_edit_pre`
2993               *  - `modified_gmt_edit_pre`
2994               *  - `content_filtered_edit_pre`
2995               *  - `parent_edit_pre`
2996               *  - `type_edit_pre`
2997               *  - `mime_type_edit_pre`
2998               *
2999               * @since 2.3.0
3000               *
3001               * @param mixed $value   Value of the post field.
3002               * @param int   $post_id Post ID.
3003               */
3004              $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id );
3005          } else {
3006              /**
3007               * Filters the value of a specific post field to edit.
3008               *
3009               * Only applied to post fields not prefixed with `post_`.
3010               *
3011               * The dynamic portion of the hook name, `$field`, refers to the
3012               * post field name. Possible filter names include:
3013               *
3014               *  - `edit_post_ID`
3015               *  - `edit_post_ping_status`
3016               *  - `edit_post_pinged`
3017               *  - `edit_post_to_ping`
3018               *  - `edit_post_comment_count`
3019               *  - `edit_post_comment_status`
3020               *  - `edit_post_guid`
3021               *  - `edit_post_menu_order`
3022               *
3023               * @since 2.3.0
3024               *
3025               * @param mixed $value   Value of the post field.
3026               * @param int   $post_id Post ID.
3027               */
3028              $value = apply_filters( "edit_post_{$field}", $value, $post_id );
3029          }
3030  
3031          if ( in_array( $field, $format_to_edit, true ) ) {
3032              if ( 'post_content' === $field ) {
3033                  $value = format_to_edit( $value, user_can_richedit() );
3034              } else {
3035                  $value = format_to_edit( $value );
3036              }
3037          } else {
3038              $value = esc_attr( $value );
3039          }
3040      } elseif ( 'db' === $context ) {
3041          if ( $prefixed ) {
3042  
3043              /**
3044               * Filters the value of a specific post field before saving.
3045               *
3046               * Only applied to post fields with a name which is prefixed with `post_`.
3047               *
3048               * The dynamic portion of the hook name, `$field`, refers to the post
3049               * field name. Possible filter names include:
3050               *
3051               *  - `pre_post_author`
3052               *  - `pre_post_date`
3053               *  - `pre_post_date_gmt`
3054               *  - `pre_post_content`
3055               *  - `pre_post_title`
3056               *  - `pre_post_excerpt`
3057               *  - `pre_post_status`
3058               *  - `pre_post_password`
3059               *  - `pre_post_name`
3060               *  - `pre_post_modified`
3061               *  - `pre_post_modified_gmt`
3062               *  - `pre_post_content_filtered`
3063               *  - `pre_post_parent`
3064               *  - `pre_post_type`
3065               *  - `pre_post_mime_type`
3066               *
3067               * @since 2.3.0
3068               *
3069               * @param mixed $value Value of the post field.
3070               */
3071              $value = apply_filters( "pre_{$field}", $value );
3072  
3073              /**
3074               * Filters the value of a specific field before saving.
3075               *
3076               * Only applied to post fields with a name which is prefixed with `post_`.
3077               *
3078               * The dynamic portion of the hook name, `$field_no_prefix`, refers to the
3079               * post field name minus the `post_` prefix. Possible filter names include:
3080               *
3081               *  - `author_save_pre`
3082               *  - `date_save_pre`
3083               *  - `date_gmt_save_pre`
3084               *  - `content_save_pre`
3085               *  - `title_save_pre`
3086               *  - `excerpt_save_pre`
3087               *  - `status_save_pre`
3088               *  - `password_save_pre`
3089               *  - `name_save_pre`
3090               *  - `modified_save_pre`
3091               *  - `modified_gmt_save_pre`
3092               *  - `content_filtered_save_pre`
3093               *  - `parent_save_pre`
3094               *  - `type_save_pre`
3095               *  - `mime_type_save_pre`
3096               *
3097               * @since 2.3.0
3098               *
3099               * @param mixed $value Value of the post field.
3100               */
3101              $value = apply_filters( "{$field_no_prefix}_save_pre", $value );
3102          } else {
3103              /**
3104               * Filters the value of a specific field before saving.
3105               *
3106               * Only applied to post fields with a name which is prefixed with `post_`.
3107               *
3108               * The dynamic portion of the hook name, `$field_no_prefix`, refers to the
3109               * post field name minus the `post_` prefix. Possible filter names include:
3110               *
3111               *  - `pre_post_ID`
3112               *  - `pre_post_comment_status`
3113               *  - `pre_post_ping_status`
3114               *  - `pre_post_to_ping`
3115               *  - `pre_post_pinged`
3116               *  - `pre_post_guid`
3117               *  - `pre_post_menu_order`
3118               *  - `pre_post_comment_count`
3119               *
3120               * @since 2.3.0
3121               *
3122               * @param mixed $value Value of the post field.
3123               */
3124              $value = apply_filters( "pre_post_{$field}", $value );
3125  
3126              /**
3127               * Filters the value of a specific post field before saving.
3128               *
3129               * Only applied to post fields with a name which is *not* prefixed with `post_`.
3130               *
3131               * The dynamic portion of the hook name, `$field`, refers to the post
3132               * field name. Possible filter names include:
3133               *
3134               *  - `ID_pre`
3135               *  - `comment_status_pre`
3136               *  - `ping_status_pre`
3137               *  - `to_ping_pre`
3138               *  - `pinged_pre`
3139               *  - `guid_pre`
3140               *  - `menu_order_pre`
3141               *  - `comment_count_pre`
3142               *
3143               * @since 2.3.0
3144               *
3145               * @param mixed $value Value of the post field.
3146               */
3147              $value = apply_filters( "{$field}_pre", $value );
3148          }
3149      } else {
3150  
3151          // Use display filters by default.
3152          if ( $prefixed ) {
3153  
3154              /**
3155               * Filters the value of a specific post field for display.
3156               *
3157               * Only applied to post fields with a name which is prefixed with `post_`.
3158               *
3159               * The dynamic portion of the hook name, `$field`, refers to the post
3160               * field name. Possible filter names include:
3161               *
3162               *  - `post_author`
3163               *  - `post_date`
3164               *  - `post_date_gmt`
3165               *  - `post_content`
3166               *  - `post_title`
3167               *  - `post_excerpt`
3168               *  - `post_status`
3169               *  - `post_password`
3170               *  - `post_name`
3171               *  - `post_modified`
3172               *  - `post_modified_gmt`
3173               *  - `post_content_filtered`
3174               *  - `post_parent`
3175               *  - `post_type`
3176               *  - `post_mime_type`
3177               *
3178               * @since 2.3.0
3179               *
3180               * @param mixed  $value   Value of the prefixed post field.
3181               * @param int    $post_id Post ID.
3182               * @param string $context Context for how to sanitize the field.
3183               *                        Accepts 'raw', 'edit', 'db', 'display',
3184               *                        'attribute', or 'js'. Default 'display'.
3185               */
3186              $value = apply_filters( "{$field}", $value, $post_id, $context );
3187          } else {
3188              /**
3189               * Filters the value of a specific post field for display.
3190               *
3191               * Only applied to post fields name which is *not* prefixed with `post_`.
3192               *
3193               * The dynamic portion of the hook name, `$field`, refers to the post
3194               * field name. Possible filter names include:
3195               *
3196               *  - `post_ID`
3197               *  - `post_comment_status`
3198               *  - `post_ping_status`
3199               *  - `post_to_ping`
3200               *  - `post_pinged`
3201               *  - `post_guid`
3202               *  - `post_menu_order`
3203               *  - `post_comment_count`
3204               *
3205               * @since 2.3.0
3206               *
3207               * @param mixed  $value   Value of the unprefixed post field.
3208               * @param int    $post_id Post ID
3209               * @param string $context Context for how to sanitize the field.
3210               *                        Accepts 'raw', 'edit', 'db', 'display',
3211               *                        'attribute', or 'js'. Default 'display'.
3212               */
3213              $value = apply_filters( "post_{$field}", $value, $post_id, $context );
3214          }
3215  
3216          if ( 'attribute' === $context ) {
3217              $value = esc_attr( $value );
3218          } elseif ( 'js' === $context ) {
3219              $value = esc_js( $value );
3220          }
3221      }
3222  
3223      // Restore the type for integer fields after esc_attr().
3224      if ( in_array( $field, $int_fields, true ) ) {
3225          $value = (int) $value;
3226      }
3227      return $value;
3228  }
3229  
3230  /**
3231   * Makes a post sticky.
3232   *
3233   * Sticky posts should be displayed at the top of the front page.
3234   *
3235   * @since 2.7.0
3236   *
3237   * @param int $post_id Post ID.
3238   */
3239  function stick_post( $post_id ) {
3240      $post_id  = (int) $post_id;
3241      $stickies = get_option( 'sticky_posts' );
3242      $updated  = false;
3243  
3244      if ( ! is_array( $stickies ) ) {
3245          $stickies = array();
3246      } else {
3247          $stickies = array_unique( array_map( 'intval', $stickies ) );
3248      }
3249  
3250      if ( ! in_array( $post_id, $stickies, true ) ) {
3251          $stickies[] = $post_id;
3252          $updated    = update_option( 'sticky_posts', array_values( $stickies ) );
3253      }
3254  
3255      if ( $updated ) {
3256          /**
3257           * Fires once a post has been added to the sticky list.
3258           *
3259           * @since 4.6.0
3260           *
3261           * @param int $post_id ID of the post that was stuck.
3262           */
3263          do_action( 'post_stuck', $post_id );
3264      }
3265  }
3266  
3267  /**
3268   * Un-sticks a post.
3269   *
3270   * Sticky posts should be displayed at the top of the front page.
3271   *
3272   * @since 2.7.0
3273   *
3274   * @param int $post_id Post ID.
3275   */
3276  function unstick_post( $post_id ) {
3277      $post_id  = (int) $post_id;
3278      $stickies = get_option( 'sticky_posts' );
3279  
3280      if ( ! is_array( $stickies ) ) {
3281          return;
3282      }
3283  
3284      $stickies = array_values( array_unique( array_map( 'intval', $stickies ) ) );
3285  
3286      if ( ! in_array( $post_id, $stickies, true ) ) {
3287          return;
3288      }
3289  
3290      $offset = array_search( $post_id, $stickies, true );
3291      if ( false === $offset ) {
3292          return;
3293      }
3294  
3295      array_splice( $stickies, $offset, 1 );
3296  
3297      $updated = update_option( 'sticky_posts', $stickies );
3298  
3299      if ( $updated ) {
3300          /**
3301           * Fires once a post has been removed from the sticky list.
3302           *
3303           * @since 4.6.0
3304           *
3305           * @param int $post_id ID of the post that was unstuck.
3306           */
3307          do_action( 'post_unstuck', $post_id );
3308      }
3309  }
3310  
3311  /**
3312   * Returns the cache key for wp_count_posts() based on the passed arguments.
3313   *
3314   * @since 3.9.0
3315   * @access private
3316   *
3317   * @param string $type Optional. Post type to retrieve count Default 'post'.
3318   * @param string $perm Optional. 'readable' or empty. Default empty.
3319   * @return string The cache key.
3320   */
3321  function _count_posts_cache_key( $type = 'post', $perm = '' ) {
3322      $cache_key = 'posts-' . $type;
3323  
3324      if ( 'readable' === $perm && is_user_logged_in() ) {
3325          $post_type_object = get_post_type_object( $type );
3326  
3327          if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
3328              $cache_key .= '_' . $perm . '_' . get_current_user_id();
3329          }
3330      }
3331  
3332      return $cache_key;
3333  }
3334  
3335  /**
3336   * Counts number of posts of a post type and if user has permissions to view.
3337   *
3338   * This function provides an efficient method of finding the amount of post's
3339   * type a blog has. Another method is to count the amount of items in
3340   * get_posts(), but that method has a lot of overhead with doing so. Therefore,
3341   * when developing for 2.5+, use this function instead.
3342   *
3343   * The $perm parameter checks for 'readable' value and if the user can read
3344   * private posts, it will display that for the user that is signed in.
3345   *
3346   * @since 2.5.0
3347   *
3348   * @global wpdb $wpdb WordPress database abstraction object.
3349   *
3350   * @param string $type Optional. Post type to retrieve count. Default 'post'.
3351   * @param string $perm Optional. 'readable' or empty. Default empty.
3352   * @return stdClass An object containing the number of posts for each status,
3353   *                  or an empty object if the post type does not exist.
3354   */
3355  function wp_count_posts( $type = 'post', $perm = '' ) {
3356      global $wpdb;
3357  
3358      if ( ! post_type_exists( $type ) ) {
3359          return new stdClass();
3360      }
3361  
3362      $cache_key = _count_posts_cache_key( $type, $perm );
3363  
3364      $counts = wp_cache_get( $cache_key, 'counts' );
3365      if ( false !== $counts ) {
3366          // We may have cached this before every status was registered.
3367          foreach ( get_post_stati() as $status ) {
3368              if ( ! isset( $counts->{$status} ) ) {
3369                  $counts->{$status} = 0;
3370              }
3371          }
3372  
3373          /** This filter is documented in wp-includes/post.php */
3374          return apply_filters( 'wp_count_posts', $counts, $type, $perm );
3375      }
3376  
3377      $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
3378  
3379      if ( 'readable' === $perm && is_user_logged_in() ) {
3380          $post_type_object = get_post_type_object( $type );
3381          if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
3382              $query .= $wpdb->prepare(
3383                  " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
3384                  get_current_user_id()
3385              );
3386          }
3387      }
3388  
3389      $query .= ' GROUP BY post_status';
3390  
3391      $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
3392      $counts  = array_fill_keys( get_post_stati(), 0 );
3393  
3394      foreach ( $results as $row ) {
3395          $counts[ $row['post_status'] ] = $row['num_posts'];
3396      }
3397  
3398      $counts = (object) $counts;
3399      wp_cache_set( $cache_key, $counts, 'counts' );
3400  
3401      /**
3402       * Filters the post counts by status for the current post type.
3403       *
3404       * @since 3.7.0
3405       *
3406       * @param stdClass $counts An object containing the current post_type's post
3407       *                         counts by status.
3408       * @param string   $type   Post type.
3409       * @param string   $perm   The permission to determine if the posts are 'readable'
3410       *                         by the current user.
3411       */
3412      return apply_filters( 'wp_count_posts', $counts, $type, $perm );
3413  }
3414  
3415  /**
3416   * Counts number of attachments for the mime type(s).
3417   *
3418   * If you set the optional mime_type parameter, then an array will still be
3419   * returned, but will only have the item you are looking for. It does not give
3420   * you the number of attachments that are children of a post. You can get that
3421   * by counting the number of children that post has.
3422   *
3423   * @since 2.5.0
3424   *
3425   * @global wpdb $wpdb WordPress database abstraction object.
3426   *
3427   * @param string|string[] $mime_type Optional. Array or comma-separated list of
3428   *                                   MIME patterns. Default empty.
3429   * @return stdClass An object containing the attachment counts by mime type.
3430   */
3431  function wp_count_attachments( $mime_type = '' ) {
3432      global $wpdb;
3433  
3434      $cache_key = sprintf(
3435          'attachments%s',
3436          ! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : ''
3437      );
3438  
3439      $counts = wp_cache_get( $cache_key, 'counts' );
3440  
3441      if ( false === $counts ) {
3442          $and   = wp_post_mime_type_where( $mime_type );
3443          $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
3444  
3445          $counts = array();
3446          foreach ( (array) $count as $row ) {
3447              $counts[ $row['post_mime_type'] ] = $row['num_posts'];
3448          }
3449          $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" );
3450  
3451          wp_cache_set( $cache_key, (object) $counts, 'counts' );
3452      }
3453  
3454      /**
3455       * Filters the attachment counts by mime type.
3456       *
3457       * @since 3.7.0
3458       *
3459       * @param stdClass        $counts    An object containing the attachment counts by
3460       *                                   mime type.
3461       * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
3462       */
3463      return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
3464  }
3465  
3466  /**
3467   * Gets default post mime types.
3468   *
3469   * @since 2.9.0
3470   * @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups.
3471   *
3472   * @return array List of post mime types.
3473   */
3474  function get_post_mime_types() {
3475      $post_mime_types = array(   // array( adj, noun )
3476          'image'       => array(
3477              __( 'Images' ),
3478              __( 'Manage Images' ),
3479              /* translators: %s: Number of images. */
3480              _n_noop(
3481                  'Image <span class="count">(%s)</span>',
3482                  'Images <span class="count">(%s)</span>'
3483              ),
3484          ),
3485          'audio'       => array(
3486              _x( 'Audio', 'file type group' ),
3487              __( 'Manage Audio' ),
3488              /* translators: %s: Number of audio files. */
3489              _n_noop(
3490                  'Audio <span class="count">(%s)</span>',
3491                  'Audio <span class="count">(%s)</span>'
3492              ),
3493          ),
3494          'video'       => array(
3495              _x( 'Video', 'file type group' ),
3496              __( 'Manage Video' ),
3497              /* translators: %s: Number of video files. */
3498              _n_noop(
3499                  'Video <span class="count">(%s)</span>',
3500                  'Video <span class="count">(%s)</span>'
3501              ),
3502          ),
3503          'document'    => array(
3504              __( 'Documents' ),
3505              __( 'Manage Documents' ),
3506              /* translators: %s: Number of documents. */
3507              _n_noop(
3508                  'Document <span class="count">(%s)</span>',
3509                  'Documents <span class="count">(%s)</span>'
3510              ),
3511          ),
3512          'spreadsheet' => array(
3513              __( 'Spreadsheets' ),
3514              __( 'Manage Spreadsheets' ),
3515              /* translators: %s: Number of spreadsheets. */
3516              _n_noop(
3517                  'Spreadsheet <span class="count">(%s)</span>',
3518                  'Spreadsheets <span class="count">(%s)</span>'
3519              ),
3520          ),
3521          'archive'     => array(
3522              _x( 'Archives', 'file type group' ),
3523              __( 'Manage Archives' ),
3524              /* translators: %s: Number of archives. */
3525              _n_noop(
3526                  'Archive <span class="count">(%s)</span>',
3527                  'Archives <span class="count">(%s)</span>'
3528              ),
3529          ),
3530      );
3531  
3532      $ext_types  = wp_get_ext_types();
3533      $mime_types = wp_get_mime_types();
3534  
3535      foreach ( $post_mime_types as $group => $labels ) {
3536          if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) {
3537              continue;
3538          }
3539  
3540          if ( ! isset( $ext_types[ $group ] ) ) {
3541              unset( $post_mime_types[ $group ] );
3542              continue;
3543          }
3544  
3545          $group_mime_types = array();
3546          foreach ( $ext_types[ $group ] as $extension ) {
3547              foreach ( $mime_types as $exts => $mime ) {
3548                  if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
3549                      $group_mime_types[] = $mime;
3550                      break;
3551                  }
3552              }
3553          }
3554          $group_mime_types = implode( ',', array_unique( $group_mime_types ) );
3555  
3556          $post_mime_types[ $group_mime_types ] = $labels;
3557          unset( $post_mime_types[ $group ] );
3558      }
3559  
3560      /**
3561       * Filters the default list of post mime types.
3562       *
3563       * @since 2.5.0
3564       *
3565       * @param array $post_mime_types Default list of post mime types.
3566       */
3567      return apply_filters( 'post_mime_types', $post_mime_types );
3568  }
3569  
3570  /**
3571   * Checks a MIME-Type against a list.
3572   *
3573   * If the `$wildcard_mime_types` parameter is a string, it must be comma separated
3574   * list. If the `$real_mime_types` is a string, it is also comma separated to
3575   * create the list.
3576   *
3577   * @since 2.5.0
3578   *
3579   * @param string|string[] $wildcard_mime_types Mime types, e.g. `audio/mpeg`, `image` (same as `image/*`),
3580   *                                             or `flash` (same as `*flash*`).
3581   * @param string|string[] $real_mime_types     Real post mime type values.
3582   * @return array array(wildcard=>array(real types)).
3583   */
3584  function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
3585      $matches = array();
3586      if ( is_string( $wildcard_mime_types ) ) {
3587          $wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) );
3588      }
3589      if ( is_string( $real_mime_types ) ) {
3590          $real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) );
3591      }
3592  
3593      $patternses = array();
3594      $wild       = '[-._a-z0-9]*';
3595  
3596      foreach ( (array) $wildcard_mime_types as $type ) {
3597          $mimes = array_map( 'trim', explode( ',', $type ) );
3598          foreach ( $mimes as $mime ) {
3599              $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) );
3600  
3601              $patternses[][ $type ] = "^$regex$";
3602  
3603              if ( ! str_contains( $mime, '/' ) ) {
3604                  $patternses[][ $type ] = "^$regex/";
3605                  $patternses[][ $type ] = $regex;
3606              }
3607          }
3608      }
3609      asort( $patternses );
3610  
3611      foreach ( $patternses as $patterns ) {
3612          foreach ( $patterns as $type => $pattern ) {
3613              foreach ( (array) $real_mime_types as $real ) {
3614                  if ( preg_match( "#$pattern#", $real )
3615                      && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) )
3616                  ) {
3617                      $matches[ $type ][] = $real;
3618                  }
3619              }
3620          }
3621      }
3622  
3623      return $matches;
3624  }
3625  
3626  /**
3627   * Converts MIME types into SQL.
3628   *
3629   * @since 2.5.0
3630   *
3631   * @param string|string[] $post_mime_types List of mime types or comma separated string
3632   *                                         of mime types.
3633   * @param string          $table_alias     Optional. Specify a table alias, if needed.
3634   *                                         Default empty.
3635   * @return string The SQL AND clause for mime searching.
3636   */
3637  function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
3638      $where     = '';
3639      $wildcards = array( '', '%', '%/%' );
3640      if ( is_string( $post_mime_types ) ) {
3641          $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) );
3642      }
3643  
3644      $where_clauses = array();
3645  
3646      foreach ( (array) $post_mime_types as $mime_type ) {
3647          $mime_type = preg_replace( '/\s/', '', $mime_type );
3648          $slashpos  = strpos( $mime_type, '/' );
3649          if ( false !== $slashpos ) {
3650              $mime_group    = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) );
3651              $mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) );
3652              if ( empty( $mime_subgroup ) ) {
3653                  $mime_subgroup = '*';
3654              } else {
3655                  $mime_subgroup = str_replace( '/', '', $mime_subgroup );
3656              }
3657              $mime_pattern = "$mime_group/$mime_subgroup";
3658          } else {
3659              $mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type );
3660              if ( ! str_contains( $mime_pattern, '*' ) ) {
3661                  $mime_pattern .= '/*';
3662              }
3663          }
3664  
3665          $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern );
3666  
3667          if ( in_array( $mime_type, $wildcards, true ) ) {
3668              return '';
3669          }
3670  
3671          if ( str_contains( $mime_pattern, '%' ) ) {
3672              $where_clauses[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'";
3673          } else {
3674              $where_clauses[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
3675          }
3676      }
3677  
3678      if ( ! empty( $where_clauses ) ) {
3679          $where = ' AND (' . implode( ' OR ', $where_clauses ) . ') ';
3680      }
3681  
3682      return $where;
3683  }
3684  
3685  /**
3686   * Trashes or deletes a post or page.
3687   *
3688   * When the post and page is permanently deleted, everything that is tied to
3689   * it is deleted also. This includes comments, post meta fields, and terms
3690   * associated with the post.
3691   *
3692   * The post or page is moved to Trash instead of permanently deleted unless
3693   * Trash is disabled, item is already in the Trash, or $force_delete is true.
3694   *
3695   * @since 1.0.0
3696   *
3697   * @global wpdb $wpdb WordPress database abstraction object.
3698   * @see wp_delete_attachment()
3699   * @see wp_trash_post()
3700   *
3701   * @param int  $post_id      Optional. Post ID. Default 0.
3702   * @param bool $force_delete Optional. Whether to bypass Trash and force deletion.
3703   *                           Default false.
3704   * @return WP_Post|false|null Post data on success, false or null on failure.
3705   */
3706  function wp_delete_post( $post_id = 0, $force_delete = false ) {
3707      global $wpdb;
3708  
3709      $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );
3710  
3711      if ( ! $post ) {
3712          return $post;
3713      }
3714  
3715      $post = get_post( $post );
3716  
3717      if ( ! $force_delete
3718          && ( 'post' === $post->post_type || 'page' === $post->post_type )
3719          && 'trash' !== get_post_status( $post_id ) && EMPTY_TRASH_DAYS
3720      ) {
3721          return wp_trash_post( $post_id );
3722      }
3723  
3724      if ( 'attachment' === $post->post_type ) {
3725          return wp_delete_attachment( $post_id, $force_delete );
3726      }
3727  
3728      /**
3729       * Filters whether a post deletion should take place.
3730       *
3731       * @since 4.4.0
3732       *
3733       * @param WP_Post|false|null $delete       Whether to go forward with deletion.
3734       * @param WP_Post            $post         Post object.
3735       * @param bool               $force_delete Whether to bypass the Trash.
3736       */
3737      $check = apply_filters( 'pre_delete_post', null, $post, $force_delete );
3738      if ( null !== $check ) {
3739          return $check;
3740      }
3741  
3742      /**
3743       * Fires before a post is deleted, at the start of wp_delete_post().
3744       *
3745       * @since 3.2.0
3746       * @since 5.5.0 Added the `$post` parameter.
3747       *
3748       * @see wp_delete_post()
3749       *
3750       * @param int     $post_id Post ID.
3751       * @param WP_Post $post    Post object.
3752       */
3753      do_action( 'before_delete_post', $post_id, $post );
3754  
3755      delete_post_meta( $post_id, '_wp_trash_meta_status' );
3756      delete_post_meta( $post_id, '_wp_trash_meta_time' );
3757  
3758      wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) );
3759  
3760      $parent_data  = array( 'post_parent' => $post->post_parent );
3761      $parent_where = array( 'post_parent' => $post_id );
3762  
3763      if ( is_post_type_hierarchical( $post->post_type ) ) {
3764          // Point children of this page to its parent, also clean the cache of affected children.
3765          $children_query = $wpdb->prepare(
3766              "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s",
3767              $post_id,
3768              $post->post_type
3769          );
3770  
3771          $children = $wpdb->get_results( $children_query );
3772  
3773          if ( $children ) {
3774              $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
3775          }
3776      }
3777  
3778      // Do raw query. wp_get_post_revisions() is filtered.
3779      $revision_ids = $wpdb->get_col(
3780          $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $post_id )
3781      );
3782  
3783      // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
3784      foreach ( $revision_ids as $revision_id ) {
3785          wp_delete_post_revision( $revision_id );
3786      }
3787  
3788      // Point all attachments to this post up one level.
3789      $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
3790  
3791      wp_defer_comment_counting( true );
3792  
3793      $comment_ids = $wpdb->get_col(
3794          $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id )
3795      );
3796  
3797      foreach ( $comment_ids as $comment_id ) {
3798          wp_delete_comment( $comment_id, true );
3799      }
3800  
3801      wp_defer_comment_counting( false );
3802  
3803      $post_meta_ids = $wpdb->get_col(
3804          $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )
3805      );
3806  
3807      foreach ( $post_meta_ids as $mid ) {
3808          delete_metadata_by_mid( 'post', $mid );
3809      }
3810  
3811      /**
3812       * Fires immediately before a post is deleted from the database.
3813       *
3814       * The dynamic portion of the hook name, `$post->post_type`, refers to
3815       * the post type slug.
3816       *
3817       * @since 6.6.0
3818       *
3819       * @param int     $post_id Post ID.
3820       * @param WP_Post $post    Post object.
3821       */
3822      do_action( "delete_post_{$post->post_type}", $post_id, $post );
3823  
3824      /**
3825       * Fires immediately before a post is deleted from the database.
3826       *
3827       * @since 1.2.0
3828       * @since 5.5.0 Added the `$post` parameter.
3829       *
3830       * @param int     $post_id Post ID.
3831       * @param WP_Post $post    Post object.
3832       */
3833      do_action( 'delete_post', $post_id, $post );
3834  
3835      $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
3836      if ( ! $result ) {
3837          return false;
3838      }
3839  
3840      /**
3841       * Fires immediately after a post is deleted from the database.
3842       *
3843       * The dynamic portion of the hook name, `$post->post_type`, refers to
3844       * the post type slug.
3845       *
3846       * @since 6.6.0
3847       *
3848       * @param int     $post_id Post ID.
3849       * @param WP_Post $post    Post object.
3850       */
3851      do_action( "deleted_post_{$post->post_type}", $post_id, $post );
3852  
3853      /**
3854       * Fires immediately after a post is deleted from the database.
3855       *
3856       * @since 2.2.0
3857       * @since 5.5.0 Added the `$post` parameter.
3858       *
3859       * @param int     $post_id Post ID.
3860       * @param WP_Post $post    Post object.
3861       */
3862      do_action( 'deleted_post', $post_id, $post );
3863  
3864      clean_post_cache( $post );
3865  
3866      if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
3867          foreach ( $children as $child ) {
3868              clean_post_cache( $child );
3869          }
3870      }
3871  
3872      wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) );
3873  
3874      /**
3875       * Fires after a post is deleted, at the conclusion of wp_delete_post().
3876       *
3877       * @since 3.2.0
3878       * @since 5.5.0 Added the `$post` parameter.
3879       *
3880       * @see wp_delete_post()
3881       *
3882       * @param int     $post_id Post ID.
3883       * @param WP_Post $post    Post object.
3884       */
3885      do_action( 'after_delete_post', $post_id, $post );
3886  
3887      return $post;
3888  }
3889  
3890  /**
3891   * Resets the page_on_front, show_on_front, and page_for_post settings when
3892   * a linked page is deleted or trashed.
3893   *
3894   * Also ensures the post is no longer sticky.
3895   *
3896   * @since 3.7.0
3897   * @access private
3898   *
3899   * @param int $post_id Post ID.
3900   */
3901  function _reset_front_page_settings_for_post( $post_id ) {
3902      $post = get_post( $post_id );
3903  
3904      if ( 'page' === $post->post_type ) {
3905          /*
3906           * If the page is defined in option page_on_front or post_for_posts,
3907           * adjust the corresponding options.
3908           */
3909          if ( (int) get_option( 'page_on_front' ) === $post->ID ) {
3910              update_option( 'show_on_front', 'posts' );
3911              update_option( 'page_on_front', 0 );
3912          }
3913          if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
3914              update_option( 'page_for_posts', 0 );
3915          }
3916      }
3917  
3918      unstick_post( $post->ID );
3919  }
3920  
3921  /**
3922   * Moves a post or page to the Trash
3923   *
3924   * If Trash is disabled, the post or page is permanently deleted.
3925   *
3926   * @since 2.9.0
3927   *
3928   * @see wp_delete_post()
3929   *
3930   * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`
3931   *                     if `EMPTY_TRASH_DAYS` equals true.
3932   * @return WP_Post|false|null Post data on success, false or null on failure.
3933   */
3934  function wp_trash_post( $post_id = 0 ) {
3935      if ( ! EMPTY_TRASH_DAYS ) {
3936          return wp_delete_post( $post_id, true );
3937      }
3938  
3939      $post = get_post( $post_id );
3940  
3941      if ( ! $post ) {
3942          return $post;
3943      }
3944  
3945      if ( 'trash' === $post->post_status ) {
3946          return false;
3947      }
3948  
3949      $previous_status = $post->post_status;
3950  
3951      /**
3952       * Filters whether a post trashing should take place.
3953       *
3954       * @since 4.9.0
3955       * @since 6.3.0 Added the `$previous_status` parameter.
3956       *
3957       * @param bool|null $trash           Whether to go forward with trashing.
3958       * @param WP_Post   $post            Post object.
3959       * @param string    $previous_status The status of the post about to be trashed.
3960       */
3961      $check = apply_filters( 'pre_trash_post', null, $post, $previous_status );
3962  
3963      if ( null !== $check ) {
3964          return $check;
3965      }
3966  
3967      /**
3968       * Fires before a post is sent to the Trash.
3969       *
3970       * @since 3.3.0
3971       * @since 6.3.0 Added the `$previous_status` parameter.
3972       *
3973       * @param int    $post_id         Post ID.
3974       * @param string $previous_status The status of the post about to be trashed.
3975       */
3976      do_action( 'wp_trash_post', $post_id, $previous_status );
3977  
3978      add_post_meta( $post_id, '_wp_trash_meta_status', $previous_status );
3979      add_post_meta( $post_id, '_wp_trash_meta_time', time() );
3980  
3981      $post_updated = wp_update_post(
3982          array(
3983              'ID'          => $post_id,
3984              'post_status' => 'trash',
3985          )
3986      );
3987  
3988      if ( ! $post_updated ) {
3989          return false;
3990      }
3991  
3992      wp_trash_post_comments( $post_id );
3993  
3994      /**
3995       * Fires after a post is sent to the Trash.
3996       *
3997       * @since 2.9.0
3998       * @since 6.3.0 Added the `$previous_status` parameter.
3999       *
4000       * @param int    $post_id         Post ID.
4001       * @param string $previous_status The status of the post at the point where it was trashed.
4002       */
4003      do_action( 'trashed_post', $post_id, $previous_status );
4004  
4005      return $post;
4006  }
4007  
4008  /**
4009   * Restores a post from the Trash.
4010   *
4011   * @since 2.9.0
4012   * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for
4013   *              attachments which are returned to their original 'inherit' status.
4014   *
4015   * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
4016   * @return WP_Post|false|null Post data on success, false or null on failure.
4017   */
4018  function wp_untrash_post( $post_id = 0 ) {
4019      $post = get_post( $post_id );
4020  
4021      if ( ! $post ) {
4022          return $post;
4023      }
4024  
4025      $post_id = $post->ID;
4026  
4027      if ( 'trash' !== $post->post_status ) {
4028          return false;
4029      }
4030  
4031      $previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
4032  
4033      /**
4034       * Filters whether a post untrashing should take place.
4035       *
4036       * @since 4.9.0
4037       * @since 5.6.0 Added the `$previous_status` parameter.
4038       *
4039       * @param bool|null $untrash         Whether to go forward with untrashing.
4040       * @param WP_Post   $post            Post object.
4041       * @param string    $previous_status The status of the post at the point where it was trashed.
4042       */
4043      $check = apply_filters( 'pre_untrash_post', null, $post, $previous_status );
4044      if ( null !== $check ) {
4045          return $check;
4046      }
4047  
4048      /**
4049       * Fires before a post is restored from the Trash.
4050       *
4051       * @since 2.9.0
4052       * @since 5.6.0 Added the `$previous_status` parameter.
4053       *
4054       * @param int    $post_id         Post ID.
4055       * @param string $previous_status The status of the post at the point where it was trashed.
4056       */
4057      do_action( 'untrash_post', $post_id, $previous_status );
4058  
4059      $new_status = ( 'attachment' === $post->post_type ) ? 'inherit' : 'draft';
4060  
4061      /**
4062       * Filters the status that a post gets assigned when it is restored from the trash (untrashed).
4063       *
4064       * By default posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status`
4065       * in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()`
4066       * function is available for this.
4067       *
4068       * Prior to WordPress 5.6.0, restored posts were always assigned their original status.
4069       *
4070       * @since 5.6.0
4071       *
4072       * @param string $new_status      The new status of the post being restored.
4073       * @param int    $post_id         The ID of the post being restored.
4074       * @param string $previous_status The status of the post at the point where it was trashed.
4075       */
4076      $post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status );
4077  
4078      delete_post_meta( $post_id, '_wp_trash_meta_status' );
4079      delete_post_meta( $post_id, '_wp_trash_meta_time' );
4080  
4081      $post_updated = wp_update_post(
4082          array(
4083              'ID'          => $post_id,
4084              'post_status' => $post_status,
4085          )
4086      );
4087  
4088      if ( ! $post_updated ) {
4089          return false;
4090      }
4091  
4092      wp_untrash_post_comments( $post_id );
4093  
4094      /**
4095       * Fires after a post is restored from the Trash.
4096       *
4097       * @since 2.9.0
4098       * @since 5.6.0 Added the `$previous_status` parameter.
4099       *
4100       * @param int    $post_id         Post ID.
4101       * @param string $previous_status The status of the post at the point where it was trashed.
4102       */
4103      do_action( 'untrashed_post', $post_id, $previous_status );
4104  
4105      return $post;
4106  }
4107  
4108  /**
4109   * Moves comments for a post to the Trash.
4110   *
4111   * @since 2.9.0
4112   *
4113   * @global wpdb $wpdb WordPress database abstraction object.
4114   *
4115   * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
4116   * @return mixed|void False on failure.
4117   */
4118  function wp_trash_post_comments( $post = null ) {
4119      global $wpdb;
4120  
4121      $post = get_post( $post );
4122  
4123      if ( ! $post ) {
4124          return;
4125      }
4126  
4127      $post_id = $post->ID;
4128  
4129      /**
4130       * Fires before comments are sent to the Trash.
4131       *
4132       * @since 2.9.0
4133       *
4134       * @param int $post_id Post ID.
4135       */
4136      do_action( 'trash_post_comments', $post_id );
4137  
4138      $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) );
4139  
4140      if ( ! $comments ) {
4141          return;
4142      }
4143  
4144      // Cache current status for each comment.
4145      $statuses = array();
4146      foreach ( $comments as $comment ) {
4147          $statuses[ $comment->comment_ID ] = $comment->comment_approved;
4148      }
4149      add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses );
4150  
4151      // Set status for all comments to post-trashed.
4152      $result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) );
4153  
4154      clean_comment_cache( array_keys( $statuses ) );
4155  
4156      /**
4157       * Fires after comments are sent to the Trash.
4158       *
4159       * @since 2.9.0
4160       *
4161       * @param int   $post_id  Post ID.
4162       * @param array $statuses Array of comment statuses.
4163       */
4164      do_action( 'trashed_post_comments', $post_id, $statuses );
4165  
4166      return $result;
4167  }
4168  
4169  /**
4170   * Restores comments for a post from the Trash.
4171   *
4172   * @since 2.9.0
4173   *
4174   * @global wpdb $wpdb WordPress database abstraction object.
4175   *
4176   * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
4177   * @return true|void
4178   */
4179  function wp_untrash_post_comments( $post = null ) {
4180      global $wpdb;
4181  
4182      $post = get_post( $post );
4183  
4184      if ( ! $post ) {
4185          return;
4186      }
4187  
4188      $post_id = $post->ID;
4189  
4190      $statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );
4191  
4192      if ( ! $statuses ) {
4193          return true;
4194      }
4195  
4196      /**
4197       * Fires before comments are restored for a post from the Trash.
4198       *
4199       * @since 2.9.0
4200       *
4201       * @param int $post_id Post ID.
4202       */
4203      do_action( 'untrash_post_comments', $post_id );
4204  
4205      // Restore each comment to its original status.
4206      $group_by_status = array();
4207      foreach ( $statuses as $comment_id => $comment_status ) {
4208          $group_by_status[ $comment_status ][] = $comment_id;
4209      }
4210  
4211      foreach ( $group_by_status as $status => $comments ) {
4212          // Confidence check. This shouldn't happen.
4213          if ( 'post-trashed' === $status ) {
4214              $status = '0';
4215          }
4216          $comments_in = implode( ', ', array_map( 'intval', $comments ) );
4217          $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) );
4218      }
4219  
4220      clean_comment_cache( array_keys( $statuses ) );
4221  
4222      delete_post_meta( $post_id, '_wp_trash_meta_comments_status' );
4223  
4224      /**
4225       * Fires after comments are restored for a post from the Trash.
4226       *
4227       * @since 2.9.0
4228       *
4229       * @param int $post_id Post ID.
4230       */
4231      do_action( 'untrashed_post_comments', $post_id );
4232  }
4233  
4234  /**
4235   * Retrieves the list of categories for a post.
4236   *
4237   * Compatibility layer for themes and plugins. Also an easy layer of abstraction
4238   * away from the complexity of the taxonomy layer.
4239   *
4240   * @since 2.1.0
4241   *
4242   * @see wp_get_object_terms()
4243   *
4244   * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
4245   *                       global $post. Default 0.
4246   * @param array $args    Optional. Category query parameters. Default empty array.
4247   *                       See WP_Term_Query::__construct() for supported arguments.
4248   * @return array|WP_Error List of categories. If the `$fields` argument passed via `$args` is 'all' or
4249   *                        'all_with_object_id', an array of WP_Term objects will be returned. If `$fields`
4250   *                        is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names.
4251   *                        WP_Error object if 'category' taxonomy doesn't exist.
4252   */
4253  function wp_get_post_categories( $post_id = 0, $args = array() ) {
4254      $post_id = (int) $post_id;
4255  
4256      $defaults = array( 'fields' => 'ids' );
4257      $args     = wp_parse_args( $args, $defaults );
4258  
4259      $cats = wp_get_object_terms( $post_id, 'category', $args );
4260      return $cats;
4261  }
4262  
4263  /**
4264   * Retrieves the tags for a post.
4265   *
4266   * There is only one default for this function, called 'fields' and by default
4267   * is set to 'all'. There are other defaults that can be overridden in
4268   * wp_get_object_terms().
4269   *
4270   * @since 2.3.0
4271   *
4272   * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
4273   *                       global $post. Default 0.
4274   * @param array $args    Optional. Tag query parameters. Default empty array.
4275   *                       See WP_Term_Query::__construct() for supported arguments.
4276   * @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found.
4277   *                        WP_Error object if 'post_tag' taxonomy doesn't exist.
4278   */
4279  function wp_get_post_tags( $post_id = 0, $args = array() ) {
4280      return wp_get_post_terms( $post_id, 'post_tag', $args );
4281  }
4282  
4283  /**
4284   * Retrieves the terms for a post.
4285   *
4286   * @since 2.8.0
4287   *
4288   * @param int             $post_id  Optional. The Post ID. Does not default to the ID of the
4289   *                                  global $post. Default 0.
4290   * @param string|string[] $taxonomy Optional. The taxonomy slug or array of slugs for which
4291   *                                  to retrieve terms. Default 'post_tag'.
4292   * @param array           $args     {
4293   *     Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments.
4294   *
4295   *     @type string $fields Term fields to retrieve. Default 'all'.
4296   * }
4297   * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found.
4298   *                        WP_Error object if `$taxonomy` doesn't exist.
4299   */
4300  function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
4301      $post_id = (int) $post_id;
4302  
4303      $defaults = array( 'fields' => 'all' );
4304      $args     = wp_parse_args( $args, $defaults );
4305  
4306      $tags = wp_get_object_terms( $post_id, $taxonomy, $args );
4307  
4308      return $tags;
4309  }
4310  
4311  /**
4312   * Retrieves a number of recent posts.
4313   *
4314   * @since 1.0.0
4315   *
4316   * @see get_posts()
4317   *
4318   * @param array  $args   Optional. Arguments to retrieve posts. Default empty array.
4319   * @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which
4320   *                       correspond to a WP_Post object or an associative array, respectively.
4321   *                       Default ARRAY_A.
4322   * @return array|false Array of recent posts, where the type of each element is determined
4323   *                     by the `$output` parameter. Empty array on failure.
4324   */
4325  function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
4326  
4327      if ( is_numeric( $args ) ) {
4328          _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
4329          $args = array( 'numberposts' => absint( $args ) );
4330      }
4331  
4332      // Set default arguments.
4333      $defaults = array(
4334          'numberposts'      => 10,
4335          'offset'           => 0,
4336          'category'         => 0,
4337          'orderby'          => 'post_date',
4338          'order'            => 'DESC',
4339          'include'          => '',
4340          'exclude'          => '',
4341          'meta_key'         => '',
4342          'meta_value'       => '',
4343          'post_type'        => 'post',
4344          'post_status'      => 'draft, publish, future, pending, private',
4345          'suppress_filters' => true,
4346      );
4347  
4348      $parsed_args = wp_parse_args( $args, $defaults );
4349  
4350      $results = get_posts( $parsed_args );
4351  
4352      // Backward compatibility. Prior to 3.1 expected posts to be returned in array.
4353      if ( ARRAY_A === $output ) {
4354          foreach ( $results as $key => $result ) {
4355              $results[ $key ] = get_object_vars( $result );
4356          }
4357          return $results ? $results : array();
4358      }
4359  
4360      return $results ? $results : false;
4361  }
4362  
4363  /**
4364   * Inserts or update a post.
4365   *
4366   * If the $postarr parameter has 'ID' set to a value, then post will be updated.
4367   *
4368   * You can set the post date manually, by setting the values for 'post_date'
4369   * and 'post_date_gmt' keys. You can close the comments or open the comments by
4370   * setting the value for 'comment_status' key.
4371   *
4372   * @since 1.0.0
4373   * @since 2.6.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
4374   * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
4375   * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.
4376   * @since 5.6.0 Added the `$fire_after_hooks` parameter.
4377   *
4378   * @see sanitize_post()
4379   * @global wpdb $wpdb WordPress database abstraction object.
4380   *
4381   * @param array $postarr {
4382   *     An array of elements that make up a post to update or insert.
4383   *
4384   *     @type int    $ID                    The post ID. If equal to something other than 0,
4385   *                                         the post with that ID will be updated. Default 0.
4386   *     @type int    $post_author           The ID of the user who added the post. Default is
4387   *                                         the current user ID.
4388   *     @type string $post_date             The date of the post. Default is the current time.
4389   *     @type string $post_date_gmt         The date of the post in the GMT timezone. Default is
4390   *                                         the value of `$post_date`.
4391   *     @type string $post_content          The post content. Default empty.
4392   *     @type string $post_content_filtered The filtered post content. Default empty.
4393   *     @type string $post_title            The post title. Default empty.
4394   *     @type string $post_excerpt          The post excerpt. Default empty.
4395   *     @type string $post_status           The post status. Default 'draft'.
4396   *     @type string $post_type             The post type. Default 'post'.
4397   *     @type string $comment_status        Whether the post can accept comments. Accepts 'open' or 'closed'.
4398   *                                         Default is the value of 'default_comment_status' option.
4399   *     @type string $ping_status           Whether the post can accept pings. Accepts 'open' or 'closed'.
4400   *                                         Default is the value of 'default_ping_status' option.
4401   *     @type string $post_password         The password to access the post. Default empty.
4402   *     @type string $post_name             The post name. Default is the sanitized post title
4403   *                                         when creating a new post.
4404   *     @type string $to_ping               Space or carriage return-separated list of URLs to ping.
4405   *                                         Default empty.
4406   *     @type string $pinged                Space or carriage return-separated list of URLs that have
4407   *                                         been pinged. Default empty.
4408   *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
4409   *     @type int    $menu_order            The order the post should be displayed in. Default 0.
4410   *     @type string $post_mime_type        The mime type of the post. Default empty.
4411   *     @type string $guid                  Global Unique ID for referencing the post. Default empty.
4412   *     @type int    $import_id             The post ID to be used when inserting a new post.
4413   *                                         If specified, must not match any existing post ID. Default 0.
4414   *     @type int[]  $post_category         Array of category IDs.
4415   *                                         Defaults to value of the 'default_category' option.
4416   *     @type array  $tags_input            Array of tag names, slugs, or IDs. Default empty.
4417   *     @type array  $tax_input             An array of taxonomy terms keyed by their taxonomy name.
4418   *                                         If the taxonomy is hierarchical, the term list needs to be
4419   *                                         either an array of term IDs or a comma-separated string of IDs.
4420   *                                         If the taxonomy is non-hierarchical, the term list can be an array
4421   *                                         that contains term names or slugs, or a comma-separated string
4422   *                                         of names or slugs. This is because, in hierarchical taxonomy,
4423   *                                         child terms can have the same names with different parent terms,
4424   *                                         so the only way to connect them is using ID. Default empty.
4425   *     @type array  $meta_input            Array of post meta values keyed by their post meta key. Default empty.
4426   *     @type string $page_template         Page template to use.
4427   * }
4428   * @param bool  $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
4429   * @param bool  $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
4430   * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
4431   */
4432  function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) {
4433      global $wpdb;
4434  
4435      // Capture original pre-sanitized array for passing into filters.
4436      $unsanitized_postarr = $postarr;
4437  
4438      $user_id = get_current_user_id();
4439  
4440      $defaults = array(
4441          'post_author'           => $user_id,
4442          'post_content'          => '',
4443          'post_content_filtered' => '',
4444          'post_title'            => '',
4445          'post_excerpt'          => '',
4446          'post_status'           => 'draft',
4447          'post_type'             => 'post',
4448          'comment_status'        => '',
4449          'ping_status'           => '',
4450          'post_password'         => '',
4451          'to_ping'               => '',
4452          'pinged'                => '',
4453          'post_parent'           => 0,
4454          'menu_order'            => 0,
4455          'guid'                  => '',
4456          'import_id'             => 0,
4457          'context'               => '',
4458          'post_date'             => '',
4459          'post_date_gmt'         => '',
4460      );
4461  
4462      $postarr = wp_parse_args( $postarr, $defaults );
4463  
4464      unset( $postarr['filter'] );
4465  
4466      $postarr = sanitize_post( $postarr, 'db' );
4467  
4468      // Are we updating or creating?
4469      $post_id = 0;
4470      $update  = false;
4471      $guid    = $postarr['guid'];
4472  
4473      if ( ! empty( $postarr['ID'] ) ) {
4474          $update = true;
4475  
4476          // Get the post ID and GUID.
4477          $post_id     = $postarr['ID'];
4478          $post_before = get_post( $post_id );
4479  
4480          if ( is_null( $post_before ) ) {
4481              if ( $wp_error ) {
4482                  return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
4483              }
4484              return 0;
4485          }
4486  
4487          $guid            = get_post_field( 'guid', $post_id );
4488          $previous_status = get_post_field( 'post_status', $post_id );
4489      } else {
4490          $previous_status = 'new';
4491          $post_before     = null;
4492      }
4493  
4494      $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
4495  
4496      $post_title   = $postarr['post_title'];
4497      $post_content = $postarr['post_content'];
4498      $post_excerpt = $postarr['post_excerpt'];
4499  
4500      if ( isset( $postarr['post_name'] ) ) {
4501          $post_name = $postarr['post_name'];
4502      } elseif ( $update ) {
4503          // For an update, don't modify the post_name if it wasn't supplied as an argument.
4504          $post_name = $post_before->post_name;
4505      }
4506  
4507      $maybe_empty = 'attachment' !== $post_type
4508          && ! $post_content && ! $post_title && ! $post_excerpt
4509          && post_type_supports( $post_type, 'editor' )
4510          && post_type_supports( $post_type, 'title' )
4511          && post_type_supports( $post_type, 'excerpt' );
4512  
4513      /**
4514       * Filters whether the post should be considered "empty".
4515       *
4516       * The post is considered "empty" if both:
4517       * 1. The post type supports the title, editor, and excerpt fields
4518       * 2. The title, editor, and excerpt fields are all empty
4519       *
4520       * Returning a truthy value from the filter will effectively short-circuit
4521       * the new post being inserted and return 0. If $wp_error is true, a WP_Error
4522       * will be returned instead.
4523       *
4524       * @since 3.3.0
4525       *
4526       * @param bool  $maybe_empty Whether the post should be considered "empty".
4527       * @param array $postarr     Array of post data.
4528       */
4529      if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
4530          if ( $wp_error ) {
4531              return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
4532          } else {
4533              return 0;
4534          }
4535      }
4536  
4537      $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
4538  
4539      if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
4540          $post_status = 'inherit';
4541      }
4542  
4543      if ( ! empty( $postarr['post_category'] ) ) {
4544          // Filter out empty terms.
4545          $post_category = array_filter( $postarr['post_category'] );
4546      } elseif ( $update && ! isset( $postarr['post_category'] ) ) {
4547          $post_category = $post_before->post_category;
4548      }
4549  
4550      // Make sure we set a valid category.
4551      if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) {
4552          // 'post' requires at least one category.
4553          if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
4554              $post_category = array( get_option( 'default_category' ) );
4555          } else {
4556              $post_category = array();
4557          }
4558      }
4559  
4560      /*
4561       * Don't allow contributors to set the post slug for pending review posts.
4562       *
4563       * For new posts check the primitive capability, for updates check the meta capability.
4564       */
4565      if ( 'pending' === $post_status ) {
4566          $post_type_object = get_post_type_object( $post_type );
4567  
4568          if ( ! $update && $post_type_object && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
4569              $post_name = '';
4570          } elseif ( $update && ! current_user_can( 'publish_post', $post_id ) ) {
4571              $post_name = '';
4572          }
4573      }
4574  
4575      /*
4576       * Create a valid post name. Drafts and pending posts are allowed to have
4577       * an empty post name.
4578       */
4579      if ( empty( $post_name ) ) {
4580          if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) {
4581              $post_name = sanitize_title( $post_title );
4582          } else {
4583              $post_name = '';
4584          }
4585      } else {
4586          // On updates, we need to check to see if it's using the old, fixed sanitization context.
4587          $check_name = sanitize_title( $post_name, '', 'old-save' );
4588  
4589          if ( $update
4590              && strtolower( urlencode( $post_name ) ) === $check_name
4591              && get_post_field( 'post_name', $post_id ) === $check_name
4592          ) {
4593              $post_name = $check_name;
4594          } else { // New post, or slug has changed.
4595              $post_name = sanitize_title( $post_name );
4596          }
4597      }
4598  
4599      /*
4600       * Resolve the post date from any provided post date or post date GMT strings;
4601       * if none are provided, the date will be set to now.
4602       */
4603      $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
4604  
4605      if ( ! $post_date ) {
4606          if ( $wp_error ) {
4607              return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
4608          } else {
4609              return 0;
4610          }
4611      }
4612  
4613      if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
4614          if ( ! in_array( $post_status, get_post_stati( array( 'date_floating' => true ) ), true ) ) {
4615              $post_date_gmt = get_gmt_from_date( $post_date );
4616          } else {
4617              $post_date_gmt = '0000-00-00 00:00:00';
4618          }
4619      } else {
4620          $post_date_gmt = $postarr['post_date_gmt'];
4621      }
4622  
4623      if ( $update || '0000-00-00 00:00:00' === $post_date ) {
4624          $post_modified     = current_time( 'mysql' );
4625          $post_modified_gmt = current_time( 'mysql', 1 );
4626      } else {
4627          $post_modified     = $post_date;
4628          $post_modified_gmt = $post_date_gmt;
4629      }
4630  
4631      if ( 'attachment' !== $post_type ) {
4632          $now = gmdate( 'Y-m-d H:i:s' );
4633  
4634          if ( 'publish' === $post_status ) {
4635              if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) {
4636                  $post_status = 'future';
4637              }
4638          } elseif ( 'future' === $post_status ) {
4639              if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) {
4640                  $post_status = 'publish';
4641              }
4642          }
4643      }
4644  
4645      // Comment status.
4646      if ( empty( $postarr['comment_status'] ) ) {
4647          if ( $update ) {
4648              $comment_status = 'closed';
4649          } else {
4650              $comment_status = get_default_comment_status( $post_type );
4651          }
4652      } else {
4653          $comment_status = $postarr['comment_status'];
4654      }
4655  
4656      // These variables are needed by compact() later.
4657      $post_content_filtered = $postarr['post_content_filtered'];
4658      $post_author           = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
4659      $ping_status           = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
4660      $to_ping               = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
4661      $pinged                = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
4662      $import_id             = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
4663  
4664      /*
4665       * The 'wp_insert_post_parent' filter expects all variables to be present.
4666       * Previously, these variables would have already been extracted
4667       */
4668      if ( isset( $postarr['menu_order'] ) ) {
4669          $menu_order = (int) $postarr['menu_order'];
4670      } else {
4671          $menu_order = 0;
4672      }
4673  
4674      $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
4675      if ( 'private' === $post_status ) {
4676          $post_password = '';
4677      }
4678  
4679      if ( isset( $postarr['post_parent'] ) ) {
4680          $post_parent = (int) $postarr['post_parent'];
4681      } else {
4682          $post_parent = 0;
4683      }
4684  
4685      $new_postarr = array_merge(
4686          array(
4687              'ID' => $post_id,
4688          ),
4689          compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) )
4690      );
4691  
4692      /**
4693       * Filters the post parent -- used to check for and prevent hierarchy loops.
4694       *
4695       * @since 3.1.0
4696       *
4697       * @param int   $post_parent Post parent ID.
4698       * @param int   $post_id     Post ID.
4699       * @param array $new_postarr Array of parsed post data.
4700       * @param array $postarr     Array of sanitized, but otherwise unmodified post data.
4701       */
4702      $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_id, $new_postarr, $postarr );
4703  
4704      /*
4705       * If the post is being untrashed and it has a desired slug stored in post meta,
4706       * reassign it.
4707       */
4708      if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
4709          $desired_post_slug = get_post_meta( $post_id, '_wp_desired_post_slug', true );
4710  
4711          if ( $desired_post_slug ) {
4712              delete_post_meta( $post_id, '_wp_desired_post_slug' );
4713              $post_name = $desired_post_slug;
4714          }
4715      }
4716  
4717      // If a trashed post has the desired slug, change it and let this post have it.
4718      if ( 'trash' !== $post_status && $post_name ) {
4719          /**
4720           * Filters whether or not to add a `__trashed` suffix to trashed posts that match the name of the updated post.
4721           *
4722           * @since 5.4.0
4723           *
4724           * @param bool   $add_trashed_suffix Whether to attempt to add the suffix.
4725           * @param string $post_name          The name of the post being updated.
4726           * @param int    $post_id            Post ID.
4727           */
4728          $add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_id );
4729  
4730          if ( $add_trashed_suffix ) {
4731              wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id );
4732          }
4733      }
4734  
4735      // When trashing an existing post, change its slug to allow non-trashed posts to use it.
4736      if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {
4737          $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_id );
4738      }
4739  
4740      $post_name = wp_unique_post_slug( $post_name, $post_id, $post_status, $post_type, $post_parent );
4741  
4742      // Don't unslash.
4743      $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
4744  
4745      // Expected_slashed (everything!).
4746      $data = compact(
4747          'post_author',
4748          'post_date',
4749          'post_date_gmt',
4750          'post_content',
4751          'post_content_filtered',
4752          'post_title',
4753          'post_excerpt',
4754          'post_status',
4755          'post_type',
4756          'comment_status',
4757          'ping_status',
4758          'post_password',
4759          'post_name',
4760          'to_ping',
4761          'pinged',
4762          'post_modified',
4763          'post_modified_gmt',
4764          'post_parent',
4765          'menu_order',
4766          'post_mime_type',
4767          'guid'
4768      );
4769  
4770      $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' );
4771  
4772      foreach ( $emoji_fields as $emoji_field ) {
4773          if ( isset( $data[ $emoji_field ] ) ) {
4774              $charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field );
4775  
4776              if ( 'utf8' === $charset ) {
4777                  $data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] );
4778              }
4779          }
4780      }
4781  
4782      if ( 'attachment' === $post_type ) {
4783          /**
4784           * Filters attachment post data before it is updated in or added to the database.
4785           *
4786           * @since 3.9.0
4787           * @since 5.4.1 The `$unsanitized_postarr` parameter was added.
4788           * @since 6.0.0 The `$update` parameter was added.
4789           *
4790           * @param array $data                An array of slashed, sanitized, and processed attachment post data.
4791           * @param array $postarr             An array of slashed and sanitized attachment post data, but not processed.
4792           * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
4793           *                                   as originally passed to wp_insert_post().
4794           * @param bool  $update              Whether this is an existing attachment post being updated.
4795           */
4796          $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update );
4797      } else {
4798          /**
4799           * Filters slashed post data just before it is inserted into the database.
4800           *
4801           * @since 2.7.0
4802           * @since 5.4.1 The `$unsanitized_postarr` parameter was added.
4803           * @since 6.0.0 The `$update` parameter was added.
4804           *
4805           * @param array $data                An array of slashed, sanitized, and processed post data.
4806           * @param array $postarr             An array of sanitized (and slashed) but otherwise unmodified post data.
4807           * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
4808           *                                   originally passed to wp_insert_post().
4809           * @param bool  $update              Whether this is an existing post being updated.
4810           */
4811          $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update );
4812      }
4813  
4814      $data  = wp_unslash( $data );
4815      $where = array( 'ID' => $post_id );
4816  
4817      if ( $update ) {
4818          /**
4819           * Fires immediately before an existing post is updated in the database.
4820           *
4821           * @since 2.5.0
4822           *
4823           * @param int   $post_id Post ID.
4824           * @param array $data    Array of unslashed post data.
4825           */
4826          do_action( 'pre_post_update', $post_id, $data );
4827  
4828          if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
4829              if ( $wp_error ) {
4830                  if ( 'attachment' === $post_type ) {
4831                      $message = __( 'Could not update attachment in the database.' );
4832                  } else {
4833                      $message = __( 'Could not update post in the database.' );
4834                  }
4835  
4836                  return new WP_Error( 'db_update_error', $message, $wpdb->last_error );
4837              } else {
4838                  return 0;
4839              }
4840          }
4841      } else {
4842          // If there is a suggested ID, use it if not already present.
4843          if ( ! empty( $import_id ) ) {
4844              $import_id = (int) $import_id;
4845  
4846              if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
4847                  $data['ID'] = $import_id;
4848              }
4849          }
4850  
4851          if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
4852              if ( $wp_error ) {
4853                  if ( 'attachment' === $post_type ) {
4854                      $message = __( 'Could not insert attachment into the database.' );
4855                  } else {
4856                      $message = __( 'Could not insert post into the database.' );
4857                  }
4858  
4859                  return new WP_Error( 'db_insert_error', $message, $wpdb->last_error );
4860              } else {
4861                  return 0;
4862              }
4863          }
4864  
4865          $post_id = (int) $wpdb->insert_id;
4866  
4867          // Use the newly generated $post_id.
4868          $where = array( 'ID' => $post_id );
4869      }
4870  
4871      if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) {
4872          $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_id ), $post_id, $data['post_status'], $post_type, $post_parent );
4873  
4874          $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
4875          clean_post_cache( $post_id );
4876      }
4877  
4878      if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
4879          wp_set_post_categories( $post_id, $post_category );
4880      }
4881  
4882      if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
4883          wp_set_post_tags( $post_id, $postarr['tags_input'] );
4884      }
4885  
4886      // Add default term for all associated custom taxonomies.
4887      if ( 'auto-draft' !== $post_status ) {
4888          foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
4889  
4890              if ( ! empty( $tax_object->default_term ) ) {
4891  
4892                  // Filter out empty terms.
4893                  if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
4894                      $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
4895                  }
4896  
4897                  // Passed custom taxonomy list overwrites the existing list if not empty.
4898                  $terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
4899                  if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) {
4900                      $postarr['tax_input'][ $taxonomy ] = $terms;
4901                  }
4902  
4903                  if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) {
4904                      $default_term_id = get_option( 'default_term_' . $taxonomy );
4905                      if ( ! empty( $default_term_id ) ) {
4906                          $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id );
4907                      }
4908                  }
4909              }
4910          }
4911      }
4912  
4913      // New-style support for all custom taxonomies.
4914      if ( ! empty( $postarr['tax_input'] ) ) {
4915          foreach ( $postarr['tax_input'] as $taxonomy => $tags ) {
4916              $taxonomy_obj = get_taxonomy( $taxonomy );
4917  
4918              if ( ! $taxonomy_obj ) {
4919                  /* translators: %s: Taxonomy name. */
4920                  _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' );
4921                  continue;
4922              }
4923  
4924              // array = hierarchical, string = non-hierarchical.
4925              if ( is_array( $tags ) ) {
4926                  $tags = array_filter( $tags );
4927              }
4928  
4929              if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
4930                  wp_set_post_terms( $post_id, $tags, $taxonomy );
4931              }
4932          }
4933      }
4934  
4935      if ( ! empty( $postarr['meta_input'] ) ) {
4936          foreach ( $postarr['meta_input'] as $field => $value ) {
4937              update_post_meta( $post_id, $field, $value );
4938          }
4939      }
4940  
4941      $current_guid = get_post_field( 'guid', $post_id );
4942  
4943      // Set GUID.
4944      if ( ! $update && '' === $current_guid ) {
4945          $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where );
4946      }
4947  
4948      if ( 'attachment' === $postarr['post_type'] ) {
4949          if ( ! empty( $postarr['file'] ) ) {
4950              update_attached_file( $post_id, $postarr['file'] );
4951          }
4952  
4953          if ( ! empty( $postarr['context'] ) ) {
4954              add_post_meta( $post_id, '_wp_attachment_context', $postarr['context'], true );
4955          }
4956      }
4957  
4958      // Set or remove featured image.
4959      if ( isset( $postarr['_thumbnail_id'] ) ) {
4960          $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type;
4961  
4962          if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
4963              if ( wp_attachment_is( 'audio', $post_id ) ) {
4964                  $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
4965              } elseif ( wp_attachment_is( 'video', $post_id ) ) {
4966                  $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
4967              }
4968          }
4969  
4970          if ( $thumbnail_support ) {
4971              $thumbnail_id = (int) $postarr['_thumbnail_id'];
4972              if ( -1 === $thumbnail_id ) {
4973                  delete_post_thumbnail( $post_id );
4974              } else {
4975                  set_post_thumbnail( $post_id, $thumbnail_id );
4976              }
4977          }
4978      }
4979  
4980      clean_post_cache( $post_id );
4981  
4982      $post = get_post( $post_id );
4983  
4984      if ( ! empty( $postarr['page_template'] ) ) {
4985          $post->page_template = $postarr['page_template'];
4986          $page_templates      = wp_get_theme()->get_page_templates( $post );
4987  
4988          if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
4989              if ( $wp_error ) {
4990                  return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) );
4991              }
4992  
4993              update_post_meta( $post_id, '_wp_page_template', 'default' );
4994          } else {
4995              update_post_meta( $post_id, '_wp_page_template', $postarr['page_template'] );
4996          }
4997      }
4998  
4999      if ( 'attachment' !== $postarr['post_type'] ) {
5000          wp_transition_post_status( $data['post_status'], $previous_status, $post );
5001      } else {
5002          if ( $update ) {
5003              /**
5004               * Fires once an existing attachment has been updated.
5005               *
5006               * @since 2.0.0
5007               *
5008               * @param int $post_id Attachment ID.
5009               */
5010              do_action( 'edit_attachment', $post_id );
5011  
5012              $post_after = get_post( $post_id );
5013  
5014              /**
5015               * Fires once an existing attachment has been updated.
5016               *
5017               * @since 4.4.0
5018               *
5019               * @param int     $post_id      Post ID.
5020               * @param WP_Post $post_after   Post object following the update.
5021               * @param WP_Post $post_before  Post object before the update.
5022               */
5023              do_action( 'attachment_updated', $post_id, $post_after, $post_before );
5024          } else {
5025  
5026              /**
5027               * Fires once an attachment has been added.
5028               *
5029               * @since 2.0.0
5030               *
5031               * @param int $post_id Attachment ID.
5032               */
5033              do_action( 'add_attachment', $post_id );
5034          }
5035  
5036          return $post_id;
5037      }
5038  
5039      if ( $update ) {
5040          /**
5041           * Fires once an existing post has been updated.
5042           *
5043           * The dynamic portion of the hook name, `$post->post_type`, refers to
5044           * the post type slug.
5045           *
5046           * Possible hook names include:
5047           *
5048           *  - `edit_post_post`
5049           *  - `edit_post_page`
5050           *
5051           * @since 5.1.0
5052           *
5053           * @param int     $post_id Post ID.
5054           * @param WP_Post $post    Post object.
5055           */
5056          do_action( "edit_post_{$post->post_type}", $post_id, $post );
5057  
5058          /**
5059           * Fires once an existing post has been updated.
5060           *
5061           * @since 1.2.0
5062           *
5063           * @param int     $post_id Post ID.
5064           * @param WP_Post $post    Post object.
5065           */
5066          do_action( 'edit_post', $post_id, $post );
5067  
5068          $post_after = get_post( $post_id );
5069  
5070          /**
5071           * Fires once an existing post has been updated.
5072           *
5073           * @since 3.0.0
5074           *
5075           * @param int     $post_id      Post ID.
5076           * @param WP_Post $post_after   Post object following the update.
5077           * @param WP_Post $post_before  Post object before the update.
5078           */
5079          do_action( 'post_updated', $post_id, $post_after, $post_before );
5080      }
5081  
5082      /**
5083       * Fires once a post has been saved.
5084       *
5085       * The dynamic portion of the hook name, `$post->post_type`, refers to
5086       * the post type slug.
5087       *
5088       * Possible hook names include:
5089       *
5090       *  - `save_post_post`
5091       *  - `save_post_page`
5092       *
5093       * @since 3.7.0
5094       *
5095       * @param int     $post_id Post ID.
5096       * @param WP_Post $post    Post object.
5097       * @param bool    $update  Whether this is an existing post being updated.
5098       */
5099      do_action( "save_post_{$post->post_type}", $post_id, $post, $update );
5100  
5101      /**
5102       * Fires once a post has been saved.
5103       *
5104       * @since 1.5.0
5105       *
5106       * @param int     $post_id Post ID.
5107       * @param WP_Post $post    Post object.
5108       * @param bool    $update  Whether this is an existing post being updated.
5109       */
5110      do_action( 'save_post', $post_id, $post, $update );
5111  
5112      /**
5113       * Fires once a post has been saved.
5114       *
5115       * @since 2.0.0
5116       *
5117       * @param int     $post_id Post ID.
5118       * @param WP_Post $post    Post object.
5119       * @param bool    $update  Whether this is an existing post being updated.
5120       */
5121      do_action( 'wp_insert_post', $post_id, $post, $update );
5122  
5123      if ( $fire_after_hooks ) {
5124          wp_after_insert_post( $post, $update, $post_before );
5125      }
5126  
5127      return $post_id;
5128  }
5129  
5130  /**
5131   * Updates a post with new post data.
5132   *
5133   * The date does not have to be set for drafts. You can set the date and it will
5134   * not be overridden.
5135   *
5136   * @since 1.0.0
5137   * @since 3.5.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
5138   * @since 5.6.0 Added the `$fire_after_hooks` parameter.
5139   *
5140   * @param array|object $postarr          Optional. Post data. Arrays are expected to be escaped,
5141   *                                       objects are not. See wp_insert_post() for accepted arguments.
5142   *                                       Default array.
5143   * @param bool         $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
5144   * @param bool         $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
5145   * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
5146   */
5147  function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) {
5148      if ( is_object( $postarr ) ) {
5149          // Non-escaped post was passed.
5150          $postarr = get_object_vars( $postarr );
5151          $postarr = wp_slash( $postarr );
5152      }
5153  
5154      // First, get all of the original fields.
5155      $post = get_post( $postarr['ID'], ARRAY_A );
5156  
5157      if ( is_null( $post ) ) {
5158          if ( $wp_error ) {
5159              return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
5160          }
5161          return 0;
5162      }
5163  
5164      // Escape data pulled from DB.
5165      $post = wp_slash( $post );
5166  
5167      // Passed post category list overwrites existing category list if not empty.
5168      if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] )
5169          && count( $postarr['post_category'] ) > 0
5170      ) {
5171          $post_cats = $postarr['post_category'];
5172      } else {
5173          $post_cats = $post['post_category'];
5174      }
5175  
5176      // Drafts shouldn't be assigned a date unless explicitly done so by the user.
5177      if ( isset( $post['post_status'] )
5178          && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true )
5179          && empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] )
5180      ) {
5181          $clear_date = true;
5182      } else {
5183          $clear_date = false;
5184      }
5185  
5186      // Merge old and new fields with new fields overwriting old ones.
5187      $postarr                  = array_merge( $post, $postarr );
5188      $postarr['post_category'] = $post_cats;
5189      if ( $clear_date ) {
5190          $postarr['post_date']     = current_time( 'mysql' );
5191          $postarr['post_date_gmt'] = '';
5192      }
5193  
5194      if ( 'attachment' === $postarr['post_type'] ) {
5195          return wp_insert_attachment( $postarr, false, 0, $wp_error );
5196      }
5197  
5198      // Discard 'tags_input' parameter if it's the same as existing post tags.
5199      if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
5200          $tags      = get_the_terms( $postarr['ID'], 'post_tag' );
5201          $tag_names = array();
5202  
5203          if ( $tags && ! is_wp_error( $tags ) ) {
5204              $tag_names = wp_list_pluck( $tags, 'name' );
5205          }
5206  
5207          if ( $postarr['tags_input'] === $tag_names ) {
5208              unset( $postarr['tags_input'] );
5209          }
5210      }
5211  
5212      return wp_insert_post( $postarr, $wp_error, $fire_after_hooks );
5213  }
5214  
5215  /**
5216   * Publishes a post by transitioning the post status.
5217   *
5218   * @since 2.1.0
5219   *
5220   * @global wpdb $wpdb WordPress database abstraction object.
5221   *
5222   * @param int|WP_Post $post Post ID or post object.
5223   */
5224  function wp_publish_post( $post ) {
5225      global $wpdb;
5226  
5227      $post = get_post( $post );
5228  
5229      if ( ! $post ) {
5230          return;
5231      }
5232  
5233      if ( 'publish' === $post->post_status ) {
5234          return;
5235      }
5236  
5237      $post_before = get_post( $post->ID );
5238  
5239      // Ensure at least one term is applied for taxonomies with a default term.
5240      foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
5241          // Skip taxonomy if no default term is set.
5242          if (
5243              'category' !== $taxonomy &&
5244              empty( $tax_object->default_term )
5245          ) {
5246              continue;
5247          }
5248  
5249          // Do not modify previously set terms.
5250          if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
5251              continue;
5252          }
5253  
5254          if ( 'category' === $taxonomy ) {
5255              $default_term_id = (int) get_option( 'default_category', 0 );
5256          } else {
5257              $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
5258          }
5259  
5260          if ( ! $default_term_id ) {
5261              continue;
5262          }
5263          wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
5264      }
5265  
5266      $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
5267  
5268      clean_post_cache( $post->ID );
5269  
5270      $old_status        = $post->post_status;
5271      $post->post_status = 'publish';
5272      wp_transition_post_status( 'publish', $old_status, $post );
5273  
5274      /** This action is documented in wp-includes/post.php */
5275      do_action( "edit_post_{$post->post_type}", $post->ID, $post );
5276  
5277      /** This action is documented in wp-includes/post.php */
5278      do_action( 'edit_post', $post->ID, $post );
5279  
5280      /** This action is documented in wp-includes/post.php */
5281      do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
5282  
5283      /** This action is documented in wp-includes/post.php */
5284      do_action( 'save_post', $post->ID, $post, true );
5285  
5286      /** This action is documented in wp-includes/post.php */
5287      do_action( 'wp_insert_post', $post->ID, $post, true );
5288  
5289      wp_after_insert_post( $post, true, $post_before );
5290  }
5291  
5292  /**
5293   * Publishes future post and make sure post ID has future post status.
5294   *
5295   * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
5296   * from publishing drafts, etc.
5297   *
5298   * @since 2.5.0
5299   *
5300   * @param int|WP_Post $post Post ID or post object.
5301   */
5302  function check_and_publish_future_post( $post ) {
5303      $post = get_post( $post );
5304  
5305      if ( ! $post ) {
5306          return;
5307      }
5308  
5309      if ( 'future' !== $post->post_status ) {
5310          return;
5311      }
5312  
5313      $time = strtotime( $post->post_date_gmt . ' GMT' );
5314  
5315      // Uh oh, someone jumped the gun!
5316      if ( $time > time() ) {
5317          wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); // Clear anything else in the system.
5318          wp_schedule_single_event( $time, 'publish_future_post', array( $post->ID ) );
5319          return;
5320      }
5321  
5322      // wp_publish_post() returns no meaningful value.
5323      wp_publish_post( $post->ID );
5324  }
5325  
5326  /**
5327   * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
5328   * If post_date is not provided, this first checks post_date_gmt if provided,
5329   * then falls back to use the current time.
5330   *
5331   * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
5332   * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
5333   *
5334   * @since 5.7.0
5335   *
5336   * @param string $post_date     The date in mysql format (`Y-m-d H:i:s`).
5337   * @param string $post_date_gmt The GMT date in mysql format (`Y-m-d H:i:s`).
5338   * @return string|false A valid Gregorian-calendar date string, or false on failure.
5339   */
5340  function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
5341      // If the date is empty, set the date to now.
5342      if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
5343          if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
5344              $post_date = current_time( 'mysql' );
5345          } else {
5346              $post_date = get_date_from_gmt( $post_date_gmt );
5347          }
5348      }
5349  
5350      // Validate the date.
5351      $month = (int) substr( $post_date, 5, 2 );
5352      $day   = (int) substr( $post_date, 8, 2 );
5353      $year  = (int) substr( $post_date, 0, 4 );
5354  
5355      $valid_date = wp_checkdate( $month, $day, $year, $post_date );
5356  
5357      if ( ! $valid_date ) {
5358          return false;
5359      }
5360      return $post_date;
5361  }
5362  
5363  /**
5364   * Computes a unique slug for the post, when given the desired slug and some post details.
5365   *
5366   * @since 2.8.0
5367   *
5368   * @global wpdb       $wpdb       WordPress database abstraction object.
5369   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
5370   *
5371   * @param string $slug        The desired slug (post_name).
5372   * @param int    $post_id     Post ID.
5373   * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
5374   * @param string $post_type   Post type.
5375   * @param int    $post_parent Post parent ID.
5376   * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
5377   */
5378  function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent ) {
5379      if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true )
5380          || ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type
5381      ) {
5382          return $slug;
5383      }
5384  
5385      /**
5386       * Filters the post slug before it is generated to be unique.
5387       *
5388       * Returning a non-null value will short-circuit the
5389       * unique slug generation, returning the passed value instead.
5390       *
5391       * @since 5.1.0
5392       *
5393       * @param string|null $override_slug Short-circuit return value.
5394       * @param string      $slug          The desired slug (post_name).
5395       * @param int         $post_id       Post ID.
5396       * @param string      $post_status   The post status.
5397       * @param string      $post_type     Post type.
5398       * @param int         $post_parent   Post parent ID.
5399       */
5400      $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_id, $post_status, $post_type, $post_parent );
5401      if ( null !== $override_slug ) {
5402          return $override_slug;
5403      }
5404  
5405      global $wpdb, $wp_rewrite;
5406  
5407      $original_slug = $slug;
5408  
5409      $feeds = $wp_rewrite->feeds;
5410      if ( ! is_array( $feeds ) ) {
5411          $feeds = array();
5412      }
5413  
5414      if ( 'attachment' === $post_type ) {
5415          // Attachment slugs must be unique across all types.
5416          $check_sql       = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
5417          $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_id ) );
5418  
5419          /**
5420           * Filters whether the post slug would make a bad attachment slug.
5421           *
5422           * @since 3.1.0
5423           *
5424           * @param bool   $bad_slug Whether the slug would be bad as an attachment slug.
5425           * @param string $slug     The post slug.
5426           */
5427          $is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug );
5428  
5429          if ( $post_name_check
5430              || in_array( $slug, $feeds, true ) || 'embed' === $slug
5431              || $is_bad_attachment_slug
5432          ) {
5433              $suffix = 2;
5434              do {
5435                  $alt_post_name   = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
5436                  $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) );
5437                  ++$suffix;
5438              } while ( $post_name_check );
5439              $slug = $alt_post_name;
5440          }
5441      } elseif ( is_post_type_hierarchical( $post_type ) ) {
5442          if ( 'nav_menu_item' === $post_type ) {
5443              return $slug;
5444          }
5445  
5446          /*
5447           * Page slugs must be unique within their own trees. Pages are in a separate
5448           * namespace than posts so page slugs are allowed to overlap post slugs.
5449           */
5450          $check_sql       = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
5451          $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id, $post_parent ) );
5452  
5453          /**
5454           * Filters whether the post slug would make a bad hierarchical post slug.
5455           *
5456           * @since 3.1.0
5457           *
5458           * @param bool   $bad_slug    Whether the post slug would be bad in a hierarchical post context.
5459           * @param string $slug        The post slug.
5460           * @param string $post_type   Post type.
5461           * @param int    $post_parent Post parent ID.
5462           */
5463          $is_bad_hierarchical_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent );
5464  
5465          if ( $post_name_check
5466              || in_array( $slug, $feeds, true ) || 'embed' === $slug
5467              || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )
5468              || $is_bad_hierarchical_slug
5469          ) {
5470              $suffix = 2;
5471              do {
5472                  $alt_post_name   = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
5473                  $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id, $post_parent ) );
5474                  ++$suffix;
5475              } while ( $post_name_check );
5476              $slug = $alt_post_name;
5477          }
5478      } else {
5479          // Post slugs must be unique across all posts.
5480          $check_sql       = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
5481          $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id ) );
5482  
5483          $post = get_post( $post_id );
5484  
5485          // Prevent new post slugs that could result in URLs that conflict with date archives.
5486          $conflicts_with_date_archive = false;
5487          if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) {
5488              $slug_num = (int) $slug;
5489  
5490              if ( $slug_num ) {
5491                  $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
5492                  $postname_index = array_search( '%postname%', $permastructs, true );
5493  
5494                  /*
5495                  * Potential date clashes are as follows:
5496                  *
5497                  * - Any integer in the first permastruct position could be a year.
5498                  * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
5499                  * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
5500                  */
5501                  if ( 0 === $postname_index ||
5502                      ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) ||
5503                      ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num )
5504                  ) {
5505                      $conflicts_with_date_archive = true;
5506                  }
5507              }
5508          }
5509  
5510          /**
5511           * Filters whether the post slug would be bad as a flat slug.
5512           *
5513           * @since 3.1.0
5514           *
5515           * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
5516           * @param string $slug      The post slug.
5517           * @param string $post_type Post type.
5518           */
5519          $is_bad_flat_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type );
5520  
5521          if ( $post_name_check
5522              || in_array( $slug, $feeds, true ) || 'embed' === $slug
5523              || $conflicts_with_date_archive
5524              || $is_bad_flat_slug
5525          ) {
5526              $suffix = 2;
5527              do {
5528                  $alt_post_name   = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
5529                  $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id ) );
5530                  ++$suffix;
5531              } while ( $post_name_check );
5532              $slug = $alt_post_name;
5533          }
5534      }
5535  
5536      /**
5537       * Filters the unique post slug.
5538       *
5539       * @since 3.3.0
5540       *
5541       * @param string $slug          The post slug.
5542       * @param int    $post_id       Post ID.
5543       * @param string $post_status   The post status.
5544       * @param string $post_type     Post type.
5545       * @param int    $post_parent   Post parent ID
5546       * @param string $original_slug The original post slug.
5547       */
5548      return apply_filters( 'wp_unique_post_slug', $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug );
5549  }
5550  
5551  /**
5552   * Truncates a post slug.
5553   *
5554   * @since 3.6.0
5555   * @access private
5556   *
5557   * @see utf8_uri_encode()
5558   *
5559   * @param string $slug   The slug to truncate.
5560   * @param int    $length Optional. Max length of the slug. Default 200 (characters).
5561   * @return string The truncated slug.
5562   */
5563  function _truncate_post_slug( $slug, $length = 200 ) {
5564      if ( strlen( $slug ) > $length ) {
5565          $decoded_slug = urldecode( $slug );
5566          if ( $decoded_slug === $slug ) {
5567              $slug = substr( $slug, 0, $length );
5568          } else {
5569              $slug = utf8_uri_encode( $decoded_slug, $length, true );
5570          }
5571      }
5572  
5573      return rtrim( $slug, '-' );
5574  }
5575  
5576  /**
5577   * Adds tags to a post.
5578   *
5579   * @see wp_set_post_tags()
5580   *
5581   * @since 2.3.0
5582   *
5583   * @param int          $post_id Optional. The Post ID. Does not default to the ID of the global $post.
5584   * @param string|array $tags    Optional. An array of tags to set for the post, or a string of tags
5585   *                              separated by commas. Default empty.
5586   * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure.
5587   */
5588  function wp_add_post_tags( $post_id = 0, $tags = '' ) {
5589      return wp_set_post_tags( $post_id, $tags, true );
5590  }
5591  
5592  /**
5593   * Sets the tags for a post.
5594   *
5595   * @since 2.3.0
5596   *
5597   * @see wp_set_object_terms()
5598   *
5599   * @param int          $post_id Optional. The Post ID. Does not default to the ID of the global $post.
5600   * @param string|array $tags    Optional. An array of tags to set for the post, or a string of tags
5601   *                              separated by commas. Default empty.
5602   * @param bool         $append  Optional. If true, don't delete existing tags, just add on. If false,
5603   *                              replace the tags with the new tags. Default false.
5604   * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
5605   */
5606  function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
5607      return wp_set_post_terms( $post_id, $tags, 'post_tag', $append );
5608  }
5609  
5610  /**
5611   * Sets the terms for a post.
5612   *
5613   * @since 2.8.0
5614   *
5615   * @see wp_set_object_terms()
5616   *
5617   * @param int          $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
5618   * @param string|array $terms    Optional. An array of terms to set for the post, or a string of terms
5619   *                               separated by commas. Hierarchical taxonomies must always pass IDs rather
5620   *                               than names so that children with the same names but different parents
5621   *                               aren't confused. Default empty.
5622   * @param string       $taxonomy Optional. Taxonomy name. Default 'post_tag'.
5623   * @param bool         $append   Optional. If true, don't delete existing terms, just add on. If false,
5624   *                               replace the terms with the new terms. Default false.
5625   * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
5626   */
5627  function wp_set_post_terms( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false ) {
5628      $post_id = (int) $post_id;
5629  
5630      if ( ! $post_id ) {
5631          return false;
5632      }
5633  
5634      if ( empty( $terms ) ) {
5635          $terms = array();
5636      }
5637  
5638      if ( ! is_array( $terms ) ) {
5639          $comma = _x( ',', 'tag delimiter' );
5640          if ( ',' !== $comma ) {
5641              $terms = str_replace( $comma, ',', $terms );
5642          }
5643          $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
5644      }
5645  
5646      /*
5647       * Hierarchical taxonomies must always pass IDs rather than names so that
5648       * children with the same names but different parents aren't confused.
5649       */
5650      if ( is_taxonomy_hierarchical( $taxonomy ) ) {
5651          $terms = array_unique( array_map( 'intval', $terms ) );
5652      }
5653  
5654      return wp_set_object_terms( $post_id, $terms, $taxonomy, $append );
5655  }
5656  
5657  /**
5658   * Sets categories for a post.
5659   *
5660   * If no categories are provided, the default category is used.
5661   *
5662   * @since 2.1.0
5663   *
5664   * @param int       $post_id         Optional. The Post ID. Does not default to the ID
5665   *                                   of the global $post. Default 0.
5666   * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category.
5667   *                                   Default empty array.
5668   * @param bool      $append          If true, don't delete existing categories, just add on.
5669   *                                   If false, replace the categories with the new categories.
5670   * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
5671   */
5672  function wp_set_post_categories( $post_id = 0, $post_categories = array(), $append = false ) {
5673      $post_id     = (int) $post_id;
5674      $post_type   = get_post_type( $post_id );
5675      $post_status = get_post_status( $post_id );
5676  
5677      // If $post_categories isn't already an array, make it one.
5678      $post_categories = (array) $post_categories;
5679  
5680      if ( empty( $post_categories ) ) {
5681          /**
5682           * Filters post types (in addition to 'post') that require a default category.
5683           *
5684           * @since 5.5.0
5685           *
5686           * @param string[] $post_types An array of post type names. Default empty array.
5687           */
5688          $default_category_post_types = apply_filters( 'default_category_post_types', array() );
5689  
5690          // Regular posts always require a default category.
5691          $default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) );
5692  
5693          if ( in_array( $post_type, $default_category_post_types, true )
5694              && is_object_in_taxonomy( $post_type, 'category' )
5695              && 'auto-draft' !== $post_status
5696          ) {
5697              $post_categories = array( get_option( 'default_category' ) );
5698              $append          = false;
5699          } else {
5700              $post_categories = array();
5701          }
5702      } elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) {
5703          return true;
5704      }
5705  
5706      return wp_set_post_terms( $post_id, $post_categories, 'category', $append );
5707  }
5708  
5709  /**
5710   * Fires actions related to the transitioning of a post's status.
5711   *
5712   * When a post is saved, the post status is "transitioned" from one status to another,
5713   * though this does not always mean the status has actually changed before and after
5714   * the save. This function fires a number of action hooks related to that transition:
5715   * the generic {@see 'transition_post_status'} action, as well as the dynamic hooks
5716   * {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note
5717   * that the function does not transition the post object in the database.
5718   *
5719   * For instance: When publishing a post for the first time, the post status may transition
5720   * from 'draft' – or some other status – to 'publish'. However, if a post is already
5721   * published and is simply being updated, the "old" and "new" statuses may both be 'publish'
5722   * before and after the transition.
5723   *
5724   * @since 2.3.0
5725   *
5726   * @param string  $new_status Transition to this post status.
5727   * @param string  $old_status Previous post status.
5728   * @param WP_Post $post Post data.
5729   */
5730  function wp_transition_post_status( $new_status, $old_status, $post ) {
5731      /**
5732       * Fires when a post is transitioned from one status to another.
5733       *
5734       * @since 2.3.0
5735       *
5736       * @param string  $new_status New post status.
5737       * @param string  $old_status Old post status.
5738       * @param WP_Post $post       Post object.
5739       */
5740      do_action( 'transition_post_status', $new_status, $old_status, $post );
5741  
5742      /**
5743       * Fires when a post is transitioned from one status to another.
5744       *
5745       * The dynamic portions of the hook name, `$new_status` and `$old_status`,
5746       * refer to the old and new post statuses, respectively.
5747       *
5748       * Possible hook names include:
5749       *
5750       *  - `draft_to_publish`
5751       *  - `publish_to_trash`
5752       *  - `pending_to_draft`
5753       *
5754       * @since 2.3.0
5755       *
5756       * @param WP_Post $post Post object.
5757       */
5758      do_action( "{$old_status}_to_{$new_status}", $post );
5759  
5760      /**
5761       * Fires when a post is transitioned from one status to another.
5762       *
5763       * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
5764       * refer to the new post status and post type, respectively.
5765       *
5766       * Possible hook names include:
5767       *
5768       *  - `draft_post`
5769       *  - `future_post`
5770       *  - `pending_post`
5771       *  - `private_post`
5772       *  - `publish_post`
5773       *  - `trash_post`
5774       *  - `draft_page`
5775       *  - `future_page`
5776       *  - `pending_page`
5777       *  - `private_page`
5778       *  - `publish_page`
5779       *  - `trash_page`
5780       *  - `publish_attachment`
5781       *  - `trash_attachment`
5782       *
5783       * Please note: When this action is hooked using a particular post status (like
5784       * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
5785       * first transitioned to that status from something else, as well as upon
5786       * subsequent post updates (old and new status are both the same).
5787       *
5788       * Therefore, if you are looking to only fire a callback when a post is first
5789       * transitioned to a status, use the {@see 'transition_post_status'} hook instead.
5790       *
5791       * @since 2.3.0
5792       * @since 5.9.0 Added `$old_status` parameter.
5793       *
5794       * @param int     $post_id    Post ID.
5795       * @param WP_Post $post       Post object.
5796       * @param string  $old_status Old post status.
5797       */
5798      do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status );
5799  }
5800  
5801  /**
5802   * Fires actions after a post, its terms and meta data has been saved.
5803   *
5804   * @since 5.6.0
5805   *
5806   * @param int|WP_Post  $post        The post ID or object that has been saved.
5807   * @param bool         $update      Whether this is an existing post being updated.
5808   * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
5809   *                                  to the update for updated posts.
5810   */
5811  function wp_after_insert_post( $post, $update, $post_before ) {
5812      $post = get_post( $post );
5813  
5814      if ( ! $post ) {
5815          return;
5816      }
5817  
5818      $post_id = $post->ID;
5819  
5820      /**
5821       * Fires once a post, its terms and meta data has been saved.
5822       *
5823       * @since 5.6.0
5824       *
5825       * @param int          $post_id     Post ID.
5826       * @param WP_Post      $post        Post object.
5827       * @param bool         $update      Whether this is an existing post being updated.
5828       * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
5829       *                                  to the update for updated posts.
5830       */
5831      do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before );
5832  }
5833  
5834  //
5835  // Comment, trackback, and pingback functions.
5836  //
5837  
5838  /**
5839   * Adds a URL to those already pinged.
5840   *
5841   * @since 1.5.0
5842   * @since 4.7.0 `$post` can be a WP_Post object.
5843   * @since 4.7.0 `$uri` can be an array of URIs.
5844   *
5845   * @global wpdb $wpdb WordPress database abstraction object.
5846   *
5847   * @param int|WP_Post  $post Post ID or post object.
5848   * @param string|array $uri  Ping URI or array of URIs.
5849   * @return int|false How many rows were updated.
5850   */
5851  function add_ping( $post, $uri ) {
5852      global $wpdb;
5853  
5854      $post = get_post( $post );
5855  
5856      if ( ! $post ) {
5857          return false;
5858      }
5859  
5860      $pung = trim( $post->pinged );
5861      $pung = preg_split( '/\s/', $pung );
5862  
5863      if ( is_array( $uri ) ) {
5864          $pung = array_merge( $pung, $uri );
5865      } else {
5866          $pung[] = $uri;
5867      }
5868      $new = implode( "\n", $pung );
5869  
5870      /**
5871       * Filters the new ping URL to add for the given post.
5872       *
5873       * @since 2.0.0
5874       *
5875       * @param string $new New ping URL to add.
5876       */
5877      $new = apply_filters( 'add_ping', $new );
5878  
5879      $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) );
5880      clean_post_cache( $post->ID );
5881      return $return;
5882  }
5883  
5884  /**
5885   * Retrieves enclosures already enclosed for a post.
5886   *
5887   * @since 1.5.0
5888   *
5889   * @param int $post_id Post ID.
5890   * @return string[] Array of enclosures for the given post.
5891   */
5892  function get_enclosed( $post_id ) {
5893      $custom_fields = get_post_custom( $post_id );
5894      $pung          = array();
5895      if ( ! is_array( $custom_fields ) ) {
5896          return $pung;
5897      }
5898  
5899      foreach ( $custom_fields as $key => $val ) {
5900          if ( 'enclosure' !== $key || ! is_array( $val ) ) {
5901              continue;
5902          }
5903          foreach ( $val as $enc ) {
5904              $enclosure = explode( "\n", $enc );
5905              $pung[]    = trim( $enclosure[0] );
5906          }
5907      }
5908  
5909      /**
5910       * Filters the list of enclosures already enclosed for the given post.
5911       *
5912       * @since 2.0.0
5913       *
5914       * @param string[] $pung    Array of enclosures for the given post.
5915       * @param int      $post_id Post ID.
5916       */
5917      return apply_filters( 'get_enclosed', $pung, $post_id );
5918  }
5919  
5920  /**
5921   * Retrieves URLs already pinged for a post.
5922   *
5923   * @since 1.5.0
5924   *
5925   * @since 4.7.0 `$post` can be a WP_Post object.
5926   *
5927   * @param int|WP_Post $post Post ID or object.
5928   * @return string[]|false Array of URLs already pinged for the given post, false if the post is not found.
5929   */
5930  function get_pung( $post ) {
5931      $post = get_post( $post );
5932  
5933      if ( ! $post ) {
5934          return false;
5935      }
5936  
5937      $pung = trim( $post->pinged );
5938      $pung = preg_split( '/\s/', $pung );
5939  
5940      /**
5941       * Filters the list of already-pinged URLs for the given post.
5942       *
5943       * @since 2.0.0
5944       *
5945       * @param string[] $pung Array of URLs already pinged for the given post.
5946       */
5947      return apply_filters( 'get_pung', $pung );
5948  }
5949  
5950  /**
5951   * Retrieves URLs that need to be pinged.
5952   *
5953   * @since 1.5.0
5954   * @since 4.7.0 `$post` can be a WP_Post object.
5955   *
5956   * @param int|WP_Post $post Post ID or post object.
5957   * @return string[]|false List of URLs yet to ping.
5958   */
5959  function get_to_ping( $post ) {
5960      $post = get_post( $post );
5961  
5962      if ( ! $post ) {
5963          return false;
5964      }
5965  
5966      $to_ping = sanitize_trackback_urls( $post->to_ping );
5967      $to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY );
5968  
5969      /**
5970       * Filters the list of URLs yet to ping for the given post.
5971       *
5972       * @since 2.0.0
5973       *
5974       * @param string[] $to_ping List of URLs yet to ping.
5975       */
5976      return apply_filters( 'get_to_ping', $to_ping );
5977  }
5978  
5979  /**
5980   * Does trackbacks for a list of URLs.
5981   *
5982   * @since 1.0.0
5983   *
5984   * @param string $tb_list Comma separated list of URLs.
5985   * @param int    $post_id Post ID.
5986   */
5987  function trackback_url_list( $tb_list, $post_id ) {
5988      if ( ! empty( $tb_list ) ) {
5989          // Get post data.
5990          $postdata = get_post( $post_id, ARRAY_A );
5991  
5992          // Form an excerpt.
5993          $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] );
5994  
5995          if ( strlen( $excerpt ) > 255 ) {
5996              $excerpt = substr( $excerpt, 0, 252 ) . '&hellip;';
5997          }
5998  
5999          $trackback_urls = explode( ',', $tb_list );
6000          foreach ( (array) $trackback_urls as $tb_url ) {
6001              $tb_url = trim( $tb_url );
6002              trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id );
6003          }
6004      }
6005  }
6006  
6007  //
6008  // Page functions.
6009  //
6010  
6011  /**
6012   * Gets a list of page IDs.
6013   *
6014   * @since 2.0.0
6015   *
6016   * @global wpdb $wpdb WordPress database abstraction object.
6017   *
6018   * @return string[] List of page IDs as strings.
6019   */
6020  function get_all_page_ids() {
6021      global $wpdb;
6022  
6023      $page_ids = wp_cache_get( 'all_page_ids', 'posts' );
6024      if ( ! is_array( $page_ids ) ) {
6025          $page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" );
6026          wp_cache_add( 'all_page_ids', $page_ids, 'posts' );
6027      }
6028  
6029      return $page_ids;
6030  }
6031  
6032  /**
6033   * Retrieves page data given a page ID or page object.
6034   *
6035   * Use get_post() instead of get_page().
6036   *
6037   * @since 1.5.1
6038   * @deprecated 3.5.0 Use get_post()
6039   *
6040   * @param int|WP_Post $page   Page object or page ID. Passed by reference.
6041   * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
6042   *                            correspond to a WP_Post object, an associative array, or a numeric array,
6043   *                            respectively. Default OBJECT.
6044   * @param string      $filter Optional. How the return value should be filtered. Accepts 'raw',
6045   *                            'edit', 'db', 'display'. Default 'raw'.
6046   * @return WP_Post|array|null WP_Post or array on success, null on failure.
6047   */
6048  function get_page( $page, $output = OBJECT, $filter = 'raw' ) {
6049      return get_post( $page, $output, $filter );
6050  }
6051  
6052  /**
6053   * Retrieves a page given its path.
6054   *
6055   * @since 2.1.0
6056   *
6057   * @global wpdb $wpdb WordPress database abstraction object.
6058   *
6059   * @param string       $page_path Page path.
6060   * @param string       $output    Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
6061   *                                correspond to a WP_Post object, an associative array, or a numeric array,
6062   *                                respectively. Default OBJECT.
6063   * @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
6064   * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
6065   */
6066  function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
6067      global $wpdb;
6068  
6069      $last_changed = wp_cache_get_last_changed( 'posts' );
6070  
6071      $hash      = md5( $page_path . serialize( $post_type ) );
6072      $cache_key = "get_page_by_path:$hash:$last_changed";
6073      $cached    = wp_cache_get( $cache_key, 'post-queries' );
6074      if ( false !== $cached ) {
6075          // Special case: '0' is a bad `$page_path`.
6076          if ( '0' === $cached || 0 === $cached ) {
6077              return;
6078          } else {
6079              return get_post( $cached, $output );
6080          }
6081      }
6082  
6083      $page_path     = rawurlencode( urldecode( $page_path ) );
6084      $page_path     = str_replace( '%2F', '/', $page_path );
6085      $page_path     = str_replace( '%20', ' ', $page_path );
6086      $parts         = explode( '/', trim( $page_path, '/' ) );
6087      $parts         = array_map( 'sanitize_title_for_query', $parts );
6088      $escaped_parts = esc_sql( $parts );
6089  
6090      $in_string = "'" . implode( "','", $escaped_parts ) . "'";
6091  
6092      if ( is_array( $post_type ) ) {
6093          $post_types = $post_type;
6094      } else {
6095          $post_types = array( $post_type, 'attachment' );
6096      }
6097  
6098      $post_types          = esc_sql( $post_types );
6099      $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
6100      $sql                 = "
6101          SELECT ID, post_name, post_parent, post_type
6102          FROM $wpdb->posts
6103          WHERE post_name IN ($in_string)
6104          AND post_type IN ($post_type_in_string)
6105      ";
6106  
6107      $pages = $wpdb->get_results( $sql, OBJECT_K );
6108  
6109      $revparts = array_reverse( $parts );
6110  
6111      $found_id = 0;
6112      foreach ( (array) $pages as $page ) {
6113          if ( $page->post_name === $revparts[0] ) {
6114              $count = 0;
6115              $p     = $page;
6116  
6117              /*
6118               * Loop through the given path parts from right to left,
6119               * ensuring each matches the post ancestry.
6120               */
6121              while ( 0 !== (int) $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
6122                  ++$count;
6123                  $parent = $pages[ $p->post_parent ];
6124                  if ( ! isset( $revparts[ $count ] ) || $parent->post_name !== $revparts[ $count ] ) {
6125                      break;
6126                  }
6127                  $p = $parent;
6128              }
6129  
6130              if ( 0 === (int) $p->post_parent
6131                  && count( $revparts ) === $count + 1
6132                  && $p->post_name === $revparts[ $count ]
6133              ) {
6134                  $found_id = $page->ID;
6135                  if ( $page->post_type === $post_type ) {
6136                      break;
6137                  }
6138              }
6139          }
6140      }
6141  
6142      // We cache misses as well as hits.
6143      wp_cache_set( $cache_key, $found_id, 'post-queries' );
6144  
6145      if ( $found_id ) {
6146          return get_post( $found_id, $output );
6147      }
6148  
6149      return null;
6150  }
6151  
6152  /**
6153   * Identifies descendants of a given page ID in a list of page objects.
6154   *
6155   * Descendants are identified from the `$pages` array passed to the function. No database queries are performed.
6156   *
6157   * @since 1.5.1
6158   *
6159   * @param int       $page_id Page ID.
6160   * @param WP_Post[] $pages   List of page objects from which descendants should be identified.
6161   * @return WP_Post[] List of page children.
6162   */
6163  function get_page_children( $page_id, $pages ) {
6164      // Build a hash of ID -> children.
6165      $children = array();
6166      foreach ( (array) $pages as $page ) {
6167          $children[ (int) $page->post_parent ][] = $page;
6168      }
6169  
6170      $page_list = array();
6171  
6172      // Start the search by looking at immediate children.
6173      if ( isset( $children[ $page_id ] ) ) {
6174          // Always start at the end of the stack in order to preserve original `$pages` order.
6175          $to_look = array_reverse( $children[ $page_id ] );
6176  
6177          while ( $to_look ) {
6178              $p           = array_pop( $to_look );
6179              $page_list[] = $p;
6180              if ( isset( $children[ $p->ID ] ) ) {
6181                  foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
6182                      // Append to the `$to_look` stack to descend the tree.
6183                      $to_look[] = $child;
6184                  }
6185              }
6186          }
6187      }
6188  
6189      return $page_list;
6190  }
6191  
6192  /**
6193   * Orders the pages with children under parents in a flat list.
6194   *
6195   * It uses auxiliary structure to hold parent-children relationships and
6196   * runs in O(N) complexity
6197   *
6198   * @since 2.0.0
6199   *
6200   * @param WP_Post[] $pages   Posts array (passed by reference).
6201   * @param int       $page_id Optional. Parent page ID. Default 0.
6202   * @return string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents.
6203   */
6204  function get_page_hierarchy( &$pages, $page_id = 0 ) {
6205      if ( empty( $pages ) ) {
6206          return array();
6207      }
6208  
6209      $children = array();
6210      foreach ( (array) $pages as $p ) {
6211          $parent_id                = (int) $p->post_parent;
6212          $children[ $parent_id ][] = $p;
6213      }
6214  
6215      $result = array();
6216      _page_traverse_name( $page_id, $children, $result );
6217  
6218      return $result;
6219  }
6220  
6221  /**
6222   * Traverses and return all the nested children post names of a root page.
6223   *
6224   * $children contains parent-children relations
6225   *
6226   * @since 2.9.0
6227   * @access private
6228   *
6229   * @see _page_traverse_name()
6230   *
6231   * @param int      $page_id  Page ID.
6232   * @param array    $children Parent-children relations (passed by reference).
6233   * @param string[] $result   Array of page names keyed by ID (passed by reference).
6234   */
6235  function _page_traverse_name( $page_id, &$children, &$result ) {
6236      if ( isset( $children[ $page_id ] ) ) {
6237          foreach ( (array) $children[ $page_id ] as $child ) {
6238              $result[ $child->ID ] = $child->post_name;
6239              _page_traverse_name( $child->ID, $children, $result );
6240          }
6241      }
6242  }
6243  
6244  /**
6245   * Builds the URI path for a page.
6246   *
6247   * Sub pages will be in the "directory" under the parent page post name.
6248   *
6249   * @since 1.5.0
6250   * @since 4.6.0 The `$page` parameter was made optional.
6251   *
6252   * @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post.
6253   * @return string|false Page URI, false on error.
6254   */
6255  function get_page_uri( $page = 0 ) {
6256      if ( ! $page instanceof WP_Post ) {
6257          $page = get_post( $page );
6258      }
6259  
6260      if ( ! $page ) {
6261          return false;
6262      }
6263  
6264      $uri = $page->post_name;
6265  
6266      foreach ( $page->ancestors as $parent ) {
6267          $parent = get_post( $parent );
6268          if ( $parent && $parent->post_name ) {
6269              $uri = $parent->post_name . '/' . $uri;
6270          }
6271      }
6272  
6273      /**
6274       * Filters the URI for a page.
6275       *
6276       * @since 4.4.0
6277       *
6278       * @param string  $uri  Page URI.
6279       * @param WP_Post $page Page object.
6280       */
6281      return apply_filters( 'get_page_uri', $uri, $page );
6282  }
6283  
6284  /**
6285   * Retrieves an array of pages (or hierarchical post type items).
6286   *
6287   * @since 1.5.0
6288   * @since 6.3.0 Use WP_Query internally.
6289   *
6290   * @param array|string $args {
6291   *     Optional. Array or string of arguments to retrieve pages.
6292   *
6293   *     @type int          $child_of     Page ID to return child and grandchild pages of. Note: The value
6294   *                                      of `$hierarchical` has no bearing on whether `$child_of` returns
6295   *                                      hierarchical results. Default 0, or no restriction.
6296   *     @type string       $sort_order   How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'.
6297   *     @type string       $sort_column  What columns to sort pages by, comma-separated. Accepts 'post_author',
6298   *                                      'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',
6299   *                                      'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'.
6300   *                                      'post_' can be omitted for any values that start with it.
6301   *                                      Default 'post_title'.
6302   *     @type bool         $hierarchical Whether to return pages hierarchically. If false in conjunction with
6303   *                                      `$child_of` also being false, both arguments will be disregarded.
6304   *                                      Default true.
6305   *     @type int[]        $exclude      Array of page IDs to exclude. Default empty array.
6306   *     @type int[]        $include      Array of page IDs to include. Cannot be used with `$child_of`,
6307   *                                      `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
6308   *                                      Default empty array.
6309   *     @type string       $meta_key     Only include pages with this meta key. Default empty.
6310   *     @type string       $meta_value   Only include pages with this meta value. Requires `$meta_key`.
6311   *                                      Default empty.
6312   *     @type string       $authors      A comma-separated list of author IDs. Default empty.
6313   *     @type int          $parent       Page ID to return direct children of. Default -1, or no restriction.
6314   *     @type string|int[] $exclude_tree Comma-separated string or array of page IDs to exclude.
6315   *                                      Default empty array.
6316   *     @type int          $number       The number of pages to return. Default 0, or all pages.
6317   *     @type int          $offset       The number of pages to skip before returning. Requires `$number`.
6318   *                                      Default 0.
6319   *     @type string       $post_type    The post type to query. Default 'page'.
6320   *     @type string|array $post_status  A comma-separated list or array of post statuses to include.
6321   *                                      Default 'publish'.
6322   * }
6323   * @return WP_Post[]|false Array of pages (or hierarchical post type items). Boolean false if the
6324   *                         specified post type is not hierarchical or the specified status is not
6325   *                         supported by the post type.
6326   */
6327  function get_pages( $args = array() ) {
6328      $defaults = array(
6329          'child_of'     => 0,
6330          'sort_order'   => 'ASC',
6331          'sort_column'  => 'post_title',
6332          'hierarchical' => 1,
6333          'exclude'      => array(),
6334          'include'      => array(),
6335          'meta_key'     => '',
6336          'meta_value'   => '',
6337          'authors'      => '',
6338          'parent'       => -1,
6339          'exclude_tree' => array(),
6340          'number'       => '',
6341          'offset'       => 0,
6342          'post_type'    => 'page',
6343          'post_status'  => 'publish',
6344      );
6345  
6346      $parsed_args = wp_parse_args( $args, $defaults );
6347  
6348      $number       = (int) $parsed_args['number'];
6349      $offset       = (int) $parsed_args['offset'];
6350      $child_of     = (int) $parsed_args['child_of'];
6351      $hierarchical = $parsed_args['hierarchical'];
6352      $exclude      = $parsed_args['exclude'];
6353      $meta_key     = $parsed_args['meta_key'];
6354      $meta_value   = $parsed_args['meta_value'];
6355      $parent       = $parsed_args['parent'];
6356      $post_status  = $parsed_args['post_status'];
6357  
6358      // Make sure the post type is hierarchical.
6359      $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
6360      if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) {
6361          return false;
6362      }
6363  
6364      if ( $parent > 0 && ! $child_of ) {
6365          $hierarchical = false;
6366      }
6367  
6368      // Make sure we have a valid post status.
6369      if ( ! is_array( $post_status ) ) {
6370          $post_status = explode( ',', $post_status );
6371      }
6372      if ( array_diff( $post_status, get_post_stati() ) ) {
6373          return false;
6374      }
6375  
6376      $query_args = array(
6377          'orderby'                => 'post_title',
6378          'order'                  => 'ASC',
6379          'post__not_in'           => wp_parse_id_list( $exclude ),
6380          'meta_key'               => $meta_key,
6381          'meta_value'             => $meta_value,
6382          'posts_per_page'         => -1,
6383          'offset'                 => $offset,
6384          'post_type'              => $parsed_args['post_type'],
6385          'post_status'            => $post_status,
6386          'update_post_term_cache' => false,
6387          'update_post_meta_cache' => false,
6388          'ignore_sticky_posts'    => true,
6389          'no_found_rows'          => true,
6390      );
6391  
6392      if ( ! empty( $parsed_args['include'] ) ) {
6393          $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
6394          $parent   = -1;
6395          unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] );
6396          $hierarchical           = false;
6397          $query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] );
6398      }
6399  
6400      if ( ! empty( $parsed_args['authors'] ) ) {
6401          $post_authors = wp_parse_list( $parsed_args['authors'] );
6402  
6403          if ( ! empty( $post_authors ) ) {
6404              $query_args['author__in'] = array();
6405              foreach ( $post_authors as $post_author ) {
6406                  // Do we have an author id or an author login?
6407                  if ( 0 === (int) $post_author ) {
6408                      $post_author = get_user_by( 'login', $post_author );
6409                      if ( empty( $post_author ) ) {
6410                          continue;
6411                      }
6412                      if ( empty( $post_author->ID ) ) {
6413                          continue;
6414                      }
6415                      $post_author = $post_author->ID;
6416                  }
6417                  $query_args['author__in'][] = (int) $post_author;
6418              }
6419          }
6420      }
6421  
6422      if ( is_array( $parent ) ) {
6423          $post_parent__in = array_map( 'absint', (array) $parent );
6424          if ( ! empty( $post_parent__in ) ) {
6425              $query_args['post_parent__in'] = $post_parent__in;
6426          }
6427      } elseif ( $parent >= 0 ) {
6428          $query_args['post_parent'] = $parent;
6429      }
6430  
6431      /*
6432       * Maintain backward compatibility for `sort_column` key.
6433       * Additionally to `WP_Query`, it has been supporting the `post_modified_gmt` field, so this logic will translate
6434       * it to `post_modified` which should result in the same order given the two dates in the fields match.
6435       */
6436      $orderby = wp_parse_list( $parsed_args['sort_column'] );
6437      $orderby = array_map(
6438          static function ( $orderby_field ) {
6439              $orderby_field = trim( $orderby_field );
6440              if ( 'post_modified_gmt' === $orderby_field || 'modified_gmt' === $orderby_field ) {
6441                  $orderby_field = str_replace( '_gmt', '', $orderby_field );
6442              }
6443              return $orderby_field;
6444          },
6445          $orderby
6446      );
6447      if ( $orderby ) {
6448          $query_args['orderby'] = array_fill_keys( $orderby, $parsed_args['sort_order'] );
6449      }
6450  
6451      $order = $parsed_args['sort_order'];
6452      if ( $order ) {
6453          $query_args['order'] = $order;
6454      }
6455  
6456      if ( ! empty( $number ) ) {
6457          $query_args['posts_per_page'] = $number;
6458      }
6459  
6460      /**
6461       * Filters query arguments passed to WP_Query in get_pages.
6462       *
6463       * @since 6.3.0
6464       *
6465       * @param array $query_args  Array of arguments passed to WP_Query.
6466       * @param array $parsed_args Array of get_pages() arguments.
6467       */
6468      $query_args = apply_filters( 'get_pages_query_args', $query_args, $parsed_args );
6469  
6470      $pages = new WP_Query();
6471      $pages = $pages->query( $query_args );
6472  
6473      if ( $child_of || $hierarchical ) {
6474          $pages = get_page_children( $child_of, $pages );
6475      }
6476  
6477      if ( ! empty( $parsed_args['exclude_tree'] ) ) {
6478          $exclude = wp_parse_id_list( $parsed_args['exclude_tree'] );
6479          foreach ( $exclude as $id ) {
6480              $children = get_page_children( $id, $pages );
6481              foreach ( $children as $child ) {
6482                  $exclude[] = $child->ID;
6483              }
6484          }
6485  
6486          $num_pages = count( $pages );
6487          for ( $i = 0; $i < $num_pages; $i++ ) {
6488              if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) {
6489                  unset( $pages[ $i ] );
6490              }
6491          }
6492      }
6493  
6494      /**
6495       * Filters the retrieved list of pages.
6496       *
6497       * @since 2.1.0
6498       *
6499       * @param WP_Post[] $pages       Array of page objects.
6500       * @param array     $parsed_args Array of get_pages() arguments.
6501       */
6502      return apply_filters( 'get_pages', $pages, $parsed_args );
6503  }
6504  
6505  //
6506  // Attachment functions.
6507  //
6508  
6509  /**
6510   * Determines whether an attachment URI is local and really an attachment.
6511   *
6512   * For more information on this and similar theme functions, check out
6513   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
6514   * Conditional Tags} article in the Theme Developer Handbook.
6515   *
6516   * @since 2.0.0
6517   *
6518   * @param string $url URL to check
6519   * @return bool True on success, false on failure.
6520   */
6521  function is_local_attachment( $url ) {
6522      if ( ! str_contains( $url, home_url() ) ) {
6523          return false;
6524      }
6525      if ( str_contains( $url, home_url( '/?attachment_id=' ) ) ) {
6526          return true;
6527      }
6528  
6529      $id = url_to_postid( $url );
6530      if ( $id ) {
6531          $post = get_post( $id );
6532          if ( 'attachment' === $post->post_type ) {
6533              return true;
6534          }
6535      }
6536      return false;
6537  }
6538  
6539  /**
6540   * Inserts an attachment.
6541   *
6542   * If you set the 'ID' in the $args parameter, it will mean that you are
6543   * updating and attempt to update the attachment. You can also set the
6544   * attachment name or title by setting the key 'post_name' or 'post_title'.
6545   *
6546   * You can set the dates for the attachment manually by setting the 'post_date'
6547   * and 'post_date_gmt' keys' values.
6548   *
6549   * By default, the comments will use the default settings for whether the
6550   * comments are allowed. You can close them manually or keep them open by
6551   * setting the value for the 'comment_status' key.
6552   *
6553   * @since 2.0.0
6554   * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
6555   * @since 5.6.0 Added the `$fire_after_hooks` parameter.
6556   *
6557   * @see wp_insert_post()
6558   *
6559   * @param string|array $args             Arguments for inserting an attachment.
6560   * @param string|false $file             Optional. Filename. Default false.
6561   * @param int          $parent_post_id   Optional. Parent post ID or 0 for no parent. Default 0.
6562   * @param bool         $wp_error         Optional. Whether to return a WP_Error on failure. Default false.
6563   * @param bool         $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
6564   * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
6565   */
6566  function wp_insert_attachment( $args, $file = false, $parent_post_id = 0, $wp_error = false, $fire_after_hooks = true ) {
6567      $defaults = array(
6568          'file'        => $file,
6569          'post_parent' => 0,
6570      );
6571  
6572      $data = wp_parse_args( $args, $defaults );
6573  
6574      if ( ! empty( $parent_post_id ) ) {
6575          $data['post_parent'] = $parent_post_id;
6576      }
6577  
6578      $data['post_type'] = 'attachment';
6579  
6580      return wp_insert_post( $data, $wp_error, $fire_after_hooks );
6581  }
6582  
6583  /**
6584   * Trashes or deletes an attachment.
6585   *
6586   * When an attachment is permanently deleted, the file will also be removed.
6587   * Deletion removes all post meta fields, taxonomy, comments, etc. associated
6588   * with the attachment (except the main post).
6589   *
6590   * The attachment is moved to the Trash instead of permanently deleted unless Trash
6591   * for media is disabled, item is already in the Trash, or $force_delete is true.
6592   *
6593   * @since 2.0.0
6594   *
6595   * @global wpdb $wpdb WordPress database abstraction object.
6596   *
6597   * @param int  $post_id      Attachment ID.
6598   * @param bool $force_delete Optional. Whether to bypass Trash and force deletion.
6599   *                           Default false.
6600   * @return WP_Post|false|null Post data on success, false or null on failure.
6601   */
6602  function wp_delete_attachment( $post_id, $force_delete = false ) {
6603      global $wpdb;
6604  
6605      $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );
6606  
6607      if ( ! $post ) {
6608          return $post;
6609      }
6610  
6611      $post = get_post( $post );
6612  
6613      if ( 'attachment' !== $post->post_type ) {
6614          return false;
6615      }
6616  
6617      if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) {
6618          return wp_trash_post( $post_id );
6619      }
6620  
6621      /**
6622       * Filters whether an attachment deletion should take place.
6623       *
6624       * @since 5.5.0
6625       *
6626       * @param WP_Post|false|null $delete       Whether to go forward with deletion.
6627       * @param WP_Post            $post         Post object.
6628       * @param bool               $force_delete Whether to bypass the Trash.
6629       */
6630      $check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete );
6631      if ( null !== $check ) {
6632          return $check;
6633      }
6634  
6635      delete_post_meta( $post_id, '_wp_trash_meta_status' );
6636      delete_post_meta( $post_id, '_wp_trash_meta_time' );
6637  
6638      $meta         = wp_get_attachment_metadata( $post_id );
6639      $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
6640      $file         = get_attached_file( $post_id );
6641  
6642      if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) {
6643          clean_dirsize_cache( $file );
6644      }
6645  
6646      /**
6647       * Fires before an attachment is deleted, at the start of wp_delete_attachment().
6648       *
6649       * @since 2.0.0
6650       * @since 5.5.0 Added the `$post` parameter.
6651       *
6652       * @param int     $post_id Attachment ID.
6653       * @param WP_Post $post    Post object.
6654       */
6655      do_action( 'delete_attachment', $post_id, $post );
6656  
6657      wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) );
6658      wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) );
6659  
6660      // Delete all for any posts.
6661      delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );
6662  
6663      wp_defer_comment_counting( true );
6664  
6665      $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) );
6666      foreach ( $comment_ids as $comment_id ) {
6667          wp_delete_comment( $comment_id, true );
6668      }
6669  
6670      wp_defer_comment_counting( false );
6671  
6672      $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) );
6673      foreach ( $post_meta_ids as $mid ) {
6674          delete_metadata_by_mid( 'post', $mid );
6675      }
6676  
6677      /** This action is documented in wp-includes/post.php */
6678      do_action( 'delete_post', $post_id, $post );
6679      $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
6680      if ( ! $result ) {
6681          return false;
6682      }
6683      /** This action is documented in wp-includes/post.php */
6684      do_action( 'deleted_post', $post_id, $post );
6685  
6686      wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
6687  
6688      clean_post_cache( $post );
6689  
6690      return $post;
6691  }
6692  
6693  /**
6694   * Deletes all files that belong to the given attachment.
6695   *
6696   * @since 4.9.7
6697   *
6698   * @global wpdb $wpdb WordPress database abstraction object.
6699   *
6700   * @param int    $post_id      Attachment ID.
6701   * @param array  $meta         The attachment's meta data.
6702   * @param array  $backup_sizes The meta data for the attachment's backup images.
6703   * @param string $file         Absolute path to the attachment's file.
6704   * @return bool True on success, false on failure.
6705   */
6706  function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
6707      global $wpdb;
6708  
6709      $uploadpath = wp_get_upload_dir();
6710      $deleted    = true;
6711  
6712      if ( ! empty( $meta['thumb'] ) ) {
6713          // Don't delete the thumb if another attachment uses it.
6714          if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
6715              $thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file );
6716  
6717              if ( ! empty( $thumbfile ) ) {
6718                  $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
6719                  $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
6720  
6721                  if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
6722                      $deleted = false;
6723                  }
6724              }
6725          }
6726      }
6727  
6728      // Remove intermediate and backup images if there are any.
6729      if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
6730          $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
6731  
6732          foreach ( $meta['sizes'] as $size => $sizeinfo ) {
6733              $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
6734  
6735              if ( ! empty( $intermediate_file ) ) {
6736                  $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
6737  
6738                  if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
6739                      $deleted = false;
6740                  }
6741              }
6742          }
6743      }
6744  
6745      if ( ! empty( $meta['original_image'] ) ) {
6746          if ( empty( $intermediate_dir ) ) {
6747              $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
6748          }
6749  
6750          $original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file );
6751  
6752          if ( ! empty( $original_image ) ) {
6753              $original_image = path_join( $uploadpath['basedir'], $original_image );
6754  
6755              if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) {
6756                  $deleted = false;
6757              }
6758          }
6759      }
6760  
6761      if ( is_array( $backup_sizes ) ) {
6762          $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
6763  
6764          foreach ( $backup_sizes as $size ) {
6765              $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
6766  
6767              if ( ! empty( $del_file ) ) {
6768                  $del_file = path_join( $uploadpath['basedir'], $del_file );
6769  
6770                  if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
6771                      $deleted = false;
6772                  }
6773              }
6774          }
6775      }
6776  
6777      if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
6778          $deleted = false;
6779      }
6780  
6781      return $deleted;
6782  }
6783  
6784  /**
6785   * Retrieves attachment metadata for attachment ID.
6786   *
6787   * @since 2.1.0
6788   * @since 6.0.0 The `$filesize` value was added to the returned array.
6789   *
6790   * @param int  $attachment_id Attachment post ID. Defaults to global $post.
6791   * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
6792   * @return array|false {
6793   *     Attachment metadata. False on failure.
6794   *
6795   *     @type int    $width      The width of the attachment.
6796   *     @type int    $height     The height of the attachment.
6797   *     @type string $file       The file path relative to `wp-content/uploads`.
6798   *     @type array  $sizes      Keys are size slugs, each value is an array containing
6799   *                              'file', 'width', 'height', and 'mime-type'.
6800   *     @type array  $image_meta Image metadata.
6801   *     @type int    $filesize   File size of the attachment.
6802   * }
6803   */
6804  function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
6805      $attachment_id = (int) $attachment_id;
6806  
6807      if ( ! $attachment_id ) {
6808          $post = get_post();
6809  
6810          if ( ! $post ) {
6811              return false;
6812          }
6813  
6814          $attachment_id = $post->ID;
6815      }
6816  
6817      $data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
6818  
6819      if ( ! $data ) {
6820          return false;
6821      }
6822  
6823      if ( $unfiltered ) {
6824          return $data;
6825      }
6826  
6827      /**
6828       * Filters the attachment meta data.
6829       *
6830       * @since 2.1.0
6831       *
6832       * @param array $data          Array of meta data for the given attachment.
6833       * @param int   $attachment_id Attachment post ID.
6834       */
6835      return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
6836  }
6837  
6838  /**
6839   * Updates metadata for an attachment.
6840   *
6841   * @since 2.1.0
6842   *
6843   * @param int   $attachment_id Attachment post ID.
6844   * @param array $data          Attachment meta data.
6845   * @return int|false False if $post is invalid.
6846   */
6847  function wp_update_attachment_metadata( $attachment_id, $data ) {
6848      $attachment_id = (int) $attachment_id;
6849  
6850      $post = get_post( $attachment_id );
6851  
6852      if ( ! $post ) {
6853          return false;
6854      }
6855  
6856      /**
6857       * Filters the updated attachment meta data.
6858       *
6859       * @since 2.1.0
6860       *
6861       * @param array $data          Array of updated attachment meta data.
6862       * @param int   $attachment_id Attachment post ID.
6863       */
6864      $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
6865      if ( $data ) {
6866          return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
6867      } else {
6868          return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
6869      }
6870  }
6871  
6872  /**
6873   * Retrieves the URL for an attachment.
6874   *
6875   * @since 2.1.0
6876   *
6877   * @global string $pagenow The filename of the current screen.
6878   *
6879   * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
6880   * @return string|false Attachment URL, otherwise false.
6881   */
6882  function wp_get_attachment_url( $attachment_id = 0 ) {
6883      global $pagenow;
6884  
6885      $attachment_id = (int) $attachment_id;
6886  
6887      $post = get_post( $attachment_id );
6888  
6889      if ( ! $post ) {
6890          return false;
6891      }
6892  
6893      if ( 'attachment' !== $post->post_type ) {
6894          return false;
6895      }
6896  
6897      $url = '';
6898      // Get attached file.
6899      $file = get_post_meta( $post->ID, '_wp_attached_file', true );
6900      if ( $file ) {
6901          // Get upload directory.
6902          $uploads = wp_get_upload_dir();
6903          if ( $uploads && false === $uploads['error'] ) {
6904              // Check that the upload base exists in the file location.
6905              if ( str_starts_with( $file, $uploads['basedir'] ) ) {
6906                  // Replace file location with url location.
6907                  $url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
6908              } elseif ( str_contains( $file, 'wp-content/uploads' ) ) {
6909                  // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
6910                  $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file );
6911              } else {
6912                  // It's a newly-uploaded file, therefore $file is relative to the basedir.
6913                  $url = $uploads['baseurl'] . "/$file";
6914              }
6915          }
6916      }
6917  
6918      /*
6919       * If any of the above options failed, Fallback on the GUID as used pre-2.7,
6920       * not recommended to rely upon this.
6921       */
6922      if ( ! $url ) {
6923          $url = get_the_guid( $post->ID );
6924      }
6925  
6926      // On SSL front end, URLs should be HTTPS.
6927      if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
6928          $url = set_url_scheme( $url );
6929      }
6930  
6931      /**
6932       * Filters the attachment URL.
6933       *
6934       * @since 2.1.0
6935       *
6936       * @param string $url           URL for the given attachment.
6937       * @param int    $attachment_id Attachment post ID.
6938       */
6939      $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
6940  
6941      if ( ! $url ) {
6942          return false;
6943      }
6944  
6945      return $url;
6946  }
6947  
6948  /**
6949   * Retrieves the caption for an attachment.
6950   *
6951   * @since 4.6.0
6952   *
6953   * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
6954   * @return string|false Attachment caption on success, false on failure.
6955   */
6956  function wp_get_attachment_caption( $post_id = 0 ) {
6957      $post_id = (int) $post_id;
6958      $post    = get_post( $post_id );
6959  
6960      if ( ! $post ) {
6961          return false;
6962      }
6963  
6964      if ( 'attachment' !== $post->post_type ) {
6965          return false;
6966      }
6967  
6968      $caption = $post->post_excerpt;
6969  
6970      /**
6971       * Filters the attachment caption.
6972       *
6973       * @since 4.6.0
6974       *
6975       * @param string $caption Caption for the given attachment.
6976       * @param int    $post_id Attachment ID.
6977       */
6978      return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
6979  }
6980  
6981  /**
6982   * Retrieves URL for an attachment thumbnail.
6983   *
6984   * @since 2.1.0
6985   * @since 6.1.0 Changed to use wp_get_attachment_image_url().
6986   *
6987   * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
6988   * @return string|false Thumbnail URL on success, false on failure.
6989   */
6990  function wp_get_attachment_thumb_url( $post_id = 0 ) {
6991      $post_id = (int) $post_id;
6992  
6993      /*
6994       * This uses image_downsize() which also looks for the (very) old format $image_meta['thumb']
6995       * when the newer format $image_meta['sizes']['thumbnail'] doesn't exist.
6996       */
6997      $thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' );
6998  
6999      if ( empty( $thumbnail_url ) ) {
7000          return false;
7001      }
7002  
7003      /**
7004       * Filters the attachment thumbnail URL.
7005       *
7006       * @since 2.1.0
7007       *
7008       * @param string $thumbnail_url URL for the attachment thumbnail.
7009       * @param int    $post_id       Attachment ID.
7010       */
7011      return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id );
7012  }
7013  
7014  /**
7015   * Verifies an attachment is of a given type.
7016   *
7017   * @since 4.2.0
7018   *
7019   * @param string      $type Attachment type. Accepts `image`, `audio`, `video`, or a file extension.
7020   * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
7021   * @return bool True if an accepted type or a matching file extension, false otherwise.
7022   */
7023  function wp_attachment_is( $type, $post = null ) {
7024      $post = get_post( $post );
7025  
7026      if ( ! $post ) {
7027          return false;
7028      }
7029  
7030      $file = get_attached_file( $post->ID );
7031  
7032      if ( ! $file ) {
7033          return false;
7034      }
7035  
7036      if ( str_starts_with( $post->post_mime_type, $type . '/' ) ) {
7037          return true;
7038      }
7039  
7040      $check = wp_check_filetype( $file );
7041  
7042      if ( empty( $check['ext'] ) ) {
7043          return false;
7044      }
7045  
7046      $ext = $check['ext'];
7047  
7048      if ( 'import' !== $post->post_mime_type ) {
7049          return $type === $ext;
7050      }
7051  
7052      switch ( $type ) {
7053          case 'image':
7054              $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif', 'heic' );
7055              return in_array( $ext, $image_exts, true );
7056  
7057          case 'audio':
7058              return in_array( $ext, wp_get_audio_extensions(), true );
7059  
7060          case 'video':
7061              return in_array( $ext, wp_get_video_extensions(), true );
7062  
7063          default:
7064              return $type === $ext;
7065      }
7066  }
7067  
7068  /**
7069   * Determines whether an attachment is an image.
7070   *
7071   * For more information on this and similar theme functions, check out
7072   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
7073   * Conditional Tags} article in the Theme Developer Handbook.
7074   *
7075   * @since 2.1.0
7076   * @since 4.2.0 Modified into wrapper for wp_attachment_is() and
7077   *              allowed WP_Post object to be passed.
7078   *
7079   * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
7080   * @return bool Whether the attachment is an image.
7081   */
7082  function wp_attachment_is_image( $post = null ) {
7083      return wp_attachment_is( 'image', $post );
7084  }
7085  
7086  /**
7087   * Retrieves the icon for a MIME type or attachment.
7088   *
7089   * @since 2.1.0
7090   * @since 6.5.0 Added the `$preferred_ext` parameter.
7091   *
7092   * @param string|int $mime          MIME type or attachment ID.
7093   * @param string     $preferred_ext File format to prefer in return. Default '.png'.
7094   * @return string|false Icon, false otherwise.
7095   */
7096  function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) {
7097      if ( ! is_numeric( $mime ) ) {
7098          $icon = wp_cache_get( "mime_type_icon_$mime" );
7099      }
7100  
7101      // Check if preferred file format variable is present and is a validly formatted file extension.
7102      if ( ! empty( $preferred_ext ) && is_string( $preferred_ext ) && ! str_starts_with( $preferred_ext, '.' ) ) {
7103          $preferred_ext = '.' . strtolower( $preferred_ext );
7104      }
7105  
7106      $post_id = 0;
7107      if ( empty( $icon ) ) {
7108          $post_mimes = array();
7109          if ( is_numeric( $mime ) ) {
7110              $mime = (int) $mime;
7111              $post = get_post( $mime );
7112              if ( $post ) {
7113                  $post_id = (int) $post->ID;
7114                  $file    = get_attached_file( $post_id );
7115                  $ext     = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file );
7116                  if ( ! empty( $ext ) ) {
7117                      $post_mimes[] = $ext;
7118                      $ext_type     = wp_ext2type( $ext );
7119                      if ( $ext_type ) {
7120                          $post_mimes[] = $ext_type;
7121                      }
7122                  }
7123                  $mime = $post->post_mime_type;
7124              } else {
7125                  $mime = 0;
7126              }
7127          } else {
7128              $post_mimes[] = $mime;
7129          }
7130  
7131          $icon_files = wp_cache_get( 'icon_files' );
7132  
7133          if ( ! is_array( $icon_files ) ) {
7134              /**
7135               * Filters the icon directory path.
7136               *
7137               * @since 2.0.0
7138               *
7139               * @param string $path Icon directory absolute path.
7140               */
7141              $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
7142  
7143              /**
7144               * Filters the icon directory URI.
7145               *
7146               * @since 2.0.0
7147               *
7148               * @param string $uri Icon directory URI.
7149               */
7150              $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );
7151  
7152              /**
7153               * Filters the array of icon directory URIs.
7154               *
7155               * @since 2.5.0
7156               *
7157               * @param string[] $uris Array of icon directory URIs keyed by directory absolute path.
7158               */
7159              $dirs       = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
7160              $icon_files = array();
7161              $all_icons  = array();
7162              while ( $dirs ) {
7163                  $keys = array_keys( $dirs );
7164                  $dir  = array_shift( $keys );
7165                  $uri  = array_shift( $dirs );
7166                  $dh   = opendir( $dir );
7167                  if ( $dh ) {
7168                      while ( false !== $file = readdir( $dh ) ) {
7169                          $file = wp_basename( $file );
7170                          if ( str_starts_with( $file, '.' ) ) {
7171                              continue;
7172                          }
7173  
7174                          $ext = strtolower( substr( $file, -4 ) );
7175                          if ( ! in_array( $ext, array( '.svg', '.png', '.gif', '.jpg' ), true ) ) {
7176                              if ( is_dir( "$dir/$file" ) ) {
7177                                  $dirs[ "$dir/$file" ] = "$uri/$file";
7178                              }
7179                              continue;
7180                          }
7181                          $all_icons[ "$dir/$file" ] = "$uri/$file";
7182                          if ( $ext === $preferred_ext ) {
7183                              $icon_files[ "$dir/$file" ] = "$uri/$file";
7184                          }
7185                      }
7186                      closedir( $dh );
7187                  }
7188              }
7189              // If directory only contained icons of a non-preferred format, return those.
7190              if ( empty( $icon_files ) ) {
7191                  $icon_files = $all_icons;
7192              }
7193              wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
7194          }
7195  
7196          $types = array();
7197          // Icon wp_basename - extension = MIME wildcard.
7198          foreach ( $icon_files as $file => $uri ) {
7199              $types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ];
7200          }
7201  
7202          if ( ! empty( $mime ) ) {
7203              $post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) );
7204              $post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 );
7205              $post_mimes[] = str_replace( '/', '_', $mime );
7206          }
7207  
7208          $matches            = wp_match_mime_types( array_keys( $types ), $post_mimes );
7209          $matches['default'] = array( 'default' );
7210  
7211          foreach ( $matches as $match => $wilds ) {
7212              foreach ( $wilds as $wild ) {
7213                  if ( ! isset( $types[ $wild ] ) ) {
7214                      continue;
7215                  }
7216  
7217                  $icon = $types[ $wild ];
7218                  if ( ! is_numeric( $mime ) ) {
7219                      wp_cache_add( "mime_type_icon_$mime", $icon );
7220                  }
7221                  break 2;
7222              }
7223          }
7224      }
7225  
7226      /**
7227       * Filters the mime type icon.
7228       *
7229       * @since 2.1.0
7230       *
7231       * @param string $icon    Path to the mime type icon.
7232       * @param string $mime    Mime type.
7233       * @param int    $post_id Attachment ID. Will equal 0 if the function passed
7234       *                        the mime type.
7235       */
7236      return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );
7237  }
7238  
7239  /**
7240   * Checks for changed slugs for published post objects and save the old slug.
7241   *
7242   * The function is used when a post object of any type is updated,
7243   * by comparing the current and previous post objects.
7244   *
7245   * If the slug was changed and not already part of the old slugs then it will be
7246   * added to the post meta field ('_wp_old_slug') for storing old slugs for that
7247   * post.
7248   *
7249   * The most logically usage of this function is redirecting changed post objects, so
7250   * that those that linked to an changed post will be redirected to the new post.
7251   *
7252   * @since 2.1.0
7253   *
7254   * @param int     $post_id     Post ID.
7255   * @param WP_Post $post        The post object.
7256   * @param WP_Post $post_before The previous post object.
7257   */
7258  function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
7259      // Don't bother if it hasn't changed.
7260      if ( $post->post_name === $post_before->post_name ) {
7261          return;
7262      }
7263  
7264      // We're only concerned with published, non-hierarchical objects.
7265      if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) )
7266          || is_post_type_hierarchical( $post->post_type )
7267      ) {
7268          return;
7269      }
7270  
7271      $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' );
7272  
7273      // If we haven't added this old slug before, add it now.
7274      if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) {
7275          add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name );
7276      }
7277  
7278      // If the new slug was used previously, delete it from the list.
7279      if ( in_array( $post->post_name, $old_slugs, true ) ) {
7280          delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
7281      }
7282  }
7283  
7284  /**
7285   * Checks for changed dates for published post objects and save the old date.
7286   *
7287   * The function is used when a post object of any type is updated,
7288   * by comparing the current and previous post objects.
7289   *
7290   * If the date was changed and not already part of the old dates then it will be
7291   * added to the post meta field ('_wp_old_date') for storing old dates for that
7292   * post.
7293   *
7294   * The most logically usage of this function is redirecting changed post objects, so
7295   * that those that linked to an changed post will be redirected to the new post.
7296   *
7297   * @since 4.9.3
7298   *
7299   * @param int     $post_id     Post ID.
7300   * @param WP_Post $post        The post object.
7301   * @param WP_Post $post_before The previous post object.
7302   */
7303  function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
7304      $previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) );
7305      $new_date      = gmdate( 'Y-m-d', strtotime( $post->post_date ) );
7306  
7307      // Don't bother if it hasn't changed.
7308      if ( $new_date === $previous_date ) {
7309          return;
7310      }
7311  
7312      // We're only concerned with published, non-hierarchical objects.
7313      if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) )
7314          || is_post_type_hierarchical( $post->post_type )
7315      ) {
7316          return;
7317      }
7318  
7319      $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
7320  
7321      // If we haven't added this old date before, add it now.
7322      if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) {
7323          add_post_meta( $post_id, '_wp_old_date', $previous_date );
7324      }
7325  
7326      // If the new slug was used previously, delete it from the list.
7327      if ( in_array( $new_date, $old_dates, true ) ) {
7328          delete_post_meta( $post_id, '_wp_old_date', $new_date );
7329      }
7330  }
7331  
7332  /**
7333   * Retrieves the private post SQL based on capability.
7334   *
7335   * This function provides a standardized way to appropriately select on the
7336   * post_status of a post type. The function will return a piece of SQL code
7337   * that can be added to a WHERE clause; this SQL is constructed to allow all
7338   * published posts, and all private posts to which the user has access.
7339   *
7340   * @since 2.2.0
7341   * @since 4.3.0 Added the ability to pass an array to `$post_type`.
7342   *
7343   * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'.
7344   * @return string SQL code that can be added to a where clause.
7345   */
7346  function get_private_posts_cap_sql( $post_type ) {
7347      return get_posts_by_author_sql( $post_type, false );
7348  }
7349  
7350  /**
7351   * Retrieves the post SQL based on capability, author, and type.
7352   *
7353   * @since 3.0.0
7354   * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`.
7355   *
7356   * @see get_private_posts_cap_sql()
7357   * @global wpdb $wpdb WordPress database abstraction object.
7358   *
7359   * @param string|string[] $post_type   Single post type or an array of post types.
7360   * @param bool            $full        Optional. Returns a full WHERE statement instead of just
7361   *                                     an 'andalso' term. Default true.
7362   * @param int             $post_author Optional. Query posts having a single author ID. Default null.
7363   * @param bool            $public_only Optional. Only return public posts. Skips cap checks for
7364   *                                     $current_user.  Default false.
7365   * @return string SQL WHERE code that can be added to a query.
7366   */
7367  function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
7368      global $wpdb;
7369  
7370      if ( is_array( $post_type ) ) {
7371          $post_types = $post_type;
7372      } else {
7373          $post_types = array( $post_type );
7374      }
7375  
7376      $post_type_clauses = array();
7377      foreach ( $post_types as $post_type ) {
7378          $post_type_obj = get_post_type_object( $post_type );
7379  
7380          if ( ! $post_type_obj ) {
7381              continue;
7382          }
7383  
7384          /**
7385           * Filters the capability to read private posts for a custom post type
7386           * when generating SQL for getting posts by author.
7387           *
7388           * @since 2.2.0
7389           * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
7390           *
7391           * @param string $cap Capability.
7392           */
7393          $cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' );
7394  
7395          if ( ! $cap ) {
7396              $cap = current_user_can( $post_type_obj->cap->read_private_posts );
7397          }
7398  
7399          // Only need to check the cap if $public_only is false.
7400          $post_status_sql = "post_status = 'publish'";
7401  
7402          if ( false === $public_only ) {
7403              if ( $cap ) {
7404                  // Does the user have the capability to view private posts? Guess so.
7405                  $post_status_sql .= " OR post_status = 'private'";
7406              } elseif ( is_user_logged_in() ) {
7407                  // Users can view their own private posts.
7408                  $id = get_current_user_id();
7409                  if ( null === $post_author || ! $full ) {
7410                      $post_status_sql .= " OR post_status = 'private' AND post_author = $id";
7411                  } elseif ( $id === (int) $post_author ) {
7412                      $post_status_sql .= " OR post_status = 'private'";
7413                  } // Else none.
7414              } // Else none.
7415          }
7416  
7417          $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )";
7418      }
7419  
7420      if ( empty( $post_type_clauses ) ) {
7421          return $full ? 'WHERE 1 = 0' : '1 = 0';
7422      }
7423  
7424      $sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )';
7425  
7426      if ( null !== $post_author ) {
7427          $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author );
7428      }
7429  
7430      if ( $full ) {
7431          $sql = 'WHERE ' . $sql;
7432      }
7433  
7434      return $sql;
7435  }
7436  
7437  /**
7438   * Retrieves the most recent time that a post on the site was published.
7439   *
7440   * The server timezone is the default and is the difference between GMT and
7441   * server time. The 'blog' value is the date when the last post was posted.
7442   * The 'gmt' is when the last post was posted in GMT formatted date.
7443   *
7444   * @since 0.71
7445   * @since 4.4.0 The `$post_type` argument was added.
7446   *
7447   * @param string $timezone  Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'.
7448   *                          'server' uses the server's internal timezone.
7449   *                          'blog' uses the `post_date` field, which proxies to the timezone set for the site.
7450   *                          'gmt' uses the `post_date_gmt` field.
7451   *                          Default 'server'.
7452   * @param string $post_type Optional. The post type to check. Default 'any'.
7453   * @return string The date of the last post, or false on failure.
7454   */
7455  function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
7456      $lastpostdate = _get_last_post_time( $timezone, 'date', $post_type );
7457  
7458      /**
7459       * Filters the most recent time that a post on the site was published.
7460       *
7461       * @since 2.3.0
7462       * @since 5.5.0 Added the `$post_type` parameter.
7463       *
7464       * @param string|false $lastpostdate The most recent time that a post was published,
7465       *                                   in 'Y-m-d H:i:s' format. False on failure.
7466       * @param string       $timezone     Location to use for getting the post published date.
7467       *                                   See get_lastpostdate() for accepted `$timezone` values.
7468       * @param string       $post_type    The post type to check.
7469       */
7470      return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type );
7471  }
7472  
7473  /**
7474   * Gets the most recent time that a post on the site was modified.
7475   *
7476   * The server timezone is the default and is the difference between GMT and
7477   * server time. The 'blog' value is just when the last post was modified.
7478   * The 'gmt' is when the last post was modified in GMT time.
7479   *
7480   * @since 1.2.0
7481   * @since 4.4.0 The `$post_type` argument was added.
7482   *
7483   * @param string $timezone  Optional. The timezone for the timestamp. See get_lastpostdate()
7484   *                          for information on accepted values.
7485   *                          Default 'server'.
7486   * @param string $post_type Optional. The post type to check. Default 'any'.
7487   * @return string The timestamp in 'Y-m-d H:i:s' format, or false on failure.
7488   */
7489  function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
7490      /**
7491       * Pre-filter the return value of get_lastpostmodified() before the query is run.
7492       *
7493       * @since 4.4.0
7494       *
7495       * @param string|false $lastpostmodified The most recent time that a post was modified,
7496       *                                       in 'Y-m-d H:i:s' format, or false. Returning anything
7497       *                                       other than false will short-circuit the function.
7498       * @param string       $timezone         Location to use for getting the post modified date.
7499       *                                       See get_lastpostdate() for accepted `$timezone` values.
7500       * @param string       $post_type        The post type to check.
7501       */
7502      $lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type );
7503  
7504      if ( false !== $lastpostmodified ) {
7505          return $lastpostmodified;
7506      }
7507  
7508      $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type );
7509      $lastpostdate     = get_lastpostdate( $timezone, $post_type );
7510  
7511      if ( $lastpostdate > $lastpostmodified ) {
7512          $lastpostmodified = $lastpostdate;
7513      }
7514  
7515      /**
7516       * Filters the most recent time that a post on the site was modified.
7517       *
7518       * @since 2.3.0
7519       * @since 5.5.0 Added the `$post_type` parameter.
7520       *
7521       * @param string|false $lastpostmodified The most recent time that a post was modified,
7522       *                                       in 'Y-m-d H:i:s' format. False on failure.
7523       * @param string       $timezone         Location to use for getting the post modified date.
7524       *                                       See get_lastpostdate() for accepted `$timezone` values.
7525       * @param string       $post_type        The post type to check.
7526       */
7527      return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone, $post_type );
7528  }
7529  
7530  /**
7531   * Gets the timestamp of the last time any post was modified or published.
7532   *
7533   * @since 3.1.0
7534   * @since 4.4.0 The `$post_type` argument was added.
7535   * @access private
7536   *
7537   * @global wpdb $wpdb WordPress database abstraction object.
7538   *
7539   * @param string $timezone  The timezone for the timestamp. See get_lastpostdate().
7540   *                          for information on accepted values.
7541   * @param string $field     Post field to check. Accepts 'date' or 'modified'.
7542   * @param string $post_type Optional. The post type to check. Default 'any'.
7543   * @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure.
7544   */
7545  function _get_last_post_time( $timezone, $field, $post_type = 'any' ) {
7546      global $wpdb;
7547  
7548      if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) {
7549          return false;
7550      }
7551  
7552      $timezone = strtolower( $timezone );
7553  
7554      $key = "lastpost{$field}:$timezone";
7555      if ( 'any' !== $post_type ) {
7556          $key .= ':' . sanitize_key( $post_type );
7557      }
7558  
7559      $date = wp_cache_get( $key, 'timeinfo' );
7560      if ( false !== $date ) {
7561          return $date;
7562      }
7563  
7564      if ( 'any' === $post_type ) {
7565          $post_types = get_post_types( array( 'public' => true ) );
7566          array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) );
7567          $post_types = "'" . implode( "', '", $post_types ) . "'";
7568      } else {
7569          $post_types = "'" . sanitize_key( $post_type ) . "'";
7570      }
7571  
7572      switch ( $timezone ) {
7573          case 'gmt':
7574              $date = $wpdb->get_var( "SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" );
7575              break;
7576          case 'blog':
7577              $date = $wpdb->get_var( "SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" );
7578              break;
7579          case 'server':
7580              $add_seconds_server = gmdate( 'Z' );
7581              $date               = $wpdb->get_var( "SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" );
7582              break;
7583      }
7584  
7585      if ( $date ) {
7586          wp_cache_set( $key, $date, 'timeinfo' );
7587  
7588          return $date;
7589      }
7590  
7591      return false;
7592  }
7593  
7594  /**
7595   * Updates posts in cache.
7596   *
7597   * @since 1.5.1
7598   *
7599   * @param WP_Post[] $posts Array of post objects (passed by reference).
7600   */
7601  function update_post_cache( &$posts ) {
7602      if ( ! $posts ) {
7603          return;
7604      }
7605  
7606      $data = array();
7607      foreach ( $posts as $post ) {
7608          if ( empty( $post->filter ) || 'raw' !== $post->filter ) {
7609              $post = sanitize_post( $post, 'raw' );
7610          }
7611          $data[ $post->ID ] = $post;
7612      }
7613      wp_cache_add_multiple( $data, 'posts' );
7614  }
7615  
7616  /**
7617   * Will clean the post in the cache.
7618   *
7619   * Cleaning means delete from the cache of the post. Will call to clean the term
7620   * object cache associated with the post ID.
7621   *
7622   * This function not run if $_wp_suspend_cache_invalidation is not empty. See
7623   * wp_suspend_cache_invalidation().
7624   *
7625   * @since 2.0.0
7626   *
7627   * @global bool $_wp_suspend_cache_invalidation
7628   *
7629   * @param int|WP_Post $post Post ID or post object to remove from the cache.
7630   */
7631  function clean_post_cache( $post ) {
7632      global $_wp_suspend_cache_invalidation;
7633  
7634      if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
7635          return;
7636      }
7637  
7638      $post = get_post( $post );
7639  
7640      if ( ! $post ) {
7641          return;
7642      }
7643  
7644      wp_cache_delete( $post->ID, 'posts' );
7645      wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' );
7646      wp_cache_delete( $post->ID, 'post_meta' );
7647  
7648      clean_object_term_cache( $post->ID, $post->post_type );
7649  
7650      wp_cache_delete( 'wp_get_archives', 'general' );
7651  
7652      /**
7653       * Fires immediately after the given post's cache is cleaned.
7654       *
7655       * @since 2.5.0
7656       *
7657       * @param int     $post_id Post ID.
7658       * @param WP_Post $post    Post object.
7659       */
7660      do_action( 'clean_post_cache', $post->ID, $post );
7661  
7662      if ( 'page' === $post->post_type ) {
7663          wp_cache_delete( 'all_page_ids', 'posts' );
7664  
7665          /**
7666           * Fires immediately after the given page's cache is cleaned.
7667           *
7668           * @since 2.5.0
7669           *
7670           * @param int $post_id Post ID.
7671           */
7672          do_action( 'clean_page_cache', $post->ID );
7673      }
7674  
7675      wp_cache_set_posts_last_changed();
7676  }
7677  
7678  /**
7679   * Updates post, term, and metadata caches for a list of post objects.
7680   *
7681   * @since 1.5.0
7682   *
7683   * @param WP_Post[] $posts             Array of post objects (passed by reference).
7684   * @param string    $post_type         Optional. Post type. Default 'post'.
7685   * @param bool      $update_term_cache Optional. Whether to update the term cache. Default true.
7686   * @param bool      $update_meta_cache Optional. Whether to update the meta cache. Default true.
7687   */
7688  function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) {
7689      // No point in doing all this work if we didn't match any posts.
7690      if ( ! $posts ) {
7691          return;
7692      }
7693  
7694      update_post_cache( $posts );
7695  
7696      $post_ids = array();
7697      foreach ( $posts as $post ) {
7698          $post_ids[] = $post->ID;
7699      }
7700  
7701      if ( ! $post_type ) {
7702          $post_type = 'any';
7703      }
7704  
7705      if ( $update_term_cache ) {
7706          if ( is_array( $post_type ) ) {
7707              $ptypes = $post_type;
7708          } elseif ( 'any' === $post_type ) {
7709              $ptypes = array();
7710              // Just use the post_types in the supplied posts.
7711              foreach ( $posts as $post ) {
7712                  $ptypes[] = $post->post_type;
7713              }
7714              $ptypes = array_unique( $ptypes );
7715          } else {
7716              $ptypes = array( $post_type );
7717          }
7718  
7719          if ( ! empty( $ptypes ) ) {
7720              update_object_term_cache( $post_ids, $ptypes );
7721          }
7722      }
7723  
7724      if ( $update_meta_cache ) {
7725          update_postmeta_cache( $post_ids );
7726      }
7727  }
7728  
7729  /**
7730   * Updates post author user caches for a list of post objects.
7731   *
7732   * @since 6.1.0
7733   *
7734   * @param WP_Post[] $posts Array of post objects.
7735   */
7736  function update_post_author_caches( $posts ) {
7737      /*
7738       * cache_users() is a pluggable function so is not available prior
7739       * to the `plugins_loaded` hook firing. This is to ensure against
7740       * fatal errors when the function is not available.
7741       */
7742      if ( ! function_exists( 'cache_users' ) ) {
7743          return;
7744      }
7745  
7746      $author_ids = wp_list_pluck( $posts, 'post_author' );
7747      $author_ids = array_map( 'absint', $author_ids );
7748      $author_ids = array_unique( array_filter( $author_ids ) );
7749  
7750      cache_users( $author_ids );
7751  }
7752  
7753  /**
7754   * Updates parent post caches for a list of post objects.
7755   *
7756   * @since 6.1.0
7757   *
7758   * @param WP_Post[] $posts Array of post objects.
7759   */
7760  function update_post_parent_caches( $posts ) {
7761      $parent_ids = wp_list_pluck( $posts, 'post_parent' );
7762      $parent_ids = array_map( 'absint', $parent_ids );
7763      $parent_ids = array_unique( array_filter( $parent_ids ) );
7764  
7765      if ( ! empty( $parent_ids ) ) {
7766          _prime_post_caches( $parent_ids, false );
7767      }
7768  }
7769  
7770  /**
7771   * Updates metadata cache for a list of post IDs.
7772   *
7773   * Performs SQL query to retrieve the metadata for the post IDs and updates the
7774   * metadata cache for the posts. Therefore, the functions, which call this
7775   * function, do not need to perform SQL queries on their own.
7776   *
7777   * @since 2.1.0
7778   *
7779   * @param int[] $post_ids Array of post IDs.
7780   * @return array|false An array of metadata on success, false if there is nothing to update.
7781   */
7782  function update_postmeta_cache( $post_ids ) {
7783      return update_meta_cache( 'post', $post_ids );
7784  }
7785  
7786  /**
7787   * Will clean the attachment in the cache.
7788   *
7789   * Cleaning means delete from the cache. Optionally will clean the term
7790   * object cache associated with the attachment ID.
7791   *
7792   * This function will not run if $_wp_suspend_cache_invalidation is not empty.
7793   *
7794   * @since 3.0.0
7795   *
7796   * @global bool $_wp_suspend_cache_invalidation
7797   *
7798   * @param int  $id          The attachment ID in the cache to clean.
7799   * @param bool $clean_terms Optional. Whether to clean terms cache. Default false.
7800   */
7801  function clean_attachment_cache( $id, $clean_terms = false ) {
7802      global $_wp_suspend_cache_invalidation;
7803  
7804      if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
7805          return;
7806      }
7807  
7808      $id = (int) $id;
7809  
7810      wp_cache_delete( $id, 'posts' );
7811      wp_cache_delete( $id, 'post_meta' );
7812  
7813      if ( $clean_terms ) {
7814          clean_object_term_cache( $id, 'attachment' );
7815      }
7816  
7817      /**
7818       * Fires after the given attachment's cache is cleaned.
7819       *
7820       * @since 3.0.0
7821       *
7822       * @param int $id Attachment ID.
7823       */
7824      do_action( 'clean_attachment_cache', $id );
7825  }
7826  
7827  //
7828  // Hooks.
7829  //
7830  
7831  /**
7832   * Hook for managing future post transitions to published.
7833   *
7834   * @since 2.3.0
7835   * @access private
7836   *
7837   * @see wp_clear_scheduled_hook()
7838   * @global wpdb $wpdb WordPress database abstraction object.
7839   *
7840   * @param string  $new_status New post status.
7841   * @param string  $old_status Previous post status.
7842   * @param WP_Post $post       Post object.
7843   */
7844  function _transition_post_status( $new_status, $old_status, $post ) {
7845      global $wpdb;
7846  
7847      if ( 'publish' !== $old_status && 'publish' === $new_status ) {
7848          // Reset GUID if transitioning to publish and it is empty.
7849          if ( '' === get_the_guid( $post->ID ) ) {
7850              $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
7851          }
7852  
7853          /**
7854           * Fires when a post's status is transitioned from private to published.
7855           *
7856           * @since 1.5.0
7857           * @deprecated 2.3.0 Use {@see 'private_to_publish'} instead.
7858           *
7859           * @param int $post_id Post ID.
7860           */
7861          do_action_deprecated( 'private_to_published', array( $post->ID ), '2.3.0', 'private_to_publish' );
7862      }
7863  
7864      // If published posts changed clear the lastpostmodified cache.
7865      if ( 'publish' === $new_status || 'publish' === $old_status ) {
7866          foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
7867              wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
7868              wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
7869              wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' );
7870          }
7871      }
7872  
7873      if ( $new_status !== $old_status ) {
7874          wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
7875          wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
7876      }
7877  
7878      // Always clears the hook in case the post status bounced from future to draft.
7879      wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
7880  }
7881  
7882  /**
7883   * Hook used to schedule publication for a post marked for the future.
7884   *
7885   * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
7886   *
7887   * @since 2.3.0
7888   * @access private
7889   *
7890   * @param int     $deprecated Not used. Can be set to null. Never implemented. Not marked
7891   *                            as deprecated with _deprecated_argument() as it conflicts with
7892   *                            wp_transition_post_status() and the default filter for _future_post_hook().
7893   * @param WP_Post $post       Post object.
7894   */
7895  function _future_post_hook( $deprecated, $post ) {
7896      wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
7897      wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) );
7898  }
7899  
7900  /**
7901   * Hook to schedule pings and enclosures when a post is published.
7902   *
7903   * Uses XMLRPC_REQUEST and WP_IMPORTING constants.
7904   *
7905   * @since 2.3.0
7906   * @access private
7907   *
7908   * @param int $post_id The ID of the post being published.
7909   */
7910  function _publish_post_hook( $post_id ) {
7911      if ( defined( 'XMLRPC_REQUEST' ) ) {
7912          /**
7913           * Fires when _publish_post_hook() is called during an XML-RPC request.
7914           *
7915           * @since 2.1.0
7916           *
7917           * @param int $post_id Post ID.
7918           */
7919          do_action( 'xmlrpc_publish_post', $post_id );
7920      }
7921  
7922      if ( defined( 'WP_IMPORTING' ) ) {
7923          return;
7924      }
7925  
7926      if ( get_option( 'default_pingback_flag' ) ) {
7927          add_post_meta( $post_id, '_pingme', '1', true );
7928      }
7929      add_post_meta( $post_id, '_encloseme', '1', true );
7930  
7931      $to_ping = get_to_ping( $post_id );
7932      if ( ! empty( $to_ping ) ) {
7933          add_post_meta( $post_id, '_trackbackme', '1' );
7934      }
7935  
7936      if ( ! wp_next_scheduled( 'do_pings' ) ) {
7937          wp_schedule_single_event( time(), 'do_pings' );
7938      }
7939  }
7940  
7941  /**
7942   * Returns the ID of the post's parent.
7943   *
7944   * @since 3.1.0
7945   * @since 5.9.0 The `$post` parameter was made optional.
7946   *
7947   * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
7948   * @return int|false Post parent ID (which can be 0 if there is no parent),
7949   *                   or false if the post does not exist.
7950   */
7951  function wp_get_post_parent_id( $post = null ) {
7952      $post = get_post( $post );
7953  
7954      if ( ! $post || is_wp_error( $post ) ) {
7955          return false;
7956      }
7957  
7958      return (int) $post->post_parent;
7959  }
7960  
7961  /**
7962   * Checks the given subset of the post hierarchy for hierarchy loops.
7963   *
7964   * Prevents loops from forming and breaks those that it finds. Attached
7965   * to the {@see 'wp_insert_post_parent'} filter.
7966   *
7967   * @since 3.1.0
7968   *
7969   * @see wp_find_hierarchy_loop()
7970   *
7971   * @param int $post_parent ID of the parent for the post we're checking.
7972   * @param int $post_id     ID of the post we're checking.
7973   * @return int The new post_parent for the post, 0 otherwise.
7974   */
7975  function wp_check_post_hierarchy_for_loops( $post_parent, $post_id ) {
7976      // Nothing fancy here - bail.
7977      if ( ! $post_parent ) {
7978          return 0;
7979      }
7980  
7981      // New post can't cause a loop.
7982      if ( ! $post_id ) {
7983          return $post_parent;
7984      }
7985  
7986      // Can't be its own parent.
7987      if ( $post_parent === $post_id ) {
7988          return 0;
7989      }
7990  
7991      // Now look for larger loops.
7992      $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_id, $post_parent );
7993      if ( ! $loop ) {
7994          return $post_parent; // No loop.
7995      }
7996  
7997      // Setting $post_parent to the given value causes a loop.
7998      if ( isset( $loop[ $post_id ] ) ) {
7999          return 0;
8000      }
8001  
8002      // There's a loop, but it doesn't contain $post_id. Break the loop.
8003      foreach ( array_keys( $loop ) as $loop_member ) {
8004          wp_update_post(
8005              array(
8006                  'ID'          => $loop_member,
8007                  'post_parent' => 0,
8008              )
8009          );
8010      }
8011  
8012      return $post_parent;
8013  }
8014  
8015  /**
8016   * Sets the post thumbnail (featured image) for the given post.
8017   *
8018   * @since 3.1.0
8019   *
8020   * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
8021   * @param int         $thumbnail_id Thumbnail to attach.
8022   * @return int|bool Post meta ID if the key didn't exist (ie. this is the first time that
8023   *                  a thumbnail has been saved for the post), true on successful update,
8024   *                  false on failure or if the value passed is the same as the one that
8025   *                  is already in the database.
8026   */
8027  function set_post_thumbnail( $post, $thumbnail_id ) {
8028      $post         = get_post( $post );
8029      $thumbnail_id = absint( $thumbnail_id );
8030      if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
8031          if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
8032              return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
8033          } else {
8034              return delete_post_meta( $post->ID, '_thumbnail_id' );
8035          }
8036      }
8037      return false;
8038  }
8039  
8040  /**
8041   * Removes the thumbnail (featured image) from the given post.
8042   *
8043   * @since 3.3.0
8044   *
8045   * @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed.
8046   * @return bool True on success, false on failure.
8047   */
8048  function delete_post_thumbnail( $post ) {
8049      $post = get_post( $post );
8050      if ( $post ) {
8051          return delete_post_meta( $post->ID, '_thumbnail_id' );
8052      }
8053      return false;
8054  }
8055  
8056  /**
8057   * Deletes auto-drafts for new posts that are > 7 days old.
8058   *
8059   * @since 3.4.0
8060   *
8061   * @global wpdb $wpdb WordPress database abstraction object.
8062   */
8063  function wp_delete_auto_drafts() {
8064      global $wpdb;
8065  
8066      // Cleanup old auto-drafts more than 7 days old.
8067      $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
8068      foreach ( (array) $old_posts as $delete ) {
8069          // Force delete.
8070          wp_delete_post( $delete, true );
8071      }
8072  }
8073  
8074  /**
8075   * Queues posts for lazy-loading of term meta.
8076   *
8077   * @since 4.5.0
8078   *
8079   * @param WP_Post[] $posts Array of WP_Post objects.
8080   */
8081  function wp_queue_posts_for_term_meta_lazyload( $posts ) {
8082      $post_type_taxonomies = array();
8083      $prime_post_terms     = array();
8084      foreach ( $posts as $post ) {
8085          if ( ! ( $post instanceof WP_Post ) ) {
8086              continue;
8087          }
8088  
8089          if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) {
8090              $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type );
8091          }
8092  
8093          foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
8094              $prime_post_terms[ $taxonomy ][] = $post->ID;
8095          }
8096      }
8097  
8098      $term_ids = array();
8099      if ( $prime_post_terms ) {
8100          foreach ( $prime_post_terms as $taxonomy => $post_ids ) {
8101              $cached_term_ids = wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" );
8102              if ( is_array( $cached_term_ids ) ) {
8103                  $cached_term_ids = array_filter( $cached_term_ids );
8104                  foreach ( $cached_term_ids as $_term_ids ) {
8105                      // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
8106                      foreach ( $_term_ids as $term_id ) {
8107                          if ( is_numeric( $term_id ) ) {
8108                              $term_ids[] = (int) $term_id;
8109                          } elseif ( isset( $term_id->term_id ) ) {
8110                              $term_ids[] = (int) $term_id->term_id;
8111                          }
8112                      }
8113                  }
8114              }
8115          }
8116          $term_ids = array_unique( $term_ids );
8117      }
8118  
8119      wp_lazyload_term_meta( $term_ids );
8120  }
8121  
8122  /**
8123   * Updates the custom taxonomies' term counts when a post's status is changed.
8124   *
8125   * For example, default posts term counts (for custom taxonomies) don't include
8126   * private / draft posts.
8127   *
8128   * @since 3.3.0
8129   * @access private
8130   *
8131   * @param string  $new_status New post status.
8132   * @param string  $old_status Old post status.
8133   * @param WP_Post $post       Post object.
8134   */
8135  function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
8136      // Update counts for the post's terms.
8137      foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
8138          $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
8139          wp_update_term_count( $tt_ids, $taxonomy );
8140      }
8141  }
8142  
8143  /**
8144   * Adds any posts from the given IDs to the cache that do not already exist in cache.
8145   *
8146   * @since 3.4.0
8147   * @since 6.1.0 This function is no longer marked as "private".
8148   *
8149   * @see update_post_cache()
8150   * @see update_postmeta_cache()
8151   * @see update_object_term_cache()
8152   *
8153   * @global wpdb $wpdb WordPress database abstraction object.
8154   *
8155   * @param int[] $ids               ID list.
8156   * @param bool  $update_term_cache Optional. Whether to update the term cache. Default true.
8157   * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
8158   */
8159  function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
8160      global $wpdb;
8161  
8162      $non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
8163      if ( ! empty( $non_cached_ids ) ) {
8164          $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
8165  
8166          if ( $fresh_posts ) {
8167              // Despite the name, update_post_cache() expects an array rather than a single post.
8168              update_post_cache( $fresh_posts );
8169          }
8170      }
8171  
8172      if ( $update_meta_cache ) {
8173          update_postmeta_cache( $ids );
8174      }
8175  
8176      if ( $update_term_cache ) {
8177          $post_types = array_map( 'get_post_type', $ids );
8178          $post_types = array_unique( $post_types );
8179          update_object_term_cache( $ids, $post_types );
8180      }
8181  }
8182  
8183  /**
8184   * Prime the cache containing the parent ID of various post objects.
8185   *
8186   * @since 6.4.0
8187   *
8188   * @global wpdb $wpdb WordPress database abstraction object.
8189   *
8190   * @param int[] $ids ID list.
8191   */
8192  function _prime_post_parent_id_caches( array $ids ) {
8193      global $wpdb;
8194  
8195      $ids = array_filter( $ids, '_validate_cache_id' );
8196      $ids = array_unique( array_map( 'intval', $ids ), SORT_NUMERIC );
8197  
8198      if ( empty( $ids ) ) {
8199          return;
8200      }
8201  
8202      $cache_keys = array();
8203      foreach ( $ids as $id ) {
8204          $cache_keys[ $id ] = 'post_parent:' . (string) $id;
8205      }
8206  
8207      $cached_data = wp_cache_get_multiple( array_values( $cache_keys ), 'posts' );
8208  
8209      $non_cached_ids = array();
8210      foreach ( $cache_keys as $id => $cache_key ) {
8211          if ( false === $cached_data[ $cache_key ] ) {
8212              $non_cached_ids[] = $id;
8213          }
8214      }
8215  
8216      if ( ! empty( $non_cached_ids ) ) {
8217          $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
8218  
8219          if ( $fresh_posts ) {
8220              $post_parent_data = array();
8221              foreach ( $fresh_posts as $fresh_post ) {
8222                  $post_parent_data[ 'post_parent:' . (string) $fresh_post->ID ] = (int) $fresh_post->post_parent;
8223              }
8224  
8225              wp_cache_add_multiple( $post_parent_data, 'posts' );
8226          }
8227      }
8228  }
8229  
8230  /**
8231   * Adds a suffix if any trashed posts have a given slug.
8232   *
8233   * Store its desired (i.e. current) slug so it can try to reclaim it
8234   * if the post is untrashed.
8235   *
8236   * For internal use.
8237   *
8238   * @since 4.5.0
8239   * @access private
8240   *
8241   * @param string $post_name Post slug.
8242   * @param int    $post_id   Optional. Post ID that should be ignored. Default 0.
8243   */
8244  function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id = 0 ) {
8245      $trashed_posts_with_desired_slug = get_posts(
8246          array(
8247              'name'         => $post_name,
8248              'post_status'  => 'trash',
8249              'post_type'    => 'any',
8250              'nopaging'     => true,
8251              'post__not_in' => array( $post_id ),
8252          )
8253      );
8254  
8255      if ( ! empty( $trashed_posts_with_desired_slug ) ) {
8256          foreach ( $trashed_posts_with_desired_slug as $_post ) {
8257              wp_add_trashed_suffix_to_post_name_for_post( $_post );
8258          }
8259      }
8260  }
8261  
8262  /**
8263   * Adds a trashed suffix for a given post.
8264   *
8265   * Store its desired (i.e. current) slug so it can try to reclaim it
8266   * if the post is untrashed.
8267   *
8268   * For internal use.
8269   *
8270   * @since 4.5.0
8271   * @access private
8272   *
8273   * @global wpdb $wpdb WordPress database abstraction object.
8274   *
8275   * @param WP_Post $post The post.
8276   * @return string New slug for the post.
8277   */
8278  function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
8279      global $wpdb;
8280  
8281      $post = get_post( $post );
8282  
8283      if ( str_ends_with( $post->post_name, '__trashed' ) ) {
8284          return $post->post_name;
8285      }
8286      add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
8287      $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed';
8288      $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
8289      clean_post_cache( $post->ID );
8290      return $post_name;
8291  }
8292  
8293  /**
8294   * Sets the last changed time for the 'posts' cache group.
8295   *
8296   * @since 5.0.0
8297   */
8298  function wp_cache_set_posts_last_changed() {
8299      wp_cache_set_last_changed( 'posts' );
8300  }
8301  
8302  /**
8303   * Gets all available post MIME types for a given post type.
8304   *
8305   * @since 2.5.0
8306   *
8307   * @global wpdb $wpdb WordPress database abstraction object.
8308   *
8309   * @param string $type
8310   * @return string[] An array of MIME types.
8311   */
8312  function get_available_post_mime_types( $type = 'attachment' ) {
8313      global $wpdb;
8314  
8315      /**
8316       * Filters the list of available post MIME types for the given post type.
8317       *
8318       * @since 6.4.0
8319       *
8320       * @param string[]|null $mime_types An array of MIME types. Default null.
8321       * @param string        $type       The post type name. Usually 'attachment' but can be any post type.
8322       */
8323      $mime_types = apply_filters( 'pre_get_available_post_mime_types', null, $type );
8324  
8325      if ( ! is_array( $mime_types ) ) {
8326          $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s AND post_mime_type != ''", $type ) );
8327      }
8328  
8329      // Remove nulls from returned $mime_types.
8330      return array_values( array_filter( $mime_types ) );
8331  }
8332  
8333  /**
8334   * Retrieves the path to an uploaded image file.
8335   *
8336   * Similar to `get_attached_file()` however some images may have been processed after uploading
8337   * to make them suitable for web use. In this case the attached "full" size file is usually replaced
8338   * with a scaled down version of the original image. This function always returns the path
8339   * to the originally uploaded image file.
8340   *
8341   * @since 5.3.0
8342   * @since 5.4.0 Added the `$unfiltered` parameter.
8343   *
8344   * @param int  $attachment_id Attachment ID.
8345   * @param bool $unfiltered Optional. Passed through to `get_attached_file()`. Default false.
8346   * @return string|false Path to the original image file or false if the attachment is not an image.
8347   */
8348  function wp_get_original_image_path( $attachment_id, $unfiltered = false ) {
8349      if ( ! wp_attachment_is_image( $attachment_id ) ) {
8350          return false;
8351      }
8352  
8353      $image_meta = wp_get_attachment_metadata( $attachment_id );
8354      $image_file = get_attached_file( $attachment_id, $unfiltered );
8355  
8356      if ( empty( $image_meta['original_image'] ) ) {
8357          $original_image = $image_file;
8358      } else {
8359          $original_image = path_join( dirname( $image_file ), $image_meta['original_image'] );
8360      }
8361  
8362      /**
8363       * Filters the path to the original image.
8364       *
8365       * @since 5.3.0
8366       *
8367       * @param string $original_image Path to original image file.
8368       * @param int    $attachment_id  Attachment ID.
8369       */
8370      return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id );
8371  }
8372  
8373  /**
8374   * Retrieves the URL to an original attachment image.
8375   *
8376   * Similar to `wp_get_attachment_url()` however some images may have been
8377   * processed after uploading. In this case this function returns the URL
8378   * to the originally uploaded image file.
8379   *
8380   * @since 5.3.0
8381   *
8382   * @param int $attachment_id Attachment post ID.
8383   * @return string|false Attachment image URL, false on error or if the attachment is not an image.
8384   */
8385  function wp_get_original_image_url( $attachment_id ) {
8386      if ( ! wp_attachment_is_image( $attachment_id ) ) {
8387          return false;
8388      }
8389  
8390      $image_url = wp_get_attachment_url( $attachment_id );
8391  
8392      if ( ! $image_url ) {
8393          return false;
8394      }
8395  
8396      $image_meta = wp_get_attachment_metadata( $attachment_id );
8397  
8398      if ( empty( $image_meta['original_image'] ) ) {
8399          $original_image_url = $image_url;
8400      } else {
8401          $original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] );
8402      }
8403  
8404      /**
8405       * Filters the URL to the original attachment image.
8406       *
8407       * @since 5.3.0
8408       *
8409       * @param string $original_image_url URL to original image.
8410       * @param int    $attachment_id      Attachment ID.
8411       */
8412      return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id );
8413  }
8414  
8415  /**
8416   * Filters callback which sets the status of an untrashed post to its previous status.
8417   *
8418   * This can be used as a callback on the `wp_untrash_post_status` filter.
8419   *
8420   * @since 5.6.0
8421   *
8422   * @param string $new_status      The new status of the post being restored.
8423   * @param int    $post_id         The ID of the post being restored.
8424   * @param string $previous_status The status of the post at the point where it was trashed.
8425   * @return string The new status of the post.
8426   */
8427  function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) {
8428      return $previous_status;
8429  }
8430  
8431  /**
8432   * Returns whether the post can be edited in the block editor.
8433   *
8434   * @since 5.0.0
8435   * @since 6.1.0 Moved to wp-includes from wp-admin.
8436   *
8437   * @param int|WP_Post $post Post ID or WP_Post object.
8438   * @return bool Whether the post can be edited in the block editor.
8439   */
8440  function use_block_editor_for_post( $post ) {
8441      $post = get_post( $post );
8442  
8443      if ( ! $post ) {
8444          return false;
8445      }
8446  
8447      // We're in the meta box loader, so don't use the block editor.
8448      if ( is_admin() && isset( $_GET['meta-box-loader'] ) ) {
8449          check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
8450          return false;
8451      }
8452  
8453      $use_block_editor = use_block_editor_for_post_type( $post->post_type );
8454  
8455      /**
8456       * Filters whether a post is able to be edited in the block editor.
8457       *
8458       * @since 5.0.0
8459       *
8460       * @param bool    $use_block_editor Whether the post can be edited or not.
8461       * @param WP_Post $post             The post being checked.
8462       */
8463      return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
8464  }
8465  
8466  /**
8467   * Returns whether a post type is compatible with the block editor.
8468   *
8469   * The block editor depends on the REST API, and if the post type is not shown in the
8470   * REST API, then it won't work with the block editor.
8471   *
8472   * @since 5.0.0
8473   * @since 6.1.0 Moved to wp-includes from wp-admin.
8474   *
8475   * @param string $post_type The post type.
8476   * @return bool Whether the post type can be edited with the block editor.
8477   */
8478  function use_block_editor_for_post_type( $post_type ) {
8479      if ( ! post_type_exists( $post_type ) ) {
8480          return false;
8481      }
8482  
8483      if ( ! post_type_supports( $post_type, 'editor' ) ) {
8484          return false;
8485      }
8486  
8487      $post_type_object = get_post_type_object( $post_type );
8488      if ( $post_type_object && ! $post_type_object->show_in_rest ) {
8489          return false;
8490      }
8491  
8492      /**
8493       * Filters whether a post is able to be edited in the block editor.
8494       *
8495       * @since 5.0.0
8496       *
8497       * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
8498       * @param string $post_type         The post type being checked.
8499       */
8500      return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
8501  }
8502  
8503  /**
8504   * Registers any additional post meta fields.
8505   *
8506   * @since 6.3.0 Adds `wp_pattern_sync_status` meta field to the wp_block post type so an unsynced option can be added.
8507   *
8508   * @link https://github.com/WordPress/gutenberg/pull/51144
8509   */
8510  function wp_create_initial_post_meta() {
8511      register_post_meta(
8512          'wp_block',
8513          'wp_pattern_sync_status',
8514          array(
8515              'sanitize_callback' => 'sanitize_text_field',
8516              'single'            => true,
8517              'type'              => 'string',
8518              'show_in_rest'      => array(
8519                  'schema' => array(
8520                      'type' => 'string',
8521                      'enum' => array( 'partial', 'unsynced' ),
8522                  ),
8523              ),
8524          )
8525      );
8526  }


Generated : Fri Feb 7 08:20:01 2025 Cross-referenced by PHPXref