getInputs(); $id = (int) ( $inputs['channel_id'] ?? 0 ); $name = $this->approval_channel_name( $id ); return sprintf( /* translators: 1: channel name, 2: channel ID. */ __( 'Deactivate channel %1$s (ID: %2$d), ledger history preserved.', 'aisentic' ), '' !== $name ? $name : __( 'unknown', 'aisentic' ), $id ); } /** * Approval card inputs with resolved channel name. * * @return array */ public function get_approval_inputs(): array { $inputs = $this->getInputs(); $id = (int) ( $inputs['channel_id'] ?? 0 ); $name = $this->approval_channel_name( $id ); if ( '' !== $name ) { /* translators: 1: channel name, 2: channel ID. */ $inputs['channel'] = sprintf( __( '%1$s (ID: %2$d)', 'aisentic' ), $name, $id ); } $inputs['impact'] = __( 'Deactivate, ledger history preserved.', 'aisentic' ); return $inputs; } /** * Execute the tool after approval. * * @param int $channel_id Channel ID. * @return array */ public function __invoke( int $channel_id ): array { $check = $this->guard( 'manage_pointics', 'manage_options' ); if ( $check ) { return $check; } $ready = $this->pointics_tables_ready(); if ( $ready ) { return $this->steer( $ready ); } if ( $channel_id <= 0 ) { return $this->steer( [ 'error' => __( 'channel_id must be a positive integer. Use pointics-list-channels to find it.', 'aisentic' ), 'code' => 'validation', 'status' => 422, ] ); } try { $controller = pointics_container()->make( \Pointics\Api\Controllers\Channels_Controller::class ); $get = new \WP_REST_Request( 'GET' ); $get->set_param( 'id', $channel_id ); $existing = $this->extract_response( $controller->get_channel( $get ), __( 'Failed to retrieve the Pointics channel before deactivating it.', 'aisentic' ), 'pointics-get-channel' ); if ( $this->is_error( $existing ) ) { if ( 'not_found' === ( $existing['code'] ?? '' ) ) { return $this->steer( [ 'error' => sprintf( /* translators: %d: channel ID. */ __( 'Channel #%d is already inactive or does not exist; deactivation is idempotent. Use pointics-list-channels to verify the current state.', 'aisentic' ), $channel_id ), 'code' => 'logical_failure', 'status' => 404, ] ); } return $existing; } $request = new \WP_REST_Request( 'DELETE' ); $request->set_param( 'id', $channel_id ); $data = $this->extract_response( $controller->delete_channel( $request ), __( 'Failed to deactivate the Pointics channel.', 'aisentic' ), 'pointics-delete-channel' ); if ( $this->is_error( $data ) ) { return $data; } if ( array_key_exists( 'deleted', $data ) && false === $data['deleted'] ) { $data['error'] = __( 'Pointics could not write the channel deactivation (storage error). Use pointics-list-channels to verify the current state before retrying.', 'aisentic' ); $data['code'] = 'logical_failure'; $data['status'] = 200; return $this->steer( $data ); } return [ 'success' => true, 'channel_id' => $channel_id, 'deleted' => true, 'message' => __( 'Pointics channel deactivated; ledger history preserved.', 'aisentic' ), ]; } catch ( \Throwable $e ) { Log::error( 'Pointics_Delete_Channel_Tool error', [ 'error' => $e->getMessage() ] ); return $this->steer( [ 'error' => __( 'Failed to deactivate the Pointics channel.', 'aisentic' ), 'code' => 'internal', 'status' => null, ] ); } } /** * Resolve a channel name for approval UI. * * @param int $channel_id Channel ID. * @return string */ private function approval_channel_name( int $channel_id ): string { $channel = $this->entity_lookup( \Pointics\Api\Controllers\Channels_Controller::class, 'get_channel', $channel_id, 'pointics-get-channel', __( 'Unable to resolve the channel for approval.', 'aisentic' ) ); return (string) ( $channel['name'] ?? '' ); } }