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