Skip to content

Commit

Permalink
add array access for leads
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeyadr committed Nov 18, 2024
1 parent 035bae6 commit 6b97f97
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions includes/class-holler-lead.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
exit;
}

class Holler_Lead {
class Holler_Lead implements ArrayAccess {

public $email = '';
public $name = '';
Expand All @@ -18,6 +18,8 @@ class Holler_Lead {
public $gdpr_consent = false;
public $request = null;

protected $data = [];

public function __construct( WP_REST_Request $request ) {

$this->name = sanitize_text_field( $request->get_param( 'name' ) );
Expand All @@ -29,38 +31,38 @@ public function __construct( WP_REST_Request $request ) {
$this->last_name = trim( $parts[1] );
}

$this->email = sanitize_email( strtolower( $request->get_param( 'email' ) ) );
$this->email = sanitize_email( strtolower( $request->get_param( 'email' ) ) );

if ( is_user_logged_in() ){
if ( is_user_logged_in() ) {

$user = wp_get_current_user();

if ( ! $this->email ){
if ( ! $this->email ) {
$this->email = $user->user_email;
}

if ( ! $this->first_name ){
if ( ! $this->first_name ) {
$this->first_name = $user->first_name;
}

if ( ! $this->last_name ){
if ( ! $this->last_name ) {
$this->last_name = $user->last_name;
}
}

if ( defined( 'GROUNDHOGG_VERSION' ) && \Groundhogg\is_a_contact( \Groundhogg\get_current_contact() ) ){
if ( defined( 'GROUNDHOGG_VERSION' ) && \Groundhogg\is_a_contact( \Groundhogg\get_current_contact() ) ) {

$contact = \Groundhogg\get_current_contact();

if ( ! $this->email ){
if ( ! $this->email ) {
$this->email = $contact->get_email();
}

if ( ! $this->first_name ){
if ( ! $this->first_name ) {
$this->first_name = $contact->get_first_name();
}

if ( ! $this->last_name ){
if ( ! $this->last_name ) {
$this->last_name = $contact->get_last_name();
}
}
Expand All @@ -71,6 +73,14 @@ public function __construct( WP_REST_Request $request ) {
$this->gdpr_consent = $request->get_param( 'gdpr_consent' ) === 'yes';
$this->message = sanitize_textarea_field( $request->get_param( 'message' ) );
$this->request = $request;

/**
* When a lead is initialized
*
* @param $lead Holler_Lead
* @param $request WP_REST_Request
*/
do_action( 'hollerbox/lead_init', $this, $request );
}

/**
Expand Down Expand Up @@ -147,4 +157,31 @@ public function get_ip() {
}


public function offsetExists( $offset ) {
return isset( $this->$offset ) || isset( $this->data[ $offset ] );
}

public function offsetGet( $offset ) {
if ( property_exists( $this, $offset ) ) {
return $this->$offset;
}

return $this->data[ $offset ];
}

public function offsetSet( $offset, $value ) {
if ( property_exists( $this, $offset ) ) {
$this->$offset = $value;
}

$this->data[ $offset ] = $value;
}

public function offsetUnset( $offset ) {
if ( property_exists( $this, $offset ) ) {
return;
}

unset( $this->data[ $offset ] );
}
}

0 comments on commit 6b97f97

Please sign in to comment.