action_name = __( 'Create Contact', 'wp-marketing-automations-pro' ); $this->action_desc = __( 'This action creates a contact', 'wp-marketing-automations-pro' ); $this->action_priority = 5; $this->support_v2 = true; $this->required_fields = array( 'email' ); } public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Localize data for html fields for the current action. */ public function admin_enqueue_assets() { if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) { $contact_status = $this->get_view_data(); BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'contact_status', $contact_status ); } } public function get_view_data() { if ( method_exists( 'BWFAN_Common', 'get_contact_status_array_list' ) ) { $status = BWFAN_Common::get_contact_status_array_list(); $arr = [ '1' => 'Subscribed' ]; foreach ( $status as $key => $value ) { $arr[ $key ] = $value['name']; } return $arr; } return array( '1' => __( 'Subscribed', 'wp-marketing-automations-pro' ), '0' => __( 'Unverified', 'wp-marketing-automations-pro' ), '3' => __( 'Unsubscribed', 'wp-marketing-automations-pro' ), '2' => __( 'Bounced', 'wp-marketing-automations-pro' ), ); } /** * Show the html fields for the current action. */ public function get_view() { $unique_slug = $this->get_slug(); ?> set_data( $action_data['processed_data'] ); $is_required_fields_present = $this->check_fields( $this->data, $this->required_fields ); if ( false === $is_required_fields_present ) { return $this->show_fields_error(); } if ( ! is_email( $this->data['email'] ) ) { return array( 'status' => 4, 'message' => __( 'Invalid email to create contact.', 'wp-marketing-automations-pro' ), ); } $status = ( isset( $this->data['status'] ) && ! empty( $this->data['status'] ) ) ? absint( $this->data['status'] ) : 0; /** If status is unsubscribed then unset status from data */ if ( 3 === $status ) { /** Keep older one in order to change back the status from unsubscribe */ unset( $this->data['status'] ); } $contact = new BWFCRM_Contact( $this->data['email'], true, $this->data ); /** Older contact */ if ( true === $contact->already_exists ) { /** Update contact fields */ $contact->update( $this->data ); /** Update contact status */ $contact->update_status( $status ); return array( 'status' => 3, 'message' => __( 'Contact updated', 'wp-marketing-automations-pro' ), ); } if ( ! $contact->is_contact_exists() ) { return array( 'status' => 4, 'message' => __( 'Unable to create contact', 'wp-marketing-automations-pro' ), ); } /** If new status is unsubscribe for new contact */ if ( 3 === $status ) { $contact->unsubscribe(); } return array( 'status' => 3, 'message' => __( 'Contact created', 'wp-marketing-automations-pro' ), ); } public function process_v2() { if ( ! is_email( $this->data['email'] ) ) { return $this->skipped_response( __( 'Invalid email to create contact.', 'wp-marketing-automations-pro' ) ); } $status = ( isset( $this->data['status'] ) && ! empty( $this->data['status'] ) ) ? intval( $this->data['status'] ) : 0; /** in case status is unsubscribed then unset status from data */ if ( 3 === $status ) { unset( $this->data['status'] ); } $contact = new BWFCRM_Contact( $this->data['email'], true, $this->data ); if ( $contact->already_exists ) { /** Update contact fields */ $contact->update( $this->data ); /** Update contact status */ $contact->update_status( $status ); return $this->success_message( __( 'Contact updated', 'wp-marketing-automations-pro' ) ); } if ( ! $contact->is_contact_exists() ) { return $this->error_response( __( 'Unable to create contact', 'wp-marketing-automations-pro' ) ); } /** in case unsubscribe than make entry in unsubscribe table */ if ( 3 === $status ) { $contact->unsubscribe(); } return $this->success_message( __( 'Contact created', 'wp-marketing-automations-pro' ) ); } public function get_fields_schema() { $options = $this->get_view_data(); $prepared_options = BWFAN_PRO_Common::prepared_field_options( $options ); return array( array( 'id' => 'email', 'label' => __( 'Email', 'wp-marketing-automations-pro' ), 'type' => 'text_with_button', 'placeholder' => __( 'Email', 'wp-marketing-automations-pro' ), 'class' => 'bwfan-input-wrapper bwfan-field-crm_create_contact', 'description' => '', 'required' => true, 'automation_merge_tags' => true ), array( 'id' => 'first_name', 'label' => __( 'First Name', 'wp-marketing-automations-pro' ), 'type' => 'text_with_button', 'placeholder' => __( 'First Name', 'wp-marketing-automations-pro' ), 'class' => 'bwfan-input-wrapper bwfan-field-crm_create_contact', 'description' => '', 'required' => false, 'automation_merge_tags' => true ), array( 'id' => 'last_name', 'label' => __( 'Last Name', 'wp-marketing-automations-pro' ), 'type' => 'text_with_button', 'placeholder' => __( 'Last Name', 'wp-marketing-automations-pro' ), 'class' => 'bwfan-input-wrapper bwfan-field-crm_create_contact', 'description' => '', 'required' => false, 'automation_merge_tags' => true ), array( 'id' => 'contact_no', 'label' => __( 'Phone', 'wp-marketing-automations-pro' ), 'type' => 'text_with_button', 'placeholder' => __( 'Phone', 'wp-marketing-automations-pro' ), 'class' => 'bwfan-input-wrapper bwfan-field-crm_create_contact', 'description' => '', 'required' => false, 'automation_merge_tags' => true ), array( 'id' => 'status', 'label' => __( 'Status', 'woocommerce' ), 'type' => 'radio', 'placeholder' => __( 'Choose status', 'wp-marketing-automations-pro' ), 'options' => $prepared_options, 'class' => 'bwfan-input-wrapper bwfan-field-crm_create_status', 'description' => '', 'required' => true, ), ); } public function get_default_values() { return [ 'status' => '1', 'email' => '{{contact_email}}' ]; } } /** * Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown. */ return 'BWFAN_CRM_Create_Contact';