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