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