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