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