| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Sellkit\Funnel\Contacts; |
| 5 |
|
| 6 |
use Sellkit\Database; |
| 7 |
use Sellkit_Funnel; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || die(); |
| 10 |
|
| 11 |
/** |
| 12 |
* Base contacts class. |
| 13 |
* |
| 14 |
* @since 1.5.0 |
| 15 |
*/ |
| 16 |
class Base_Contacts { |
| 17 |
|
| 18 |
/** |
| 19 |
* SellKit database. |
| 20 |
* |
| 21 |
* @var Database |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
public $db; |
| 25 |
|
| 26 |
/** |
| 27 |
* SellKit funnel class. |
| 28 |
* |
| 29 |
* @var Object|Sellkit_Funnel|null |
| 30 |
* @since 1.5.0 |
| 31 |
*/ |
| 32 |
public $sellkit_funnel; |
| 33 |
|
| 34 |
/** |
| 35 |
* Base_Contacts constructor. |
| 36 |
* |
| 37 |
* @since 1.5.0 |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
if ( headers_sent() ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! session_id() ) { |
| 45 |
session_start(); |
| 46 |
} |
| 47 |
|
| 48 |
$this->sellkit_funnel = Sellkit_Funnel::get_instance(); |
| 49 |
$this->db = new Database(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Created a new log. |
| 54 |
* |
| 55 |
* @since 1.5.0 |
| 56 |
*/ |
| 57 |
public function add_new_log() { |
| 58 |
$analytics_id = $this->db->insert( 'funnel_contact', [ |
| 59 |
'funnel_id' => $this->sellkit_funnel->funnel_id, |
| 60 |
'user_id' => get_current_user_id(), |
| 61 |
'updated_at' => time(), |
| 62 |
'created_at' => time(), |
| 63 |
] ); |
| 64 |
|
| 65 |
$_SESSION['entered_funnel_id'] = $analytics_id; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Adds accept status. |
| 70 |
* |
| 71 |
* @since 1.5.0 |
| 72 |
*/ |
| 73 |
public static function step_is_passed() { |
| 74 |
$database = new Database(); |
| 75 |
$funnel = Sellkit_Funnel::get_instance(); |
| 76 |
|
| 77 |
if ( empty( $funnel->funnel_id ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$type = $funnel->current_step_data['type']['key']; |
| 82 |
$old_values = []; |
| 83 |
|
| 84 |
// Getting old data. |
| 85 |
$result = $database->get( 'funnel_contact', [ 'id' => $_SESSION['entered_funnel_id'] ] ); |
| 86 |
|
| 87 |
if ( ! empty( $result[0][ $type ] ) ) { |
| 88 |
$old_values = unserialize( $result[ 0 ][ $type ] ); // phpcs:ignore |
| 89 |
} |
| 90 |
|
| 91 |
$new_values = array_merge( $old_values, [ $funnel->current_step_data['page_id'] ] ); |
| 92 |
|
| 93 |
$database->update( |
| 94 |
'funnel_contact', |
| 95 |
[ $funnel->current_step_data['type']['key'] => $new_values ], |
| 96 |
[ 'id' => $_SESSION['entered_funnel_id'] ] |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Adds price to total spent column of this funnel. |
| 102 |
* |
| 103 |
* @param string $price Price value. |
| 104 |
* @param int $funnel_id funnel id. |
| 105 |
* @since 1.5.0 |
| 106 |
*/ |
| 107 |
public function add_total_spent( $price, $funnel_id ) { |
| 108 |
// Getting old data. |
| 109 |
$result = $this->db->get( 'funnel_contact', [ 'id' => $funnel_id ] ); |
| 110 |
$old_spent = ! empty( $result[0]['total_spent'] ) ? $result[0]['total_spent'] : 0; |
| 111 |
|
| 112 |
$new_spent = floatval( $old_spent ) + floatval( $price ); |
| 113 |
|
| 114 |
$this->db->update( |
| 115 |
'funnel_contact', |
| 116 |
[ 'total_spent' => $new_spent ], |
| 117 |
[ 'id' => $funnel_id ] |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|