[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/build/scripts/core-fields/ -> index.php (source)

   1  <?php
   2  /**
   3   * Registers the fields of WordPress core through the Entity fields API, like a
   4   * plugin registers its own.
   5   *
   6   * The default fields every post type derives from its supports are
   7   * registered in code, see wp_register_core_post_type_supports_fields(). Their
   8   * definitions are the `field.php` files of the `post_type_supports` folder. The
   9   * post types whose fields differ from the defaults unregister the ones they
  10   * do not get, as a plugin would, see wp_register_core_field_collections().
  11   *
  12   * The fields of a single entity, a post type or the site, are declarative
  13   * collections, the other folders next to this file, see
  14   * wp_register_entity_field_collection() for their format.
  15   *
  16   * @package WordPress
  17   */
  18  
  19  /**
  20   * Registers the default fields of each post type exposed in the REST API,
  21   * from what it supports, or whatever it supports:
  22   *
  23   * - `author`, for the post types supporting `author`.
  24   * - `comment_status`, for the post types supporting `comments`.
  25   * - `date`, for every post type but the design ones.
  26   * - `discussion`, for the post types supporting `comments` or `trackbacks`.
  27   * - `excerpt`, for the post types supporting `excerpt`.
  28   * - `featured_media`, for the post types supporting `thumbnail` when the
  29   *   theme supports post thumbnails for them.
  30   * - `format`, for the post types supporting `post-formats` when the theme
  31   *   supports post formats.
  32   * - `last_edited_date`, for every post type.
  33   * - `notesCount`, for the post types whose `editor` support has the `notes`
  34   *   argument.
  35   * - `parent`, for the post types supporting `page-attributes`.
  36   * - `password`, for every post type but the design ones.
  37   * - `ping_status`, for the post types supporting `trackbacks`.
  38   * - `post-content-info`, for the post types supporting `editor`.
  39   * - `scheduled_date`, for every post type but the design ones.
  40   * - `slug`, for the viewable post types but the design ones.
  41   * - `status`, for every post type but the design ones.
  42   * - `sticky`, for posts, the only post type with sticky posts.
  43   * - `template`, for every post type but the design ones.
  44   * - `title`, for the post types supporting `title`.
  45   *
  46   * It makes no exception for any post type: the fields a post type does not
  47   * get are unregistered afterwards, see wp_register_core_field_collections().
  48   *
  49   * Only some fields have JavaScript parts, but every field is registered
  50   * with the script module of the folder: a post type supporting only
  51   * comments or notes lists the module too, which is small.
  52   *
  53   * @since 7.2.0
  54   */
  55  function wp_register_core_post_type_supports_fields() {
  56      $definitions = wp_get_entity_field_collection_fields( __DIR__ . '/post_type_supports' );
  57  
  58      foreach ( get_post_types( array( 'show_in_rest' => true ) ) as $post_type ) {
  59          // WordPress stores the arguments of a support as a list of argument
  60          // arrays, and a support without arguments as `true`.
  61          $editor = get_all_post_type_supports( $post_type )['editor'] ?? null;
  62  
  63          $applies = array(
  64              'author'            => post_type_supports( $post_type, 'author' ),
  65              'comment_status'    => post_type_supports( $post_type, 'comments' ),
  66              'date'              => true,
  67              'discussion'        => post_type_supports( $post_type, 'comments' ) || post_type_supports( $post_type, 'trackbacks' ),
  68              'excerpt'           => post_type_supports( $post_type, 'excerpt' ),
  69              'featured_media'    => post_type_supports( $post_type, 'thumbnail' ) && current_theme_supports( 'post-thumbnails', $post_type ),
  70              'format'            => post_type_supports( $post_type, 'post-formats' ) && current_theme_supports( 'post-formats' ),
  71              'last_edited_date'  => true,
  72              'notesCount'        => is_array( $editor ) && (bool) array_filter( array_column( $editor, 'notes' ) ),
  73              'parent'            => post_type_supports( $post_type, 'page-attributes' ),
  74              'password'          => true,
  75              'ping_status'       => post_type_supports( $post_type, 'trackbacks' ),
  76              'post-content-info' => post_type_supports( $post_type, 'editor' ),
  77              'scheduled_date'    => true,
  78              'slug'              => is_post_type_viewable( $post_type ),
  79              'status'            => true,
  80              'sticky'            => 'post' === $post_type,
  81              'template'          => true,
  82              'title'             => post_type_supports( $post_type, 'title' ),
  83          );
  84  
  85          // Keeps the alphabetical order of the folders.
  86          $fields = array_values( array_intersect_key( $definitions, array_filter( $applies ) ) );
  87          if ( $fields ) {
  88              wp_register_entity_fields( 'core', 'postType', $post_type, $fields, '@wordpress/core-fields/post_type_supports' );
  89          }
  90      }
  91  }
  92  
  93  /**
  94   * Registers the fields of WordPress core: the defaults first, without the
  95   * ones the core post types do not get, then the collections of single post
  96   * types, then the collection of the site.
  97   *
  98   * Hooked at priority 0, so a plugin hooking `wp_entity_fields_api_init` at the
  99   * default priority sees the core fields registered, and can update or
 100   * unregister them.
 101   *
 102   * @since 7.2.0
 103   */
 104  function wp_register_core_field_collections() {
 105      // Defaults registered for all post types based on their supports.
 106      wp_register_core_post_type_supports_fields();
 107  
 108      // page: unregister unwanted default fields, and register its own.
 109      wp_unregister_entity_fields( 'postType', 'page', array( 'title' ) );
 110      wp_register_entity_field_collection( __DIR__ . '/page' );
 111  
 112      // wp_template: unregister unwanted default fields, and register its own.
 113      wp_unregister_entity_fields( 'postType', 'wp_template', array( 'author', 'date', 'excerpt', 'password', 'post-content-info', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
 114      wp_register_entity_field_collection( __DIR__ . '/wp_template' );
 115  
 116      // wp_template_part: unregister unwanted default fields, and register its own.
 117      wp_unregister_entity_fields( 'postType', 'wp_template_part', array( 'author', 'date', 'excerpt', 'password', 'post-content-info', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
 118      wp_register_entity_field_collection( __DIR__ . '/wp_template_part' );
 119  
 120      // wp_block: unregister unwanted default fields, and register its own.
 121      wp_unregister_entity_fields( 'postType', 'wp_block', array( 'date', 'excerpt', 'password', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
 122      wp_register_entity_field_collection( __DIR__ . '/wp_block' );
 123      // Patterns are sorted client-side, where the author's ID isn't a useful
 124      // order, like the template authors.
 125      if ( post_type_supports( 'wp_block', 'author' ) ) {
 126          wp_update_entity_fields(
 127              'core',
 128              'postType',
 129              'wp_block',
 130              array(
 131                  array(
 132                      'id'            => 'author',
 133                      'enableSorting' => false,
 134                  ),
 135              )
 136          );
 137      }
 138  
 139      // attachment: unregister all default fields, and register its own.
 140      wp_unregister_entity_fields( 'postType', 'attachment' );
 141      wp_register_entity_field_collection( __DIR__ . '/attachment' );
 142  
 143      // wp_navigation: unregister unwanted default fields.
 144      wp_unregister_entity_fields( 'postType', 'wp_navigation', array( 'date', 'password', 'post-content-info', 'scheduled_date', 'slug', 'status', 'template' ) );
 145  
 146      // Register fields for root/site (entity/kind).
 147      wp_register_entity_field_collection( __DIR__ . '/root_site' );
 148  }
 149  add_action( 'wp_entity_fields_api_init', 'wp_register_core_field_collections', 0 );


Generated : Thu Oct 22 08:20:34 2026 Cross-referenced by PHPXref