Skip to:
Content

BuddyPress.org

Changeset 12463


Ignore:
Timestamp:
09/25/2019 11:22:00 PM (4 months ago)
Author:
imath
Message:

Improve the way BP REST API Controllers are loaded

Introduce a new filter to allow site owners to eventually disable one or more BP REST API Controllers.

See #7156

Location:
trunk
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/classes/class-bp-activity-component.php

    r12451 r12463  
    462462     *
    463463     * @since 5.0.0
    464      */
    465     public function rest_api_init() {
    466         $controller = new BP_REST_Activity_Endpoint();
    467         $controller->register_routes();
    468 
    469         parent::rest_api_init();
     464     *
     465     * @param array $controllers Optional. See BP_Component::rest_api_init() for
     466     *                           description.
     467     */
     468    public function rest_api_init( $controllers = array() ) {
     469        parent::rest_api_init( array( 'BP_REST_Activity_Endpoint' ) );
    470470    }
    471471}
  • trunk/src/bp-core/classes/class-bp-component.php

    r12451 r12463  
    868868     *
    869869     * @since 5.0.0
    870      */
    871     public function rest_api_init() {
     870     *
     871     * @param array $controllers The list of BP REST controllers to load.
     872     */
     873    public function rest_api_init( $controllers = array() ) {
     874        if ( is_array( $controllers ) && $controllers ) {
     875            // Built-in controllers.
     876            $_controllers = $controllers;
     877
     878            /**
     879             * Use this filter to disable all or some REST API controllers
     880             * for the component.
     881             *
     882             * This is a dynamic hook that is based on the component string ID.
     883             *
     884             * @since 5.0.0
     885             *
     886             * @param array $controllers The list of BP REST API controllers to load.
     887             */
     888            $controllers = (array) apply_filters( 'bp_' . $this->id . '_rest_api_controllers', $controllers );
     889
     890            foreach( $controllers as $controller ) {
     891                if ( ! in_array( $controller, $_controllers, true ) ) {
     892                    continue;
     893                }
     894
     895                $component_controller = new $controller;
     896                $component_controller->register_routes();
     897            }
     898        }
    872899
    873900        /**
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r12451 r12463  
    928928     *
    929929     * @since 5.0.0
    930      */
    931     public function rest_api_init() {
    932         $controller = new BP_REST_Groups_Endpoint();
    933         $controller->register_routes();
    934 
    935         $controller = new BP_REST_Group_Membership_Endpoint();
    936         $controller->register_routes();
    937 
    938         $controller = new BP_REST_Group_Invites_Endpoint();
    939         $controller->register_routes();
    940 
    941         $controller = new BP_REST_Group_Membership_Request_Endpoint();
    942         $controller->register_routes();
    943 
    944         $controller = new BP_REST_Attachments_Group_Avatar_Endpoint();
    945         $controller->register_routes();
    946 
    947         parent::rest_api_init();
     930     *
     931     * @param array $controllers Optional. See BP_Component::rest_api_init() for
     932     *                           description.
     933     */
     934    public function rest_api_init( $controllers = array() ) {
     935        parent::rest_api_init( array(
     936            'BP_REST_Groups_Endpoint',
     937            'BP_REST_Group_Membership_Endpoint',
     938            'BP_REST_Group_Invites_Endpoint',
     939            'BP_REST_Group_Membership_Request_Endpoint',
     940            'BP_REST_Attachments_Group_Avatar_Endpoint',
     941        ) );
    948942    }
    949943}
  • trunk/src/bp-members/classes/class-bp-members-component.php

    r12451 r12463  
    462462     *
    463463     * @since 5.0.0
    464      */
    465     public function rest_api_init() {
    466         /**
    467          * As the Members component is always loaded,
    468          * let's register the Components endpoint here.
    469          */
    470         $controller = new BP_REST_Components_Endpoint();
    471         $controller->register_routes();
    472 
    473         $controller = new BP_REST_Members_Endpoint();
    474         $controller->register_routes();
    475 
    476         $controller = new BP_REST_Attachments_Member_Avatar_Endpoint();
    477         $controller->register_routes();
    478 
    479         parent::rest_api_init();
     464     *
     465     * @param array $controllers Optional. See BP_Component::rest_api_init() for
     466     *                           description.
     467     */
     468    public function rest_api_init( $controllers = array() ) {
     469        parent::rest_api_init( array(
     470            /**
     471             * As the Members component is always loaded,
     472             * let's register the Components endpoint here.
     473             */
     474            'BP_REST_Components_Endpoint',
     475            'BP_REST_Members_Endpoint',
     476            'BP_REST_Attachments_Member_Avatar_Endpoint',
     477        ) );
    480478    }
    481479}
  • trunk/src/bp-messages/classes/class-bp-messages-component.php

    r12451 r12463  
    439439     *
    440440     * @since 5.0.0
    441      */
    442     public function rest_api_init() {
    443         $controller = new BP_REST_Messages_Endpoint();
    444         $controller->register_routes();
    445 
    446         parent::rest_api_init();
     441     *
     442     * @param array $controllers Optional. See BP_Component::rest_api_init() for
     443     *                           description.
     444     */
     445    public function rest_api_init( $controllers = array() ) {
     446        parent::rest_api_init( array( 'BP_REST_Messages_Endpoint' ) );
    447447    }
    448448}
  • trunk/src/bp-notifications/classes/class-bp-notifications-component.php

    r12451 r12463  
    321321     *
    322322     * @since 5.0.0
    323      */
    324     public function rest_api_init() {
    325         $controller = new BP_REST_Notifications_Endpoint();
    326         $controller->register_routes();
    327 
    328         parent::rest_api_init();
     323     *
     324     * @param array $controllers Optional. See BP_Component::rest_api_init() for
     325     *                           description.
     326     */
     327    public function rest_api_init( $controllers = array() ) {
     328        parent::rest_api_init( array( 'BP_REST_Notifications_Endpoint' ) );
    329329    }
    330330}
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-component.php

    r12451 r12463  
    495495     *
    496496     * @since 5.0.0
    497      */
    498     public function rest_api_init() {
    499         $controller = new BP_REST_XProfile_Fields_Endpoint();
    500         $controller->register_routes();
    501 
    502         $controller = new BP_REST_XProfile_Field_Groups_Endpoint();
    503         $controller->register_routes();
    504 
    505         $controller = new BP_REST_XProfile_Data_Endpoint();
    506         $controller->register_routes();
    507 
    508         parent::rest_api_init();
     497     *
     498     * @param array $controllers Optional. See BP_Component::rest_api_init() for
     499     *                           description.
     500     */
     501    public function rest_api_init( $controllers = array() ) {
     502        parent::rest_api_init( array(
     503            'BP_REST_XProfile_Fields_Endpoint',
     504            'BP_REST_XProfile_Field_Groups_Endpoint',
     505            'BP_REST_XProfile_Data_Endpoint',
     506        ) );
    509507    }
    510508}
Note: See TracChangeset for help on using the changeset viewer.