| @@ -36,16 +36,8 @@ | ||
| 36 | 36 | * |
| 37 | 37 | * @since 1.5.0 |
| 38 | 38 | */ |
| 39 | 39 | public function __construct() { |
| 40 | - if ( headers_sent() ) { | |
| 41 | - return; | |
| 42 | - } | |
| 43 | - | |
| 44 | - if ( ! session_id() ) { | |
| 45 | - session_start(); | |
| 46 | - } | |
| 47 | - | |
| 48 | 40 | $this->sellkit_funnel = Sellkit_Funnel::get_instance(); |
| 49 | 41 | $this->db = new Database(); |
| 50 | 42 | } |
| 51 | 43 | |
| @@ -61,21 +53,34 @@ | ||
| 61 | 53 | 'updated_at' => time(), |
| 62 | 54 | 'created_at' => time(), |
| 63 | 55 | ] ); |
| 64 | 56 | |
| 57 | + self::start_session(); | |
| 58 | + | |
| 65 | 59 | $_SESSION['entered_funnel_id'] = $analytics_id; |
| 60 | + | |
| 61 | + self::end_session(); | |
| 66 | 62 | } |
| 67 | 63 | |
| 68 | 64 | /** |
| 69 | 65 | * Adds accept status. |
| 70 | 66 | * |
| 67 | + * @param array $upsell_downsell_data Upsell and downsell data. | |
| 71 | 68 | * @since 1.5.0 |
| 72 | 69 | */ |
| 73 | - public static function step_is_passed() { | |
| 70 | + public static function step_is_passed( $upsell_downsell_data = [] ) { | |
| 71 | + self::start_session(); | |
| 72 | + | |
| 74 | 73 | $database = new Database(); |
| 75 | 74 | $funnel = Sellkit_Funnel::get_instance(); |
| 76 | 75 | |
| 76 | + if ( ! empty( $upsell_downsell_data ) ) { | |
| 77 | + self::handle_contact_data_on_upsell_donwsell( $upsell_downsell_data, $database ); | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 77 | 81 | if ( empty( $funnel->funnel_id ) ) { |
| 82 | + self::end_session(); | |
| 78 | 83 | return; |
| 79 | 84 | } |
| 80 | 85 | |
| 81 | 86 | $type = $funnel->current_step_data['type']['key']; |
| @@ -80,10 +85,19 @@ | ||
| 80 | 85 | |
| 81 | 86 | $type = $funnel->current_step_data['type']['key']; |
| 82 | 87 | $old_values = []; |
| 83 | 88 | |
| 89 | + if ( ! isset( $_SESSION['entered_funnel_id'] ) ) { | |
| 90 | + self::end_session(); | |
| 91 | + return; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $contact_id = absint( $_SESSION['entered_funnel_id'] ); | |
| 95 | + | |
| 96 | + self::end_session(); | |
| 97 | + | |
| 84 | 98 | // Getting old data. |
| 85 | - $result = $database->get( 'funnel_contact', [ 'id' => $_SESSION['entered_funnel_id'] ] ); | |
| 99 | + $result = $database->get( 'funnel_contact', [ 'id' => $contact_id ] ); | |
| 86 | 100 | |
| 87 | 101 | if ( ! empty( $result[0][ $type ] ) ) { |
| 88 | 102 | $old_values = unserialize( $result[ 0 ][ $type ] ); // phpcs:ignore |
| 89 | 103 | } |
| @@ -92,22 +106,65 @@ | ||
| 92 | 106 | |
| 93 | 107 | $database->update( |
| 94 | 108 | 'funnel_contact', |
| 95 | 109 | [ $funnel->current_step_data['type']['key'] => $new_values ], |
| 96 | - [ 'id' => $_SESSION['entered_funnel_id'] ] | |
| 110 | + [ 'id' => $contact_id ] | |
| 97 | 111 | ); |
| 98 | 112 | } |
| 99 | 113 | |
| 100 | 114 | /** |
| 115 | + * Handles contact data on upsell and downsell. | |
| 116 | + * | |
| 117 | + * @param array $upsell_downsell_data Upsell and downsell data. | |
| 118 | + * @param object $database Database object. | |
| 119 | + * @since 1.9.2 | |
| 120 | + */ | |
| 121 | + public static function handle_contact_data_on_upsell_donwsell( $upsell_downsell_data, $database ) { | |
| 122 | + self::start_session(); | |
| 123 | + | |
| 124 | + if ( ! isset( $_SESSION['entered_funnel_id'] ) ) { | |
| 125 | + self::end_session(); | |
| 126 | + return; | |
| 127 | + } | |
| 128 | + | |
| 129 | + $contact_id = absint( $_SESSION['entered_funnel_id'] ); | |
| 130 | + | |
| 131 | + self::end_session(); | |
| 132 | + | |
| 133 | + $result = $database->get( 'funnel_contact', [ 'id' => $contact_id ] ); | |
| 134 | + | |
| 135 | + $old_values = []; | |
| 136 | + | |
| 137 | + if ( ! empty( $result[0][ $upsell_downsell_data['key'] ] ) ) { | |
| 138 | + $old_values = unserialize( $result[ 0 ][ $upsell_downsell_data['key'] ] ); // phpcs:ignore | |
| 139 | + } | |
| 140 | + | |
| 141 | + $new_values = array_merge( $old_values, [ $upsell_downsell_data['page_id'] ] ); | |
| 142 | + | |
| 143 | + $database->update( | |
| 144 | + 'funnel_contact', | |
| 145 | + [ $upsell_downsell_data['key'] => $new_values ], | |
| 146 | + [ 'id' => $contact_id ] | |
| 147 | + ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 101 | 151 | * Adds price to total spent column of this funnel. |
| 102 | 152 | * |
| 103 | 153 | * @param string $price Price value. |
| 104 | 154 | * @param int $funnel_id funnel id. |
| 105 | 155 | * @since 1.5.0 |
| 156 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
| 106 | 157 | */ |
| 107 | 158 | public function add_total_spent( $price, $funnel_id ) { |
| 159 | + self::start_session(); | |
| 160 | + | |
| 161 | + $contact_id = isset( $_SESSION['entered_funnel_id'] ) ? absint( $_SESSION['entered_funnel_id'] ) : 0; | |
| 162 | + | |
| 163 | + self::end_session(); | |
| 164 | + | |
| 108 | 165 | // Getting old data. |
| 109 | - $result = $this->db->get( 'funnel_contact', [ 'id' => $funnel_id ] ); | |
| 166 | + $result = $this->db->get( 'funnel_contact', [ 'id' => $contact_id ] ); | |
| 110 | 167 | $old_spent = ! empty( $result[0]['total_spent'] ) ? $result[0]['total_spent'] : 0; |
| 111 | 168 | |
| 112 | 169 | $new_spent = floatval( $old_spent ) + floatval( $price ); |
| 113 | 170 | |
| @@ -113,8 +170,30 @@ | ||
| 113 | 170 | |
| 114 | 171 | $this->db->update( |
| 115 | 172 | 'funnel_contact', |
| 116 | 173 | [ 'total_spent' => $new_spent ], |
| 117 | - [ 'id' => $funnel_id ] | |
| 174 | + [ 'id' => $contact_id ] | |
| 118 | 175 | ); |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * Starts a session if not already started. | |
| 180 | + * | |
| 181 | + * @since 2.6.0 | |
| 182 | + */ | |
| 183 | + public static function start_session() { | |
| 184 | + if ( ! headers_sent() && session_status() !== PHP_SESSION_ACTIVE ) { | |
| 185 | + session_start(); | |
| 186 | + } | |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Ends the session if it is active. | |
| 191 | + * | |
| 192 | + * @since 2.6.0 | |
| 193 | + */ | |
| 194 | + public static function end_session() { | |
| 195 | + if ( session_status() === PHP_SESSION_ACTIVE ) { | |
| 196 | + session_write_close(); | |
| 197 | + } | |
| 119 | 198 | } |
| 120 | 199 | } |