Make WordPress Core


Ignore:
Timestamp:
11/01/2021 03:26:06 AM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support custom namespaces for taxonomies.

While a taxonomy can define a custom route by using the rest_base argument, a namespace of wp/v2 was assumed. This commit introduces support for a rest_namespace argument.

A new rest_get_route_for_taxonomy_items function has been introduced and the rest_get_route_for_term function updated to facilitate getting the correct route for taxonomies.

For maximum compatibility sticking with the default wp/v2 namespace is recommended until the API functions see wider use.

Props spacedmonkey.
Fixes #54267.
See [51962].

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r51786 r51964  
    251251                }
    252252
     253                if ( in_array( 'rest_namespace', $fields, true ) ) {
     254                        $data['rest_namespace'] = $taxonomy->rest_namespace;
     255                }
     256
    253257                if ( in_array( 'visibility', $fields, true ) ) {
    254258                        $data['visibility'] = array(
     
    275279                                ),
    276280                                'https://api.w.org/items' => array(
    277                                         'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
     281                                        'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
    278282                                ),
    279283                        )
     
    311315                        'type'       => 'object',
    312316                        'properties' => array(
    313                                 'capabilities' => array(
     317                                'capabilities'   => array(
    314318                                        'description' => __( 'All capabilities used by the taxonomy.' ),
    315319                                        'type'        => 'object',
     
    317321                                        'readonly'    => true,
    318322                                ),
    319                                 'description'  => array(
     323                                'description'    => array(
    320324                                        'description' => __( 'A human-readable description of the taxonomy.' ),
    321325                                        'type'        => 'string',
     
    323327                                        'readonly'    => true,
    324328                                ),
    325                                 'hierarchical' => array(
     329                                'hierarchical'   => array(
    326330                                        'description' => __( 'Whether or not the taxonomy should have children.' ),
    327331                                        'type'        => 'boolean',
     
    329333                                        'readonly'    => true,
    330334                                ),
    331                                 'labels'       => array(
     335                                'labels'         => array(
    332336                                        'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
    333337                                        'type'        => 'object',
     
    335339                                        'readonly'    => true,
    336340                                ),
    337                                 'name'         => array(
     341                                'name'           => array(
    338342                                        'description' => __( 'The title for the taxonomy.' ),
    339343                                        'type'        => 'string',
     
    341345                                        'readonly'    => true,
    342346                                ),
    343                                 'slug'         => array(
     347                                'slug'           => array(
    344348                                        'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
    345349                                        'type'        => 'string',
     
    347351                                        'readonly'    => true,
    348352                                ),
    349                                 'show_cloud'   => array(
     353                                'show_cloud'     => array(
    350354                                        'description' => __( 'Whether or not the term cloud should be displayed.' ),
    351355                                        'type'        => 'boolean',
     
    353357                                        'readonly'    => true,
    354358                                ),
    355                                 'types'        => array(
     359                                'types'          => array(
    356360                                        'description' => __( 'Types associated with the taxonomy.' ),
    357361                                        'type'        => 'array',
     
    362366                                        'readonly'    => true,
    363367                                ),
    364                                 'rest_base'    => array(
     368                                'rest_base'      => array(
    365369                                        'description' => __( 'REST base route for the taxonomy.' ),
    366370                                        'type'        => 'string',
     
    368372                                        'readonly'    => true,
    369373                                ),
    370                                 'visibility'   => array(
     374                                'rest_namespace' => array(
     375                                        'description' => __( 'REST namespace route for the taxonomy.' ),
     376                                        'type'        => 'string',
     377                                        'context'     => array( 'view', 'edit', 'embed' ),
     378                                        'readonly'    => true,
     379                                ),
     380                                'visibility'     => array(
    371381                                        'description' => __( 'The visibility settings for the taxonomy.' ),
    372382                                        'type'        => 'object',
Note: See TracChangeset for help on using the changeset viewer.