| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPFunnels\Report; |
| 4 |
|
| 5 |
|
| 6 |
use WPFunnels\Optin\Optin_Record; |
| 7 |
use WPFunnels\Wpfnl_functions; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class OptinRecorder |
| 11 |
* |
| 12 |
* Handles recording opt-in form submissions. |
| 13 |
*/ |
| 14 |
class OptinRecorder { |
| 15 |
|
| 16 |
/** |
| 17 |
* OptinRecorder constructor. |
| 18 |
* |
| 19 |
* Registers hooks. |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
add_action( 'wpfunnels/after_optin_submit', array($this, 'record_optin_submission'), 10, 5 ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Record opt-in form submission. |
| 28 |
* |
| 29 |
* @param $step_id |
| 30 |
* @param $post_action |
| 31 |
* @param $action_type |
| 32 |
* @param $record Optin_Record |
| 33 |
* @param $post_data |
| 34 |
* @return void |
| 35 |
* |
| 36 |
* @since 3.5.0 |
| 37 |
*/ |
| 38 |
public function record_optin_submission( $step_id, $post_action, $action_type, $record, $post_data ) { |
| 39 |
|
| 40 |
$funnel_id = Wpfnl_functions::get_funnel_id_from_step( $step_id ); |
| 41 |
$fields = method_exists( $record, 'get_fields' ) ? $record->get_fields() : ( isset( $record->form_data ) ? $record->form_data : array() ); |
| 42 |
$email = ''; |
| 43 |
$user_id = 0; |
| 44 |
$hash = ''; |
| 45 |
if ( $fields && is_array( $fields ) ) { |
| 46 |
foreach( $fields as $key => $value ) { |
| 47 |
if ( 'email' === $key ) { |
| 48 |
$email = $value; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if ( $email ) { |
| 54 |
$hash = $this->get_rand_hash($email); |
| 55 |
$user = get_user_by('email', $email ); |
| 56 |
if ( $user ) { |
| 57 |
$user_id = $user->ID; |
| 58 |
} |
| 59 |
|
| 60 |
// Insert the data into the database table |
| 61 |
global $wpdb; |
| 62 |
$table_name = $wpdb->prefix . 'wpfnl_optin_entries'; |
| 63 |
$wpdb->insert($table_name, array( |
| 64 |
'funnel_id' => $funnel_id, |
| 65 |
'step_id' => $step_id, |
| 66 |
'user_id' => $user_id, |
| 67 |
'email' => $email, |
| 68 |
'hash' => $hash, |
| 69 |
'data' => serialize($post_data), |
| 70 |
'date_created' => current_time('mysql'), |
| 71 |
)); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Returns alphanumeric hash |
| 79 |
* |
| 80 |
* @param $email |
| 81 |
* @param $len |
| 82 |
* @return string |
| 83 |
* |
| 84 |
* @since 3.5.0 |
| 85 |
*/ |
| 86 |
public function get_rand_hash( $email, $len = 32 ) { |
| 87 |
return substr( md5( $email ), -$len ); |
| 88 |
} |
| 89 |
} |
| 90 |
|