Make WordPress Core


Ignore:
Timestamp:
07/02/2020 05:55:04 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Link to the REST route for the currently queried resource.

This allows for programatically determining the REST version of the current page. The links also aid human discovery of the REST API in general.

Props dshanske, tfrommen, TimothyBlynJacobs.
Fixes #49116.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-taxonomy.php

    r47808 r48273  
    209209     */
    210210    public $rest_controller_class;
     211
     212    /**
     213     * The controller instance for this taxonomy's REST API endpoints.
     214     *
     215     * Lazily computed. Should be accessed using {@see WP_Taxonomy::get_rest_controller()}.
     216     *
     217     * @since 5.5.0
     218     * @var WP_REST_Controller $rest_controller
     219     */
     220    public $rest_controller;
    211221
    212222    /**
     
    453463        remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
    454464    }
     465
     466    /**
     467     * Gets the REST API controller for this taxonomy.
     468     *
     469     * Will only instantiate the controller class once per request.
     470     *
     471     * @since 5.5.0
     472     *
     473     * @return WP_REST_Controller|null The controller instance, or null if the taxonomy
     474     *                                 is set not to show in rest.
     475     */
     476    public function get_rest_controller() {
     477        if ( ! $this->show_in_rest ) {
     478            return null;
     479        }
     480
     481        $class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Terms_Controller::class;
     482
     483        if ( ! class_exists( $class ) ) {
     484            return null;
     485        }
     486
     487        if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
     488            return null;
     489        }
     490
     491        if ( ! $this->rest_controller ) {
     492            $this->rest_controller = new $class( $this->name );
     493        }
     494
     495        if ( ! ( $this->rest_controller instanceof $class ) ) {
     496            return null;
     497        }
     498
     499        return $this->rest_controller;
     500    }
    455501}
Note: See TracChangeset for help on using the changeset viewer.