Ticket #36217: 36217.6.diff
File 36217.6.diff, 39.2 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/nav-menu.php
diff --git src/wp-admin/includes/nav-menu.php src/wp-admin/includes/nav-menu.php index 349b6db..f3f9540 100644
function wp_nav_menu_item_link_meta_box() { 292 292 * @global int $_nav_menu_placeholder 293 293 * @global int|string $nav_menu_selected_id 294 294 * 295 * @param string $objectNot used.296 * @param string$post_type The post type object.295 * @param string $object Not used. 296 * @param WP_Post_Type $post_type The post type object. 297 297 */ 298 298 function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) { 299 299 global $_nav_menu_placeholder, $nav_menu_selected_id; -
new file src/wp-includes/class-wp-post-type.php
diff --git src/wp-includes/class-wp-post-type.php src/wp-includes/class-wp-post-type.php new file mode 100644 index 0000000..211e49a
- + 1 <?php 2 /** 3 * Post API: WP_Post_Type class 4 * 5 * @package WordPress 6 * @subpackage Post 7 * @since 4.6.0 8 */ 9 10 /** 11 * Core class used for interacting with post types. 12 * 13 * @since 4.6.0 14 */ 15 final class WP_Post_Type { 16 /** 17 * Post type key. 18 * 19 * @since 4.6.0 20 * @access public 21 * @var string $name 22 */ 23 public $name; 24 25 /** 26 * Name of the post type shown in the menu. Usually plural. 27 * 28 * @since 4.6.0 29 * @access public 30 * @var string $label 31 */ 32 public $label; 33 34 /** 35 * An array of labels for this post type. 36 * 37 * If not set, post labels are inherited for non-hierarchical types 38 * and page labels for hierarchical ones. 39 * 40 * @see get_post_type_labels() 41 * 42 * @since 4.6.0 43 * @access public 44 * @var array $labels 45 */ 46 public $labels; 47 48 /** 49 * A short descriptive summary of what the post type is. 50 * 51 * Default empty. 52 * 53 * @since 4.6.0 54 * @access public 55 * @var string $description 56 */ 57 public $description = ''; 58 59 /** 60 * Whether a post type is intended for use publicly either via the admin interface or by front-end users. 61 * 62 * While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus 63 * are inherited from public, each does not rely on this relationship and controls a very specific intention. 64 * 65 * Default false. 66 * 67 * @since 4.6.0 68 * @access public 69 * @var bool $public 70 */ 71 public $public = false; 72 73 /** 74 * Whether the post type is hierarchical (e.g. page). 75 * 76 * Default false. 77 * 78 * @since 4.6.0 79 * @access public 80 * @var bool $hierarchical 81 */ 82 public $hierarchical = false; 83 84 /** 85 * Whether to exclude posts with this post type from front end search 86 * results. 87 * 88 * Default is the opposite value of $public. 89 * 90 * @since 4.6.0 91 * @access public 92 * @var bool $exclude_from_search 93 */ 94 public $exclude_from_search = null; 95 96 /** 97 * Whether queries can be performed on the front end for the post type as part of {@see parse_request()}. 98 * 99 * Endpoints would include: 100 * ?post_type={post_type_key} 101 * ?{post_type_key}={single_post_slug} 102 * ?{post_type_query_var}={single_post_slug} 103 * 104 * Default is the value of $public. 105 * 106 * @since 4.6.0 107 * @access public 108 * @var bool $publicly_queryable 109 */ 110 public $publicly_queryable = null; 111 112 /** 113 * Whether to generate and allow a UI for managing this post type in the admin. 114 * 115 * Default is the value of $public. 116 * 117 * @since 4.6.0 118 * @access public 119 * @var bool $show_ui 120 */ 121 public $show_ui = null; 122 123 /** 124 * Where to show the post type in the admin menu. 125 * 126 * To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is 127 * shown. If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type 128 * will be placed as a sub-menu of that. 129 * 130 * Default is the value of $show_ui. 131 * 132 * @since 4.6.0 133 * @access public 134 * @var bool $show_in_menu 135 */ 136 public $show_in_menu = null; 137 138 /** 139 * Makes this post type available for selection in navigation menus. 140 * 141 * Default is the value $public. 142 * 143 * @since 4.6.0 144 * @access public 145 * @var bool $show_in_nav_menus 146 */ 147 public $show_in_nav_menus = null; 148 149 /** 150 * Makes this post type available via the admin bar. 151 * 152 * Default is the value of $show_in_menu. 153 * 154 * @since 4.6.0 155 * @access public 156 * @var bool $show_in_admin_bar 157 */ 158 public $show_in_admin_bar = null; 159 160 /** 161 * The position in the menu order the post type should appear. 162 * 163 * To work, $show_in_menu must be true. Default null (at the bottom). 164 * 165 * @since 4.6.0 166 * @access public 167 * @var int $menu_position 168 */ 169 public $menu_position = null; 170 171 /** 172 * The url to the icon to be used for this menu. 173 * 174 * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. 175 * This should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class to use a font 176 * icon, e.g. 177 * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. 178 * 179 * Defaults to use the posts icon. 180 * 181 * @since 4.6.0 182 * @access public 183 * @var string $menu_icon 184 */ 185 public $menu_icon = null; 186 187 /** 188 * The string to use to build the read, edit, and delete capabilities. 189 * 190 * May be passed as an array to allow for alternative plurals when using 191 * this argument as a base to construct the capabilities, e.g. 192 * array('story', 'stories'). Default 'post'. 193 * 194 * @since 4.6.0 195 * @access public 196 * @var string $capability_type 197 */ 198 public $capability_type = 'post'; 199 200 /** 201 * Whether to use the internal default meta capability handling. 202 * 203 * Default false. 204 * 205 * @since 4.6.0 206 * @access public 207 * @var bool $map_meta_cap 208 */ 209 public $map_meta_cap = false; 210 211 /** 212 * Provide a callback function that sets up the meta boxes for the edit form. 213 * 214 * Do remove_meta_box() and add_meta_box() calls in the callback. Default null. 215 * 216 * @since 4.6.0 217 * @access public 218 * @var string $register_meta_box_cb 219 */ 220 public $register_meta_box_cb = null; 221 222 /** 223 * An array of taxonomy identifiers that will be registered for the post type. 224 * 225 * Taxonomies can be registered later with {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}. 226 * 227 * Default empty array. 228 * 229 * @since 4.6.0 230 * @access public 231 * @var array $taxonomies 232 */ 233 public $taxonomies = array(); 234 235 /** 236 * Whether there should be post type archives, or if a string, the archive slug to use. 237 * 238 * Will generate the proper rewrite rules if $rewrite is enabled. Default false. 239 * 240 * @since 4.6.0 241 * @access public 242 * @var bool|string $has_archive 243 */ 244 public $has_archive = false; 245 246 /** 247 * Sets the query_var key for this post type. 248 * 249 * Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. 250 * If specified as a string, the query ?{query_var_string}={post_slug} will be valid. 251 * 252 * @since 4.6.0 253 * @access public 254 * @var string|bool $query_var 255 */ 256 public $query_var; 257 258 /** 259 * Whether to allow this post type to be exported. 260 * 261 * Default true. 262 * 263 * @since 4.6.0 264 * @access public 265 * @var bool $can_export 266 */ 267 public $can_export = true; 268 269 /** 270 * Whether to delete posts of this type when deleting a user. 271 * 272 * If true, posts of this type belonging to the user will be moved to trash when then user is deleted. 273 * If false, posts of this type belonging to the user will *not* be trashed or deleted. 274 * If not set (the default), posts are trashed if post_type_supports('author'). 275 * Otherwise posts are not trashed or deleted. Default null. 276 * 277 * @since 4.6.0 278 * @access public 279 * @var bool $delete_with_user 280 */ 281 public $delete_with_user = null; 282 283 /** 284 * Whether this post type is a native or "built-in" post_type. 285 * 286 * Default false. 287 * 288 * @since 4.6.0 289 * @access public 290 * @var bool $_builtin 291 */ 292 public $_builtin = false; 293 294 /** 295 * URL segment to use for edit link of this post type. 296 * 297 * Default 'post.php?post=%d' 298 * 299 * @since 4.6.0 300 * @access public 301 * @var string $_edit_link 302 */ 303 public $_edit_link = 'post.php?post=%d'; 304 305 306 /** 307 * Post type capabilities. 308 * 309 * @since 4.6.0 310 * @access public 311 * @var array $cap 312 */ 313 public $cap; 314 315 /** 316 * Triggers the handling of rewrites for this post type. 317 * 318 * Defaults to true, using $post_type as slug 319 * 320 * @since 4.6.0 321 * @access public 322 * @var array|false $rewrite 323 */ 324 public $rewrite; 325 326 /** 327 * The features supported by the post type. 328 * 329 * @since 4.6.0 330 * @access public 331 * @var array|bool $supports 332 */ 333 public $supports; 334 335 /** 336 * Constructor. 337 * 338 * Will populate object properties from the provided arguments and assign other 339 * default properties based on that information. 340 * 341 * @since 4.6.0 342 * @access public 343 * 344 * @see register_post_type() 345 * 346 * @param string $post_type Post type key. 347 * @param array|string $args Optional. Array or string of arguments for registering a post type. 348 * Default empty array. 349 */ 350 public function __construct( $post_type, $args = array() ) { 351 $this->name = $post_type; 352 353 $this->set_props( $args ); 354 } 355 356 /** 357 * Sets post type properties. 358 * 359 * @since 4.6.0 360 * @access public 361 * 362 * @param array|string $args Array or string of arguments for registering a post type. 363 */ 364 public function set_props( $args ) { 365 $args = wp_parse_args( $args ); 366 367 /** 368 * Filter the arguments for registering a post type. 369 * 370 * @since 4.4.0 371 * 372 * @param array $args Array of arguments for registering a post type. 373 * @param string $post_type Post type key. 374 */ 375 $args = apply_filters( 'register_post_type_args', $args, $this->name ); 376 377 $has_edit_link = ! empty( $args['_edit_link'] ); 378 379 // Args prefixed with an underscore are reserved for internal use. 380 $defaults = array( 381 'labels' => array(), 382 'description' => '', 383 'public' => false, 384 'hierarchical' => false, 385 'exclude_from_search' => null, 386 'publicly_queryable' => null, 387 'show_ui' => null, 388 'show_in_menu' => null, 389 'show_in_nav_menus' => null, 390 'show_in_admin_bar' => null, 391 'menu_position' => null, 392 'menu_icon' => null, 393 'capability_type' => 'post', 394 'capabilities' => array(), 395 'map_meta_cap' => null, 396 'supports' => array(), 397 'register_meta_box_cb' => null, 398 'taxonomies' => array(), 399 'has_archive' => false, 400 'rewrite' => true, 401 'query_var' => true, 402 'can_export' => true, 403 'delete_with_user' => null, 404 '_builtin' => false, 405 '_edit_link' => 'post.php?post=%d', 406 ); 407 408 $args = array_merge( $defaults, $args ); 409 410 $args['name'] = $this->name; 411 412 // If not set, default to the setting for public. 413 if ( null === $args['publicly_queryable'] ) { 414 $args['publicly_queryable'] = $args['public']; 415 } 416 417 // If not set, default to the setting for public. 418 if ( null === $args['show_ui'] ) { 419 $args['show_ui'] = $args['public']; 420 } 421 422 // If not set, default to the setting for show_ui. 423 if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { 424 $args['show_in_menu'] = $args['show_ui']; 425 } 426 427 // If not set, default to the whether the full UI is shown. 428 if ( null === $args['show_in_admin_bar'] ) { 429 $args['show_in_admin_bar'] = (bool) $args['show_in_menu']; 430 } 431 432 // If not set, default to the setting for public. 433 if ( null === $args['show_in_nav_menus'] ) { 434 $args['show_in_nav_menus'] = $args['public']; 435 } 436 437 // If not set, default to true if not public, false if public. 438 if ( null === $args['exclude_from_search'] ) { 439 $args['exclude_from_search'] = ! $args['public']; 440 } 441 442 // Back compat with quirky handling in version 3.0. #14122. 443 if ( empty( $args['capabilities'] ) && null === $args['map_meta_cap'] && in_array( $args['capability_type'], array( 444 'post', 445 'page', 446 ) ) 447 ) { 448 $args['map_meta_cap'] = true; 449 } 450 451 // If not set, default to false. 452 if ( null === $args['map_meta_cap'] ) { 453 $args['map_meta_cap'] = false; 454 } 455 456 // If there's no specified edit link and no UI, remove the edit link. 457 if ( ! $args['show_ui'] && ! $has_edit_link ) { 458 $args['_edit_link'] = ''; 459 } 460 461 $this->cap = get_post_type_capabilities( (object) $args ); 462 unset( $args['capabilities'] ); 463 464 if ( is_array( $args['capability_type'] ) ) { 465 $args['capability_type'] = $args['capability_type'][0]; 466 } 467 468 if ( false !== $args['query_var'] ) { 469 if ( true === $args['query_var'] ) { 470 $args['query_var'] = $this->name; 471 } else { 472 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); 473 } 474 } 475 476 if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 477 if ( ! is_array( $args['rewrite'] ) ) { 478 $args['rewrite'] = array(); 479 } 480 if ( empty( $args['rewrite']['slug'] ) ) { 481 $args['rewrite']['slug'] = $this->name; 482 } 483 if ( ! isset( $args['rewrite']['with_front'] ) ) { 484 $args['rewrite']['with_front'] = true; 485 } 486 if ( ! isset( $args['rewrite']['pages'] ) ) { 487 $args['rewrite']['pages'] = true; 488 } 489 if ( ! isset( $args['rewrite']['feeds'] ) || ! $args['has_archive'] ) { 490 $args['rewrite']['feeds'] = (bool) $args['has_archive']; 491 } 492 if ( ! isset( $args['rewrite']['ep_mask'] ) ) { 493 if ( isset( $args['permalink_epmask'] ) ) { 494 $args['rewrite']['ep_mask'] = $args['permalink_epmask']; 495 } else { 496 $args['rewrite']['ep_mask'] = EP_PERMALINK; 497 } 498 } 499 } 500 501 foreach ( $args as $property_name => $property_value ) { 502 $this->$property_name = $property_value; 503 } 504 505 $this->labels = get_post_type_labels( $this ); 506 $this->label = $this->labels->name; 507 } 508 509 /** 510 * Sets the features support for the post type. 511 * 512 * @since 4.6.0 513 * @access public 514 */ 515 public function add_supports() { 516 if ( ! empty( $this->supports ) ) { 517 add_post_type_support( $this->name, $this->supports ); 518 unset( $this->supports ); 519 } elseif ( false !== $this->supports ) { 520 // Add default features 521 add_post_type_support( $this->name, array( 'title', 'editor' ) ); 522 } 523 } 524 525 /** 526 * Adds the necessary rewrite rules for the post type. 527 * 528 * @since 4.6.0 529 * @access public 530 * 531 * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component. 532 * @global WP $wp Current WordPress environment instance. 533 */ 534 public function add_rewrite_rules() { 535 global $wp_rewrite, $wp; 536 537 if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) { 538 $wp->add_query_var( $this->query_var ); 539 } 540 541 if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 542 if ( $this->hierarchical ) { 543 add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" ); 544 } else { 545 add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" ); 546 } 547 548 if ( $this->has_archive ) { 549 $archive_slug = $this->has_archive === true ? $this->rewrite['slug'] : $this->has_archive; 550 if ( $this->rewrite['with_front'] ) { 551 $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; 552 } else { 553 $archive_slug = $wp_rewrite->root . $archive_slug; 554 } 555 556 add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' ); 557 if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) { 558 $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; 559 add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); 560 add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); 561 } 562 if ( $this->rewrite['pages'] ) { 563 add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' ); 564 } 565 } 566 567 $permastruct_args = $this->rewrite; 568 $permastruct_args['feed'] = $permastruct_args['feeds']; 569 add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args ); 570 } 571 } 572 573 /** 574 * Registers the post type meta box if a custom callback was specified. 575 * 576 * @since 4.6.0 577 * @access public 578 */ 579 public function register_meta_boxes() { 580 if ( $this->register_meta_box_cb ) { 581 add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 ); 582 } 583 } 584 585 /** 586 * Adds the future post hook action for the post type. 587 * 588 * @since 4.6.0 589 * @access public 590 */ 591 public function add_hooks() { 592 add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 ); 593 } 594 595 /** 596 * Registers the taxonomies for the post type. 597 * 598 * @since 4.6.0 599 * @access public 600 */ 601 public function register_taxonomies() { 602 foreach ( $this->taxonomies as $taxonomy ) { 603 register_taxonomy_for_object_type( $taxonomy, $this->name ); 604 } 605 } 606 607 /** 608 * Removes the features support for the post type. 609 * 610 * @since 4.6.0 611 * @access public 612 * 613 * @global array $_wp_post_type_features Post type features. 614 */ 615 public function remove_supports() { 616 global $_wp_post_type_features; 617 618 unset( $_wp_post_type_features[ $this->name ] ); 619 } 620 621 /** 622 * Removes any rewrite rules, permastructs, and rules for the post type. 623 * 624 * @since 4.6.0 625 * @access public 626 * 627 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 628 * @global WP $wp Current WordPress environment instance. 629 * @global array $post_type_meta_caps Used to remove meta capabilities. 630 */ 631 public function remove_rewrite_rules() { 632 global $wp, $wp_rewrite, $post_type_meta_caps; 633 634 // Remove query var. 635 if ( false !== $this->query_var ) { 636 $wp->remove_query_var( $this->query_var ); 637 } 638 639 // Remove any rewrite rules, permastructs, and rules. 640 if ( false !== $this->rewrite ) { 641 remove_rewrite_tag( "%$this->name%" ); 642 remove_permastruct( $this->name ); 643 foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { 644 if ( false !== strpos( $query, "index.php?post_type=$this->name" ) ) { 645 unset( $wp_rewrite->extra_rules_top[ $regex ] ); 646 } 647 } 648 } 649 650 // Remove registered custom meta capabilities. 651 foreach ( $this->cap as $cap ) { 652 unset( $post_type_meta_caps[ $cap ] ); 653 } 654 } 655 656 /** 657 * Unregisters the post type meta box if a custom callback was specified. 658 * 659 * @since 4.6.0 660 * @access public 661 */ 662 public function unregister_meta_boxes() { 663 if ( $this->register_meta_box_cb ) { 664 remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 ); 665 } 666 } 667 668 /** 669 * Removes the post type from all taxonomies. 670 * 671 * @since 4.6.0 672 * @access public 673 */ 674 public function unregister_taxonomies() { 675 foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) { 676 unregister_taxonomy_for_object_type( $taxonomy, $this->name ); 677 } 678 } 679 680 /** 681 * Removes the future post hook action for the post type. 682 * 683 * @since 4.6.0 684 * @access public 685 */ 686 public function remove_hooks() { 687 remove_action( 'future_' . $this->name, '_future_post_hook', 5 ); 688 } 689 } -
src/wp-includes/class-wp-xmlrpc-server.php
diff --git src/wp-includes/class-wp-xmlrpc-server.php src/wp-includes/class-wp-xmlrpc-server.php index bdabf98..dff68a6 100644
class wp_xmlrpc_server extends IXR_Server { 883 883 * 884 884 * @access protected 885 885 * 886 * @param object$post_type Post type object.887 * @param array $fields The subset of post fields to return.886 * @param WP_Post_Type $post_type Post type object. 887 * @param array $fields The subset of post fields to return. 888 888 * @return array The prepared post type data. 889 889 */ 890 890 protected function _prepare_post_type( $post_type, $fields ) { … … class wp_xmlrpc_server extends IXR_Server { 922 922 * 923 923 * @since 3.4.0 924 924 * 925 * @param array $_post_type An array of post type data.926 * @param object$post_type Post type object.925 * @param array $_post_type An array of post type data. 926 * @param WP_Post_Type $post_type Post type object. 927 927 */ 928 928 return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type ); 929 929 } -
src/wp-includes/post.php
diff --git src/wp-includes/post.php src/wp-includes/post.php index 606d266..6db1ddb 100644
function get_post_type( $post = null ) { 834 834 } 835 835 836 836 /** 837 * Retrieve a post type object by name.837 * Retrieves a post type object by name. 838 838 * 839 839 * @since 3.0.0 840 840 * … … function get_post_type( $post = null ) { 843 843 * @see register_post_type() 844 844 * 845 845 * @param string $post_type The name of a registered post type. 846 * @return object|null A post type object.846 * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. 847 847 */ 848 848 function get_post_type_object( $post_type ) { 849 849 global $wp_post_types; … … function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) 893 893 * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing screen and post editing screen. 894 894 * 895 895 * @global array $wp_post_types List of post types. 896 * @global WP_Rewrite $wp_rewrite Used for default feeds.897 * @global WP $wp Used to add query vars.898 896 * 899 897 * @param string $post_type Post type key, must not exceed 20 characters. 900 898 * @param array|string $args { … … function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) 994 992 * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of 995 993 * this post type. Default 'post.php?post=%d'. 996 994 * } 997 * @return object|WP_Error The registered post type object, or an error object.995 * @return WP_Post_Type|WP_Error The registered post type object, or an error object. 998 996 */ 999 997 function register_post_type( $post_type, $args = array() ) { 1000 global $wp_post_types , $wp_rewrite, $wp;998 global $wp_post_types; 1001 999 1002 1000 if ( ! is_array( $wp_post_types ) ) { 1003 1001 $wp_post_types = array(); … … function register_post_type( $post_type, $args = array() ) { 1005 1003 1006 1004 // Sanitize post type name 1007 1005 $post_type = sanitize_key( $post_type ); 1008 $args = wp_parse_args( $args );1009 1010 /**1011 * Filters the arguments for registering a post type.1012 *1013 * @since 4.4.01014 *1015 * @param array $args Array of arguments for registering a post type.1016 * @param string $post_type Post type key.1017 */1018 $args = apply_filters( 'register_post_type_args', $args, $post_type );1019 1020 $has_edit_link = ! empty( $args['_edit_link'] );1021 1022 // Args prefixed with an underscore are reserved for internal use.1023 $defaults = array(1024 'labels' => array(),1025 'description' => '',1026 'public' => false,1027 'hierarchical' => false,1028 'exclude_from_search' => null,1029 'publicly_queryable' => null,1030 'show_ui' => null,1031 'show_in_menu' => null,1032 'show_in_nav_menus' => null,1033 'show_in_admin_bar' => null,1034 'menu_position' => null,1035 'menu_icon' => null,1036 'capability_type' => 'post',1037 'capabilities' => array(),1038 'map_meta_cap' => null,1039 'supports' => array(),1040 'register_meta_box_cb' => null,1041 'taxonomies' => array(),1042 'has_archive' => false,1043 'rewrite' => true,1044 'query_var' => true,1045 'can_export' => true,1046 'delete_with_user' => null,1047 '_builtin' => false,1048 '_edit_link' => 'post.php?post=%d',1049 );1050 $args = array_merge( $defaults, $args );1051 $args = (object) $args;1052 1053 $args->name = $post_type;1054 1006 1055 1007 if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { 1056 1008 _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2' ); 1057 1009 return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); 1058 1010 } 1059 1011 1060 // If not set, default to the setting for public. 1061 if ( null === $args->publicly_queryable ) 1062 $args->publicly_queryable = $args->public; 1063 1064 // If not set, default to the setting for public. 1065 if ( null === $args->show_ui ) 1066 $args->show_ui = $args->public; 1067 1068 // If not set, default to the setting for show_ui. 1069 if ( null === $args->show_in_menu || ! $args->show_ui ) 1070 $args->show_in_menu = $args->show_ui; 1071 1072 // If not set, default to the whether the full UI is shown. 1073 if ( null === $args->show_in_admin_bar ) 1074 $args->show_in_admin_bar = (bool) $args->show_in_menu; 1075 1076 // If not set, default to the setting for public. 1077 if ( null === $args->show_in_nav_menus ) 1078 $args->show_in_nav_menus = $args->public; 1079 1080 // If not set, default to true if not public, false if public. 1081 if ( null === $args->exclude_from_search ) 1082 $args->exclude_from_search = !$args->public; 1083 1084 // Back compat with quirky handling in version 3.0. #14122. 1085 if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) ) 1086 $args->map_meta_cap = true; 1087 1088 // If not set, default to false. 1089 if ( null === $args->map_meta_cap ) 1090 $args->map_meta_cap = false; 1091 1092 // If there's no specified edit link and no UI, remove the edit link. 1093 if ( ! $args->show_ui && ! $has_edit_link ) { 1094 $args->_edit_link = ''; 1095 } 1096 1097 $args->cap = get_post_type_capabilities( $args ); 1098 unset( $args->capabilities ); 1099 1100 if ( is_array( $args->capability_type ) ) 1101 $args->capability_type = $args->capability_type[0]; 1102 1103 if ( ! empty( $args->supports ) ) { 1104 add_post_type_support( $post_type, $args->supports ); 1105 unset( $args->supports ); 1106 } elseif ( false !== $args->supports ) { 1107 // Add default features 1108 add_post_type_support( $post_type, array( 'title', 'editor' ) ); 1109 } 1110 1111 if ( false !== $args->query_var ) { 1112 if ( true === $args->query_var ) 1113 $args->query_var = $post_type; 1114 else 1115 $args->query_var = sanitize_title_with_dashes( $args->query_var ); 1116 1117 if ( $wp && is_post_type_viewable( $args ) ) { 1118 $wp->add_query_var( $args->query_var ); 1119 } 1120 } 1121 1122 if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 1123 if ( ! is_array( $args->rewrite ) ) 1124 $args->rewrite = array(); 1125 if ( empty( $args->rewrite['slug'] ) ) 1126 $args->rewrite['slug'] = $post_type; 1127 if ( ! isset( $args->rewrite['with_front'] ) ) 1128 $args->rewrite['with_front'] = true; 1129 if ( ! isset( $args->rewrite['pages'] ) ) 1130 $args->rewrite['pages'] = true; 1131 if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive ) 1132 $args->rewrite['feeds'] = (bool) $args->has_archive; 1133 if ( ! isset( $args->rewrite['ep_mask'] ) ) { 1134 if ( isset( $args->permalink_epmask ) ) 1135 $args->rewrite['ep_mask'] = $args->permalink_epmask; 1136 else 1137 $args->rewrite['ep_mask'] = EP_PERMALINK; 1138 } 1139 1140 if ( $args->hierarchical ) 1141 add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" ); 1142 else 1143 add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" ); 1144 1145 if ( $args->has_archive ) { 1146 $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; 1147 if ( $args->rewrite['with_front'] ) 1148 $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; 1149 else 1150 $archive_slug = $wp_rewrite->root . $archive_slug; 1151 1152 add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' ); 1153 if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) { 1154 $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; 1155 add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); 1156 add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' ); 1157 } 1158 if ( $args->rewrite['pages'] ) 1159 add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' ); 1160 } 1161 1162 $permastruct_args = $args->rewrite; 1163 $permastruct_args['feed'] = $permastruct_args['feeds']; 1164 add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args ); 1165 } 1166 1167 // Register the post type meta box if a custom callback was specified. 1168 if ( $args->register_meta_box_cb ) 1169 add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 ); 1012 $post_type_object = new WP_Post_Type( $post_type, $args ); 1013 $post_type_object->add_supports(); 1014 $post_type_object->add_rewrite_rules(); 1015 $post_type_object->register_meta_boxes(); 1170 1016 1171 $args->labels = get_post_type_labels( $args ); 1172 $args->label = $args->labels->name; 1017 $wp_post_types[ $post_type ] = $post_type_object; 1173 1018 1174 $wp_post_types[ $post_type ] = $args; 1175 1176 add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 ); 1177 1178 foreach ( $args->taxonomies as $taxonomy ) { 1179 register_taxonomy_for_object_type( $taxonomy, $post_type ); 1180 } 1019 $post_type_object->add_hooks(); 1020 $post_type_object->register_taxonomies(); 1181 1021 1182 1022 /** 1183 1023 * Fires after a post type is registered. 1184 1024 * 1185 1025 * @since 3.3.0 1186 1026 * 1187 * @param string $post_typePost type.1188 * @param object $argsArguments used to register the post type.1027 * @param string $post_type Post type. 1028 * @param WP_Post_Type $post_type_object Arguments used to register the post type. 1189 1029 */ 1190 do_action( 'registered_post_type', $post_type, $ args);1030 do_action( 'registered_post_type', $post_type, $post_type_object ); 1191 1031 1192 return $ args;1032 return $post_type_object; 1193 1033 } 1194 1034 1195 1035 /** … … function register_post_type( $post_type, $args = array() ) { 1199 1039 * 1200 1040 * @since 4.5.0 1201 1041 * 1202 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1203 * @global WP $wp Current WordPress environment instance. 1204 * @global array $_wp_post_type_features Used to remove post type features. 1205 * @global array $post_type_meta_caps Used to remove meta capabilities. 1206 * @global array $wp_post_types List of post types. 1042 * @global array $wp_post_types List of post types. 1207 1043 * 1208 1044 * @param string $post_type Post type to unregister. 1209 1045 * @return bool|WP_Error True on success, WP_Error on failure or if the post type doesn't exist. 1210 1046 */ 1211 1047 function unregister_post_type( $post_type ) { 1048 global $wp_post_types; 1049 1212 1050 if ( ! post_type_exists( $post_type ) ) { 1213 1051 return new WP_Error( 'invalid_post_type', __( 'Invalid post type' ) ); 1214 1052 } 1215 1053 1216 $post_type_ args= get_post_type_object( $post_type );1054 $post_type_object = get_post_type_object( $post_type ); 1217 1055 1218 1056 // Do not allow unregistering internal post types. 1219 if ( $post_type_ args->_builtin ) {1057 if ( $post_type_object->_builtin ) { 1220 1058 return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); 1221 1059 } 1222 1060 1223 global $wp, $wp_rewrite, $_wp_post_type_features, $post_type_meta_caps, $wp_post_types; 1224 1225 // Remove query var. 1226 if ( false !== $post_type_args->query_var ) { 1227 $wp->remove_query_var( $post_type_args->query_var ); 1228 } 1229 1230 // Remove any rewrite rules, permastructs, and rules. 1231 if ( false !== $post_type_args->rewrite ) { 1232 remove_rewrite_tag( "%$post_type%" ); 1233 remove_permastruct( $post_type ); 1234 foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { 1235 if ( false !== strpos( $query, "index.php?post_type=$post_type" ) ) { 1236 unset( $wp_rewrite->extra_rules_top[ $regex ] ); 1237 } 1238 } 1239 } 1240 1241 // Remove registered custom meta capabilities. 1242 foreach ( $post_type_args->cap as $cap ) { 1243 unset( $post_type_meta_caps[ $cap ] ); 1244 } 1245 1246 // Remove all post type support. 1247 unset( $_wp_post_type_features[ $post_type ] ); 1248 1249 // Unregister the post type meta box if a custom callback was specified. 1250 if ( $post_type_args->register_meta_box_cb ) { 1251 remove_action( 'add_meta_boxes_' . $post_type, $post_type_args->register_meta_box_cb ); 1252 } 1253 1254 // Remove the post type from all taxonomies. 1255 foreach ( get_object_taxonomies( $post_type ) as $taxonomy ) { 1256 unregister_taxonomy_for_object_type( $taxonomy, $post_type ); 1257 } 1258 1259 // Remove the future post hook action. 1260 remove_action( 'future_' . $post_type, '_future_post_hook', 5 ); 1061 $post_type_object->remove_supports(); 1062 $post_type_object->remove_rewrite_rules(); 1063 $post_type_object->unregister_meta_boxes(); 1064 $post_type_object->remove_hooks(); 1065 $post_type_object->unregister_taxonomies(); 1261 1066 1262 // Remove the post type.1263 1067 unset( $wp_post_types[ $post_type ] ); 1264 1068 1265 1069 /** … … function _post_type_meta_capabilities( $capabilities = null ) { 1440 1244 * 1441 1245 * @access private 1442 1246 * 1443 * @param object $post_type_object Post type object.1247 * @param object|WP_Post_Type $post_type_object Custom-something object or WP_Post_Type object. 1444 1248 * @return object object with all the labels as member variables. 1445 1249 */ 1446 1250 function get_post_type_labels( $post_type_object ) { -
src/wp-settings.php
diff --git src/wp-settings.php src/wp-settings.php index ae7a173..7d69cf7 100644
require( ABSPATH . WPINC . '/author-template.php' ); 157 157 require( ABSPATH . WPINC . '/post.php' ); 158 158 require( ABSPATH . WPINC . '/class-walker-page.php' ); 159 159 require( ABSPATH . WPINC . '/class-walker-page-dropdown.php' ); 160 require( ABSPATH . WPINC . '/class-wp-post-type.php' ); 160 161 require( ABSPATH . WPINC . '/class-wp-post.php' ); 161 162 require( ABSPATH . WPINC . '/post-template.php' ); 162 163 require( ABSPATH . WPINC . '/revision.php' ); -
tests/phpunit/tests/post/types.php
diff --git tests/phpunit/tests/post/types.php tests/phpunit/tests/post/types.php index d203b74..489266f 100644
class Tests_Post_Types extends WP_UnitTestCase { 29 29 register_post_type( 'foo' ); 30 30 31 31 $pobj = get_post_type_object( 'foo' ); 32 $this->assertInstanceOf( ' stdClass', $pobj );32 $this->assertInstanceOf( 'WP_Post_Type', $pobj ); 33 33 $this->assertEquals( 'foo', $pobj->name ); 34 34 35 35 // Test some defaults … … class Tests_Post_Types extends WP_UnitTestCase { 554 554 * @since 4.5.0 555 555 * 556 556 * @param array $args register_post_type() arguments. 557 * @return stdClassPost type object for `$this->post_type`.557 * @return WP_Post_Type Post type object for `$this->post_type`. 558 558 */ 559 559 public function register_post_type( $args = array() ) { 560 560 register_post_type( $this->post_type, $args ); -
new file tests/phpunit/tests/post/wpPostType.php
diff --git tests/phpunit/tests/post/wpPostType.php tests/phpunit/tests/post/wpPostType.php new file mode 100644 index 0000000..4d4602b
- + 1 <?php 2 3 /** 4 * @group post 5 */ 6 class Tests_WP_Post_Type extends WP_UnitTestCase { 7 public function test_instances() { 8 global $wp_post_types; 9 10 foreach ( $wp_post_types as $post_type ) { 11 $this->assertInstanceOf( 'WP_Post_Type', $post_type ); 12 } 13 } 14 15 public function test_add_supports_defaults() { 16 $post_type = rand_str(); 17 $post_type_object = new WP_Post_Type( $post_type ); 18 19 $post_type_object->add_supports(); 20 $post_type_supports = get_all_post_type_supports( $post_type ); 21 22 $post_type_object->remove_supports(); 23 $post_type_supports_after = get_all_post_type_supports( $post_type ); 24 25 $this->assertEqualSets( array( 'title' => true, 'editor' => true ), $post_type_supports ); 26 $this->assertEqualSets( array(), $post_type_supports_after ); 27 } 28 29 public function test_add_supports_custom() { 30 $post_type = rand_str(); 31 $post_type_object = new WP_Post_Type( $post_type, array( 32 'supports' => array( 33 'editor', 34 'comments', 35 'revisions', 36 ), 37 ) ); 38 39 $post_type_object->add_supports(); 40 $post_type_supports = get_all_post_type_supports( $post_type ); 41 42 $post_type_object->remove_supports(); 43 $post_type_supports_after = get_all_post_type_supports( $post_type ); 44 45 $this->assertEqualSets( array( 46 'editor' => true, 47 'comments' => true, 48 'revisions' => true, 49 ), $post_type_supports ); 50 $this->assertEqualSets( array(), $post_type_supports_after ); 51 } 52 53 public function test_does_not_add_query_var_if_not_public() { 54 $this->set_permalink_structure( '/%postname%' ); 55 56 /* @var WP $wp */ 57 global $wp; 58 59 $post_type = rand_str(); 60 $post_type_object = new WP_Post_Type( $post_type, array( 'rewrite' => false, 'query_var' => 'foobar' ) ); 61 $post_type_object->add_rewrite_rules(); 62 63 $this->assertFalse( in_array( 'foobar', $wp->public_query_vars ) ); 64 } 65 66 public function test_adds_query_var_if_public() { 67 $this->set_permalink_structure( '/%postname%' ); 68 69 /* @var WP $wp */ 70 global $wp; 71 72 $post_type = rand_str(); 73 $post_type_object = new WP_Post_Type( $post_type, array( 74 'public' => true, 75 'rewrite' => false, 76 'query_var' => 'foobar', 77 ) ); 78 79 $post_type_object->add_rewrite_rules(); 80 $in_array = in_array( 'foobar', $wp->public_query_vars ); 81 82 $post_type_object->remove_rewrite_rules(); 83 $in_array_after = in_array( 'foobar', $wp->public_query_vars ); 84 85 $this->assertTrue( $in_array ); 86 $this->assertFalse( $in_array_after ); 87 } 88 89 public function test_adds_rewrite_rules() { 90 $this->set_permalink_structure( '/%postname%' ); 91 92 /* @var WP_Rewrite $wp_rewrite */ 93 global $wp_rewrite; 94 95 $post_type = rand_str(); 96 $post_type_object = new WP_Post_Type( $post_type, array( 'public' => true, 'rewrite' => true ) ); 97 98 $post_type_object->add_rewrite_rules(); 99 $rewrite_tags = $wp_rewrite->rewritecode; 100 101 $post_type_object->remove_rewrite_rules(); 102 $rewrite_tags_after = $wp_rewrite->rewritecode; 103 104 $this->assertTrue( false !== array_search( "%$post_type%", $rewrite_tags ) ); 105 $this->assertFalse( array_search( "%$post_type%", $rewrite_tags_after ) ); 106 } 107 108 public function test_register_meta_boxes() { 109 $post_type = rand_str(); 110 $post_type_object = new WP_Post_Type( $post_type, array( 'register_meta_box_cb' => '__return_false' ) ); 111 112 $post_type_object->register_meta_boxes(); 113 $has_action = has_action( "add_meta_boxes_$post_type", '__return_false' ); 114 $post_type_object->unregister_meta_boxes(); 115 $has_action_after = has_action( "add_meta_boxes_$post_type", '__return_false' ); 116 117 $this->assertSame( 10, $has_action ); 118 $this->assertFalse( $has_action_after ); 119 } 120 121 public function test_adds_future_post_hook() { 122 $post_type = rand_str(); 123 $post_type_object = new WP_Post_Type( $post_type ); 124 $post_type_object->add_hooks(); 125 $has_action = has_action( "future_$post_type", '_future_post_hook' ); 126 $post_type_object->remove_hooks(); 127 $has_action_after = has_action( "future_$post_type", '_future_post_hook' ); 128 129 $this->assertSame( 5, $has_action ); 130 $this->assertFalse( $has_action_after ); 131 } 132 133 public function test_register_taxonomies() { 134 global $wp_post_types; 135 136 $post_type = rand_str(); 137 $post_type_object = new WP_Post_Type( $post_type, array( 'taxonomies' => array( 'post_tag' ) ) ); 138 139 $wp_post_types[ $post_type ] = $post_type_object; 140 141 $post_type_object->register_taxonomies(); 142 $taxonomies = get_object_taxonomies( $post_type ); 143 $post_type_object->unregister_taxonomies(); 144 $taxonomies_after = get_object_taxonomies( $post_type ); 145 146 unset( $wp_post_types[ $post_type ] ); 147 148 $this->assertEqualSets( array( 'post_tag' ), $taxonomies ); 149 $this->assertEqualSets( array(), $taxonomies_after ); 150 } 151 }