Automattic\WooCommerce\Internal\PushNotifications\Controllers

PushTokenRestController::delete │ public │ WC 10.6.0

Deletes a push token record.

Users can only delete their own tokens. WPCOM can delete any token, so support can stop notifications to a device its owner can no longer reach from the app.

Метод класса: PushTokenRestController{}

Хуков нет.

Возвращает

WP_REST_Response|WP_Error.

Использование

$PushTokenRestController = new PushTokenRestController();
$PushTokenRestController->delete( $request );
$request(WP_REST_Request) (обязательный)
The request object.

Список изменений

С версии 10.6.0 Введена.

Код PushTokenRestController::delete() WC 11.3.0

public function delete( WP_REST_Request $request ) {
	try {
		$id         = (int) $request->get_param( 'id' );
		$from_wpcom = $this->is_signed_with_blog_token();
		$data_store = wc_get_container()->get( PushTokensDataStore::class );
		$push_token = $data_store->read( $id );

		if ( ! $from_wpcom && $push_token->get_user_id() !== get_current_user_id() ) {
			throw new PushTokenNotFoundException();
		}

		$deleted = $data_store->delete( $id );

		if ( ! $deleted ) {
			throw new WC_Data_Exception(
				'woocommerce_push_token_not_deleted',
				'The push token could not be deleted.',
				WP_Http::INTERNAL_SERVER_ERROR
			);
		}

		if ( $from_wpcom ) {
			wc_get_container()
				->get( LegacyProxy::class )
				->call_function( 'wc_get_logger' )
				->info(
					'Push token deleted by WordPress.com support. The device it was registered from will no longer receive push notifications from this store.',
					array(
						'source'   => PushNotifications::FEATURE_NAME,
						'token_id' => $id,
						'user_id'  => $push_token->get_user_id(),
						'platform' => $push_token->get_platform(),
					)
				);
		}
	} catch ( Exception $e ) {
		return $this->convert_exception_to_wp_error( $e );
	}

	return new WP_REST_Response( null, WP_Http::NO_CONTENT );
}