[ 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, the following keys are accepted as part of the capabilities array: 1910 * 1911 * - edit_post, read_post, and delete_post are meta capabilities, which are then 1912 * generally mapped to corresponding primitive capabilities depending on the 1913 * context, which would be the post being edited/read/deleted and the user or 1914 * role being checked. Thus these capabilities would generally not be granted 1915 * directly to users or roles. 1916 * 1917 * - edit_posts - Controls whether objects of this post type can be edited. 1918 * - edit_others_posts - Controls whether objects of this type owned by other users 1919 * can be edited. If the post type does not support an author, then this will 1920 * behave like edit_posts. 1921 * - delete_posts - Controls whether objects of this post type can be deleted. 1922 * - publish_posts - Controls publishing objects of this post type. 1923 * - read_private_posts - Controls whether private objects can be read. 1924 * - create_posts - Controls whether objects of this post type can be created. 1925 * 1926 * These primitive capabilities are checked in core in various locations. 1927 * There are also six other primitive capabilities which are not referenced 1928 * directly in core, except in map_meta_cap(), which takes the three aforementioned 1929 * meta capabilities and translates them into one or more primitive capabilities 1930 * that must then be checked against the user or role, depending on the context. 1931 * 1932 * - read - Controls whether objects of this post type can be read. 1933 * - delete_private_posts - Controls whether private objects can be deleted. 1934 * - delete_published_posts - Controls whether published objects can be deleted. 1935 * - delete_others_posts - Controls whether objects owned by other users can be 1936 * can be deleted. If the post type does not support an author, then this will 1937 * behave like delete_posts. 1938 * - edit_private_posts - Controls whether private objects can be edited. 1939 * - edit_published_posts - Controls whether published objects can be edited. 1940 * 1941 * These additional capabilities are only used in map_meta_cap(). Thus, they are 1942 * only assigned by default if the post type is registered with the 'map_meta_cap' 1943 * argument set to true (default is false). 1944 * 1945 * @since 3.0.0 1946 * @since 5.4.0 'delete_posts' is included in default capabilities. 1947 * 1948 * @see register_post_type() 1949 * @see map_meta_cap() 1950 * 1951 * @param object $args Post type registration arguments. 1952 * @return object { 1953 * Object with all the capabilities as member variables. 1954 * 1955 * @type string $edit_post Capability to edit a post. 1956 * @type string $read_post Capability to read a post. 1957 * @type string $delete_post Capability to delete a post. 1958 * @type string $edit_posts Capability to edit posts. 1959 * @type string $edit_others_posts Capability to edit others' posts. 1960 * @type string $delete_posts Capability to delete posts. 1961 * @type string $publish_posts Capability to publish posts. 1962 * @type string $read_private_posts Capability to read private posts. 1963 * @type string $create_posts Capability to create posts. 1964 * @type string $read Optional. Capability to read a post. 1965 * @type string $delete_private_posts Optional. Capability to delete private posts. 1966 * @type string $delete_published_posts Optional. Capability to delete published posts. 1967 * @type string $delete_others_posts Optional. Capability to delete others' posts. 1968 * @type string $edit_private_posts Optional. Capability to edit private posts. 1969 * @type string $edit_published_posts Optional. Capability to edit published posts. 1970 * } 1971 */ 1972 function get_post_type_capabilities( $args ) { 1973 if ( ! is_array( $args->capability_type ) ) { 1974 $args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); 1975 } 1976 1977 // Singular base for meta capabilities, plural base for primitive capabilities. 1978 list( $singular_base, $plural_base ) = $args->capability_type; 1979 1980 $default_capabilities = array( 1981 // Meta capabilities. 1982 'edit_post' => 'edit_' . $singular_base, 1983 'read_post' => 'read_' . $singular_base, 1984 'delete_post' => 'delete_' . $singular_base, 1985 // Primitive capabilities used outside of map_meta_cap(): 1986 'edit_posts' => 'edit_' . $plural_base, 1987 'edit_others_posts' => 'edit_others_' . $plural_base, 1988 'delete_posts' => 'delete_' . $plural_base, 1989 'publish_posts' => 'publish_' . $plural_base, 1990 'read_private_posts' => 'read_private_' . $plural_base, 1991 ); 1992 1993 // Primitive capabilities used within map_meta_cap(): 1994 if ( $args->map_meta_cap ) { 1995 $default_capabilities_for_mapping = array( 1996 'read' => 'read', 1997 'delete_private_posts' => 'delete_private_' . $plural_base, 1998 'delete_published_posts' => 'delete_published_' . $plural_base, 1999 'delete_others_posts' => 'delete_others_' . $plural_base, 2000 'edit_private_posts' => 'edit_private_' . $plural_base, 2001 'edit_published_posts' => 'edit_published_' . $plural_base, 2002 ); 2003 $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); 2004 } 2005 2006 $capabilities = array_merge( $default_capabilities, $args->capabilities ); 2007 2008 // Post creation capability simply maps to edit_posts by default: 2009 if ( ! isset( $capabilities['create_posts'] ) ) { 2010 $capabilities['create_posts'] = $capabilities['edit_posts']; 2011 } 2012 2013 // Remember meta capabilities for future reference. 2014 if ( $args->map_meta_cap ) { 2015 _post_type_meta_capabilities( $capabilities ); 2016 } 2017 2018 return (object) $capabilities; 2019 } 2020 2021 /** 2022 * Stores or returns a list of post type meta caps for map_meta_cap(). 2023 * 2024 * @since 3.1.0 2025 * @access private 2026 * 2027 * @global array $post_type_meta_caps Used to store meta capabilities. 2028 * 2029 * @param string[] $capabilities Post type meta capabilities. 2030 */ 2031 function _post_type_meta_capabilities( $capabilities = null ) { 2032 global $post_type_meta_caps; 2033 2034 foreach ( $capabilities as $core => $custom ) { 2035 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) { 2036 $post_type_meta_caps[ $custom ] = $core; 2037 } 2038 } 2039 } 2040 2041 /** 2042 * Builds an object with all post type labels out of a post type object. 2043 * 2044 * Accepted keys of the label array in the post type object: 2045 * 2046 * - `name` - General name for the post type, usually plural. The same and overridden 2047 * by `$post_type_object->label`. Default is 'Posts' / 'Pages'. 2048 * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. 2049 * - `add_new` - Label for adding a new item. Default is 'Add Post' / 'Add Page'. 2050 * - `add_new_item` - Label for adding a new singular item. Default is 'Add Post' / 'Add Page'. 2051 * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. 2052 * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. 2053 * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. 2054 * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'. 2055 * - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'. 2056 * - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. 2057 * - `not_found_in_trash` - Label used when no items are in the Trash. Default is 'No posts found in Trash' / 2058 * 'No pages found in Trash'. 2059 * - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical 2060 * post types. Default is 'Parent Page:'. 2061 * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'. 2062 * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'. 2063 * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'. 2064 * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'. 2065 * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' / 2066 * 'Uploaded to this page'. 2067 * - `featured_image` - Label for the featured image meta box title. Default is 'Featured image'. 2068 * - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'. 2069 * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'. 2070 * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'. 2071 * - `menu_name` - Label for the menu name. Default is the same as `name`. 2072 * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' / 2073 * 'Filter pages list'. 2074 * - `filter_by_date` - Label for the date filter in list tables. Default is 'Filter by date'. 2075 * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' / 2076 * 'Pages list navigation'. 2077 * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. 2078 * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' 2079 * - `item_published_privately` - Label used when an item is published with private visibility. 2080 * Default is 'Post published privately.' / 'Page published privately.' 2081 * - `item_reverted_to_draft` - Label used when an item is switched to a draft. 2082 * Default is 'Post reverted to draft.' / 'Page reverted to draft.' 2083 * - `item_trashed` - Label used when an item is moved to Trash. Default is 'Post trashed.' / 'Page trashed.' 2084 * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / 2085 * 'Page scheduled.' 2086 * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' 2087 * - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'. 2088 * - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' / 2089 * 'A link to a page.' 2090 * 2091 * Above, the first default value is for non-hierarchical post types (like posts) 2092 * and the second one is for hierarchical post types (like pages). 2093 * 2094 * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter. 2095 * 2096 * @since 3.0.0 2097 * @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`, 2098 * and `use_featured_image` labels. 2099 * @since 4.4.0 Added the `archives`, `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`, 2100 * `items_list_navigation`, and `items_list` labels. 2101 * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. 2102 * @since 4.7.0 Added the `view_items` and `attributes` labels. 2103 * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, 2104 * `item_scheduled`, and `item_updated` labels. 2105 * @since 5.7.0 Added the `filter_by_date` label. 2106 * @since 5.8.0 Added the `item_link` and `item_link_description` labels. 2107 * @since 6.3.0 Added the `item_trashed` label. 2108 * @since 6.4.0 Changed default values for the `add_new` label to include the type of content. 2109 * This matches `add_new_item` and provides more context for better accessibility. 2110 * @since 6.6.0 Added the `template_name` label. 2111 * @since 6.7.0 Restored pre-6.4.0 defaults for the `add_new` label and updated documentation. 2112 * Updated core usage to reference `add_new_item`. 2113 * 2114 * @access private 2115 * 2116 * @param object|WP_Post_Type $post_type_object Post type object. 2117 * @return object Object with all the labels as member variables. 2118 */ 2119 function get_post_type_labels( $post_type_object ) { 2120 $nohier_vs_hier_defaults = WP_Post_Type::get_default_labels(); 2121 2122 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; 2123 2124 $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); 2125 2126 if ( ! isset( $post_type_object->labels->template_name ) && isset( $post_type_object->labels->singular_name ) ) { 2127 /* translators: %s: Post type name. */ 2128 $labels->template_name = sprintf( __( 'Single item: %s' ), $post_type_object->labels->singular_name ); 2129 } 2130 2131 $post_type = $post_type_object->name; 2132 2133 $default_labels = clone $labels; 2134 2135 /** 2136 * Filters the labels of a specific post type. 2137 * 2138 * The dynamic portion of the hook name, `$post_type`, refers to 2139 * the post type slug. 2140 * 2141 * Possible hook names include: 2142 * 2143 * - `post_type_labels_post` 2144 * - `post_type_labels_page` 2145 * - `post_type_labels_attachment` 2146 * 2147 * @since 3.5.0 2148 * 2149 * @see get_post_type_labels() for the full list of labels. 2150 * 2151 * @param object $labels Object with labels for the post type as member variables. 2152 */ 2153 $labels = apply_filters( "post_type_labels_{$post_type}", $labels ); 2154 2155 // Ensure that the filtered labels contain all required default values. 2156 $labels = (object) array_merge( (array) $default_labels, (array) $labels ); 2157 2158 return $labels; 2159 } 2160 2161 /** 2162 * Builds an object with custom-something object (post type, taxonomy) labels 2163 * out of a custom-something object 2164 * 2165 * @since 3.0.0 2166 * @access private 2167 * 2168 * @param object $data_object A custom-something object. 2169 * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. 2170 * @return object Object containing labels for the given custom-something object. 2171 */ 2172 function _get_custom_object_labels( $data_object, $nohier_vs_hier_defaults ) { 2173 $data_object->labels = (array) $data_object->labels; 2174 2175 if ( isset( $data_object->label ) && empty( $data_object->labels['name'] ) ) { 2176 $data_object->labels['name'] = $data_object->label; 2177 } 2178 2179 if ( ! isset( $data_object->labels['singular_name'] ) && isset( $data_object->labels['name'] ) ) { 2180 $data_object->labels['singular_name'] = $data_object->labels['name']; 2181 } 2182 2183 if ( ! isset( $data_object->labels['name_admin_bar'] ) ) { 2184 $data_object->labels['name_admin_bar'] = 2185 isset( $data_object->labels['singular_name'] ) 2186 ? $data_object->labels['singular_name'] 2187 : $data_object->name; 2188 } 2189 2190 if ( ! isset( $data_object->labels['menu_name'] ) && isset( $data_object->labels['name'] ) ) { 2191 $data_object->labels['menu_name'] = $data_object->labels['name']; 2192 } 2193 2194 if ( ! isset( $data_object->labels['all_items'] ) && isset( $data_object->labels['menu_name'] ) ) { 2195 $data_object->labels['all_items'] = $data_object->labels['menu_name']; 2196 } 2197 2198 if ( ! isset( $data_object->labels['archives'] ) && isset( $data_object->labels['all_items'] ) ) { 2199 $data_object->labels['archives'] = $data_object->labels['all_items']; 2200 } 2201 2202 $defaults = array(); 2203 foreach ( $nohier_vs_hier_defaults as $key => $value ) { 2204 $defaults[ $key ] = $data_object->hierarchical ? $value[1] : $value[0]; 2205 } 2206 2207 $labels = array_merge( $defaults, $data_object->labels ); 2208 $data_object->labels = (object) $data_object->labels; 2209 2210 return (object) $labels; 2211 } 2212 2213 /** 2214 * Adds submenus for post types. 2215 * 2216 * @access private 2217 * @since 3.1.0 2218 */ 2219 function _add_post_type_submenus() { 2220 foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { 2221 $ptype_obj = get_post_type_object( $ptype ); 2222 // Sub-menus only. 2223 if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) { 2224 continue; 2225 } 2226 add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); 2227 } 2228 } 2229 2230 /** 2231 * Registers support of certain features for a post type. 2232 * 2233 * All core features are directly associated with a functional area of the edit 2234 * screen, such as the editor or a meta box. Features include: 'title', 'editor', 2235 * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 2236 * 'thumbnail', 'custom-fields', and 'post-formats'. 2237 * 2238 * Additionally, the 'revisions' feature dictates whether the post type will 2239 * store revisions, the 'autosave' feature dictates whether the post type 2240 * will be autosaved, and the 'comments' feature dictates whether the comments 2241 * count will show on the edit screen. 2242 * 2243 * A third, optional parameter can also be passed along with a feature to provide 2244 * additional information about supporting that feature. 2245 * 2246 * Example usage: 2247 * 2248 * add_post_type_support( 'my_post_type', 'comments' ); 2249 * add_post_type_support( 'my_post_type', array( 2250 * 'author', 'excerpt', 2251 * ) ); 2252 * add_post_type_support( 'my_post_type', 'my_feature', array( 2253 * 'field' => 'value', 2254 * ) ); 2255 * 2256 * @since 3.0.0 2257 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 2258 * by adding it to the function signature. 2259 * 2260 * @global array $_wp_post_type_features 2261 * 2262 * @param string $post_type The post type for which to add the feature. 2263 * @param string|array $feature The feature being added, accepts an array of 2264 * feature strings or a single string. 2265 * @param mixed ...$args Optional extra arguments to pass along with certain features. 2266 */ 2267 function add_post_type_support( $post_type, $feature, ...$args ) { 2268 global $_wp_post_type_features; 2269 2270 $features = (array) $feature; 2271 foreach ( $features as $feature ) { 2272 if ( $args ) { 2273 $_wp_post_type_features[ $post_type ][ $feature ] = $args; 2274 } else { 2275 $_wp_post_type_features[ $post_type ][ $feature ] = true; 2276 } 2277 } 2278 } 2279 2280 /** 2281 * Removes support for a feature from a post type. 2282 * 2283 * @since 3.0.0 2284 * 2285 * @global array $_wp_post_type_features 2286 * 2287 * @param string $post_type The post type for which to remove the feature. 2288 * @param string $feature The feature being removed. 2289 */ 2290 function remove_post_type_support( $post_type, $feature ) { 2291 global $_wp_post_type_features; 2292 2293 unset( $_wp_post_type_features[ $post_type ][ $feature ] ); 2294 } 2295 2296 /** 2297 * Gets all the post type features 2298 * 2299 * @since 3.4.0 2300 * 2301 * @global array $_wp_post_type_features 2302 * 2303 * @param string $post_type The post type. 2304 * @return array Post type supports list. 2305 */ 2306 function get_all_post_type_supports( $post_type ) { 2307 global $_wp_post_type_features; 2308 2309 if ( isset( $_wp_post_type_features[ $post_type ] ) ) { 2310 return $_wp_post_type_features[ $post_type ]; 2311 } 2312 2313 return array(); 2314 } 2315 2316 /** 2317 * Checks a post type's support for a given feature. 2318 * 2319 * @since 3.0.0 2320 * 2321 * @global array $_wp_post_type_features 2322 * 2323 * @param string $post_type The post type being checked. 2324 * @param string $feature The feature being checked. 2325 * @return bool Whether the post type supports the given feature. 2326 */ 2327 function post_type_supports( $post_type, $feature ) { 2328 global $_wp_post_type_features; 2329 2330 return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) ); 2331 } 2332 2333 /** 2334 * Retrieves a list of post type names that support a specific feature. 2335 * 2336 * @since 4.5.0 2337 * 2338 * @global array $_wp_post_type_features Post type features 2339 * 2340 * @param array|string $feature Single feature or an array of features the post types should support. 2341 * @param string $operator Optional. The logical operation to perform. 'or' means 2342 * only one element from the array needs to match; 'and' 2343 * means all elements must match; 'not' means no elements may 2344 * match. Default 'and'. 2345 * @return string[] A list of post type names. 2346 */ 2347 function get_post_types_by_support( $feature, $operator = 'and' ) { 2348 global $_wp_post_type_features; 2349 2350 $features = array_fill_keys( (array) $feature, true ); 2351 2352 return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); 2353 } 2354 2355 /** 2356 * Updates the post type for the post ID. 2357 * 2358 * The page or post cache will be cleaned for the post ID. 2359 * 2360 * @since 2.5.0 2361 * 2362 * @global wpdb $wpdb WordPress database abstraction object. 2363 * 2364 * @param int $post_id Optional. Post ID to change post type. Default 0. 2365 * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to 2366 * name a few. Default 'post'. 2367 * @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. 2368 */ 2369 function set_post_type( $post_id = 0, $post_type = 'post' ) { 2370 global $wpdb; 2371 2372 $post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' ); 2373 $return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) ); 2374 2375 clean_post_cache( $post_id ); 2376 2377 return $return; 2378 } 2379 2380 /** 2381 * Determines whether a post type is considered "viewable". 2382 * 2383 * For built-in post types such as posts and pages, the 'public' value will be evaluated. 2384 * For all others, the 'publicly_queryable' value will be used. 2385 * 2386 * @since 4.4.0 2387 * @since 4.5.0 Added the ability to pass a post type name in addition to object. 2388 * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. 2389 * @since 5.9.0 Added `is_post_type_viewable` hook to filter the result. 2390 * 2391 * @param string|WP_Post_Type $post_type Post type name or object. 2392 * @return bool Whether the post type should be considered viewable. 2393 */ 2394 function is_post_type_viewable( $post_type ) { 2395 if ( is_scalar( $post_type ) ) { 2396 $post_type = get_post_type_object( $post_type ); 2397 2398 if ( ! $post_type ) { 2399 return false; 2400 } 2401 } 2402 2403 if ( ! is_object( $post_type ) ) { 2404 return false; 2405 } 2406 2407 $is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); 2408 2409 /** 2410 * Filters whether a post type is considered "viewable". 2411 * 2412 * The returned filtered value must be a boolean type to ensure 2413 * `is_post_type_viewable()` only returns a boolean. This strictness 2414 * is by design to maintain backwards-compatibility and guard against 2415 * potential type errors in PHP 8.1+. Non-boolean values (even falsey 2416 * and truthy values) will result in the function returning false. 2417 * 2418 * @since 5.9.0 2419 * 2420 * @param bool $is_viewable Whether the post type is "viewable" (strict type). 2421 * @param WP_Post_Type $post_type Post type object. 2422 */ 2423 return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type ); 2424 } 2425 2426 /** 2427 * Determines whether a post status is considered "viewable". 2428 * 2429 * For built-in post statuses such as publish and private, the 'public' value will be evaluated. 2430 * For all others, the 'publicly_queryable' value will be used. 2431 * 2432 * @since 5.7.0 2433 * @since 5.9.0 Added `is_post_status_viewable` hook to filter the result. 2434 * 2435 * @param string|stdClass $post_status Post status name or object. 2436 * @return bool Whether the post status should be considered viewable. 2437 */ 2438 function is_post_status_viewable( $post_status ) { 2439 if ( is_scalar( $post_status ) ) { 2440 $post_status = get_post_status_object( $post_status ); 2441 2442 if ( ! $post_status ) { 2443 return false; 2444 } 2445 } 2446 2447 if ( 2448 ! is_object( $post_status ) || 2449 $post_status->internal || 2450 $post_status->protected 2451 ) { 2452 return false; 2453 } 2454 2455 $is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public ); 2456 2457 /** 2458 * Filters whether a post status is considered "viewable". 2459 * 2460 * The returned filtered value must be a boolean type to ensure 2461 * `is_post_status_viewable()` only returns a boolean. This strictness 2462 * is by design to maintain backwards-compatibility and guard against 2463 * potential type errors in PHP 8.1+. Non-boolean values (even falsey 2464 * and truthy values) will result in the function returning false. 2465 * 2466 * @since 5.9.0 2467 * 2468 * @param bool $is_viewable Whether the post status is "viewable" (strict type). 2469 * @param stdClass $post_status Post status object. 2470 */ 2471 return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status ); 2472 } 2473 2474 /** 2475 * Determines whether a post is publicly viewable. 2476 * 2477 * Posts are considered publicly viewable if both the post status and post type 2478 * are viewable. 2479 * 2480 * @since 5.7.0 2481 * 2482 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 2483 * @return bool Whether the post is publicly viewable. 2484 */ 2485 function is_post_publicly_viewable( $post = null ) { 2486 $post = get_post( $post ); 2487 2488 if ( ! $post ) { 2489 return false; 2490 } 2491 2492 $post_type = get_post_type( $post ); 2493 $post_status = get_post_status( $post ); 2494 2495 return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status ); 2496 } 2497 2498 /** 2499 * Determines whether a post is embeddable. 2500 * 2501 * @since 6.8.0 2502 * 2503 * @param int|WP_Post|null $post Optional. Post ID or `WP_Post` object. Defaults to global $post. 2504 * @return bool Whether the post should be considered embeddable. 2505 */ 2506 function is_post_embeddable( $post = null ) { 2507 $post = get_post( $post ); 2508 2509 if ( ! $post ) { 2510 return false; 2511 } 2512 2513 $post_type = get_post_type_object( $post->post_type ); 2514 2515 if ( ! $post_type ) { 2516 return false; 2517 } 2518 2519 $is_embeddable = $post_type->embeddable; 2520 2521 /** 2522 * Filter whether a post is embeddable. 2523 * 2524 * @since 6.8.0 2525 * 2526 * @param bool $is_embeddable Whether the post is embeddable. 2527 * @param WP_Post $post Post object. 2528 */ 2529 return apply_filters( 'is_post_embeddable', $is_embeddable, $post ); 2530 } 2531 2532 /** 2533 * Retrieves an array of the latest posts, or posts matching the given criteria. 2534 * 2535 * For more information on the accepted arguments, see the 2536 * {@link https://developer.wordpress.org/reference/classes/wp_query/ 2537 * WP_Query} documentation in the Developer Handbook. 2538 * 2539 * The `$ignore_sticky_posts` and `$no_found_rows` arguments are ignored by 2540 * this function and both are set to `true`. 2541 * 2542 * The defaults are as follows: 2543 * 2544 * @since 1.2.0 2545 * 2546 * @see WP_Query 2547 * @see WP_Query::parse_query() 2548 * 2549 * @param array $args { 2550 * Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments. 2551 * 2552 * @type int $numberposts Total number of posts to retrieve. Is an alias of `$posts_per_page` 2553 * in WP_Query. Accepts -1 for all. Default 5. 2554 * @type int|string $category Category ID or comma-separated list of IDs (this or any children). 2555 * Is an alias of `$cat` in WP_Query. Default 0. 2556 * @type int[] $include An array of post IDs to retrieve, sticky posts will be included. 2557 * Is an alias of `$post__in` in WP_Query. Default empty array. 2558 * @type int[] $exclude An array of post IDs not to retrieve. Default empty array. 2559 * @type bool $suppress_filters Whether to suppress filters. Default true. 2560 * } 2561 * @return WP_Post[]|int[] Array of post objects or post IDs. 2562 */ 2563 function get_posts( $args = null ) { 2564 $defaults = array( 2565 'numberposts' => 5, 2566 'category' => 0, 2567 'orderby' => 'date', 2568 'order' => 'DESC', 2569 'include' => array(), 2570 'exclude' => array(), 2571 'meta_key' => '', 2572 'meta_value' => '', 2573 'post_type' => 'post', 2574 'suppress_filters' => true, 2575 ); 2576 2577 $parsed_args = wp_parse_args( $args, $defaults ); 2578 if ( empty( $parsed_args['post_status'] ) ) { 2579 $parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish'; 2580 } 2581 if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) { 2582 $parsed_args['posts_per_page'] = $parsed_args['numberposts']; 2583 } 2584 if ( ! empty( $parsed_args['category'] ) ) { 2585 $parsed_args['cat'] = $parsed_args['category']; 2586 } 2587 if ( ! empty( $parsed_args['include'] ) ) { 2588 $incposts = wp_parse_id_list( $parsed_args['include'] ); 2589 $parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included. 2590 $parsed_args['post__in'] = $incposts; 2591 } elseif ( ! empty( $parsed_args['exclude'] ) ) { 2592 $parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] ); 2593 } 2594 2595 $parsed_args['ignore_sticky_posts'] = true; 2596 $parsed_args['no_found_rows'] = true; 2597 2598 $get_posts = new WP_Query(); 2599 return $get_posts->query( $parsed_args ); 2600 } 2601 2602 // 2603 // Post meta functions. 2604 // 2605 2606 /** 2607 * Adds a meta field to the given post. 2608 * 2609 * Post meta data is called "Custom Fields" on the Administration Screen. 2610 * 2611 * @since 1.5.0 2612 * 2613 * @param int $post_id Post ID. 2614 * @param string $meta_key Metadata name. 2615 * @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and 2616 * will be returned as the same type when retrieved. Other data types will 2617 * be stored as strings in the database: 2618 * - false is stored and retrieved as an empty string ('') 2619 * - true is stored and retrieved as '1' 2620 * - numbers (both integer and float) are stored and retrieved as strings 2621 * Must be serializable if non-scalar. 2622 * @param bool $unique Optional. Whether the same key should not be added. 2623 * Default false. 2624 * @return int|false Meta ID on success, false on failure. 2625 */ 2626 function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { 2627 // Make sure meta is added to the post, not a revision. 2628 $the_post = wp_is_post_revision( $post_id ); 2629 if ( $the_post ) { 2630 $post_id = $the_post; 2631 } 2632 2633 return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); 2634 } 2635 2636 /** 2637 * Deletes a post meta field for the given post ID. 2638 * 2639 * You can match based on the key, or key and value. Removing based on key and 2640 * value, will keep from removing duplicate metadata with the same key. It also 2641 * allows removing all metadata matching the key, if needed. 2642 * 2643 * @since 1.5.0 2644 * 2645 * @param int $post_id Post ID. 2646 * @param string $meta_key Metadata name. 2647 * @param mixed $meta_value Optional. Metadata value. If provided, 2648 * rows will only be removed that match the value. 2649 * Must be serializable if non-scalar. Default empty. 2650 * @return bool True on success, false on failure. 2651 */ 2652 function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { 2653 // Make sure meta is deleted from the post, not from a revision. 2654 $the_post = wp_is_post_revision( $post_id ); 2655 if ( $the_post ) { 2656 $post_id = $the_post; 2657 } 2658 2659 return delete_metadata( 'post', $post_id, $meta_key, $meta_value ); 2660 } 2661 2662 /** 2663 * Retrieves a post meta field for the given post ID. 2664 * 2665 * @since 1.5.0 2666 * 2667 * @param int $post_id Post ID. 2668 * @param string $key Optional. The meta key to retrieve. By default, 2669 * returns data for all keys. Default empty. 2670 * @param bool $single Optional. Whether to return a single value. 2671 * This parameter has no effect if `$key` is not specified. 2672 * Default false. 2673 * @return mixed An array of values if `$single` is false. 2674 * The value of the meta field if `$single` is true. 2675 * False for an invalid `$post_id` (non-numeric, zero, or negative value). 2676 * An empty array if a valid but non-existing post ID is passed and `$single` is false. 2677 * An empty string if a valid but non-existing post ID is passed and `$single` is true. 2678 * Note: Non-serialized values are returned as strings: 2679 * - false values are returned as empty strings ('') 2680 * - true values are returned as '1' 2681 * - numbers (both integer and float) are returned as strings 2682 * Arrays and objects retain their original type. 2683 */ 2684 function get_post_meta( $post_id, $key = '', $single = false ) { 2685 return get_metadata( 'post', $post_id, $key, $single ); 2686 } 2687 2688 /** 2689 * Updates a post meta field based on the given post ID. 2690 * 2691 * Use the `$prev_value` parameter to differentiate between meta fields with the 2692 * same key and post ID. 2693 * 2694 * If the meta field for the post does not exist, it will be added and its ID returned. 2695 * 2696 * Can be used in place of add_post_meta(). 2697 * 2698 * @since 1.5.0 2699 * 2700 * @param int $post_id Post ID. 2701 * @param string $meta_key Metadata key. 2702 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 2703 * @param mixed $prev_value Optional. Previous value to check before updating. 2704 * If specified, only update existing metadata entries with 2705 * this value. Otherwise, update all entries. Default empty. 2706 * @return int|bool Meta ID if the key didn't exist, true on successful update, 2707 * false on failure or if the value passed to the function 2708 * is the same as the one that is already in the database. 2709 */ 2710 function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { 2711 // Make sure meta is updated for the post, not for a revision. 2712 $the_post = wp_is_post_revision( $post_id ); 2713 if ( $the_post ) { 2714 $post_id = $the_post; 2715 } 2716 2717 return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); 2718 } 2719 2720 /** 2721 * Deletes everything from post meta matching the given meta key. 2722 * 2723 * @since 2.3.0 2724 * 2725 * @param string $post_meta_key Key to search for when deleting. 2726 * @return bool Whether the post meta key was deleted from the database. 2727 */ 2728 function delete_post_meta_by_key( $post_meta_key ) { 2729 return delete_metadata( 'post', null, $post_meta_key, '', true ); 2730 } 2731 2732 /** 2733 * Registers a meta key for posts. 2734 * 2735 * @since 4.9.8 2736 * 2737 * @param string $post_type Post type to register a meta key for. Pass an empty string 2738 * to register the meta key across all existing post types. 2739 * @param string $meta_key The meta key to register. 2740 * @param array $args Data used to describe the meta key when registered. See 2741 * {@see register_meta()} for a list of supported arguments. 2742 * @return bool True if the meta key was successfully registered, false if not. 2743 */ 2744 function register_post_meta( $post_type, $meta_key, array $args ) { 2745 $args['object_subtype'] = $post_type; 2746 2747 return register_meta( 'post', $meta_key, $args ); 2748 } 2749 2750 /** 2751 * Unregisters a meta key for posts. 2752 * 2753 * @since 4.9.8 2754 * 2755 * @param string $post_type Post type the meta key is currently registered for. Pass 2756 * an empty string if the meta key is registered across all 2757 * existing post types. 2758 * @param string $meta_key The meta key to unregister. 2759 * @return bool True on success, false if the meta key was not previously registered. 2760 */ 2761 function unregister_post_meta( $post_type, $meta_key ) { 2762 return unregister_meta_key( 'post', $meta_key, $post_type ); 2763 } 2764 2765 /** 2766 * Retrieves post meta fields, based on post ID. 2767 * 2768 * The post meta fields are retrieved from the cache where possible, 2769 * so the function is optimized to be called more than once. 2770 * 2771 * @since 1.2.0 2772 * 2773 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 2774 * @return mixed An array of values. 2775 * False for an invalid `$post_id` (non-numeric, zero, or negative value). 2776 * An empty string if a valid but non-existing post ID is passed. 2777 */ 2778 function get_post_custom( $post_id = 0 ) { 2779 $post_id = absint( $post_id ); 2780 2781 if ( ! $post_id ) { 2782 $post_id = get_the_ID(); 2783 } 2784 2785 return get_post_meta( $post_id ); 2786 } 2787 2788 /** 2789 * Retrieves meta field names for a post. 2790 * 2791 * If there are no meta fields, then nothing (null) will be returned. 2792 * 2793 * @since 1.2.0 2794 * 2795 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 2796 * @return array|void Array of the keys, if retrieved. 2797 */ 2798 function get_post_custom_keys( $post_id = 0 ) { 2799 $custom = get_post_custom( $post_id ); 2800 2801 if ( ! is_array( $custom ) ) { 2802 return; 2803 } 2804 2805 $keys = array_keys( $custom ); 2806 if ( $keys ) { 2807 return $keys; 2808 } 2809 } 2810 2811 /** 2812 * Retrieves values for a custom post field. 2813 * 2814 * The parameters must not be considered optional. All of the post meta fields 2815 * will be retrieved and only the meta field key values returned. 2816 * 2817 * @since 1.2.0 2818 * 2819 * @param string $key Optional. Meta field key. Default empty. 2820 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 2821 * @return array|null Meta field values. 2822 */ 2823 function get_post_custom_values( $key = '', $post_id = 0 ) { 2824 if ( ! $key ) { 2825 return null; 2826 } 2827 2828 $custom = get_post_custom( $post_id ); 2829 2830 return isset( $custom[ $key ] ) ? $custom[ $key ] : null; 2831 } 2832 2833 /** 2834 * Determines whether a post is sticky. 2835 * 2836 * Sticky posts should remain at the top of The Loop. If the post ID is not 2837 * given, then The Loop ID for the current post will be used. 2838 * 2839 * For more information on this and similar theme functions, check out 2840 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 2841 * Conditional Tags} article in the Theme Developer Handbook. 2842 * 2843 * @since 2.7.0 2844 * 2845 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 2846 * @return bool Whether post is sticky. 2847 */ 2848 function is_sticky( $post_id = 0 ) { 2849 $post_id = absint( $post_id ); 2850 2851 if ( ! $post_id ) { 2852 $post_id = get_the_ID(); 2853 } 2854 2855 $stickies = get_option( 'sticky_posts' ); 2856 2857 if ( is_array( $stickies ) ) { 2858 $stickies = array_map( 'intval', $stickies ); 2859 $is_sticky = in_array( $post_id, $stickies, true ); 2860 } else { 2861 $is_sticky = false; 2862 } 2863 2864 /** 2865 * Filters whether a post is sticky. 2866 * 2867 * @since 5.3.0 2868 * 2869 * @param bool $is_sticky Whether a post is sticky. 2870 * @param int $post_id Post ID. 2871 */ 2872 return apply_filters( 'is_sticky', $is_sticky, $post_id ); 2873 } 2874 2875 /** 2876 * Sanitizes every post field. 2877 * 2878 * If the context is 'raw', then the post object or array will get minimal 2879 * sanitization of the integer fields. 2880 * 2881 * @since 2.3.0 2882 * 2883 * @see sanitize_post_field() 2884 * 2885 * @param object|WP_Post|array $post The post object or array 2886 * @param string $context Optional. How to sanitize post fields. 2887 * Accepts 'raw', 'edit', 'db', 'display', 2888 * 'attribute', or 'js'. Default 'display'. 2889 * @return object|WP_Post|array The now sanitized post object or array (will be the 2890 * same type as `$post`). 2891 */ 2892 function sanitize_post( $post, $context = 'display' ) { 2893 if ( is_object( $post ) ) { 2894 // Check if post already filtered for this context. 2895 if ( isset( $post->filter ) && $context === $post->filter ) { 2896 return $post; 2897 } 2898 if ( ! isset( $post->ID ) ) { 2899 $post->ID = 0; 2900 } 2901 foreach ( array_keys( get_object_vars( $post ) ) as $field ) { 2902 $post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context ); 2903 } 2904 $post->filter = $context; 2905 } elseif ( is_array( $post ) ) { 2906 // Check if post already filtered for this context. 2907 if ( isset( $post['filter'] ) && $context === $post['filter'] ) { 2908 return $post; 2909 } 2910 if ( ! isset( $post['ID'] ) ) { 2911 $post['ID'] = 0; 2912 } 2913 foreach ( array_keys( $post ) as $field ) { 2914 $post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context ); 2915 } 2916 $post['filter'] = $context; 2917 } 2918 return $post; 2919 } 2920 2921 /** 2922 * Sanitizes a post field based on context. 2923 * 2924 * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 2925 * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts 2926 * are treated like 'display' when calling filters. 2927 * 2928 * @since 2.3.0 2929 * @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. 2930 * 2931 * @param string $field The Post Object field name. 2932 * @param mixed $value The Post Object value. 2933 * @param int $post_id Post ID. 2934 * @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit', 2935 * 'db', 'display', 'attribute' and 'js'. Default 'display'. 2936 * @return mixed Sanitized value. 2937 */ 2938 function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { 2939 $int_fields = array( 'ID', 'post_parent', 'menu_order' ); 2940 if ( in_array( $field, $int_fields, true ) ) { 2941 $value = (int) $value; 2942 } 2943 2944 // Fields which contain arrays of integers. 2945 $array_int_fields = array( 'ancestors' ); 2946 if ( in_array( $field, $array_int_fields, true ) ) { 2947 $value = array_map( 'absint', $value ); 2948 return $value; 2949 } 2950 2951 if ( 'raw' === $context ) { 2952 return $value; 2953 } 2954 2955 $prefixed = false; 2956 if ( str_contains( $field, 'post_' ) ) { 2957 $prefixed = true; 2958 $field_no_prefix = str_replace( 'post_', '', $field ); 2959 } 2960 2961 if ( 'edit' === $context ) { 2962 $format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' ); 2963 2964 if ( $prefixed ) { 2965 2966 /** 2967 * Filters the value of a specific post field to edit. 2968 * 2969 * The dynamic portion of the hook name, `$field`, refers to the post 2970 * field name. Possible filter names include: 2971 * 2972 * - `edit_post_author` 2973 * - `edit_post_date` 2974 * - `edit_post_date_gmt` 2975 * - `edit_post_content` 2976 * - `edit_post_title` 2977 * - `edit_post_excerpt` 2978 * - `edit_post_status` 2979 * - `edit_post_password` 2980 * - `edit_post_name` 2981 * - `edit_post_modified` 2982 * - `edit_post_modified_gmt` 2983 * - `edit_post_content_filtered` 2984 * - `edit_post_parent` 2985 * - `edit_post_type` 2986 * - `edit_post_mime_type` 2987 * 2988 * @since 2.3.0 2989 * 2990 * @param mixed $value Value of the post field. 2991 * @param int $post_id Post ID. 2992 */ 2993 $value = apply_filters( "edit_{$field}", $value, $post_id ); 2994 2995 /** 2996 * Filters the value of a specific post field to edit. 2997 * 2998 * Only applied to post fields with a name which is prefixed with `post_`. 2999 * 3000 * The dynamic portion of the hook name, `$field_no_prefix`, refers to the 3001 * post field name minus the `post_` prefix. Possible filter names include: 3002 * 3003 * - `author_edit_pre` 3004 * - `date_edit_pre` 3005 * - `date_gmt_edit_pre` 3006 * - `content_edit_pre` 3007 * - `title_edit_pre` 3008 * - `excerpt_edit_pre` 3009 * - `status_edit_pre` 3010 * - `password_edit_pre` 3011 * - `name_edit_pre` 3012 * - `modified_edit_pre` 3013 * - `modified_gmt_edit_pre` 3014 * - `content_filtered_edit_pre` 3015 * - `parent_edit_pre` 3016 * - `type_edit_pre` 3017 * - `mime_type_edit_pre` 3018 * 3019 * @since 2.3.0 3020 * 3021 * @param mixed $value Value of the post field. 3022 * @param int $post_id Post ID. 3023 */ 3024 $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); 3025 } else { 3026 /** 3027 * Filters the value of a specific post field to edit. 3028 * 3029 * Only applied to post fields not prefixed with `post_`. 3030 * 3031 * The dynamic portion of the hook name, `$field`, refers to the 3032 * post field name. Possible filter names include: 3033 * 3034 * - `edit_post_ID` 3035 * - `edit_post_ping_status` 3036 * - `edit_post_pinged` 3037 * - `edit_post_to_ping` 3038 * - `edit_post_comment_count` 3039 * - `edit_post_comment_status` 3040 * - `edit_post_guid` 3041 * - `edit_post_menu_order` 3042 * 3043 * @since 2.3.0 3044 * 3045 * @param mixed $value Value of the post field. 3046 * @param int $post_id Post ID. 3047 */ 3048 $value = apply_filters( "edit_post_{$field}", $value, $post_id ); 3049 } 3050 3051 if ( in_array( $field, $format_to_edit, true ) ) { 3052 if ( 'post_content' === $field ) { 3053 $value = format_to_edit( $value, user_can_richedit() ); 3054 } else { 3055 $value = format_to_edit( $value ); 3056 } 3057 } else { 3058 $value = esc_attr( $value ); 3059 } 3060 } elseif ( 'db' === $context ) { 3061 if ( $prefixed ) { 3062 3063 /** 3064 * Filters the value of a specific post field before saving. 3065 * 3066 * Only applied to post fields with a name which is prefixed with `post_`. 3067 * 3068 * The dynamic portion of the hook name, `$field`, refers to the post 3069 * field name. Possible filter names include: 3070 * 3071 * - `pre_post_author` 3072 * - `pre_post_date` 3073 * - `pre_post_date_gmt` 3074 * - `pre_post_content` 3075 * - `pre_post_title` 3076 * - `pre_post_excerpt` 3077 * - `pre_post_status` 3078 * - `pre_post_password` 3079 * - `pre_post_name` 3080 * - `pre_post_modified` 3081 * - `pre_post_modified_gmt` 3082 * - `pre_post_content_filtered` 3083 * - `pre_post_parent` 3084 * - `pre_post_type` 3085 * - `pre_post_mime_type` 3086 * 3087 * @since 2.3.0 3088 * 3089 * @param mixed $value Value of the post field. 3090 */ 3091 $value = apply_filters( "pre_{$field}", $value ); 3092 3093 /** 3094 * Filters the value of a specific field before saving. 3095 * 3096 * Only applied to post fields with a name which is prefixed with `post_`. 3097 * 3098 * The dynamic portion of the hook name, `$field_no_prefix`, refers to the 3099 * post field name minus the `post_` prefix. Possible filter names include: 3100 * 3101 * - `author_save_pre` 3102 * - `date_save_pre` 3103 * - `date_gmt_save_pre` 3104 * - `content_save_pre` 3105 * - `title_save_pre` 3106 * - `excerpt_save_pre` 3107 * - `status_save_pre` 3108 * - `password_save_pre` 3109 * - `name_save_pre` 3110 * - `modified_save_pre` 3111 * - `modified_gmt_save_pre` 3112 * - `content_filtered_save_pre` 3113 * - `parent_save_pre` 3114 * - `type_save_pre` 3115 * - `mime_type_save_pre` 3116 * 3117 * @since 2.3.0 3118 * 3119 * @param mixed $value Value of the post field. 3120 */ 3121 $value = apply_filters( "{$field_no_prefix}_save_pre", $value ); 3122 } else { 3123 /** 3124 * Filters the value of a specific field before saving. 3125 * 3126 * Only applied to post fields with a name which is prefixed with `post_`. 3127 * 3128 * The dynamic portion of the hook name, `$field_no_prefix`, refers to the 3129 * post field name minus the `post_` prefix. Possible filter names include: 3130 * 3131 * - `pre_post_ID` 3132 * - `pre_post_comment_status` 3133 * - `pre_post_ping_status` 3134 * - `pre_post_to_ping` 3135 * - `pre_post_pinged` 3136 * - `pre_post_guid` 3137 * - `pre_post_menu_order` 3138 * - `pre_post_comment_count` 3139 * 3140 * @since 2.3.0 3141 * 3142 * @param mixed $value Value of the post field. 3143 */ 3144 $value = apply_filters( "pre_post_{$field}", $value ); 3145 3146 /** 3147 * Filters the value of a specific post field before saving. 3148 * 3149 * Only applied to post fields with a name which is *not* prefixed with `post_`. 3150 * 3151 * The dynamic portion of the hook name, `$field`, refers to the post 3152 * field name. Possible filter names include: 3153 * 3154 * - `ID_pre` 3155 * - `comment_status_pre` 3156 * - `ping_status_pre` 3157 * - `to_ping_pre` 3158 * - `pinged_pre` 3159 * - `guid_pre` 3160 * - `menu_order_pre` 3161 * - `comment_count_pre` 3162 * 3163 * @since 2.3.0 3164 * 3165 * @param mixed $value Value of the post field. 3166 */ 3167 $value = apply_filters( "{$field}_pre", $value ); 3168 } 3169 } else { 3170 3171 // Use display filters by default. 3172 if ( $prefixed ) { 3173 3174 /** 3175 * Filters the value of a specific post field for display. 3176 * 3177 * Only applied to post fields with a name which is prefixed with `post_`. 3178 * 3179 * The dynamic portion of the hook name, `$field`, refers to the post 3180 * field name. Possible filter names include: 3181 * 3182 * - `post_author` 3183 * - `post_date` 3184 * - `post_date_gmt` 3185 * - `post_content` 3186 * - `post_title` 3187 * - `post_excerpt` 3188 * - `post_status` 3189 * - `post_password` 3190 * - `post_name` 3191 * - `post_modified` 3192 * - `post_modified_gmt` 3193 * - `post_content_filtered` 3194 * - `post_parent` 3195 * - `post_type` 3196 * - `post_mime_type` 3197 * 3198 * @since 2.3.0 3199 * 3200 * @param mixed $value Value of the prefixed post field. 3201 * @param int $post_id Post ID. 3202 * @param string $context Context for how to sanitize the field. 3203 * Accepts 'raw', 'edit', 'db', 'display', 3204 * 'attribute', or 'js'. Default 'display'. 3205 */ 3206 $value = apply_filters( "{$field}", $value, $post_id, $context ); 3207 } else { 3208 /** 3209 * Filters the value of a specific post field for display. 3210 * 3211 * Only applied to post fields name which is *not* prefixed with `post_`. 3212 * 3213 * The dynamic portion of the hook name, `$field`, refers to the post 3214 * field name. Possible filter names include: 3215 * 3216 * - `post_ID` 3217 * - `post_comment_status` 3218 * - `post_ping_status` 3219 * - `post_to_ping` 3220 * - `post_pinged` 3221 * - `post_guid` 3222 * - `post_menu_order` 3223 * - `post_comment_count` 3224 * 3225 * @since 2.3.0 3226 * 3227 * @param mixed $value Value of the unprefixed post field. 3228 * @param int $post_id Post ID 3229 * @param string $context Context for how to sanitize the field. 3230 * Accepts 'raw', 'edit', 'db', 'display', 3231 * 'attribute', or 'js'. Default 'display'. 3232 */ 3233 $value = apply_filters( "post_{$field}", $value, $post_id, $context ); 3234 } 3235 3236 if ( 'attribute' === $context ) { 3237 $value = esc_attr( $value ); 3238 } elseif ( 'js' === $context ) { 3239 $value = esc_js( $value ); 3240 } 3241 } 3242 3243 // Restore the type for integer fields after esc_attr(). 3244 if ( in_array( $field, $int_fields, true ) ) { 3245 $value = (int) $value; 3246 } 3247 return $value; 3248 } 3249 3250 /** 3251 * Makes a post sticky. 3252 * 3253 * Sticky posts should be displayed at the top of the front page. 3254 * 3255 * @since 2.7.0 3256 * 3257 * @param int $post_id Post ID. 3258 */ 3259 function stick_post( $post_id ) { 3260 $post_id = (int) $post_id; 3261 $stickies = get_option( 'sticky_posts' ); 3262 $updated = false; 3263 3264 if ( ! is_array( $stickies ) ) { 3265 $stickies = array(); 3266 } else { 3267 $stickies = array_unique( array_map( 'intval', $stickies ) ); 3268 } 3269 3270 if ( ! in_array( $post_id, $stickies, true ) ) { 3271 $stickies[] = $post_id; 3272 $updated = update_option( 'sticky_posts', array_values( $stickies ) ); 3273 } 3274 3275 if ( $updated ) { 3276 /** 3277 * Fires once a post has been added to the sticky list. 3278 * 3279 * @since 4.6.0 3280 * 3281 * @param int $post_id ID of the post that was stuck. 3282 */ 3283 do_action( 'post_stuck', $post_id ); 3284 } 3285 } 3286 3287 /** 3288 * Un-sticks a post. 3289 * 3290 * Sticky posts should be displayed at the top of the front page. 3291 * 3292 * @since 2.7.0 3293 * 3294 * @param int $post_id Post ID. 3295 */ 3296 function unstick_post( $post_id ) { 3297 $post_id = (int) $post_id; 3298 $stickies = get_option( 'sticky_posts' ); 3299 3300 if ( ! is_array( $stickies ) ) { 3301 return; 3302 } 3303 3304 $stickies = array_values( array_unique( array_map( 'intval', $stickies ) ) ); 3305 3306 if ( ! in_array( $post_id, $stickies, true ) ) { 3307 return; 3308 } 3309 3310 $offset = array_search( $post_id, $stickies, true ); 3311 if ( false === $offset ) { 3312 return; 3313 } 3314 3315 array_splice( $stickies, $offset, 1 ); 3316 3317 $updated = update_option( 'sticky_posts', $stickies ); 3318 3319 if ( $updated ) { 3320 /** 3321 * Fires once a post has been removed from the sticky list. 3322 * 3323 * @since 4.6.0 3324 * 3325 * @param int $post_id ID of the post that was unstuck. 3326 */ 3327 do_action( 'post_unstuck', $post_id ); 3328 } 3329 } 3330 3331 /** 3332 * Returns the cache key for wp_count_posts() based on the passed arguments. 3333 * 3334 * @since 3.9.0 3335 * @access private 3336 * 3337 * @param string $type Optional. Post type to retrieve count Default 'post'. 3338 * @param string $perm Optional. 'readable' or empty. Default empty. 3339 * @return string The cache key. 3340 */ 3341 function _count_posts_cache_key( $type = 'post', $perm = '' ) { 3342 $cache_key = 'posts-' . $type; 3343 3344 if ( 'readable' === $perm && is_user_logged_in() ) { 3345 $post_type_object = get_post_type_object( $type ); 3346 3347 if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 3348 $cache_key .= '_' . $perm . '_' . get_current_user_id(); 3349 } 3350 } 3351 3352 return $cache_key; 3353 } 3354 3355 /** 3356 * Counts number of posts of a post type and if user has permissions to view. 3357 * 3358 * This function provides an efficient method of finding the amount of post's 3359 * type a blog has. Another method is to count the amount of items in 3360 * get_posts(), but that method has a lot of overhead with doing so. Therefore, 3361 * when developing for 2.5+, use this function instead. 3362 * 3363 * The $perm parameter checks for 'readable' value and if the user can read 3364 * private posts, it will display that for the user that is signed in. 3365 * 3366 * @since 2.5.0 3367 * 3368 * @global wpdb $wpdb WordPress database abstraction object. 3369 * 3370 * @param string $type Optional. Post type to retrieve count. Default 'post'. 3371 * @param string $perm Optional. 'readable' or empty. Default empty. 3372 * @return stdClass An object containing the number of posts for each status, 3373 * or an empty object if the post type does not exist. 3374 */ 3375 function wp_count_posts( $type = 'post', $perm = '' ) { 3376 global $wpdb; 3377 3378 if ( ! post_type_exists( $type ) ) { 3379 return new stdClass(); 3380 } 3381 3382 $cache_key = _count_posts_cache_key( $type, $perm ); 3383 3384 $counts = wp_cache_get( $cache_key, 'counts' ); 3385 if ( false !== $counts ) { 3386 // We may have cached this before every status was registered. 3387 foreach ( get_post_stati() as $status ) { 3388 if ( ! isset( $counts->{$status} ) ) { 3389 $counts->{$status} = 0; 3390 } 3391 } 3392 3393 /** This filter is documented in wp-includes/post.php */ 3394 return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 3395 } 3396 3397 $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; 3398 3399 if ( 'readable' === $perm && is_user_logged_in() ) { 3400 $post_type_object = get_post_type_object( $type ); 3401 if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 3402 $query .= $wpdb->prepare( 3403 " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", 3404 get_current_user_id() 3405 ); 3406 } 3407 } 3408 3409 $query .= ' GROUP BY post_status'; 3410 3411 $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); 3412 $counts = array_fill_keys( get_post_stati(), 0 ); 3413 3414 foreach ( $results as $row ) { 3415 $counts[ $row['post_status'] ] = $row['num_posts']; 3416 } 3417 3418 $counts = (object) $counts; 3419 wp_cache_set( $cache_key, $counts, 'counts' ); 3420 3421 /** 3422 * Filters the post counts by status for the current post type. 3423 * 3424 * @since 3.7.0 3425 * 3426 * @param stdClass $counts An object containing the current post_type's post 3427 * counts by status. 3428 * @param string $type Post type. 3429 * @param string $perm The permission to determine if the posts are 'readable' 3430 * by the current user. 3431 */ 3432 return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 3433 } 3434 3435 /** 3436 * Counts number of attachments for the mime type(s). 3437 * 3438 * If you set the optional mime_type parameter, then an array will still be 3439 * returned, but will only have the item you are looking for. It does not give 3440 * you the number of attachments that are children of a post. You can get that 3441 * by counting the number of children that post has. 3442 * 3443 * @since 2.5.0 3444 * 3445 * @global wpdb $wpdb WordPress database abstraction object. 3446 * 3447 * @param string|string[] $mime_type Optional. Array or comma-separated list of 3448 * MIME patterns. Default empty. 3449 * @return stdClass An object containing the attachment counts by mime type. 3450 */ 3451 function wp_count_attachments( $mime_type = '' ) { 3452 global $wpdb; 3453 3454 $cache_key = sprintf( 3455 'attachments%s', 3456 ! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : '' 3457 ); 3458 3459 $counts = wp_cache_get( $cache_key, 'counts' ); 3460 3461 if ( false === $counts ) { 3462 $and = wp_post_mime_type_where( $mime_type ); 3463 $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 ); 3464 3465 $counts = array(); 3466 foreach ( (array) $count as $row ) { 3467 $counts[ $row['post_mime_type'] ] = $row['num_posts']; 3468 } 3469 $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); 3470 3471 wp_cache_set( $cache_key, (object) $counts, 'counts' ); 3472 } 3473 3474 /** 3475 * Filters the attachment counts by mime type. 3476 * 3477 * @since 3.7.0 3478 * 3479 * @param stdClass $counts An object containing the attachment counts by 3480 * mime type. 3481 * @param string|string[] $mime_type Array or comma-separated list of MIME patterns. 3482 */ 3483 return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); 3484 } 3485 3486 /** 3487 * Gets default post mime types. 3488 * 3489 * @since 2.9.0 3490 * @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups. 3491 * 3492 * @return array List of post mime types. 3493 */ 3494 function get_post_mime_types() { 3495 $post_mime_types = array( // array( adj, noun ) 3496 'image' => array( 3497 __( 'Images' ), 3498 __( 'Manage Images' ), 3499 /* translators: %s: Number of images. */ 3500 _n_noop( 3501 'Image <span class="count">(%s)</span>', 3502 'Images <span class="count">(%s)</span>' 3503 ), 3504 ), 3505 'audio' => array( 3506 _x( 'Audio', 'file type group' ), 3507 __( 'Manage Audio' ), 3508 /* translators: %s: Number of audio files. */ 3509 _n_noop( 3510 'Audio <span class="count">(%s)</span>', 3511 'Audio <span class="count">(%s)</span>' 3512 ), 3513 ), 3514 'video' => array( 3515 _x( 'Video', 'file type group' ), 3516 __( 'Manage Video' ), 3517 /* translators: %s: Number of video files. */ 3518 _n_noop( 3519 'Video <span class="count">(%s)</span>', 3520 'Video <span class="count">(%s)</span>' 3521 ), 3522 ), 3523 'document' => array( 3524 __( 'Documents' ), 3525 __( 'Manage Documents' ), 3526 /* translators: %s: Number of documents. */ 3527 _n_noop( 3528 'Document <span class="count">(%s)</span>', 3529 'Documents <span class="count">(%s)</span>' 3530 ), 3531 ), 3532 'spreadsheet' => array( 3533 __( 'Spreadsheets' ), 3534 __( 'Manage Spreadsheets' ), 3535 /* translators: %s: Number of spreadsheets. */ 3536 _n_noop( 3537 'Spreadsheet <span class="count">(%s)</span>', 3538 'Spreadsheets <span class="count">(%s)</span>' 3539 ), 3540 ), 3541 'archive' => array( 3542 _x( 'Archives', 'file type group' ), 3543 __( 'Manage Archives' ), 3544 /* translators: %s: Number of archives. */ 3545 _n_noop( 3546 'Archive <span class="count">(%s)</span>', 3547 'Archives <span class="count">(%s)</span>' 3548 ), 3549 ), 3550 ); 3551 3552 $ext_types = wp_get_ext_types(); 3553 $mime_types = wp_get_mime_types(); 3554 3555 foreach ( $post_mime_types as $group => $labels ) { 3556 if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) { 3557 continue; 3558 } 3559 3560 if ( ! isset( $ext_types[ $group ] ) ) { 3561 unset( $post_mime_types[ $group ] ); 3562 continue; 3563 } 3564 3565 $group_mime_types = array(); 3566 foreach ( $ext_types[ $group ] as $extension ) { 3567 foreach ( $mime_types as $exts => $mime ) { 3568 if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { 3569 $group_mime_types[] = $mime; 3570 break; 3571 } 3572 } 3573 } 3574 $group_mime_types = implode( ',', array_unique( $group_mime_types ) ); 3575 3576 $post_mime_types[ $group_mime_types ] = $labels; 3577 unset( $post_mime_types[ $group ] ); 3578 } 3579 3580 /** 3581 * Filters the default list of post mime types. 3582 * 3583 * @since 2.5.0 3584 * 3585 * @param array $post_mime_types Default list of post mime types. 3586 */ 3587 return apply_filters( 'post_mime_types', $post_mime_types ); 3588 } 3589 3590 /** 3591 * Checks a MIME-Type against a list. 3592 * 3593 * If the `$wildcard_mime_types` parameter is a string, it must be comma separated 3594 * list. If the `$real_mime_types` is a string, it is also comma separated to 3595 * create the list. 3596 * 3597 * @since 2.5.0 3598 * 3599 * @param string|string[] $wildcard_mime_types Mime types, e.g. `audio/mpeg`, `image` (same as `image/*`), 3600 * or `flash` (same as `*flash*`). 3601 * @param string|string[] $real_mime_types Real post mime type values. 3602 * @return array array(wildcard=>array(real types)). 3603 */ 3604 function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { 3605 $matches = array(); 3606 if ( is_string( $wildcard_mime_types ) ) { 3607 $wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) ); 3608 } 3609 if ( is_string( $real_mime_types ) ) { 3610 $real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) ); 3611 } 3612 3613 $patternses = array(); 3614 $wild = '[-._a-z0-9]*'; 3615 3616 foreach ( (array) $wildcard_mime_types as $type ) { 3617 $mimes = array_map( 'trim', explode( ',', $type ) ); 3618 foreach ( $mimes as $mime ) { 3619 $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); 3620 3621 $patternses[][ $type ] = "^$regex$"; 3622 3623 if ( ! str_contains( $mime, '/' ) ) { 3624 $patternses[][ $type ] = "^$regex/"; 3625 $patternses[][ $type ] = $regex; 3626 } 3627 } 3628 } 3629 asort( $patternses ); 3630 3631 foreach ( $patternses as $patterns ) { 3632 foreach ( $patterns as $type => $pattern ) { 3633 foreach ( (array) $real_mime_types as $real ) { 3634 if ( preg_match( "#$pattern#", $real ) 3635 && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) ) 3636 ) { 3637 $matches[ $type ][] = $real; 3638 } 3639 } 3640 } 3641 } 3642 3643 return $matches; 3644 } 3645 3646 /** 3647 * Converts MIME types into SQL. 3648 * 3649 * @since 2.5.0 3650 * 3651 * @param string|string[] $post_mime_types List of mime types or comma separated string 3652 * of mime types. 3653 * @param string $table_alias Optional. Specify a table alias, if needed. 3654 * Default empty. 3655 * @return string The SQL AND clause for mime searching. 3656 */ 3657 function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { 3658 $where = ''; 3659 $wildcards = array( '', '%', '%/%' ); 3660 if ( is_string( $post_mime_types ) ) { 3661 $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); 3662 } 3663 3664 $where_clauses = array(); 3665 3666 foreach ( (array) $post_mime_types as $mime_type ) { 3667 $mime_type = preg_replace( '/\s/', '', $mime_type ); 3668 $slashpos = strpos( $mime_type, '/' ); 3669 if ( false !== $slashpos ) { 3670 $mime_group = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) ); 3671 $mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) ); 3672 if ( empty( $mime_subgroup ) ) { 3673 $mime_subgroup = '*'; 3674 } else { 3675 $mime_subgroup = str_replace( '/', '', $mime_subgroup ); 3676 } 3677 $mime_pattern = "$mime_group/$mime_subgroup"; 3678 } else { 3679 $mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); 3680 if ( ! str_contains( $mime_pattern, '*' ) ) { 3681 $mime_pattern .= '/*'; 3682 } 3683 } 3684 3685 $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); 3686 3687 if ( in_array( $mime_type, $wildcards, true ) ) { 3688 return ''; 3689 } 3690 3691 if ( str_contains( $mime_pattern, '%' ) ) { 3692 $where_clauses[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; 3693 } else { 3694 $where_clauses[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; 3695 } 3696 } 3697 3698 if ( ! empty( $where_clauses ) ) { 3699 $where = ' AND (' . implode( ' OR ', $where_clauses ) . ') '; 3700 } 3701 3702 return $where; 3703 } 3704 3705 /** 3706 * Trashes or deletes a post or page. 3707 * 3708 * When the post and page is permanently deleted, everything that is tied to 3709 * it is deleted also. This includes comments, post meta fields, and terms 3710 * associated with the post. 3711 * 3712 * The post or page is moved to Trash instead of permanently deleted unless 3713 * Trash is disabled, item is already in the Trash, or $force_delete is true. 3714 * 3715 * @since 1.0.0 3716 * 3717 * @global wpdb $wpdb WordPress database abstraction object. 3718 * @see wp_delete_attachment() 3719 * @see wp_trash_post() 3720 * 3721 * @param int $post_id Optional. Post ID. Default 0. 3722 * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. 3723 * Default false. 3724 * @return WP_Post|false|null Post data on success, false or null on failure. 3725 */ 3726 function wp_delete_post( $post_id = 0, $force_delete = false ) { 3727 global $wpdb; 3728 3729 $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) ); 3730 3731 if ( ! $post ) { 3732 return $post; 3733 } 3734 3735 $post = get_post( $post ); 3736 3737 if ( ! $force_delete 3738 && ( 'post' === $post->post_type || 'page' === $post->post_type ) 3739 && 'trash' !== get_post_status( $post_id ) && EMPTY_TRASH_DAYS 3740 ) { 3741 return wp_trash_post( $post_id ); 3742 } 3743 3744 if ( 'attachment' === $post->post_type ) { 3745 return wp_delete_attachment( $post_id, $force_delete ); 3746 } 3747 3748 /** 3749 * Filters whether a post deletion should take place. 3750 * 3751 * @since 4.4.0 3752 * 3753 * @param WP_Post|false|null $delete Whether to go forward with deletion. 3754 * @param WP_Post $post Post object. 3755 * @param bool $force_delete Whether to bypass the Trash. 3756 */ 3757 $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); 3758 if ( null !== $check ) { 3759 return $check; 3760 } 3761 3762 /** 3763 * Fires before a post is deleted, at the start of wp_delete_post(). 3764 * 3765 * @since 3.2.0 3766 * @since 5.5.0 Added the `$post` parameter. 3767 * 3768 * @see wp_delete_post() 3769 * 3770 * @param int $post_id Post ID. 3771 * @param WP_Post $post Post object. 3772 */ 3773 do_action( 'before_delete_post', $post_id, $post ); 3774 3775 delete_post_meta( $post_id, '_wp_trash_meta_status' ); 3776 delete_post_meta( $post_id, '_wp_trash_meta_time' ); 3777 3778 wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); 3779 3780 $parent_data = array( 'post_parent' => $post->post_parent ); 3781 $parent_where = array( 'post_parent' => $post_id ); 3782 3783 if ( is_post_type_hierarchical( $post->post_type ) ) { 3784 // Point children of this page to its parent, also clean the cache of affected children. 3785 $children_query = $wpdb->prepare( 3786 "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", 3787 $post_id, 3788 $post->post_type 3789 ); 3790 3791 $children = $wpdb->get_results( $children_query ); 3792 3793 if ( $children ) { 3794 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); 3795 } 3796 } 3797 3798 // Do raw query. wp_get_post_revisions() is filtered. 3799 $revision_ids = $wpdb->get_col( 3800 $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $post_id ) 3801 ); 3802 3803 // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. 3804 foreach ( $revision_ids as $revision_id ) { 3805 wp_delete_post_revision( $revision_id ); 3806 } 3807 3808 // Point all attachments to this post up one level. 3809 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); 3810 3811 wp_defer_comment_counting( true ); 3812 3813 $comment_ids = $wpdb->get_col( 3814 $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) 3815 ); 3816 3817 foreach ( $comment_ids as $comment_id ) { 3818 wp_delete_comment( $comment_id, true ); 3819 } 3820 3821 wp_defer_comment_counting( false ); 3822 3823 $post_meta_ids = $wpdb->get_col( 3824 $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) 3825 ); 3826 3827 foreach ( $post_meta_ids as $mid ) { 3828 delete_metadata_by_mid( 'post', $mid ); 3829 } 3830 3831 /** 3832 * Fires immediately before a post is deleted from the database. 3833 * 3834 * The dynamic portion of the hook name, `$post->post_type`, refers to 3835 * the post type slug. 3836 * 3837 * @since 6.6.0 3838 * 3839 * @param int $post_id Post ID. 3840 * @param WP_Post $post Post object. 3841 */ 3842 do_action( "delete_post_{$post->post_type}", $post_id, $post ); 3843 3844 /** 3845 * Fires immediately before a post is deleted from the database. 3846 * 3847 * @since 1.2.0 3848 * @since 5.5.0 Added the `$post` parameter. 3849 * 3850 * @param int $post_id Post ID. 3851 * @param WP_Post $post Post object. 3852 */ 3853 do_action( 'delete_post', $post_id, $post ); 3854 3855 $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); 3856 if ( ! $result ) { 3857 return false; 3858 } 3859 3860 /** 3861 * Fires immediately after a post is deleted from the database. 3862 * 3863 * The dynamic portion of the hook name, `$post->post_type`, refers to 3864 * the post type slug. 3865 * 3866 * @since 6.6.0 3867 * 3868 * @param int $post_id Post ID. 3869 * @param WP_Post $post Post object. 3870 */ 3871 do_action( "deleted_post_{$post->post_type}", $post_id, $post ); 3872 3873 /** 3874 * Fires immediately after a post is deleted from the database. 3875 * 3876 * @since 2.2.0 3877 * @since 5.5.0 Added the `$post` parameter. 3878 * 3879 * @param int $post_id Post ID. 3880 * @param WP_Post $post Post object. 3881 */ 3882 do_action( 'deleted_post', $post_id, $post ); 3883 3884 clean_post_cache( $post ); 3885 3886 if ( is_post_type_hierarchical( $post->post_type ) && $children ) { 3887 foreach ( $children as $child ) { 3888 clean_post_cache( $child ); 3889 } 3890 } 3891 3892 wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); 3893 3894 /** 3895 * Fires after a post is deleted, at the conclusion of wp_delete_post(). 3896 * 3897 * @since 3.2.0 3898 * @since 5.5.0 Added the `$post` parameter. 3899 * 3900 * @see wp_delete_post() 3901 * 3902 * @param int $post_id Post ID. 3903 * @param WP_Post $post Post object. 3904 */ 3905 do_action( 'after_delete_post', $post_id, $post ); 3906 3907 return $post; 3908 } 3909 3910 /** 3911 * Resets the page_on_front, show_on_front, and page_for_post settings when 3912 * a linked page is deleted or trashed. 3913 * 3914 * Also ensures the post is no longer sticky. 3915 * 3916 * @since 3.7.0 3917 * @access private 3918 * 3919 * @param int $post_id Post ID. 3920 */ 3921 function _reset_front_page_settings_for_post( $post_id ) { 3922 $post = get_post( $post_id ); 3923 3924 if ( 'page' === $post->post_type ) { 3925 /* 3926 * If the page is defined in option page_on_front or post_for_posts, 3927 * adjust the corresponding options. 3928 */ 3929 if ( (int) get_option( 'page_on_front' ) === $post->ID ) { 3930 update_option( 'show_on_front', 'posts' ); 3931 update_option( 'page_on_front', 0 ); 3932 } 3933 if ( (int) get_option( 'page_for_posts' ) === $post->ID ) { 3934 update_option( 'page_for_posts', 0 ); 3935 } 3936 } 3937 3938 unstick_post( $post->ID ); 3939 } 3940 3941 /** 3942 * Moves a post or page to the Trash 3943 * 3944 * If Trash is disabled, the post or page is permanently deleted. 3945 * 3946 * @since 2.9.0 3947 * 3948 * @see wp_delete_post() 3949 * 3950 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post` 3951 * if `EMPTY_TRASH_DAYS` equals true. 3952 * @return WP_Post|false|null Post data on success, false or null on failure. 3953 */ 3954 function wp_trash_post( $post_id = 0 ) { 3955 if ( ! EMPTY_TRASH_DAYS ) { 3956 return wp_delete_post( $post_id, true ); 3957 } 3958 3959 $post = get_post( $post_id ); 3960 3961 if ( ! $post ) { 3962 return $post; 3963 } 3964 3965 if ( 'trash' === $post->post_status ) { 3966 return false; 3967 } 3968 3969 $previous_status = $post->post_status; 3970 3971 /** 3972 * Filters whether a post trashing should take place. 3973 * 3974 * @since 4.9.0 3975 * @since 6.3.0 Added the `$previous_status` parameter. 3976 * 3977 * @param bool|null $trash Whether to go forward with trashing. 3978 * @param WP_Post $post Post object. 3979 * @param string $previous_status The status of the post about to be trashed. 3980 */ 3981 $check = apply_filters( 'pre_trash_post', null, $post, $previous_status ); 3982 3983 if ( null !== $check ) { 3984 return $check; 3985 } 3986 3987 /** 3988 * Fires before a post is sent to the Trash. 3989 * 3990 * @since 3.3.0 3991 * @since 6.3.0 Added the `$previous_status` parameter. 3992 * 3993 * @param int $post_id Post ID. 3994 * @param string $previous_status The status of the post about to be trashed. 3995 */ 3996 do_action( 'wp_trash_post', $post_id, $previous_status ); 3997 3998 add_post_meta( $post_id, '_wp_trash_meta_status', $previous_status ); 3999 add_post_meta( $post_id, '_wp_trash_meta_time', time() ); 4000 4001 $post_updated = wp_update_post( 4002 array( 4003 'ID' => $post_id, 4004 'post_status' => 'trash', 4005 ) 4006 ); 4007 4008 if ( ! $post_updated ) { 4009 return false; 4010 } 4011 4012 wp_trash_post_comments( $post_id ); 4013 4014 /** 4015 * Fires after a post is sent to the Trash. 4016 * 4017 * @since 2.9.0 4018 * @since 6.3.0 Added the `$previous_status` parameter. 4019 * 4020 * @param int $post_id Post ID. 4021 * @param string $previous_status The status of the post at the point where it was trashed. 4022 */ 4023 do_action( 'trashed_post', $post_id, $previous_status ); 4024 4025 return $post; 4026 } 4027 4028 /** 4029 * Restores a post from the Trash. 4030 * 4031 * @since 2.9.0 4032 * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for 4033 * attachments which are returned to their original 'inherit' status. 4034 * 4035 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 4036 * @return WP_Post|false|null Post data on success, false or null on failure. 4037 */ 4038 function wp_untrash_post( $post_id = 0 ) { 4039 $post = get_post( $post_id ); 4040 4041 if ( ! $post ) { 4042 return $post; 4043 } 4044 4045 $post_id = $post->ID; 4046 4047 if ( 'trash' !== $post->post_status ) { 4048 return false; 4049 } 4050 4051 $previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true ); 4052 4053 /** 4054 * Filters whether a post untrashing should take place. 4055 * 4056 * @since 4.9.0 4057 * @since 5.6.0 Added the `$previous_status` parameter. 4058 * 4059 * @param bool|null $untrash Whether to go forward with untrashing. 4060 * @param WP_Post $post Post object. 4061 * @param string $previous_status The status of the post at the point where it was trashed. 4062 */ 4063 $check = apply_filters( 'pre_untrash_post', null, $post, $previous_status ); 4064 if ( null !== $check ) { 4065 return $check; 4066 } 4067 4068 /** 4069 * Fires before a post is restored from the Trash. 4070 * 4071 * @since 2.9.0 4072 * @since 5.6.0 Added the `$previous_status` parameter. 4073 * 4074 * @param int $post_id Post ID. 4075 * @param string $previous_status The status of the post at the point where it was trashed. 4076 */ 4077 do_action( 'untrash_post', $post_id, $previous_status ); 4078 4079 $new_status = ( 'attachment' === $post->post_type ) ? 'inherit' : 'draft'; 4080 4081 /** 4082 * Filters the status that a post gets assigned when it is restored from the trash (untrashed). 4083 * 4084 * By default posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status` 4085 * in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()` 4086 * function is available for this. 4087 * 4088 * Prior to WordPress 5.6.0, restored posts were always assigned their original status. 4089 * 4090 * @since 5.6.0 4091 * 4092 * @param string $new_status The new status of the post being restored. 4093 * @param int $post_id The ID of the post being restored. 4094 * @param string $previous_status The status of the post at the point where it was trashed. 4095 */ 4096 $post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status ); 4097 4098 delete_post_meta( $post_id, '_wp_trash_meta_status' ); 4099 delete_post_meta( $post_id, '_wp_trash_meta_time' ); 4100 4101 $post_updated = wp_update_post( 4102 array( 4103 'ID' => $post_id, 4104 'post_status' => $post_status, 4105 ) 4106 ); 4107 4108 if ( ! $post_updated ) { 4109 return false; 4110 } 4111 4112 wp_untrash_post_comments( $post_id ); 4113 4114 /** 4115 * Fires after a post is restored from the Trash. 4116 * 4117 * @since 2.9.0 4118 * @since 5.6.0 Added the `$previous_status` parameter. 4119 * 4120 * @param int $post_id Post ID. 4121 * @param string $previous_status The status of the post at the point where it was trashed. 4122 */ 4123 do_action( 'untrashed_post', $post_id, $previous_status ); 4124 4125 return $post; 4126 } 4127 4128 /** 4129 * Moves comments for a post to the Trash. 4130 * 4131 * @since 2.9.0 4132 * 4133 * @global wpdb $wpdb WordPress database abstraction object. 4134 * 4135 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 4136 * @return mixed|void False on failure. 4137 */ 4138 function wp_trash_post_comments( $post = null ) { 4139 global $wpdb; 4140 4141 $post = get_post( $post ); 4142 4143 if ( ! $post ) { 4144 return; 4145 } 4146 4147 $post_id = $post->ID; 4148 4149 /** 4150 * Fires before comments are sent to the Trash. 4151 * 4152 * @since 2.9.0 4153 * 4154 * @param int $post_id Post ID. 4155 */ 4156 do_action( 'trash_post_comments', $post_id ); 4157 4158 $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); 4159 4160 if ( ! $comments ) { 4161 return; 4162 } 4163 4164 // Cache current status for each comment. 4165 $statuses = array(); 4166 foreach ( $comments as $comment ) { 4167 $statuses[ $comment->comment_ID ] = $comment->comment_approved; 4168 } 4169 add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses ); 4170 4171 // Set status for all comments to post-trashed. 4172 $result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) ); 4173 4174 clean_comment_cache( array_keys( $statuses ) ); 4175 4176 /** 4177 * Fires after comments are sent to the Trash. 4178 * 4179 * @since 2.9.0 4180 * 4181 * @param int $post_id Post ID. 4182 * @param array $statuses Array of comment statuses. 4183 */ 4184 do_action( 'trashed_post_comments', $post_id, $statuses ); 4185 4186 return $result; 4187 } 4188 4189 /** 4190 * Restores comments for a post from the Trash. 4191 * 4192 * @since 2.9.0 4193 * 4194 * @global wpdb $wpdb WordPress database abstraction object. 4195 * 4196 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 4197 * @return true|void 4198 */ 4199 function wp_untrash_post_comments( $post = null ) { 4200 global $wpdb; 4201 4202 $post = get_post( $post ); 4203 4204 if ( ! $post ) { 4205 return; 4206 } 4207 4208 $post_id = $post->ID; 4209 4210 $statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true ); 4211 4212 if ( ! $statuses ) { 4213 return true; 4214 } 4215 4216 /** 4217 * Fires before comments are restored for a post from the Trash. 4218 * 4219 * @since 2.9.0 4220 * 4221 * @param int $post_id Post ID. 4222 */ 4223 do_action( 'untrash_post_comments', $post_id ); 4224 4225 // Restore each comment to its original status. 4226 $group_by_status = array(); 4227 foreach ( $statuses as $comment_id => $comment_status ) { 4228 $group_by_status[ $comment_status ][] = $comment_id; 4229 } 4230 4231 foreach ( $group_by_status as $status => $comments ) { 4232 // Confidence check. This shouldn't happen. 4233 if ( 'post-trashed' === $status ) { 4234 $status = '0'; 4235 } 4236 $comments_in = implode( ', ', array_map( 'intval', $comments ) ); 4237 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); 4238 } 4239 4240 clean_comment_cache( array_keys( $statuses ) ); 4241 4242 delete_post_meta( $post_id, '_wp_trash_meta_comments_status' ); 4243 4244 /** 4245 * Fires after comments are restored for a post from the Trash. 4246 * 4247 * @since 2.9.0 4248 * 4249 * @param int $post_id Post ID. 4250 */ 4251 do_action( 'untrashed_post_comments', $post_id ); 4252 } 4253 4254 /** 4255 * Retrieves the list of categories for a post. 4256 * 4257 * Compatibility layer for themes and plugins. Also an easy layer of abstraction 4258 * away from the complexity of the taxonomy layer. 4259 * 4260 * @since 2.1.0 4261 * 4262 * @see wp_get_object_terms() 4263 * 4264 * @param int $post_id Optional. The Post ID. Does not default to the ID of the 4265 * global $post. Default 0. 4266 * @param array $args Optional. Category query parameters. Default empty array. 4267 * See WP_Term_Query::__construct() for supported arguments. 4268 * @return array|WP_Error List of categories. If the `$fields` argument passed via `$args` is 'all' or 4269 * 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` 4270 * is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names. 4271 * WP_Error object if 'category' taxonomy doesn't exist. 4272 */ 4273 function wp_get_post_categories( $post_id = 0, $args = array() ) { 4274 $post_id = (int) $post_id; 4275 4276 $defaults = array( 'fields' => 'ids' ); 4277 $args = wp_parse_args( $args, $defaults ); 4278 4279 $cats = wp_get_object_terms( $post_id, 'category', $args ); 4280 return $cats; 4281 } 4282 4283 /** 4284 * Retrieves the tags for a post. 4285 * 4286 * There is only one default for this function, called 'fields' and by default 4287 * is set to 'all'. There are other defaults that can be overridden in 4288 * wp_get_object_terms(). 4289 * 4290 * @since 2.3.0 4291 * 4292 * @param int $post_id Optional. The Post ID. Does not default to the ID of the 4293 * global $post. Default 0. 4294 * @param array $args Optional. Tag query parameters. Default empty array. 4295 * See WP_Term_Query::__construct() for supported arguments. 4296 * @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found. 4297 * WP_Error object if 'post_tag' taxonomy doesn't exist. 4298 */ 4299 function wp_get_post_tags( $post_id = 0, $args = array() ) { 4300 return wp_get_post_terms( $post_id, 'post_tag', $args ); 4301 } 4302 4303 /** 4304 * Retrieves the terms for a post. 4305 * 4306 * @since 2.8.0 4307 * 4308 * @param int $post_id Optional. The Post ID. Does not default to the ID of the 4309 * global $post. Default 0. 4310 * @param string|string[] $taxonomy Optional. The taxonomy slug or array of slugs for which 4311 * to retrieve terms. Default 'post_tag'. 4312 * @param array $args { 4313 * Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments. 4314 * 4315 * @type string $fields Term fields to retrieve. Default 'all'. 4316 * } 4317 * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found. 4318 * WP_Error object if `$taxonomy` doesn't exist. 4319 */ 4320 function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { 4321 $post_id = (int) $post_id; 4322 4323 $defaults = array( 'fields' => 'all' ); 4324 $args = wp_parse_args( $args, $defaults ); 4325 4326 $tags = wp_get_object_terms( $post_id, $taxonomy, $args ); 4327 4328 return $tags; 4329 } 4330 4331 /** 4332 * Retrieves a number of recent posts. 4333 * 4334 * @since 1.0.0 4335 * 4336 * @see get_posts() 4337 * 4338 * @param array $args Optional. Arguments to retrieve posts. Default empty array. 4339 * @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which 4340 * correspond to a WP_Post object or an associative array, respectively. 4341 * Default ARRAY_A. 4342 * @return array|false Array of recent posts, where the type of each element is determined 4343 * by the `$output` parameter. Empty array on failure. 4344 */ 4345 function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { 4346 4347 if ( is_numeric( $args ) ) { 4348 _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); 4349 $args = array( 'numberposts' => absint( $args ) ); 4350 } 4351 4352 // Set default arguments. 4353 $defaults = array( 4354 'numberposts' => 10, 4355 'offset' => 0, 4356 'category' => 0, 4357 'orderby' => 'post_date', 4358 'order' => 'DESC', 4359 'include' => '', 4360 'exclude' => '', 4361 'meta_key' => '', 4362 'meta_value' => '', 4363 'post_type' => 'post', 4364 'post_status' => 'draft, publish, future, pending, private', 4365 'suppress_filters' => true, 4366 ); 4367 4368 $parsed_args = wp_parse_args( $args, $defaults ); 4369 4370 $results = get_posts( $parsed_args ); 4371 4372 // Backward compatibility. Prior to 3.1 expected posts to be returned in array. 4373 if ( ARRAY_A === $output ) { 4374 foreach ( $results as $key => $result ) { 4375 $results[ $key ] = get_object_vars( $result ); 4376 } 4377 return $results ? $results : array(); 4378 } 4379 4380 return $results ? $results : false; 4381 } 4382 4383 /** 4384 * Inserts or update a post. 4385 * 4386 * If the $postarr parameter has 'ID' set to a value, then post will be updated. 4387 * 4388 * You can set the post date manually, by setting the values for 'post_date' 4389 * and 'post_date_gmt' keys. You can close the comments or open the comments by 4390 * setting the value for 'comment_status' key. 4391 * 4392 * @since 1.0.0 4393 * @since 2.6.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. 4394 * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. 4395 * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. 4396 * @since 5.6.0 Added the `$fire_after_hooks` parameter. 4397 * 4398 * @see sanitize_post() 4399 * @global wpdb $wpdb WordPress database abstraction object. 4400 * 4401 * @param array $postarr { 4402 * An array of elements that make up a post to update or insert. 4403 * 4404 * @type int $ID The post ID. If equal to something other than 0, 4405 * the post with that ID will be updated. Default 0. 4406 * @type int $post_author The ID of the user who added the post. Default is 4407 * the current user ID. 4408 * @type string $post_date The date of the post. Default is the current time. 4409 * @type string $post_date_gmt The date of the post in the GMT timezone. Default is 4410 * the value of `$post_date`. 4411 * @type string $post_content The post content. Default empty. 4412 * @type string $post_content_filtered The filtered post content. Default empty. 4413 * @type string $post_title The post title. Default empty. 4414 * @type string $post_excerpt The post excerpt. Default empty. 4415 * @type string $post_status The post status. Default 'draft'. 4416 * @type string $post_type The post type. Default 'post'. 4417 * @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. 4418 * Default is the value of 'default_comment_status' option. 4419 * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. 4420 * Default is the value of 'default_ping_status' option. 4421 * @type string $post_password The password to access the post. Default empty. 4422 * @type string $post_name The post name. Default is the sanitized post title 4423 * when creating a new post. 4424 * @type string $to_ping Space or carriage return-separated list of URLs to ping. 4425 * Default empty. 4426 * @type string $pinged Space or carriage return-separated list of URLs that have 4427 * been pinged. Default empty. 4428 * @type int $post_parent Set this for the post it belongs to, if any. Default 0. 4429 * @type int $menu_order The order the post should be displayed in. Default 0. 4430 * @type string $post_mime_type The mime type of the post. Default empty. 4431 * @type string $guid Global Unique ID for referencing the post. Default empty. 4432 * @type int $import_id The post ID to be used when inserting a new post. 4433 * If specified, must not match any existing post ID. Default 0. 4434 * @type int[] $post_category Array of category IDs. 4435 * Defaults to value of the 'default_category' option. 4436 * @type array $tags_input Array of tag names, slugs, or IDs. Default empty. 4437 * @type array $tax_input An array of taxonomy terms keyed by their taxonomy name. 4438 * If the taxonomy is hierarchical, the term list needs to be 4439 * either an array of term IDs or a comma-separated string of IDs. 4440 * If the taxonomy is non-hierarchical, the term list can be an array 4441 * that contains term names or slugs, or a comma-separated string 4442 * of names or slugs. This is because, in hierarchical taxonomy, 4443 * child terms can have the same names with different parent terms, 4444 * so the only way to connect them is using ID. Default empty. 4445 * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. 4446 * @type string $page_template Page template to use. 4447 * } 4448 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 4449 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. 4450 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. 4451 */ 4452 function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) { 4453 global $wpdb; 4454 4455 // Capture original pre-sanitized array for passing into filters. 4456 $unsanitized_postarr = $postarr; 4457 4458 $user_id = get_current_user_id(); 4459 4460 $defaults = array( 4461 'post_author' => $user_id, 4462 'post_content' => '', 4463 'post_content_filtered' => '', 4464 'post_title' => '', 4465 'post_excerpt' => '', 4466 'post_status' => 'draft', 4467 'post_type' => 'post', 4468 'comment_status' => '', 4469 'ping_status' => '', 4470 'post_password' => '', 4471 'to_ping' => '', 4472 'pinged' => '', 4473 'post_parent' => 0, 4474 'menu_order' => 0, 4475 'guid' => '', 4476 'import_id' => 0, 4477 'context' => '', 4478 'post_date' => '', 4479 'post_date_gmt' => '', 4480 ); 4481 4482 $postarr = wp_parse_args( $postarr, $defaults ); 4483 4484 unset( $postarr['filter'] ); 4485 4486 $postarr = sanitize_post( $postarr, 'db' ); 4487 4488 // Are we updating or creating? 4489 $post_id = 0; 4490 $update = false; 4491 $guid = $postarr['guid']; 4492 4493 if ( ! empty( $postarr['ID'] ) ) { 4494 $update = true; 4495 4496 // Get the post ID and GUID. 4497 $post_id = $postarr['ID']; 4498 $post_before = get_post( $post_id ); 4499 4500 if ( is_null( $post_before ) ) { 4501 if ( $wp_error ) { 4502 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 4503 } 4504 return 0; 4505 } 4506 4507 $guid = get_post_field( 'guid', $post_id ); 4508 $previous_status = get_post_field( 'post_status', $post_id ); 4509 } else { 4510 $previous_status = 'new'; 4511 $post_before = null; 4512 } 4513 4514 $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; 4515 4516 $post_title = $postarr['post_title']; 4517 $post_content = $postarr['post_content']; 4518 $post_excerpt = $postarr['post_excerpt']; 4519 4520 if ( isset( $postarr['post_name'] ) ) { 4521 $post_name = $postarr['post_name']; 4522 } elseif ( $update ) { 4523 // For an update, don't modify the post_name if it wasn't supplied as an argument. 4524 $post_name = $post_before->post_name; 4525 } 4526 4527 $maybe_empty = 'attachment' !== $post_type 4528 && ! $post_content && ! $post_title && ! $post_excerpt 4529 && post_type_supports( $post_type, 'editor' ) 4530 && post_type_supports( $post_type, 'title' ) 4531 && post_type_supports( $post_type, 'excerpt' ); 4532 4533 /** 4534 * Filters whether the post should be considered "empty". 4535 * 4536 * The post is considered "empty" if both: 4537 * 1. The post type supports the title, editor, and excerpt fields 4538 * 2. The title, editor, and excerpt fields are all empty 4539 * 4540 * Returning a truthy value from the filter will effectively short-circuit 4541 * the new post being inserted and return 0. If $wp_error is true, a WP_Error 4542 * will be returned instead. 4543 * 4544 * @since 3.3.0 4545 * 4546 * @param bool $maybe_empty Whether the post should be considered "empty". 4547 * @param array $postarr Array of post data. 4548 */ 4549 if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { 4550 if ( $wp_error ) { 4551 return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); 4552 } else { 4553 return 0; 4554 } 4555 } 4556 4557 $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; 4558 4559 if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) { 4560 $post_status = 'inherit'; 4561 } 4562 4563 if ( ! empty( $postarr['post_category'] ) ) { 4564 // Filter out empty terms. 4565 $post_category = array_filter( $postarr['post_category'] ); 4566 } elseif ( $update && ! isset( $postarr['post_category'] ) ) { 4567 $post_category = $post_before->post_category; 4568 } 4569 4570 // Make sure we set a valid category. 4571 if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) { 4572 // 'post' requires at least one category. 4573 if ( 'post' === $post_type && 'auto-draft' !== $post_status ) { 4574 $post_category = array( get_option( 'default_category' ) ); 4575 } else { 4576 $post_category = array(); 4577 } 4578 } 4579 4580 /* 4581 * Don't allow contributors to set the post slug for pending review posts. 4582 * 4583 * For new posts check the primitive capability, for updates check the meta capability. 4584 */ 4585 if ( 'pending' === $post_status ) { 4586 $post_type_object = get_post_type_object( $post_type ); 4587 4588 if ( ! $update && $post_type_object && ! current_user_can( $post_type_object->cap->publish_posts ) ) { 4589 $post_name = ''; 4590 } elseif ( $update && ! current_user_can( 'publish_post', $post_id ) ) { 4591 $post_name = ''; 4592 } 4593 } 4594 4595 /* 4596 * Create a valid post name. Drafts and pending posts are allowed to have 4597 * an empty post name. 4598 */ 4599 if ( empty( $post_name ) ) { 4600 if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) { 4601 $post_name = sanitize_title( $post_title ); 4602 } else { 4603 $post_name = ''; 4604 } 4605 } else { 4606 // On updates, we need to check to see if it's using the old, fixed sanitization context. 4607 $check_name = sanitize_title( $post_name, '', 'old-save' ); 4608 4609 if ( $update 4610 && strtolower( urlencode( $post_name ) ) === $check_name 4611 && get_post_field( 'post_name', $post_id ) === $check_name 4612 ) { 4613 $post_name = $check_name; 4614 } else { // New post, or slug has changed. 4615 $post_name = sanitize_title( $post_name ); 4616 } 4617 } 4618 4619 /* 4620 * Resolve the post date from any provided post date or post date GMT strings; 4621 * if none are provided, the date will be set to now. 4622 */ 4623 $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] ); 4624 4625 if ( ! $post_date ) { 4626 if ( $wp_error ) { 4627 return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); 4628 } else { 4629 return 0; 4630 } 4631 } 4632 4633 if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) { 4634 if ( ! in_array( $post_status, get_post_stati( array( 'date_floating' => true ) ), true ) ) { 4635 $post_date_gmt = get_gmt_from_date( $post_date ); 4636 } else { 4637 $post_date_gmt = '0000-00-00 00:00:00'; 4638 } 4639 } else { 4640 $post_date_gmt = $postarr['post_date_gmt']; 4641 } 4642 4643 if ( $update || '0000-00-00 00:00:00' === $post_date ) { 4644 $post_modified = current_time( 'mysql' ); 4645 $post_modified_gmt = current_time( 'mysql', true ); 4646 } else { 4647 $post_modified = $post_date; 4648 $post_modified_gmt = $post_date_gmt; 4649 } 4650 4651 if ( 'attachment' !== $post_type ) { 4652 $now = gmdate( 'Y-m-d H:i:s' ); 4653 4654 if ( 'publish' === $post_status ) { 4655 if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) { 4656 $post_status = 'future'; 4657 } 4658 } elseif ( 'future' === $post_status ) { 4659 if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) { 4660 $post_status = 'publish'; 4661 } 4662 } 4663 } 4664 4665 // Comment status. 4666 if ( empty( $postarr['comment_status'] ) ) { 4667 if ( $update ) { 4668 $comment_status = 'closed'; 4669 } else { 4670 $comment_status = get_default_comment_status( $post_type ); 4671 } 4672 } else { 4673 $comment_status = $postarr['comment_status']; 4674 } 4675 4676 // These variables are needed by compact() later. 4677 $post_content_filtered = $postarr['post_content_filtered']; 4678 $post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; 4679 $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; 4680 $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; 4681 $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; 4682 $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; 4683 4684 /* 4685 * The 'wp_insert_post_parent' filter expects all variables to be present. 4686 * Previously, these variables would have already been extracted 4687 */ 4688 if ( isset( $postarr['menu_order'] ) ) { 4689 $menu_order = (int) $postarr['menu_order']; 4690 } else { 4691 $menu_order = 0; 4692 } 4693 4694 $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; 4695 if ( 'private' === $post_status ) { 4696 $post_password = ''; 4697 } 4698 4699 if ( isset( $postarr['post_parent'] ) ) { 4700 $post_parent = (int) $postarr['post_parent']; 4701 } else { 4702 $post_parent = 0; 4703 } 4704 4705 $new_postarr = array_merge( 4706 array( 4707 'ID' => $post_id, 4708 ), 4709 compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) 4710 ); 4711 4712 /** 4713 * Filters the post parent -- used to check for and prevent hierarchy loops. 4714 * 4715 * @since 3.1.0 4716 * 4717 * @param int $post_parent Post parent ID. 4718 * @param int $post_id Post ID. 4719 * @param array $new_postarr Array of parsed post data. 4720 * @param array $postarr Array of sanitized, but otherwise unmodified post data. 4721 */ 4722 $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_id, $new_postarr, $postarr ); 4723 4724 /* 4725 * If the post is being untrashed and it has a desired slug stored in post meta, 4726 * reassign it. 4727 */ 4728 if ( 'trash' === $previous_status && 'trash' !== $post_status ) { 4729 $desired_post_slug = get_post_meta( $post_id, '_wp_desired_post_slug', true ); 4730 4731 if ( $desired_post_slug ) { 4732 delete_post_meta( $post_id, '_wp_desired_post_slug' ); 4733 $post_name = $desired_post_slug; 4734 } 4735 } 4736 4737 // If a trashed post has the desired slug, change it and let this post have it. 4738 if ( 'trash' !== $post_status && $post_name ) { 4739 /** 4740 * Filters whether or not to add a `__trashed` suffix to trashed posts that match the name of the updated post. 4741 * 4742 * @since 5.4.0 4743 * 4744 * @param bool $add_trashed_suffix Whether to attempt to add the suffix. 4745 * @param string $post_name The name of the post being updated. 4746 * @param int $post_id Post ID. 4747 */ 4748 $add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_id ); 4749 4750 if ( $add_trashed_suffix ) { 4751 wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id ); 4752 } 4753 } 4754 4755 // When trashing an existing post, change its slug to allow non-trashed posts to use it. 4756 if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { 4757 $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_id ); 4758 } 4759 4760 $post_name = wp_unique_post_slug( $post_name, $post_id, $post_status, $post_type, $post_parent ); 4761 4762 // Don't unslash. 4763 $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; 4764 4765 // Expected_slashed (everything!). 4766 $data = compact( 4767 'post_author', 4768 'post_date', 4769 'post_date_gmt', 4770 'post_content', 4771 'post_content_filtered', 4772 'post_title', 4773 'post_excerpt', 4774 'post_status', 4775 'post_type', 4776 'comment_status', 4777 'ping_status', 4778 'post_password', 4779 'post_name', 4780 'to_ping', 4781 'pinged', 4782 'post_modified', 4783 'post_modified_gmt', 4784 'post_parent', 4785 'menu_order', 4786 'post_mime_type', 4787 'guid' 4788 ); 4789 4790 $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); 4791 4792 foreach ( $emoji_fields as $emoji_field ) { 4793 if ( isset( $data[ $emoji_field ] ) ) { 4794 $charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); 4795 4796 if ( 'utf8' === $charset ) { 4797 $data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); 4798 } 4799 } 4800 } 4801 4802 if ( 'attachment' === $post_type ) { 4803 /** 4804 * Filters attachment post data before it is updated in or added to the database. 4805 * 4806 * @since 3.9.0 4807 * @since 5.4.1 The `$unsanitized_postarr` parameter was added. 4808 * @since 6.0.0 The `$update` parameter was added. 4809 * 4810 * @param array $data An array of slashed, sanitized, and processed attachment post data. 4811 * @param array $postarr An array of slashed and sanitized attachment post data, but not processed. 4812 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data 4813 * as originally passed to wp_insert_post(). 4814 * @param bool $update Whether this is an existing attachment post being updated. 4815 */ 4816 $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update ); 4817 } else { 4818 /** 4819 * Filters slashed post data just before it is inserted into the database. 4820 * 4821 * @since 2.7.0 4822 * @since 5.4.1 The `$unsanitized_postarr` parameter was added. 4823 * @since 6.0.0 The `$update` parameter was added. 4824 * 4825 * @param array $data An array of slashed, sanitized, and processed post data. 4826 * @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data. 4827 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as 4828 * originally passed to wp_insert_post(). 4829 * @param bool $update Whether this is an existing post being updated. 4830 */ 4831 $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update ); 4832 } 4833 4834 $data = wp_unslash( $data ); 4835 $where = array( 'ID' => $post_id ); 4836 4837 if ( $update ) { 4838 /** 4839 * Fires immediately before an existing post is updated in the database. 4840 * 4841 * @since 2.5.0 4842 * 4843 * @param int $post_id Post ID. 4844 * @param array $data Array of unslashed post data. 4845 */ 4846 do_action( 'pre_post_update', $post_id, $data ); 4847 4848 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { 4849 if ( $wp_error ) { 4850 if ( 'attachment' === $post_type ) { 4851 $message = __( 'Could not update attachment in the database.' ); 4852 } else { 4853 $message = __( 'Could not update post in the database.' ); 4854 } 4855 4856 return new WP_Error( 'db_update_error', $message, $wpdb->last_error ); 4857 } else { 4858 return 0; 4859 } 4860 } 4861 } else { 4862 // If there is a suggested ID, use it if not already present. 4863 if ( ! empty( $import_id ) ) { 4864 $import_id = (int) $import_id; 4865 4866 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { 4867 $data['ID'] = $import_id; 4868 } 4869 } 4870 4871 if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { 4872 if ( $wp_error ) { 4873 if ( 'attachment' === $post_type ) { 4874 $message = __( 'Could not insert attachment into the database.' ); 4875 } else { 4876 $message = __( 'Could not insert post into the database.' ); 4877 } 4878 4879 return new WP_Error( 'db_insert_error', $message, $wpdb->last_error ); 4880 } else { 4881 return 0; 4882 } 4883 } 4884 4885 $post_id = (int) $wpdb->insert_id; 4886 4887 // Use the newly generated $post_id. 4888 $where = array( 'ID' => $post_id ); 4889 } 4890 4891 if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) { 4892 $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_id ), $post_id, $data['post_status'], $post_type, $post_parent ); 4893 4894 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); 4895 clean_post_cache( $post_id ); 4896 } 4897 4898 if ( is_object_in_taxonomy( $post_type, 'category' ) ) { 4899 wp_set_post_categories( $post_id, $post_category ); 4900 } 4901 4902 if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { 4903 wp_set_post_tags( $post_id, $postarr['tags_input'] ); 4904 } 4905 4906 // Add default term for all associated custom taxonomies. 4907 if ( 'auto-draft' !== $post_status ) { 4908 foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { 4909 4910 if ( ! empty( $tax_object->default_term ) ) { 4911 4912 // Filter out empty terms. 4913 if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { 4914 $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); 4915 } 4916 4917 // Passed custom taxonomy list overwrites the existing list if not empty. 4918 $terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) ); 4919 if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) { 4920 $postarr['tax_input'][ $taxonomy ] = $terms; 4921 } 4922 4923 if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { 4924 $default_term_id = get_option( 'default_term_' . $taxonomy ); 4925 if ( ! empty( $default_term_id ) ) { 4926 $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id ); 4927 } 4928 } 4929 } 4930 } 4931 } 4932 4933 // New-style support for all custom taxonomies. 4934 if ( ! empty( $postarr['tax_input'] ) ) { 4935 foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { 4936 $taxonomy_obj = get_taxonomy( $taxonomy ); 4937 4938 if ( ! $taxonomy_obj ) { 4939 /* translators: %s: Taxonomy name. */ 4940 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' ); 4941 continue; 4942 } 4943 4944 // array = hierarchical, string = non-hierarchical. 4945 if ( is_array( $tags ) ) { 4946 $tags = array_filter( $tags ); 4947 } 4948 4949 if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { 4950 wp_set_post_terms( $post_id, $tags, $taxonomy ); 4951 } 4952 } 4953 } 4954 4955 if ( ! empty( $postarr['meta_input'] ) ) { 4956 foreach ( $postarr['meta_input'] as $field => $value ) { 4957 update_post_meta( $post_id, $field, $value ); 4958 } 4959 } 4960 4961 $current_guid = get_post_field( 'guid', $post_id ); 4962 4963 // Set GUID. 4964 if ( ! $update && '' === $current_guid ) { 4965 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where ); 4966 } 4967 4968 if ( 'attachment' === $postarr['post_type'] ) { 4969 if ( ! empty( $postarr['file'] ) ) { 4970 update_attached_file( $post_id, $postarr['file'] ); 4971 } 4972 4973 if ( ! empty( $postarr['context'] ) ) { 4974 add_post_meta( $post_id, '_wp_attachment_context', $postarr['context'], true ); 4975 } 4976 } 4977 4978 // Set or remove featured image. 4979 if ( isset( $postarr['_thumbnail_id'] ) ) { 4980 $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; 4981 4982 if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { 4983 if ( wp_attachment_is( 'audio', $post_id ) ) { 4984 $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 4985 } elseif ( wp_attachment_is( 'video', $post_id ) ) { 4986 $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 4987 } 4988 } 4989 4990 if ( $thumbnail_support ) { 4991 $thumbnail_id = (int) $postarr['_thumbnail_id']; 4992 if ( -1 === $thumbnail_id ) { 4993 delete_post_thumbnail( $post_id ); 4994 } else { 4995 set_post_thumbnail( $post_id, $thumbnail_id ); 4996 } 4997 } 4998 } 4999 5000 clean_post_cache( $post_id ); 5001 5002 $post = get_post( $post_id ); 5003 5004 if ( ! empty( $postarr['page_template'] ) ) { 5005 $post->page_template = $postarr['page_template']; 5006 $page_templates = wp_get_theme()->get_page_templates( $post ); 5007 5008 if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { 5009 if ( $wp_error ) { 5010 return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); 5011 } 5012 5013 update_post_meta( $post_id, '_wp_page_template', 'default' ); 5014 } else { 5015 update_post_meta( $post_id, '_wp_page_template', $postarr['page_template'] ); 5016 } 5017 } 5018 5019 if ( 'attachment' !== $postarr['post_type'] ) { 5020 wp_transition_post_status( $data['post_status'], $previous_status, $post ); 5021 } else { 5022 if ( $update ) { 5023 /** 5024 * Fires once an existing attachment has been updated. 5025 * 5026 * @since 2.0.0 5027 * 5028 * @param int $post_id Attachment ID. 5029 */ 5030 do_action( 'edit_attachment', $post_id ); 5031 5032 $post_after = get_post( $post_id ); 5033 5034 /** 5035 * Fires once an existing attachment has been updated. 5036 * 5037 * @since 4.4.0 5038 * 5039 * @param int $post_id Post ID. 5040 * @param WP_Post $post_after Post object following the update. 5041 * @param WP_Post $post_before Post object before the update. 5042 */ 5043 do_action( 'attachment_updated', $post_id, $post_after, $post_before ); 5044 } else { 5045 5046 /** 5047 * Fires once an attachment has been added. 5048 * 5049 * @since 2.0.0 5050 * 5051 * @param int $post_id Attachment ID. 5052 */ 5053 do_action( 'add_attachment', $post_id ); 5054 } 5055 5056 return $post_id; 5057 } 5058 5059 if ( $update ) { 5060 /** 5061 * Fires once an existing post has been updated. 5062 * 5063 * The dynamic portion of the hook name, `$post->post_type`, refers to 5064 * the post type slug. 5065 * 5066 * Possible hook names include: 5067 * 5068 * - `edit_post_post` 5069 * - `edit_post_page` 5070 * 5071 * @since 5.1.0 5072 * 5073 * @param int $post_id Post ID. 5074 * @param WP_Post $post Post object. 5075 */ 5076 do_action( "edit_post_{$post->post_type}", $post_id, $post ); 5077 5078 /** 5079 * Fires once an existing post has been updated. 5080 * 5081 * @since 1.2.0 5082 * 5083 * @param int $post_id Post ID. 5084 * @param WP_Post $post Post object. 5085 */ 5086 do_action( 'edit_post', $post_id, $post ); 5087 5088 $post_after = get_post( $post_id ); 5089 5090 /** 5091 * Fires once an existing post has been updated. 5092 * 5093 * @since 3.0.0 5094 * 5095 * @param int $post_id Post ID. 5096 * @param WP_Post $post_after Post object following the update. 5097 * @param WP_Post $post_before Post object before the update. 5098 */ 5099 do_action( 'post_updated', $post_id, $post_after, $post_before ); 5100 } 5101 5102 /** 5103 * Fires once a post has been saved. 5104 * 5105 * The dynamic portion of the hook name, `$post->post_type`, refers to 5106 * the post type slug. 5107 * 5108 * Possible hook names include: 5109 * 5110 * - `save_post_post` 5111 * - `save_post_page` 5112 * 5113 * @since 3.7.0 5114 * 5115 * @param int $post_id Post ID. 5116 * @param WP_Post $post Post object. 5117 * @param bool $update Whether this is an existing post being updated. 5118 */ 5119 do_action( "save_post_{$post->post_type}", $post_id, $post, $update ); 5120 5121 /** 5122 * Fires once a post has been saved. 5123 * 5124 * @since 1.5.0 5125 * 5126 * @param int $post_id Post ID. 5127 * @param WP_Post $post Post object. 5128 * @param bool $update Whether this is an existing post being updated. 5129 */ 5130 do_action( 'save_post', $post_id, $post, $update ); 5131 5132 /** 5133 * Fires once a post has been saved. 5134 * 5135 * @since 2.0.0 5136 * 5137 * @param int $post_id Post ID. 5138 * @param WP_Post $post Post object. 5139 * @param bool $update Whether this is an existing post being updated. 5140 */ 5141 do_action( 'wp_insert_post', $post_id, $post, $update ); 5142 5143 if ( $fire_after_hooks ) { 5144 wp_after_insert_post( $post, $update, $post_before ); 5145 } 5146 5147 return $post_id; 5148 } 5149 5150 /** 5151 * Updates a post with new post data. 5152 * 5153 * The date does not have to be set for drafts. You can set the date and it will 5154 * not be overridden. 5155 * 5156 * @since 1.0.0 5157 * @since 3.5.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. 5158 * @since 5.6.0 Added the `$fire_after_hooks` parameter. 5159 * 5160 * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, 5161 * objects are not. See wp_insert_post() for accepted arguments. 5162 * Default array. 5163 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 5164 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. 5165 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. 5166 */ 5167 function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) { 5168 if ( is_object( $postarr ) ) { 5169 // Non-escaped post was passed. 5170 $postarr = get_object_vars( $postarr ); 5171 $postarr = wp_slash( $postarr ); 5172 } 5173 5174 // First, get all of the original fields. 5175 $post = get_post( $postarr['ID'], ARRAY_A ); 5176 5177 if ( is_null( $post ) ) { 5178 if ( $wp_error ) { 5179 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 5180 } 5181 return 0; 5182 } 5183 5184 // Escape data pulled from DB. 5185 $post = wp_slash( $post ); 5186 5187 // Passed post category list overwrites existing category list if not empty. 5188 if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] ) 5189 && count( $postarr['post_category'] ) > 0 5190 ) { 5191 $post_cats = $postarr['post_category']; 5192 } else { 5193 $post_cats = $post['post_category']; 5194 } 5195 5196 // Drafts shouldn't be assigned a date unless explicitly done so by the user. 5197 if ( isset( $post['post_status'] ) 5198 && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) 5199 && empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] ) 5200 ) { 5201 $clear_date = true; 5202 } else { 5203 $clear_date = false; 5204 } 5205 5206 // Merge old and new fields with new fields overwriting old ones. 5207 $postarr = array_merge( $post, $postarr ); 5208 $postarr['post_category'] = $post_cats; 5209 if ( $clear_date ) { 5210 $postarr['post_date'] = current_time( 'mysql' ); 5211 $postarr['post_date_gmt'] = ''; 5212 } 5213 5214 if ( 'attachment' === $postarr['post_type'] ) { 5215 return wp_insert_attachment( $postarr, false, 0, $wp_error ); 5216 } 5217 5218 // Discard 'tags_input' parameter if it's the same as existing post tags. 5219 if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) { 5220 $tags = get_the_terms( $postarr['ID'], 'post_tag' ); 5221 $tag_names = array(); 5222 5223 if ( $tags && ! is_wp_error( $tags ) ) { 5224 $tag_names = wp_list_pluck( $tags, 'name' ); 5225 } 5226 5227 if ( $postarr['tags_input'] === $tag_names ) { 5228 unset( $postarr['tags_input'] ); 5229 } 5230 } 5231 5232 return wp_insert_post( $postarr, $wp_error, $fire_after_hooks ); 5233 } 5234 5235 /** 5236 * Publishes a post by transitioning the post status. 5237 * 5238 * @since 2.1.0 5239 * 5240 * @global wpdb $wpdb WordPress database abstraction object. 5241 * 5242 * @param int|WP_Post $post Post ID or post object. 5243 */ 5244 function wp_publish_post( $post ) { 5245 global $wpdb; 5246 5247 $post = get_post( $post ); 5248 5249 if ( ! $post ) { 5250 return; 5251 } 5252 5253 if ( 'publish' === $post->post_status ) { 5254 return; 5255 } 5256 5257 $post_before = get_post( $post->ID ); 5258 5259 // Ensure at least one term is applied for taxonomies with a default term. 5260 foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) { 5261 // Skip taxonomy if no default term is set. 5262 if ( 5263 'category' !== $taxonomy && 5264 empty( $tax_object->default_term ) 5265 ) { 5266 continue; 5267 } 5268 5269 // Do not modify previously set terms. 5270 if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) { 5271 continue; 5272 } 5273 5274 if ( 'category' === $taxonomy ) { 5275 $default_term_id = (int) get_option( 'default_category', 0 ); 5276 } else { 5277 $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 ); 5278 } 5279 5280 if ( ! $default_term_id ) { 5281 continue; 5282 } 5283 wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy ); 5284 } 5285 5286 $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); 5287 5288 clean_post_cache( $post->ID ); 5289 5290 $old_status = $post->post_status; 5291 $post->post_status = 'publish'; 5292 wp_transition_post_status( 'publish', $old_status, $post ); 5293 5294 /** This action is documented in wp-includes/post.php */ 5295 do_action( "edit_post_{$post->post_type}", $post->ID, $post ); 5296 5297 /** This action is documented in wp-includes/post.php */ 5298 do_action( 'edit_post', $post->ID, $post ); 5299 5300 /** This action is documented in wp-includes/post.php */ 5301 do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); 5302 5303 /** This action is documented in wp-includes/post.php */ 5304 do_action( 'save_post', $post->ID, $post, true ); 5305 5306 /** This action is documented in wp-includes/post.php */ 5307 do_action( 'wp_insert_post', $post->ID, $post, true ); 5308 5309 wp_after_insert_post( $post, true, $post_before ); 5310 } 5311 5312 /** 5313 * Publishes future post and make sure post ID has future post status. 5314 * 5315 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron 5316 * from publishing drafts, etc. 5317 * 5318 * @since 2.5.0 5319 * 5320 * @param int|WP_Post $post Post ID or post object. 5321 */ 5322 function check_and_publish_future_post( $post ) { 5323 $post = get_post( $post ); 5324 5325 if ( ! $post ) { 5326 return; 5327 } 5328 5329 if ( 'future' !== $post->post_status ) { 5330 return; 5331 } 5332 5333 $time = strtotime( $post->post_date_gmt . ' GMT' ); 5334 5335 // Uh oh, someone jumped the gun! 5336 if ( $time > time() ) { 5337 wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); // Clear anything else in the system. 5338 wp_schedule_single_event( $time, 'publish_future_post', array( $post->ID ) ); 5339 return; 5340 } 5341 5342 // wp_publish_post() returns no meaningful value. 5343 wp_publish_post( $post->ID ); 5344 } 5345 5346 /** 5347 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date. 5348 * If post_date is not provided, this first checks post_date_gmt if provided, 5349 * then falls back to use the current time. 5350 * 5351 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid 5352 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false. 5353 * 5354 * @since 5.7.0 5355 * 5356 * @param string $post_date The date in mysql format (`Y-m-d H:i:s`). 5357 * @param string $post_date_gmt The GMT date in mysql format (`Y-m-d H:i:s`). 5358 * @return string|false A valid Gregorian-calendar date string, or false on failure. 5359 */ 5360 function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { 5361 // If the date is empty, set the date to now. 5362 if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) { 5363 if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) { 5364 $post_date = current_time( 'mysql' ); 5365 } else { 5366 $post_date = get_date_from_gmt( $post_date_gmt ); 5367 } 5368 } 5369 5370 // Validate the date. 5371 $month = (int) substr( $post_date, 5, 2 ); 5372 $day = (int) substr( $post_date, 8, 2 ); 5373 $year = (int) substr( $post_date, 0, 4 ); 5374 5375 $valid_date = wp_checkdate( $month, $day, $year, $post_date ); 5376 5377 if ( ! $valid_date ) { 5378 return false; 5379 } 5380 return $post_date; 5381 } 5382 5383 /** 5384 * Computes a unique slug for the post, when given the desired slug and some post details. 5385 * 5386 * @since 2.8.0 5387 * 5388 * @global wpdb $wpdb WordPress database abstraction object. 5389 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 5390 * 5391 * @param string $slug The desired slug (post_name). 5392 * @param int $post_id Post ID. 5393 * @param string $post_status No uniqueness checks are made if the post is still draft or pending. 5394 * @param string $post_type Post type. 5395 * @param int $post_parent Post parent ID. 5396 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) 5397 */ 5398 function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent ) { 5399 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) 5400 || ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type 5401 ) { 5402 return $slug; 5403 } 5404 5405 /** 5406 * Filters the post slug before it is generated to be unique. 5407 * 5408 * Returning a non-null value will short-circuit the 5409 * unique slug generation, returning the passed value instead. 5410 * 5411 * @since 5.1.0 5412 * 5413 * @param string|null $override_slug Short-circuit return value. 5414 * @param string $slug The desired slug (post_name). 5415 * @param int $post_id Post ID. 5416 * @param string $post_status The post status. 5417 * @param string $post_type Post type. 5418 * @param int $post_parent Post parent ID. 5419 */ 5420 $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_id, $post_status, $post_type, $post_parent ); 5421 if ( null !== $override_slug ) { 5422 return $override_slug; 5423 } 5424 5425 global $wpdb, $wp_rewrite; 5426 5427 $original_slug = $slug; 5428 5429 $feeds = $wp_rewrite->feeds; 5430 if ( ! is_array( $feeds ) ) { 5431 $feeds = array(); 5432 } 5433 5434 if ( 'attachment' === $post_type ) { 5435 // Attachment slugs must be unique across all types. 5436 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; 5437 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_id ) ); 5438 5439 /** 5440 * Filters whether the post slug would make a bad attachment slug. 5441 * 5442 * @since 3.1.0 5443 * 5444 * @param bool $bad_slug Whether the slug would be bad as an attachment slug. 5445 * @param string $slug The post slug. 5446 */ 5447 $is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ); 5448 5449 if ( $post_name_check 5450 || in_array( $slug, $feeds, true ) || 'embed' === $slug 5451 || $is_bad_attachment_slug 5452 ) { 5453 $suffix = 2; 5454 do { 5455 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 5456 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) ); 5457 ++$suffix; 5458 } while ( $post_name_check ); 5459 $slug = $alt_post_name; 5460 } 5461 } elseif ( is_post_type_hierarchical( $post_type ) ) { 5462 if ( 'nav_menu_item' === $post_type ) { 5463 return $slug; 5464 } 5465 5466 /* 5467 * Page slugs must be unique within their own trees. Pages are in a separate 5468 * namespace than posts so page slugs are allowed to overlap post slugs. 5469 */ 5470 $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"; 5471 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id, $post_parent ) ); 5472 5473 /** 5474 * Filters whether the post slug would make a bad hierarchical post slug. 5475 * 5476 * @since 3.1.0 5477 * 5478 * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. 5479 * @param string $slug The post slug. 5480 * @param string $post_type Post type. 5481 * @param int $post_parent Post parent ID. 5482 */ 5483 $is_bad_hierarchical_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ); 5484 5485 if ( $post_name_check 5486 || in_array( $slug, $feeds, true ) || 'embed' === $slug 5487 || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) 5488 || $is_bad_hierarchical_slug 5489 ) { 5490 $suffix = 2; 5491 do { 5492 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 5493 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id, $post_parent ) ); 5494 ++$suffix; 5495 } while ( $post_name_check ); 5496 $slug = $alt_post_name; 5497 } 5498 } else { 5499 // Post slugs must be unique across all posts. 5500 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; 5501 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id ) ); 5502 5503 $post = get_post( $post_id ); 5504 5505 // Prevent new post slugs that could result in URLs that conflict with date archives. 5506 $conflicts_with_date_archive = false; 5507 if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { 5508 $slug_num = (int) $slug; 5509 5510 if ( $slug_num ) { 5511 $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); 5512 $postname_index = array_search( '%postname%', $permastructs, true ); 5513 5514 /* 5515 * Potential date clashes are as follows: 5516 * 5517 * - Any integer in the first permastruct position could be a year. 5518 * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. 5519 * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. 5520 */ 5521 if ( 0 === $postname_index || 5522 ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || 5523 ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) 5524 ) { 5525 $conflicts_with_date_archive = true; 5526 } 5527 } 5528 } 5529 5530 /** 5531 * Filters whether the post slug would be bad as a flat slug. 5532 * 5533 * @since 3.1.0 5534 * 5535 * @param bool $bad_slug Whether the post slug would be bad as a flat slug. 5536 * @param string $slug The post slug. 5537 * @param string $post_type Post type. 5538 */ 5539 $is_bad_flat_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ); 5540 5541 if ( $post_name_check 5542 || in_array( $slug, $feeds, true ) || 'embed' === $slug 5543 || $conflicts_with_date_archive 5544 || $is_bad_flat_slug 5545 ) { 5546 $suffix = 2; 5547 do { 5548 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 5549 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id ) ); 5550 ++$suffix; 5551 } while ( $post_name_check ); 5552 $slug = $alt_post_name; 5553 } 5554 } 5555 5556 /** 5557 * Filters the unique post slug. 5558 * 5559 * @since 3.3.0 5560 * 5561 * @param string $slug The post slug. 5562 * @param int $post_id Post ID. 5563 * @param string $post_status The post status. 5564 * @param string $post_type Post type. 5565 * @param int $post_parent Post parent ID 5566 * @param string $original_slug The original post slug. 5567 */ 5568 return apply_filters( 'wp_unique_post_slug', $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ); 5569 } 5570 5571 /** 5572 * Truncates a post slug. 5573 * 5574 * @since 3.6.0 5575 * @access private 5576 * 5577 * @see utf8_uri_encode() 5578 * 5579 * @param string $slug The slug to truncate. 5580 * @param int $length Optional. Max length of the slug. Default 200 (characters). 5581 * @return string The truncated slug. 5582 */ 5583 function _truncate_post_slug( $slug, $length = 200 ) { 5584 if ( strlen( $slug ) > $length ) { 5585 $decoded_slug = urldecode( $slug ); 5586 if ( $decoded_slug === $slug ) { 5587 $slug = substr( $slug, 0, $length ); 5588 } else { 5589 $slug = utf8_uri_encode( $decoded_slug, $length, true ); 5590 } 5591 } 5592 5593 return rtrim( $slug, '-' ); 5594 } 5595 5596 /** 5597 * Adds tags to a post. 5598 * 5599 * @see wp_set_post_tags() 5600 * 5601 * @since 2.3.0 5602 * 5603 * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 5604 * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags 5605 * separated by commas. Default empty. 5606 * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. 5607 */ 5608 function wp_add_post_tags( $post_id = 0, $tags = '' ) { 5609 return wp_set_post_tags( $post_id, $tags, true ); 5610 } 5611 5612 /** 5613 * Sets the tags for a post. 5614 * 5615 * @since 2.3.0 5616 * 5617 * @see wp_set_object_terms() 5618 * 5619 * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 5620 * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags 5621 * separated by commas. Default empty. 5622 * @param bool $append Optional. If true, don't delete existing tags, just add on. If false, 5623 * replace the tags with the new tags. Default false. 5624 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 5625 */ 5626 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { 5627 return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); 5628 } 5629 5630 /** 5631 * Sets the terms for a post. 5632 * 5633 * @since 2.8.0 5634 * 5635 * @see wp_set_object_terms() 5636 * 5637 * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 5638 * @param string|array $terms Optional. An array of terms to set for the post, or a string of terms 5639 * separated by commas. Hierarchical taxonomies must always pass IDs rather 5640 * than names so that children with the same names but different parents 5641 * aren't confused. Default empty. 5642 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. 5643 * @param bool $append Optional. If true, don't delete existing terms, just add on. If false, 5644 * replace the terms with the new terms. Default false. 5645 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 5646 */ 5647 function wp_set_post_terms( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false ) { 5648 $post_id = (int) $post_id; 5649 5650 if ( ! $post_id ) { 5651 return false; 5652 } 5653 5654 if ( empty( $terms ) ) { 5655 $terms = array(); 5656 } 5657 5658 if ( ! is_array( $terms ) ) { 5659 $comma = _x( ',', 'tag delimiter' ); 5660 if ( ',' !== $comma ) { 5661 $terms = str_replace( $comma, ',', $terms ); 5662 } 5663 $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); 5664 } 5665 5666 /* 5667 * Hierarchical taxonomies must always pass IDs rather than names so that 5668 * children with the same names but different parents aren't confused. 5669 */ 5670 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 5671 $terms = array_unique( array_map( 'intval', $terms ) ); 5672 } 5673 5674 return wp_set_object_terms( $post_id, $terms, $taxonomy, $append ); 5675 } 5676 5677 /** 5678 * Sets categories for a post. 5679 * 5680 * If no categories are provided, the default category is used. 5681 * 5682 * @since 2.1.0 5683 * 5684 * @param int $post_id Optional. The Post ID. Does not default to the ID 5685 * of the global $post. Default 0. 5686 * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category. 5687 * Default empty array. 5688 * @param bool $append If true, don't delete existing categories, just add on. 5689 * If false, replace the categories with the new categories. 5690 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. 5691 */ 5692 function wp_set_post_categories( $post_id = 0, $post_categories = array(), $append = false ) { 5693 $post_id = (int) $post_id; 5694 $post_type = get_post_type( $post_id ); 5695 $post_status = get_post_status( $post_id ); 5696 5697 // If $post_categories isn't already an array, make it one. 5698 $post_categories = (array) $post_categories; 5699 5700 if ( empty( $post_categories ) ) { 5701 /** 5702 * Filters post types (in addition to 'post') that require a default category. 5703 * 5704 * @since 5.5.0 5705 * 5706 * @param string[] $post_types An array of post type names. Default empty array. 5707 */ 5708 $default_category_post_types = apply_filters( 'default_category_post_types', array() ); 5709 5710 // Regular posts always require a default category. 5711 $default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) ); 5712 5713 if ( in_array( $post_type, $default_category_post_types, true ) 5714 && is_object_in_taxonomy( $post_type, 'category' ) 5715 && 'auto-draft' !== $post_status 5716 ) { 5717 $post_categories = array( get_option( 'default_category' ) ); 5718 $append = false; 5719 } else { 5720 $post_categories = array(); 5721 } 5722 } elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) { 5723 return true; 5724 } 5725 5726 return wp_set_post_terms( $post_id, $post_categories, 'category', $append ); 5727 } 5728 5729 /** 5730 * Fires actions related to the transitioning of a post's status. 5731 * 5732 * When a post is saved, the post status is "transitioned" from one status to another, 5733 * though this does not always mean the status has actually changed before and after 5734 * the save. This function fires a number of action hooks related to that transition: 5735 * the generic {@see 'transition_post_status'} action, as well as the dynamic hooks 5736 * {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note 5737 * that the function does not transition the post object in the database. 5738 * 5739 * For instance: When publishing a post for the first time, the post status may transition 5740 * from 'draft' – or some other status – to 'publish'. However, if a post is already 5741 * published and is simply being updated, the "old" and "new" statuses may both be 'publish' 5742 * before and after the transition. 5743 * 5744 * @since 2.3.0 5745 * 5746 * @param string $new_status Transition to this post status. 5747 * @param string $old_status Previous post status. 5748 * @param WP_Post $post Post data. 5749 */ 5750 function wp_transition_post_status( $new_status, $old_status, $post ) { 5751 /** 5752 * Fires when a post is transitioned from one status to another. 5753 * 5754 * @since 2.3.0 5755 * 5756 * @param string $new_status New post status. 5757 * @param string $old_status Old post status. 5758 * @param WP_Post $post Post object. 5759 */ 5760 do_action( 'transition_post_status', $new_status, $old_status, $post ); 5761 5762 /** 5763 * Fires when a post is transitioned from one status to another. 5764 * 5765 * The dynamic portions of the hook name, `$new_status` and `$old_status`, 5766 * refer to the old and new post statuses, respectively. 5767 * 5768 * Possible hook names include: 5769 * 5770 * - `draft_to_publish` 5771 * - `publish_to_trash` 5772 * - `pending_to_draft` 5773 * 5774 * @since 2.3.0 5775 * 5776 * @param WP_Post $post Post object. 5777 */ 5778 do_action( "{$old_status}_to_{$new_status}", $post ); 5779 5780 /** 5781 * Fires when a post is transitioned from one status to another. 5782 * 5783 * The dynamic portions of the hook name, `$new_status` and `$post->post_type`, 5784 * refer to the new post status and post type, respectively. 5785 * 5786 * Possible hook names include: 5787 * 5788 * - `draft_post` 5789 * - `future_post` 5790 * - `pending_post` 5791 * - `private_post` 5792 * - `publish_post` 5793 * - `trash_post` 5794 * - `draft_page` 5795 * - `future_page` 5796 * - `pending_page` 5797 * - `private_page` 5798 * - `publish_page` 5799 * - `trash_page` 5800 * - `publish_attachment` 5801 * - `trash_attachment` 5802 * 5803 * Please note: When this action is hooked using a particular post status (like 5804 * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is 5805 * first transitioned to that status from something else, as well as upon 5806 * subsequent post updates (old and new status are both the same). 5807 * 5808 * Therefore, if you are looking to only fire a callback when a post is first 5809 * transitioned to a status, use the {@see 'transition_post_status'} hook instead. 5810 * 5811 * @since 2.3.0 5812 * @since 5.9.0 Added `$old_status` parameter. 5813 * 5814 * @param int $post_id Post ID. 5815 * @param WP_Post $post Post object. 5816 * @param string $old_status Old post status. 5817 */ 5818 do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status ); 5819 } 5820 5821 /** 5822 * Fires actions after a post, its terms and meta data has been saved. 5823 * 5824 * @since 5.6.0 5825 * 5826 * @param int|WP_Post $post The post ID or object that has been saved. 5827 * @param bool $update Whether this is an existing post being updated. 5828 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior 5829 * to the update for updated posts. 5830 */ 5831 function wp_after_insert_post( $post, $update, $post_before ) { 5832 $post = get_post( $post ); 5833 5834 if ( ! $post ) { 5835 return; 5836 } 5837 5838 $post_id = $post->ID; 5839 5840 /** 5841 * Fires once a post, its terms and meta data has been saved. 5842 * 5843 * @since 5.6.0 5844 * 5845 * @param int $post_id Post ID. 5846 * @param WP_Post $post Post object. 5847 * @param bool $update Whether this is an existing post being updated. 5848 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior 5849 * to the update for updated posts. 5850 */ 5851 do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before ); 5852 } 5853 5854 // 5855 // Comment, trackback, and pingback functions. 5856 // 5857 5858 /** 5859 * Adds a URL to those already pinged. 5860 * 5861 * @since 1.5.0 5862 * @since 4.7.0 `$post` can be a WP_Post object. 5863 * @since 4.7.0 `$uri` can be an array of URIs. 5864 * 5865 * @global wpdb $wpdb WordPress database abstraction object. 5866 * 5867 * @param int|WP_Post $post Post ID or post object. 5868 * @param string|array $uri Ping URI or array of URIs. 5869 * @return int|false How many rows were updated. 5870 */ 5871 function add_ping( $post, $uri ) { 5872 global $wpdb; 5873 5874 $post = get_post( $post ); 5875 5876 if ( ! $post ) { 5877 return false; 5878 } 5879 5880 $pung = trim( $post->pinged ); 5881 $pung = preg_split( '/\s/', $pung ); 5882 5883 if ( is_array( $uri ) ) { 5884 $pung = array_merge( $pung, $uri ); 5885 } else { 5886 $pung[] = $uri; 5887 } 5888 $new = implode( "\n", $pung ); 5889 5890 /** 5891 * Filters the new ping URL to add for the given post. 5892 * 5893 * @since 2.0.0 5894 * 5895 * @param string $new New ping URL to add. 5896 */ 5897 $new = apply_filters( 'add_ping', $new ); 5898 5899 $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) ); 5900 clean_post_cache( $post->ID ); 5901 return $return; 5902 } 5903 5904 /** 5905 * Retrieves enclosures already enclosed for a post. 5906 * 5907 * @since 1.5.0 5908 * 5909 * @param int $post_id Post ID. 5910 * @return string[] Array of enclosures for the given post. 5911 */ 5912 function get_enclosed( $post_id ) { 5913 $custom_fields = get_post_custom( $post_id ); 5914 $pung = array(); 5915 if ( ! is_array( $custom_fields ) ) { 5916 return $pung; 5917 } 5918 5919 foreach ( $custom_fields as $key => $val ) { 5920 if ( 'enclosure' !== $key || ! is_array( $val ) ) { 5921 continue; 5922 } 5923 foreach ( $val as $enc ) { 5924 $enclosure = explode( "\n", $enc ); 5925 $pung[] = trim( $enclosure[0] ); 5926 } 5927 } 5928 5929 /** 5930 * Filters the list of enclosures already enclosed for the given post. 5931 * 5932 * @since 2.0.0 5933 * 5934 * @param string[] $pung Array of enclosures for the given post. 5935 * @param int $post_id Post ID. 5936 */ 5937 return apply_filters( 'get_enclosed', $pung, $post_id ); 5938 } 5939 5940 /** 5941 * Retrieves URLs already pinged for a post. 5942 * 5943 * @since 1.5.0 5944 * 5945 * @since 4.7.0 `$post` can be a WP_Post object. 5946 * 5947 * @param int|WP_Post $post Post ID or object. 5948 * @return string[]|false Array of URLs already pinged for the given post, false if the post is not found. 5949 */ 5950 function get_pung( $post ) { 5951 $post = get_post( $post ); 5952 5953 if ( ! $post ) { 5954 return false; 5955 } 5956 5957 $pung = trim( $post->pinged ); 5958 $pung = preg_split( '/\s/', $pung ); 5959 5960 /** 5961 * Filters the list of already-pinged URLs for the given post. 5962 * 5963 * @since 2.0.0 5964 * 5965 * @param string[] $pung Array of URLs already pinged for the given post. 5966 */ 5967 return apply_filters( 'get_pung', $pung ); 5968 } 5969 5970 /** 5971 * Retrieves URLs that need to be pinged. 5972 * 5973 * @since 1.5.0 5974 * @since 4.7.0 `$post` can be a WP_Post object. 5975 * 5976 * @param int|WP_Post $post Post ID or post object. 5977 * @return string[]|false List of URLs yet to ping. 5978 */ 5979 function get_to_ping( $post ) { 5980 $post = get_post( $post ); 5981 5982 if ( ! $post ) { 5983 return false; 5984 } 5985 5986 $to_ping = sanitize_trackback_urls( $post->to_ping ); 5987 $to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY ); 5988 5989 /** 5990 * Filters the list of URLs yet to ping for the given post. 5991 * 5992 * @since 2.0.0 5993 * 5994 * @param string[] $to_ping List of URLs yet to ping. 5995 */ 5996 return apply_filters( 'get_to_ping', $to_ping ); 5997 } 5998 5999 /** 6000 * Does trackbacks for a list of URLs. 6001 * 6002 * @since 1.0.0 6003 * 6004 * @param string $tb_list Comma separated list of URLs. 6005 * @param int $post_id Post ID. 6006 */ 6007 function trackback_url_list( $tb_list, $post_id ) { 6008 if ( ! empty( $tb_list ) ) { 6009 // Get post data. 6010 $postdata = get_post( $post_id, ARRAY_A ); 6011 6012 // Form an excerpt. 6013 $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); 6014 6015 if ( strlen( $excerpt ) > 255 ) { 6016 $excerpt = substr( $excerpt, 0, 252 ) . '…'; 6017 } 6018 6019 $trackback_urls = explode( ',', $tb_list ); 6020 foreach ( (array) $trackback_urls as $tb_url ) { 6021 $tb_url = trim( $tb_url ); 6022 trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); 6023 } 6024 } 6025 } 6026 6027 // 6028 // Page functions. 6029 // 6030 6031 /** 6032 * Gets a list of page IDs. 6033 * 6034 * @since 2.0.0 6035 * 6036 * @global wpdb $wpdb WordPress database abstraction object. 6037 * 6038 * @return string[] List of page IDs as strings. 6039 */ 6040 function get_all_page_ids() { 6041 global $wpdb; 6042 6043 $page_ids = wp_cache_get( 'all_page_ids', 'posts' ); 6044 if ( ! is_array( $page_ids ) ) { 6045 $page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" ); 6046 wp_cache_add( 'all_page_ids', $page_ids, 'posts' ); 6047 } 6048 6049 return $page_ids; 6050 } 6051 6052 /** 6053 * Retrieves page data given a page ID or page object. 6054 * 6055 * Use get_post() instead of get_page(). 6056 * 6057 * @since 1.5.1 6058 * @deprecated 3.5.0 Use get_post() 6059 * 6060 * @param int|WP_Post $page Page object or page ID. Passed by reference. 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 $filter Optional. How the return value should be filtered. Accepts 'raw', 6065 * 'edit', 'db', 'display'. Default 'raw'. 6066 * @return WP_Post|array|null WP_Post or array on success, null on failure. 6067 */ 6068 function get_page( $page, $output = OBJECT, $filter = 'raw' ) { 6069 return get_post( $page, $output, $filter ); 6070 } 6071 6072 /** 6073 * Retrieves a page given its path. 6074 * 6075 * @since 2.1.0 6076 * 6077 * @global wpdb $wpdb WordPress database abstraction object. 6078 * 6079 * @param string $page_path Page path. 6080 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which 6081 * correspond to a WP_Post object, an associative array, or a numeric array, 6082 * respectively. Default OBJECT. 6083 * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. 6084 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. 6085 */ 6086 function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { 6087 global $wpdb; 6088 6089 $last_changed = wp_cache_get_last_changed( 'posts' ); 6090 6091 $hash = md5( $page_path . serialize( $post_type ) ); 6092 $cache_key = "get_page_by_path:$hash:$last_changed"; 6093 $cached = wp_cache_get( $cache_key, 'post-queries' ); 6094 if ( false !== $cached ) { 6095 // Special case: '0' is a bad `$page_path`. 6096 if ( '0' === $cached || 0 === $cached ) { 6097 return; 6098 } else { 6099 return get_post( $cached, $output ); 6100 } 6101 } 6102 6103 $page_path = rawurlencode( urldecode( $page_path ) ); 6104 $page_path = str_replace( '%2F', '/', $page_path ); 6105 $page_path = str_replace( '%20', ' ', $page_path ); 6106 $parts = explode( '/', trim( $page_path, '/' ) ); 6107 $parts = array_map( 'sanitize_title_for_query', $parts ); 6108 $escaped_parts = esc_sql( $parts ); 6109 6110 $in_string = "'" . implode( "','", $escaped_parts ) . "'"; 6111 6112 if ( is_array( $post_type ) ) { 6113 $post_types = $post_type; 6114 } else { 6115 $post_types = array( $post_type, 'attachment' ); 6116 } 6117 6118 $post_types = esc_sql( $post_types ); 6119 $post_type_in_string = "'" . implode( "','", $post_types ) . "'"; 6120 $sql = " 6121 SELECT ID, post_name, post_parent, post_type 6122 FROM $wpdb->posts 6123 WHERE post_name IN ($in_string) 6124 AND post_type IN ($post_type_in_string) 6125 "; 6126 6127 $pages = $wpdb->get_results( $sql, OBJECT_K ); 6128 6129 $revparts = array_reverse( $parts ); 6130 6131 $found_id = 0; 6132 foreach ( (array) $pages as $page ) { 6133 if ( $page->post_name === $revparts[0] ) { 6134 $count = 0; 6135 $p = $page; 6136 6137 /* 6138 * Loop through the given path parts from right to left, 6139 * ensuring each matches the post ancestry. 6140 */ 6141 while ( 0 !== (int) $p->post_parent && isset( $pages[ $p->post_parent ] ) ) { 6142 ++$count; 6143 $parent = $pages[ $p->post_parent ]; 6144 if ( ! isset( $revparts[ $count ] ) || $parent->post_name !== $revparts[ $count ] ) { 6145 break; 6146 } 6147 $p = $parent; 6148 } 6149 6150 if ( 0 === (int) $p->post_parent 6151 && count( $revparts ) === $count + 1 6152 && $p->post_name === $revparts[ $count ] 6153 ) { 6154 $found_id = $page->ID; 6155 if ( $page->post_type === $post_type ) { 6156 break; 6157 } 6158 } 6159 } 6160 } 6161 6162 // We cache misses as well as hits. 6163 wp_cache_set( $cache_key, $found_id, 'post-queries' ); 6164 6165 if ( $found_id ) { 6166 return get_post( $found_id, $output ); 6167 } 6168 6169 return null; 6170 } 6171 6172 /** 6173 * Identifies descendants of a given page ID in a list of page objects. 6174 * 6175 * Descendants are identified from the `$pages` array passed to the function. No database queries are performed. 6176 * 6177 * @since 1.5.1 6178 * 6179 * @param int $page_id Page ID. 6180 * @param WP_Post[] $pages List of page objects from which descendants should be identified. 6181 * @return WP_Post[] List of page children. 6182 */ 6183 function get_page_children( $page_id, $pages ) { 6184 // Build a hash of ID -> children. 6185 $children = array(); 6186 foreach ( (array) $pages as $page ) { 6187 $children[ (int) $page->post_parent ][] = $page; 6188 } 6189 6190 $page_list = array(); 6191 6192 // Start the search by looking at immediate children. 6193 if ( isset( $children[ $page_id ] ) ) { 6194 // Always start at the end of the stack in order to preserve original `$pages` order. 6195 $to_look = array_reverse( $children[ $page_id ] ); 6196 6197 while ( $to_look ) { 6198 $p = array_pop( $to_look ); 6199 $page_list[] = $p; 6200 if ( isset( $children[ $p->ID ] ) ) { 6201 foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { 6202 // Append to the `$to_look` stack to descend the tree. 6203 $to_look[] = $child; 6204 } 6205 } 6206 } 6207 } 6208 6209 return $page_list; 6210 } 6211 6212 /** 6213 * Orders the pages with children under parents in a flat list. 6214 * 6215 * It uses auxiliary structure to hold parent-children relationships and 6216 * runs in O(N) complexity 6217 * 6218 * @since 2.0.0 6219 * 6220 * @param WP_Post[] $pages Posts array (passed by reference). 6221 * @param int $page_id Optional. Parent page ID. Default 0. 6222 * @return string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents. 6223 */ 6224 function get_page_hierarchy( &$pages, $page_id = 0 ) { 6225 if ( empty( $pages ) ) { 6226 return array(); 6227 } 6228 6229 $children = array(); 6230 foreach ( (array) $pages as $p ) { 6231 $parent_id = (int) $p->post_parent; 6232 $children[ $parent_id ][] = $p; 6233 } 6234 6235 $result = array(); 6236 _page_traverse_name( $page_id, $children, $result ); 6237 6238 return $result; 6239 } 6240 6241 /** 6242 * Traverses and return all the nested children post names of a root page. 6243 * 6244 * $children contains parent-children relations 6245 * 6246 * @since 2.9.0 6247 * @access private 6248 * 6249 * @see _page_traverse_name() 6250 * 6251 * @param int $page_id Page ID. 6252 * @param array $children Parent-children relations (passed by reference). 6253 * @param string[] $result Array of page names keyed by ID (passed by reference). 6254 */ 6255 function _page_traverse_name( $page_id, &$children, &$result ) { 6256 if ( isset( $children[ $page_id ] ) ) { 6257 foreach ( (array) $children[ $page_id ] as $child ) { 6258 $result[ $child->ID ] = $child->post_name; 6259 _page_traverse_name( $child->ID, $children, $result ); 6260 } 6261 } 6262 } 6263 6264 /** 6265 * Builds the URI path for a page. 6266 * 6267 * Sub pages will be in the "directory" under the parent page post name. 6268 * 6269 * @since 1.5.0 6270 * @since 4.6.0 The `$page` parameter was made optional. 6271 * 6272 * @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. 6273 * @return string|false Page URI, false on error. 6274 */ 6275 function get_page_uri( $page = 0 ) { 6276 if ( ! $page instanceof WP_Post ) { 6277 $page = get_post( $page ); 6278 } 6279 6280 if ( ! $page ) { 6281 return false; 6282 } 6283 6284 $uri = $page->post_name; 6285 6286 foreach ( $page->ancestors as $parent ) { 6287 $parent = get_post( $parent ); 6288 if ( $parent && $parent->post_name ) { 6289 $uri = $parent->post_name . '/' . $uri; 6290 } 6291 } 6292 6293 /** 6294 * Filters the URI for a page. 6295 * 6296 * @since 4.4.0 6297 * 6298 * @param string $uri Page URI. 6299 * @param WP_Post $page Page object. 6300 */ 6301 return apply_filters( 'get_page_uri', $uri, $page ); 6302 } 6303 6304 /** 6305 * Retrieves an array of pages (or hierarchical post type items). 6306 * 6307 * @since 1.5.0 6308 * @since 6.3.0 Use WP_Query internally. 6309 * 6310 * @param array|string $args { 6311 * Optional. Array or string of arguments to retrieve pages. 6312 * 6313 * @type int $child_of Page ID to return child and grandchild pages of. Note: The value 6314 * of `$hierarchical` has no bearing on whether `$child_of` returns 6315 * hierarchical results. Default 0, or no restriction. 6316 * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. 6317 * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', 6318 * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', 6319 * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. 6320 * 'post_' can be omitted for any values that start with it. 6321 * Default 'post_title'. 6322 * @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with 6323 * `$child_of` also being false, both arguments will be disregarded. 6324 * Default true. 6325 * @type int[] $exclude Array of page IDs to exclude. Default empty array. 6326 * @type int[] $include Array of page IDs to include. Cannot be used with `$child_of`, 6327 * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. 6328 * Default empty array. 6329 * @type string $meta_key Only include pages with this meta key. Default empty. 6330 * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. 6331 * Default empty. 6332 * @type string $authors A comma-separated list of author IDs. Default empty. 6333 * @type int $parent Page ID to return direct children of. Default -1, or no restriction. 6334 * @type string|int[] $exclude_tree Comma-separated string or array of page IDs to exclude. 6335 * Default empty array. 6336 * @type int $number The number of pages to return. Default 0, or all pages. 6337 * @type int $offset The number of pages to skip before returning. Requires `$number`. 6338 * Default 0. 6339 * @type string $post_type The post type to query. Default 'page'. 6340 * @type string|array $post_status A comma-separated list or array of post statuses to include. 6341 * Default 'publish'. 6342 * } 6343 * @return WP_Post[]|false Array of pages (or hierarchical post type items). Boolean false if the 6344 * specified post type is not hierarchical or the specified status is not 6345 * supported by the post type. 6346 */ 6347 function get_pages( $args = array() ) { 6348 $defaults = array( 6349 'child_of' => 0, 6350 'sort_order' => 'ASC', 6351 'sort_column' => 'post_title', 6352 'hierarchical' => 1, 6353 'exclude' => array(), 6354 'include' => array(), 6355 'meta_key' => '', 6356 'meta_value' => '', 6357 'authors' => '', 6358 'parent' => -1, 6359 'exclude_tree' => array(), 6360 'number' => '', 6361 'offset' => 0, 6362 'post_type' => 'page', 6363 'post_status' => 'publish', 6364 ); 6365 6366 $parsed_args = wp_parse_args( $args, $defaults ); 6367 6368 $number = (int) $parsed_args['number']; 6369 $offset = (int) $parsed_args['offset']; 6370 $child_of = (int) $parsed_args['child_of']; 6371 $hierarchical = $parsed_args['hierarchical']; 6372 $exclude = $parsed_args['exclude']; 6373 $meta_key = $parsed_args['meta_key']; 6374 $meta_value = $parsed_args['meta_value']; 6375 $parent = $parsed_args['parent']; 6376 $post_status = $parsed_args['post_status']; 6377 6378 // Make sure the post type is hierarchical. 6379 $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); 6380 if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) { 6381 return false; 6382 } 6383 6384 if ( $parent > 0 && ! $child_of ) { 6385 $hierarchical = false; 6386 } 6387 6388 // Make sure we have a valid post status. 6389 if ( ! is_array( $post_status ) ) { 6390 $post_status = explode( ',', $post_status ); 6391 } 6392 if ( array_diff( $post_status, get_post_stati() ) ) { 6393 return false; 6394 } 6395 6396 $query_args = array( 6397 'orderby' => 'post_title', 6398 'order' => 'ASC', 6399 'post__not_in' => wp_parse_id_list( $exclude ), 6400 'meta_key' => $meta_key, 6401 'meta_value' => $meta_value, 6402 'posts_per_page' => -1, 6403 'offset' => $offset, 6404 'post_type' => $parsed_args['post_type'], 6405 'post_status' => $post_status, 6406 'update_post_term_cache' => false, 6407 'update_post_meta_cache' => false, 6408 'ignore_sticky_posts' => true, 6409 'no_found_rows' => true, 6410 ); 6411 6412 if ( ! empty( $parsed_args['include'] ) ) { 6413 $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. 6414 $parent = -1; 6415 unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] ); 6416 $hierarchical = false; 6417 $query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] ); 6418 } 6419 6420 if ( ! empty( $parsed_args['authors'] ) ) { 6421 $post_authors = wp_parse_list( $parsed_args['authors'] ); 6422 6423 if ( ! empty( $post_authors ) ) { 6424 $query_args['author__in'] = array(); 6425 foreach ( $post_authors as $post_author ) { 6426 // Do we have an author id or an author login? 6427 if ( 0 === (int) $post_author ) { 6428 $post_author = get_user_by( 'login', $post_author ); 6429 if ( empty( $post_author ) ) { 6430 continue; 6431 } 6432 if ( empty( $post_author->ID ) ) { 6433 continue; 6434 } 6435 $post_author = $post_author->ID; 6436 } 6437 $query_args['author__in'][] = (int) $post_author; 6438 } 6439 } 6440 } 6441 6442 if ( is_array( $parent ) ) { 6443 $post_parent__in = array_map( 'absint', (array) $parent ); 6444 if ( ! empty( $post_parent__in ) ) { 6445 $query_args['post_parent__in'] = $post_parent__in; 6446 } 6447 } elseif ( $parent >= 0 ) { 6448 $query_args['post_parent'] = $parent; 6449 } 6450 6451 /* 6452 * Maintain backward compatibility for `sort_column` key. 6453 * Additionally to `WP_Query`, it has been supporting the `post_modified_gmt` field, so this logic will translate 6454 * it to `post_modified` which should result in the same order given the two dates in the fields match. 6455 */ 6456 $orderby = wp_parse_list( $parsed_args['sort_column'] ); 6457 $orderby = array_map( 6458 static function ( $orderby_field ) { 6459 $orderby_field = trim( $orderby_field ); 6460 if ( 'post_modified_gmt' === $orderby_field || 'modified_gmt' === $orderby_field ) { 6461 $orderby_field = str_replace( '_gmt', '', $orderby_field ); 6462 } 6463 return $orderby_field; 6464 }, 6465 $orderby 6466 ); 6467 if ( $orderby ) { 6468 $query_args['orderby'] = array_fill_keys( $orderby, $parsed_args['sort_order'] ); 6469 } 6470 6471 $order = $parsed_args['sort_order']; 6472 if ( $order ) { 6473 $query_args['order'] = $order; 6474 } 6475 6476 if ( ! empty( $number ) ) { 6477 $query_args['posts_per_page'] = $number; 6478 } 6479 6480 /** 6481 * Filters query arguments passed to WP_Query in get_pages. 6482 * 6483 * @since 6.3.0 6484 * 6485 * @param array $query_args Array of arguments passed to WP_Query. 6486 * @param array $parsed_args Array of get_pages() arguments. 6487 */ 6488 $query_args = apply_filters( 'get_pages_query_args', $query_args, $parsed_args ); 6489 6490 $pages = new WP_Query(); 6491 $pages = $pages->query( $query_args ); 6492 6493 if ( $child_of || $hierarchical ) { 6494 $pages = get_page_children( $child_of, $pages ); 6495 } 6496 6497 if ( ! empty( $parsed_args['exclude_tree'] ) ) { 6498 $exclude = wp_parse_id_list( $parsed_args['exclude_tree'] ); 6499 foreach ( $exclude as $id ) { 6500 $children = get_page_children( $id, $pages ); 6501 foreach ( $children as $child ) { 6502 $exclude[] = $child->ID; 6503 } 6504 } 6505 6506 $num_pages = count( $pages ); 6507 for ( $i = 0; $i < $num_pages; $i++ ) { 6508 if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) { 6509 unset( $pages[ $i ] ); 6510 } 6511 } 6512 } 6513 6514 /** 6515 * Filters the retrieved list of pages. 6516 * 6517 * @since 2.1.0 6518 * 6519 * @param WP_Post[] $pages Array of page objects. 6520 * @param array $parsed_args Array of get_pages() arguments. 6521 */ 6522 return apply_filters( 'get_pages', $pages, $parsed_args ); 6523 } 6524 6525 // 6526 // Attachment functions. 6527 // 6528 6529 /** 6530 * Determines whether an attachment URI is local and really an attachment. 6531 * 6532 * For more information on this and similar theme functions, check out 6533 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 6534 * Conditional Tags} article in the Theme Developer Handbook. 6535 * 6536 * @since 2.0.0 6537 * 6538 * @param string $url URL to check 6539 * @return bool True on success, false on failure. 6540 */ 6541 function is_local_attachment( $url ) { 6542 if ( ! str_contains( $url, home_url() ) ) { 6543 return false; 6544 } 6545 if ( str_contains( $url, home_url( '/?attachment_id=' ) ) ) { 6546 return true; 6547 } 6548 6549 $id = url_to_postid( $url ); 6550 if ( $id ) { 6551 $post = get_post( $id ); 6552 if ( 'attachment' === $post->post_type ) { 6553 return true; 6554 } 6555 } 6556 return false; 6557 } 6558 6559 /** 6560 * Inserts an attachment. 6561 * 6562 * If you set the 'ID' in the $args parameter, it will mean that you are 6563 * updating and attempt to update the attachment. You can also set the 6564 * attachment name or title by setting the key 'post_name' or 'post_title'. 6565 * 6566 * You can set the dates for the attachment manually by setting the 'post_date' 6567 * and 'post_date_gmt' keys' values. 6568 * 6569 * By default, the comments will use the default settings for whether the 6570 * comments are allowed. You can close them manually or keep them open by 6571 * setting the value for the 'comment_status' key. 6572 * 6573 * @since 2.0.0 6574 * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. 6575 * @since 5.6.0 Added the `$fire_after_hooks` parameter. 6576 * 6577 * @see wp_insert_post() 6578 * 6579 * @param string|array $args Arguments for inserting an attachment. 6580 * @param string|false $file Optional. Filename. Default false. 6581 * @param int $parent_post_id Optional. Parent post ID or 0 for no parent. Default 0. 6582 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 6583 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. 6584 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. 6585 */ 6586 function wp_insert_attachment( $args, $file = false, $parent_post_id = 0, $wp_error = false, $fire_after_hooks = true ) { 6587 $defaults = array( 6588 'file' => $file, 6589 'post_parent' => 0, 6590 ); 6591 6592 $data = wp_parse_args( $args, $defaults ); 6593 6594 if ( ! empty( $parent_post_id ) ) { 6595 $data['post_parent'] = $parent_post_id; 6596 } 6597 6598 $data['post_type'] = 'attachment'; 6599 6600 return wp_insert_post( $data, $wp_error, $fire_after_hooks ); 6601 } 6602 6603 /** 6604 * Trashes or deletes an attachment. 6605 * 6606 * When an attachment is permanently deleted, the file will also be removed. 6607 * Deletion removes all post meta fields, taxonomy, comments, etc. associated 6608 * with the attachment (except the main post). 6609 * 6610 * The attachment is moved to the Trash instead of permanently deleted unless Trash 6611 * for media is disabled, item is already in the Trash, or $force_delete is true. 6612 * 6613 * @since 2.0.0 6614 * 6615 * @global wpdb $wpdb WordPress database abstraction object. 6616 * 6617 * @param int $post_id Attachment ID. 6618 * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. 6619 * Default false. 6620 * @return WP_Post|false|null Post data on success, false or null on failure. 6621 */ 6622 function wp_delete_attachment( $post_id, $force_delete = false ) { 6623 global $wpdb; 6624 6625 $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) ); 6626 6627 if ( ! $post ) { 6628 return $post; 6629 } 6630 6631 $post = get_post( $post ); 6632 6633 if ( 'attachment' !== $post->post_type ) { 6634 return false; 6635 } 6636 6637 if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) { 6638 return wp_trash_post( $post_id ); 6639 } 6640 6641 /** 6642 * Filters whether an attachment deletion should take place. 6643 * 6644 * @since 5.5.0 6645 * 6646 * @param WP_Post|false|null $delete Whether to go forward with deletion. 6647 * @param WP_Post $post Post object. 6648 * @param bool $force_delete Whether to bypass the Trash. 6649 */ 6650 $check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete ); 6651 if ( null !== $check ) { 6652 return $check; 6653 } 6654 6655 delete_post_meta( $post_id, '_wp_trash_meta_status' ); 6656 delete_post_meta( $post_id, '_wp_trash_meta_time' ); 6657 6658 $meta = wp_get_attachment_metadata( $post_id ); 6659 $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); 6660 $file = get_attached_file( $post_id ); 6661 6662 if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) { 6663 clean_dirsize_cache( $file ); 6664 } 6665 6666 /** 6667 * Fires before an attachment is deleted, at the start of wp_delete_attachment(). 6668 * 6669 * @since 2.0.0 6670 * @since 5.5.0 Added the `$post` parameter. 6671 * 6672 * @param int $post_id Attachment ID. 6673 * @param WP_Post $post Post object. 6674 */ 6675 do_action( 'delete_attachment', $post_id, $post ); 6676 6677 wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) ); 6678 wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); 6679 6680 // Delete all for any posts. 6681 delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); 6682 6683 wp_defer_comment_counting( true ); 6684 6685 $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 ) ); 6686 foreach ( $comment_ids as $comment_id ) { 6687 wp_delete_comment( $comment_id, true ); 6688 } 6689 6690 wp_defer_comment_counting( false ); 6691 6692 $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) ); 6693 foreach ( $post_meta_ids as $mid ) { 6694 delete_metadata_by_mid( 'post', $mid ); 6695 } 6696 6697 /** This action is documented in wp-includes/post.php */ 6698 do_action( 'delete_post', $post_id, $post ); 6699 $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); 6700 if ( ! $result ) { 6701 return false; 6702 } 6703 /** This action is documented in wp-includes/post.php */ 6704 do_action( 'deleted_post', $post_id, $post ); 6705 6706 wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); 6707 6708 clean_post_cache( $post ); 6709 6710 return $post; 6711 } 6712 6713 /** 6714 * Deletes all files that belong to the given attachment. 6715 * 6716 * @since 4.9.7 6717 * 6718 * @global wpdb $wpdb WordPress database abstraction object. 6719 * 6720 * @param int $post_id Attachment ID. 6721 * @param array $meta The attachment's meta data. 6722 * @param array $backup_sizes The meta data for the attachment's backup images. 6723 * @param string $file Absolute path to the attachment's file. 6724 * @return bool True on success, false on failure. 6725 */ 6726 function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { 6727 global $wpdb; 6728 6729 $uploadpath = wp_get_upload_dir(); 6730 $deleted = true; 6731 6732 if ( ! empty( $meta['thumb'] ) ) { 6733 // Don't delete the thumb if another attachment uses it. 6734 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 ) ) ) { 6735 $thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file ); 6736 6737 if ( ! empty( $thumbfile ) ) { 6738 $thumbfile = path_join( $uploadpath['basedir'], $thumbfile ); 6739 $thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) ); 6740 6741 if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) { 6742 $deleted = false; 6743 } 6744 } 6745 } 6746 } 6747 6748 // Remove intermediate and backup images if there are any. 6749 if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { 6750 $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); 6751 6752 foreach ( $meta['sizes'] as $size => $sizeinfo ) { 6753 $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file ); 6754 6755 if ( ! empty( $intermediate_file ) ) { 6756 $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file ); 6757 6758 if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) { 6759 $deleted = false; 6760 } 6761 } 6762 } 6763 } 6764 6765 if ( ! empty( $meta['original_image'] ) ) { 6766 if ( empty( $intermediate_dir ) ) { 6767 $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); 6768 } 6769 6770 $original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file ); 6771 6772 if ( ! empty( $original_image ) ) { 6773 $original_image = path_join( $uploadpath['basedir'], $original_image ); 6774 6775 if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) { 6776 $deleted = false; 6777 } 6778 } 6779 } 6780 6781 if ( is_array( $backup_sizes ) ) { 6782 $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) ); 6783 6784 foreach ( $backup_sizes as $size ) { 6785 $del_file = path_join( dirname( $meta['file'] ), $size['file'] ); 6786 6787 if ( ! empty( $del_file ) ) { 6788 $del_file = path_join( $uploadpath['basedir'], $del_file ); 6789 6790 if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) { 6791 $deleted = false; 6792 } 6793 } 6794 } 6795 } 6796 6797 if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) { 6798 $deleted = false; 6799 } 6800 6801 return $deleted; 6802 } 6803 6804 /** 6805 * Retrieves attachment metadata for attachment ID. 6806 * 6807 * @since 2.1.0 6808 * @since 6.0.0 The `$filesize` value was added to the returned array. 6809 * 6810 * @param int $attachment_id Attachment post ID. Defaults to global $post. 6811 * @param bool $unfiltered Optional. If true, filters are not run. Default false. 6812 * @return array|false { 6813 * Attachment metadata. False on failure. 6814 * 6815 * @type int $width The width of the attachment. 6816 * @type int $height The height of the attachment. 6817 * @type string $file The file path relative to `wp-content/uploads`. 6818 * @type array $sizes Keys are size slugs, each value is an array containing 6819 * 'file', 'width', 'height', and 'mime-type'. 6820 * @type array $image_meta Image metadata. 6821 * @type int $filesize File size of the attachment. 6822 * } 6823 */ 6824 function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { 6825 $attachment_id = (int) $attachment_id; 6826 6827 if ( ! $attachment_id ) { 6828 $post = get_post(); 6829 6830 if ( ! $post ) { 6831 return false; 6832 } 6833 6834 $attachment_id = $post->ID; 6835 } 6836 6837 $data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 6838 6839 if ( ! $data ) { 6840 return false; 6841 } 6842 6843 if ( $unfiltered ) { 6844 return $data; 6845 } 6846 6847 /** 6848 * Filters the attachment meta data. 6849 * 6850 * @since 2.1.0 6851 * 6852 * @param array $data Array of meta data for the given attachment. 6853 * @param int $attachment_id Attachment post ID. 6854 */ 6855 return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id ); 6856 } 6857 6858 /** 6859 * Updates metadata for an attachment. 6860 * 6861 * @since 2.1.0 6862 * 6863 * @param int $attachment_id Attachment post ID. 6864 * @param array $data Attachment meta data. 6865 * @return int|false False if $post is invalid. 6866 */ 6867 function wp_update_attachment_metadata( $attachment_id, $data ) { 6868 $attachment_id = (int) $attachment_id; 6869 6870 $post = get_post( $attachment_id ); 6871 6872 if ( ! $post ) { 6873 return false; 6874 } 6875 6876 /** 6877 * Filters the updated attachment meta data. 6878 * 6879 * @since 2.1.0 6880 * 6881 * @param array $data Array of updated attachment meta data. 6882 * @param int $attachment_id Attachment post ID. 6883 */ 6884 $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); 6885 if ( $data ) { 6886 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); 6887 } else { 6888 return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); 6889 } 6890 } 6891 6892 /** 6893 * Retrieves the URL for an attachment. 6894 * 6895 * @since 2.1.0 6896 * 6897 * @global string $pagenow The filename of the current screen. 6898 * 6899 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post. 6900 * @return string|false Attachment URL, otherwise false. 6901 */ 6902 function wp_get_attachment_url( $attachment_id = 0 ) { 6903 global $pagenow; 6904 6905 $attachment_id = (int) $attachment_id; 6906 6907 $post = get_post( $attachment_id ); 6908 6909 if ( ! $post ) { 6910 return false; 6911 } 6912 6913 if ( 'attachment' !== $post->post_type ) { 6914 return false; 6915 } 6916 6917 $url = ''; 6918 // Get attached file. 6919 $file = get_post_meta( $post->ID, '_wp_attached_file', true ); 6920 if ( $file ) { 6921 // Get upload directory. 6922 $uploads = wp_get_upload_dir(); 6923 if ( $uploads && false === $uploads['error'] ) { 6924 // Check that the upload base exists in the file location. 6925 if ( str_starts_with( $file, $uploads['basedir'] ) ) { 6926 // Replace file location with url location. 6927 $url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); 6928 } elseif ( str_contains( $file, 'wp-content/uploads' ) ) { 6929 // Get the directory name relative to the basedir (back compat for pre-2.7 uploads). 6930 $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); 6931 } else { 6932 // It's a newly-uploaded file, therefore $file is relative to the basedir. 6933 $url = $uploads['baseurl'] . "/$file"; 6934 } 6935 } 6936 } 6937 6938 /* 6939 * If any of the above options failed, Fallback on the GUID as used pre-2.7, 6940 * not recommended to rely upon this. 6941 */ 6942 if ( ! $url ) { 6943 $url = get_the_guid( $post->ID ); 6944 } 6945 6946 // On SSL front end, URLs should be HTTPS. 6947 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) { 6948 $url = set_url_scheme( $url ); 6949 } 6950 6951 /** 6952 * Filters the attachment URL. 6953 * 6954 * @since 2.1.0 6955 * 6956 * @param string $url URL for the given attachment. 6957 * @param int $attachment_id Attachment post ID. 6958 */ 6959 $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); 6960 6961 if ( ! $url ) { 6962 return false; 6963 } 6964 6965 return $url; 6966 } 6967 6968 /** 6969 * Retrieves the caption for an attachment. 6970 * 6971 * @since 4.6.0 6972 * 6973 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. 6974 * @return string|false Attachment caption on success, false on failure. 6975 */ 6976 function wp_get_attachment_caption( $post_id = 0 ) { 6977 $post_id = (int) $post_id; 6978 $post = get_post( $post_id ); 6979 6980 if ( ! $post ) { 6981 return false; 6982 } 6983 6984 if ( 'attachment' !== $post->post_type ) { 6985 return false; 6986 } 6987 6988 $caption = $post->post_excerpt; 6989 6990 /** 6991 * Filters the attachment caption. 6992 * 6993 * @since 4.6.0 6994 * 6995 * @param string $caption Caption for the given attachment. 6996 * @param int $post_id Attachment ID. 6997 */ 6998 return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); 6999 } 7000 7001 /** 7002 * Retrieves URL for an attachment thumbnail. 7003 * 7004 * @since 2.1.0 7005 * @since 6.1.0 Changed to use wp_get_attachment_image_url(). 7006 * 7007 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. 7008 * @return string|false Thumbnail URL on success, false on failure. 7009 */ 7010 function wp_get_attachment_thumb_url( $post_id = 0 ) { 7011 $post_id = (int) $post_id; 7012 7013 /* 7014 * This uses image_downsize() which also looks for the (very) old format $image_meta['thumb'] 7015 * when the newer format $image_meta['sizes']['thumbnail'] doesn't exist. 7016 */ 7017 $thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' ); 7018 7019 if ( empty( $thumbnail_url ) ) { 7020 return false; 7021 } 7022 7023 /** 7024 * Filters the attachment thumbnail URL. 7025 * 7026 * @since 2.1.0 7027 * 7028 * @param string $thumbnail_url URL for the attachment thumbnail. 7029 * @param int $post_id Attachment ID. 7030 */ 7031 return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id ); 7032 } 7033 7034 /** 7035 * Verifies an attachment is of a given type. 7036 * 7037 * @since 4.2.0 7038 * 7039 * @param string $type Attachment type. Accepts `image`, `audio`, `video`, or a file extension. 7040 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. 7041 * @return bool True if an accepted type or a matching file extension, false otherwise. 7042 */ 7043 function wp_attachment_is( $type, $post = null ) { 7044 $post = get_post( $post ); 7045 7046 if ( ! $post ) { 7047 return false; 7048 } 7049 7050 $file = get_attached_file( $post->ID ); 7051 7052 if ( ! $file ) { 7053 return false; 7054 } 7055 7056 if ( str_starts_with( $post->post_mime_type, $type . '/' ) ) { 7057 return true; 7058 } 7059 7060 $check = wp_check_filetype( $file ); 7061 7062 if ( empty( $check['ext'] ) ) { 7063 return false; 7064 } 7065 7066 $ext = $check['ext']; 7067 7068 if ( 'import' !== $post->post_mime_type ) { 7069 return $type === $ext; 7070 } 7071 7072 switch ( $type ) { 7073 case 'image': 7074 $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif', 'heic' ); 7075 return in_array( $ext, $image_exts, true ); 7076 7077 case 'audio': 7078 return in_array( $ext, wp_get_audio_extensions(), true ); 7079 7080 case 'video': 7081 return in_array( $ext, wp_get_video_extensions(), true ); 7082 7083 default: 7084 return $type === $ext; 7085 } 7086 } 7087 7088 /** 7089 * Determines whether an attachment is an image. 7090 * 7091 * For more information on this and similar theme functions, check out 7092 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 7093 * Conditional Tags} article in the Theme Developer Handbook. 7094 * 7095 * @since 2.1.0 7096 * @since 4.2.0 Modified into wrapper for wp_attachment_is() and 7097 * allowed WP_Post object to be passed. 7098 * 7099 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. 7100 * @return bool Whether the attachment is an image. 7101 */ 7102 function wp_attachment_is_image( $post = null ) { 7103 return wp_attachment_is( 'image', $post ); 7104 } 7105 7106 /** 7107 * Retrieves the icon for a MIME type or attachment. 7108 * 7109 * @since 2.1.0 7110 * @since 6.5.0 Added the `$preferred_ext` parameter. 7111 * 7112 * @param string|int $mime MIME type or attachment ID. 7113 * @param string $preferred_ext File format to prefer in return. Default '.png'. 7114 * @return string|false Icon, false otherwise. 7115 */ 7116 function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) { 7117 if ( ! is_numeric( $mime ) ) { 7118 $icon = wp_cache_get( "mime_type_icon_$mime" ); 7119 } 7120 7121 // Check if preferred file format variable is present and is a validly formatted file extension. 7122 if ( ! empty( $preferred_ext ) && is_string( $preferred_ext ) && ! str_starts_with( $preferred_ext, '.' ) ) { 7123 $preferred_ext = '.' . strtolower( $preferred_ext ); 7124 } 7125 7126 $post_id = 0; 7127 if ( empty( $icon ) ) { 7128 $post_mimes = array(); 7129 if ( is_numeric( $mime ) ) { 7130 $mime = (int) $mime; 7131 $post = get_post( $mime ); 7132 if ( $post ) { 7133 $post_id = (int) $post->ID; 7134 $file = get_attached_file( $post_id ); 7135 $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file ); 7136 if ( ! empty( $ext ) ) { 7137 $post_mimes[] = $ext; 7138 $ext_type = wp_ext2type( $ext ); 7139 if ( $ext_type ) { 7140 $post_mimes[] = $ext_type; 7141 } 7142 } 7143 $mime = $post->post_mime_type; 7144 } else { 7145 $mime = 0; 7146 } 7147 } else { 7148 $post_mimes[] = $mime; 7149 } 7150 7151 $icon_files = wp_cache_get( 'icon_files' ); 7152 7153 if ( ! is_array( $icon_files ) ) { 7154 /** 7155 * Filters the icon directory path. 7156 * 7157 * @since 2.0.0 7158 * 7159 * @param string $path Icon directory absolute path. 7160 */ 7161 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 7162 7163 /** 7164 * Filters the icon directory URI. 7165 * 7166 * @since 2.0.0 7167 * 7168 * @param string $uri Icon directory URI. 7169 */ 7170 $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); 7171 7172 /** 7173 * Filters the array of icon directory URIs. 7174 * 7175 * @since 2.5.0 7176 * 7177 * @param string[] $uris Array of icon directory URIs keyed by directory absolute path. 7178 */ 7179 $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); 7180 $icon_files = array(); 7181 $all_icons = array(); 7182 while ( $dirs ) { 7183 $keys = array_keys( $dirs ); 7184 $dir = array_shift( $keys ); 7185 $uri = array_shift( $dirs ); 7186 $dh = opendir( $dir ); 7187 if ( $dh ) { 7188 while ( false !== $file = readdir( $dh ) ) { 7189 $file = wp_basename( $file ); 7190 if ( str_starts_with( $file, '.' ) ) { 7191 continue; 7192 } 7193 7194 $ext = strtolower( substr( $file, -4 ) ); 7195 if ( ! in_array( $ext, array( '.svg', '.png', '.gif', '.jpg' ), true ) ) { 7196 if ( is_dir( "$dir/$file" ) ) { 7197 $dirs[ "$dir/$file" ] = "$uri/$file"; 7198 } 7199 continue; 7200 } 7201 $all_icons[ "$dir/$file" ] = "$uri/$file"; 7202 if ( $ext === $preferred_ext ) { 7203 $icon_files[ "$dir/$file" ] = "$uri/$file"; 7204 } 7205 } 7206 closedir( $dh ); 7207 } 7208 } 7209 // If directory only contained icons of a non-preferred format, return those. 7210 if ( empty( $icon_files ) ) { 7211 $icon_files = $all_icons; 7212 } 7213 wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); 7214 } 7215 7216 $types = array(); 7217 // Icon wp_basename - extension = MIME wildcard. 7218 foreach ( $icon_files as $file => $uri ) { 7219 $types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ]; 7220 } 7221 7222 if ( ! empty( $mime ) ) { 7223 $post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) ); 7224 $post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 ); 7225 $post_mimes[] = str_replace( '/', '_', $mime ); 7226 } 7227 7228 $matches = wp_match_mime_types( array_keys( $types ), $post_mimes ); 7229 $matches['default'] = array( 'default' ); 7230 7231 foreach ( $matches as $match => $wilds ) { 7232 foreach ( $wilds as $wild ) { 7233 if ( ! isset( $types[ $wild ] ) ) { 7234 continue; 7235 } 7236 7237 $icon = $types[ $wild ]; 7238 if ( ! is_numeric( $mime ) ) { 7239 wp_cache_add( "mime_type_icon_$mime", $icon ); 7240 } 7241 break 2; 7242 } 7243 } 7244 } 7245 7246 /** 7247 * Filters the mime type icon. 7248 * 7249 * @since 2.1.0 7250 * 7251 * @param string $icon Path to the mime type icon. 7252 * @param string $mime Mime type. 7253 * @param int $post_id Attachment ID. Will equal 0 if the function passed 7254 * the mime type. 7255 */ 7256 return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); 7257 } 7258 7259 /** 7260 * Checks for changed slugs for published post objects and save the old slug. 7261 * 7262 * The function is used when a post object of any type is updated, 7263 * by comparing the current and previous post objects. 7264 * 7265 * If the slug was changed and not already part of the old slugs then it will be 7266 * added to the post meta field ('_wp_old_slug') for storing old slugs for that 7267 * post. 7268 * 7269 * The most logically usage of this function is redirecting changed post objects, so 7270 * that those that linked to an changed post will be redirected to the new post. 7271 * 7272 * @since 2.1.0 7273 * 7274 * @param int $post_id Post ID. 7275 * @param WP_Post $post The post object. 7276 * @param WP_Post $post_before The previous post object. 7277 */ 7278 function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { 7279 // Don't bother if it hasn't changed. 7280 if ( $post->post_name === $post_before->post_name ) { 7281 return; 7282 } 7283 7284 // We're only concerned with published, non-hierarchical objects. 7285 if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) ) 7286 || is_post_type_hierarchical( $post->post_type ) 7287 ) { 7288 return; 7289 } 7290 7291 $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); 7292 7293 // If we haven't added this old slug before, add it now. 7294 if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) { 7295 add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name ); 7296 } 7297 7298 // If the new slug was used previously, delete it from the list. 7299 if ( in_array( $post->post_name, $old_slugs, true ) ) { 7300 delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); 7301 } 7302 } 7303 7304 /** 7305 * Checks for changed dates for published post objects and save the old date. 7306 * 7307 * The function is used when a post object of any type is updated, 7308 * by comparing the current and previous post objects. 7309 * 7310 * If the date was changed and not already part of the old dates then it will be 7311 * added to the post meta field ('_wp_old_date') for storing old dates for that 7312 * post. 7313 * 7314 * The most logically usage of this function is redirecting changed post objects, so 7315 * that those that linked to an changed post will be redirected to the new post. 7316 * 7317 * @since 4.9.3 7318 * 7319 * @param int $post_id Post ID. 7320 * @param WP_Post $post The post object. 7321 * @param WP_Post $post_before The previous post object. 7322 */ 7323 function wp_check_for_changed_dates( $post_id, $post, $post_before ) { 7324 $previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) ); 7325 $new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) ); 7326 7327 // Don't bother if it hasn't changed. 7328 if ( $new_date === $previous_date ) { 7329 return; 7330 } 7331 7332 // We're only concerned with published, non-hierarchical objects. 7333 if ( ! ( 'publish' === $post->post_status || ( 'attachment' === $post->post_type && 'inherit' === $post->post_status ) ) 7334 || is_post_type_hierarchical( $post->post_type ) 7335 ) { 7336 return; 7337 } 7338 7339 $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' ); 7340 7341 // If we haven't added this old date before, add it now. 7342 if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) { 7343 add_post_meta( $post_id, '_wp_old_date', $previous_date ); 7344 } 7345 7346 // If the new slug was used previously, delete it from the list. 7347 if ( in_array( $new_date, $old_dates, true ) ) { 7348 delete_post_meta( $post_id, '_wp_old_date', $new_date ); 7349 } 7350 } 7351 7352 /** 7353 * Retrieves the private post SQL based on capability. 7354 * 7355 * This function provides a standardized way to appropriately select on the 7356 * post_status of a post type. The function will return a piece of SQL code 7357 * that can be added to a WHERE clause; this SQL is constructed to allow all 7358 * published posts, and all private posts to which the user has access. 7359 * 7360 * @since 2.2.0 7361 * @since 4.3.0 Added the ability to pass an array to `$post_type`. 7362 * 7363 * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. 7364 * @return string SQL code that can be added to a where clause. 7365 */ 7366 function get_private_posts_cap_sql( $post_type ) { 7367 return get_posts_by_author_sql( $post_type, false ); 7368 } 7369 7370 /** 7371 * Retrieves the post SQL based on capability, author, and type. 7372 * 7373 * @since 3.0.0 7374 * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. 7375 * 7376 * @see get_private_posts_cap_sql() 7377 * @global wpdb $wpdb WordPress database abstraction object. 7378 * 7379 * @param string|string[] $post_type Single post type or an array of post types. 7380 * @param bool $full Optional. Returns a full WHERE statement instead of just 7381 * an 'andalso' term. Default true. 7382 * @param int $post_author Optional. Query posts having a single author ID. Default null. 7383 * @param bool $public_only Optional. Only return public posts. Skips cap checks for 7384 * $current_user. Default false. 7385 * @return string SQL WHERE code that can be added to a query. 7386 */ 7387 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { 7388 global $wpdb; 7389 7390 if ( is_array( $post_type ) ) { 7391 $post_types = $post_type; 7392 } else { 7393 $post_types = array( $post_type ); 7394 } 7395 7396 $post_type_clauses = array(); 7397 foreach ( $post_types as $post_type ) { 7398 $post_type_obj = get_post_type_object( $post_type ); 7399 7400 if ( ! $post_type_obj ) { 7401 continue; 7402 } 7403 7404 /** 7405 * Filters the capability to read private posts for a custom post type 7406 * when generating SQL for getting posts by author. 7407 * 7408 * @since 2.2.0 7409 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". 7410 * 7411 * @param string $cap Capability. 7412 */ 7413 $cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' ); 7414 7415 if ( ! $cap ) { 7416 $cap = current_user_can( $post_type_obj->cap->read_private_posts ); 7417 } 7418 7419 // Only need to check the cap if $public_only is false. 7420 $post_status_sql = "post_status = 'publish'"; 7421 7422 if ( false === $public_only ) { 7423 if ( $cap ) { 7424 // Does the user have the capability to view private posts? Guess so. 7425 $post_status_sql .= " OR post_status = 'private'"; 7426 } elseif ( is_user_logged_in() ) { 7427 // Users can view their own private posts. 7428 $id = get_current_user_id(); 7429 if ( null === $post_author || ! $full ) { 7430 $post_status_sql .= " OR post_status = 'private' AND post_author = $id"; 7431 } elseif ( $id === (int) $post_author ) { 7432 $post_status_sql .= " OR post_status = 'private'"; 7433 } // Else none. 7434 } // Else none. 7435 } 7436 7437 $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )"; 7438 } 7439 7440 if ( empty( $post_type_clauses ) ) { 7441 return $full ? 'WHERE 1 = 0' : '1 = 0'; 7442 } 7443 7444 $sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )'; 7445 7446 if ( null !== $post_author ) { 7447 $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); 7448 } 7449 7450 if ( $full ) { 7451 $sql = 'WHERE ' . $sql; 7452 } 7453 7454 return $sql; 7455 } 7456 7457 /** 7458 * Retrieves the most recent time that a post on the site was published. 7459 * 7460 * The server timezone is the default and is the difference between GMT and 7461 * server time. The 'blog' value is the date when the last post was posted. 7462 * The 'gmt' is when the last post was posted in GMT formatted date. 7463 * 7464 * @since 0.71 7465 * @since 4.4.0 The `$post_type` argument was added. 7466 * 7467 * @param string $timezone Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'. 7468 * 'server' uses the server's internal timezone. 7469 * 'blog' uses the `post_date` field, which proxies to the timezone set for the site. 7470 * 'gmt' uses the `post_date_gmt` field. 7471 * Default 'server'. 7472 * @param string $post_type Optional. The post type to check. Default 'any'. 7473 * @return string The date of the last post, or false on failure. 7474 */ 7475 function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { 7476 $lastpostdate = _get_last_post_time( $timezone, 'date', $post_type ); 7477 7478 /** 7479 * Filters the most recent time that a post on the site was published. 7480 * 7481 * @since 2.3.0 7482 * @since 5.5.0 Added the `$post_type` parameter. 7483 * 7484 * @param string|false $lastpostdate The most recent time that a post was published, 7485 * in 'Y-m-d H:i:s' format. False on failure. 7486 * @param string $timezone Location to use for getting the post published date. 7487 * See get_lastpostdate() for accepted `$timezone` values. 7488 * @param string $post_type The post type to check. 7489 */ 7490 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type ); 7491 } 7492 7493 /** 7494 * Gets the most recent time that a post on the site was modified. 7495 * 7496 * The server timezone is the default and is the difference between GMT and 7497 * server time. The 'blog' value is just when the last post was modified. 7498 * The 'gmt' is when the last post was modified in GMT time. 7499 * 7500 * @since 1.2.0 7501 * @since 4.4.0 The `$post_type` argument was added. 7502 * 7503 * @param string $timezone Optional. The timezone for the timestamp. See get_lastpostdate() 7504 * for information on accepted values. 7505 * Default 'server'. 7506 * @param string $post_type Optional. The post type to check. Default 'any'. 7507 * @return string The timestamp in 'Y-m-d H:i:s' format, or false on failure. 7508 */ 7509 function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { 7510 /** 7511 * Pre-filter the return value of get_lastpostmodified() before the query is run. 7512 * 7513 * @since 4.4.0 7514 * 7515 * @param string|false $lastpostmodified The most recent time that a post was modified, 7516 * in 'Y-m-d H:i:s' format, or false. Returning anything 7517 * other than false will short-circuit the function. 7518 * @param string $timezone Location to use for getting the post modified date. 7519 * See get_lastpostdate() for accepted `$timezone` values. 7520 * @param string $post_type The post type to check. 7521 */ 7522 $lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); 7523 7524 if ( false !== $lastpostmodified ) { 7525 return $lastpostmodified; 7526 } 7527 7528 $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); 7529 $lastpostdate = get_lastpostdate( $timezone, $post_type ); 7530 7531 if ( $lastpostdate > $lastpostmodified ) { 7532 $lastpostmodified = $lastpostdate; 7533 } 7534 7535 /** 7536 * Filters the most recent time that a post on the site was modified. 7537 * 7538 * @since 2.3.0 7539 * @since 5.5.0 Added the `$post_type` parameter. 7540 * 7541 * @param string|false $lastpostmodified The most recent time that a post was modified, 7542 * in 'Y-m-d H:i:s' format. False on failure. 7543 * @param string $timezone Location to use for getting the post modified date. 7544 * See get_lastpostdate() for accepted `$timezone` values. 7545 * @param string $post_type The post type to check. 7546 */ 7547 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone, $post_type ); 7548 } 7549 7550 /** 7551 * Gets the timestamp of the last time any post was modified or published. 7552 * 7553 * @since 3.1.0 7554 * @since 4.4.0 The `$post_type` argument was added. 7555 * @access private 7556 * 7557 * @global wpdb $wpdb WordPress database abstraction object. 7558 * 7559 * @param string $timezone The timezone for the timestamp. See get_lastpostdate(). 7560 * for information on accepted values. 7561 * @param string $field Post field to check. Accepts 'date' or 'modified'. 7562 * @param string $post_type Optional. The post type to check. Default 'any'. 7563 * @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure. 7564 */ 7565 function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { 7566 global $wpdb; 7567 7568 if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) { 7569 return false; 7570 } 7571 7572 $timezone = strtolower( $timezone ); 7573 7574 $key = "lastpost{$field}:$timezone"; 7575 if ( 'any' !== $post_type ) { 7576 $key .= ':' . sanitize_key( $post_type ); 7577 } 7578 7579 $date = wp_cache_get( $key, 'timeinfo' ); 7580 if ( false !== $date ) { 7581 return $date; 7582 } 7583 7584 if ( 'any' === $post_type ) { 7585 $post_types = get_post_types( array( 'public' => true ) ); 7586 array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); 7587 $post_types = "'" . implode( "', '", $post_types ) . "'"; 7588 } else { 7589 $post_types = "'" . sanitize_key( $post_type ) . "'"; 7590 } 7591 7592 switch ( $timezone ) { 7593 case 'gmt': 7594 $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" ); 7595 break; 7596 case 'blog': 7597 $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" ); 7598 break; 7599 case 'server': 7600 $add_seconds_server = gmdate( 'Z' ); 7601 $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" ); 7602 break; 7603 } 7604 7605 if ( $date ) { 7606 wp_cache_set( $key, $date, 'timeinfo' ); 7607 7608 return $date; 7609 } 7610 7611 return false; 7612 } 7613 7614 /** 7615 * Updates posts in cache. 7616 * 7617 * @since 1.5.1 7618 * 7619 * @param WP_Post[] $posts Array of post objects (passed by reference). 7620 */ 7621 function update_post_cache( &$posts ) { 7622 if ( ! $posts ) { 7623 return; 7624 } 7625 7626 $data = array(); 7627 foreach ( $posts as $post ) { 7628 if ( empty( $post->filter ) || 'raw' !== $post->filter ) { 7629 $post = sanitize_post( $post, 'raw' ); 7630 } 7631 $data[ $post->ID ] = $post; 7632 } 7633 wp_cache_add_multiple( $data, 'posts' ); 7634 } 7635 7636 /** 7637 * Will clean the post in the cache. 7638 * 7639 * Cleaning means delete from the cache of the post. Will call to clean the term 7640 * object cache associated with the post ID. 7641 * 7642 * This function not run if $_wp_suspend_cache_invalidation is not empty. See 7643 * wp_suspend_cache_invalidation(). 7644 * 7645 * @since 2.0.0 7646 * 7647 * @global bool $_wp_suspend_cache_invalidation 7648 * 7649 * @param int|WP_Post $post Post ID or post object to remove from the cache. 7650 */ 7651 function clean_post_cache( $post ) { 7652 global $_wp_suspend_cache_invalidation; 7653 7654 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { 7655 return; 7656 } 7657 7658 $post = get_post( $post ); 7659 7660 if ( ! $post ) { 7661 return; 7662 } 7663 7664 wp_cache_delete( $post->ID, 'posts' ); 7665 wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' ); 7666 wp_cache_delete( $post->ID, 'post_meta' ); 7667 7668 clean_object_term_cache( $post->ID, $post->post_type ); 7669 7670 wp_cache_delete( 'wp_get_archives', 'general' ); 7671 7672 /** 7673 * Fires immediately after the given post's cache is cleaned. 7674 * 7675 * @since 2.5.0 7676 * 7677 * @param int $post_id Post ID. 7678 * @param WP_Post $post Post object. 7679 */ 7680 do_action( 'clean_post_cache', $post->ID, $post ); 7681 7682 if ( 'page' === $post->post_type ) { 7683 wp_cache_delete( 'all_page_ids', 'posts' ); 7684 7685 /** 7686 * Fires immediately after the given page's cache is cleaned. 7687 * 7688 * @since 2.5.0 7689 * 7690 * @param int $post_id Post ID. 7691 */ 7692 do_action( 'clean_page_cache', $post->ID ); 7693 } 7694 7695 wp_cache_set_posts_last_changed(); 7696 } 7697 7698 /** 7699 * Updates post, term, and metadata caches for a list of post objects. 7700 * 7701 * @since 1.5.0 7702 * 7703 * @param WP_Post[] $posts Array of post objects (passed by reference). 7704 * @param string $post_type Optional. Post type. Default 'post'. 7705 * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. 7706 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 7707 */ 7708 function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { 7709 // No point in doing all this work if we didn't match any posts. 7710 if ( ! $posts ) { 7711 return; 7712 } 7713 7714 update_post_cache( $posts ); 7715 7716 $post_ids = array(); 7717 foreach ( $posts as $post ) { 7718 $post_ids[] = $post->ID; 7719 } 7720 7721 if ( ! $post_type ) { 7722 $post_type = 'any'; 7723 } 7724 7725 if ( $update_term_cache ) { 7726 if ( is_array( $post_type ) ) { 7727 $ptypes = $post_type; 7728 } elseif ( 'any' === $post_type ) { 7729 $ptypes = array(); 7730 // Just use the post_types in the supplied posts. 7731 foreach ( $posts as $post ) { 7732 $ptypes[] = $post->post_type; 7733 } 7734 $ptypes = array_unique( $ptypes ); 7735 } else { 7736 $ptypes = array( $post_type ); 7737 } 7738 7739 if ( ! empty( $ptypes ) ) { 7740 update_object_term_cache( $post_ids, $ptypes ); 7741 } 7742 } 7743 7744 if ( $update_meta_cache ) { 7745 update_postmeta_cache( $post_ids ); 7746 } 7747 } 7748 7749 /** 7750 * Updates post author user caches for a list of post objects. 7751 * 7752 * @since 6.1.0 7753 * 7754 * @param WP_Post[] $posts Array of post objects. 7755 */ 7756 function update_post_author_caches( $posts ) { 7757 /* 7758 * cache_users() is a pluggable function so is not available prior 7759 * to the `plugins_loaded` hook firing. This is to ensure against 7760 * fatal errors when the function is not available. 7761 */ 7762 if ( ! function_exists( 'cache_users' ) ) { 7763 return; 7764 } 7765 7766 $author_ids = wp_list_pluck( $posts, 'post_author' ); 7767 $author_ids = array_map( 'absint', $author_ids ); 7768 $author_ids = array_unique( array_filter( $author_ids ) ); 7769 7770 cache_users( $author_ids ); 7771 } 7772 7773 /** 7774 * Updates parent post caches for a list of post objects. 7775 * 7776 * @since 6.1.0 7777 * 7778 * @param WP_Post[] $posts Array of post objects. 7779 */ 7780 function update_post_parent_caches( $posts ) { 7781 $parent_ids = wp_list_pluck( $posts, 'post_parent' ); 7782 $parent_ids = array_map( 'absint', $parent_ids ); 7783 $parent_ids = array_unique( array_filter( $parent_ids ) ); 7784 7785 if ( ! empty( $parent_ids ) ) { 7786 _prime_post_caches( $parent_ids, false ); 7787 } 7788 } 7789 7790 /** 7791 * Updates metadata cache for a list of post IDs. 7792 * 7793 * Performs SQL query to retrieve the metadata for the post IDs and updates the 7794 * metadata cache for the posts. Therefore, the functions, which call this 7795 * function, do not need to perform SQL queries on their own. 7796 * 7797 * @since 2.1.0 7798 * 7799 * @param int[] $post_ids Array of post IDs. 7800 * @return array|false An array of metadata on success, false if there is nothing to update. 7801 */ 7802 function update_postmeta_cache( $post_ids ) { 7803 return update_meta_cache( 'post', $post_ids ); 7804 } 7805 7806 /** 7807 * Will clean the attachment in the cache. 7808 * 7809 * Cleaning means delete from the cache. Optionally will clean the term 7810 * object cache associated with the attachment ID. 7811 * 7812 * This function will not run if $_wp_suspend_cache_invalidation is not empty. 7813 * 7814 * @since 3.0.0 7815 * 7816 * @global bool $_wp_suspend_cache_invalidation 7817 * 7818 * @param int $id The attachment ID in the cache to clean. 7819 * @param bool $clean_terms Optional. Whether to clean terms cache. Default false. 7820 */ 7821 function clean_attachment_cache( $id, $clean_terms = false ) { 7822 global $_wp_suspend_cache_invalidation; 7823 7824 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { 7825 return; 7826 } 7827 7828 $id = (int) $id; 7829 7830 wp_cache_delete( $id, 'posts' ); 7831 wp_cache_delete( $id, 'post_meta' ); 7832 7833 if ( $clean_terms ) { 7834 clean_object_term_cache( $id, 'attachment' ); 7835 } 7836 7837 /** 7838 * Fires after the given attachment's cache is cleaned. 7839 * 7840 * @since 3.0.0 7841 * 7842 * @param int $id Attachment ID. 7843 */ 7844 do_action( 'clean_attachment_cache', $id ); 7845 } 7846 7847 // 7848 // Hooks. 7849 // 7850 7851 /** 7852 * Hook for managing future post transitions to published. 7853 * 7854 * @since 2.3.0 7855 * @access private 7856 * 7857 * @see wp_clear_scheduled_hook() 7858 * @global wpdb $wpdb WordPress database abstraction object. 7859 * 7860 * @param string $new_status New post status. 7861 * @param string $old_status Previous post status. 7862 * @param WP_Post $post Post object. 7863 */ 7864 function _transition_post_status( $new_status, $old_status, $post ) { 7865 global $wpdb; 7866 7867 if ( 'publish' !== $old_status && 'publish' === $new_status ) { 7868 // Reset GUID if transitioning to publish and it is empty. 7869 if ( '' === get_the_guid( $post->ID ) ) { 7870 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); 7871 } 7872 7873 /** 7874 * Fires when a post's status is transitioned from private to published. 7875 * 7876 * @since 1.5.0 7877 * @deprecated 2.3.0 Use {@see 'private_to_publish'} instead. 7878 * 7879 * @param int $post_id Post ID. 7880 */ 7881 do_action_deprecated( 'private_to_published', array( $post->ID ), '2.3.0', 'private_to_publish' ); 7882 } 7883 7884 // If published posts changed clear the lastpostmodified cache. 7885 if ( 'publish' === $new_status || 'publish' === $old_status ) { 7886 foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 7887 wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); 7888 wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); 7889 wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); 7890 } 7891 } 7892 7893 if ( $new_status !== $old_status ) { 7894 wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); 7895 wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); 7896 } 7897 7898 // Always clears the hook in case the post status bounced from future to draft. 7899 wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); 7900 } 7901 7902 /** 7903 * Hook used to schedule publication for a post marked for the future. 7904 * 7905 * The $post properties used and must exist are 'ID' and 'post_date_gmt'. 7906 * 7907 * @since 2.3.0 7908 * @access private 7909 * 7910 * @param int $deprecated Not used. Can be set to null. Never implemented. Not marked 7911 * as deprecated with _deprecated_argument() as it conflicts with 7912 * wp_transition_post_status() and the default filter for _future_post_hook(). 7913 * @param WP_Post $post Post object. 7914 */ 7915 function _future_post_hook( $deprecated, $post ) { 7916 wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); 7917 wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) ); 7918 } 7919 7920 /** 7921 * Hook to schedule pings and enclosures when a post is published. 7922 * 7923 * Uses XMLRPC_REQUEST and WP_IMPORTING constants. 7924 * 7925 * @since 2.3.0 7926 * @access private 7927 * 7928 * @param int $post_id The ID of the post being published. 7929 */ 7930 function _publish_post_hook( $post_id ) { 7931 if ( defined( 'XMLRPC_REQUEST' ) ) { 7932 /** 7933 * Fires when _publish_post_hook() is called during an XML-RPC request. 7934 * 7935 * @since 2.1.0 7936 * 7937 * @param int $post_id Post ID. 7938 */ 7939 do_action( 'xmlrpc_publish_post', $post_id ); 7940 } 7941 7942 if ( defined( 'WP_IMPORTING' ) ) { 7943 return; 7944 } 7945 7946 if ( get_option( 'default_pingback_flag' ) ) { 7947 add_post_meta( $post_id, '_pingme', '1', true ); 7948 } 7949 add_post_meta( $post_id, '_encloseme', '1', true ); 7950 7951 $to_ping = get_to_ping( $post_id ); 7952 if ( ! empty( $to_ping ) ) { 7953 add_post_meta( $post_id, '_trackbackme', '1' ); 7954 } 7955 7956 if ( ! wp_next_scheduled( 'do_pings' ) ) { 7957 wp_schedule_single_event( time(), 'do_pings' ); 7958 } 7959 } 7960 7961 /** 7962 * Returns the ID of the post's parent. 7963 * 7964 * @since 3.1.0 7965 * @since 5.9.0 The `$post` parameter was made optional. 7966 * 7967 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 7968 * @return int|false Post parent ID (which can be 0 if there is no parent), 7969 * or false if the post does not exist. 7970 */ 7971 function wp_get_post_parent_id( $post = null ) { 7972 $post = get_post( $post ); 7973 7974 if ( ! $post || is_wp_error( $post ) ) { 7975 return false; 7976 } 7977 7978 return (int) $post->post_parent; 7979 } 7980 7981 /** 7982 * Checks the given subset of the post hierarchy for hierarchy loops. 7983 * 7984 * Prevents loops from forming and breaks those that it finds. Attached 7985 * to the {@see 'wp_insert_post_parent'} filter. 7986 * 7987 * @since 3.1.0 7988 * 7989 * @see wp_find_hierarchy_loop() 7990 * 7991 * @param int $post_parent ID of the parent for the post we're checking. 7992 * @param int $post_id ID of the post we're checking. 7993 * @return int The new post_parent for the post, 0 otherwise. 7994 */ 7995 function wp_check_post_hierarchy_for_loops( $post_parent, $post_id ) { 7996 // Nothing fancy here - bail. 7997 if ( ! $post_parent ) { 7998 return 0; 7999 } 8000 8001 // New post can't cause a loop. 8002 if ( ! $post_id ) { 8003 return $post_parent; 8004 } 8005 8006 // Can't be its own parent. 8007 if ( $post_parent === $post_id ) { 8008 return 0; 8009 } 8010 8011 // Now look for larger loops. 8012 $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_id, $post_parent ); 8013 if ( ! $loop ) { 8014 return $post_parent; // No loop. 8015 } 8016 8017 // Setting $post_parent to the given value causes a loop. 8018 if ( isset( $loop[ $post_id ] ) ) { 8019 return 0; 8020 } 8021 8022 // There's a loop, but it doesn't contain $post_id. Break the loop. 8023 foreach ( array_keys( $loop ) as $loop_member ) { 8024 wp_update_post( 8025 array( 8026 'ID' => $loop_member, 8027 'post_parent' => 0, 8028 ) 8029 ); 8030 } 8031 8032 return $post_parent; 8033 } 8034 8035 /** 8036 * Sets the post thumbnail (featured image) for the given post. 8037 * 8038 * @since 3.1.0 8039 * 8040 * @param int|WP_Post $post Post ID or post object where thumbnail should be attached. 8041 * @param int $thumbnail_id Thumbnail to attach. 8042 * @return int|bool Post meta ID if the key didn't exist (ie. this is the first time that 8043 * a thumbnail has been saved for the post), true on successful update, 8044 * false on failure or if the value passed is the same as the one that 8045 * is already in the database. 8046 */ 8047 function set_post_thumbnail( $post, $thumbnail_id ) { 8048 $post = get_post( $post ); 8049 $thumbnail_id = absint( $thumbnail_id ); 8050 if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { 8051 if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) { 8052 return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); 8053 } else { 8054 return delete_post_meta( $post->ID, '_thumbnail_id' ); 8055 } 8056 } 8057 return false; 8058 } 8059 8060 /** 8061 * Removes the thumbnail (featured image) from the given post. 8062 * 8063 * @since 3.3.0 8064 * 8065 * @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed. 8066 * @return bool True on success, false on failure. 8067 */ 8068 function delete_post_thumbnail( $post ) { 8069 $post = get_post( $post ); 8070 if ( $post ) { 8071 return delete_post_meta( $post->ID, '_thumbnail_id' ); 8072 } 8073 return false; 8074 } 8075 8076 /** 8077 * Deletes auto-drafts for new posts that are > 7 days old. 8078 * 8079 * @since 3.4.0 8080 * 8081 * @global wpdb $wpdb WordPress database abstraction object. 8082 */ 8083 function wp_delete_auto_drafts() { 8084 global $wpdb; 8085 8086 // Cleanup old auto-drafts more than 7 days old. 8087 $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" ); 8088 foreach ( (array) $old_posts as $delete ) { 8089 // Force delete. 8090 wp_delete_post( $delete, true ); 8091 } 8092 } 8093 8094 /** 8095 * Queues posts for lazy-loading of term meta. 8096 * 8097 * @since 4.5.0 8098 * 8099 * @param WP_Post[] $posts Array of WP_Post objects. 8100 */ 8101 function wp_queue_posts_for_term_meta_lazyload( $posts ) { 8102 $post_type_taxonomies = array(); 8103 $prime_post_terms = array(); 8104 foreach ( $posts as $post ) { 8105 if ( ! ( $post instanceof WP_Post ) ) { 8106 continue; 8107 } 8108 8109 if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { 8110 $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); 8111 } 8112 8113 foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { 8114 $prime_post_terms[ $taxonomy ][] = $post->ID; 8115 } 8116 } 8117 8118 $term_ids = array(); 8119 if ( $prime_post_terms ) { 8120 foreach ( $prime_post_terms as $taxonomy => $post_ids ) { 8121 $cached_term_ids = wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" ); 8122 if ( is_array( $cached_term_ids ) ) { 8123 $cached_term_ids = array_filter( $cached_term_ids ); 8124 foreach ( $cached_term_ids as $_term_ids ) { 8125 // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs. 8126 foreach ( $_term_ids as $term_id ) { 8127 if ( is_numeric( $term_id ) ) { 8128 $term_ids[] = (int) $term_id; 8129 } elseif ( isset( $term_id->term_id ) ) { 8130 $term_ids[] = (int) $term_id->term_id; 8131 } 8132 } 8133 } 8134 } 8135 } 8136 $term_ids = array_unique( $term_ids ); 8137 } 8138 8139 wp_lazyload_term_meta( $term_ids ); 8140 } 8141 8142 /** 8143 * Updates the custom taxonomies' term counts when a post's status is changed. 8144 * 8145 * For example, default posts term counts (for custom taxonomies) don't include 8146 * private / draft posts. 8147 * 8148 * @since 3.3.0 8149 * @access private 8150 * 8151 * @param string $new_status New post status. 8152 * @param string $old_status Old post status. 8153 * @param WP_Post $post Post object. 8154 */ 8155 function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { 8156 // Update counts for the post's terms. 8157 foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { 8158 $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); 8159 wp_update_term_count( $tt_ids, $taxonomy ); 8160 } 8161 } 8162 8163 /** 8164 * Adds any posts from the given IDs to the cache that do not already exist in cache. 8165 * 8166 * @since 3.4.0 8167 * @since 6.1.0 This function is no longer marked as "private". 8168 * 8169 * @see update_post_cache() 8170 * @see update_postmeta_cache() 8171 * @see update_object_term_cache() 8172 * 8173 * @global wpdb $wpdb WordPress database abstraction object. 8174 * 8175 * @param int[] $ids ID list. 8176 * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. 8177 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 8178 */ 8179 function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { 8180 global $wpdb; 8181 8182 $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); 8183 if ( ! empty( $non_cached_ids ) ) { 8184 $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) ); 8185 8186 if ( $fresh_posts ) { 8187 // Despite the name, update_post_cache() expects an array rather than a single post. 8188 update_post_cache( $fresh_posts ); 8189 } 8190 } 8191 8192 if ( $update_meta_cache ) { 8193 update_postmeta_cache( $ids ); 8194 } 8195 8196 if ( $update_term_cache ) { 8197 $post_types = array_map( 'get_post_type', $ids ); 8198 $post_types = array_unique( $post_types ); 8199 update_object_term_cache( $ids, $post_types ); 8200 } 8201 } 8202 8203 /** 8204 * Prime the cache containing the parent ID of various post objects. 8205 * 8206 * @since 6.4.0 8207 * 8208 * @global wpdb $wpdb WordPress database abstraction object. 8209 * 8210 * @param int[] $ids ID list. 8211 */ 8212 function _prime_post_parent_id_caches( array $ids ) { 8213 global $wpdb; 8214 8215 $ids = array_filter( $ids, '_validate_cache_id' ); 8216 $ids = array_unique( array_map( 'intval', $ids ), SORT_NUMERIC ); 8217 8218 if ( empty( $ids ) ) { 8219 return; 8220 } 8221 8222 $cache_keys = array(); 8223 foreach ( $ids as $id ) { 8224 $cache_keys[ $id ] = 'post_parent:' . (string) $id; 8225 } 8226 8227 $cached_data = wp_cache_get_multiple( array_values( $cache_keys ), 'posts' ); 8228 8229 $non_cached_ids = array(); 8230 foreach ( $cache_keys as $id => $cache_key ) { 8231 if ( false === $cached_data[ $cache_key ] ) { 8232 $non_cached_ids[] = $id; 8233 } 8234 } 8235 8236 if ( ! empty( $non_cached_ids ) ) { 8237 $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 ) ) ); 8238 8239 if ( $fresh_posts ) { 8240 $post_parent_data = array(); 8241 foreach ( $fresh_posts as $fresh_post ) { 8242 $post_parent_data[ 'post_parent:' . (string) $fresh_post->ID ] = (int) $fresh_post->post_parent; 8243 } 8244 8245 wp_cache_add_multiple( $post_parent_data, 'posts' ); 8246 } 8247 } 8248 } 8249 8250 /** 8251 * Adds a suffix if any trashed posts have a given slug. 8252 * 8253 * Store its desired (i.e. current) slug so it can try to reclaim it 8254 * if the post is untrashed. 8255 * 8256 * For internal use. 8257 * 8258 * @since 4.5.0 8259 * @access private 8260 * 8261 * @param string $post_name Post slug. 8262 * @param int $post_id Optional. Post ID that should be ignored. Default 0. 8263 */ 8264 function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id = 0 ) { 8265 $trashed_posts_with_desired_slug = get_posts( 8266 array( 8267 'name' => $post_name, 8268 'post_status' => 'trash', 8269 'post_type' => 'any', 8270 'nopaging' => true, 8271 'post__not_in' => array( $post_id ), 8272 ) 8273 ); 8274 8275 if ( ! empty( $trashed_posts_with_desired_slug ) ) { 8276 foreach ( $trashed_posts_with_desired_slug as $_post ) { 8277 wp_add_trashed_suffix_to_post_name_for_post( $_post ); 8278 } 8279 } 8280 } 8281 8282 /** 8283 * Adds a trashed suffix for a given post. 8284 * 8285 * Store its desired (i.e. current) slug so it can try to reclaim it 8286 * if the post is untrashed. 8287 * 8288 * For internal use. 8289 * 8290 * @since 4.5.0 8291 * @access private 8292 * 8293 * @global wpdb $wpdb WordPress database abstraction object. 8294 * 8295 * @param WP_Post $post The post. 8296 * @return string New slug for the post. 8297 */ 8298 function wp_add_trashed_suffix_to_post_name_for_post( $post ) { 8299 global $wpdb; 8300 8301 $post = get_post( $post ); 8302 8303 if ( str_ends_with( $post->post_name, '__trashed' ) ) { 8304 return $post->post_name; 8305 } 8306 add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); 8307 $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; 8308 $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); 8309 clean_post_cache( $post->ID ); 8310 return $post_name; 8311 } 8312 8313 /** 8314 * Sets the last changed time for the 'posts' cache group. 8315 * 8316 * @since 5.0.0 8317 */ 8318 function wp_cache_set_posts_last_changed() { 8319 wp_cache_set_last_changed( 'posts' ); 8320 } 8321 8322 /** 8323 * Gets all available post MIME types for a given post type. 8324 * 8325 * @since 2.5.0 8326 * 8327 * @global wpdb $wpdb WordPress database abstraction object. 8328 * 8329 * @param string $type 8330 * @return string[] An array of MIME types. 8331 */ 8332 function get_available_post_mime_types( $type = 'attachment' ) { 8333 global $wpdb; 8334 8335 /** 8336 * Filters the list of available post MIME types for the given post type. 8337 * 8338 * @since 6.4.0 8339 * 8340 * @param string[]|null $mime_types An array of MIME types. Default null. 8341 * @param string $type The post type name. Usually 'attachment' but can be any post type. 8342 */ 8343 $mime_types = apply_filters( 'pre_get_available_post_mime_types', null, $type ); 8344 8345 if ( ! is_array( $mime_types ) ) { 8346 $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s AND post_mime_type != ''", $type ) ); 8347 } 8348 8349 // Remove nulls from returned $mime_types. 8350 return array_values( array_filter( $mime_types ) ); 8351 } 8352 8353 /** 8354 * Retrieves the path to an uploaded image file. 8355 * 8356 * Similar to `get_attached_file()` however some images may have been processed after uploading 8357 * to make them suitable for web use. In this case the attached "full" size file is usually replaced 8358 * with a scaled down version of the original image. This function always returns the path 8359 * to the originally uploaded image file. 8360 * 8361 * @since 5.3.0 8362 * @since 5.4.0 Added the `$unfiltered` parameter. 8363 * 8364 * @param int $attachment_id Attachment ID. 8365 * @param bool $unfiltered Optional. Passed through to `get_attached_file()`. Default false. 8366 * @return string|false Path to the original image file or false if the attachment is not an image. 8367 */ 8368 function wp_get_original_image_path( $attachment_id, $unfiltered = false ) { 8369 if ( ! wp_attachment_is_image( $attachment_id ) ) { 8370 return false; 8371 } 8372 8373 $image_meta = wp_get_attachment_metadata( $attachment_id ); 8374 $image_file = get_attached_file( $attachment_id, $unfiltered ); 8375 8376 if ( empty( $image_meta['original_image'] ) ) { 8377 $original_image = $image_file; 8378 } else { 8379 $original_image = path_join( dirname( $image_file ), $image_meta['original_image'] ); 8380 } 8381 8382 /** 8383 * Filters the path to the original image. 8384 * 8385 * @since 5.3.0 8386 * 8387 * @param string $original_image Path to original image file. 8388 * @param int $attachment_id Attachment ID. 8389 */ 8390 return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id ); 8391 } 8392 8393 /** 8394 * Retrieves the URL to an original attachment image. 8395 * 8396 * Similar to `wp_get_attachment_url()` however some images may have been 8397 * processed after uploading. In this case this function returns the URL 8398 * to the originally uploaded image file. 8399 * 8400 * @since 5.3.0 8401 * 8402 * @param int $attachment_id Attachment post ID. 8403 * @return string|false Attachment image URL, false on error or if the attachment is not an image. 8404 */ 8405 function wp_get_original_image_url( $attachment_id ) { 8406 if ( ! wp_attachment_is_image( $attachment_id ) ) { 8407 return false; 8408 } 8409 8410 $image_url = wp_get_attachment_url( $attachment_id ); 8411 8412 if ( ! $image_url ) { 8413 return false; 8414 } 8415 8416 $image_meta = wp_get_attachment_metadata( $attachment_id ); 8417 8418 if ( empty( $image_meta['original_image'] ) ) { 8419 $original_image_url = $image_url; 8420 } else { 8421 $original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] ); 8422 } 8423 8424 /** 8425 * Filters the URL to the original attachment image. 8426 * 8427 * @since 5.3.0 8428 * 8429 * @param string $original_image_url URL to original image. 8430 * @param int $attachment_id Attachment ID. 8431 */ 8432 return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id ); 8433 } 8434 8435 /** 8436 * Filters callback which sets the status of an untrashed post to its previous status. 8437 * 8438 * This can be used as a callback on the `wp_untrash_post_status` filter. 8439 * 8440 * @since 5.6.0 8441 * 8442 * @param string $new_status The new status of the post being restored. 8443 * @param int $post_id The ID of the post being restored. 8444 * @param string $previous_status The status of the post at the point where it was trashed. 8445 * @return string The new status of the post. 8446 */ 8447 function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) { 8448 return $previous_status; 8449 } 8450 8451 /** 8452 * Returns whether the post can be edited in the block editor. 8453 * 8454 * @since 5.0.0 8455 * @since 6.1.0 Moved to wp-includes from wp-admin. 8456 * 8457 * @param int|WP_Post $post Post ID or WP_Post object. 8458 * @return bool Whether the post can be edited in the block editor. 8459 */ 8460 function use_block_editor_for_post( $post ) { 8461 $post = get_post( $post ); 8462 8463 if ( ! $post ) { 8464 return false; 8465 } 8466 8467 // We're in the meta box loader, so don't use the block editor. 8468 if ( is_admin() && isset( $_GET['meta-box-loader'] ) ) { 8469 check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' ); 8470 return false; 8471 } 8472 8473 $use_block_editor = use_block_editor_for_post_type( $post->post_type ); 8474 8475 /** 8476 * Filters whether a post is able to be edited in the block editor. 8477 * 8478 * @since 5.0.0 8479 * 8480 * @param bool $use_block_editor Whether the post can be edited or not. 8481 * @param WP_Post $post The post being checked. 8482 */ 8483 return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post ); 8484 } 8485 8486 /** 8487 * Returns whether a post type is compatible with the block editor. 8488 * 8489 * The block editor depends on the REST API, and if the post type is not shown in the 8490 * REST API, then it won't work with the block editor. 8491 * 8492 * @since 5.0.0 8493 * @since 6.1.0 Moved to wp-includes from wp-admin. 8494 * 8495 * @param string $post_type The post type. 8496 * @return bool Whether the post type can be edited with the block editor. 8497 */ 8498 function use_block_editor_for_post_type( $post_type ) { 8499 if ( ! post_type_exists( $post_type ) ) { 8500 return false; 8501 } 8502 8503 if ( ! post_type_supports( $post_type, 'editor' ) ) { 8504 return false; 8505 } 8506 8507 $post_type_object = get_post_type_object( $post_type ); 8508 if ( $post_type_object && ! $post_type_object->show_in_rest ) { 8509 return false; 8510 } 8511 8512 /** 8513 * Filters whether a post is able to be edited in the block editor. 8514 * 8515 * @since 5.0.0 8516 * 8517 * @param bool $use_block_editor Whether the post type can be edited or not. Default true. 8518 * @param string $post_type The post type being checked. 8519 */ 8520 return apply_filters( 'use_block_editor_for_post_type', true, $post_type ); 8521 } 8522 8523 /** 8524 * Registers any additional post meta fields. 8525 * 8526 * @since 6.3.0 Adds `wp_pattern_sync_status` meta field to the wp_block post type so an unsynced option can be added. 8527 * 8528 * @link https://github.com/WordPress/gutenberg/pull/51144 8529 */ 8530 function wp_create_initial_post_meta() { 8531 register_post_meta( 8532 'wp_block', 8533 'wp_pattern_sync_status', 8534 array( 8535 'sanitize_callback' => 'sanitize_text_field', 8536 'single' => true, 8537 'type' => 'string', 8538 'show_in_rest' => array( 8539 'schema' => array( 8540 'type' => 'string', 8541 'enum' => array( 'partial', 'unsynced' ), 8542 ), 8543 ), 8544 ) 8545 ); 8546 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri May 23 08:20:01 2025 | Cross-referenced by PHPXref |