BP_REST_Components_Endpoint::update_item( WP_REST_Request $request )

Activate/Deactivate a component.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-core/classes/class-bp-rest-components-endpoint.php

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
public function update_item( $request ) {
    $component = $request['name'];
 
    if ( ! $this->component_exists( $component ) ) {
        return new WP_Error(
            'bp_rest_component_nonexistent',
            __( 'Sorry, this component does not exist.', 'buddyboss' ),
            array(
                'status' => 404,
            )
        );
    }
 
    $action = $request['action'];
    if ( empty( $action ) || ! in_array( $action, array( 'activate', 'deactivate' ), true ) ) {
        return new WP_Error(
            'bp_rest_component_invalid_action',
            __( 'Sorry, this is not a valid action.', 'buddyboss' ),
            array(
                'status' => 500,
            )
        );
    }
 
    if ( 'activate' === $action ) {
        if ( bp_is_active( $component ) ) {
            return new WP_Error(
                'bp_rest_component_already_active',
                __( 'Sorry, this component is already active.', 'buddyboss' ),
                array(
                    'status' => 400,
                )
            );
        }
 
        $component_info = $this->activate_helper( $component );
    } else {
        if ( ! bp_is_active( $component ) ) {
            return new WP_Error(
                'bp_rest_component_inactive',
                __( 'Sorry, this component is not active.', 'buddyboss' ),
                array(
                    'status' => 400,
                )
            );
        }
 
        if ( array_key_exists( $component, bp_core_get_components( 'required' ) ) ) {
            return new WP_Error(
                'bp_rest_required_component',
                __( 'Sorry, you cannot deactivate a required component.', 'buddyboss' ),
                array(
                    'status' => 400,
                )
            );
        }
 
        $component_info = $this->deactivate_helper( $component );
    }
 
    $request->set_param( 'context', 'edit' );
 
    $retval = array(
        $this->prepare_response_for_collection(
            $this->prepare_item_for_response( $component_info, $request )
        ),
    );
 
    $response = rest_ensure_response( $retval );
 
    /**
     * Fires after a component is updated via the REST API.
     *
     * @param array $component_info Component info.
     * @param WP_REST_Response $response The response data.
     * @param WP_REST_Request $request The request sent to the API.
     *
     * @since 0.1.0
     */
    do_action( 'bp_rest_components_update_item', $component_info, $response, $request );
 
    return $response;
}

Changelog

Changelog
Version Description
0.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.