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