getInputs(); $reward = $this->approval_reward(); $name = (string) ( $reward['name'] ?? 'Reward ID ' . ( $inputs['reward_id'] ?? '?' ) ); $cost = (string) ( $reward['cost_points'] ?? '?' ); $type = (string) ( $reward['redeem_type'] ?? '?' ); /* translators: %1$s reward name, %2$s point cost, %3$s redemption type. */ return sprintf( __( 'Deactivate reward "%1$s" (%2$s points, %3$s redemption context); deactivate, remains in history.', 'aisentic' ), $name, $cost, $type ); } public function get_approval_inputs(): array { $inputs = $this->getInputs(); $reward = $this->approval_reward(); if ( [] !== $reward ) { $inputs['reward'] = sprintf( '%s (ID: %d)', (string) ( $reward['name'] ?? 'Reward' ), (int) ( $reward['id'] ?? $inputs['reward_id'] ?? 0 ) ); $inputs['cost_points'] = $reward['cost_points'] ?? null; $inputs['redemption_context'] = sprintf( '%s / %s', (string) ( $reward['redeem_method'] ?? '' ), (string) ( $reward['redeem_type'] ?? '' ) ); } $inputs['deletion'] = __( 'Deactivate; remains in redemption history.', 'aisentic' ); return $inputs; } public function __invoke( int $reward_id ): array { $guard = $this->guard( 'manage_pointics', 'manage_options' ); if ( null !== $guard ) { return $guard; } $ready = $this->pointics_tables_ready(); if ( null !== $ready ) { return $this->steer( $ready ); } try { $controller = pointics_container()->make( \Pointics\Api\Controllers\Rewards_Controller::class ); $request = new \WP_REST_Request( 'DELETE', '/pointics/v1/rewards/' . $reward_id ); $request->set_url_params( [ 'id' => $reward_id ] ); $data = $this->extract_response( $controller->delete_reward( $request ), __( 'Failed to deactivate the Pointics reward.', 'aisentic' ), 'pointics-delete-reward' ); if ( $this->is_error( $data ) ) { return $data; } return [ 'success' => true, 'deleted' => true, 'reward_id' => $reward_id, 'message' => __( 'Pointics reward deactivated; it remains in redemption history.', 'aisentic' ), ]; } catch ( \Throwable $e ) { Log::error( 'Pointics_Delete_Reward_Tool error', [ 'error' => $e->getMessage() ] ); return $this->steer( [ 'error' => __( 'Failed to deactivate the Pointics reward.', 'aisentic' ), 'code' => 'internal', 'status' => null, ] ); } } private function approval_reward(): array { $id = (int) ( $this->getInputs()['reward_id'] ?? 0 ); return $this->entity_lookup( \Pointics\Api\Controllers\Rewards_Controller::class, 'get_reward', $id, 'pointics-get-reward', __( 'Failed to retrieve the Pointics reward.', 'aisentic' ) ); } }