# sellkit/2.7.0/includes/funnel/contacts/base-contacts.php

SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster, version 2.7.0. 200 lines.

- Page: https://pluginprobe.com/plugins/sellkit/2.7.0/code/includes/funnel/contacts/base-contacts.php
- Raw: https://pluginprobe.com/plugins/sellkit/2.7.0/raw/includes/funnel/contacts/base-contacts.php
- Modified: 2026-08-17T07:36:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sellkit/2.7.0/code/includes/funnel/contacts/base-contacts.php#L10-L20`.

```php
<?php


namespace Sellkit\Funnel\Contacts;

use Sellkit\Database;
use Sellkit_Funnel;

defined( 'ABSPATH' ) || die();

/**
 * Base contacts class.
 *
 * @since 1.5.0
 */
class Base_Contacts {

	/**
	 * SellKit database.
	 *
	 * @var Database
	 * @since 1.5.0
	 */
	public $db;

	/**
	 * SellKit funnel class.
	 *
	 * @var Object|Sellkit_Funnel|null
	 * @since 1.5.0
	 */
	public $sellkit_funnel;

	/**
	 * Base_Contacts constructor.
	 *
	 * @since 1.5.0
	 */
	public function __construct() {
		$this->sellkit_funnel = Sellkit_Funnel::get_instance();
		$this->db             = new Database();
	}

	/**
	 * Created a new log.
	 *
	 * @since 1.5.0
	 */
	public function add_new_log() {
		$analytics_id = $this->db->insert( 'funnel_contact', [
			'funnel_id' => $this->sellkit_funnel->funnel_id,
			'user_id' => get_current_user_id(),
			'updated_at' => time(),
			'created_at' => time(),
		] );

		self::start_session();

		$_SESSION['entered_funnel_id'] = $analytics_id;

		self::end_session();
	}

	/**
	 * Adds accept status.
	 *
	 * @param array $upsell_downsell_data Upsell and downsell data.
	 * @since 1.5.0
	 */
	public static function step_is_passed( $upsell_downsell_data = [] ) {
		self::start_session();

		$database = new Database();
		$funnel   = Sellkit_Funnel::get_instance();

		if ( ! empty( $upsell_downsell_data ) ) {
			self::handle_contact_data_on_upsell_donwsell( $upsell_downsell_data, $database );
			return;
		}

		if ( empty( $funnel->funnel_id ) ) {
			self::end_session();
			return;
		}

		$type       = $funnel->current_step_data['type']['key'];
		$old_values = [];

		if ( ! isset( $_SESSION['entered_funnel_id'] ) ) {
			self::end_session();
			return;
		}

		$contact_id = absint( $_SESSION['entered_funnel_id'] );

		self::end_session();

		// Getting old data.
		$result = $database->get( 'funnel_contact', [ 'id' => $contact_id ] );

		if ( ! empty( $result[0][ $type ] ) ) {
			$old_values = unserialize( $result[ 0 ][ $type ] ); // phpcs:ignore
		}

		$new_values = array_merge( $old_values, [ $funnel->current_step_data['page_id'] ] );

		$database->update(
			'funnel_contact',
			[ $funnel->current_step_data['type']['key'] => $new_values ],
			[ 'id' => $contact_id ]
		);
	}

	/**
	 * Handles contact data on upsell and downsell.
	 *
	 * @param array  $upsell_downsell_data Upsell and downsell data.
	 * @param object $database Database object.
	 * @since 1.9.2
	 */
	public static function handle_contact_data_on_upsell_donwsell( $upsell_downsell_data, $database ) {
		self::start_session();

		if ( ! isset( $_SESSION['entered_funnel_id'] ) ) {
			self::end_session();
			return;
		}

		$contact_id = absint( $_SESSION['entered_funnel_id'] );

		self::end_session();

		$result = $database->get( 'funnel_contact', [ 'id' => $contact_id ] );

		$old_values = [];

		if ( ! empty( $result[0][ $upsell_downsell_data['key'] ] ) ) {
			$old_values = unserialize( $result[ 0 ][ $upsell_downsell_data['key'] ] ); // phpcs:ignore
		}

		$new_values = array_merge( $old_values, [ $upsell_downsell_data['page_id'] ] );

		$database->update(
			'funnel_contact',
			[ $upsell_downsell_data['key'] => $new_values ],
			[ 'id' => $contact_id ]
		);
	}

	/**
	 * Adds price to total spent column of this funnel.
	 *
	 * @param string $price Price value.
	 * @param int    $funnel_id funnel id.
	 * @since 1.5.0
	 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
	 */
	public function add_total_spent( $price, $funnel_id ) {
		self::start_session();

		$contact_id = isset( $_SESSION['entered_funnel_id'] ) ? absint( $_SESSION['entered_funnel_id'] ) : 0;

		self::end_session();

		// Getting old data.
		$result    = $this->db->get( 'funnel_contact', [ 'id' => $contact_id ] );
		$old_spent = ! empty( $result[0]['total_spent'] ) ? $result[0]['total_spent'] : 0;

		$new_spent = floatval( $old_spent ) + floatval( $price );

		$this->db->update(
			'funnel_contact',
			[ 'total_spent' => $new_spent ],
			[ 'id' => $contact_id ]
		);
	}

	/**
	 * Starts a session if not already started.
	 *
	 * @since 2.6.0
	 */
	public static function start_session() {
		if ( ! headers_sent() && session_status() !== PHP_SESSION_ACTIVE ) {
			session_start();
		}
	}

	/**
	 * Ends the session if it is active.
	 *
	 * @since 2.6.0
	 */
	public static function end_session() {
		if ( session_status() === PHP_SESSION_ACTIVE ) {
			session_write_close();
		}
	}
}

```
