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


Generated : Wed Nov 11 08:20:02 2026 Cross-referenced by PHPXref