changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
20:7b1b88e27a20 | 21:48c4eec2b7e6 |
---|---|
73 'attachment', |
73 'attachment', |
74 array( |
74 array( |
75 'labels' => array( |
75 'labels' => array( |
76 'name' => _x( 'Media', 'post type general name' ), |
76 'name' => _x( 'Media', 'post type general name' ), |
77 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), |
77 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), |
78 'add_new' => _x( 'Add New', 'add new media' ), |
78 'add_new' => __( 'Add New Media File' ), |
79 'edit_item' => __( 'Edit Media' ), |
79 'edit_item' => __( 'Edit Media' ), |
80 'view_item' => __( 'View Attachment Page' ), |
80 'view_item' => ( '1' === get_option( 'wp_attachment_pages_enabled' ) ) ? __( 'View Attachment Page' ) : __( 'View Media File' ), |
81 'attributes' => __( 'Attachment Attributes' ), |
81 'attributes' => __( 'Attachment Attributes' ), |
82 ), |
82 ), |
83 'public' => true, |
83 'public' => true, |
84 'show_ui' => true, |
84 'show_ui' => true, |
85 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
85 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
200 'customize_changeset', |
200 'customize_changeset', |
201 array( |
201 array( |
202 'labels' => array( |
202 'labels' => array( |
203 'name' => _x( 'Changesets', 'post type general name' ), |
203 'name' => _x( 'Changesets', 'post type general name' ), |
204 'singular_name' => _x( 'Changeset', 'post type singular name' ), |
204 'singular_name' => _x( 'Changeset', 'post type singular name' ), |
205 'add_new' => _x( 'Add New', 'Customize Changeset' ), |
205 'add_new' => __( 'Add New Changeset' ), |
206 'add_new_item' => __( 'Add New Changeset' ), |
206 'add_new_item' => __( 'Add New Changeset' ), |
207 'new_item' => __( 'New Changeset' ), |
207 'new_item' => __( 'New Changeset' ), |
208 'edit_item' => __( 'Edit Changeset' ), |
208 'edit_item' => __( 'Edit Changeset' ), |
209 'view_item' => __( 'View Changeset' ), |
209 'view_item' => __( 'View Changeset' ), |
210 'all_items' => __( 'All Changesets' ), |
210 'all_items' => __( 'All Changesets' ), |
280 |
280 |
281 register_post_type( |
281 register_post_type( |
282 'wp_block', |
282 'wp_block', |
283 array( |
283 array( |
284 'labels' => array( |
284 'labels' => array( |
285 'name' => _x( 'Reusable blocks', 'post type general name' ), |
285 'name' => _x( 'Patterns', 'post type general name' ), |
286 'singular_name' => _x( 'Reusable block', 'post type singular name' ), |
286 'singular_name' => _x( 'Pattern', 'post type singular name' ), |
287 'add_new' => _x( 'Add New', 'Reusable block' ), |
287 'add_new' => __( 'Add New Pattern' ), |
288 'add_new_item' => __( 'Add new Reusable block' ), |
288 'add_new_item' => __( 'Add New Pattern' ), |
289 'new_item' => __( 'New Reusable block' ), |
289 'new_item' => __( 'New Pattern' ), |
290 'edit_item' => __( 'Edit Reusable block' ), |
290 'edit_item' => __( 'Edit Block Pattern' ), |
291 'view_item' => __( 'View Reusable block' ), |
291 'view_item' => __( 'View Pattern' ), |
292 'all_items' => __( 'All Reusable blocks' ), |
292 'view_items' => __( 'View Patterns' ), |
293 'search_items' => __( 'Search Reusable blocks' ), |
293 'all_items' => __( 'All Patterns' ), |
294 'not_found' => __( 'No reusable blocks found.' ), |
294 'search_items' => __( 'Search Patterns' ), |
295 'not_found_in_trash' => __( 'No reusable blocks found in Trash.' ), |
295 'not_found' => __( 'No patterns found.' ), |
296 'filter_items_list' => __( 'Filter reusable blocks list' ), |
296 'not_found_in_trash' => __( 'No patterns found in Trash.' ), |
297 'items_list_navigation' => __( 'Reusable blocks list navigation' ), |
297 'filter_items_list' => __( 'Filter patterns list' ), |
298 'items_list' => __( 'Reusable blocks list' ), |
298 'items_list_navigation' => __( 'Patterns list navigation' ), |
299 'item_published' => __( 'Reusable block published.' ), |
299 'items_list' => __( 'Patterns list' ), |
300 'item_published_privately' => __( 'Reusable block published privately.' ), |
300 'item_published' => __( 'Pattern published.' ), |
301 'item_reverted_to_draft' => __( 'Reusable block reverted to draft.' ), |
301 'item_published_privately' => __( 'Pattern published privately.' ), |
302 'item_scheduled' => __( 'Reusable block scheduled.' ), |
302 'item_reverted_to_draft' => __( 'Pattern reverted to draft.' ), |
303 'item_updated' => __( 'Reusable block updated.' ), |
303 'item_scheduled' => __( 'Pattern scheduled.' ), |
304 'item_updated' => __( 'Pattern updated.' ), |
|
304 ), |
305 ), |
305 'public' => false, |
306 'public' => false, |
306 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
307 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
307 'show_ui' => true, |
308 'show_ui' => true, |
308 'show_in_menu' => false, |
309 'show_in_menu' => false, |
317 // You need to be able to publish posts, in order to create blocks. |
318 // You need to be able to publish posts, in order to create blocks. |
318 'create_posts' => 'publish_posts', |
319 'create_posts' => 'publish_posts', |
319 'edit_posts' => 'edit_posts', |
320 'edit_posts' => 'edit_posts', |
320 'edit_published_posts' => 'edit_published_posts', |
321 'edit_published_posts' => 'edit_published_posts', |
321 'delete_published_posts' => 'delete_published_posts', |
322 'delete_published_posts' => 'delete_published_posts', |
323 // Enables trashing draft posts as well. |
|
324 'delete_posts' => 'delete_posts', |
|
322 'edit_others_posts' => 'edit_others_posts', |
325 'edit_others_posts' => 'edit_others_posts', |
323 'delete_others_posts' => 'delete_others_posts', |
326 'delete_others_posts' => 'delete_others_posts', |
324 ), |
327 ), |
325 'map_meta_cap' => true, |
328 'map_meta_cap' => true, |
326 'supports' => array( |
329 'supports' => array( |
327 'title', |
330 'title', |
331 'excerpt', |
|
328 'editor', |
332 'editor', |
329 'revisions', |
333 'revisions', |
334 'custom-fields', |
|
330 ), |
335 ), |
331 ) |
336 ) |
332 ); |
337 ); |
333 |
338 |
339 $template_edit_link = 'site-editor.php?' . build_query( |
|
340 array( |
|
341 'postType' => '%s', |
|
342 'postId' => '%s', |
|
343 'canvas' => 'edit', |
|
344 ) |
|
345 ); |
|
346 |
|
334 register_post_type( |
347 register_post_type( |
335 'wp_template', |
348 'wp_template', |
336 array( |
349 array( |
337 'labels' => array( |
350 'labels' => array( |
338 'name' => _x( 'Templates', 'post type general name' ), |
351 'name' => _x( 'Templates', 'post type general name' ), |
339 'singular_name' => _x( 'Template', 'post type singular name' ), |
352 'singular_name' => _x( 'Template', 'post type singular name' ), |
340 'add_new' => _x( 'Add New', 'Template' ), |
353 'add_new' => __( 'Add New Template' ), |
341 'add_new_item' => __( 'Add New Template' ), |
354 'add_new_item' => __( 'Add New Template' ), |
342 'new_item' => __( 'New Template' ), |
355 'new_item' => __( 'New Template' ), |
343 'edit_item' => __( 'Edit Template' ), |
356 'edit_item' => __( 'Edit Template' ), |
344 'view_item' => __( 'View Template' ), |
357 'view_item' => __( 'View Template' ), |
345 'all_items' => __( 'Templates' ), |
358 'all_items' => __( 'Templates' ), |
351 'insert_into_item' => __( 'Insert into template' ), |
364 'insert_into_item' => __( 'Insert into template' ), |
352 'uploaded_to_this_item' => __( 'Uploaded to this template' ), |
365 'uploaded_to_this_item' => __( 'Uploaded to this template' ), |
353 'filter_items_list' => __( 'Filter templates list' ), |
366 'filter_items_list' => __( 'Filter templates list' ), |
354 'items_list_navigation' => __( 'Templates list navigation' ), |
367 'items_list_navigation' => __( 'Templates list navigation' ), |
355 'items_list' => __( 'Templates list' ), |
368 'items_list' => __( 'Templates list' ), |
369 'item_updated' => __( 'Template updated.' ), |
|
356 ), |
370 ), |
357 'description' => __( 'Templates to include in your theme.' ), |
371 'description' => __( 'Templates to include in your theme.' ), |
358 'public' => false, |
372 'public' => false, |
359 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
373 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
360 'has_archive' => false, |
374 '_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */ |
361 'show_ui' => false, |
375 'has_archive' => false, |
362 'show_in_menu' => false, |
376 'show_ui' => false, |
363 'show_in_rest' => true, |
377 'show_in_menu' => false, |
364 'rewrite' => false, |
378 'show_in_rest' => true, |
365 'rest_base' => 'templates', |
379 'rewrite' => false, |
366 'rest_controller_class' => 'WP_REST_Templates_Controller', |
380 'rest_base' => 'templates', |
367 'capability_type' => array( 'template', 'templates' ), |
381 'rest_controller_class' => 'WP_REST_Templates_Controller', |
368 'capabilities' => array( |
382 'autosave_rest_controller_class' => 'WP_REST_Template_Autosaves_Controller', |
383 'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller', |
|
384 'late_route_registration' => true, |
|
385 'capability_type' => array( 'template', 'templates' ), |
|
386 'capabilities' => array( |
|
369 'create_posts' => 'edit_theme_options', |
387 'create_posts' => 'edit_theme_options', |
370 'delete_posts' => 'edit_theme_options', |
388 'delete_posts' => 'edit_theme_options', |
371 'delete_others_posts' => 'edit_theme_options', |
389 'delete_others_posts' => 'edit_theme_options', |
372 'delete_private_posts' => 'edit_theme_options', |
390 'delete_private_posts' => 'edit_theme_options', |
373 'delete_published_posts' => 'edit_theme_options', |
391 'delete_published_posts' => 'edit_theme_options', |
377 'edit_published_posts' => 'edit_theme_options', |
395 'edit_published_posts' => 'edit_theme_options', |
378 'publish_posts' => 'edit_theme_options', |
396 'publish_posts' => 'edit_theme_options', |
379 'read' => 'edit_theme_options', |
397 'read' => 'edit_theme_options', |
380 'read_private_posts' => 'edit_theme_options', |
398 'read_private_posts' => 'edit_theme_options', |
381 ), |
399 ), |
382 'map_meta_cap' => true, |
400 'map_meta_cap' => true, |
383 'supports' => array( |
401 'supports' => array( |
384 'title', |
402 'title', |
385 'slug', |
403 'slug', |
386 'excerpt', |
404 'excerpt', |
387 'editor', |
405 'editor', |
388 'revisions', |
406 'revisions', |
392 ); |
410 ); |
393 |
411 |
394 register_post_type( |
412 register_post_type( |
395 'wp_template_part', |
413 'wp_template_part', |
396 array( |
414 array( |
397 'labels' => array( |
415 'labels' => array( |
398 'name' => _x( 'Template Parts', 'post type general name' ), |
416 'name' => _x( 'Template Parts', 'post type general name' ), |
399 'singular_name' => _x( 'Template Part', 'post type singular name' ), |
417 'singular_name' => _x( 'Template Part', 'post type singular name' ), |
400 'add_new' => _x( 'Add New', 'Template Part' ), |
418 'add_new' => __( 'Add New Template Part' ), |
401 'add_new_item' => __( 'Add New Template Part' ), |
419 'add_new_item' => __( 'Add New Template Part' ), |
402 'new_item' => __( 'New Template Part' ), |
420 'new_item' => __( 'New Template Part' ), |
403 'edit_item' => __( 'Edit Template Part' ), |
421 'edit_item' => __( 'Edit Template Part' ), |
404 'view_item' => __( 'View Template Part' ), |
422 'view_item' => __( 'View Template Part' ), |
405 'all_items' => __( 'Template Parts' ), |
423 'all_items' => __( 'Template Parts' ), |
411 'insert_into_item' => __( 'Insert into template part' ), |
429 'insert_into_item' => __( 'Insert into template part' ), |
412 'uploaded_to_this_item' => __( 'Uploaded to this template part' ), |
430 'uploaded_to_this_item' => __( 'Uploaded to this template part' ), |
413 'filter_items_list' => __( 'Filter template parts list' ), |
431 'filter_items_list' => __( 'Filter template parts list' ), |
414 'items_list_navigation' => __( 'Template parts list navigation' ), |
432 'items_list_navigation' => __( 'Template parts list navigation' ), |
415 'items_list' => __( 'Template parts list' ), |
433 'items_list' => __( 'Template parts list' ), |
434 'item_updated' => __( 'Template part updated.' ), |
|
416 ), |
435 ), |
417 'description' => __( 'Template parts to include in your templates.' ), |
436 'description' => __( 'Template parts to include in your templates.' ), |
418 'public' => false, |
437 'public' => false, |
419 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
438 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
420 'has_archive' => false, |
439 '_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */ |
421 'show_ui' => false, |
440 'has_archive' => false, |
422 'show_in_menu' => false, |
441 'show_ui' => false, |
423 'show_in_rest' => true, |
442 'show_in_menu' => false, |
424 'rewrite' => false, |
443 'show_in_rest' => true, |
425 'rest_base' => 'template-parts', |
444 'rewrite' => false, |
426 'rest_controller_class' => 'WP_REST_Templates_Controller', |
445 'rest_base' => 'template-parts', |
427 'map_meta_cap' => true, |
446 'rest_controller_class' => 'WP_REST_Templates_Controller', |
428 'capabilities' => array( |
447 'autosave_rest_controller_class' => 'WP_REST_Template_Autosaves_Controller', |
448 'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller', |
|
449 'late_route_registration' => true, |
|
450 'map_meta_cap' => true, |
|
451 'capabilities' => array( |
|
429 'create_posts' => 'edit_theme_options', |
452 'create_posts' => 'edit_theme_options', |
430 'delete_posts' => 'edit_theme_options', |
453 'delete_posts' => 'edit_theme_options', |
431 'delete_others_posts' => 'edit_theme_options', |
454 'delete_others_posts' => 'edit_theme_options', |
432 'delete_private_posts' => 'edit_theme_options', |
455 'delete_private_posts' => 'edit_theme_options', |
433 'delete_published_posts' => 'edit_theme_options', |
456 'delete_published_posts' => 'edit_theme_options', |
437 'edit_published_posts' => 'edit_theme_options', |
460 'edit_published_posts' => 'edit_theme_options', |
438 'publish_posts' => 'edit_theme_options', |
461 'publish_posts' => 'edit_theme_options', |
439 'read' => 'edit_theme_options', |
462 'read' => 'edit_theme_options', |
440 'read_private_posts' => 'edit_theme_options', |
463 'read_private_posts' => 'edit_theme_options', |
441 ), |
464 ), |
442 'supports' => array( |
465 'supports' => array( |
443 'title', |
466 'title', |
444 'slug', |
467 'slug', |
445 'excerpt', |
468 'excerpt', |
446 'editor', |
469 'editor', |
447 'revisions', |
470 'revisions', |
451 ); |
474 ); |
452 |
475 |
453 register_post_type( |
476 register_post_type( |
454 'wp_global_styles', |
477 'wp_global_styles', |
455 array( |
478 array( |
456 'label' => _x( 'Global Styles', 'post type general name' ), |
479 'label' => _x( 'Global Styles', 'post type general name' ), |
457 'description' => __( 'Global styles to include in themes.' ), |
480 'description' => __( 'Global styles to include in themes.' ), |
458 'public' => false, |
481 'public' => false, |
459 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
482 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
460 'show_ui' => false, |
483 '_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */ |
461 'show_in_rest' => false, |
484 'show_ui' => false, |
462 'rewrite' => false, |
485 'show_in_rest' => true, |
463 'capabilities' => array( |
486 'rewrite' => false, |
487 'rest_base' => 'global-styles', |
|
488 'rest_controller_class' => 'WP_REST_Global_Styles_Controller', |
|
489 'revisions_rest_controller_class' => 'WP_REST_Global_Styles_Revisions_Controller', |
|
490 'late_route_registration' => true, |
|
491 'capabilities' => array( |
|
464 'read' => 'edit_theme_options', |
492 'read' => 'edit_theme_options', |
465 'create_posts' => 'edit_theme_options', |
493 'create_posts' => 'edit_theme_options', |
466 'edit_posts' => 'edit_theme_options', |
494 'edit_posts' => 'edit_theme_options', |
467 'edit_published_posts' => 'edit_theme_options', |
495 'edit_published_posts' => 'edit_theme_options', |
468 'delete_published_posts' => 'edit_theme_options', |
496 'delete_published_posts' => 'edit_theme_options', |
469 'edit_others_posts' => 'edit_theme_options', |
497 'edit_others_posts' => 'edit_theme_options', |
470 'delete_others_posts' => 'edit_theme_options', |
498 'delete_others_posts' => 'edit_theme_options', |
471 ), |
499 ), |
472 'map_meta_cap' => true, |
500 'map_meta_cap' => true, |
473 'supports' => array( |
501 'supports' => array( |
474 'title', |
502 'title', |
475 'editor', |
503 'editor', |
476 'revisions', |
504 'revisions', |
477 ), |
505 ), |
478 ) |
506 ) |
479 ); |
507 ); |
508 // Disable autosave endpoints for global styles. |
|
509 remove_post_type_support( 'wp_global_styles', 'autosave' ); |
|
510 |
|
511 $navigation_post_edit_link = 'site-editor.php?' . build_query( |
|
512 array( |
|
513 'postId' => '%s', |
|
514 'postType' => 'wp_navigation', |
|
515 'canvas' => 'edit', |
|
516 ) |
|
517 ); |
|
480 |
518 |
481 register_post_type( |
519 register_post_type( |
482 'wp_navigation', |
520 'wp_navigation', |
483 array( |
521 array( |
484 'labels' => array( |
522 'labels' => array( |
485 'name' => _x( 'Navigation Menus', 'post type general name' ), |
523 'name' => _x( 'Navigation Menus', 'post type general name' ), |
486 'singular_name' => _x( 'Navigation Menu', 'post type singular name' ), |
524 'singular_name' => _x( 'Navigation Menu', 'post type singular name' ), |
487 'add_new' => _x( 'Add New', 'Navigation Menu' ), |
525 'add_new' => __( 'Add New Navigation Menu' ), |
488 'add_new_item' => __( 'Add New Navigation Menu' ), |
526 'add_new_item' => __( 'Add New Navigation Menu' ), |
489 'new_item' => __( 'New Navigation Menu' ), |
527 'new_item' => __( 'New Navigation Menu' ), |
490 'edit_item' => __( 'Edit Navigation Menu' ), |
528 'edit_item' => __( 'Edit Navigation Menu' ), |
491 'view_item' => __( 'View Navigation Menu' ), |
529 'view_item' => __( 'View Navigation Menu' ), |
492 'all_items' => __( 'Navigation Menus' ), |
530 'all_items' => __( 'Navigation Menus' ), |
502 'items_list' => __( 'Navigation Menus list' ), |
540 'items_list' => __( 'Navigation Menus list' ), |
503 ), |
541 ), |
504 'description' => __( 'Navigation menus that can be inserted into your site.' ), |
542 'description' => __( 'Navigation menus that can be inserted into your site.' ), |
505 'public' => false, |
543 'public' => false, |
506 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
544 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
545 '_edit_link' => $navigation_post_edit_link, /* internal use only. don't use this when registering your own post type. */ |
|
507 'has_archive' => false, |
546 'has_archive' => false, |
508 'show_ui' => true, |
547 'show_ui' => true, |
509 'show_in_menu' => false, |
548 'show_in_menu' => false, |
510 'show_in_admin_bar' => false, |
549 'show_in_admin_bar' => false, |
511 'show_in_rest' => true, |
550 'show_in_rest' => true, |
532 'revisions', |
571 'revisions', |
533 ), |
572 ), |
534 ) |
573 ) |
535 ); |
574 ); |
536 |
575 |
576 register_post_type( |
|
577 'wp_font_family', |
|
578 array( |
|
579 'labels' => array( |
|
580 'name' => __( 'Font Families' ), |
|
581 'singular_name' => __( 'Font Family' ), |
|
582 ), |
|
583 'public' => false, |
|
584 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
585 'hierarchical' => false, |
|
586 'capabilities' => array( |
|
587 'read' => 'edit_theme_options', |
|
588 'read_private_posts' => 'edit_theme_options', |
|
589 'create_posts' => 'edit_theme_options', |
|
590 'publish_posts' => 'edit_theme_options', |
|
591 'edit_posts' => 'edit_theme_options', |
|
592 'edit_others_posts' => 'edit_theme_options', |
|
593 'edit_published_posts' => 'edit_theme_options', |
|
594 'delete_posts' => 'edit_theme_options', |
|
595 'delete_others_posts' => 'edit_theme_options', |
|
596 'delete_published_posts' => 'edit_theme_options', |
|
597 ), |
|
598 'map_meta_cap' => true, |
|
599 'query_var' => false, |
|
600 'rewrite' => false, |
|
601 'show_in_rest' => true, |
|
602 'rest_base' => 'font-families', |
|
603 'rest_controller_class' => 'WP_REST_Font_Families_Controller', |
|
604 'supports' => array( 'title' ), |
|
605 ) |
|
606 ); |
|
607 |
|
608 register_post_type( |
|
609 'wp_font_face', |
|
610 array( |
|
611 'labels' => array( |
|
612 'name' => __( 'Font Faces' ), |
|
613 'singular_name' => __( 'Font Face' ), |
|
614 ), |
|
615 'public' => false, |
|
616 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
617 'hierarchical' => false, |
|
618 'capabilities' => array( |
|
619 'read' => 'edit_theme_options', |
|
620 'read_private_posts' => 'edit_theme_options', |
|
621 'create_posts' => 'edit_theme_options', |
|
622 'publish_posts' => 'edit_theme_options', |
|
623 'edit_posts' => 'edit_theme_options', |
|
624 'edit_others_posts' => 'edit_theme_options', |
|
625 'edit_published_posts' => 'edit_theme_options', |
|
626 'delete_posts' => 'edit_theme_options', |
|
627 'delete_others_posts' => 'edit_theme_options', |
|
628 'delete_published_posts' => 'edit_theme_options', |
|
629 ), |
|
630 'map_meta_cap' => true, |
|
631 'query_var' => false, |
|
632 'rewrite' => false, |
|
633 'show_in_rest' => true, |
|
634 'rest_base' => 'font-families/(?P<font_family_id>[\d]+)/font-faces', |
|
635 'rest_controller_class' => 'WP_REST_Font_Faces_Controller', |
|
636 'supports' => array( 'title' ), |
|
637 ) |
|
638 ); |
|
639 |
|
537 register_post_status( |
640 register_post_status( |
538 'publish', |
641 'publish', |
539 array( |
642 array( |
540 'label' => _x( 'Published', 'post status' ), |
643 'label' => _x( 'Published', 'post status' ), |
541 'public' => true, |
644 'public' => true, |
701 ) |
804 ) |
702 ); |
805 ); |
703 } |
806 } |
704 |
807 |
705 /** |
808 /** |
706 * Retrieve attached file path based on attachment ID. |
809 * Retrieves attached file path based on attachment ID. |
707 * |
810 * |
708 * By default the path will go through the 'get_attached_file' filter, but |
811 * By default the path will go through the {@see 'get_attached_file'} filter, but |
709 * passing a true to the $unfiltered argument of get_attached_file() will |
812 * passing `true` to the `$unfiltered` argument will return the file path unfiltered. |
710 * return the file path unfiltered. |
813 * |
711 * |
814 * The function works by retrieving the `_wp_attached_file` post meta value. |
712 * The function works by getting the single post meta name, named |
815 * This is a convenience function to prevent looking up the meta name and provide |
713 * '_wp_attached_file' and returning it. This is a convenience function to |
816 * a mechanism for sending the attached filename through a filter. |
714 * prevent looking up the meta name and provide a mechanism for sending the |
|
715 * attached filename through a filter. |
|
716 * |
817 * |
717 * @since 2.0.0 |
818 * @since 2.0.0 |
718 * |
819 * |
719 * @param int $attachment_id Attachment ID. |
820 * @param int $attachment_id Attachment ID. |
720 * @param bool $unfiltered Optional. Whether to apply filters. Default false. |
821 * @param bool $unfiltered Optional. Whether to skip the {@see 'get_attached_file'} filter. |
822 * Default false. |
|
721 * @return string|false The file path to where the attached file should be, false otherwise. |
823 * @return string|false The file path to where the attached file should be, false otherwise. |
722 */ |
824 */ |
723 function get_attached_file( $attachment_id, $unfiltered = false ) { |
825 function get_attached_file( $attachment_id, $unfiltered = false ) { |
724 $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
826 $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
725 |
827 |
726 // If the file is relative, prepend upload dir. |
828 // If the file is relative, prepend upload dir. |
727 if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { |
829 if ( $file && ! str_starts_with( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { |
728 $uploads = wp_get_upload_dir(); |
830 $uploads = wp_get_upload_dir(); |
729 if ( false === $uploads['error'] ) { |
831 if ( false === $uploads['error'] ) { |
730 $file = $uploads['basedir'] . "/$file"; |
832 $file = $uploads['basedir'] . "/$file"; |
731 } |
833 } |
732 } |
834 } |
745 */ |
847 */ |
746 return apply_filters( 'get_attached_file', $file, $attachment_id ); |
848 return apply_filters( 'get_attached_file', $file, $attachment_id ); |
747 } |
849 } |
748 |
850 |
749 /** |
851 /** |
750 * Update attachment file path based on attachment ID. |
852 * Updates attachment file path based on attachment ID. |
751 * |
853 * |
752 * Used to update the file path of the attachment, which uses post meta name |
854 * Used to update the file path of the attachment, which uses post meta name |
753 * '_wp_attached_file' to store the path of the attachment. |
855 * '_wp_attached_file' to store the path of the attachment. |
754 * |
856 * |
755 * @since 2.1.0 |
857 * @since 2.1.0 |
780 return delete_post_meta( $attachment_id, '_wp_attached_file' ); |
882 return delete_post_meta( $attachment_id, '_wp_attached_file' ); |
781 } |
883 } |
782 } |
884 } |
783 |
885 |
784 /** |
886 /** |
785 * Return relative path to an uploaded file. |
887 * Returns relative path to an uploaded file. |
786 * |
888 * |
787 * The path is relative to the current upload dir. |
889 * The path is relative to the current upload dir. |
788 * |
890 * |
789 * @since 2.9.0 |
891 * @since 2.9.0 |
790 * @access private |
892 * @access private |
794 */ |
896 */ |
795 function _wp_relative_upload_path( $path ) { |
897 function _wp_relative_upload_path( $path ) { |
796 $new_path = $path; |
898 $new_path = $path; |
797 |
899 |
798 $uploads = wp_get_upload_dir(); |
900 $uploads = wp_get_upload_dir(); |
799 if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { |
901 if ( str_starts_with( $new_path, $uploads['basedir'] ) ) { |
800 $new_path = str_replace( $uploads['basedir'], '', $new_path ); |
902 $new_path = str_replace( $uploads['basedir'], '', $new_path ); |
801 $new_path = ltrim( $new_path, '/' ); |
903 $new_path = ltrim( $new_path, '/' ); |
802 } |
904 } |
803 |
905 |
804 /** |
906 /** |
811 */ |
913 */ |
812 return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
914 return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
813 } |
915 } |
814 |
916 |
815 /** |
917 /** |
816 * Retrieve all children of the post parent ID. |
918 * Retrieves all children of the post parent ID. |
817 * |
919 * |
818 * Normally, without any enhancements, the children would apply to pages. In the |
920 * Normally, without any enhancements, the children would apply to pages. In the |
819 * context of the inner workings of WordPress, pages, posts, and attachments |
921 * context of the inner workings of WordPress, pages, posts, and attachments |
820 * share the same table, so therefore the functionality could apply to any one |
922 * share the same table, so therefore the functionality could apply to any one |
821 * of them. It is then noted that while this function does not work on posts, it |
923 * of them. It is then noted that while this function does not work on posts, it |
859 * |
961 * |
860 * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. |
962 * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. |
861 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
963 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
862 * correspond to a WP_Post object, an associative array, or a numeric array, |
964 * correspond to a WP_Post object, an associative array, or a numeric array, |
863 * respectively. Default OBJECT. |
965 * respectively. Default OBJECT. |
864 * @return WP_Post[]|int[] Array of post objects or post IDs. |
966 * @return WP_Post[]|array[]|int[] Array of post objects, arrays, or IDs, depending on `$output`. |
865 */ |
967 */ |
866 function get_children( $args = '', $output = OBJECT ) { |
968 function get_children( $args = '', $output = OBJECT ) { |
867 $kids = array(); |
969 $kids = array(); |
868 if ( empty( $args ) ) { |
970 if ( empty( $args ) ) { |
869 if ( isset( $GLOBALS['post'] ) ) { |
971 if ( isset( $GLOBALS['post'] ) ) { |
920 return $kids; |
1022 return $kids; |
921 } |
1023 } |
922 } |
1024 } |
923 |
1025 |
924 /** |
1026 /** |
925 * Get extended entry info (<!--more-->). |
1027 * Gets extended entry info (<!--more-->). |
926 * |
1028 * |
927 * There should not be any space after the second dash and before the word |
1029 * There should not be any space after the second dash and before the word |
928 * 'more'. There can be text or space(s) after the word 'more', but won't be |
1030 * 'more'. There can be text or space(s) after the word 'more', but won't be |
929 * referenced. |
1031 * referenced. |
930 * |
1032 * |
1054 |
1156 |
1055 return $ancestors; |
1157 return $ancestors; |
1056 } |
1158 } |
1057 |
1159 |
1058 /** |
1160 /** |
1059 * Retrieve data from a post field based on Post ID. |
1161 * Retrieves data from a post field based on Post ID. |
1060 * |
1162 * |
1061 * Examples of the post field will be, 'post_type', 'post_status', 'post_content', |
1163 * Examples of the post field will be, 'post_type', 'post_status', 'post_content', |
1062 * etc and based off of the post object property or key names. |
1164 * etc and based off of the post object property or key names. |
1063 * |
1165 * |
1064 * The context values are based off of the taxonomy filter functions and |
1166 * The context values are based off of the taxonomy filter functions and |
1088 |
1190 |
1089 return sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
1191 return sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
1090 } |
1192 } |
1091 |
1193 |
1092 /** |
1194 /** |
1093 * Retrieve the mime type of an attachment based on the ID. |
1195 * Retrieves the mime type of an attachment based on the ID. |
1094 * |
1196 * |
1095 * This function can be used with any post type, but it makes more sense with |
1197 * This function can be used with any post type, but it makes more sense with |
1096 * attachments. |
1198 * attachments. |
1097 * |
1199 * |
1098 * @since 2.0.0 |
1200 * @since 2.0.0 |
1109 |
1211 |
1110 return false; |
1212 return false; |
1111 } |
1213 } |
1112 |
1214 |
1113 /** |
1215 /** |
1114 * Retrieve the post status based on the post ID. |
1216 * Retrieves the post status based on the post ID. |
1115 * |
1217 * |
1116 * If the post ID is of an attachment, then the parent post status will be given |
1218 * If the post ID is of an attachment, then the parent post status will be given |
1117 * instead. |
1219 * instead. |
1118 * |
1220 * |
1119 * @since 2.0.0 |
1221 * @since 2.0.0 |
1120 * |
1222 * |
1121 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
1223 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
1122 * @return string|false Post status on success, false on failure. |
1224 * @return string|false Post status on success, false on failure. |
1123 */ |
1225 */ |
1124 function get_post_status( $post = null ) { |
1226 function get_post_status( $post = null ) { |
1125 $post = get_post( $post ); |
1227 // Normalize the post object if necessary, skip normalization if called from get_sample_permalink(). |
1228 if ( ! $post instanceof WP_Post || ! isset( $post->filter ) || 'sample' !== $post->filter ) { |
|
1229 $post = get_post( $post ); |
|
1230 } |
|
1126 |
1231 |
1127 if ( ! is_object( $post ) ) { |
1232 if ( ! is_object( $post ) ) { |
1128 return false; |
1233 return false; |
1129 } |
1234 } |
1130 |
1235 |
1142 // Unattached attachments with inherit status are assumed to be published. |
1247 // Unattached attachments with inherit status are assumed to be published. |
1143 $post_status = 'publish'; |
1248 $post_status = 'publish'; |
1144 } elseif ( 'trash' === get_post_status( $post->post_parent ) ) { |
1249 } elseif ( 'trash' === get_post_status( $post->post_parent ) ) { |
1145 // Get parent status prior to trashing. |
1250 // Get parent status prior to trashing. |
1146 $post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); |
1251 $post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); |
1252 |
|
1147 if ( ! $post_status ) { |
1253 if ( ! $post_status ) { |
1148 // Assume publish as above. |
1254 // Assume publish as above. |
1149 $post_status = 'publish'; |
1255 $post_status = 'publish'; |
1150 } |
1256 } |
1151 } else { |
1257 } else { |
1176 */ |
1282 */ |
1177 return apply_filters( 'get_post_status', $post_status, $post ); |
1283 return apply_filters( 'get_post_status', $post_status, $post ); |
1178 } |
1284 } |
1179 |
1285 |
1180 /** |
1286 /** |
1181 * Retrieve all of the WordPress supported post statuses. |
1287 * Retrieves all of the WordPress supported post statuses. |
1182 * |
1288 * |
1183 * Posts have a limited set of valid status values, this provides the |
1289 * Posts have a limited set of valid status values, this provides the |
1184 * post_status values and descriptions. |
1290 * post_status values and descriptions. |
1185 * |
1291 * |
1186 * @since 2.5.0 |
1292 * @since 2.5.0 |
1197 |
1303 |
1198 return $status; |
1304 return $status; |
1199 } |
1305 } |
1200 |
1306 |
1201 /** |
1307 /** |
1202 * Retrieve all of the WordPress support page statuses. |
1308 * Retrieves all of the WordPress support page statuses. |
1203 * |
1309 * |
1204 * Pages have a limited set of valid status values, this provides the |
1310 * Pages have a limited set of valid status values, this provides the |
1205 * post_status values and descriptions. |
1311 * post_status values and descriptions. |
1206 * |
1312 * |
1207 * @since 2.5.0 |
1313 * @since 2.5.0 |
1217 |
1323 |
1218 return $status; |
1324 return $status; |
1219 } |
1325 } |
1220 |
1326 |
1221 /** |
1327 /** |
1222 * Return statuses for privacy requests. |
1328 * Returns statuses for privacy requests. |
1223 * |
1329 * |
1224 * @since 4.9.6 |
1330 * @since 4.9.6 |
1225 * @access private |
1331 * @access private |
1226 * |
1332 * |
1227 * @return array |
1333 * @return string[] Array of privacy request status labels keyed by their status. |
1228 */ |
1334 */ |
1229 function _wp_privacy_statuses() { |
1335 function _wp_privacy_statuses() { |
1230 return array( |
1336 return array( |
1231 'request-pending' => _x( 'Pending', 'request status' ), // Pending confirmation from user. |
1337 'request-pending' => _x( 'Pending', 'request status' ), // Pending confirmation from user. |
1232 'request-confirmed' => _x( 'Confirmed', 'request status' ), // User has confirmed the action. |
1338 'request-confirmed' => _x( 'Confirmed', 'request status' ), // User has confirmed the action. |
1234 'request-completed' => _x( 'Completed', 'request status' ), // Admin has handled the request. |
1340 'request-completed' => _x( 'Completed', 'request status' ), // Admin has handled the request. |
1235 ); |
1341 ); |
1236 } |
1342 } |
1237 |
1343 |
1238 /** |
1344 /** |
1239 * Register a post status. Do not use before init. |
1345 * Registers a post status. Do not use before init. |
1240 * |
1346 * |
1241 * A simple function for creating or modifying a post status based on the |
1347 * A simple function for creating or modifying a post status based on the |
1242 * parameters given. The function will accept an array (second optional |
1348 * parameters given. The function will accept an array (second optional |
1243 * parameter), along with a string for the post status name. |
1349 * parameter), along with a string for the post status name. |
1244 * |
1350 * |
1252 * @param array|string $args { |
1358 * @param array|string $args { |
1253 * Optional. Array or string of post status arguments. |
1359 * Optional. Array or string of post status arguments. |
1254 * |
1360 * |
1255 * @type bool|string $label A descriptive name for the post status marked |
1361 * @type bool|string $label A descriptive name for the post status marked |
1256 * for translation. Defaults to value of $post_status. |
1362 * for translation. Defaults to value of $post_status. |
1257 * @type bool|array $label_count Descriptive text to use for nooped plurals. |
1363 * @type array|false $label_count Nooped plural text from _n_noop() to provide the singular |
1258 * Default array of $label, twice. |
1364 * and plural forms of the label for counts. Default false |
1365 * which means the `$label` argument will be used for both |
|
1366 * the singular and plural forms of this label. |
|
1259 * @type bool $exclude_from_search Whether to exclude posts with this post status |
1367 * @type bool $exclude_from_search Whether to exclude posts with this post status |
1260 * from search results. Default is value of $internal. |
1368 * from search results. Default is value of $internal. |
1261 * @type bool $_builtin Whether the status is built-in. Core-use only. |
1369 * @type bool $_builtin Whether the status is built-in. Core-use only. |
1262 * Default false. |
1370 * Default false. |
1263 * @type bool $public Whether posts of this status should be shown |
1371 * @type bool $public Whether posts of this status should be shown |
1354 if ( false === $args->label ) { |
1462 if ( false === $args->label ) { |
1355 $args->label = $post_status; |
1463 $args->label = $post_status; |
1356 } |
1464 } |
1357 |
1465 |
1358 if ( false === $args->label_count ) { |
1466 if ( false === $args->label_count ) { |
1359 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
1467 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural |
1360 $args->label_count = _n_noop( $args->label, $args->label ); |
1468 $args->label_count = _n_noop( $args->label, $args->label ); |
1361 } |
1469 } |
1362 |
1470 |
1363 $wp_post_statuses[ $post_status ] = $args; |
1471 $wp_post_statuses[ $post_status ] = $args; |
1364 |
1472 |
1365 return $args; |
1473 return $args; |
1366 } |
1474 } |
1367 |
1475 |
1368 /** |
1476 /** |
1369 * Retrieve a post status object by name. |
1477 * Retrieves a post status object by name. |
1370 * |
1478 * |
1371 * @since 3.0.0 |
1479 * @since 3.0.0 |
1372 * |
1480 * |
1373 * @global stdClass[] $wp_post_statuses List of post statuses. |
1481 * @global stdClass[] $wp_post_statuses List of post statuses. |
1374 * |
1482 * |
1386 |
1494 |
1387 return $wp_post_statuses[ $post_status ]; |
1495 return $wp_post_statuses[ $post_status ]; |
1388 } |
1496 } |
1389 |
1497 |
1390 /** |
1498 /** |
1391 * Get a list of post statuses. |
1499 * Gets a list of post statuses. |
1392 * |
1500 * |
1393 * @since 3.0.0 |
1501 * @since 3.0.0 |
1394 * |
1502 * |
1395 * @global stdClass[] $wp_post_statuses List of post statuses. |
1503 * @global stdClass[] $wp_post_statuses List of post statuses. |
1396 * |
1504 * |
1411 |
1519 |
1412 return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field ); |
1520 return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field ); |
1413 } |
1521 } |
1414 |
1522 |
1415 /** |
1523 /** |
1416 * Whether the post type is hierarchical. |
1524 * Determines whether the post type is hierarchical. |
1417 * |
1525 * |
1418 * A false return value might also mean that the post type does not exist. |
1526 * A false return value might also mean that the post type does not exist. |
1419 * |
1527 * |
1420 * @since 3.0.0 |
1528 * @since 3.0.0 |
1421 * |
1529 * |
1490 |
1598 |
1491 return $wp_post_types[ $post_type ]; |
1599 return $wp_post_types[ $post_type ]; |
1492 } |
1600 } |
1493 |
1601 |
1494 /** |
1602 /** |
1495 * Get a list of all registered post type objects. |
1603 * Gets a list of all registered post type objects. |
1496 * |
1604 * |
1497 * @since 2.9.0 |
1605 * @since 2.9.0 |
1498 * |
1606 * |
1499 * @global array $wp_post_types List of post types. |
1607 * @global array $wp_post_types List of post types. |
1500 * |
1608 * |
1501 * @see register_post_type() for accepted arguments. |
1609 * @see register_post_type() for accepted arguments. |
1502 * |
1610 * |
1503 * @param array|string $args Optional. An array of key => value arguments to match against |
1611 * @param array|string $args Optional. An array of key => value arguments to match against |
1504 * the post type objects. Default empty array. |
1612 * the post type objects. Default empty array. |
1505 * @param string $output Optional. The type of output to return. Accepts post type 'names' |
1613 * @param string $output Optional. The type of output to return. Either 'names' |
1506 * or 'objects'. Default 'names'. |
1614 * or 'objects'. Default 'names'. |
1507 * @param string $operator Optional. The logical operation to perform. 'or' means only one |
1615 * @param string $operator Optional. The logical operation to perform. 'or' means only one |
1508 * element from the array needs to match; 'and' means all elements |
1616 * element from the array needs to match; 'and' means all elements |
1509 * must match; 'not' means no elements may match. Default 'and'. |
1617 * must match; 'not' means no elements may match. Default 'and'. |
1510 * @return string[]|WP_Post_Type[] An array of post type names or objects. |
1618 * @return string[]|WP_Post_Type[] An array of post type names or objects. |
1542 * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature. |
1650 * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature. |
1543 * @since 5.9.0 The `rest_namespace` argument was added. |
1651 * @since 5.9.0 The `rest_namespace` argument was added. |
1544 * |
1652 * |
1545 * @global array $wp_post_types List of post types. |
1653 * @global array $wp_post_types List of post types. |
1546 * |
1654 * |
1547 * @param string $post_type Post type key. Must not exceed 20 characters and may |
1655 * @param string $post_type Post type key. Must not exceed 20 characters and may only contain |
1548 * only contain lowercase alphanumeric characters, dashes, |
1656 * lowercase alphanumeric characters, dashes, and underscores. See sanitize_key(). |
1549 * and underscores. See sanitize_key(). |
|
1550 * @param array|string $args { |
1657 * @param array|string $args { |
1551 * Array or string of arguments for registering a post type. |
1658 * Array or string of arguments for registering a post type. |
1552 * |
1659 * |
1553 * @type string $label Name of the post type shown in the menu. Usually plural. |
1660 * @type string $label Name of the post type shown in the menu. Usually plural. |
1554 * Default is value of $labels['name']. |
1661 * Default is value of $labels['name']. |
1555 * @type string[] $labels An array of labels for this post type. If not set, post |
1662 * @type string[] $labels An array of labels for this post type. If not set, post |
1556 * labels are inherited for non-hierarchical types and page |
1663 * labels are inherited for non-hierarchical types and page |
1557 * labels for hierarchical ones. See get_post_type_labels() for a full |
1664 * labels for hierarchical ones. See get_post_type_labels() for a full |
1558 * list of supported labels. |
1665 * list of supported labels. |
1559 * @type string $description A short descriptive summary of what the post type is. |
1666 * @type string $description A short descriptive summary of what the post type is. |
1560 * Default empty. |
1667 * Default empty. |
1561 * @type bool $public Whether a post type is intended for use publicly either via |
1668 * @type bool $public Whether a post type is intended for use publicly either via |
1562 * the admin interface or by front-end users. While the default |
1669 * the admin interface or by front-end users. While the default |
1563 * settings of $exclude_from_search, $publicly_queryable, $show_ui, |
1670 * settings of $exclude_from_search, $publicly_queryable, $show_ui, |
1564 * and $show_in_nav_menus are inherited from $public, each does not |
1671 * and $show_in_nav_menus are inherited from $public, each does not |
1565 * rely on this relationship and controls a very specific intention. |
1672 * rely on this relationship and controls a very specific intention. |
1566 * Default false. |
1673 * Default false. |
1567 * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. |
1674 * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. |
1568 * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search |
1675 * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search |
1569 * results. Default is the opposite value of $public. |
1676 * results. Default is the opposite value of $public. |
1570 * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type |
1677 * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type |
1571 * as part of parse_request(). Endpoints would include: |
1678 * as part of parse_request(). Endpoints would include: |
1572 * * ?post_type={post_type_key} |
1679 * * ?post_type={post_type_key} |
1573 * * ?{post_type_key}={single_post_slug} |
1680 * * ?{post_type_key}={single_post_slug} |
1574 * * ?{post_type_query_var}={single_post_slug} |
1681 * * ?{post_type_query_var}={single_post_slug} |
1575 * If not set, the default is inherited from $public. |
1682 * If not set, the default is inherited from $public. |
1576 * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the |
1683 * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the |
1577 * admin. Default is value of $public. |
1684 * admin. Default is value of $public. |
1578 * @type bool|string $show_in_menu Where to show the post type in the admin menu. To work, $show_ui |
1685 * @type bool|string $show_in_menu Where to show the post type in the admin menu. To work, $show_ui |
1579 * must be true. If true, the post type is shown in its own top level |
1686 * must be true. If true, the post type is shown in its own top level |
1580 * menu. If false, no menu is shown. If a string of an existing top |
1687 * menu. If false, no menu is shown. If a string of an existing top |
1581 * level menu ('tools.php' or 'edit.php?post_type=page', for example), the |
1688 * level menu ('tools.php' or 'edit.php?post_type=page', for example), the |
1582 * post type will be placed as a sub-menu of that. |
1689 * post type will be placed as a sub-menu of that. |
1583 * Default is value of $show_ui. |
1690 * Default is value of $show_ui. |
1584 * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. |
1691 * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. |
1585 * Default is value of $public. |
1692 * Default is value of $public. |
1586 * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value |
1693 * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value |
1587 * of $show_in_menu. |
1694 * of $show_in_menu. |
1588 * @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true |
1695 * @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true |
1589 * for the post type to be available in the block editor. |
1696 * for the post type to be available in the block editor. |
1590 * @type string $rest_base To change the base URL of REST API route. Default is $post_type. |
1697 * @type string $rest_base To change the base URL of REST API route. Default is $post_type. |
1591 * @type string $rest_namespace To change the namespace URL of REST API route. Default is wp/v2. |
1698 * @type string $rest_namespace To change the namespace URL of REST API route. Default is wp/v2. |
1592 * @type string $rest_controller_class REST API controller class name. Default is 'WP_REST_Posts_Controller'. |
1699 * @type string $rest_controller_class REST API controller class name. Default is 'WP_REST_Posts_Controller'. |
1593 * @type int $menu_position The position in the menu order the post type should appear. To work, |
1700 * @type string|bool $autosave_rest_controller_class REST API controller class name. Default is 'WP_REST_Autosaves_Controller'. |
1594 * $show_in_menu must be true. Default null (at the bottom). |
1701 * @type string|bool $revisions_rest_controller_class REST API controller class name. Default is 'WP_REST_Revisions_Controller'. |
1595 * @type string $menu_icon The URL to the icon to be used for this menu. Pass a base64-encoded |
1702 * @type bool $late_route_registration A flag to direct the REST API controllers for autosave / revisions |
1596 * SVG using a data URI, which will be colored to match the color scheme |
1703 * should be registered before/after the post type controller. |
1597 * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name |
1704 * @type int $menu_position The position in the menu order the post type should appear. To work, |
1598 * of a Dashicons helper class to use a font icon, e.g. |
1705 * $show_in_menu must be true. Default null (at the bottom). |
1599 * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty |
1706 * @type string $menu_icon The URL to the icon to be used for this menu. Pass a base64-encoded |
1600 * so an icon can be added via CSS. Defaults to use the posts icon. |
1707 * SVG using a data URI, which will be colored to match the color scheme |
1601 * @type string|array $capability_type The string to use to build the read, edit, and delete capabilities. |
1708 * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name |
1602 * May be passed as an array to allow for alternative plurals when using |
1709 * of a Dashicons helper class to use a font icon, e.g. |
1603 * this argument as a base to construct the capabilities, e.g. |
1710 * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty |
1604 * array('story', 'stories'). Default 'post'. |
1711 * so an icon can be added via CSS. Defaults to use the posts icon. |
1605 * @type string[] $capabilities Array of capabilities for this post type. $capability_type is used |
1712 * @type string|array $capability_type The string to use to build the read, edit, and delete capabilities. |
1606 * as a base to construct capabilities by default. |
1713 * May be passed as an array to allow for alternative plurals when using |
1607 * See get_post_type_capabilities(). |
1714 * this argument as a base to construct the capabilities, e.g. |
1608 * @type bool $map_meta_cap Whether to use the internal default meta capability handling. |
1715 * array('story', 'stories'). Default 'post'. |
1609 * Default false. |
1716 * @type string[] $capabilities Array of capabilities for this post type. $capability_type is used |
1610 * @type array $supports Core feature(s) the post type supports. Serves as an alias for calling |
1717 * as a base to construct capabilities by default. |
1611 * add_post_type_support() directly. Core features include 'title', |
1718 * See get_post_type_capabilities(). |
1612 * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
1719 * @type bool $map_meta_cap Whether to use the internal default meta capability handling. |
1613 * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
1720 * Default false. |
1614 * Additionally, the 'revisions' feature dictates whether the post type |
1721 * @type array|false $supports Core feature(s) the post type supports. Serves as an alias for calling |
1615 * will store revisions, and the 'comments' feature dictates whether the |
1722 * add_post_type_support() directly. Core features include 'title', |
1616 * comments count will show on the edit screen. A feature can also be |
1723 * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
1617 * specified as an array of arguments to provide additional information |
1724 * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
1618 * about supporting that feature. |
1725 * Additionally, the 'revisions' feature dictates whether the post type |
1619 * Example: `array( 'my_feature', array( 'field' => 'value' ) )`. |
1726 * will store revisions, the 'autosave' feature dictates whether the post type |
1620 * Default is an array containing 'title' and 'editor'. |
1727 * will be autosaved, and the 'comments' feature dictates whether the |
1621 * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the |
1728 * comments count will show on the edit screen. For backward compatibility reasons, |
1622 * edit form. Do remove_meta_box() and add_meta_box() calls in the |
1729 * adding 'editor' support implies 'autosave' support too. A feature can also be |
1623 * callback. Default null. |
1730 * specified as an array of arguments to provide additional information |
1624 * @type string[] $taxonomies An array of taxonomy identifiers that will be registered for the |
1731 * about supporting that feature. |
1625 * post type. Taxonomies can be registered later with register_taxonomy() |
1732 * Example: `array( 'my_feature', array( 'field' => 'value' ) )`. |
1626 * or register_taxonomy_for_object_type(). |
1733 * If false, no features will be added. |
1627 * Default empty array. |
1734 * Default is an array containing 'title' and 'editor'. |
1628 * @type bool|string $has_archive Whether there should be post type archives, or if a string, the |
1735 * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the |
1629 * archive slug to use. Will generate the proper rewrite rules if |
1736 * edit form. Do remove_meta_box() and add_meta_box() calls in the |
1630 * $rewrite is enabled. Default false. |
1737 * callback. Default null. |
1631 * @type bool|array $rewrite { |
1738 * @type string[] $taxonomies An array of taxonomy identifiers that will be registered for the |
1739 * post type. Taxonomies can be registered later with register_taxonomy() |
|
1740 * or register_taxonomy_for_object_type(). |
|
1741 * Default empty array. |
|
1742 * @type bool|string $has_archive Whether there should be post type archives, or if a string, the |
|
1743 * archive slug to use. Will generate the proper rewrite rules if |
|
1744 * $rewrite is enabled. Default false. |
|
1745 * @type bool|array $rewrite { |
|
1632 * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
1746 * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
1633 * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be |
1747 * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be |
1634 * passed with any of these keys: |
1748 * passed with any of these keys: |
1635 * |
1749 * |
1636 * @type string $slug Customize the permastruct slug. Defaults to $post_type key. |
1750 * @type string $slug Customize the permastruct slug. Defaults to $post_type key. |
1641 * @type bool $pages Whether the permastruct should provide for pagination. Default true. |
1755 * @type bool $pages Whether the permastruct should provide for pagination. Default true. |
1642 * @type int $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, |
1756 * @type int $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, |
1643 * inherits from $permalink_epmask. If not specified and permalink_epmask |
1757 * inherits from $permalink_epmask. If not specified and permalink_epmask |
1644 * is not set, defaults to EP_PERMALINK. |
1758 * is not set, defaults to EP_PERMALINK. |
1645 * } |
1759 * } |
1646 * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type |
1760 * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type |
1647 * key. If false, a post type cannot be loaded at |
1761 * key. If false, a post type cannot be loaded at |
1648 * ?{query_var}={post_slug}. If specified as a string, the query |
1762 * ?{query_var}={post_slug}. If specified as a string, the query |
1649 * ?{query_var_string}={post_slug} will be valid. |
1763 * ?{query_var_string}={post_slug} will be valid. |
1650 * @type bool $can_export Whether to allow this post type to be exported. Default true. |
1764 * @type bool $can_export Whether to allow this post type to be exported. Default true. |
1651 * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. |
1765 * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. |
1652 * * If true, posts of this type belonging to the user will be moved |
1766 * * If true, posts of this type belonging to the user will be moved |
1653 * to Trash when the user is deleted. |
1767 * to Trash when the user is deleted. |
1654 * * If false, posts of this type belonging to the user will *not* |
1768 * * If false, posts of this type belonging to the user will *not* |
1655 * be trashed or deleted. |
1769 * be trashed or deleted. |
1656 * * If not set (the default), posts are trashed if post type supports |
1770 * * If not set (the default), posts are trashed if post type supports |
1657 * the 'author' feature. Otherwise posts are not trashed or deleted. |
1771 * the 'author' feature. Otherwise posts are not trashed or deleted. |
1658 * Default null. |
1772 * Default null. |
1659 * @type array $template Array of blocks to use as the default initial state for an editor |
1773 * @type array $template Array of blocks to use as the default initial state for an editor |
1660 * session. Each item should be an array containing block name and |
1774 * session. Each item should be an array containing block name and |
1661 * optional attributes. Default empty array. |
1775 * optional attributes. Default empty array. |
1662 * @type string|false $template_lock Whether the block template should be locked if $template is set. |
1776 * @type string|false $template_lock Whether the block template should be locked if $template is set. |
1663 * * If set to 'all', the user is unable to insert new blocks, |
1777 * * If set to 'all', the user is unable to insert new blocks, |
1664 * move existing blocks and delete blocks. |
1778 * move existing blocks and delete blocks. |
1665 * * If set to 'insert', the user is able to move existing blocks |
1779 * * If set to 'insert', the user is able to move existing blocks |
1666 * but is unable to insert new blocks and delete blocks. |
1780 * but is unable to insert new blocks and delete blocks. |
1667 * Default false. |
1781 * Default false. |
1668 * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or |
1782 * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or |
1669 * "built-in" post_type. Default false. |
1783 * "built-in" post_type. Default false. |
1670 * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of |
1784 * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of |
1671 * this post type. Default 'post.php?post=%d'. |
1785 * this post type. Default 'post.php?post=%d'. |
1672 * } |
1786 * } |
1673 * @return WP_Post_Type|WP_Error The registered post type object on success, |
1787 * @return WP_Post_Type|WP_Error The registered post type object on success, |
1674 * WP_Error object on failure. |
1788 * WP_Error object on failure. |
1675 */ |
1789 */ |
1676 function register_post_type( $post_type, $args = array() ) { |
1790 function register_post_type( $post_type, $args = array() ) { |
1774 |
1888 |
1775 return true; |
1889 return true; |
1776 } |
1890 } |
1777 |
1891 |
1778 /** |
1892 /** |
1779 * Build an object with all post type capabilities out of a post type object |
1893 * Builds an object with all post type capabilities out of a post type object |
1780 * |
1894 * |
1781 * Post type capabilities use the 'capability_type' argument as a base, if the |
1895 * Post type capabilities use the 'capability_type' argument as a base, if the |
1782 * capability is not set in the 'capabilities' argument array or if the |
1896 * capability is not set in the 'capabilities' argument array or if the |
1783 * 'capabilities' argument is not supplied. |
1897 * 'capabilities' argument is not supplied. |
1784 * |
1898 * |
1879 |
1993 |
1880 return (object) $capabilities; |
1994 return (object) $capabilities; |
1881 } |
1995 } |
1882 |
1996 |
1883 /** |
1997 /** |
1884 * Store or return a list of post type meta caps for map_meta_cap(). |
1998 * Stores or returns a list of post type meta caps for map_meta_cap(). |
1885 * |
1999 * |
1886 * @since 3.1.0 |
2000 * @since 3.1.0 |
1887 * @access private |
2001 * @access private |
1888 * |
2002 * |
1889 * @global array $post_type_meta_caps Used to store meta capabilities. |
2003 * @global array $post_type_meta_caps Used to store meta capabilities. |
1906 * Accepted keys of the label array in the post type object: |
2020 * Accepted keys of the label array in the post type object: |
1907 * |
2021 * |
1908 * - `name` - General name for the post type, usually plural. The same and overridden |
2022 * - `name` - General name for the post type, usually plural. The same and overridden |
1909 * by `$post_type_object->label`. Default is 'Posts' / 'Pages'. |
2023 * by `$post_type_object->label`. Default is 'Posts' / 'Pages'. |
1910 * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. |
2024 * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. |
1911 * - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. |
2025 * - `add_new` - Label for adding a new item. Default is 'Add New Post' / 'Add New Page'. |
1912 * When internationalizing this string, please use a {@link https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#disambiguation-by-context gettext context} |
|
1913 * matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`. |
|
1914 * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'. |
2026 * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'. |
1915 * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. |
2027 * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. |
1916 * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. |
2028 * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. |
1917 * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. |
2029 * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. |
1918 * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'. |
2030 * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'. |
1942 * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' |
2054 * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' |
1943 * - `item_published_privately` - Label used when an item is published with private visibility. |
2055 * - `item_published_privately` - Label used when an item is published with private visibility. |
1944 * Default is 'Post published privately.' / 'Page published privately.' |
2056 * Default is 'Post published privately.' / 'Page published privately.' |
1945 * - `item_reverted_to_draft` - Label used when an item is switched to a draft. |
2057 * - `item_reverted_to_draft` - Label used when an item is switched to a draft. |
1946 * Default is 'Post reverted to draft.' / 'Page reverted to draft.' |
2058 * Default is 'Post reverted to draft.' / 'Page reverted to draft.' |
2059 * - `item_trashed` - Label used when an item is moved to Trash. Default is 'Post trashed.' / 'Page trashed.' |
|
1947 * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / |
2060 * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / |
1948 * 'Page scheduled.' |
2061 * 'Page scheduled.' |
1949 * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' |
2062 * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' |
1950 * - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'. |
2063 * - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'. |
1951 * - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' / |
2064 * - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' / |
1965 * @since 4.7.0 Added the `view_items` and `attributes` labels. |
2078 * @since 4.7.0 Added the `view_items` and `attributes` labels. |
1966 * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, |
2079 * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, |
1967 * `item_scheduled`, and `item_updated` labels. |
2080 * `item_scheduled`, and `item_updated` labels. |
1968 * @since 5.7.0 Added the `filter_by_date` label. |
2081 * @since 5.7.0 Added the `filter_by_date` label. |
1969 * @since 5.8.0 Added the `item_link` and `item_link_description` labels. |
2082 * @since 5.8.0 Added the `item_link` and `item_link_description` labels. |
2083 * @since 6.3.0 Added the `item_trashed` label. |
|
2084 * @since 6.4.0 Changed default values for the `add_new` label to include the type of content. |
|
2085 * This matches `add_new_item` and provides more context for better accessibility. |
|
2086 * @since 6.6.0 Added the `template_name` label. |
|
1970 * |
2087 * |
1971 * @access private |
2088 * @access private |
1972 * |
2089 * |
1973 * @param object|WP_Post_Type $post_type_object Post type object. |
2090 * @param object|WP_Post_Type $post_type_object Post type object. |
1974 * @return object Object with all the labels as member variables. |
2091 * @return object Object with all the labels as member variables. |
1977 $nohier_vs_hier_defaults = WP_Post_Type::get_default_labels(); |
2094 $nohier_vs_hier_defaults = WP_Post_Type::get_default_labels(); |
1978 |
2095 |
1979 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
2096 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
1980 |
2097 |
1981 $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
2098 $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
2099 |
|
2100 if ( ! isset( $post_type_object->labels->template_name ) && isset( $post_type_object->labels->singular_name ) ) { |
|
2101 /* translators: %s: Post type name. */ |
|
2102 $labels->template_name = sprintf( __( 'Single item: %s' ), $post_type_object->labels->singular_name ); |
|
2103 } |
|
1982 |
2104 |
1983 $post_type = $post_type_object->name; |
2105 $post_type = $post_type_object->name; |
1984 |
2106 |
1985 $default_labels = clone $labels; |
2107 $default_labels = clone $labels; |
1986 |
2108 |
2009 |
2131 |
2010 return $labels; |
2132 return $labels; |
2011 } |
2133 } |
2012 |
2134 |
2013 /** |
2135 /** |
2014 * Build an object with custom-something object (post type, taxonomy) labels |
2136 * Builds an object with custom-something object (post type, taxonomy) labels |
2015 * out of a custom-something object |
2137 * out of a custom-something object |
2016 * |
2138 * |
2017 * @since 3.0.0 |
2139 * @since 3.0.0 |
2018 * @access private |
2140 * @access private |
2019 * |
2141 * |
2020 * @param object $object A custom-something object. |
2142 * @param object $data_object A custom-something object. |
2021 * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. |
2143 * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. |
2022 * @return object Object containing labels for the given custom-something object. |
2144 * @return object Object containing labels for the given custom-something object. |
2023 */ |
2145 */ |
2024 function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { |
2146 function _get_custom_object_labels( $data_object, $nohier_vs_hier_defaults ) { |
2025 $object->labels = (array) $object->labels; |
2147 $data_object->labels = (array) $data_object->labels; |
2026 |
2148 |
2027 if ( isset( $object->label ) && empty( $object->labels['name'] ) ) { |
2149 if ( isset( $data_object->label ) && empty( $data_object->labels['name'] ) ) { |
2028 $object->labels['name'] = $object->label; |
2150 $data_object->labels['name'] = $data_object->label; |
2029 } |
2151 } |
2030 |
2152 |
2031 if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) { |
2153 if ( ! isset( $data_object->labels['singular_name'] ) && isset( $data_object->labels['name'] ) ) { |
2032 $object->labels['singular_name'] = $object->labels['name']; |
2154 $data_object->labels['singular_name'] = $data_object->labels['name']; |
2033 } |
2155 } |
2034 |
2156 |
2035 if ( ! isset( $object->labels['name_admin_bar'] ) ) { |
2157 if ( ! isset( $data_object->labels['name_admin_bar'] ) ) { |
2036 $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; |
2158 $data_object->labels['name_admin_bar'] = |
2037 } |
2159 isset( $data_object->labels['singular_name'] ) |
2038 |
2160 ? $data_object->labels['singular_name'] |
2039 if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) { |
2161 : $data_object->name; |
2040 $object->labels['menu_name'] = $object->labels['name']; |
2162 } |
2041 } |
2163 |
2042 |
2164 if ( ! isset( $data_object->labels['menu_name'] ) && isset( $data_object->labels['name'] ) ) { |
2043 if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) { |
2165 $data_object->labels['menu_name'] = $data_object->labels['name']; |
2044 $object->labels['all_items'] = $object->labels['menu_name']; |
2166 } |
2045 } |
2167 |
2046 |
2168 if ( ! isset( $data_object->labels['all_items'] ) && isset( $data_object->labels['menu_name'] ) ) { |
2047 if ( ! isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) { |
2169 $data_object->labels['all_items'] = $data_object->labels['menu_name']; |
2048 $object->labels['archives'] = $object->labels['all_items']; |
2170 } |
2171 |
|
2172 if ( ! isset( $data_object->labels['archives'] ) && isset( $data_object->labels['all_items'] ) ) { |
|
2173 $data_object->labels['archives'] = $data_object->labels['all_items']; |
|
2049 } |
2174 } |
2050 |
2175 |
2051 $defaults = array(); |
2176 $defaults = array(); |
2052 foreach ( $nohier_vs_hier_defaults as $key => $value ) { |
2177 foreach ( $nohier_vs_hier_defaults as $key => $value ) { |
2053 $defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0]; |
2178 $defaults[ $key ] = $data_object->hierarchical ? $value[1] : $value[0]; |
2054 } |
2179 } |
2055 $labels = array_merge( $defaults, $object->labels ); |
2180 |
2056 $object->labels = (object) $object->labels; |
2181 $labels = array_merge( $defaults, $data_object->labels ); |
2182 $data_object->labels = (object) $data_object->labels; |
|
2057 |
2183 |
2058 return (object) $labels; |
2184 return (object) $labels; |
2059 } |
2185 } |
2060 |
2186 |
2061 /** |
2187 /** |
2062 * Add submenus for post types. |
2188 * Adds submenus for post types. |
2063 * |
2189 * |
2064 * @access private |
2190 * @access private |
2065 * @since 3.1.0 |
2191 * @since 3.1.0 |
2066 */ |
2192 */ |
2067 function _add_post_type_submenus() { |
2193 function _add_post_type_submenus() { |
2082 * screen, such as the editor or a meta box. Features include: 'title', 'editor', |
2208 * screen, such as the editor or a meta box. Features include: 'title', 'editor', |
2083 * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', |
2209 * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', |
2084 * 'thumbnail', 'custom-fields', and 'post-formats'. |
2210 * 'thumbnail', 'custom-fields', and 'post-formats'. |
2085 * |
2211 * |
2086 * Additionally, the 'revisions' feature dictates whether the post type will |
2212 * Additionally, the 'revisions' feature dictates whether the post type will |
2087 * store revisions, and the 'comments' feature dictates whether the comments |
2213 * store revisions, the 'autosave' feature dictates whether the post type |
2214 * will be autosaved, and the 'comments' feature dictates whether the comments |
|
2088 * count will show on the edit screen. |
2215 * count will show on the edit screen. |
2089 * |
2216 * |
2090 * A third, optional parameter can also be passed along with a feature to provide |
2217 * A third, optional parameter can also be passed along with a feature to provide |
2091 * additional information about supporting that feature. |
2218 * additional information about supporting that feature. |
2092 * |
2219 * |
2123 } |
2250 } |
2124 } |
2251 } |
2125 } |
2252 } |
2126 |
2253 |
2127 /** |
2254 /** |
2128 * Remove support for a feature from a post type. |
2255 * Removes support for a feature from a post type. |
2129 * |
2256 * |
2130 * @since 3.0.0 |
2257 * @since 3.0.0 |
2131 * |
2258 * |
2132 * @global array $_wp_post_type_features |
2259 * @global array $_wp_post_type_features |
2133 * |
2260 * |
2139 |
2266 |
2140 unset( $_wp_post_type_features[ $post_type ][ $feature ] ); |
2267 unset( $_wp_post_type_features[ $post_type ][ $feature ] ); |
2141 } |
2268 } |
2142 |
2269 |
2143 /** |
2270 /** |
2144 * Get all the post type features |
2271 * Gets all the post type features |
2145 * |
2272 * |
2146 * @since 3.4.0 |
2273 * @since 3.4.0 |
2147 * |
2274 * |
2148 * @global array $_wp_post_type_features |
2275 * @global array $_wp_post_type_features |
2149 * |
2276 * |
2159 |
2286 |
2160 return array(); |
2287 return array(); |
2161 } |
2288 } |
2162 |
2289 |
2163 /** |
2290 /** |
2164 * Check a post type's support for a given feature. |
2291 * Checks a post type's support for a given feature. |
2165 * |
2292 * |
2166 * @since 3.0.0 |
2293 * @since 3.0.0 |
2167 * |
2294 * |
2168 * @global array $_wp_post_type_features |
2295 * @global array $_wp_post_type_features |
2169 * |
2296 * |
2198 |
2325 |
2199 return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); |
2326 return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); |
2200 } |
2327 } |
2201 |
2328 |
2202 /** |
2329 /** |
2203 * Update the post type for the post ID. |
2330 * Updates the post type for the post ID. |
2204 * |
2331 * |
2205 * The page or post cache will be cleaned for the post ID. |
2332 * The page or post cache will be cleaned for the post ID. |
2206 * |
2333 * |
2207 * @since 2.5.0 |
2334 * @since 2.5.0 |
2208 * |
2335 * |
2239 * @return bool Whether the post type should be considered viewable. |
2366 * @return bool Whether the post type should be considered viewable. |
2240 */ |
2367 */ |
2241 function is_post_type_viewable( $post_type ) { |
2368 function is_post_type_viewable( $post_type ) { |
2242 if ( is_scalar( $post_type ) ) { |
2369 if ( is_scalar( $post_type ) ) { |
2243 $post_type = get_post_type_object( $post_type ); |
2370 $post_type = get_post_type_object( $post_type ); |
2371 |
|
2244 if ( ! $post_type ) { |
2372 if ( ! $post_type ) { |
2245 return false; |
2373 return false; |
2246 } |
2374 } |
2247 } |
2375 } |
2248 |
2376 |
2268 */ |
2396 */ |
2269 return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type ); |
2397 return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type ); |
2270 } |
2398 } |
2271 |
2399 |
2272 /** |
2400 /** |
2273 * Determine whether a post status is considered "viewable". |
2401 * Determines whether a post status is considered "viewable". |
2274 * |
2402 * |
2275 * For built-in post statuses such as publish and private, the 'public' value will be evaluated. |
2403 * For built-in post statuses such as publish and private, the 'public' value will be evaluated. |
2276 * For all others, the 'publicly_queryable' value will be used. |
2404 * For all others, the 'publicly_queryable' value will be used. |
2277 * |
2405 * |
2278 * @since 5.7.0 |
2406 * @since 5.7.0 |
2282 * @return bool Whether the post status should be considered viewable. |
2410 * @return bool Whether the post status should be considered viewable. |
2283 */ |
2411 */ |
2284 function is_post_status_viewable( $post_status ) { |
2412 function is_post_status_viewable( $post_status ) { |
2285 if ( is_scalar( $post_status ) ) { |
2413 if ( is_scalar( $post_status ) ) { |
2286 $post_status = get_post_status_object( $post_status ); |
2414 $post_status = get_post_status_object( $post_status ); |
2415 |
|
2287 if ( ! $post_status ) { |
2416 if ( ! $post_status ) { |
2288 return false; |
2417 return false; |
2289 } |
2418 } |
2290 } |
2419 } |
2291 |
2420 |
2315 */ |
2444 */ |
2316 return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status ); |
2445 return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status ); |
2317 } |
2446 } |
2318 |
2447 |
2319 /** |
2448 /** |
2320 * Determine whether a post is publicly viewable. |
2449 * Determines whether a post is publicly viewable. |
2321 * |
2450 * |
2322 * Posts are considered publicly viewable if both the post status and post type |
2451 * Posts are considered publicly viewable if both the post status and post type |
2323 * are viewable. |
2452 * are viewable. |
2324 * |
2453 * |
2325 * @since 5.7.0 |
2454 * @since 5.7.0 |
2404 } |
2533 } |
2405 |
2534 |
2406 $parsed_args['ignore_sticky_posts'] = true; |
2535 $parsed_args['ignore_sticky_posts'] = true; |
2407 $parsed_args['no_found_rows'] = true; |
2536 $parsed_args['no_found_rows'] = true; |
2408 |
2537 |
2409 $get_posts = new WP_Query; |
2538 $get_posts = new WP_Query(); |
2410 return $get_posts->query( $parsed_args ); |
2539 return $get_posts->query( $parsed_args ); |
2411 |
|
2412 } |
2540 } |
2413 |
2541 |
2414 // |
2542 // |
2415 // Post meta functions. |
2543 // Post meta functions. |
2416 // |
2544 // |
2561 function unregister_post_meta( $post_type, $meta_key ) { |
2689 function unregister_post_meta( $post_type, $meta_key ) { |
2562 return unregister_meta_key( 'post', $meta_key, $post_type ); |
2690 return unregister_meta_key( 'post', $meta_key, $post_type ); |
2563 } |
2691 } |
2564 |
2692 |
2565 /** |
2693 /** |
2566 * Retrieve post meta fields, based on post ID. |
2694 * Retrieves post meta fields, based on post ID. |
2567 * |
2695 * |
2568 * The post meta fields are retrieved from the cache where possible, |
2696 * The post meta fields are retrieved from the cache where possible, |
2569 * so the function is optimized to be called more than once. |
2697 * so the function is optimized to be called more than once. |
2570 * |
2698 * |
2571 * @since 1.2.0 |
2699 * @since 1.2.0 |
2575 * False for an invalid `$post_id` (non-numeric, zero, or negative value). |
2703 * False for an invalid `$post_id` (non-numeric, zero, or negative value). |
2576 * An empty string if a valid but non-existing post ID is passed. |
2704 * An empty string if a valid but non-existing post ID is passed. |
2577 */ |
2705 */ |
2578 function get_post_custom( $post_id = 0 ) { |
2706 function get_post_custom( $post_id = 0 ) { |
2579 $post_id = absint( $post_id ); |
2707 $post_id = absint( $post_id ); |
2708 |
|
2580 if ( ! $post_id ) { |
2709 if ( ! $post_id ) { |
2581 $post_id = get_the_ID(); |
2710 $post_id = get_the_ID(); |
2582 } |
2711 } |
2583 |
2712 |
2584 return get_post_meta( $post_id ); |
2713 return get_post_meta( $post_id ); |
2585 } |
2714 } |
2586 |
2715 |
2587 /** |
2716 /** |
2588 * Retrieve meta field names for a post. |
2717 * Retrieves meta field names for a post. |
2589 * |
2718 * |
2590 * If there are no meta fields, then nothing (null) will be returned. |
2719 * If there are no meta fields, then nothing (null) will be returned. |
2591 * |
2720 * |
2592 * @since 1.2.0 |
2721 * @since 1.2.0 |
2593 * |
2722 * |
2606 return $keys; |
2735 return $keys; |
2607 } |
2736 } |
2608 } |
2737 } |
2609 |
2738 |
2610 /** |
2739 /** |
2611 * Retrieve values for a custom post field. |
2740 * Retrieves values for a custom post field. |
2612 * |
2741 * |
2613 * The parameters must not be considered optional. All of the post meta fields |
2742 * The parameters must not be considered optional. All of the post meta fields |
2614 * will be retrieved and only the meta field key values returned. |
2743 * will be retrieved and only the meta field key values returned. |
2615 * |
2744 * |
2616 * @since 1.2.0 |
2745 * @since 1.2.0 |
2750 if ( 'raw' === $context ) { |
2879 if ( 'raw' === $context ) { |
2751 return $value; |
2880 return $value; |
2752 } |
2881 } |
2753 |
2882 |
2754 $prefixed = false; |
2883 $prefixed = false; |
2755 if ( false !== strpos( $field, 'post_' ) ) { |
2884 if ( str_contains( $field, 'post_' ) ) { |
2756 $prefixed = true; |
2885 $prefixed = true; |
2757 $field_no_prefix = str_replace( 'post_', '', $field ); |
2886 $field_no_prefix = str_replace( 'post_', '', $field ); |
2758 } |
2887 } |
2759 |
2888 |
2760 if ( 'edit' === $context ) { |
2889 if ( 'edit' === $context ) { |
2879 |
3008 |
2880 return $value; |
3009 return $value; |
2881 } |
3010 } |
2882 |
3011 |
2883 /** |
3012 /** |
2884 * Make a post sticky. |
3013 * Makes a post sticky. |
2885 * |
3014 * |
2886 * Sticky posts should be displayed at the top of the front page. |
3015 * Sticky posts should be displayed at the top of the front page. |
2887 * |
3016 * |
2888 * @since 2.7.0 |
3017 * @since 2.7.0 |
2889 * |
3018 * |
2916 do_action( 'post_stuck', $post_id ); |
3045 do_action( 'post_stuck', $post_id ); |
2917 } |
3046 } |
2918 } |
3047 } |
2919 |
3048 |
2920 /** |
3049 /** |
2921 * Un-stick a post. |
3050 * Un-sticks a post. |
2922 * |
3051 * |
2923 * Sticky posts should be displayed at the top of the front page. |
3052 * Sticky posts should be displayed at the top of the front page. |
2924 * |
3053 * |
2925 * @since 2.7.0 |
3054 * @since 2.7.0 |
2926 * |
3055 * |
2960 do_action( 'post_unstuck', $post_id ); |
3089 do_action( 'post_unstuck', $post_id ); |
2961 } |
3090 } |
2962 } |
3091 } |
2963 |
3092 |
2964 /** |
3093 /** |
2965 * Return the cache key for wp_count_posts() based on the passed arguments. |
3094 * Returns the cache key for wp_count_posts() based on the passed arguments. |
2966 * |
3095 * |
2967 * @since 3.9.0 |
3096 * @since 3.9.0 |
2968 * @access private |
3097 * @access private |
2969 * |
3098 * |
2970 * @param string $type Optional. Post type to retrieve count Default 'post'. |
3099 * @param string $type Optional. Post type to retrieve count Default 'post'. |
2984 |
3113 |
2985 return $cache_key; |
3114 return $cache_key; |
2986 } |
3115 } |
2987 |
3116 |
2988 /** |
3117 /** |
2989 * Count number of posts of a post type and if user has permissions to view. |
3118 * Counts number of posts of a post type and if user has permissions to view. |
2990 * |
3119 * |
2991 * This function provides an efficient method of finding the amount of post's |
3120 * This function provides an efficient method of finding the amount of post's |
2992 * type a blog has. Another method is to count the amount of items in |
3121 * type a blog has. Another method is to count the amount of items in |
2993 * get_posts(), but that method has a lot of overhead with doing so. Therefore, |
3122 * get_posts(), but that method has a lot of overhead with doing so. Therefore, |
2994 * when developing for 2.5+, use this function instead. |
3123 * when developing for 2.5+, use this function instead. |
3000 * |
3129 * |
3001 * @global wpdb $wpdb WordPress database abstraction object. |
3130 * @global wpdb $wpdb WordPress database abstraction object. |
3002 * |
3131 * |
3003 * @param string $type Optional. Post type to retrieve count. Default 'post'. |
3132 * @param string $type Optional. Post type to retrieve count. Default 'post'. |
3004 * @param string $perm Optional. 'readable' or empty. Default empty. |
3133 * @param string $perm Optional. 'readable' or empty. Default empty. |
3005 * @return stdClass Number of posts for each status. |
3134 * @return stdClass An object containing the number of posts for each status, |
3135 * or an empty object if the post type does not exist. |
|
3006 */ |
3136 */ |
3007 function wp_count_posts( $type = 'post', $perm = '' ) { |
3137 function wp_count_posts( $type = 'post', $perm = '' ) { |
3008 global $wpdb; |
3138 global $wpdb; |
3009 |
3139 |
3010 if ( ! post_type_exists( $type ) ) { |
3140 if ( ! post_type_exists( $type ) ) { |
3011 return new stdClass; |
3141 return new stdClass(); |
3012 } |
3142 } |
3013 |
3143 |
3014 $cache_key = _count_posts_cache_key( $type, $perm ); |
3144 $cache_key = _count_posts_cache_key( $type, $perm ); |
3015 |
3145 |
3016 $counts = wp_cache_get( $cache_key, 'counts' ); |
3146 $counts = wp_cache_get( $cache_key, 'counts' ); |
3049 |
3179 |
3050 $counts = (object) $counts; |
3180 $counts = (object) $counts; |
3051 wp_cache_set( $cache_key, $counts, 'counts' ); |
3181 wp_cache_set( $cache_key, $counts, 'counts' ); |
3052 |
3182 |
3053 /** |
3183 /** |
3054 * Modify returned post counts by status for the current post type. |
3184 * Filters the post counts by status for the current post type. |
3055 * |
3185 * |
3056 * @since 3.7.0 |
3186 * @since 3.7.0 |
3057 * |
3187 * |
3058 * @param stdClass $counts An object containing the current post_type's post |
3188 * @param stdClass $counts An object containing the current post_type's post |
3059 * counts by status. |
3189 * counts by status. |
3063 */ |
3193 */ |
3064 return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
3194 return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
3065 } |
3195 } |
3066 |
3196 |
3067 /** |
3197 /** |
3068 * Count number of attachments for the mime type(s). |
3198 * Counts number of attachments for the mime type(s). |
3069 * |
3199 * |
3070 * If you set the optional mime_type parameter, then an array will still be |
3200 * If you set the optional mime_type parameter, then an array will still be |
3071 * returned, but will only have the item you are looking for. It does not give |
3201 * returned, but will only have the item you are looking for. It does not give |
3072 * you the number of attachments that are children of a post. You can get that |
3202 * you the number of attachments that are children of a post. You can get that |
3073 * by counting the number of children that post has. |
3203 * by counting the number of children that post has. |
3081 * @return stdClass An object containing the attachment counts by mime type. |
3211 * @return stdClass An object containing the attachment counts by mime type. |
3082 */ |
3212 */ |
3083 function wp_count_attachments( $mime_type = '' ) { |
3213 function wp_count_attachments( $mime_type = '' ) { |
3084 global $wpdb; |
3214 global $wpdb; |
3085 |
3215 |
3086 $and = wp_post_mime_type_where( $mime_type ); |
3216 $cache_key = sprintf( |
3087 $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 ); |
3217 'attachments%s', |
3088 |
3218 ! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : '' |
3089 $counts = array(); |
3219 ); |
3090 foreach ( (array) $count as $row ) { |
3220 |
3091 $counts[ $row['post_mime_type'] ] = $row['num_posts']; |
3221 $counts = wp_cache_get( $cache_key, 'counts' ); |
3092 } |
3222 if ( false == $counts ) { |
3093 $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); |
3223 $and = wp_post_mime_type_where( $mime_type ); |
3224 $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 ); |
|
3225 |
|
3226 $counts = array(); |
|
3227 foreach ( (array) $count as $row ) { |
|
3228 $counts[ $row['post_mime_type'] ] = $row['num_posts']; |
|
3229 } |
|
3230 $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); |
|
3231 |
|
3232 wp_cache_set( $cache_key, (object) $counts, 'counts' ); |
|
3233 } |
|
3094 |
3234 |
3095 /** |
3235 /** |
3096 * Modify returned attachment counts by mime type. |
3236 * Filters the attachment counts by mime type. |
3097 * |
3237 * |
3098 * @since 3.7.0 |
3238 * @since 3.7.0 |
3099 * |
3239 * |
3100 * @param stdClass $counts An object containing the attachment counts by |
3240 * @param stdClass $counts An object containing the attachment counts by |
3101 * mime type. |
3241 * mime type. |
3103 */ |
3243 */ |
3104 return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); |
3244 return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); |
3105 } |
3245 } |
3106 |
3246 |
3107 /** |
3247 /** |
3108 * Get default post mime types. |
3248 * Gets default post mime types. |
3109 * |
3249 * |
3110 * @since 2.9.0 |
3250 * @since 2.9.0 |
3111 * @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups. |
3251 * @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups. |
3112 * |
3252 * |
3113 * @return array List of post mime types. |
3253 * @return array List of post mime types. |
3207 */ |
3347 */ |
3208 return apply_filters( 'post_mime_types', $post_mime_types ); |
3348 return apply_filters( 'post_mime_types', $post_mime_types ); |
3209 } |
3349 } |
3210 |
3350 |
3211 /** |
3351 /** |
3212 * Check a MIME-Type against a list. |
3352 * Checks a MIME-Type against a list. |
3213 * |
3353 * |
3214 * If the wildcard_mime_types parameter is a string, it must be comma separated |
3354 * If the `$wildcard_mime_types` parameter is a string, it must be comma separated |
3215 * list. If the real_mime_types is a string, it is also comma separated to |
3355 * list. If the `$real_mime_types` is a string, it is also comma separated to |
3216 * create the list. |
3356 * create the list. |
3217 * |
3357 * |
3218 * @since 2.5.0 |
3358 * @since 2.5.0 |
3219 * |
3359 * |
3220 * @param string|string[] $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*) |
3360 * @param string|string[] $wildcard_mime_types Mime types, e.g. `audio/mpeg`, `image` (same as `image/*`), |
3221 * or flash (same as *flash*). |
3361 * or `flash` (same as `*flash*`). |
3222 * @param string|string[] $real_mime_types Real post mime type values. |
3362 * @param string|string[] $real_mime_types Real post mime type values. |
3223 * @return array array(wildcard=>array(real types)). |
3363 * @return array array(wildcard=>array(real types)). |
3224 */ |
3364 */ |
3225 function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { |
3365 function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { |
3226 $matches = array(); |
3366 $matches = array(); |
3239 foreach ( $mimes as $mime ) { |
3379 foreach ( $mimes as $mime ) { |
3240 $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); |
3380 $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); |
3241 |
3381 |
3242 $patternses[][ $type ] = "^$regex$"; |
3382 $patternses[][ $type ] = "^$regex$"; |
3243 |
3383 |
3244 if ( false === strpos( $mime, '/' ) ) { |
3384 if ( ! str_contains( $mime, '/' ) ) { |
3245 $patternses[][ $type ] = "^$regex/"; |
3385 $patternses[][ $type ] = "^$regex/"; |
3246 $patternses[][ $type ] = $regex; |
3386 $patternses[][ $type ] = $regex; |
3247 } |
3387 } |
3248 } |
3388 } |
3249 } |
3389 } |
3263 |
3403 |
3264 return $matches; |
3404 return $matches; |
3265 } |
3405 } |
3266 |
3406 |
3267 /** |
3407 /** |
3268 * Convert MIME types into SQL. |
3408 * Converts MIME types into SQL. |
3269 * |
3409 * |
3270 * @since 2.5.0 |
3410 * @since 2.5.0 |
3271 * |
3411 * |
3272 * @param string|string[] $post_mime_types List of mime types or comma separated string |
3412 * @param string|string[] $post_mime_types List of mime types or comma separated string |
3273 * of mime types. |
3413 * of mime types. |
3280 $wildcards = array( '', '%', '%/%' ); |
3420 $wildcards = array( '', '%', '%/%' ); |
3281 if ( is_string( $post_mime_types ) ) { |
3421 if ( is_string( $post_mime_types ) ) { |
3282 $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); |
3422 $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); |
3283 } |
3423 } |
3284 |
3424 |
3285 $wheres = array(); |
3425 $where_clauses = array(); |
3286 |
3426 |
3287 foreach ( (array) $post_mime_types as $mime_type ) { |
3427 foreach ( (array) $post_mime_types as $mime_type ) { |
3288 $mime_type = preg_replace( '/\s/', '', $mime_type ); |
3428 $mime_type = preg_replace( '/\s/', '', $mime_type ); |
3289 $slashpos = strpos( $mime_type, '/' ); |
3429 $slashpos = strpos( $mime_type, '/' ); |
3290 if ( false !== $slashpos ) { |
3430 if ( false !== $slashpos ) { |
3296 $mime_subgroup = str_replace( '/', '', $mime_subgroup ); |
3436 $mime_subgroup = str_replace( '/', '', $mime_subgroup ); |
3297 } |
3437 } |
3298 $mime_pattern = "$mime_group/$mime_subgroup"; |
3438 $mime_pattern = "$mime_group/$mime_subgroup"; |
3299 } else { |
3439 } else { |
3300 $mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); |
3440 $mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); |
3301 if ( false === strpos( $mime_pattern, '*' ) ) { |
3441 if ( ! str_contains( $mime_pattern, '*' ) ) { |
3302 $mime_pattern .= '/*'; |
3442 $mime_pattern .= '/*'; |
3303 } |
3443 } |
3304 } |
3444 } |
3305 |
3445 |
3306 $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); |
3446 $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); |
3307 |
3447 |
3308 if ( in_array( $mime_type, $wildcards, true ) ) { |
3448 if ( in_array( $mime_type, $wildcards, true ) ) { |
3309 return ''; |
3449 return ''; |
3310 } |
3450 } |
3311 |
3451 |
3312 if ( false !== strpos( $mime_pattern, '%' ) ) { |
3452 if ( str_contains( $mime_pattern, '%' ) ) { |
3313 $wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
3453 $where_clauses[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
3314 } else { |
3454 } else { |
3315 $wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
3455 $where_clauses[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
3316 } |
3456 } |
3317 } |
3457 } |
3318 |
3458 |
3319 if ( ! empty( $wheres ) ) { |
3459 if ( ! empty( $where_clauses ) ) { |
3320 $where = ' AND (' . implode( ' OR ', $wheres ) . ') '; |
3460 $where = ' AND (' . implode( ' OR ', $where_clauses ) . ') '; |
3321 } |
3461 } |
3322 |
3462 |
3323 return $where; |
3463 return $where; |
3324 } |
3464 } |
3325 |
3465 |
3326 /** |
3466 /** |
3327 * Trash or delete a post or page. |
3467 * Trashes or deletes a post or page. |
3328 * |
3468 * |
3329 * When the post and page is permanently deleted, everything that is tied to |
3469 * When the post and page is permanently deleted, everything that is tied to |
3330 * it is deleted also. This includes comments, post meta fields, and terms |
3470 * it is deleted also. This includes comments, post meta fields, and terms |
3331 * associated with the post. |
3471 * associated with the post. |
3332 * |
3472 * |
3337 * |
3477 * |
3338 * @global wpdb $wpdb WordPress database abstraction object. |
3478 * @global wpdb $wpdb WordPress database abstraction object. |
3339 * @see wp_delete_attachment() |
3479 * @see wp_delete_attachment() |
3340 * @see wp_trash_post() |
3480 * @see wp_trash_post() |
3341 * |
3481 * |
3342 * @param int $postid Optional. Post ID. Default 0. |
3482 * @param int $post_id Optional. Post ID. Default 0. |
3343 * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. |
3483 * @param bool $force_delete Optional. Whether to bypass Trash and force deletion. |
3344 * Default false. |
3484 * Default false. |
3345 * @return WP_Post|false|null Post data on success, false or null on failure. |
3485 * @return WP_Post|false|null Post data on success, false or null on failure. |
3346 */ |
3486 */ |
3347 function wp_delete_post( $postid = 0, $force_delete = false ) { |
3487 function wp_delete_post( $post_id = 0, $force_delete = false ) { |
3348 global $wpdb; |
3488 global $wpdb; |
3349 |
3489 |
3350 $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) ); |
3490 $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) ); |
3351 |
3491 |
3352 if ( ! $post ) { |
3492 if ( ! $post ) { |
3353 return $post; |
3493 return $post; |
3354 } |
3494 } |
3355 |
3495 |
3356 $post = get_post( $post ); |
3496 $post = get_post( $post ); |
3357 |
3497 |
3358 if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) { |
3498 if ( ! $force_delete |
3359 return wp_trash_post( $postid ); |
3499 && ( 'post' === $post->post_type || 'page' === $post->post_type ) |
3500 && 'trash' !== get_post_status( $post_id ) && EMPTY_TRASH_DAYS |
|
3501 ) { |
|
3502 return wp_trash_post( $post_id ); |
|
3360 } |
3503 } |
3361 |
3504 |
3362 if ( 'attachment' === $post->post_type ) { |
3505 if ( 'attachment' === $post->post_type ) { |
3363 return wp_delete_attachment( $postid, $force_delete ); |
3506 return wp_delete_attachment( $post_id, $force_delete ); |
3364 } |
3507 } |
3365 |
3508 |
3366 /** |
3509 /** |
3367 * Filters whether a post deletion should take place. |
3510 * Filters whether a post deletion should take place. |
3368 * |
3511 * |
3369 * @since 4.4.0 |
3512 * @since 4.4.0 |
3370 * |
3513 * |
3371 * @param WP_Post|false|null $delete Whether to go forward with deletion. @TODO description |
3514 * @param WP_Post|false|null $delete Whether to go forward with deletion. |
3372 * @param WP_Post $post Post object. |
3515 * @param WP_Post $post Post object. |
3373 * @param bool $force_delete Whether to bypass the Trash. |
3516 * @param bool $force_delete Whether to bypass the Trash. |
3374 */ |
3517 */ |
3375 $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); |
3518 $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); |
3376 if ( null !== $check ) { |
3519 if ( null !== $check ) { |
3383 * @since 3.2.0 |
3526 * @since 3.2.0 |
3384 * @since 5.5.0 Added the `$post` parameter. |
3527 * @since 5.5.0 Added the `$post` parameter. |
3385 * |
3528 * |
3386 * @see wp_delete_post() |
3529 * @see wp_delete_post() |
3387 * |
3530 * |
3388 * @param int $postid Post ID. |
3531 * @param int $post_id Post ID. |
3389 * @param WP_Post $post Post object. |
3532 * @param WP_Post $post Post object. |
3390 */ |
3533 */ |
3391 do_action( 'before_delete_post', $postid, $post ); |
3534 do_action( 'before_delete_post', $post_id, $post ); |
3392 |
3535 |
3393 delete_post_meta( $postid, '_wp_trash_meta_status' ); |
3536 delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
3394 delete_post_meta( $postid, '_wp_trash_meta_time' ); |
3537 delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
3395 |
3538 |
3396 wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) ); |
3539 wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); |
3397 |
3540 |
3398 $parent_data = array( 'post_parent' => $post->post_parent ); |
3541 $parent_data = array( 'post_parent' => $post->post_parent ); |
3399 $parent_where = array( 'post_parent' => $postid ); |
3542 $parent_where = array( 'post_parent' => $post_id ); |
3400 |
3543 |
3401 if ( is_post_type_hierarchical( $post->post_type ) ) { |
3544 if ( is_post_type_hierarchical( $post->post_type ) ) { |
3402 // Point children of this page to its parent, also clean the cache of affected children. |
3545 // Point children of this page to its parent, also clean the cache of affected children. |
3403 $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); |
3546 $children_query = $wpdb->prepare( |
3404 $children = $wpdb->get_results( $children_query ); |
3547 "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", |
3548 $post_id, |
|
3549 $post->post_type |
|
3550 ); |
|
3551 |
|
3552 $children = $wpdb->get_results( $children_query ); |
|
3553 |
|
3405 if ( $children ) { |
3554 if ( $children ) { |
3406 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); |
3555 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); |
3407 } |
3556 } |
3408 } |
3557 } |
3409 |
3558 |
3410 // Do raw query. wp_get_post_revisions() is filtered. |
3559 // Do raw query. wp_get_post_revisions() is filtered. |
3411 $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
3560 $revision_ids = $wpdb->get_col( |
3561 $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $post_id ) |
|
3562 ); |
|
3563 |
|
3412 // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
3564 // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
3413 foreach ( $revision_ids as $revision_id ) { |
3565 foreach ( $revision_ids as $revision_id ) { |
3414 wp_delete_post_revision( $revision_id ); |
3566 wp_delete_post_revision( $revision_id ); |
3415 } |
3567 } |
3416 |
3568 |
3417 // Point all attachments to this post up one level. |
3569 // Point all attachments to this post up one level. |
3418 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
3570 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
3419 |
3571 |
3420 wp_defer_comment_counting( true ); |
3572 wp_defer_comment_counting( true ); |
3421 |
3573 |
3422 $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $postid ) ); |
3574 $comment_ids = $wpdb->get_col( |
3575 $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) |
|
3576 ); |
|
3577 |
|
3423 foreach ( $comment_ids as $comment_id ) { |
3578 foreach ( $comment_ids as $comment_id ) { |
3424 wp_delete_comment( $comment_id, true ); |
3579 wp_delete_comment( $comment_id, true ); |
3425 } |
3580 } |
3426 |
3581 |
3427 wp_defer_comment_counting( false ); |
3582 wp_defer_comment_counting( false ); |
3428 |
3583 |
3429 $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) ); |
3584 $post_meta_ids = $wpdb->get_col( |
3585 $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) |
|
3586 ); |
|
3587 |
|
3430 foreach ( $post_meta_ids as $mid ) { |
3588 foreach ( $post_meta_ids as $mid ) { |
3431 delete_metadata_by_mid( 'post', $mid ); |
3589 delete_metadata_by_mid( 'post', $mid ); |
3432 } |
3590 } |
3433 |
3591 |
3434 /** |
3592 /** |
3435 * Fires immediately before a post is deleted from the database. |
3593 * Fires immediately before a post is deleted from the database. |
3436 * |
3594 * |
3595 * The dynamic portion of the hook name, `$post->post_type`, refers to |
|
3596 * the post type slug. |
|
3597 * |
|
3598 * @since 6.6.0 |
|
3599 * |
|
3600 * @param int $post_id Post ID. |
|
3601 * @param WP_Post $post Post object. |
|
3602 */ |
|
3603 do_action( "delete_post_{$post->post_type}", $post_id, $post ); |
|
3604 |
|
3605 /** |
|
3606 * Fires immediately before a post is deleted from the database. |
|
3607 * |
|
3437 * @since 1.2.0 |
3608 * @since 1.2.0 |
3438 * @since 5.5.0 Added the `$post` parameter. |
3609 * @since 5.5.0 Added the `$post` parameter. |
3439 * |
3610 * |
3440 * @param int $postid Post ID. |
3611 * @param int $post_id Post ID. |
3441 * @param WP_Post $post Post object. |
3612 * @param WP_Post $post Post object. |
3442 */ |
3613 */ |
3443 do_action( 'delete_post', $postid, $post ); |
3614 do_action( 'delete_post', $post_id, $post ); |
3444 |
3615 |
3445 $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); |
3616 $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); |
3446 if ( ! $result ) { |
3617 if ( ! $result ) { |
3447 return false; |
3618 return false; |
3448 } |
3619 } |
3449 |
3620 |
3450 /** |
3621 /** |
3451 * Fires immediately after a post is deleted from the database. |
3622 * Fires immediately after a post is deleted from the database. |
3452 * |
3623 * |
3624 * The dynamic portion of the hook name, `$post->post_type`, refers to |
|
3625 * the post type slug. |
|
3626 * |
|
3627 * @since 6.6.0 |
|
3628 * |
|
3629 * @param int $post_id Post ID. |
|
3630 * @param WP_Post $post Post object. |
|
3631 */ |
|
3632 do_action( "deleted_post_{$post->post_type}", $post_id, $post ); |
|
3633 |
|
3634 /** |
|
3635 * Fires immediately after a post is deleted from the database. |
|
3636 * |
|
3453 * @since 2.2.0 |
3637 * @since 2.2.0 |
3454 * @since 5.5.0 Added the `$post` parameter. |
3638 * @since 5.5.0 Added the `$post` parameter. |
3455 * |
3639 * |
3456 * @param int $postid Post ID. |
3640 * @param int $post_id Post ID. |
3457 * @param WP_Post $post Post object. |
3641 * @param WP_Post $post Post object. |
3458 */ |
3642 */ |
3459 do_action( 'deleted_post', $postid, $post ); |
3643 do_action( 'deleted_post', $post_id, $post ); |
3460 |
3644 |
3461 clean_post_cache( $post ); |
3645 clean_post_cache( $post ); |
3462 |
3646 |
3463 if ( is_post_type_hierarchical( $post->post_type ) && $children ) { |
3647 if ( is_post_type_hierarchical( $post->post_type ) && $children ) { |
3464 foreach ( $children as $child ) { |
3648 foreach ( $children as $child ) { |
3465 clean_post_cache( $child ); |
3649 clean_post_cache( $child ); |
3466 } |
3650 } |
3467 } |
3651 } |
3468 |
3652 |
3469 wp_clear_scheduled_hook( 'publish_future_post', array( $postid ) ); |
3653 wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); |
3470 |
3654 |
3471 /** |
3655 /** |
3472 * Fires after a post is deleted, at the conclusion of wp_delete_post(). |
3656 * Fires after a post is deleted, at the conclusion of wp_delete_post(). |
3473 * |
3657 * |
3474 * @since 3.2.0 |
3658 * @since 3.2.0 |
3475 * @since 5.5.0 Added the `$post` parameter. |
3659 * @since 5.5.0 Added the `$post` parameter. |
3476 * |
3660 * |
3477 * @see wp_delete_post() |
3661 * @see wp_delete_post() |
3478 * |
3662 * |
3479 * @param int $postid Post ID. |
3663 * @param int $post_id Post ID. |
3480 * @param WP_Post $post Post object. |
3664 * @param WP_Post $post Post object. |
3481 */ |
3665 */ |
3482 do_action( 'after_delete_post', $postid, $post ); |
3666 do_action( 'after_delete_post', $post_id, $post ); |
3483 |
3667 |
3484 return $post; |
3668 return $post; |
3485 } |
3669 } |
3486 |
3670 |
3487 /** |
3671 /** |
3488 * Reset the page_on_front, show_on_front, and page_for_post settings when |
3672 * Resets the page_on_front, show_on_front, and page_for_post settings when |
3489 * a linked page is deleted or trashed. |
3673 * a linked page is deleted or trashed. |
3490 * |
3674 * |
3491 * Also ensures the post is no longer sticky. |
3675 * Also ensures the post is no longer sticky. |
3492 * |
3676 * |
3493 * @since 3.7.0 |
3677 * @since 3.7.0 |
3514 |
3698 |
3515 unstick_post( $post->ID ); |
3699 unstick_post( $post->ID ); |
3516 } |
3700 } |
3517 |
3701 |
3518 /** |
3702 /** |
3519 * Move a post or page to the Trash |
3703 * Moves a post or page to the Trash |
3520 * |
3704 * |
3521 * If Trash is disabled, the post or page is permanently deleted. |
3705 * If Trash is disabled, the post or page is permanently deleted. |
3522 * |
3706 * |
3523 * @since 2.9.0 |
3707 * @since 2.9.0 |
3524 * |
3708 * |
3541 |
3725 |
3542 if ( 'trash' === $post->post_status ) { |
3726 if ( 'trash' === $post->post_status ) { |
3543 return false; |
3727 return false; |
3544 } |
3728 } |
3545 |
3729 |
3730 $previous_status = $post->post_status; |
|
3731 |
|
3546 /** |
3732 /** |
3547 * Filters whether a post trashing should take place. |
3733 * Filters whether a post trashing should take place. |
3548 * |
3734 * |
3549 * @since 4.9.0 |
3735 * @since 4.9.0 |
3550 * |
3736 * @since 6.3.0 Added the `$previous_status` parameter. |
3551 * @param bool|null $trash Whether to go forward with trashing. |
3737 * |
3552 * @param WP_Post $post Post object. |
3738 * @param bool|null $trash Whether to go forward with trashing. |
3739 * @param WP_Post $post Post object. |
|
3740 * @param string $previous_status The status of the post about to be trashed. |
|
3553 */ |
3741 */ |
3554 $check = apply_filters( 'pre_trash_post', null, $post ); |
3742 $check = apply_filters( 'pre_trash_post', null, $post, $previous_status ); |
3555 |
3743 |
3556 if ( null !== $check ) { |
3744 if ( null !== $check ) { |
3557 return $check; |
3745 return $check; |
3558 } |
3746 } |
3559 |
3747 |
3560 /** |
3748 /** |
3561 * Fires before a post is sent to the Trash. |
3749 * Fires before a post is sent to the Trash. |
3562 * |
3750 * |
3563 * @since 3.3.0 |
3751 * @since 3.3.0 |
3564 * |
3752 * @since 6.3.0 Added the `$previous_status` parameter. |
3565 * @param int $post_id Post ID. |
3753 * |
3754 * @param int $post_id Post ID. |
|
3755 * @param string $previous_status The status of the post about to be trashed. |
|
3566 */ |
3756 */ |
3567 do_action( 'wp_trash_post', $post_id ); |
3757 do_action( 'wp_trash_post', $post_id, $previous_status ); |
3568 |
3758 |
3569 add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status ); |
3759 add_post_meta( $post_id, '_wp_trash_meta_status', $previous_status ); |
3570 add_post_meta( $post_id, '_wp_trash_meta_time', time() ); |
3760 add_post_meta( $post_id, '_wp_trash_meta_time', time() ); |
3571 |
3761 |
3572 $post_updated = wp_update_post( |
3762 $post_updated = wp_update_post( |
3573 array( |
3763 array( |
3574 'ID' => $post_id, |
3764 'ID' => $post_id, |
3584 |
3774 |
3585 /** |
3775 /** |
3586 * Fires after a post is sent to the Trash. |
3776 * Fires after a post is sent to the Trash. |
3587 * |
3777 * |
3588 * @since 2.9.0 |
3778 * @since 2.9.0 |
3589 * |
3779 * @since 6.3.0 Added the `$previous_status` parameter. |
3590 * @param int $post_id Post ID. |
3780 * |
3781 * @param int $post_id Post ID. |
|
3782 * @param string $previous_status The status of the post at the point where it was trashed. |
|
3591 */ |
3783 */ |
3592 do_action( 'trashed_post', $post_id ); |
3784 do_action( 'trashed_post', $post_id, $previous_status ); |
3593 |
3785 |
3594 return $post; |
3786 return $post; |
3595 } |
3787 } |
3596 |
3788 |
3597 /** |
3789 /** |
3621 |
3813 |
3622 /** |
3814 /** |
3623 * Filters whether a post untrashing should take place. |
3815 * Filters whether a post untrashing should take place. |
3624 * |
3816 * |
3625 * @since 4.9.0 |
3817 * @since 4.9.0 |
3626 * @since 5.6.0 The `$previous_status` parameter was added. |
3818 * @since 5.6.0 Added the `$previous_status` parameter. |
3627 * |
3819 * |
3628 * @param bool|null $untrash Whether to go forward with untrashing. |
3820 * @param bool|null $untrash Whether to go forward with untrashing. |
3629 * @param WP_Post $post Post object. |
3821 * @param WP_Post $post Post object. |
3630 * @param string $previous_status The status of the post at the point where it was trashed. |
3822 * @param string $previous_status The status of the post at the point where it was trashed. |
3631 */ |
3823 */ |
3636 |
3828 |
3637 /** |
3829 /** |
3638 * Fires before a post is restored from the Trash. |
3830 * Fires before a post is restored from the Trash. |
3639 * |
3831 * |
3640 * @since 2.9.0 |
3832 * @since 2.9.0 |
3641 * @since 5.6.0 The `$previous_status` parameter was added. |
3833 * @since 5.6.0 Added the `$previous_status` parameter. |
3642 * |
3834 * |
3643 * @param int $post_id Post ID. |
3835 * @param int $post_id Post ID. |
3644 * @param string $previous_status The status of the post at the point where it was trashed. |
3836 * @param string $previous_status The status of the post at the point where it was trashed. |
3645 */ |
3837 */ |
3646 do_action( 'untrash_post', $post_id, $previous_status ); |
3838 do_action( 'untrash_post', $post_id, $previous_status ); |
3682 |
3874 |
3683 /** |
3875 /** |
3684 * Fires after a post is restored from the Trash. |
3876 * Fires after a post is restored from the Trash. |
3685 * |
3877 * |
3686 * @since 2.9.0 |
3878 * @since 2.9.0 |
3687 * @since 5.6.0 The `$previous_status` parameter was added. |
3879 * @since 5.6.0 Added the `$previous_status` parameter. |
3688 * |
3880 * |
3689 * @param int $post_id Post ID. |
3881 * @param int $post_id Post ID. |
3690 * @param string $previous_status The status of the post at the point where it was trashed. |
3882 * @param string $previous_status The status of the post at the point where it was trashed. |
3691 */ |
3883 */ |
3692 do_action( 'untrashed_post', $post_id, $previous_status ); |
3884 do_action( 'untrashed_post', $post_id, $previous_status ); |
3754 |
3946 |
3755 return $result; |
3947 return $result; |
3756 } |
3948 } |
3757 |
3949 |
3758 /** |
3950 /** |
3759 * Restore comments for a post from the Trash. |
3951 * Restores comments for a post from the Trash. |
3760 * |
3952 * |
3761 * @since 2.9.0 |
3953 * @since 2.9.0 |
3762 * |
3954 * |
3763 * @global wpdb $wpdb WordPress database abstraction object. |
3955 * @global wpdb $wpdb WordPress database abstraction object. |
3764 * |
3956 * |
3796 foreach ( $statuses as $comment_id => $comment_status ) { |
3988 foreach ( $statuses as $comment_id => $comment_status ) { |
3797 $group_by_status[ $comment_status ][] = $comment_id; |
3989 $group_by_status[ $comment_status ][] = $comment_id; |
3798 } |
3990 } |
3799 |
3991 |
3800 foreach ( $group_by_status as $status => $comments ) { |
3992 foreach ( $group_by_status as $status => $comments ) { |
3801 // Sanity check. This shouldn't happen. |
3993 // Confidence check. This shouldn't happen. |
3802 if ( 'post-trashed' === $status ) { |
3994 if ( 'post-trashed' === $status ) { |
3803 $status = '0'; |
3995 $status = '0'; |
3804 } |
3996 } |
3805 $comments_in = implode( ', ', array_map( 'intval', $comments ) ); |
3997 $comments_in = implode( ', ', array_map( 'intval', $comments ) ); |
3806 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); |
3998 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); |
3819 */ |
4011 */ |
3820 do_action( 'untrashed_post_comments', $post_id ); |
4012 do_action( 'untrashed_post_comments', $post_id ); |
3821 } |
4013 } |
3822 |
4014 |
3823 /** |
4015 /** |
3824 * Retrieve the list of categories for a post. |
4016 * Retrieves the list of categories for a post. |
3825 * |
4017 * |
3826 * Compatibility layer for themes and plugins. Also an easy layer of abstraction |
4018 * Compatibility layer for themes and plugins. Also an easy layer of abstraction |
3827 * away from the complexity of the taxonomy layer. |
4019 * away from the complexity of the taxonomy layer. |
3828 * |
4020 * |
3829 * @since 2.1.0 |
4021 * @since 2.1.0 |
3848 $cats = wp_get_object_terms( $post_id, 'category', $args ); |
4040 $cats = wp_get_object_terms( $post_id, 'category', $args ); |
3849 return $cats; |
4041 return $cats; |
3850 } |
4042 } |
3851 |
4043 |
3852 /** |
4044 /** |
3853 * Retrieve the tags for a post. |
4045 * Retrieves the tags for a post. |
3854 * |
4046 * |
3855 * There is only one default for this function, called 'fields' and by default |
4047 * There is only one default for this function, called 'fields' and by default |
3856 * is set to 'all'. There are other defaults that can be overridden in |
4048 * is set to 'all'. There are other defaults that can be overridden in |
3857 * wp_get_object_terms(). |
4049 * wp_get_object_terms(). |
3858 * |
4050 * |
3896 |
4088 |
3897 return $tags; |
4089 return $tags; |
3898 } |
4090 } |
3899 |
4091 |
3900 /** |
4092 /** |
3901 * Retrieve a number of recent posts. |
4093 * Retrieves a number of recent posts. |
3902 * |
4094 * |
3903 * @since 1.0.0 |
4095 * @since 1.0.0 |
3904 * |
4096 * |
3905 * @see get_posts() |
4097 * @see get_posts() |
3906 * |
4098 * |
3945 } |
4137 } |
3946 return $results ? $results : array(); |
4138 return $results ? $results : array(); |
3947 } |
4139 } |
3948 |
4140 |
3949 return $results ? $results : false; |
4141 return $results ? $results : false; |
3950 |
4142 } |
3951 } |
4143 |
3952 |
4144 /** |
3953 /** |
4145 * Inserts or update a post. |
3954 * Insert or update a post. |
|
3955 * |
4146 * |
3956 * If the $postarr parameter has 'ID' set to a value, then post will be updated. |
4147 * If the $postarr parameter has 'ID' set to a value, then post will be updated. |
3957 * |
4148 * |
3958 * You can set the post date manually, by setting the values for 'post_date' |
4149 * You can set the post date manually, by setting the values for 'post_date' |
3959 * and 'post_date_gmt' keys. You can close the comments or open the comments by |
4150 * and 'post_date_gmt' keys. You can close the comments or open the comments by |
3993 * when creating a new post. |
4184 * when creating a new post. |
3994 * @type string $to_ping Space or carriage return-separated list of URLs to ping. |
4185 * @type string $to_ping Space or carriage return-separated list of URLs to ping. |
3995 * Default empty. |
4186 * Default empty. |
3996 * @type string $pinged Space or carriage return-separated list of URLs that have |
4187 * @type string $pinged Space or carriage return-separated list of URLs that have |
3997 * been pinged. Default empty. |
4188 * been pinged. Default empty. |
3998 * @type string $post_modified The date when the post was last modified. Default is |
|
3999 * the current time. |
|
4000 * @type string $post_modified_gmt The date when the post was last modified in the GMT |
|
4001 * timezone. Default is the current time. |
|
4002 * @type int $post_parent Set this for the post it belongs to, if any. Default 0. |
4189 * @type int $post_parent Set this for the post it belongs to, if any. Default 0. |
4003 * @type int $menu_order The order the post should be displayed in. Default 0. |
4190 * @type int $menu_order The order the post should be displayed in. Default 0. |
4004 * @type string $post_mime_type The mime type of the post. Default empty. |
4191 * @type string $post_mime_type The mime type of the post. Default empty. |
4005 * @type string $guid Global Unique ID for referencing the post. Default empty. |
4192 * @type string $guid Global Unique ID for referencing the post. Default empty. |
4006 * @type int $import_id The post ID to be used when inserting a new post. |
4193 * @type int $import_id The post ID to be used when inserting a new post. |
4015 * that contains term names or slugs, or a comma-separated string |
4202 * that contains term names or slugs, or a comma-separated string |
4016 * of names or slugs. This is because, in hierarchical taxonomy, |
4203 * of names or slugs. This is because, in hierarchical taxonomy, |
4017 * child terms can have the same names with different parent terms, |
4204 * child terms can have the same names with different parent terms, |
4018 * so the only way to connect them is using ID. Default empty. |
4205 * so the only way to connect them is using ID. Default empty. |
4019 * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. |
4206 * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. |
4207 * @type string $page_template Page template to use. |
|
4020 * } |
4208 * } |
4021 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
4209 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
4022 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
4210 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
4023 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
4211 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
4024 */ |
4212 */ |
4057 unset( $postarr['filter'] ); |
4245 unset( $postarr['filter'] ); |
4058 |
4246 |
4059 $postarr = sanitize_post( $postarr, 'db' ); |
4247 $postarr = sanitize_post( $postarr, 'db' ); |
4060 |
4248 |
4061 // Are we updating or creating? |
4249 // Are we updating or creating? |
4062 $post_ID = 0; |
4250 $post_id = 0; |
4063 $update = false; |
4251 $update = false; |
4064 $guid = $postarr['guid']; |
4252 $guid = $postarr['guid']; |
4065 |
4253 |
4066 if ( ! empty( $postarr['ID'] ) ) { |
4254 if ( ! empty( $postarr['ID'] ) ) { |
4067 $update = true; |
4255 $update = true; |
4068 |
4256 |
4069 // Get the post ID and GUID. |
4257 // Get the post ID and GUID. |
4070 $post_ID = $postarr['ID']; |
4258 $post_id = $postarr['ID']; |
4071 $post_before = get_post( $post_ID ); |
4259 $post_before = get_post( $post_id ); |
4072 |
4260 |
4073 if ( is_null( $post_before ) ) { |
4261 if ( is_null( $post_before ) ) { |
4074 if ( $wp_error ) { |
4262 if ( $wp_error ) { |
4075 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
4263 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
4076 } |
4264 } |
4077 return 0; |
4265 return 0; |
4078 } |
4266 } |
4079 |
4267 |
4080 $guid = get_post_field( 'guid', $post_ID ); |
4268 $guid = get_post_field( 'guid', $post_id ); |
4081 $previous_status = get_post_field( 'post_status', $post_ID ); |
4269 $previous_status = get_post_field( 'post_status', $post_id ); |
4082 } else { |
4270 } else { |
4083 $previous_status = 'new'; |
4271 $previous_status = 'new'; |
4084 $post_before = null; |
4272 $post_before = null; |
4085 } |
4273 } |
4086 |
4274 |
4134 } |
4322 } |
4135 |
4323 |
4136 if ( ! empty( $postarr['post_category'] ) ) { |
4324 if ( ! empty( $postarr['post_category'] ) ) { |
4137 // Filter out empty terms. |
4325 // Filter out empty terms. |
4138 $post_category = array_filter( $postarr['post_category'] ); |
4326 $post_category = array_filter( $postarr['post_category'] ); |
4327 } elseif ( $update && ! isset( $postarr['post_category'] ) ) { |
|
4328 $post_category = $post_before->post_category; |
|
4139 } |
4329 } |
4140 |
4330 |
4141 // Make sure we set a valid category. |
4331 // Make sure we set a valid category. |
4142 if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) { |
4332 if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) { |
4143 // 'post' requires at least one category. |
4333 // 'post' requires at least one category. |
4151 /* |
4341 /* |
4152 * Don't allow contributors to set the post slug for pending review posts. |
4342 * Don't allow contributors to set the post slug for pending review posts. |
4153 * |
4343 * |
4154 * For new posts check the primitive capability, for updates check the meta capability. |
4344 * For new posts check the primitive capability, for updates check the meta capability. |
4155 */ |
4345 */ |
4156 $post_type_object = get_post_type_object( $post_type ); |
4346 if ( 'pending' === $post_status ) { |
4157 |
4347 $post_type_object = get_post_type_object( $post_type ); |
4158 if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
4348 |
4159 $post_name = ''; |
4349 if ( ! $update && $post_type_object && ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
4160 } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) { |
4350 $post_name = ''; |
4161 $post_name = ''; |
4351 } elseif ( $update && ! current_user_can( 'publish_post', $post_id ) ) { |
4352 $post_name = ''; |
|
4353 } |
|
4162 } |
4354 } |
4163 |
4355 |
4164 /* |
4356 /* |
4165 * Create a valid post name. Drafts and pending posts are allowed to have |
4357 * Create a valid post name. Drafts and pending posts are allowed to have |
4166 * an empty post name. |
4358 * an empty post name. |
4173 } |
4365 } |
4174 } else { |
4366 } else { |
4175 // On updates, we need to check to see if it's using the old, fixed sanitization context. |
4367 // On updates, we need to check to see if it's using the old, fixed sanitization context. |
4176 $check_name = sanitize_title( $post_name, '', 'old-save' ); |
4368 $check_name = sanitize_title( $post_name, '', 'old-save' ); |
4177 |
4369 |
4178 if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { |
4370 if ( $update |
4371 && strtolower( urlencode( $post_name ) ) === $check_name |
|
4372 && get_post_field( 'post_name', $post_id ) === $check_name |
|
4373 ) { |
|
4179 $post_name = $check_name; |
4374 $post_name = $check_name; |
4180 } else { // new post, or slug has changed. |
4375 } else { // New post, or slug has changed. |
4181 $post_name = sanitize_title( $post_name ); |
4376 $post_name = sanitize_title( $post_name ); |
4182 } |
4377 } |
4183 } |
4378 } |
4184 |
4379 |
4185 /* |
4380 /* |
4186 * Resolve the post date from any provided post date or post date GMT strings; |
4381 * Resolve the post date from any provided post date or post date GMT strings; |
4187 * if none are provided, the date will be set to now. |
4382 * if none are provided, the date will be set to now. |
4188 */ |
4383 */ |
4189 $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] ); |
4384 $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] ); |
4385 |
|
4190 if ( ! $post_date ) { |
4386 if ( ! $post_date ) { |
4191 if ( $wp_error ) { |
4387 if ( $wp_error ) { |
4192 return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
4388 return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
4193 } else { |
4389 } else { |
4194 return 0; |
4390 return 0; |
4267 $post_parent = 0; |
4463 $post_parent = 0; |
4268 } |
4464 } |
4269 |
4465 |
4270 $new_postarr = array_merge( |
4466 $new_postarr = array_merge( |
4271 array( |
4467 array( |
4272 'ID' => $post_ID, |
4468 'ID' => $post_id, |
4273 ), |
4469 ), |
4274 compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) |
4470 compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) |
4275 ); |
4471 ); |
4276 |
4472 |
4277 /** |
4473 /** |
4278 * Filters the post parent -- used to check for and prevent hierarchy loops. |
4474 * Filters the post parent -- used to check for and prevent hierarchy loops. |
4279 * |
4475 * |
4280 * @since 3.1.0 |
4476 * @since 3.1.0 |
4281 * |
4477 * |
4282 * @param int $post_parent Post parent ID. |
4478 * @param int $post_parent Post parent ID. |
4283 * @param int $post_ID Post ID. |
4479 * @param int $post_id Post ID. |
4284 * @param array $new_postarr Array of parsed post data. |
4480 * @param array $new_postarr Array of parsed post data. |
4285 * @param array $postarr Array of sanitized, but otherwise unmodified post data. |
4481 * @param array $postarr Array of sanitized, but otherwise unmodified post data. |
4286 */ |
4482 */ |
4287 $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr ); |
4483 $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_id, $new_postarr, $postarr ); |
4288 |
4484 |
4289 /* |
4485 /* |
4290 * If the post is being untrashed and it has a desired slug stored in post meta, |
4486 * If the post is being untrashed and it has a desired slug stored in post meta, |
4291 * reassign it. |
4487 * reassign it. |
4292 */ |
4488 */ |
4293 if ( 'trash' === $previous_status && 'trash' !== $post_status ) { |
4489 if ( 'trash' === $previous_status && 'trash' !== $post_status ) { |
4294 $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); |
4490 $desired_post_slug = get_post_meta( $post_id, '_wp_desired_post_slug', true ); |
4295 |
4491 |
4296 if ( $desired_post_slug ) { |
4492 if ( $desired_post_slug ) { |
4297 delete_post_meta( $post_ID, '_wp_desired_post_slug' ); |
4493 delete_post_meta( $post_id, '_wp_desired_post_slug' ); |
4298 $post_name = $desired_post_slug; |
4494 $post_name = $desired_post_slug; |
4299 } |
4495 } |
4300 } |
4496 } |
4301 |
4497 |
4302 // If a trashed post has the desired slug, change it and let this post have it. |
4498 // If a trashed post has the desired slug, change it and let this post have it. |
4306 * |
4502 * |
4307 * @since 5.4.0 |
4503 * @since 5.4.0 |
4308 * |
4504 * |
4309 * @param bool $add_trashed_suffix Whether to attempt to add the suffix. |
4505 * @param bool $add_trashed_suffix Whether to attempt to add the suffix. |
4310 * @param string $post_name The name of the post being updated. |
4506 * @param string $post_name The name of the post being updated. |
4311 * @param int $post_ID Post ID. |
4507 * @param int $post_id Post ID. |
4312 */ |
4508 */ |
4313 $add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_ID ); |
4509 $add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_id ); |
4314 |
4510 |
4315 if ( $add_trashed_suffix ) { |
4511 if ( $add_trashed_suffix ) { |
4316 wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); |
4512 wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id ); |
4317 } |
4513 } |
4318 } |
4514 } |
4319 |
4515 |
4320 // When trashing an existing post, change its slug to allow non-trashed posts to use it. |
4516 // When trashing an existing post, change its slug to allow non-trashed posts to use it. |
4321 if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { |
4517 if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { |
4322 $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID ); |
4518 $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_id ); |
4323 } |
4519 } |
4324 |
4520 |
4325 $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); |
4521 $post_name = wp_unique_post_slug( $post_name, $post_id, $post_status, $post_type, $post_parent ); |
4326 |
4522 |
4327 // Don't unslash. |
4523 // Don't unslash. |
4328 $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; |
4524 $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; |
4329 |
4525 |
4330 // Expected_slashed (everything!). |
4526 // Expected_slashed (everything!). |
4331 $data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ); |
4527 $data = compact( |
4528 'post_author', |
|
4529 'post_date', |
|
4530 'post_date_gmt', |
|
4531 'post_content', |
|
4532 'post_content_filtered', |
|
4533 'post_title', |
|
4534 'post_excerpt', |
|
4535 'post_status', |
|
4536 'post_type', |
|
4537 'comment_status', |
|
4538 'ping_status', |
|
4539 'post_password', |
|
4540 'post_name', |
|
4541 'to_ping', |
|
4542 'pinged', |
|
4543 'post_modified', |
|
4544 'post_modified_gmt', |
|
4545 'post_parent', |
|
4546 'menu_order', |
|
4547 'post_mime_type', |
|
4548 'guid' |
|
4549 ); |
|
4332 |
4550 |
4333 $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
4551 $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
4334 |
4552 |
4335 foreach ( $emoji_fields as $emoji_field ) { |
4553 foreach ( $emoji_fields as $emoji_field ) { |
4336 if ( isset( $data[ $emoji_field ] ) ) { |
4554 if ( isset( $data[ $emoji_field ] ) ) { |
4373 */ |
4591 */ |
4374 $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update ); |
4592 $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update ); |
4375 } |
4593 } |
4376 |
4594 |
4377 $data = wp_unslash( $data ); |
4595 $data = wp_unslash( $data ); |
4378 $where = array( 'ID' => $post_ID ); |
4596 $where = array( 'ID' => $post_id ); |
4379 |
4597 |
4380 if ( $update ) { |
4598 if ( $update ) { |
4381 /** |
4599 /** |
4382 * Fires immediately before an existing post is updated in the database. |
4600 * Fires immediately before an existing post is updated in the database. |
4383 * |
4601 * |
4384 * @since 2.5.0 |
4602 * @since 2.5.0 |
4385 * |
4603 * |
4386 * @param int $post_ID Post ID. |
4604 * @param int $post_id Post ID. |
4387 * @param array $data Array of unslashed post data. |
4605 * @param array $data Array of unslashed post data. |
4388 */ |
4606 */ |
4389 do_action( 'pre_post_update', $post_ID, $data ); |
4607 do_action( 'pre_post_update', $post_id, $data ); |
4390 |
4608 |
4391 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
4609 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
4392 if ( $wp_error ) { |
4610 if ( $wp_error ) { |
4393 if ( 'attachment' === $post_type ) { |
4611 if ( 'attachment' === $post_type ) { |
4394 $message = __( 'Could not update attachment in the database.' ); |
4612 $message = __( 'Could not update attachment in the database.' ); |
4423 } else { |
4641 } else { |
4424 return 0; |
4642 return 0; |
4425 } |
4643 } |
4426 } |
4644 } |
4427 |
4645 |
4428 $post_ID = (int) $wpdb->insert_id; |
4646 $post_id = (int) $wpdb->insert_id; |
4429 |
4647 |
4430 // Use the newly generated $post_ID. |
4648 // Use the newly generated $post_id. |
4431 $where = array( 'ID' => $post_ID ); |
4649 $where = array( 'ID' => $post_id ); |
4432 } |
4650 } |
4433 |
4651 |
4434 if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) { |
4652 if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) { |
4435 $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); |
4653 $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_id ), $post_id, $data['post_status'], $post_type, $post_parent ); |
4436 |
4654 |
4437 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
4655 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
4438 clean_post_cache( $post_ID ); |
4656 clean_post_cache( $post_id ); |
4439 } |
4657 } |
4440 |
4658 |
4441 if ( is_object_in_taxonomy( $post_type, 'category' ) ) { |
4659 if ( is_object_in_taxonomy( $post_type, 'category' ) ) { |
4442 wp_set_post_categories( $post_ID, $post_category ); |
4660 wp_set_post_categories( $post_id, $post_category ); |
4443 } |
4661 } |
4444 |
4662 |
4445 if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { |
4663 if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { |
4446 wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
4664 wp_set_post_tags( $post_id, $postarr['tags_input'] ); |
4447 } |
4665 } |
4448 |
4666 |
4449 // Add default term for all associated custom taxonomies. |
4667 // Add default term for all associated custom taxonomies. |
4450 if ( 'auto-draft' !== $post_status ) { |
4668 if ( 'auto-draft' !== $post_status ) { |
4451 foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
4669 foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
4456 if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { |
4674 if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { |
4457 $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); |
4675 $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); |
4458 } |
4676 } |
4459 |
4677 |
4460 // Passed custom taxonomy list overwrites the existing list if not empty. |
4678 // Passed custom taxonomy list overwrites the existing list if not empty. |
4461 $terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) ); |
4679 $terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) ); |
4462 if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
4680 if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
4463 $postarr['tax_input'][ $taxonomy ] = $terms; |
4681 $postarr['tax_input'][ $taxonomy ] = $terms; |
4464 } |
4682 } |
4465 |
4683 |
4466 if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
4684 if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
4488 if ( is_array( $tags ) ) { |
4706 if ( is_array( $tags ) ) { |
4489 $tags = array_filter( $tags ); |
4707 $tags = array_filter( $tags ); |
4490 } |
4708 } |
4491 |
4709 |
4492 if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
4710 if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
4493 wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
4711 wp_set_post_terms( $post_id, $tags, $taxonomy ); |
4494 } |
4712 } |
4495 } |
4713 } |
4496 } |
4714 } |
4497 |
4715 |
4498 if ( ! empty( $postarr['meta_input'] ) ) { |
4716 if ( ! empty( $postarr['meta_input'] ) ) { |
4499 foreach ( $postarr['meta_input'] as $field => $value ) { |
4717 foreach ( $postarr['meta_input'] as $field => $value ) { |
4500 update_post_meta( $post_ID, $field, $value ); |
4718 update_post_meta( $post_id, $field, $value ); |
4501 } |
4719 } |
4502 } |
4720 } |
4503 |
4721 |
4504 $current_guid = get_post_field( 'guid', $post_ID ); |
4722 $current_guid = get_post_field( 'guid', $post_id ); |
4505 |
4723 |
4506 // Set GUID. |
4724 // Set GUID. |
4507 if ( ! $update && '' === $current_guid ) { |
4725 if ( ! $update && '' === $current_guid ) { |
4508 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
4726 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where ); |
4509 } |
4727 } |
4510 |
4728 |
4511 if ( 'attachment' === $postarr['post_type'] ) { |
4729 if ( 'attachment' === $postarr['post_type'] ) { |
4512 if ( ! empty( $postarr['file'] ) ) { |
4730 if ( ! empty( $postarr['file'] ) ) { |
4513 update_attached_file( $post_ID, $postarr['file'] ); |
4731 update_attached_file( $post_id, $postarr['file'] ); |
4514 } |
4732 } |
4515 |
4733 |
4516 if ( ! empty( $postarr['context'] ) ) { |
4734 if ( ! empty( $postarr['context'] ) ) { |
4517 add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); |
4735 add_post_meta( $post_id, '_wp_attachment_context', $postarr['context'], true ); |
4518 } |
4736 } |
4519 } |
4737 } |
4520 |
4738 |
4521 // Set or remove featured image. |
4739 // Set or remove featured image. |
4522 if ( isset( $postarr['_thumbnail_id'] ) ) { |
4740 if ( isset( $postarr['_thumbnail_id'] ) ) { |
4523 $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; |
4741 $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; |
4524 |
4742 |
4525 if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { |
4743 if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { |
4526 if ( wp_attachment_is( 'audio', $post_ID ) ) { |
4744 if ( wp_attachment_is( 'audio', $post_id ) ) { |
4527 $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
4745 $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
4528 } elseif ( wp_attachment_is( 'video', $post_ID ) ) { |
4746 } elseif ( wp_attachment_is( 'video', $post_id ) ) { |
4529 $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
4747 $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
4530 } |
4748 } |
4531 } |
4749 } |
4532 |
4750 |
4533 if ( $thumbnail_support ) { |
4751 if ( $thumbnail_support ) { |
4534 $thumbnail_id = (int) $postarr['_thumbnail_id']; |
4752 $thumbnail_id = (int) $postarr['_thumbnail_id']; |
4535 if ( -1 === $thumbnail_id ) { |
4753 if ( -1 === $thumbnail_id ) { |
4536 delete_post_thumbnail( $post_ID ); |
4754 delete_post_thumbnail( $post_id ); |
4537 } else { |
4755 } else { |
4538 set_post_thumbnail( $post_ID, $thumbnail_id ); |
4756 set_post_thumbnail( $post_id, $thumbnail_id ); |
4539 } |
4757 } |
4540 } |
4758 } |
4541 } |
4759 } |
4542 |
4760 |
4543 clean_post_cache( $post_ID ); |
4761 clean_post_cache( $post_id ); |
4544 |
4762 |
4545 $post = get_post( $post_ID ); |
4763 $post = get_post( $post_id ); |
4546 |
4764 |
4547 if ( ! empty( $postarr['page_template'] ) ) { |
4765 if ( ! empty( $postarr['page_template'] ) ) { |
4548 $post->page_template = $postarr['page_template']; |
4766 $post->page_template = $postarr['page_template']; |
4549 $page_templates = wp_get_theme()->get_page_templates( $post ); |
4767 $page_templates = wp_get_theme()->get_page_templates( $post ); |
4550 |
4768 |
4551 if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { |
4769 if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { |
4552 if ( $wp_error ) { |
4770 if ( $wp_error ) { |
4553 return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); |
4771 return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); |
4554 } |
4772 } |
4555 |
4773 |
4556 update_post_meta( $post_ID, '_wp_page_template', 'default' ); |
4774 update_post_meta( $post_id, '_wp_page_template', 'default' ); |
4557 } else { |
4775 } else { |
4558 update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); |
4776 update_post_meta( $post_id, '_wp_page_template', $postarr['page_template'] ); |
4559 } |
4777 } |
4560 } |
4778 } |
4561 |
4779 |
4562 if ( 'attachment' !== $postarr['post_type'] ) { |
4780 if ( 'attachment' !== $postarr['post_type'] ) { |
4563 wp_transition_post_status( $data['post_status'], $previous_status, $post ); |
4781 wp_transition_post_status( $data['post_status'], $previous_status, $post ); |
4566 /** |
4784 /** |
4567 * Fires once an existing attachment has been updated. |
4785 * Fires once an existing attachment has been updated. |
4568 * |
4786 * |
4569 * @since 2.0.0 |
4787 * @since 2.0.0 |
4570 * |
4788 * |
4571 * @param int $post_ID Attachment ID. |
4789 * @param int $post_id Attachment ID. |
4572 */ |
4790 */ |
4573 do_action( 'edit_attachment', $post_ID ); |
4791 do_action( 'edit_attachment', $post_id ); |
4574 |
4792 |
4575 $post_after = get_post( $post_ID ); |
4793 $post_after = get_post( $post_id ); |
4576 |
4794 |
4577 /** |
4795 /** |
4578 * Fires once an existing attachment has been updated. |
4796 * Fires once an existing attachment has been updated. |
4579 * |
4797 * |
4580 * @since 4.4.0 |
4798 * @since 4.4.0 |
4581 * |
4799 * |
4582 * @param int $post_ID Post ID. |
4800 * @param int $post_id Post ID. |
4583 * @param WP_Post $post_after Post object following the update. |
4801 * @param WP_Post $post_after Post object following the update. |
4584 * @param WP_Post $post_before Post object before the update. |
4802 * @param WP_Post $post_before Post object before the update. |
4585 */ |
4803 */ |
4586 do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); |
4804 do_action( 'attachment_updated', $post_id, $post_after, $post_before ); |
4587 } else { |
4805 } else { |
4588 |
4806 |
4589 /** |
4807 /** |
4590 * Fires once an attachment has been added. |
4808 * Fires once an attachment has been added. |
4591 * |
4809 * |
4592 * @since 2.0.0 |
4810 * @since 2.0.0 |
4593 * |
4811 * |
4594 * @param int $post_ID Attachment ID. |
4812 * @param int $post_id Attachment ID. |
4595 */ |
4813 */ |
4596 do_action( 'add_attachment', $post_ID ); |
4814 do_action( 'add_attachment', $post_id ); |
4597 } |
4815 } |
4598 |
4816 |
4599 return $post_ID; |
4817 return $post_id; |
4600 } |
4818 } |
4601 |
4819 |
4602 if ( $update ) { |
4820 if ( $update ) { |
4603 /** |
4821 /** |
4604 * Fires once an existing post has been updated. |
4822 * Fires once an existing post has been updated. |
4611 * - `edit_post_post` |
4829 * - `edit_post_post` |
4612 * - `edit_post_page` |
4830 * - `edit_post_page` |
4613 * |
4831 * |
4614 * @since 5.1.0 |
4832 * @since 5.1.0 |
4615 * |
4833 * |
4616 * @param int $post_ID Post ID. |
4834 * @param int $post_id Post ID. |
4617 * @param WP_Post $post Post object. |
4835 * @param WP_Post $post Post object. |
4618 */ |
4836 */ |
4619 do_action( "edit_post_{$post->post_type}", $post_ID, $post ); |
4837 do_action( "edit_post_{$post->post_type}", $post_id, $post ); |
4620 |
4838 |
4621 /** |
4839 /** |
4622 * Fires once an existing post has been updated. |
4840 * Fires once an existing post has been updated. |
4623 * |
4841 * |
4624 * @since 1.2.0 |
4842 * @since 1.2.0 |
4625 * |
4843 * |
4626 * @param int $post_ID Post ID. |
4844 * @param int $post_id Post ID. |
4627 * @param WP_Post $post Post object. |
4845 * @param WP_Post $post Post object. |
4628 */ |
4846 */ |
4629 do_action( 'edit_post', $post_ID, $post ); |
4847 do_action( 'edit_post', $post_id, $post ); |
4630 |
4848 |
4631 $post_after = get_post( $post_ID ); |
4849 $post_after = get_post( $post_id ); |
4632 |
4850 |
4633 /** |
4851 /** |
4634 * Fires once an existing post has been updated. |
4852 * Fires once an existing post has been updated. |
4635 * |
4853 * |
4636 * @since 3.0.0 |
4854 * @since 3.0.0 |
4637 * |
4855 * |
4638 * @param int $post_ID Post ID. |
4856 * @param int $post_id Post ID. |
4639 * @param WP_Post $post_after Post object following the update. |
4857 * @param WP_Post $post_after Post object following the update. |
4640 * @param WP_Post $post_before Post object before the update. |
4858 * @param WP_Post $post_before Post object before the update. |
4641 */ |
4859 */ |
4642 do_action( 'post_updated', $post_ID, $post_after, $post_before ); |
4860 do_action( 'post_updated', $post_id, $post_after, $post_before ); |
4643 } |
4861 } |
4644 |
4862 |
4645 /** |
4863 /** |
4646 * Fires once a post has been saved. |
4864 * Fires once a post has been saved. |
4647 * |
4865 * |
4653 * - `save_post_post` |
4871 * - `save_post_post` |
4654 * - `save_post_page` |
4872 * - `save_post_page` |
4655 * |
4873 * |
4656 * @since 3.7.0 |
4874 * @since 3.7.0 |
4657 * |
4875 * |
4658 * @param int $post_ID Post ID. |
4876 * @param int $post_id Post ID. |
4659 * @param WP_Post $post Post object. |
4877 * @param WP_Post $post Post object. |
4660 * @param bool $update Whether this is an existing post being updated. |
4878 * @param bool $update Whether this is an existing post being updated. |
4661 */ |
4879 */ |
4662 do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); |
4880 do_action( "save_post_{$post->post_type}", $post_id, $post, $update ); |
4663 |
4881 |
4664 /** |
4882 /** |
4665 * Fires once a post has been saved. |
4883 * Fires once a post has been saved. |
4666 * |
4884 * |
4667 * @since 1.5.0 |
4885 * @since 1.5.0 |
4668 * |
4886 * |
4669 * @param int $post_ID Post ID. |
4887 * @param int $post_id Post ID. |
4670 * @param WP_Post $post Post object. |
4888 * @param WP_Post $post Post object. |
4671 * @param bool $update Whether this is an existing post being updated. |
4889 * @param bool $update Whether this is an existing post being updated. |
4672 */ |
4890 */ |
4673 do_action( 'save_post', $post_ID, $post, $update ); |
4891 do_action( 'save_post', $post_id, $post, $update ); |
4674 |
4892 |
4675 /** |
4893 /** |
4676 * Fires once a post has been saved. |
4894 * Fires once a post has been saved. |
4677 * |
4895 * |
4678 * @since 2.0.0 |
4896 * @since 2.0.0 |
4679 * |
4897 * |
4680 * @param int $post_ID Post ID. |
4898 * @param int $post_id Post ID. |
4681 * @param WP_Post $post Post object. |
4899 * @param WP_Post $post Post object. |
4682 * @param bool $update Whether this is an existing post being updated. |
4900 * @param bool $update Whether this is an existing post being updated. |
4683 */ |
4901 */ |
4684 do_action( 'wp_insert_post', $post_ID, $post, $update ); |
4902 do_action( 'wp_insert_post', $post_id, $post, $update ); |
4685 |
4903 |
4686 if ( $fire_after_hooks ) { |
4904 if ( $fire_after_hooks ) { |
4687 wp_after_insert_post( $post, $update, $post_before ); |
4905 wp_after_insert_post( $post, $update, $post_before ); |
4688 } |
4906 } |
4689 |
4907 |
4690 return $post_ID; |
4908 return $post_id; |
4691 } |
4909 } |
4692 |
4910 |
4693 /** |
4911 /** |
4694 * Update a post with new post data. |
4912 * Updates a post with new post data. |
4695 * |
4913 * |
4696 * The date does not have to be set for drafts. You can set the date and it will |
4914 * The date does not have to be set for drafts. You can set the date and it will |
4697 * not be overridden. |
4915 * not be overridden. |
4698 * |
4916 * |
4699 * @since 1.0.0 |
4917 * @since 1.0.0 |
4774 |
4992 |
4775 return wp_insert_post( $postarr, $wp_error, $fire_after_hooks ); |
4993 return wp_insert_post( $postarr, $wp_error, $fire_after_hooks ); |
4776 } |
4994 } |
4777 |
4995 |
4778 /** |
4996 /** |
4779 * Publish a post by transitioning the post status. |
4997 * Publishes a post by transitioning the post status. |
4780 * |
4998 * |
4781 * @since 2.1.0 |
4999 * @since 2.1.0 |
4782 * |
5000 * |
4783 * @global wpdb $wpdb WordPress database abstraction object. |
5001 * @global wpdb $wpdb WordPress database abstraction object. |
4784 * |
5002 * |
4851 |
5069 |
4852 wp_after_insert_post( $post, true, $post_before ); |
5070 wp_after_insert_post( $post, true, $post_before ); |
4853 } |
5071 } |
4854 |
5072 |
4855 /** |
5073 /** |
4856 * Publish future post and make sure post ID has future post status. |
5074 * Publishes future post and make sure post ID has future post status. |
4857 * |
5075 * |
4858 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
5076 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
4859 * from publishing drafts, etc. |
5077 * from publishing drafts, etc. |
4860 * |
5078 * |
4861 * @since 2.5.0 |
5079 * @since 2.5.0 |
4862 * |
5080 * |
4863 * @param int|WP_Post $post_id Post ID or post object. |
5081 * @param int|WP_Post $post Post ID or post object. |
4864 */ |
5082 */ |
4865 function check_and_publish_future_post( $post_id ) { |
5083 function check_and_publish_future_post( $post ) { |
4866 $post = get_post( $post_id ); |
5084 $post = get_post( $post ); |
4867 |
5085 |
4868 if ( ! $post ) { |
5086 if ( ! $post ) { |
4869 return; |
5087 return; |
4870 } |
5088 } |
4871 |
5089 |
4875 |
5093 |
4876 $time = strtotime( $post->post_date_gmt . ' GMT' ); |
5094 $time = strtotime( $post->post_date_gmt . ' GMT' ); |
4877 |
5095 |
4878 // Uh oh, someone jumped the gun! |
5096 // Uh oh, someone jumped the gun! |
4879 if ( $time > time() ) { |
5097 if ( $time > time() ) { |
4880 wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // Clear anything else in the system. |
5098 wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); // Clear anything else in the system. |
4881 wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); |
5099 wp_schedule_single_event( $time, 'publish_future_post', array( $post->ID ) ); |
4882 return; |
5100 return; |
4883 } |
5101 } |
4884 |
5102 |
4885 // wp_publish_post() returns no meaningful value. |
5103 // wp_publish_post() returns no meaningful value. |
4886 wp_publish_post( $post_id ); |
5104 wp_publish_post( $post->ID ); |
4887 } |
5105 } |
4888 |
5106 |
4889 /** |
5107 /** |
4890 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date. |
5108 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date. |
4891 * If post_date is not provided, this first checks post_date_gmt if provided, |
5109 * If post_date is not provided, this first checks post_date_gmt if provided, |
4894 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid |
5112 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid |
4895 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false. |
5113 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false. |
4896 * |
5114 * |
4897 * @since 5.7.0 |
5115 * @since 5.7.0 |
4898 * |
5116 * |
4899 * @param string $post_date The date in mysql format. |
5117 * @param string $post_date The date in mysql format (`Y-m-d H:i:s`). |
4900 * @param string $post_date_gmt The GMT date in mysql format. |
5118 * @param string $post_date_gmt The GMT date in mysql format (`Y-m-d H:i:s`). |
4901 * @return string|false A valid Gregorian-calendar date string, or false on failure. |
5119 * @return string|false A valid Gregorian-calendar date string, or false on failure. |
4902 */ |
5120 */ |
4903 function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { |
5121 function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { |
4904 // If the date is empty, set the date to now. |
5122 // If the date is empty, set the date to now. |
4905 if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) { |
5123 if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) { |
4909 $post_date = get_date_from_gmt( $post_date_gmt ); |
5127 $post_date = get_date_from_gmt( $post_date_gmt ); |
4910 } |
5128 } |
4911 } |
5129 } |
4912 |
5130 |
4913 // Validate the date. |
5131 // Validate the date. |
4914 $month = substr( $post_date, 5, 2 ); |
5132 $month = (int) substr( $post_date, 5, 2 ); |
4915 $day = substr( $post_date, 8, 2 ); |
5133 $day = (int) substr( $post_date, 8, 2 ); |
4916 $year = substr( $post_date, 0, 4 ); |
5134 $year = (int) substr( $post_date, 0, 4 ); |
4917 |
5135 |
4918 $valid_date = wp_checkdate( $month, $day, $year, $post_date ); |
5136 $valid_date = wp_checkdate( $month, $day, $year, $post_date ); |
4919 |
5137 |
4920 if ( ! $valid_date ) { |
5138 if ( ! $valid_date ) { |
4921 return false; |
5139 return false; |
4930 * |
5148 * |
4931 * @global wpdb $wpdb WordPress database abstraction object. |
5149 * @global wpdb $wpdb WordPress database abstraction object. |
4932 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
5150 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
4933 * |
5151 * |
4934 * @param string $slug The desired slug (post_name). |
5152 * @param string $slug The desired slug (post_name). |
4935 * @param int $post_ID Post ID. |
5153 * @param int $post_id Post ID. |
4936 * @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
5154 * @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
4937 * @param string $post_type Post type. |
5155 * @param string $post_type Post type. |
4938 * @param int $post_parent Post parent ID. |
5156 * @param int $post_parent Post parent ID. |
4939 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
5157 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
4940 */ |
5158 */ |
4941 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
5159 function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent ) { |
4942 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) |
5160 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) |
4943 || ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type |
5161 || ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type |
4944 ) { |
5162 ) { |
4945 return $slug; |
5163 return $slug; |
4946 } |
5164 } |
4953 * |
5171 * |
4954 * @since 5.1.0 |
5172 * @since 5.1.0 |
4955 * |
5173 * |
4956 * @param string|null $override_slug Short-circuit return value. |
5174 * @param string|null $override_slug Short-circuit return value. |
4957 * @param string $slug The desired slug (post_name). |
5175 * @param string $slug The desired slug (post_name). |
4958 * @param int $post_ID Post ID. |
5176 * @param int $post_id Post ID. |
4959 * @param string $post_status The post status. |
5177 * @param string $post_status The post status. |
4960 * @param string $post_type Post type. |
5178 * @param string $post_type Post type. |
4961 * @param int $post_parent Post parent ID. |
5179 * @param int $post_parent Post parent ID. |
4962 */ |
5180 */ |
4963 $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent ); |
5181 $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_id, $post_status, $post_type, $post_parent ); |
4964 if ( null !== $override_slug ) { |
5182 if ( null !== $override_slug ) { |
4965 return $override_slug; |
5183 return $override_slug; |
4966 } |
5184 } |
4967 |
5185 |
4968 global $wpdb, $wp_rewrite; |
5186 global $wpdb, $wp_rewrite; |
4975 } |
5193 } |
4976 |
5194 |
4977 if ( 'attachment' === $post_type ) { |
5195 if ( 'attachment' === $post_type ) { |
4978 // Attachment slugs must be unique across all types. |
5196 // Attachment slugs must be unique across all types. |
4979 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
5197 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
4980 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); |
5198 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_id ) ); |
4981 |
5199 |
4982 /** |
5200 /** |
4983 * Filters whether the post slug would make a bad attachment slug. |
5201 * Filters whether the post slug would make a bad attachment slug. |
4984 * |
5202 * |
4985 * @since 3.1.0 |
5203 * @since 3.1.0 |
4994 || $is_bad_attachment_slug |
5212 || $is_bad_attachment_slug |
4995 ) { |
5213 ) { |
4996 $suffix = 2; |
5214 $suffix = 2; |
4997 do { |
5215 do { |
4998 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5216 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
4999 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); |
5217 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_id ) ); |
5000 $suffix++; |
5218 ++$suffix; |
5001 } while ( $post_name_check ); |
5219 } while ( $post_name_check ); |
5002 $slug = $alt_post_name; |
5220 $slug = $alt_post_name; |
5003 } |
5221 } |
5004 } elseif ( is_post_type_hierarchical( $post_type ) ) { |
5222 } elseif ( is_post_type_hierarchical( $post_type ) ) { |
5005 if ( 'nav_menu_item' === $post_type ) { |
5223 if ( 'nav_menu_item' === $post_type ) { |
5009 /* |
5227 /* |
5010 * Page slugs must be unique within their own trees. Pages are in a separate |
5228 * Page slugs must be unique within their own trees. Pages are in a separate |
5011 * namespace than posts so page slugs are allowed to overlap post slugs. |
5229 * namespace than posts so page slugs are allowed to overlap post slugs. |
5012 */ |
5230 */ |
5013 $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"; |
5231 $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"; |
5014 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); |
5232 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id, $post_parent ) ); |
5015 |
5233 |
5016 /** |
5234 /** |
5017 * Filters whether the post slug would make a bad hierarchical post slug. |
5235 * Filters whether the post slug would make a bad hierarchical post slug. |
5018 * |
5236 * |
5019 * @since 3.1.0 |
5237 * @since 3.1.0 |
5031 || $is_bad_hierarchical_slug |
5249 || $is_bad_hierarchical_slug |
5032 ) { |
5250 ) { |
5033 $suffix = 2; |
5251 $suffix = 2; |
5034 do { |
5252 do { |
5035 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5253 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5036 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); |
5254 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id, $post_parent ) ); |
5037 $suffix++; |
5255 ++$suffix; |
5038 } while ( $post_name_check ); |
5256 } while ( $post_name_check ); |
5039 $slug = $alt_post_name; |
5257 $slug = $alt_post_name; |
5040 } |
5258 } |
5041 } else { |
5259 } else { |
5042 // Post slugs must be unique across all posts. |
5260 // Post slugs must be unique across all posts. |
5043 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
5261 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
5044 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
5262 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_id ) ); |
5045 |
5263 |
5046 $post = get_post( $post_ID ); |
5264 $post = get_post( $post_id ); |
5047 |
5265 |
5048 // Prevent new post slugs that could result in URLs that conflict with date archives. |
5266 // Prevent new post slugs that could result in URLs that conflict with date archives. |
5049 $conflicts_with_date_archive = false; |
5267 $conflicts_with_date_archive = false; |
5050 if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { |
5268 if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { |
5051 $slug_num = (int) $slug; |
5269 $slug_num = (int) $slug; |
5087 || $is_bad_flat_slug |
5305 || $is_bad_flat_slug |
5088 ) { |
5306 ) { |
5089 $suffix = 2; |
5307 $suffix = 2; |
5090 do { |
5308 do { |
5091 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5309 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5092 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
5310 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_id ) ); |
5093 $suffix++; |
5311 ++$suffix; |
5094 } while ( $post_name_check ); |
5312 } while ( $post_name_check ); |
5095 $slug = $alt_post_name; |
5313 $slug = $alt_post_name; |
5096 } |
5314 } |
5097 } |
5315 } |
5098 |
5316 |
5100 * Filters the unique post slug. |
5318 * Filters the unique post slug. |
5101 * |
5319 * |
5102 * @since 3.3.0 |
5320 * @since 3.3.0 |
5103 * |
5321 * |
5104 * @param string $slug The post slug. |
5322 * @param string $slug The post slug. |
5105 * @param int $post_ID Post ID. |
5323 * @param int $post_id Post ID. |
5106 * @param string $post_status The post status. |
5324 * @param string $post_status The post status. |
5107 * @param string $post_type Post type. |
5325 * @param string $post_type Post type. |
5108 * @param int $post_parent Post parent ID |
5326 * @param int $post_parent Post parent ID |
5109 * @param string $original_slug The original post slug. |
5327 * @param string $original_slug The original post slug. |
5110 */ |
5328 */ |
5111 return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); |
5329 return apply_filters( 'wp_unique_post_slug', $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ); |
5112 } |
5330 } |
5113 |
5331 |
5114 /** |
5332 /** |
5115 * Truncate a post slug. |
5333 * Truncates a post slug. |
5116 * |
5334 * |
5117 * @since 3.6.0 |
5335 * @since 3.6.0 |
5118 * @access private |
5336 * @access private |
5119 * |
5337 * |
5120 * @see utf8_uri_encode() |
5338 * @see utf8_uri_encode() |
5135 |
5353 |
5136 return rtrim( $slug, '-' ); |
5354 return rtrim( $slug, '-' ); |
5137 } |
5355 } |
5138 |
5356 |
5139 /** |
5357 /** |
5140 * Add tags to a post. |
5358 * Adds tags to a post. |
5141 * |
5359 * |
5142 * @see wp_set_post_tags() |
5360 * @see wp_set_post_tags() |
5143 * |
5361 * |
5144 * @since 2.3.0 |
5362 * @since 2.3.0 |
5145 * |
5363 * |
5151 function wp_add_post_tags( $post_id = 0, $tags = '' ) { |
5369 function wp_add_post_tags( $post_id = 0, $tags = '' ) { |
5152 return wp_set_post_tags( $post_id, $tags, true ); |
5370 return wp_set_post_tags( $post_id, $tags, true ); |
5153 } |
5371 } |
5154 |
5372 |
5155 /** |
5373 /** |
5156 * Set the tags for a post. |
5374 * Sets the tags for a post. |
5157 * |
5375 * |
5158 * @since 2.3.0 |
5376 * @since 2.3.0 |
5159 * |
5377 * |
5160 * @see wp_set_object_terms() |
5378 * @see wp_set_object_terms() |
5161 * |
5379 * |
5169 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
5387 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
5170 return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); |
5388 return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); |
5171 } |
5389 } |
5172 |
5390 |
5173 /** |
5391 /** |
5174 * Set the terms for a post. |
5392 * Sets the terms for a post. |
5175 * |
5393 * |
5176 * @since 2.8.0 |
5394 * @since 2.8.0 |
5177 * |
5395 * |
5178 * @see wp_set_object_terms() |
5396 * @see wp_set_object_terms() |
5179 * |
5397 * |
5180 * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
5398 * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
5181 * @param string|array $tags Optional. An array of terms to set for the post, or a string of terms |
5399 * @param string|array $terms Optional. An array of terms to set for the post, or a string of terms |
5182 * separated by commas. Hierarchical taxonomies must always pass IDs rather |
5400 * separated by commas. Hierarchical taxonomies must always pass IDs rather |
5183 * than names so that children with the same names but different parents |
5401 * than names so that children with the same names but different parents |
5184 * aren't confused. Default empty. |
5402 * aren't confused. Default empty. |
5185 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. |
5403 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. |
5186 * @param bool $append Optional. If true, don't delete existing terms, just add on. If false, |
5404 * @param bool $append Optional. If true, don't delete existing terms, just add on. If false, |
5187 * replace the terms with the new terms. Default false. |
5405 * replace the terms with the new terms. Default false. |
5188 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
5406 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
5189 */ |
5407 */ |
5190 function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { |
5408 function wp_set_post_terms( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false ) { |
5191 $post_id = (int) $post_id; |
5409 $post_id = (int) $post_id; |
5192 |
5410 |
5193 if ( ! $post_id ) { |
5411 if ( ! $post_id ) { |
5194 return false; |
5412 return false; |
5195 } |
5413 } |
5196 |
5414 |
5197 if ( empty( $tags ) ) { |
5415 if ( empty( $terms ) ) { |
5198 $tags = array(); |
5416 $terms = array(); |
5199 } |
5417 } |
5200 |
5418 |
5201 if ( ! is_array( $tags ) ) { |
5419 if ( ! is_array( $terms ) ) { |
5202 $comma = _x( ',', 'tag delimiter' ); |
5420 $comma = _x( ',', 'tag delimiter' ); |
5203 if ( ',' !== $comma ) { |
5421 if ( ',' !== $comma ) { |
5204 $tags = str_replace( $comma, ',', $tags ); |
5422 $terms = str_replace( $comma, ',', $terms ); |
5205 } |
5423 } |
5206 $tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) ); |
5424 $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
5207 } |
5425 } |
5208 |
5426 |
5209 /* |
5427 /* |
5210 * Hierarchical taxonomies must always pass IDs rather than names so that |
5428 * Hierarchical taxonomies must always pass IDs rather than names so that |
5211 * children with the same names but different parents aren't confused. |
5429 * children with the same names but different parents aren't confused. |
5212 */ |
5430 */ |
5213 if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
5431 if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
5214 $tags = array_unique( array_map( 'intval', $tags ) ); |
5432 $terms = array_unique( array_map( 'intval', $terms ) ); |
5215 } |
5433 } |
5216 |
5434 |
5217 return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); |
5435 return wp_set_object_terms( $post_id, $terms, $taxonomy, $append ); |
5218 } |
5436 } |
5219 |
5437 |
5220 /** |
5438 /** |
5221 * Set categories for a post. |
5439 * Sets categories for a post. |
5222 * |
5440 * |
5223 * If no categories are provided, the default category is used. |
5441 * If no categories are provided, the default category is used. |
5224 * |
5442 * |
5225 * @since 2.1.0 |
5443 * @since 2.1.0 |
5226 * |
5444 * |
5227 * @param int $post_ID Optional. The Post ID. Does not default to the ID |
5445 * @param int $post_id Optional. The Post ID. Does not default to the ID |
5228 * of the global $post. Default 0. |
5446 * of the global $post. Default 0. |
5229 * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category. |
5447 * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category. |
5230 * Default empty array. |
5448 * Default empty array. |
5231 * @param bool $append If true, don't delete existing categories, just add on. |
5449 * @param bool $append If true, don't delete existing categories, just add on. |
5232 * If false, replace the categories with the new categories. |
5450 * If false, replace the categories with the new categories. |
5233 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. |
5451 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. |
5234 */ |
5452 */ |
5235 function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { |
5453 function wp_set_post_categories( $post_id = 0, $post_categories = array(), $append = false ) { |
5236 $post_ID = (int) $post_ID; |
5454 $post_id = (int) $post_id; |
5237 $post_type = get_post_type( $post_ID ); |
5455 $post_type = get_post_type( $post_id ); |
5238 $post_status = get_post_status( $post_ID ); |
5456 $post_status = get_post_status( $post_id ); |
5239 |
5457 |
5240 // If $post_categories isn't already an array, make it one. |
5458 // If $post_categories isn't already an array, make it one. |
5241 $post_categories = (array) $post_categories; |
5459 $post_categories = (array) $post_categories; |
5242 |
5460 |
5243 if ( empty( $post_categories ) ) { |
5461 if ( empty( $post_categories ) ) { |
5264 } |
5482 } |
5265 } elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) { |
5483 } elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) { |
5266 return true; |
5484 return true; |
5267 } |
5485 } |
5268 |
5486 |
5269 return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); |
5487 return wp_set_post_terms( $post_id, $post_categories, 'category', $append ); |
5270 } |
5488 } |
5271 |
5489 |
5272 /** |
5490 /** |
5273 * Fires actions related to the transitioning of a post's status. |
5491 * Fires actions related to the transitioning of a post's status. |
5274 * |
5492 * |
5371 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior |
5589 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior |
5372 * to the update for updated posts. |
5590 * to the update for updated posts. |
5373 */ |
5591 */ |
5374 function wp_after_insert_post( $post, $update, $post_before ) { |
5592 function wp_after_insert_post( $post, $update, $post_before ) { |
5375 $post = get_post( $post ); |
5593 $post = get_post( $post ); |
5594 |
|
5376 if ( ! $post ) { |
5595 if ( ! $post ) { |
5377 return; |
5596 return; |
5378 } |
5597 } |
5379 |
5598 |
5380 $post_id = $post->ID; |
5599 $post_id = $post->ID; |
5396 // |
5615 // |
5397 // Comment, trackback, and pingback functions. |
5616 // Comment, trackback, and pingback functions. |
5398 // |
5617 // |
5399 |
5618 |
5400 /** |
5619 /** |
5401 * Add a URL to those already pinged. |
5620 * Adds a URL to those already pinged. |
5402 * |
5621 * |
5403 * @since 1.5.0 |
5622 * @since 1.5.0 |
5404 * @since 4.7.0 `$post_id` can be a WP_Post object. |
5623 * @since 4.7.0 `$post` can be a WP_Post object. |
5405 * @since 4.7.0 `$uri` can be an array of URIs. |
5624 * @since 4.7.0 `$uri` can be an array of URIs. |
5406 * |
5625 * |
5407 * @global wpdb $wpdb WordPress database abstraction object. |
5626 * @global wpdb $wpdb WordPress database abstraction object. |
5408 * |
5627 * |
5409 * @param int|WP_Post $post_id Post object or ID. |
5628 * @param int|WP_Post $post Post ID or post object. |
5410 * @param string|array $uri Ping URI or array of URIs. |
5629 * @param string|array $uri Ping URI or array of URIs. |
5411 * @return int|false How many rows were updated. |
5630 * @return int|false How many rows were updated. |
5412 */ |
5631 */ |
5413 function add_ping( $post_id, $uri ) { |
5632 function add_ping( $post, $uri ) { |
5414 global $wpdb; |
5633 global $wpdb; |
5415 |
5634 |
5416 $post = get_post( $post_id ); |
5635 $post = get_post( $post ); |
5417 |
5636 |
5418 if ( ! $post ) { |
5637 if ( ! $post ) { |
5419 return false; |
5638 return false; |
5420 } |
5639 } |
5421 |
5640 |
5442 clean_post_cache( $post->ID ); |
5661 clean_post_cache( $post->ID ); |
5443 return $return; |
5662 return $return; |
5444 } |
5663 } |
5445 |
5664 |
5446 /** |
5665 /** |
5447 * Retrieve enclosures already enclosed for a post. |
5666 * Retrieves enclosures already enclosed for a post. |
5448 * |
5667 * |
5449 * @since 1.5.0 |
5668 * @since 1.5.0 |
5450 * |
5669 * |
5451 * @param int $post_id Post ID. |
5670 * @param int $post_id Post ID. |
5452 * @return string[] Array of enclosures for the given post. |
5671 * @return string[] Array of enclosures for the given post. |
5478 */ |
5697 */ |
5479 return apply_filters( 'get_enclosed', $pung, $post_id ); |
5698 return apply_filters( 'get_enclosed', $pung, $post_id ); |
5480 } |
5699 } |
5481 |
5700 |
5482 /** |
5701 /** |
5483 * Retrieve URLs already pinged for a post. |
5702 * Retrieves URLs already pinged for a post. |
5484 * |
5703 * |
5485 * @since 1.5.0 |
5704 * @since 1.5.0 |
5486 * |
5705 * |
5487 * @since 4.7.0 `$post_id` can be a WP_Post object. |
5706 * @since 4.7.0 `$post` can be a WP_Post object. |
5488 * |
5707 * |
5489 * @param int|WP_Post $post_id Post ID or object. |
5708 * @param int|WP_Post $post Post ID or object. |
5490 * @return string[]|false Array of URLs already pinged for the given post, false if the post is not found. |
5709 * @return string[]|false Array of URLs already pinged for the given post, false if the post is not found. |
5491 */ |
5710 */ |
5492 function get_pung( $post_id ) { |
5711 function get_pung( $post ) { |
5493 $post = get_post( $post_id ); |
5712 $post = get_post( $post ); |
5494 |
5713 |
5495 if ( ! $post ) { |
5714 if ( ! $post ) { |
5496 return false; |
5715 return false; |
5497 } |
5716 } |
5498 |
5717 |
5508 */ |
5727 */ |
5509 return apply_filters( 'get_pung', $pung ); |
5728 return apply_filters( 'get_pung', $pung ); |
5510 } |
5729 } |
5511 |
5730 |
5512 /** |
5731 /** |
5513 * Retrieve URLs that need to be pinged. |
5732 * Retrieves URLs that need to be pinged. |
5514 * |
5733 * |
5515 * @since 1.5.0 |
5734 * @since 1.5.0 |
5516 * @since 4.7.0 `$post_id` can be a WP_Post object. |
5735 * @since 4.7.0 `$post` can be a WP_Post object. |
5517 * |
5736 * |
5518 * @param int|WP_Post $post_id Post Object or ID |
5737 * @param int|WP_Post $post Post ID or post object. |
5519 * @return string[]|false List of URLs yet to ping. |
5738 * @return string[]|false List of URLs yet to ping. |
5520 */ |
5739 */ |
5521 function get_to_ping( $post_id ) { |
5740 function get_to_ping( $post ) { |
5522 $post = get_post( $post_id ); |
5741 $post = get_post( $post ); |
5523 |
5742 |
5524 if ( ! $post ) { |
5743 if ( ! $post ) { |
5525 return false; |
5744 return false; |
5526 } |
5745 } |
5527 |
5746 |
5537 */ |
5756 */ |
5538 return apply_filters( 'get_to_ping', $to_ping ); |
5757 return apply_filters( 'get_to_ping', $to_ping ); |
5539 } |
5758 } |
5540 |
5759 |
5541 /** |
5760 /** |
5542 * Do trackbacks for a list of URLs. |
5761 * Does trackbacks for a list of URLs. |
5543 * |
5762 * |
5544 * @since 1.0.0 |
5763 * @since 1.0.0 |
5545 * |
5764 * |
5546 * @param string $tb_list Comma separated list of URLs. |
5765 * @param string $tb_list Comma separated list of URLs. |
5547 * @param int $post_id Post ID. |
5766 * @param int $post_id Post ID. |
5569 // |
5788 // |
5570 // Page functions. |
5789 // Page functions. |
5571 // |
5790 // |
5572 |
5791 |
5573 /** |
5792 /** |
5574 * Get a list of page IDs. |
5793 * Gets a list of page IDs. |
5575 * |
5794 * |
5576 * @since 2.0.0 |
5795 * @since 2.0.0 |
5577 * |
5796 * |
5578 * @global wpdb $wpdb WordPress database abstraction object. |
5797 * @global wpdb $wpdb WordPress database abstraction object. |
5579 * |
5798 * |
5630 |
5849 |
5631 $last_changed = wp_cache_get_last_changed( 'posts' ); |
5850 $last_changed = wp_cache_get_last_changed( 'posts' ); |
5632 |
5851 |
5633 $hash = md5( $page_path . serialize( $post_type ) ); |
5852 $hash = md5( $page_path . serialize( $post_type ) ); |
5634 $cache_key = "get_page_by_path:$hash:$last_changed"; |
5853 $cache_key = "get_page_by_path:$hash:$last_changed"; |
5635 $cached = wp_cache_get( $cache_key, 'posts' ); |
5854 $cached = wp_cache_get( $cache_key, 'post-queries' ); |
5636 if ( false !== $cached ) { |
5855 if ( false !== $cached ) { |
5637 // Special case: '0' is a bad `$page_path`. |
5856 // Special case: '0' is a bad `$page_path`. |
5638 if ( '0' === $cached || 0 === $cached ) { |
5857 if ( '0' === $cached || 0 === $cached ) { |
5639 return; |
5858 return; |
5640 } else { |
5859 } else { |
5679 /* |
5898 /* |
5680 * Loop through the given path parts from right to left, |
5899 * Loop through the given path parts from right to left, |
5681 * ensuring each matches the post ancestry. |
5900 * ensuring each matches the post ancestry. |
5682 */ |
5901 */ |
5683 while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) { |
5902 while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) { |
5684 $count++; |
5903 ++$count; |
5685 $parent = $pages[ $p->post_parent ]; |
5904 $parent = $pages[ $p->post_parent ]; |
5686 if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) { |
5905 if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) { |
5687 break; |
5906 break; |
5688 } |
5907 } |
5689 $p = $parent; |
5908 $p = $parent; |
5690 } |
5909 } |
5691 |
5910 |
5692 if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) { |
5911 if ( 0 == $p->post_parent && count( $revparts ) === $count + 1 && $p->post_name == $revparts[ $count ] ) { |
5693 $foundid = $page->ID; |
5912 $foundid = $page->ID; |
5694 if ( $page->post_type == $post_type ) { |
5913 if ( $page->post_type == $post_type ) { |
5695 break; |
5914 break; |
5696 } |
5915 } |
5697 } |
5916 } |
5698 } |
5917 } |
5699 } |
5918 } |
5700 |
5919 |
5701 // We cache misses as well as hits. |
5920 // We cache misses as well as hits. |
5702 wp_cache_set( $cache_key, $foundid, 'posts' ); |
5921 wp_cache_set( $cache_key, $foundid, 'post-queries' ); |
5703 |
5922 |
5704 if ( $foundid ) { |
5923 if ( $foundid ) { |
5705 return get_post( $foundid, $output ); |
5924 return get_post( $foundid, $output ); |
5706 } |
5925 } |
5707 |
5926 |
5708 return null; |
5927 return null; |
5709 } |
5928 } |
5710 |
5929 |
5711 /** |
5930 /** |
5712 * Retrieve a page given its title. |
5931 * Identifies descendants of a given page ID in a list of page objects. |
5713 * |
|
5714 * If more than one post uses the same title, the post with the smallest ID will be returned. |
|
5715 * Be careful: in case of more than one post having the same title, it will check the oldest |
|
5716 * publication date, not the smallest ID. |
|
5717 * |
|
5718 * Because this function uses the MySQL '=' comparison, $page_title will usually be matched |
|
5719 * as case-insensitive with default collation. |
|
5720 * |
|
5721 * @since 2.1.0 |
|
5722 * @since 3.0.0 The `$post_type` parameter was added. |
|
5723 * |
|
5724 * @global wpdb $wpdb WordPress database abstraction object. |
|
5725 * |
|
5726 * @param string $page_title Page title. |
|
5727 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
5728 * correspond to a WP_Post object, an associative array, or a numeric array, |
|
5729 * respectively. Default OBJECT. |
|
5730 * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. |
|
5731 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
|
5732 */ |
|
5733 function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { |
|
5734 global $wpdb; |
|
5735 |
|
5736 if ( is_array( $post_type ) ) { |
|
5737 $post_type = esc_sql( $post_type ); |
|
5738 $post_type_in_string = "'" . implode( "','", $post_type ) . "'"; |
|
5739 $sql = $wpdb->prepare( |
|
5740 " |
|
5741 SELECT ID |
|
5742 FROM $wpdb->posts |
|
5743 WHERE post_title = %s |
|
5744 AND post_type IN ($post_type_in_string) |
|
5745 ", |
|
5746 $page_title |
|
5747 ); |
|
5748 } else { |
|
5749 $sql = $wpdb->prepare( |
|
5750 " |
|
5751 SELECT ID |
|
5752 FROM $wpdb->posts |
|
5753 WHERE post_title = %s |
|
5754 AND post_type = %s |
|
5755 ", |
|
5756 $page_title, |
|
5757 $post_type |
|
5758 ); |
|
5759 } |
|
5760 |
|
5761 $page = $wpdb->get_var( $sql ); |
|
5762 |
|
5763 if ( $page ) { |
|
5764 return get_post( $page, $output ); |
|
5765 } |
|
5766 |
|
5767 return null; |
|
5768 } |
|
5769 |
|
5770 /** |
|
5771 * Identify descendants of a given page ID in a list of page objects. |
|
5772 * |
5932 * |
5773 * Descendants are identified from the `$pages` array passed to the function. No database queries are performed. |
5933 * Descendants are identified from the `$pages` array passed to the function. No database queries are performed. |
5774 * |
5934 * |
5775 * @since 1.5.1 |
5935 * @since 1.5.1 |
5776 * |
5936 * |
5777 * @param int $page_id Page ID. |
5937 * @param int $page_id Page ID. |
5778 * @param array $pages List of page objects from which descendants should be identified. |
5938 * @param WP_Post[] $pages List of page objects from which descendants should be identified. |
5779 * @return array List of page children. |
5939 * @return WP_Post[] List of page children. |
5780 */ |
5940 */ |
5781 function get_page_children( $page_id, $pages ) { |
5941 function get_page_children( $page_id, $pages ) { |
5782 // Build a hash of ID -> children. |
5942 // Build a hash of ID -> children. |
5783 $children = array(); |
5943 $children = array(); |
5784 foreach ( (array) $pages as $page ) { |
5944 foreach ( (array) $pages as $page ) { |
5806 |
5966 |
5807 return $page_list; |
5967 return $page_list; |
5808 } |
5968 } |
5809 |
5969 |
5810 /** |
5970 /** |
5811 * Order the pages with children under parents in a flat list. |
5971 * Orders the pages with children under parents in a flat list. |
5812 * |
5972 * |
5813 * It uses auxiliary structure to hold parent-children relationships and |
5973 * It uses auxiliary structure to hold parent-children relationships and |
5814 * runs in O(N) complexity |
5974 * runs in O(N) complexity |
5815 * |
5975 * |
5816 * @since 2.0.0 |
5976 * @since 2.0.0 |
5835 |
5995 |
5836 return $result; |
5996 return $result; |
5837 } |
5997 } |
5838 |
5998 |
5839 /** |
5999 /** |
5840 * Traverse and return all the nested children post names of a root page. |
6000 * Traverses and return all the nested children post names of a root page. |
5841 * |
6001 * |
5842 * $children contains parent-children relations |
6002 * $children contains parent-children relations |
5843 * |
6003 * |
5844 * @since 2.9.0 |
6004 * @since 2.9.0 |
5845 * @access private |
6005 * @access private |
5858 } |
6018 } |
5859 } |
6019 } |
5860 } |
6020 } |
5861 |
6021 |
5862 /** |
6022 /** |
5863 * Build the URI path for a page. |
6023 * Builds the URI path for a page. |
5864 * |
6024 * |
5865 * Sub pages will be in the "directory" under the parent page post name. |
6025 * Sub pages will be in the "directory" under the parent page post name. |
5866 * |
6026 * |
5867 * @since 1.5.0 |
6027 * @since 1.5.0 |
5868 * @since 4.6.0 The `$page` parameter was made optional. |
6028 * @since 4.6.0 The `$page` parameter was made optional. |
5898 */ |
6058 */ |
5899 return apply_filters( 'get_page_uri', $uri, $page ); |
6059 return apply_filters( 'get_page_uri', $uri, $page ); |
5900 } |
6060 } |
5901 |
6061 |
5902 /** |
6062 /** |
5903 * Retrieve an array of pages (or hierarchical post type items). |
6063 * Retrieves an array of pages (or hierarchical post type items). |
5904 * |
|
5905 * @global wpdb $wpdb WordPress database abstraction object. |
|
5906 * |
6064 * |
5907 * @since 1.5.0 |
6065 * @since 1.5.0 |
6066 * @since 6.3.0 Use WP_Query internally. |
|
5908 * |
6067 * |
5909 * @param array|string $args { |
6068 * @param array|string $args { |
5910 * Optional. Array or string of arguments to retrieve pages. |
6069 * Optional. Array or string of arguments to retrieve pages. |
5911 * |
6070 * |
5912 * @type int $child_of Page ID to return child and grandchild pages of. Note: The value |
6071 * @type int $child_of Page ID to return child and grandchild pages of. Note: The value |
5937 * Default 0. |
6096 * Default 0. |
5938 * @type string $post_type The post type to query. Default 'page'. |
6097 * @type string $post_type The post type to query. Default 'page'. |
5939 * @type string|array $post_status A comma-separated list or array of post statuses to include. |
6098 * @type string|array $post_status A comma-separated list or array of post statuses to include. |
5940 * Default 'publish'. |
6099 * Default 'publish'. |
5941 * } |
6100 * } |
5942 * @return WP_Post[]|int[]|false Array of pages (or hierarchical post type items). Boolean false if the |
6101 * @return WP_Post[]|false Array of pages (or hierarchical post type items). Boolean false if the |
5943 * specified post type is not hierarchical or the specified status is not |
6102 * specified post type is not hierarchical or the specified status is not |
5944 * supported by the post type. |
6103 * supported by the post type. |
5945 */ |
6104 */ |
5946 function get_pages( $args = array() ) { |
6105 function get_pages( $args = array() ) { |
5947 global $wpdb; |
|
5948 |
|
5949 $defaults = array( |
6106 $defaults = array( |
5950 'child_of' => 0, |
6107 'child_of' => 0, |
5951 'sort_order' => 'ASC', |
6108 'sort_order' => 'ASC', |
5952 'sort_column' => 'post_title', |
6109 'sort_column' => 'post_title', |
5953 'hierarchical' => 1, |
6110 'hierarchical' => 1, |
5992 } |
6149 } |
5993 if ( array_diff( $post_status, get_post_stati() ) ) { |
6150 if ( array_diff( $post_status, get_post_stati() ) ) { |
5994 return false; |
6151 return false; |
5995 } |
6152 } |
5996 |
6153 |
5997 // $args can be whatever, only use the args defined in defaults to compute the key. |
6154 $query_args = array( |
5998 $key = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) ); |
6155 'orderby' => 'post_title', |
5999 $last_changed = wp_cache_get_last_changed( 'posts' ); |
6156 'order' => 'ASC', |
6000 |
6157 'post__not_in' => wp_parse_id_list( $exclude ), |
6001 $cache_key = "get_pages:$key:$last_changed"; |
6158 'meta_key' => $meta_key, |
6002 $cache = wp_cache_get( $cache_key, 'posts' ); |
6159 'meta_value' => $meta_value, |
6003 if ( false !== $cache ) { |
6160 'posts_per_page' => -1, |
6004 _prime_post_caches( $cache, false, false ); |
6161 'offset' => $offset, |
6005 |
6162 'post_type' => $parsed_args['post_type'], |
6006 // Convert to WP_Post instances. |
6163 'post_status' => $post_status, |
6007 $pages = array_map( 'get_post', $cache ); |
6164 'update_post_term_cache' => false, |
6008 /** This filter is documented in wp-includes/post.php */ |
6165 'update_post_meta_cache' => false, |
6009 $pages = apply_filters( 'get_pages', $pages, $parsed_args ); |
6166 'ignore_sticky_posts' => true, |
6010 |
6167 'no_found_rows' => true, |
6011 return $pages; |
6168 ); |
6012 } |
6169 |
6013 |
|
6014 $inclusions = ''; |
|
6015 if ( ! empty( $parsed_args['include'] ) ) { |
6170 if ( ! empty( $parsed_args['include'] ) ) { |
6016 $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. |
6171 $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. |
6017 $parent = -1; |
6172 $parent = -1; |
6018 $exclude = ''; |
6173 unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] ); |
6019 $meta_key = ''; |
6174 $hierarchical = false; |
6020 $meta_value = ''; |
6175 $query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] ); |
6021 $hierarchical = false; |
6176 } |
6022 $incpages = wp_parse_id_list( $parsed_args['include'] ); |
6177 |
6023 if ( ! empty( $incpages ) ) { |
|
6024 $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; |
|
6025 } |
|
6026 } |
|
6027 |
|
6028 $exclusions = ''; |
|
6029 if ( ! empty( $exclude ) ) { |
|
6030 $expages = wp_parse_id_list( $exclude ); |
|
6031 if ( ! empty( $expages ) ) { |
|
6032 $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; |
|
6033 } |
|
6034 } |
|
6035 |
|
6036 $author_query = ''; |
|
6037 if ( ! empty( $parsed_args['authors'] ) ) { |
6178 if ( ! empty( $parsed_args['authors'] ) ) { |
6038 $post_authors = wp_parse_list( $parsed_args['authors'] ); |
6179 $post_authors = wp_parse_list( $parsed_args['authors'] ); |
6039 |
6180 |
6040 if ( ! empty( $post_authors ) ) { |
6181 if ( ! empty( $post_authors ) ) { |
6182 $query_args['author__in'] = array(); |
|
6041 foreach ( $post_authors as $post_author ) { |
6183 foreach ( $post_authors as $post_author ) { |
6042 // Do we have an author id or an author login? |
6184 // Do we have an author id or an author login? |
6043 if ( 0 == (int) $post_author ) { |
6185 if ( 0 == (int) $post_author ) { |
6044 $post_author = get_user_by( 'login', $post_author ); |
6186 $post_author = get_user_by( 'login', $post_author ); |
6045 if ( empty( $post_author ) ) { |
6187 if ( empty( $post_author ) ) { |
6048 if ( empty( $post_author->ID ) ) { |
6190 if ( empty( $post_author->ID ) ) { |
6049 continue; |
6191 continue; |
6050 } |
6192 } |
6051 $post_author = $post_author->ID; |
6193 $post_author = $post_author->ID; |
6052 } |
6194 } |
6053 |
6195 $query_args['author__in'][] = (int) $post_author; |
6054 if ( '' === $author_query ) { |
|
6055 $author_query = $wpdb->prepare( ' post_author = %d ', $post_author ); |
|
6056 } else { |
|
6057 $author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author ); |
|
6058 } |
|
6059 } |
6196 } |
6060 if ( '' !== $author_query ) { |
6197 } |
6061 $author_query = " AND ($author_query)"; |
6198 } |
6199 |
|
6200 if ( is_array( $parent ) ) { |
|
6201 $post_parent__in = array_map( 'absint', (array) $parent ); |
|
6202 if ( ! empty( $post_parent__in ) ) { |
|
6203 $query_args['post_parent__in'] = $post_parent__in; |
|
6204 } |
|
6205 } elseif ( $parent >= 0 ) { |
|
6206 $query_args['post_parent'] = $parent; |
|
6207 } |
|
6208 |
|
6209 /* |
|
6210 * Maintain backward compatibility for `sort_column` key. |
|
6211 * Additionally to `WP_Query`, it has been supporting the `post_modified_gmt` field, so this logic will translate |
|
6212 * it to `post_modified` which should result in the same order given the two dates in the fields match. |
|
6213 */ |
|
6214 $orderby = wp_parse_list( $parsed_args['sort_column'] ); |
|
6215 $orderby = array_map( |
|
6216 static function ( $orderby_field ) { |
|
6217 $orderby_field = trim( $orderby_field ); |
|
6218 if ( 'post_modified_gmt' === $orderby_field || 'modified_gmt' === $orderby_field ) { |
|
6219 $orderby_field = str_replace( '_gmt', '', $orderby_field ); |
|
6062 } |
6220 } |
6063 } |
6221 return $orderby_field; |
6064 } |
6222 }, |
6065 |
6223 $orderby |
6066 $join = ''; |
|
6067 $where = "$exclusions $inclusions "; |
|
6068 if ( '' !== $meta_key || '' !== $meta_value ) { |
|
6069 $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
|
6070 |
|
6071 // meta_key and meta_value might be slashed. |
|
6072 $meta_key = wp_unslash( $meta_key ); |
|
6073 $meta_value = wp_unslash( $meta_value ); |
|
6074 if ( '' !== $meta_key ) { |
|
6075 $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key ); |
|
6076 } |
|
6077 if ( '' !== $meta_value ) { |
|
6078 $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value ); |
|
6079 } |
|
6080 } |
|
6081 |
|
6082 if ( is_array( $parent ) ) { |
|
6083 $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); |
|
6084 if ( ! empty( $post_parent__in ) ) { |
|
6085 $where .= " AND post_parent IN ($post_parent__in)"; |
|
6086 } |
|
6087 } elseif ( $parent >= 0 ) { |
|
6088 $where .= $wpdb->prepare( ' AND post_parent = %d ', $parent ); |
|
6089 } |
|
6090 |
|
6091 if ( 1 === count( $post_status ) ) { |
|
6092 $where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) ); |
|
6093 } else { |
|
6094 $post_status = implode( "', '", $post_status ); |
|
6095 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] ); |
|
6096 } |
|
6097 |
|
6098 $orderby_array = array(); |
|
6099 $allowed_keys = array( |
|
6100 'author', |
|
6101 'post_author', |
|
6102 'date', |
|
6103 'post_date', |
|
6104 'title', |
|
6105 'post_title', |
|
6106 'name', |
|
6107 'post_name', |
|
6108 'modified', |
|
6109 'post_modified', |
|
6110 'modified_gmt', |
|
6111 'post_modified_gmt', |
|
6112 'menu_order', |
|
6113 'parent', |
|
6114 'post_parent', |
|
6115 'ID', |
|
6116 'rand', |
|
6117 'comment_count', |
|
6118 ); |
6224 ); |
6119 |
6225 if ( $orderby ) { |
6120 foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) { |
6226 $query_args['orderby'] = array_fill_keys( $orderby, $parsed_args['sort_order'] ); |
6121 $orderby = trim( $orderby ); |
6227 } |
6122 if ( ! in_array( $orderby, $allowed_keys, true ) ) { |
6228 |
6123 continue; |
6229 $order = $parsed_args['sort_order']; |
6124 } |
6230 if ( $order ) { |
6125 |
6231 $query_args['order'] = $order; |
6126 switch ( $orderby ) { |
6232 } |
6127 case 'menu_order': |
|
6128 break; |
|
6129 case 'ID': |
|
6130 $orderby = "$wpdb->posts.ID"; |
|
6131 break; |
|
6132 case 'rand': |
|
6133 $orderby = 'RAND()'; |
|
6134 break; |
|
6135 case 'comment_count': |
|
6136 $orderby = "$wpdb->posts.comment_count"; |
|
6137 break; |
|
6138 default: |
|
6139 if ( 0 === strpos( $orderby, 'post_' ) ) { |
|
6140 $orderby = "$wpdb->posts." . $orderby; |
|
6141 } else { |
|
6142 $orderby = "$wpdb->posts.post_" . $orderby; |
|
6143 } |
|
6144 } |
|
6145 |
|
6146 $orderby_array[] = $orderby; |
|
6147 |
|
6148 } |
|
6149 $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; |
|
6150 |
|
6151 $sort_order = strtoupper( $parsed_args['sort_order'] ); |
|
6152 if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) { |
|
6153 $sort_order = 'ASC'; |
|
6154 } |
|
6155 |
|
6156 $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
|
6157 $query .= $author_query; |
|
6158 $query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order; |
|
6159 |
6233 |
6160 if ( ! empty( $number ) ) { |
6234 if ( ! empty( $number ) ) { |
6161 $query .= ' LIMIT ' . $offset . ',' . $number; |
6235 $query_args['posts_per_page'] = $number; |
6162 } |
6236 } |
6163 |
6237 |
6164 $pages = $wpdb->get_results( $query ); |
6238 /** |
6165 |
6239 * Filters query arguments passed to WP_Query in get_pages. |
6166 if ( empty( $pages ) ) { |
6240 * |
6167 wp_cache_set( $cache_key, array(), 'posts' ); |
6241 * @since 6.3.0 |
6168 |
6242 * |
6169 /** This filter is documented in wp-includes/post.php */ |
6243 * @param array $query_args Array of arguments passed to WP_Query. |
6170 $pages = apply_filters( 'get_pages', array(), $parsed_args ); |
6244 * @param array $parsed_args Array of get_pages() arguments. |
6171 |
6245 */ |
6172 return $pages; |
6246 $query_args = apply_filters( 'get_pages_query_args', $query_args, $parsed_args ); |
6173 } |
6247 |
6174 |
6248 $pages = new WP_Query(); |
6175 // Sanitize before caching so it'll only get done once. |
6249 $pages = $pages->query( $query_args ); |
6176 $num_pages = count( $pages ); |
|
6177 for ( $i = 0; $i < $num_pages; $i++ ) { |
|
6178 $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); |
|
6179 } |
|
6180 |
|
6181 // Update cache. |
|
6182 update_post_cache( $pages ); |
|
6183 |
6250 |
6184 if ( $child_of || $hierarchical ) { |
6251 if ( $child_of || $hierarchical ) { |
6185 $pages = get_page_children( $child_of, $pages ); |
6252 $pages = get_page_children( $child_of, $pages ); |
6186 } |
6253 } |
6187 |
6254 |
6200 unset( $pages[ $i ] ); |
6267 unset( $pages[ $i ] ); |
6201 } |
6268 } |
6202 } |
6269 } |
6203 } |
6270 } |
6204 |
6271 |
6205 $page_structure = array(); |
|
6206 foreach ( $pages as $page ) { |
|
6207 $page_structure[] = $page->ID; |
|
6208 } |
|
6209 |
|
6210 wp_cache_set( $cache_key, $page_structure, 'posts' ); |
|
6211 |
|
6212 // Convert to WP_Post instances. |
|
6213 $pages = array_map( 'get_post', $pages ); |
|
6214 |
|
6215 /** |
6272 /** |
6216 * Filters the retrieved list of pages. |
6273 * Filters the retrieved list of pages. |
6217 * |
6274 * |
6218 * @since 2.1.0 |
6275 * @since 2.1.0 |
6219 * |
6276 * |
6238 * |
6295 * |
6239 * @param string $url URL to check |
6296 * @param string $url URL to check |
6240 * @return bool True on success, false on failure. |
6297 * @return bool True on success, false on failure. |
6241 */ |
6298 */ |
6242 function is_local_attachment( $url ) { |
6299 function is_local_attachment( $url ) { |
6243 if ( strpos( $url, home_url() ) === false ) { |
6300 if ( ! str_contains( $url, home_url() ) ) { |
6244 return false; |
6301 return false; |
6245 } |
6302 } |
6246 if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) { |
6303 if ( str_contains( $url, home_url( '/?attachment_id=' ) ) ) { |
6247 return true; |
6304 return true; |
6248 } |
6305 } |
6249 |
6306 |
6250 $id = url_to_postid( $url ); |
6307 $id = url_to_postid( $url ); |
6251 if ( $id ) { |
6308 if ( $id ) { |
6256 } |
6313 } |
6257 return false; |
6314 return false; |
6258 } |
6315 } |
6259 |
6316 |
6260 /** |
6317 /** |
6261 * Insert an attachment. |
6318 * Inserts an attachment. |
6262 * |
6319 * |
6263 * If you set the 'ID' in the $args parameter, it will mean that you are |
6320 * If you set the 'ID' in the $args parameter, it will mean that you are |
6264 * updating and attempt to update the attachment. You can also set the |
6321 * updating and attempt to update the attachment. You can also set the |
6265 * attachment name or title by setting the key 'post_name' or 'post_title'. |
6322 * attachment name or title by setting the key 'post_name' or 'post_title'. |
6266 * |
6323 * |
6276 * @since 5.6.0 Added the `$fire_after_hooks` parameter. |
6333 * @since 5.6.0 Added the `$fire_after_hooks` parameter. |
6277 * |
6334 * |
6278 * @see wp_insert_post() |
6335 * @see wp_insert_post() |
6279 * |
6336 * |
6280 * @param string|array $args Arguments for inserting an attachment. |
6337 * @param string|array $args Arguments for inserting an attachment. |
6281 * @param string|false $file Optional. Filename. |
6338 * @param string|false $file Optional. Filename. Default false. |
6282 * @param int $parent Optional. Parent post ID. |
6339 * @param int $parent_post_id Optional. Parent post ID or 0 for no parent. Default 0. |
6283 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
6340 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
6284 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
6341 * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
6285 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. |
6342 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. |
6286 */ |
6343 */ |
6287 function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) { |
6344 function wp_insert_attachment( $args, $file = false, $parent_post_id = 0, $wp_error = false, $fire_after_hooks = true ) { |
6288 $defaults = array( |
6345 $defaults = array( |
6289 'file' => $file, |
6346 'file' => $file, |
6290 'post_parent' => 0, |
6347 'post_parent' => 0, |
6291 ); |
6348 ); |
6292 |
6349 |
6293 $data = wp_parse_args( $args, $defaults ); |
6350 $data = wp_parse_args( $args, $defaults ); |
6294 |
6351 |
6295 if ( ! empty( $parent ) ) { |
6352 if ( ! empty( $parent_post_id ) ) { |
6296 $data['post_parent'] = $parent; |
6353 $data['post_parent'] = $parent_post_id; |
6297 } |
6354 } |
6298 |
6355 |
6299 $data['post_type'] = 'attachment'; |
6356 $data['post_type'] = 'attachment'; |
6300 |
6357 |
6301 return wp_insert_post( $data, $wp_error, $fire_after_hooks ); |
6358 return wp_insert_post( $data, $wp_error, $fire_after_hooks ); |
6302 } |
6359 } |
6303 |
6360 |
6304 /** |
6361 /** |
6305 * Trash or delete an attachment. |
6362 * Trashes or deletes an attachment. |
6306 * |
6363 * |
6307 * When an attachment is permanently deleted, the file will also be removed. |
6364 * When an attachment is permanently deleted, the file will also be removed. |
6308 * Deletion removes all post meta fields, taxonomy, comments, etc. associated |
6365 * Deletion removes all post meta fields, taxonomy, comments, etc. associated |
6309 * with the attachment (except the main post). |
6366 * with the attachment (except the main post). |
6310 * |
6367 * |
6342 /** |
6399 /** |
6343 * Filters whether an attachment deletion should take place. |
6400 * Filters whether an attachment deletion should take place. |
6344 * |
6401 * |
6345 * @since 5.5.0 |
6402 * @since 5.5.0 |
6346 * |
6403 * |
6347 * @param WP_Post|false|null $delete Whether to go forward with deletion. @TODO description |
6404 * @param WP_Post|false|null $delete Whether to go forward with deletion. |
6348 * @param WP_Post $post Post object. |
6405 * @param WP_Post $post Post object. |
6349 * @param bool $force_delete Whether to bypass the Trash. |
6406 * @param bool $force_delete Whether to bypass the Trash. |
6350 */ |
6407 */ |
6351 $check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete ); |
6408 $check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete ); |
6352 if ( null !== $check ) { |
6409 if ( null !== $check ) { |
6504 |
6561 |
6505 /** |
6562 /** |
6506 * Retrieves attachment metadata for attachment ID. |
6563 * Retrieves attachment metadata for attachment ID. |
6507 * |
6564 * |
6508 * @since 2.1.0 |
6565 * @since 2.1.0 |
6566 * @since 6.0.0 The `$filesize` value was added to the returned array. |
|
6509 * |
6567 * |
6510 * @param int $attachment_id Attachment post ID. Defaults to global $post. |
6568 * @param int $attachment_id Attachment post ID. Defaults to global $post. |
6511 * @param bool $unfiltered Optional. If true, filters are not run. Default false. |
6569 * @param bool $unfiltered Optional. If true, filters are not run. Default false. |
6512 * @return array|false { |
6570 * @return array|false { |
6513 * Attachment metadata. False on failure. |
6571 * Attachment metadata. False on failure. |
6588 return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
6646 return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
6589 } |
6647 } |
6590 } |
6648 } |
6591 |
6649 |
6592 /** |
6650 /** |
6593 * Retrieve the URL for an attachment. |
6651 * Retrieves the URL for an attachment. |
6594 * |
6652 * |
6595 * @since 2.1.0 |
6653 * @since 2.1.0 |
6596 * |
6654 * |
6597 * @global string $pagenow The filename of the current screen. |
6655 * @global string $pagenow The filename of the current screen. |
6598 * |
6656 * |
6620 if ( $file ) { |
6678 if ( $file ) { |
6621 // Get upload directory. |
6679 // Get upload directory. |
6622 $uploads = wp_get_upload_dir(); |
6680 $uploads = wp_get_upload_dir(); |
6623 if ( $uploads && false === $uploads['error'] ) { |
6681 if ( $uploads && false === $uploads['error'] ) { |
6624 // Check that the upload base exists in the file location. |
6682 // Check that the upload base exists in the file location. |
6625 if ( 0 === strpos( $file, $uploads['basedir'] ) ) { |
6683 if ( str_starts_with( $file, $uploads['basedir'] ) ) { |
6626 // Replace file location with url location. |
6684 // Replace file location with url location. |
6627 $url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); |
6685 $url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); |
6628 } elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) { |
6686 } elseif ( str_contains( $file, 'wp-content/uploads' ) ) { |
6629 // Get the directory name relative to the basedir (back compat for pre-2.7 uploads). |
6687 // Get the directory name relative to the basedir (back compat for pre-2.7 uploads). |
6630 $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); |
6688 $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); |
6631 } else { |
6689 } else { |
6632 // It's a newly-uploaded file, therefore $file is relative to the basedir. |
6690 // It's a newly-uploaded file, therefore $file is relative to the basedir. |
6633 $url = $uploads['baseurl'] . "/$file"; |
6691 $url = $uploads['baseurl'] . "/$file"; |
6697 */ |
6755 */ |
6698 return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); |
6756 return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); |
6699 } |
6757 } |
6700 |
6758 |
6701 /** |
6759 /** |
6702 * Retrieve thumbnail for an attachment. |
6760 * Retrieves URL for an attachment thumbnail. |
6703 * |
6761 * |
6704 * @since 2.1.0 |
6762 * @since 2.1.0 |
6763 * @since 6.1.0 Changed to use wp_get_attachment_image_url(). |
|
6705 * |
6764 * |
6706 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
6765 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
6707 * @return string|false Thumbnail file path on success, false on failure. |
6766 * @return string|false Thumbnail URL on success, false on failure. |
6708 */ |
6767 */ |
6709 function wp_get_attachment_thumb_file( $post_id = 0 ) { |
6768 function wp_get_attachment_thumb_url( $post_id = 0 ) { |
6710 $post_id = (int) $post_id; |
6769 $post_id = (int) $post_id; |
6711 $post = get_post( $post_id ); |
6770 |
6771 /* |
|
6772 * This uses image_downsize() which also looks for the (very) old format $image_meta['thumb'] |
|
6773 * when the newer format $image_meta['sizes']['thumbnail'] doesn't exist. |
|
6774 */ |
|
6775 $thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' ); |
|
6776 |
|
6777 if ( empty( $thumbnail_url ) ) { |
|
6778 return false; |
|
6779 } |
|
6780 |
|
6781 /** |
|
6782 * Filters the attachment thumbnail URL. |
|
6783 * |
|
6784 * @since 2.1.0 |
|
6785 * |
|
6786 * @param string $thumbnail_url URL for the attachment thumbnail. |
|
6787 * @param int $post_id Attachment ID. |
|
6788 */ |
|
6789 return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id ); |
|
6790 } |
|
6791 |
|
6792 /** |
|
6793 * Verifies an attachment is of a given type. |
|
6794 * |
|
6795 * @since 4.2.0 |
|
6796 * |
|
6797 * @param string $type Attachment type. Accepts `image`, `audio`, `video`, or a file extension. |
|
6798 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
|
6799 * @return bool True if an accepted type or a matching file extension, false otherwise. |
|
6800 */ |
|
6801 function wp_attachment_is( $type, $post = null ) { |
|
6802 $post = get_post( $post ); |
|
6712 |
6803 |
6713 if ( ! $post ) { |
6804 if ( ! $post ) { |
6714 return false; |
6805 return false; |
6715 } |
6806 } |
6716 |
6807 |
6717 $imagedata = wp_get_attachment_metadata( $post->ID ); |
|
6718 if ( ! is_array( $imagedata ) ) { |
|
6719 return false; |
|
6720 } |
|
6721 |
|
6722 $file = get_attached_file( $post->ID ); |
|
6723 |
|
6724 if ( ! empty( $imagedata['thumb'] ) ) { |
|
6725 $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ); |
|
6726 if ( file_exists( $thumbfile ) ) { |
|
6727 /** |
|
6728 * Filters the attachment thumbnail file path. |
|
6729 * |
|
6730 * @since 2.1.0 |
|
6731 * |
|
6732 * @param string $thumbfile File path to the attachment thumbnail. |
|
6733 * @param int $post_id Attachment ID. |
|
6734 */ |
|
6735 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
|
6736 } |
|
6737 } |
|
6738 return false; |
|
6739 } |
|
6740 |
|
6741 /** |
|
6742 * Retrieve URL for an attachment thumbnail. |
|
6743 * |
|
6744 * @since 2.1.0 |
|
6745 * |
|
6746 * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
|
6747 * @return string|false Thumbnail URL on success, false on failure. |
|
6748 */ |
|
6749 function wp_get_attachment_thumb_url( $post_id = 0 ) { |
|
6750 $post_id = (int) $post_id; |
|
6751 $post = get_post( $post_id ); |
|
6752 |
|
6753 if ( ! $post ) { |
|
6754 return false; |
|
6755 } |
|
6756 |
|
6757 $url = wp_get_attachment_url( $post->ID ); |
|
6758 if ( ! $url ) { |
|
6759 return false; |
|
6760 } |
|
6761 |
|
6762 $sized = image_downsize( $post_id, 'thumbnail' ); |
|
6763 if ( $sized ) { |
|
6764 return $sized[0]; |
|
6765 } |
|
6766 |
|
6767 $thumb = wp_get_attachment_thumb_file( $post->ID ); |
|
6768 if ( ! $thumb ) { |
|
6769 return false; |
|
6770 } |
|
6771 |
|
6772 $url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url ); |
|
6773 |
|
6774 /** |
|
6775 * Filters the attachment thumbnail URL. |
|
6776 * |
|
6777 * @since 2.1.0 |
|
6778 * |
|
6779 * @param string $url URL for the attachment thumbnail. |
|
6780 * @param int $post_id Attachment ID. |
|
6781 */ |
|
6782 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
|
6783 } |
|
6784 |
|
6785 /** |
|
6786 * Verifies an attachment is of a given type. |
|
6787 * |
|
6788 * @since 4.2.0 |
|
6789 * |
|
6790 * @param string $type Attachment type. Accepts 'image', 'audio', or 'video'. |
|
6791 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
|
6792 * @return bool True if one of the accepted types, false otherwise. |
|
6793 */ |
|
6794 function wp_attachment_is( $type, $post = null ) { |
|
6795 $post = get_post( $post ); |
|
6796 |
|
6797 if ( ! $post ) { |
|
6798 return false; |
|
6799 } |
|
6800 |
|
6801 $file = get_attached_file( $post->ID ); |
6808 $file = get_attached_file( $post->ID ); |
6802 |
6809 |
6803 if ( ! $file ) { |
6810 if ( ! $file ) { |
6804 return false; |
6811 return false; |
6805 } |
6812 } |
6806 |
6813 |
6807 if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { |
6814 if ( str_starts_with( $post->post_mime_type, $type . '/' ) ) { |
6808 return true; |
6815 return true; |
6809 } |
6816 } |
6810 |
6817 |
6811 $check = wp_check_filetype( $file ); |
6818 $check = wp_check_filetype( $file ); |
6812 |
6819 |
6820 return $type === $ext; |
6827 return $type === $ext; |
6821 } |
6828 } |
6822 |
6829 |
6823 switch ( $type ) { |
6830 switch ( $type ) { |
6824 case 'image': |
6831 case 'image': |
6825 $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ); |
6832 $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif' ); |
6826 return in_array( $ext, $image_exts, true ); |
6833 return in_array( $ext, $image_exts, true ); |
6827 |
6834 |
6828 case 'audio': |
6835 case 'audio': |
6829 return in_array( $ext, wp_get_audio_extensions(), true ); |
6836 return in_array( $ext, wp_get_audio_extensions(), true ); |
6830 |
6837 |
6853 function wp_attachment_is_image( $post = null ) { |
6860 function wp_attachment_is_image( $post = null ) { |
6854 return wp_attachment_is( 'image', $post ); |
6861 return wp_attachment_is( 'image', $post ); |
6855 } |
6862 } |
6856 |
6863 |
6857 /** |
6864 /** |
6858 * Retrieve the icon for a MIME type or attachment. |
6865 * Retrieves the icon for a MIME type or attachment. |
6859 * |
6866 * |
6860 * @since 2.1.0 |
6867 * @since 2.1.0 |
6861 * |
6868 * @since 6.5.0 Added the `$preferred_ext` parameter. |
6862 * @param string|int $mime MIME type or attachment ID. |
6869 * |
6870 * @param string|int $mime MIME type or attachment ID. |
|
6871 * @param string $preferred_ext File format to prefer in return. Default '.png'. |
|
6863 * @return string|false Icon, false otherwise. |
6872 * @return string|false Icon, false otherwise. |
6864 */ |
6873 */ |
6865 function wp_mime_type_icon( $mime = 0 ) { |
6874 function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) { |
6866 if ( ! is_numeric( $mime ) ) { |
6875 if ( ! is_numeric( $mime ) ) { |
6867 $icon = wp_cache_get( "mime_type_icon_$mime" ); |
6876 $icon = wp_cache_get( "mime_type_icon_$mime" ); |
6877 } |
|
6878 |
|
6879 // Check if preferred file format variable is present and is a validly formatted file extension. |
|
6880 if ( ! empty( $preferred_ext ) && is_string( $preferred_ext ) && ! str_starts_with( $preferred_ext, '.' ) ) { |
|
6881 $preferred_ext = '.' . strtolower( $preferred_ext ); |
|
6868 } |
6882 } |
6869 |
6883 |
6870 $post_id = 0; |
6884 $post_id = 0; |
6871 if ( empty( $icon ) ) { |
6885 if ( empty( $icon ) ) { |
6872 $post_mimes = array(); |
6886 $post_mimes = array(); |
6920 * |
6934 * |
6921 * @param string[] $uris Array of icon directory URIs keyed by directory absolute path. |
6935 * @param string[] $uris Array of icon directory URIs keyed by directory absolute path. |
6922 */ |
6936 */ |
6923 $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); |
6937 $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); |
6924 $icon_files = array(); |
6938 $icon_files = array(); |
6939 $all_icons = array(); |
|
6925 while ( $dirs ) { |
6940 while ( $dirs ) { |
6926 $keys = array_keys( $dirs ); |
6941 $keys = array_keys( $dirs ); |
6927 $dir = array_shift( $keys ); |
6942 $dir = array_shift( $keys ); |
6928 $uri = array_shift( $dirs ); |
6943 $uri = array_shift( $dirs ); |
6929 $dh = opendir( $dir ); |
6944 $dh = opendir( $dir ); |
6930 if ( $dh ) { |
6945 if ( $dh ) { |
6931 while ( false !== $file = readdir( $dh ) ) { |
6946 while ( false !== $file = readdir( $dh ) ) { |
6932 $file = wp_basename( $file ); |
6947 $file = wp_basename( $file ); |
6933 if ( '.' === substr( $file, 0, 1 ) ) { |
6948 if ( str_starts_with( $file, '.' ) ) { |
6934 continue; |
6949 continue; |
6935 } |
6950 } |
6936 |
6951 |
6937 $ext = strtolower( substr( $file, -4 ) ); |
6952 $ext = strtolower( substr( $file, -4 ) ); |
6938 if ( ! in_array( $ext, array( '.png', '.gif', '.jpg' ), true ) ) { |
6953 if ( ! in_array( $ext, array( '.svg', '.png', '.gif', '.jpg' ), true ) ) { |
6939 if ( is_dir( "$dir/$file" ) ) { |
6954 if ( is_dir( "$dir/$file" ) ) { |
6940 $dirs[ "$dir/$file" ] = "$uri/$file"; |
6955 $dirs[ "$dir/$file" ] = "$uri/$file"; |
6941 } |
6956 } |
6942 continue; |
6957 continue; |
6943 } |
6958 } |
6944 $icon_files[ "$dir/$file" ] = "$uri/$file"; |
6959 $all_icons[ "$dir/$file" ] = "$uri/$file"; |
6960 if ( $ext === $preferred_ext ) { |
|
6961 $icon_files[ "$dir/$file" ] = "$uri/$file"; |
|
6962 } |
|
6945 } |
6963 } |
6946 closedir( $dh ); |
6964 closedir( $dh ); |
6947 } |
6965 } |
6966 } |
|
6967 // If directory only contained icons of a non-preferred format, return those. |
|
6968 if ( empty( $icon_files ) ) { |
|
6969 $icon_files = $all_icons; |
|
6948 } |
6970 } |
6949 wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); |
6971 wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); |
6950 } |
6972 } |
6951 |
6973 |
6952 $types = array(); |
6974 $types = array(); |
6991 */ |
7013 */ |
6992 return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); |
7014 return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); |
6993 } |
7015 } |
6994 |
7016 |
6995 /** |
7017 /** |
6996 * Check for changed slugs for published post objects and save the old slug. |
7018 * Checks for changed slugs for published post objects and save the old slug. |
6997 * |
7019 * |
6998 * The function is used when a post object of any type is updated, |
7020 * The function is used when a post object of any type is updated, |
6999 * by comparing the current and previous post objects. |
7021 * by comparing the current and previous post objects. |
7000 * |
7022 * |
7001 * If the slug was changed and not already part of the old slugs then it will be |
7023 * If the slug was changed and not already part of the old slugs then it will be |
7006 * that those that linked to an changed post will be redirected to the new post. |
7028 * that those that linked to an changed post will be redirected to the new post. |
7007 * |
7029 * |
7008 * @since 2.1.0 |
7030 * @since 2.1.0 |
7009 * |
7031 * |
7010 * @param int $post_id Post ID. |
7032 * @param int $post_id Post ID. |
7011 * @param WP_Post $post The Post Object |
7033 * @param WP_Post $post The post object. |
7012 * @param WP_Post $post_before The Previous Post Object |
7034 * @param WP_Post $post_before The previous post object. |
7013 */ |
7035 */ |
7014 function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { |
7036 function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { |
7015 // Don't bother if it hasn't changed. |
7037 // Don't bother if it hasn't changed. |
7016 if ( $post->post_name == $post_before->post_name ) { |
7038 if ( $post->post_name == $post_before->post_name ) { |
7017 return; |
7039 return; |
7034 delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); |
7056 delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); |
7035 } |
7057 } |
7036 } |
7058 } |
7037 |
7059 |
7038 /** |
7060 /** |
7039 * Check for changed dates for published post objects and save the old date. |
7061 * Checks for changed dates for published post objects and save the old date. |
7040 * |
7062 * |
7041 * The function is used when a post object of any type is updated, |
7063 * The function is used when a post object of any type is updated, |
7042 * by comparing the current and previous post objects. |
7064 * by comparing the current and previous post objects. |
7043 * |
7065 * |
7044 * If the date was changed and not already part of the old dates then it will be |
7066 * If the date was changed and not already part of the old dates then it will be |
7049 * that those that linked to an changed post will be redirected to the new post. |
7071 * that those that linked to an changed post will be redirected to the new post. |
7050 * |
7072 * |
7051 * @since 4.9.3 |
7073 * @since 4.9.3 |
7052 * |
7074 * |
7053 * @param int $post_id Post ID. |
7075 * @param int $post_id Post ID. |
7054 * @param WP_Post $post The Post Object |
7076 * @param WP_Post $post The post object. |
7055 * @param WP_Post $post_before The Previous Post Object |
7077 * @param WP_Post $post_before The previous post object. |
7056 */ |
7078 */ |
7057 function wp_check_for_changed_dates( $post_id, $post, $post_before ) { |
7079 function wp_check_for_changed_dates( $post_id, $post, $post_before ) { |
7058 $previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) ); |
7080 $previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) ); |
7059 $new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) ); |
7081 $new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) ); |
7060 |
7082 |
7080 delete_post_meta( $post_id, '_wp_old_date', $new_date ); |
7102 delete_post_meta( $post_id, '_wp_old_date', $new_date ); |
7081 } |
7103 } |
7082 } |
7104 } |
7083 |
7105 |
7084 /** |
7106 /** |
7085 * Retrieve the private post SQL based on capability. |
7107 * Retrieves the private post SQL based on capability. |
7086 * |
7108 * |
7087 * This function provides a standardized way to appropriately select on the |
7109 * This function provides a standardized way to appropriately select on the |
7088 * post_status of a post type. The function will return a piece of SQL code |
7110 * post_status of a post type. The function will return a piece of SQL code |
7089 * that can be added to a WHERE clause; this SQL is constructed to allow all |
7111 * that can be added to a WHERE clause; this SQL is constructed to allow all |
7090 * published posts, and all private posts to which the user has access. |
7112 * published posts, and all private posts to which the user has access. |
7098 function get_private_posts_cap_sql( $post_type ) { |
7120 function get_private_posts_cap_sql( $post_type ) { |
7099 return get_posts_by_author_sql( $post_type, false ); |
7121 return get_posts_by_author_sql( $post_type, false ); |
7100 } |
7122 } |
7101 |
7123 |
7102 /** |
7124 /** |
7103 * Retrieve the post SQL based on capability, author, and type. |
7125 * Retrieves the post SQL based on capability, author, and type. |
7104 * |
7126 * |
7105 * @since 3.0.0 |
7127 * @since 3.0.0 |
7106 * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. |
7128 * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. |
7107 * |
7129 * |
7108 * @see get_private_posts_cap_sql() |
7130 * @see get_private_posts_cap_sql() |
7126 } |
7148 } |
7127 |
7149 |
7128 $post_type_clauses = array(); |
7150 $post_type_clauses = array(); |
7129 foreach ( $post_types as $post_type ) { |
7151 foreach ( $post_types as $post_type ) { |
7130 $post_type_obj = get_post_type_object( $post_type ); |
7152 $post_type_obj = get_post_type_object( $post_type ); |
7153 |
|
7131 if ( ! $post_type_obj ) { |
7154 if ( ! $post_type_obj ) { |
7132 continue; |
7155 continue; |
7133 } |
7156 } |
7134 |
7157 |
7135 /** |
7158 /** |
7140 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". |
7163 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". |
7141 * |
7164 * |
7142 * @param string $cap Capability. |
7165 * @param string $cap Capability. |
7143 */ |
7166 */ |
7144 $cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' ); |
7167 $cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' ); |
7168 |
|
7145 if ( ! $cap ) { |
7169 if ( ! $cap ) { |
7146 $cap = current_user_can( $post_type_obj->cap->read_private_posts ); |
7170 $cap = current_user_can( $post_type_obj->cap->read_private_posts ); |
7147 } |
7171 } |
7148 |
7172 |
7149 // Only need to check the cap if $public_only is false. |
7173 // Only need to check the cap if $public_only is false. |
7150 $post_status_sql = "post_status = 'publish'"; |
7174 $post_status_sql = "post_status = 'publish'"; |
7175 |
|
7151 if ( false === $public_only ) { |
7176 if ( false === $public_only ) { |
7152 if ( $cap ) { |
7177 if ( $cap ) { |
7153 // Does the user have the capability to view private posts? Guess so. |
7178 // Does the user have the capability to view private posts? Guess so. |
7154 $post_status_sql .= " OR post_status = 'private'"; |
7179 $post_status_sql .= " OR post_status = 'private'"; |
7155 } elseif ( is_user_logged_in() ) { |
7180 } elseif ( is_user_logged_in() ) { |
7218 */ |
7243 */ |
7219 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type ); |
7244 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type ); |
7220 } |
7245 } |
7221 |
7246 |
7222 /** |
7247 /** |
7223 * Get the most recent time that a post on the site was modified. |
7248 * Gets the most recent time that a post on the site was modified. |
7224 * |
7249 * |
7225 * The server timezone is the default and is the difference between GMT and |
7250 * The server timezone is the default and is the difference between GMT and |
7226 * server time. The 'blog' value is just when the last post was modified. |
7251 * server time. The 'blog' value is just when the last post was modified. |
7227 * The 'gmt' is when the last post was modified in GMT time. |
7252 * The 'gmt' is when the last post was modified in GMT time. |
7228 * |
7253 * |
7389 if ( ! $post ) { |
7414 if ( ! $post ) { |
7390 return; |
7415 return; |
7391 } |
7416 } |
7392 |
7417 |
7393 wp_cache_delete( $post->ID, 'posts' ); |
7418 wp_cache_delete( $post->ID, 'posts' ); |
7419 wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' ); |
|
7394 wp_cache_delete( $post->ID, 'post_meta' ); |
7420 wp_cache_delete( $post->ID, 'post_meta' ); |
7395 |
7421 |
7396 clean_object_term_cache( $post->ID, $post->post_type ); |
7422 clean_object_term_cache( $post->ID, $post->post_type ); |
7397 |
7423 |
7398 wp_cache_delete( 'wp_get_archives', 'general' ); |
7424 wp_cache_delete( 'wp_get_archives', 'general' ); |
7418 * @param int $post_id Post ID. |
7444 * @param int $post_id Post ID. |
7419 */ |
7445 */ |
7420 do_action( 'clean_page_cache', $post->ID ); |
7446 do_action( 'clean_page_cache', $post->ID ); |
7421 } |
7447 } |
7422 |
7448 |
7423 wp_cache_set( 'last_changed', microtime(), 'posts' ); |
7449 wp_cache_set_posts_last_changed(); |
7424 } |
7450 } |
7425 |
7451 |
7426 /** |
7452 /** |
7427 * Call major cache updating functions for list of Post objects. |
7453 * Updates post, term, and metadata caches for a list of post objects. |
7428 * |
7454 * |
7429 * @since 1.5.0 |
7455 * @since 1.5.0 |
7430 * |
7456 * |
7431 * @param WP_Post[] $posts Array of Post objects |
7457 * @param WP_Post[] $posts Array of post objects (passed by reference). |
7432 * @param string $post_type Optional. Post type. Default 'post'. |
7458 * @param string $post_type Optional. Post type. Default 'post'. |
7433 * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
7459 * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
7434 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
7460 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
7435 */ |
7461 */ |
7436 function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { |
7462 function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { |
7473 update_postmeta_cache( $post_ids ); |
7499 update_postmeta_cache( $post_ids ); |
7474 } |
7500 } |
7475 } |
7501 } |
7476 |
7502 |
7477 /** |
7503 /** |
7478 * Updates metadata cache for list of post IDs. |
7504 * Updates post author user caches for a list of post objects. |
7505 * |
|
7506 * @since 6.1.0 |
|
7507 * |
|
7508 * @param WP_Post[] $posts Array of post objects. |
|
7509 */ |
|
7510 function update_post_author_caches( $posts ) { |
|
7511 /* |
|
7512 * cache_users() is a pluggable function so is not available prior |
|
7513 * to the `plugins_loaded` hook firing. This is to ensure against |
|
7514 * fatal errors when the function is not available. |
|
7515 */ |
|
7516 if ( ! function_exists( 'cache_users' ) ) { |
|
7517 return; |
|
7518 } |
|
7519 |
|
7520 $author_ids = wp_list_pluck( $posts, 'post_author' ); |
|
7521 $author_ids = array_map( 'absint', $author_ids ); |
|
7522 $author_ids = array_unique( array_filter( $author_ids ) ); |
|
7523 |
|
7524 cache_users( $author_ids ); |
|
7525 } |
|
7526 |
|
7527 /** |
|
7528 * Updates parent post caches for a list of post objects. |
|
7529 * |
|
7530 * @since 6.1.0 |
|
7531 * |
|
7532 * @param WP_Post[] $posts Array of post objects. |
|
7533 */ |
|
7534 function update_post_parent_caches( $posts ) { |
|
7535 $parent_ids = wp_list_pluck( $posts, 'post_parent' ); |
|
7536 $parent_ids = array_map( 'absint', $parent_ids ); |
|
7537 $parent_ids = array_unique( array_filter( $parent_ids ) ); |
|
7538 |
|
7539 if ( ! empty( $parent_ids ) ) { |
|
7540 _prime_post_caches( $parent_ids, false ); |
|
7541 } |
|
7542 } |
|
7543 |
|
7544 /** |
|
7545 * Updates metadata cache for a list of post IDs. |
|
7479 * |
7546 * |
7480 * Performs SQL query to retrieve the metadata for the post IDs and updates the |
7547 * Performs SQL query to retrieve the metadata for the post IDs and updates the |
7481 * metadata cache for the posts. Therefore, the functions, which call this |
7548 * metadata cache for the posts. Therefore, the functions, which call this |
7482 * function, do not need to perform SQL queries on their own. |
7549 * function, do not need to perform SQL queries on their own. |
7483 * |
7550 * |
7655 * @return int|false Post parent ID (which can be 0 if there is no parent), |
7722 * @return int|false Post parent ID (which can be 0 if there is no parent), |
7656 * or false if the post does not exist. |
7723 * or false if the post does not exist. |
7657 */ |
7724 */ |
7658 function wp_get_post_parent_id( $post = null ) { |
7725 function wp_get_post_parent_id( $post = null ) { |
7659 $post = get_post( $post ); |
7726 $post = get_post( $post ); |
7727 |
|
7660 if ( ! $post || is_wp_error( $post ) ) { |
7728 if ( ! $post || is_wp_error( $post ) ) { |
7661 return false; |
7729 return false; |
7662 } |
7730 } |
7731 |
|
7663 return (int) $post->post_parent; |
7732 return (int) $post->post_parent; |
7664 } |
7733 } |
7665 |
7734 |
7666 /** |
7735 /** |
7667 * Check the given subset of the post hierarchy for hierarchy loops. |
7736 * Checks the given subset of the post hierarchy for hierarchy loops. |
7668 * |
7737 * |
7669 * Prevents loops from forming and breaks those that it finds. Attached |
7738 * Prevents loops from forming and breaks those that it finds. Attached |
7670 * to the {@see 'wp_insert_post_parent'} filter. |
7739 * to the {@see 'wp_insert_post_parent'} filter. |
7671 * |
7740 * |
7672 * @since 3.1.0 |
7741 * @since 3.1.0 |
7673 * |
7742 * |
7674 * @see wp_find_hierarchy_loop() |
7743 * @see wp_find_hierarchy_loop() |
7675 * |
7744 * |
7676 * @param int $post_parent ID of the parent for the post we're checking. |
7745 * @param int $post_parent ID of the parent for the post we're checking. |
7677 * @param int $post_ID ID of the post we're checking. |
7746 * @param int $post_id ID of the post we're checking. |
7678 * @return int The new post_parent for the post, 0 otherwise. |
7747 * @return int The new post_parent for the post, 0 otherwise. |
7679 */ |
7748 */ |
7680 function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { |
7749 function wp_check_post_hierarchy_for_loops( $post_parent, $post_id ) { |
7681 // Nothing fancy here - bail. |
7750 // Nothing fancy here - bail. |
7682 if ( ! $post_parent ) { |
7751 if ( ! $post_parent ) { |
7683 return 0; |
7752 return 0; |
7684 } |
7753 } |
7685 |
7754 |
7686 // New post can't cause a loop. |
7755 // New post can't cause a loop. |
7687 if ( ! $post_ID ) { |
7756 if ( ! $post_id ) { |
7688 return $post_parent; |
7757 return $post_parent; |
7689 } |
7758 } |
7690 |
7759 |
7691 // Can't be its own parent. |
7760 // Can't be its own parent. |
7692 if ( $post_parent == $post_ID ) { |
7761 if ( $post_parent == $post_id ) { |
7693 return 0; |
7762 return 0; |
7694 } |
7763 } |
7695 |
7764 |
7696 // Now look for larger loops. |
7765 // Now look for larger loops. |
7697 $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ); |
7766 $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_id, $post_parent ); |
7698 if ( ! $loop ) { |
7767 if ( ! $loop ) { |
7699 return $post_parent; // No loop. |
7768 return $post_parent; // No loop. |
7700 } |
7769 } |
7701 |
7770 |
7702 // Setting $post_parent to the given value causes a loop. |
7771 // Setting $post_parent to the given value causes a loop. |
7703 if ( isset( $loop[ $post_ID ] ) ) { |
7772 if ( isset( $loop[ $post_id ] ) ) { |
7704 return 0; |
7773 return 0; |
7705 } |
7774 } |
7706 |
7775 |
7707 // There's a loop, but it doesn't contain $post_ID. Break the loop. |
7776 // There's a loop, but it doesn't contain $post_id. Break the loop. |
7708 foreach ( array_keys( $loop ) as $loop_member ) { |
7777 foreach ( array_keys( $loop ) as $loop_member ) { |
7709 wp_update_post( |
7778 wp_update_post( |
7710 array( |
7779 array( |
7711 'ID' => $loop_member, |
7780 'ID' => $loop_member, |
7712 'post_parent' => 0, |
7781 'post_parent' => 0, |
7754 } |
7823 } |
7755 return false; |
7824 return false; |
7756 } |
7825 } |
7757 |
7826 |
7758 /** |
7827 /** |
7759 * Delete auto-drafts for new posts that are > 7 days old. |
7828 * Deletes auto-drafts for new posts that are > 7 days old. |
7760 * |
7829 * |
7761 * @since 3.4.0 |
7830 * @since 3.4.0 |
7762 * |
7831 * |
7763 * @global wpdb $wpdb WordPress database abstraction object. |
7832 * @global wpdb $wpdb WordPress database abstraction object. |
7764 */ |
7833 */ |
7776 /** |
7845 /** |
7777 * Queues posts for lazy-loading of term meta. |
7846 * Queues posts for lazy-loading of term meta. |
7778 * |
7847 * |
7779 * @since 4.5.0 |
7848 * @since 4.5.0 |
7780 * |
7849 * |
7781 * @param array $posts Array of WP_Post objects. |
7850 * @param WP_Post[] $posts Array of WP_Post objects. |
7782 */ |
7851 */ |
7783 function wp_queue_posts_for_term_meta_lazyload( $posts ) { |
7852 function wp_queue_posts_for_term_meta_lazyload( $posts ) { |
7784 $post_type_taxonomies = array(); |
7853 $post_type_taxonomies = array(); |
7785 $term_ids = array(); |
7854 $prime_post_terms = array(); |
7786 foreach ( $posts as $post ) { |
7855 foreach ( $posts as $post ) { |
7787 if ( ! ( $post instanceof WP_Post ) ) { |
7856 if ( ! ( $post instanceof WP_Post ) ) { |
7788 continue; |
7857 continue; |
7789 } |
7858 } |
7790 |
7859 |
7791 if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { |
7860 if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { |
7792 $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); |
7861 $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); |
7793 } |
7862 } |
7794 |
7863 |
7795 foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { |
7864 foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { |
7796 // Term cache should already be primed by `update_post_term_cache()`. |
7865 $prime_post_terms[ $taxonomy ][] = $post->ID; |
7797 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
7866 } |
7798 if ( false !== $terms ) { |
7867 } |
7799 foreach ( $terms as $term ) { |
7868 |
7800 if ( ! in_array( $term->term_id, $term_ids, true ) ) { |
7869 $term_ids = array(); |
7801 $term_ids[] = $term->term_id; |
7870 if ( $prime_post_terms ) { |
7871 foreach ( $prime_post_terms as $taxonomy => $post_ids ) { |
|
7872 $cached_term_ids = wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" ); |
|
7873 if ( is_array( $cached_term_ids ) ) { |
|
7874 $cached_term_ids = array_filter( $cached_term_ids ); |
|
7875 foreach ( $cached_term_ids as $_term_ids ) { |
|
7876 // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs. |
|
7877 foreach ( $_term_ids as $term_id ) { |
|
7878 if ( is_numeric( $term_id ) ) { |
|
7879 $term_ids[] = (int) $term_id; |
|
7880 } elseif ( isset( $term_id->term_id ) ) { |
|
7881 $term_ids[] = (int) $term_id->term_id; |
|
7882 } |
|
7802 } |
7883 } |
7803 } |
7884 } |
7804 } |
7885 } |
7805 } |
7886 } |
7806 } |
7887 $term_ids = array_unique( $term_ids ); |
7807 |
7888 } |
7808 if ( $term_ids ) { |
7889 |
7809 $lazyloader = wp_metadata_lazyloader(); |
7890 wp_lazyload_term_meta( $term_ids ); |
7810 $lazyloader->queue_objects( 'term', $term_ids ); |
7891 } |
7811 } |
7892 |
7812 } |
7893 /** |
7813 |
7894 * Updates the custom taxonomies' term counts when a post's status is changed. |
7814 /** |
|
7815 * Update the custom taxonomies' term counts when a post's status is changed. |
|
7816 * |
7895 * |
7817 * For example, default posts term counts (for custom taxonomies) don't include |
7896 * For example, default posts term counts (for custom taxonomies) don't include |
7818 * private / draft posts. |
7897 * private / draft posts. |
7819 * |
7898 * |
7820 * @since 3.3.0 |
7899 * @since 3.3.0 |
7834 |
7913 |
7835 /** |
7914 /** |
7836 * Adds any posts from the given IDs to the cache that do not already exist in cache. |
7915 * Adds any posts from the given IDs to the cache that do not already exist in cache. |
7837 * |
7916 * |
7838 * @since 3.4.0 |
7917 * @since 3.4.0 |
7839 * @access private |
7918 * @since 6.1.0 This function is no longer marked as "private". |
7840 * |
7919 * |
7841 * @see update_post_caches() |
7920 * @see update_post_cache() |
7921 * @see update_postmeta_cache() |
|
7922 * @see update_object_term_cache() |
|
7842 * |
7923 * |
7843 * @global wpdb $wpdb WordPress database abstraction object. |
7924 * @global wpdb $wpdb WordPress database abstraction object. |
7844 * |
7925 * |
7845 * @param array $ids ID list. |
7926 * @param int[] $ids ID list. |
7846 * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
7927 * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
7847 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
7928 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
7848 */ |
7929 */ |
7849 function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { |
7930 function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { |
7850 global $wpdb; |
7931 global $wpdb; |
7851 |
7932 |
7852 $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); |
7933 $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); |
7853 if ( ! empty( $non_cached_ids ) ) { |
7934 if ( ! empty( $non_cached_ids ) ) { |
7854 $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) ); |
7935 $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) ); |
7855 |
7936 |
7856 update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); |
7937 if ( $fresh_posts ) { |
7938 // Despite the name, update_post_cache() expects an array rather than a single post. |
|
7939 update_post_cache( $fresh_posts ); |
|
7940 } |
|
7941 } |
|
7942 |
|
7943 if ( $update_meta_cache ) { |
|
7944 update_postmeta_cache( $ids ); |
|
7945 } |
|
7946 |
|
7947 if ( $update_term_cache ) { |
|
7948 $post_types = array_map( 'get_post_type', $ids ); |
|
7949 $post_types = array_unique( $post_types ); |
|
7950 update_object_term_cache( $ids, $post_types ); |
|
7951 } |
|
7952 } |
|
7953 |
|
7954 /** |
|
7955 * Prime the cache containing the parent ID of various post objects. |
|
7956 * |
|
7957 * @since 6.4.0 |
|
7958 * |
|
7959 * @global wpdb $wpdb WordPress database abstraction object. |
|
7960 * |
|
7961 * @param int[] $ids ID list. |
|
7962 */ |
|
7963 function _prime_post_parent_id_caches( array $ids ) { |
|
7964 global $wpdb; |
|
7965 |
|
7966 $ids = array_filter( $ids, '_validate_cache_id' ); |
|
7967 $ids = array_unique( array_map( 'intval', $ids ), SORT_NUMERIC ); |
|
7968 |
|
7969 if ( empty( $ids ) ) { |
|
7970 return; |
|
7971 } |
|
7972 |
|
7973 $cache_keys = array(); |
|
7974 foreach ( $ids as $id ) { |
|
7975 $cache_keys[ $id ] = 'post_parent:' . (string) $id; |
|
7976 } |
|
7977 |
|
7978 $cached_data = wp_cache_get_multiple( array_values( $cache_keys ), 'posts' ); |
|
7979 |
|
7980 $non_cached_ids = array(); |
|
7981 foreach ( $cache_keys as $id => $cache_key ) { |
|
7982 if ( false === $cached_data[ $cache_key ] ) { |
|
7983 $non_cached_ids[] = $id; |
|
7984 } |
|
7985 } |
|
7986 |
|
7987 if ( ! empty( $non_cached_ids ) ) { |
|
7988 $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 ) ) ); |
|
7989 |
|
7990 if ( $fresh_posts ) { |
|
7991 $post_parent_data = array(); |
|
7992 foreach ( $fresh_posts as $fresh_post ) { |
|
7993 $post_parent_data[ 'post_parent:' . (string) $fresh_post->ID ] = (int) $fresh_post->post_parent; |
|
7994 } |
|
7995 |
|
7996 wp_cache_add_multiple( $post_parent_data, 'posts' ); |
|
7997 } |
|
7857 } |
7998 } |
7858 } |
7999 } |
7859 |
8000 |
7860 /** |
8001 /** |
7861 * Adds a suffix if any trashed posts have a given slug. |
8002 * Adds a suffix if any trashed posts have a given slug. |
7867 * |
8008 * |
7868 * @since 4.5.0 |
8009 * @since 4.5.0 |
7869 * @access private |
8010 * @access private |
7870 * |
8011 * |
7871 * @param string $post_name Post slug. |
8012 * @param string $post_name Post slug. |
7872 * @param int $post_ID Optional. Post ID that should be ignored. Default 0. |
8013 * @param int $post_id Optional. Post ID that should be ignored. Default 0. |
7873 */ |
8014 */ |
7874 function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { |
8015 function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_id = 0 ) { |
7875 $trashed_posts_with_desired_slug = get_posts( |
8016 $trashed_posts_with_desired_slug = get_posts( |
7876 array( |
8017 array( |
7877 'name' => $post_name, |
8018 'name' => $post_name, |
7878 'post_status' => 'trash', |
8019 'post_status' => 'trash', |
7879 'post_type' => 'any', |
8020 'post_type' => 'any', |
7880 'nopaging' => true, |
8021 'nopaging' => true, |
7881 'post__not_in' => array( $post_ID ), |
8022 'post__not_in' => array( $post_id ), |
7882 ) |
8023 ) |
7883 ); |
8024 ); |
7884 |
8025 |
7885 if ( ! empty( $trashed_posts_with_desired_slug ) ) { |
8026 if ( ! empty( $trashed_posts_with_desired_slug ) ) { |
7886 foreach ( $trashed_posts_with_desired_slug as $_post ) { |
8027 foreach ( $trashed_posts_with_desired_slug as $_post ) { |
7898 * For internal use. |
8039 * For internal use. |
7899 * |
8040 * |
7900 * @since 4.5.0 |
8041 * @since 4.5.0 |
7901 * @access private |
8042 * @access private |
7902 * |
8043 * |
8044 * @global wpdb $wpdb WordPress database abstraction object. |
|
8045 * |
|
7903 * @param WP_Post $post The post. |
8046 * @param WP_Post $post The post. |
7904 * @return string New slug for the post. |
8047 * @return string New slug for the post. |
7905 */ |
8048 */ |
7906 function wp_add_trashed_suffix_to_post_name_for_post( $post ) { |
8049 function wp_add_trashed_suffix_to_post_name_for_post( $post ) { |
7907 global $wpdb; |
8050 global $wpdb; |
7908 |
8051 |
7909 $post = get_post( $post ); |
8052 $post = get_post( $post ); |
7910 |
8053 |
7911 if ( '__trashed' === substr( $post->post_name, -9 ) ) { |
8054 if ( str_ends_with( $post->post_name, '__trashed' ) ) { |
7912 return $post->post_name; |
8055 return $post->post_name; |
7913 } |
8056 } |
7914 add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); |
8057 add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); |
7915 $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; |
8058 $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; |
7916 $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); |
8059 $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); |
7917 clean_post_cache( $post->ID ); |
8060 clean_post_cache( $post->ID ); |
7918 return $post_name; |
8061 return $post_name; |
7919 } |
8062 } |
7920 |
8063 |
7921 /** |
8064 /** |
7922 * Filters the SQL clauses of an attachment query to include filenames. |
8065 * Sets the last changed time for the 'posts' cache group. |
7923 * |
8066 * |
7924 * @since 4.7.0 |
8067 * @since 5.0.0 |
7925 * @access private |
8068 */ |
8069 function wp_cache_set_posts_last_changed() { |
|
8070 wp_cache_set_last_changed( 'posts' ); |
|
8071 } |
|
8072 |
|
8073 /** |
|
8074 * Gets all available post MIME types for a given post type. |
|
8075 * |
|
8076 * @since 2.5.0 |
|
7926 * |
8077 * |
7927 * @global wpdb $wpdb WordPress database abstraction object. |
8078 * @global wpdb $wpdb WordPress database abstraction object. |
7928 * |
8079 * |
7929 * @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
|
7930 * DISTINCT, fields (SELECT), and LIMITS clauses. |
|
7931 * @return string[] The modified array of clauses. |
|
7932 */ |
|
7933 function _filter_query_attachment_filenames( $clauses ) { |
|
7934 global $wpdb; |
|
7935 remove_filter( 'posts_clauses', __FUNCTION__ ); |
|
7936 |
|
7937 // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. |
|
7938 $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; |
|
7939 |
|
7940 $clauses['groupby'] = "{$wpdb->posts}.ID"; |
|
7941 |
|
7942 $clauses['where'] = preg_replace( |
|
7943 "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
|
7944 '$0 OR ( sq1.meta_value $1 $2 )', |
|
7945 $clauses['where'] |
|
7946 ); |
|
7947 |
|
7948 return $clauses; |
|
7949 } |
|
7950 |
|
7951 /** |
|
7952 * Sets the last changed time for the 'posts' cache group. |
|
7953 * |
|
7954 * @since 5.0.0 |
|
7955 */ |
|
7956 function wp_cache_set_posts_last_changed() { |
|
7957 wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
7958 } |
|
7959 |
|
7960 /** |
|
7961 * Get all available post MIME types for a given post type. |
|
7962 * |
|
7963 * @since 2.5.0 |
|
7964 * |
|
7965 * @global wpdb $wpdb WordPress database abstraction object. |
|
7966 * |
|
7967 * @param string $type |
8080 * @param string $type |
7968 * @return mixed |
8081 * @return string[] An array of MIME types. |
7969 */ |
8082 */ |
7970 function get_available_post_mime_types( $type = 'attachment' ) { |
8083 function get_available_post_mime_types( $type = 'attachment' ) { |
7971 global $wpdb; |
8084 global $wpdb; |
7972 |
8085 |
7973 $types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) ); |
8086 /** |
7974 return $types; |
8087 * Filters the list of available post MIME types for the given post type. |
8088 * |
|
8089 * @since 6.4.0 |
|
8090 * |
|
8091 * @param string[]|null $mime_types An array of MIME types. Default null. |
|
8092 * @param string $type The post type name. Usually 'attachment' but can be any post type. |
|
8093 */ |
|
8094 $mime_types = apply_filters( 'pre_get_available_post_mime_types', null, $type ); |
|
8095 |
|
8096 if ( ! is_array( $mime_types ) ) { |
|
8097 $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s AND post_mime_type != ''", $type ) ); |
|
8098 } |
|
8099 |
|
8100 // Remove nulls from returned $mime_types. |
|
8101 return array_values( array_filter( $mime_types ) ); |
|
7975 } |
8102 } |
7976 |
8103 |
7977 /** |
8104 /** |
7978 * Retrieves the path to an uploaded image file. |
8105 * Retrieves the path to an uploaded image file. |
7979 * |
8106 * |
8013 */ |
8140 */ |
8014 return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id ); |
8141 return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id ); |
8015 } |
8142 } |
8016 |
8143 |
8017 /** |
8144 /** |
8018 * Retrieve the URL to an original attachment image. |
8145 * Retrieves the URL to an original attachment image. |
8019 * |
8146 * |
8020 * Similar to `wp_get_attachment_url()` however some images may have been |
8147 * Similar to `wp_get_attachment_url()` however some images may have been |
8021 * processed after uploading. In this case this function returns the URL |
8148 * processed after uploading. In this case this function returns the URL |
8022 * to the originally uploaded image file. |
8149 * to the originally uploaded image file. |
8023 * |
8150 * |
8055 */ |
8182 */ |
8056 return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id ); |
8183 return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id ); |
8057 } |
8184 } |
8058 |
8185 |
8059 /** |
8186 /** |
8060 * Filter callback which sets the status of an untrashed post to its previous status. |
8187 * Filters callback which sets the status of an untrashed post to its previous status. |
8061 * |
8188 * |
8062 * This can be used as a callback on the `wp_untrash_post_status` filter. |
8189 * This can be used as a callback on the `wp_untrash_post_status` filter. |
8063 * |
8190 * |
8064 * @since 5.6.0 |
8191 * @since 5.6.0 |
8065 * |
8192 * |
8069 * @return string The new status of the post. |
8196 * @return string The new status of the post. |
8070 */ |
8197 */ |
8071 function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) { |
8198 function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) { |
8072 return $previous_status; |
8199 return $previous_status; |
8073 } |
8200 } |
8201 |
|
8202 /** |
|
8203 * Returns whether the post can be edited in the block editor. |
|
8204 * |
|
8205 * @since 5.0.0 |
|
8206 * @since 6.1.0 Moved to wp-includes from wp-admin. |
|
8207 * |
|
8208 * @param int|WP_Post $post Post ID or WP_Post object. |
|
8209 * @return bool Whether the post can be edited in the block editor. |
|
8210 */ |
|
8211 function use_block_editor_for_post( $post ) { |
|
8212 $post = get_post( $post ); |
|
8213 |
|
8214 if ( ! $post ) { |
|
8215 return false; |
|
8216 } |
|
8217 |
|
8218 // We're in the meta box loader, so don't use the block editor. |
|
8219 if ( is_admin() && isset( $_GET['meta-box-loader'] ) ) { |
|
8220 check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' ); |
|
8221 return false; |
|
8222 } |
|
8223 |
|
8224 $use_block_editor = use_block_editor_for_post_type( $post->post_type ); |
|
8225 |
|
8226 /** |
|
8227 * Filters whether a post is able to be edited in the block editor. |
|
8228 * |
|
8229 * @since 5.0.0 |
|
8230 * |
|
8231 * @param bool $use_block_editor Whether the post can be edited or not. |
|
8232 * @param WP_Post $post The post being checked. |
|
8233 */ |
|
8234 return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post ); |
|
8235 } |
|
8236 |
|
8237 /** |
|
8238 * Returns whether a post type is compatible with the block editor. |
|
8239 * |
|
8240 * The block editor depends on the REST API, and if the post type is not shown in the |
|
8241 * REST API, then it won't work with the block editor. |
|
8242 * |
|
8243 * @since 5.0.0 |
|
8244 * @since 6.1.0 Moved to wp-includes from wp-admin. |
|
8245 * |
|
8246 * @param string $post_type The post type. |
|
8247 * @return bool Whether the post type can be edited with the block editor. |
|
8248 */ |
|
8249 function use_block_editor_for_post_type( $post_type ) { |
|
8250 if ( ! post_type_exists( $post_type ) ) { |
|
8251 return false; |
|
8252 } |
|
8253 |
|
8254 if ( ! post_type_supports( $post_type, 'editor' ) ) { |
|
8255 return false; |
|
8256 } |
|
8257 |
|
8258 $post_type_object = get_post_type_object( $post_type ); |
|
8259 if ( $post_type_object && ! $post_type_object->show_in_rest ) { |
|
8260 return false; |
|
8261 } |
|
8262 |
|
8263 /** |
|
8264 * Filters whether a post is able to be edited in the block editor. |
|
8265 * |
|
8266 * @since 5.0.0 |
|
8267 * |
|
8268 * @param bool $use_block_editor Whether the post type can be edited or not. Default true. |
|
8269 * @param string $post_type The post type being checked. |
|
8270 */ |
|
8271 return apply_filters( 'use_block_editor_for_post_type', true, $post_type ); |
|
8272 } |
|
8273 |
|
8274 /** |
|
8275 * Registers any additional post meta fields. |
|
8276 * |
|
8277 * @since 6.3.0 Adds `wp_pattern_sync_status` meta field to the wp_block post type so an unsynced option can be added. |
|
8278 * |
|
8279 * @link https://github.com/WordPress/gutenberg/pull/51144 |
|
8280 */ |
|
8281 function wp_create_initial_post_meta() { |
|
8282 register_post_meta( |
|
8283 'wp_block', |
|
8284 'wp_pattern_sync_status', |
|
8285 array( |
|
8286 'sanitize_callback' => 'sanitize_text_field', |
|
8287 'single' => true, |
|
8288 'type' => 'string', |
|
8289 'show_in_rest' => array( |
|
8290 'schema' => array( |
|
8291 'type' => 'string', |
|
8292 'enum' => array( 'partial', 'unsynced' ), |
|
8293 ), |
|
8294 ), |
|
8295 ) |
|
8296 ); |
|
8297 } |