[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Registers the actions of WordPress core through the entity actions API, like a
   4   * plugin registers its own.
   5   *
   6   * The folders next to this file are the script modules of the actions:
   7   * `post_type_supports` for the actions of every post type, `page` for the
   8   * actions of pages, `wp_block` for the actions of patterns, and
   9   * `wp_template_part` for the actions of template parts. Each action has a
  10   * folder named after its id, whose `action.php` returns the serializable
  11   * part of the action, and whose `action.tsx` holds its JavaScript parts.
  12   *
  13   * @package WordPress
  14   */
  15  
  16  /**
  17   * Registers the actions of WordPress core on each post type exposed in the
  18   * REST API, from what it supports and what the current user can do:
  19   *
  20   * - `view-post`, for the viewable post types.
  21   * - `view-post-revisions`, for the post types supporting `revisions`.
  22   * - `duplicate-post`, for the post types the user can create, except
  23   *   patterns, templates, and template parts. Only in the Gutenberg plugin.
  24   * - `duplicate-template-part`, for template parts, when the user can create
  25   *   them and the theme is a block theme.
  26   * - `duplicate-pattern`, for patterns, when the user can create them.
  27   * - `rename-post`, for the post types supporting `title`.
  28   * - `order-pages`, for the post types supporting `page-attributes`.
  29   * - `export-pattern`, for patterns.
  30   * - `restore`, `reset-post`, `delete-post`, `move-to-trash`, and
  31   *   `permanently-delete`, for every post type: each one checks which posts
  32   *   it applies to.
  33   * - `set-as-homepage` and `set-as-posts-page`, for pages, when the user can
  34   *   manage the site options and the theme has no `front-page` template:
  35   *   each one applies to the pages with its `action-*` REST link.
  36   *
  37   * The actions are registered in that order, which is the order they are
  38   * shown in, each with the script module of its folder. The registry is built
  39   * for each request, so the actions only depending on the user are registered
  40   * for the current one.
  41   *
  42   * Hooked at priority 0, so a plugin hooking `wp_entity_actions_api_init` at the
  43   * default priority sees the core actions registered, and can unregister or
  44   * replace them.
  45   *
  46   * @since 7.2.0
  47   */
  48  function register_core_entity_actions() {
  49      $is_gutenberg   = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN;
  50      $is_block_theme = wp_is_block_theme();
  51      $design_types   = array( 'wp_block', 'wp_template', 'wp_template_part' );
  52      $definitions    = array();
  53  
  54      // Each file is required in its own variable scope.
  55      $require = static function ( $file ) {
  56          return require $file;
  57      };
  58  
  59      foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  60          $name       = $post_type->name;
  61          $can_create = current_user_can( $post_type->cap->create_posts );
  62  
  63          // The homepage and the posts page are site settings. When the theme
  64          // has a `front-page` template, the front page cannot be changed, see
  65          // https://developer.wordpress.org/themes/basics/template-hierarchy/.
  66          $can_set_homepage = 'page' === $name &&
  67              current_user_can( 'manage_options' ) &&
  68              empty( get_block_templates( array( 'slug__in' => array( 'front-page' ) ) ) );
  69  
  70          // The actions of the post type, keyed by the folder of their script
  71          // module and their id, in the order they are shown.
  72          $applies = array(
  73              'post_type_supports/view-post'             => is_post_type_viewable( $post_type ),
  74              'post_type_supports/view-post-revisions'   => post_type_supports( $name, 'revisions' ),
  75              'post_type_supports/duplicate-post'        => $is_gutenberg && $can_create && ! in_array( $name, $design_types, true ),
  76              'wp_template_part/duplicate-template-part' => 'wp_template_part' === $name && $can_create && $is_block_theme,
  77              'wp_block/duplicate-pattern'               => 'wp_block' === $name && $can_create,
  78              'post_type_supports/rename-post'           => post_type_supports( $name, 'title' ),
  79              'post_type_supports/order-pages'           => post_type_supports( $name, 'page-attributes' ),
  80              'wp_block/export-pattern'                  => 'wp_block' === $name,
  81              'post_type_supports/restore'               => true,
  82              'post_type_supports/reset-post'            => true,
  83              'post_type_supports/delete-post'           => true,
  84              'post_type_supports/move-to-trash'         => true,
  85              'post_type_supports/permanently-delete'    => true,
  86              'page/set-as-homepage'                     => $can_set_homepage,
  87              'page/set-as-posts-page'                   => $can_set_homepage,
  88          );
  89  
  90          foreach ( array_keys( array_filter( $applies ) ) as $key ) {
  91              list( $module, $id ) = explode( '/', $key );
  92              if ( ! isset( $definitions[ $key ] ) ) {
  93                  // The id comes first, like in the actions registered directly.
  94                  $definitions[ $key ] = array_merge( array( 'id' => $id ), $require( __DIR__ . "/{$key}/action.php" ) );
  95              }
  96              wp_register_entity_actions( 'core', 'postType', $name, array( $definitions[ $key ] ), "@wordpress/core-actions/{$module}" );
  97          }
  98      }
  99  }
 100  add_action( 'wp_entity_actions_api_init', 'register_core_entity_actions', 0 );


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