[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-entity-actions-controller.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Entity_Actions_Controller class
   4   *
   5   * @package    WordPress
   6   * @subpackage REST_API
   7   * @since      7.2.0
   8   */
   9  
  10  /**
  11   * Controller which provides a REST endpoint for retrieving the actions
  12   * registered for a given entity type.
  13   *
  14   * The actions are the ones registered on the server on the
  15   * `wp_entity_actions_api_init` action: the serializable part of each action, plus
  16   * the script modules that provide the JavaScript parts (callbacks, modals,
  17   * eligibility checks, icons), each with the ids of the actions it applies
  18   * to. The client merges both into the actions it shows.
  19   *
  20   * @since 7.2.0
  21   *
  22   * @see WP_REST_Controller
  23   */
  24  class WP_REST_Entity_Actions_Controller extends WP_REST_Controller {
  25  
  26      /**
  27       * Constructor.
  28       *
  29       * @since 7.2.0
  30       */
  31  	public function __construct() {
  32          $this->namespace = 'wp/v2';
  33          $this->rest_base = 'entity-actions';
  34      }
  35  
  36      /**
  37       * Registers the routes for the controller.
  38       *
  39       * @since 7.2.0
  40       */
  41  	public function register_routes() {
  42          register_rest_route(
  43              $this->namespace,
  44              '/' . $this->rest_base,
  45              array(
  46                  array(
  47                      'methods'             => WP_REST_Server::READABLE,
  48                      'callback'            => array( $this, 'get_items' ),
  49                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  50                      'args'                => array(
  51                          'kind' => array(
  52                              'description' => __( 'Entity kind.' ),
  53                              'type'        => 'string',
  54                              'minLength'   => 1,
  55                              'required'    => true,
  56                          ),
  57                          'name' => array(
  58                              'description' => __( 'Entity name.' ),
  59                              'type'        => 'string',
  60                              'minLength'   => 1,
  61                              'required'    => true,
  62                          ),
  63                      ),
  64                  ),
  65                  'schema' => array( $this, 'get_public_item_schema' ),
  66              )
  67          );
  68      }
  69  
  70      /**
  71       * Checks if a given request has access to read the actions of an entity.
  72       *
  73       * @since 7.2.0
  74       *
  75       * @param WP_REST_Request $request Full details about the request.
  76       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  77       */
  78  	public function get_items_permissions_check( $request ) {
  79          $kind = $request->get_param( 'kind' );
  80          $name = $request->get_param( 'name' );
  81  
  82          $capability = $this->get_required_capability( $kind, $name );
  83  
  84          if ( null === $capability ) {
  85              return new WP_Error(
  86                  'rest_entity_actions_invalid_entity',
  87                  __( 'Invalid entity kind or name.' ),
  88                  array( 'status' => 404 )
  89              );
  90          }
  91  
  92          if ( ! current_user_can( $capability ) ) {
  93              return new WP_Error(
  94                  'rest_cannot_read',
  95                  __( 'Sorry, you are not allowed to read the actions of this entity.' ),
  96                  array( 'status' => rest_authorization_required_code() )
  97              );
  98          }
  99  
 100          return true;
 101      }
 102  
 103      /**
 104       * Resolves the capability required to read the actions of an entity.
 105       *
 106       * Mirrors the fields endpoint, which gates the same entities: post types
 107       * use their own `edit_posts` capability (which honors custom
 108       * `capability_type` registrations), taxonomies use `manage_terms`, and
 109       * root-level entities use `manage_options`. A post type or taxonomy that is
 110       * not registered, or not exposed to the REST API, resolves to `null` so the
 111       * request is treated as referencing an unknown entity.
 112       *
 113       * Any other kind falls back to `edit_posts`, so actions registered for a
 114       * custom entity kind stay readable behind a baseline capability.
 115       *
 116       * @since 7.2.0
 117       *
 118       * @param string $kind The entity kind (e.g. `postType`).
 119       * @param string $name The entity name (e.g. `page`).
 120       * @return string|null Capability required to read the actions, or null if
 121       *                     the entity is not registered.
 122       */
 123  	protected function get_required_capability( $kind, $name ) {
 124          switch ( $kind ) {
 125              case 'postType':
 126                  $post_type = get_post_type_object( $name );
 127                  if ( $post_type && $post_type->show_in_rest ) {
 128                      return $post_type->cap->edit_posts;
 129                  }
 130                  return null;
 131  
 132              case 'taxonomy':
 133                  $taxonomy = get_taxonomy( $name );
 134                  if ( $taxonomy && $taxonomy->show_in_rest ) {
 135                      return $taxonomy->cap->manage_terms;
 136                  }
 137                  return null;
 138  
 139              case 'root':
 140                  return 'manage_options';
 141          }
 142  
 143          return 'edit_posts';
 144      }
 145  
 146      /**
 147       * Returns the actions registered for the given entity type.
 148       *
 149       * @since 7.2.0
 150       *
 151       * @param WP_REST_Request $request Full details about the request.
 152       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 153       */
 154  	public function get_items( $request ) {
 155          $kind = $request->get_param( 'kind' );
 156          $name = $request->get_param( 'name' );
 157  
 158          $response = array(
 159              'kind'           => $kind,
 160              'name'           => $name,
 161              'actions'        => wp_get_registered_entity_actions( $kind, $name ),
 162              'script_modules' => wp_get_registered_entity_action_modules( $kind, $name ),
 163          );
 164  
 165          return rest_ensure_response( $response );
 166      }
 167  
 168      /**
 169       * Retrieves the item's schema, conforming to JSON Schema.
 170       *
 171       * @since 7.2.0
 172       *
 173       * @return array Item schema data.
 174       */
 175  	public function get_item_schema() {
 176          if ( $this->schema ) {
 177              return $this->add_additional_fields_schema( $this->schema );
 178          }
 179  
 180          $this->schema = array(
 181              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 182              'title'      => 'entity-actions',
 183              'type'       => 'object',
 184              'properties' => array(
 185                  'kind'           => array(
 186                      'description' => __( 'Entity kind.' ),
 187                      'type'        => 'string',
 188                      'readonly'    => true,
 189                  ),
 190                  'name'           => array(
 191                      'description' => __( 'Entity name.' ),
 192                      'type'        => 'string',
 193                      'readonly'    => true,
 194                  ),
 195                  'actions'        => array(
 196                      'description' => __( 'The actions registered for the entity, in registration order.' ),
 197                      'type'        => 'array',
 198                      'readonly'    => true,
 199                      'items'       => $this->get_action_schema(),
 200                  ),
 201                  'script_modules' => array(
 202                      'description' => __( 'The script modules providing the JavaScript parts of the actions, each with the ids of the actions it applies to.' ),
 203                      'type'        => 'array',
 204                      'readonly'    => true,
 205                      'items'       => array(
 206                          'type'       => 'object',
 207                          'properties' => array(
 208                              'id'      => array(
 209                                  'description' => __( 'The id of the script module.' ),
 210                                  'type'        => 'string',
 211                              ),
 212                              'actions' => array(
 213                                  'description' => __( 'The ids of the actions the script module applies to.' ),
 214                                  'type'        => 'array',
 215                                  'items'       => array(
 216                                      'type' => 'string',
 217                                  ),
 218                              ),
 219                          ),
 220                      ),
 221                  ),
 222              ),
 223          );
 224  
 225          return $this->add_additional_fields_schema( $this->schema );
 226      }
 227  
 228      /**
 229       * Returns the schema for an action definition.
 230       *
 231       * Describes the serializable subset of the DataViews Action API, see
 232       * packages/dataviews/src/types/dataviews.ts. The properties that are
 233       * JavaScript callbacks, components, or elements (`callback`,
 234       * `RenderModal`, `isEligible`, `icon`, …) come from the script modules and
 235       * are not part of the definition. Additional properties are allowed, so a
 236       * plugin can register an action with properties of its own that its script
 237       * module understands.
 238       *
 239       * @since 7.2.0
 240       *
 241       * @return array Schema for an action definition.
 242       */
 243  	protected function get_action_schema() {
 244          return array(
 245              'type'                 => 'object',
 246              'properties'           => array(
 247                  'id'                => array(
 248                      'description' => __( 'The unique identifier of the action.' ),
 249                      'type'        => 'string',
 250                  ),
 251                  'origin'            => array(
 252                      'description' => __( 'Who registered the action: `core`, or the slug of a plugin or theme.' ),
 253                      'type'        => 'object',
 254                      'properties'  => array(
 255                          'registeredBy' => array(
 256                              'description' => __( 'Who registered the action.' ),
 257                              'type'        => 'string',
 258                          ),
 259                      ),
 260                  ),
 261                  'label'             => array(
 262                      'description' => __( 'The label of the action.' ),
 263                      'type'        => 'string',
 264                  ),
 265                  'isPrimary'         => array(
 266                      'description' => __( 'Whether the action is a primary action.' ),
 267                      'type'        => 'boolean',
 268                  ),
 269                  'supportsBulk'      => array(
 270                      'description' => __( 'Whether the action can be performed on several items at once.' ),
 271                      'type'        => 'boolean',
 272                  ),
 273                  'disabled'          => array(
 274                      'description' => __( 'Whether the action is disabled.' ),
 275                      'type'        => 'boolean',
 276                  ),
 277                  'context'           => array(
 278                      'description' => __( 'The context the action is limited to, such as `list` for the lists of items. Without it, the action shows everywhere.' ),
 279                      'type'        => 'string',
 280                      'enum'        => WP_Entity_Actions_Registry::SUPPORTED_VALUES['context'],
 281                  ),
 282                  'modalHeader'       => array(
 283                      'description' => __( 'The header of the modal of the action.' ),
 284                      'type'        => 'string',
 285                  ),
 286                  'hideModalHeader'   => array(
 287                      'description' => __( 'Whether to hide the header of the modal of the action.' ),
 288                      'type'        => 'boolean',
 289                  ),
 290                  'modalSize'         => array(
 291                      'description' => __( 'The size of the modal of the action.' ),
 292                      'type'        => 'string',
 293                      'enum'        => WP_Entity_Actions_Registry::SUPPORTED_VALUES['modalSize'],
 294                  ),
 295                  'modalFocusOnMount' => array(
 296                      'description' => __( 'Where the focus goes when the modal of the action opens.' ),
 297                      'type'        => array( 'boolean', 'string' ),
 298                  ),
 299              ),
 300              'additionalProperties' => true,
 301          );
 302      }
 303  }


Generated : Sun Nov 29 08:20:01 2026 Cross-referenced by PHPXref