Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
<?php
final class BWFAN_Add_To_Automation extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Add To Automation', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'Add Contact into Automation', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'email' );
$this->support_v2 = true;
$this->action_priority = 20;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_view_data() {
$automations = BWFAN_Core()->automations->get_active_v1_automation_names();
if ( empty( $automations ) ) {
return array();
}
$automations_to_return = array();
foreach ( $automations as $automation ) {
$automations_to_return[ $automation['ID'] ] = ( ! empty( $automation['meta_value'] ) ? $automation['meta_value'] : '' );
}
return $automations_to_return;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['selected_automation'] = isset( $step_data['selected_automation'] ) && ! empty( $step_data['selected_automation'] ) ? $step_data['selected_automation'] : [];
$data_to_set['cid'] = isset( $automation_data['global']['cid'] ) ? $automation_data['global']['cid'] : 0;
$data_to_set['email'] = $automation_data['global']['email'];
if ( ! is_email( $data_to_set['email'] ) && isset( $automation_data['global']['user_id'] ) ) {
$data_to_set['email'] = ( get_user_by( 'ID', $automation_data['global']['user_id'] ) )->user_email;
}
if ( empty( $data_to_set['cid'] ) && is_email( $data_to_set['email'] ) ) {
$contact = bwf_get_contact( '', $data_to_set['email'] );
$data_to_set['cid'] = $contact->get_id();
}
return $data_to_set;
}
public function process_v2() {
$selected_automations = $this->data['selected_automation'] ?? [];
$contact_id = intval( $this->data['cid'] );
if ( empty( $selected_automations ) || empty( $contact_id ) ) {
return $this->skipped_response( __( 'No Automation selected in action.', 'wp-marketing-automations-pro' ) );
}
$automation_ids = array_column( $selected_automations, 'id' );
$automation_id = $automation_ids[0] ?? null;
if ( $automation_id === null ) {
return $this->skipped_response( __( 'No Automation ID found.', 'wp-marketing-automations-pro' ) );
}
$ins = new BWFAN_Add_Contact_To_Automation_Controller( $automation_id, $contact_id );
$response = $ins->add_contact_to_automation();
if ( 200 !== $response['code'] ) {
return $this->skipped_response( __( $response['message'], 'wp-marketing-automations-pro' ) );
}
return $this->success_message( $response['message'] );
}
public function get_fields_schema() {
return [
[
'id' => 'selected_automation',
"type" => 'custom_search',
'autocompleterOption' => [
'path' => 'allowed_automations',
'slug' => 'allowed_automations',
'labelText' => __( 'Automations', 'wp-marketing-automations-pro' ),
],
'label' => __( 'Select Automation', 'wp-marketing-automations-pro' ),
'placeholder' => __( 'Select', 'wp-marketing-automations-pro' ),
"tip" => __( "Any scheduled tasks for the selected automation will be removed for the user.", 'wp-marketing-automations-pro' ),
"required" => true,
"allowFreeTextSearch" => false,
"multiple" => false,
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['selected_automation'] ) || empty( $data['selected_automation'] ) ) {
return '';
}
$automations = [];
foreach ( $data['selected_automation'] as $automation ) {
if ( ! isset( $automation['name'] ) || empty( $automation['name'] ) ) {
continue;
}
$automations[] = $automation['name'];
}
return $automations;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_Add_To_Automation';

View File

@@ -0,0 +1,318 @@
<?php
final class BWFAN_Automation_End extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'End Automation', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'End a FunnelKit Automation', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'email' );
$this->support_v2 = true;
$this->action_priority = 20;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Localize data for html fields for the current action.
*/
public function admin_enqueue_assets() {
if ( BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'automations_options', $data );
}
}
public function get_view_data() {
$automations = BWFAN_Core()->automations->get_active_v1_automation_names();
if ( empty( $automations ) ) {
return array();
}
$automations_to_return = array();
foreach ( $automations as $automation ) {
$automations_to_return[ $automation['ID'] ] = ( ! empty( $automation['meta_value'] ) ? $automation['meta_value'] : '' );
}
return $automations_to_return;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_automation = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'selected_automation')) ? data.actionSavedData.data.selected_automation : '';
#>
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Select Automation', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper bwfan-single-select" name="bwfan[{{data.action_id}}][data][selected_automation]">
<#
if(_.has(data.actionFieldsOptions, 'automations_options') && _.isObject(data.actionFieldsOptions.automations_options) ) {
_.each( data.actionFieldsOptions.automations_options, function( value, key ){
selected = (key == selected_automation) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}} (#{{key}})</option>
<# })
}
#>
</select>
<div class="clearfix bwfan_field_desc bwfan-mb10">
Any scheduled tasks for the selected automation will be removed for the user.
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['end_automation'] = $task_meta['data']['selected_automation'];
if ( isset( $task_meta['global']['phone'] ) && ! empty( $task_meta['global']['phone'] ) ) {
$data_to_set['phone'] = $task_meta['global']['phone'];
}
$data_to_set['email'] = $task_meta['global']['email'];
if ( ! is_email( $data_to_set['email'] ) && isset( $task_meta['global']['user_id'] ) ) {
$data_to_set['email'] = ( get_user_by( 'ID', $task_meta['global']['user_id'] ) )->user_email;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['end_automation'] = isset( $step_data['selected_automation'] ) && ! empty( $step_data['selected_automation'] ) ? array_column( $step_data['selected_automation'], 'id' ) : [];
$data_to_set['cid'] = isset( $automation_data['global']['cid'] ) ? $automation_data['global']['cid'] : 0;
$data_to_set['sid'] = isset( $automation_data['step_id'] ) ? $automation_data['step_id'] : 0;
if ( isset( $automation_data['global']['phone'] ) && ! empty( $automation_data['global']['phone'] ) ) {
$automation_data['phone'] = $automation_data['global']['phone'];
}
$data_to_set['email'] = $automation_data['global']['email'];
if ( ! is_email( $data_to_set['email'] ) && isset( $automation_data['global']['user_id'] ) ) {
$data_to_set['email'] = ( get_user_by( 'ID', $automation_data['global']['user_id'] ) )->user_email;
}
if ( empty( $data_to_set['cid'] ) && is_email( $data_to_set['email'] ) ) {
$contact = bwf_get_contact( '', $data_to_set['email'] );
$data_to_set['cid'] = $contact->get_id();
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$action_data['processed_data']['task_id'] = absint( $action_data['task_id'] );
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( ! is_array( $result ) ) {
$email = is_email( $this->data['email'] ) ? ' Email: ' . $this->data['email'] : '';
$phone = isset( $this->data['phone'] ) && ! empty( $this->data['phone'] ) ? ' Phone: ' . $this->data['phone'] : '';
$message = __( $result . ' tasks having were deleted successfully, for' . $email . $phone, 'autonami-automation-pro' );
return array(
'status' => 3,
'message' => $message
);
}
return array(
'status' => 4,
'message' => __( is_array( $result ) && isset( $result['message'] ) ? $result['message'] : 'Something went wrong while ending automation: ' . $this->data['automation_id'], 'wp-marketing-automations-pro' ),
);
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$automation_id = absint( $this->data['end_automation'] );
$automation_data = BWFAN_Model_Automations::get( $automation_id );
/** if automation data not found */
if ( empty( $automation_data ) ) {
return array( 'message' => 'No Automation found with ID: ' . $automation_id );
}
/** checking automation status before deleting tasks */
if ( isset( $automation_data['status'] ) && 2 === absint( $automation_data['status'] ) ) {
return array( 'message' => 'Automation with ID ' . $automation_id . ' is inactive.' );
}
$tasks = array();
if ( isset( $this->data['email'] ) && is_email( $this->data['email'] ) ) {
$email_tasks = BWFAN_Common::get_schedule_task_by_email( array( $automation_id ), $this->data['email'] );
$tasks = isset( $email_tasks[ $automation_id ] ) && ! empty( $email_tasks[ $automation_id ] ) ? $email_tasks[ $automation_id ] : $tasks;
}
if ( isset( $this->data['phone'] ) && ! empty( $this->data['phone'] ) ) {
$phone_tasks = BWFAN_Common::get_schedule_task_by_phone( array( $this->data['end_automation'] ), $this->data['phone'] );
$tasks = isset( $phone_tasks[ $automation_id ] ) && ! empty( $phone_tasks[ $automation_id ] ) ? array_replace( $tasks, $phone_tasks[ $automation_id ] ) : $tasks;
}
if ( ! is_array( $tasks ) || 0 === count( $tasks ) ) {
return array( 'message' => 'No tasks found' );
}
$delete_tasks = array();
foreach ( $tasks as $task ) {
$delete_tasks[] = absint( $task['ID'] );
}
/** Unset Current task */
if ( absint( $this->data['end_automation'] ) === absint( $this->data['automation_id'] ) && false !== ( $key = array_search( $this->data['task_id'], $delete_tasks, true ) ) ) {
unset( $delete_tasks[ $key ] );
}
if ( 0 === count( $delete_tasks ) ) {
return array( 'message' => 'No tasks found' );
}
BWFAN_Core()->tasks->delete_tasks( $delete_tasks );
return count( $delete_tasks );
}
public function process_v2() {
$end_automations = ! is_array( $this->data['end_automation'] ) ? array( $this->data['end_automation'] ) : $this->data['end_automation'];
$contact_id = absint( $this->data['cid'] );
if ( empty( $end_automations ) ) {
return $this->success_message( __( 'No End Automation selected in action', 'wp-marketing-automations-pro' ) );
}
$end_automations = array_map( 'intval', $end_automations );
foreach ( $end_automations as $automation_id ) {
/** Get automation contact data */
$automation_contact = BWFAN_PRO_Common::get_automation_contact_rows( $automation_id, $contact_id );
if ( ! is_array( $automation_contact ) || 0 === count( $automation_contact ) ) {
continue;
}
foreach ( $automation_contact as $a_contact ) {
if ( absint( $a_contact['aid'] ) === absint( $a_contact ) ) {
/** using special property to end the current automation process for a contact */
BWFAN_Common::$end_v2_current_contact_automation = true;
}
$reason = [
'type' => BWFAN_Automation_Controller::$ACTION_END,
'data' => [
'sid' => $this->data['step_id'],
'aid' => $this->automation_id
]
];
$a_contact = BWFAN_PRO_Common::set_automation_ended_reason( $reason, $a_contact );
/** set data for insert complete contact automation */
$data = [
'cid' => $a_contact['cid'],
'aid' => $a_contact['aid'],
'event' => $a_contact['event'],
's_date' => $a_contact['c_date'],
'c_date' => current_time( 'mysql', 1 ),
'data' => $a_contact['data'],
'trail' => $a_contact['trail'],
];
$format = [
'%d',// cid
'%d',// aid
'%s',// event
'%s',// s_date
'%s',// c_date
'%s',// data
'%s',// trail
];
if ( method_exists( 'BWFAN_Model_Automation_Complete_Contact', 'insert_ignore' ) ) {
BWFAN_Model_Automation_Complete_Contact::insert_ignore( $data, $format );
} else {
BWFCRM_Common::insert_ignore( 'bwfan_automation_complete_contact', $data, $format );
}
BWFAN_Model_Automation_Contact::delete( $a_contact['ID'] );
/** Update status as success for any step trail where status was waiting */
BWFAN_Model_Automation_Contact_Trail::update_all_step_trail_status_complete( $a_contact['trail'] );
BWFAN_Common::update_automation_contact_fields( $a_contact['cid'], $a_contact['aid'] );
}
}
return $this->success_message( __( 'Automation ended', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
'id' => 'selected_automation',
'type' => 'search',
'label' => __( 'Select Automation', 'wp-marketing-automations-pro' ),
'placeholder' => __( 'Select', 'wp-marketing-automations-pro' ),
"tip" => __( "Any scheduled tasks for the selected automation will be removed for the user.", 'wp-marketing-automations-pro' ),
"required" => true,
"allowFreeTextSearch" => false,
"multiple" => true,
'autocompleter' => 'automations',
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['selected_automation'] ) || empty( $data['selected_automation'] ) ) {
return '';
}
$automations = [];
foreach ( $data['selected_automation'] as $automation ) {
if ( ! isset( $automation['name'] ) || empty( $automation['name'] ) ) {
continue;
}
$automations[] = $automation['name'];
}
return $automations;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_Automation_End';

View File

@@ -0,0 +1,251 @@
<?php
final class BWFAN_CRM_Add_Contact_Note extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Add Contact Note', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action adds note to the Contact', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'title', 'body' );
$this->support_v2 = true;
$this->action_priority = 10;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
body = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'body')) ? data.actionSavedData.data.body : '';
note_type = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'note_type')) ? data.actionSavedData.data.note_type : '';
selected_title = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'title')) ? data.actionSavedData.data.title : '';
#>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Title', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text"
class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>"
name="bwfan[{{data.action_id}}][data][title]" placeholder="Title"
value="{{selected_title}}"/>
</div>
</div>
<div class="bwfan-input-form clearfix">
<label for=""
class="bwfan-label-title"><?php echo esc_html__( 'Note', 'wp-marketing-automations-pro' ); ?><?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?></label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0">
<textarea required class="bwfan-input-wrapper" rows="4"
placeholder="<?php echo esc_html__( 'Contact Note', 'wp-marketing-automations-pro' ); ?>"
name="bwfan[{{data.action_id}}][data][body]">{{body}}</textarea>
</div>
</div>
<div class="bwfan-input-form clearfix">
<label for=""
class="bwfan-label-title"><?php echo esc_html__( 'Note Type', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper bwfan-single-select"
name="bwfan[{{data.action_id}}][data][note_type]">
<option {{ note_type===
'billing' ? 'selected' : '' }}
value="billing"><?php echo esc_html__( 'Billing', 'woocommerce' ); ?></option>
<option {{ note_type===
'shipping' ? 'selected' : '' }}
value="shipping"><?php echo esc_html__( 'Shipping', 'woocommerce' ); ?></option>
<option {{ note_type===
'refund' ? 'selected' : '' }}
value="refund"><?php echo esc_html__( 'Refund', 'woocommerce' ); ?></option>
<option {{ note_type===
'subscription' ? 'selected' : '' }}
value="subscription"><?php echo esc_html__( 'Subscription', 'wp-marketing-automations-pro' ); ?></option>
<option {{ note_type===
'feedback' ? 'selected' : '' }}
value="feedback"><?php echo esc_html__( 'Feedback', 'wp-marketing-automations-pro' ); ?></option>
<option {{ note_type===
'others' ? 'selected' : '' }}
value="others"><?php echo esc_html__( 'Others', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$this->set_data_for_merge_tags( $task_meta );
$title = $task_meta['data']['title'];
$body = $task_meta['data']['body'];
$note_type = $task_meta['data']['note_type'];
$title = BWFAN_Common::decode_merge_tags( $title );
$body = BWFAN_Common::decode_merge_tags( $body );
$data_to_set = array();
$data_to_set['body'] = $body;
$data_to_set['type'] = $note_type;
$data_to_set['title'] = $title;
foreach ( $task_meta['global'] as $key1 => $value1 ) {
$data_to_set[ $key1 ] = $value1;
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$title = $step_data['title'];
$body = $step_data['body'];
$note_type = $step_data['note_type'];
$title = BWFAN_Common::decode_merge_tags( $title );
$body = BWFAN_Common::decode_merge_tags( $body );
$data_to_set = array();
$data_to_set['body'] = $body;
$data_to_set['type'] = $note_type;
$data_to_set['title'] = $title;
$data_to_set['email'] = ( isset( $automation_data['global']['email'] ) && is_email( $automation_data['global']['email'] ) ) ? $automation_data['global']['email'] : '';
foreach ( $automation_data['global'] as $key1 => $value1 ) {
$data_to_set[ $key1 ] = $value1;
}
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
$result = $this->process();
if ( false === $result ) {
return array(
'status' => 4,
'message' => '',
);
}
return array(
'status' => 3,
'message' => __( 'Contact note added', 'wp-marketing-automations-pro' ),
);
}
/**
* Process and do the actual processing for the current action.
* This function is present in every action class.
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$email = BWFAN_Merge_Tag_Loader::get_data( 'email' );
$contact = new BWFCRM_Contact( $email, true );
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Contact not exist', 'wp-marketing-automations-pro' ),
);
}
return $contact->add_note_to_contact( $this->data );
}
public function process_v2() {
if ( ! is_email( $this->data['email'] ) ) {
return $this->skipped_response( __( 'Required field email is missing.', 'wp-marketing-automations-pro' ) );
}
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Contact not exist.', 'wp-marketing-automations-pro' ) );
}
$contact->add_note_to_contact( $this->data );
return $this->success_message( 'Contact note added.', 'wp-marketing-automations-pro' );
}
public function get_fields_schema() {
$note_types = BWFAN_Common::get_contact_notes_type();
$def = array(
array(
'value' => '',
'label' => __( 'Select', 'wp-marketing-automations-pro' ),
)
);
$note_types = array_merge( $def, $note_types );
return [
[
'id' => 'title',
'type' => 'text',
'label' => __( 'Title', 'wp-marketing-automations-pro' ),
'placeholder' => __( 'Title', 'wp-marketing-automations-pro' ),
'tip' => "",
"description" => "",
"required" => true,
],
[
'id' => 'body',
'type' => 'textarea',
'label' => __( 'Note', 'wp-marketing-automations-pro' ),
'placeholder' => __( 'Note', 'wp-marketing-automations-pro' ),
'tip' => "",
"description" => "",
"required" => true,
],
[
'id' => 'note_type',
'type' => 'wp_select',
'label' => __( 'Note Type', 'wp-marketing-automations-pro' ),
'options' => $note_types,
'placeholder' => __( 'Choose Note Type', 'wp-marketing-automations-pro' ),
'tip' => "",
"description" => "",
"required" => true,
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['body'] ) || empty( $data['body'] ) ) {
return '';
}
return $data['body'];
}
}
return 'BWFAN_CRM_Add_Contact_Note';

View File

@@ -0,0 +1,357 @@
<?php
final class BWFAN_CRM_Add_Tag extends BWFAN_Action {
private static $instance = null;
private function __construct() {
$this->action_name = __( 'Add Tags', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action adds the selected tags to the contact', 'wp-marketing-automations-pro' );
$this->action_priority = 5;
$this->support_v2 = true;
$this->required_fields = array( 'tags', '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' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'tags_options', $data );
}
}
public function get_view_data() {
$bwfcrm_tags = BWFCRM_Tag::get_tags();
$tags = array();
if ( empty( $bwfcrm_tags ) ) {
return $tags;
}
foreach ( $bwfcrm_tags as $key => $tag ) {
$tags[ $tag['ID'] ] = $tag['name'];
}
return $tags;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_tags = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'tags')) ? data.actionSavedData.data.tags : {};
if(_.has(data.actionFieldsOptions, 'tags_options') && _.isObject(data.actionFieldsOptions.tags_options) ) {
tags_options_clone = data.actionFieldsOptions.tags_options;
if( _.size(selected_tags) > 0 ) {
diffTags = _.difference(selected_tags,_.keys(tags_options_clone));
if(_.size(diffTags) > 0) {
_.each( diffTags, function( value, key ){
tags_options_clone[value] = value;
});
}
}
}
#>
<label for="" class="bwfan-label-title">
<?php
echo esc_html__( 'Add Tags', 'wp-marketing-automations-pro' );
$message = __( 'Add available tags.', 'wp-marketing-automations-pro' );
echo $this->add_description( $message, '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan_add_tags">
<div class="bwfan_tags_wrap">
<input list="tags" type="text" id="new-tag-bwfan_tag" class="bwfan-input-wrapper" autocomplete="on">
<input type="button" class="button bwfan-tag-add" value="Add">
</div>
<ul class="tagchecklist" role="list"></ul>
<select style="display: none" name="bwfan[{{data.action_id}}][data][tags][]" multiple class="bwfan_add_tags_final_value" data-name="tags" data-action="<?php echo esc_attr( $unique_slug ) ?>">
</select>
</div>
</script>
<?php
}
/**
* Overrides the parent class method to return new array type values
*
* @param $dynamic_array
* @param $integration_data
*
* @return array
*/
public function parse_merge_tags( $dynamic_array, $integration_data ) {
return $this->parse_tags_fields( $dynamic_array, $integration_data );
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object BWFAN_Integration
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$this->is_action_tag = true;
$final_tags = [];
$user_input_tags = isset( $task_meta['data']['tags'] ) ? $task_meta['data']['tags'] : [];
$data_to_set = array();
$data_to_set['email'] = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
$data_to_set['user_id'] = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $task_meta['global']['phone'] ) ? $task_meta['global']['phone'] : '';
$db_tags = $this->get_view_data();
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
if ( ! empty( $user_input_tags ) ) {
foreach ( $user_input_tags as $tag_value ) {
$tags_response = BWFAN_Common::decode_merge_tags( $tag_value );
$tags = json_decode( $tags_response );
if ( is_array( $tags ) && count( $tags ) > 0 ) {
foreach ( $tags as $single_tag ) {
$final_tags[] = $single_tag;
}
continue;
}
$final_tags[] = $tags_response;
}
}
$final_tag_data = array();
foreach ( $final_tags as $fkey => $final_tag ) {
/** checking if the id already there than passing the id and value in array */
if ( isset( $db_tags[ $final_tag ] ) && ! empty( $db_tags[ $final_tag ] ) ) {
$final_tag_data[ $fkey ] = array( 'id' => $final_tag, 'value' => $db_tags[ $final_tag ] );
unset( $final_tags[ $fkey ] );
continue;
}
$final_tag_data[ $fkey ] = array( 'id' => 0, 'value' => $final_tag );
unset( $final_tags[ $fkey ] );
}
$data_to_set['tags'] = $final_tag_data;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$this->is_action_tag = true;
$final_tags = [];
$user_input_tags = isset( $step_data['tags'] ) && is_array( $step_data['tags'] ) ? $step_data['tags'] : [];
$data_to_set = array();
$data_to_set['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$data_to_set['phone'] = isset( $automation_data['global']['phone'] ) ? $automation_data['global']['phone'] : '';
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
$final_tags = array_map( function ( $tag ) {
$tag['value'] = BWFAN_Common::decode_merge_tags( $tag['name'] );
unset( $tag['name'] );
return $tag;
}, $user_input_tags );
$data_to_set['tags'] = $final_tags;
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->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();
}
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ),
);
}
}
$tags = $this->data['tags'];
$tags_added = $contact->add_tags( $tags );
/** in case getting wp_error instance then return the error message **/
if ( $tags_added instanceof WP_Error ) {
return array(
'status' => 4,
'message' => __( $tags_added->get_error_message(), 'wp-marketing-automations-pro' ),
);
}
if ( empty( $tags_added ) ) {
return array(
'status' => 3,
'message' => __( 'All provided tags are applied already.', 'wp-marketing-automations-pro' ),
);
}
if ( count( $tags ) !== count( $tags_added ) ) {
$applied_tags = array_map( function ( $tag ) {
return $tag->get_name();
}, $tags_added );
$applied_tags = implode( ', ', $applied_tags );
return array(
'status' => 3,
'message' => __( 'Some tags are applied already. These tags are now applied: ' . $applied_tags, 'wp-marketing-automations-pro' ),
);
}
return array(
'status' => 3,
'message' => __( 'All tags are added', 'wp-marketing-automations-pro' ),
);
}
public function process_v2() {
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** Get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ) );
}
}
/** Checking for comma in tag values */
$tags = BWFAN_PRO_Common::check_for_comma_seperated( $this->data['tags'] );
$tags_added = $contact->add_tags( $tags );
/** in case getting wp_error instance then return the error message **/
if ( $tags_added instanceof WP_Error ) {
return $this->error_response( $tags_added->get_error_message() );
}
if ( empty( $tags_added ) ) {
return $this->success_message( __( 'Tags are already applied.', 'wp-marketing-automations-pro' ) );
}
if ( count( $tags ) !== count( $tags_added ) ) {
$applied_tags = array_map( function ( $tag ) {
return $tag->get_name();
}, $tags_added );
$applied_tags = implode( ', ', $applied_tags );
return $this->success_message( __( 'Some tags are applied already. These tags are now applied: ' . $applied_tags, 'wp-marketing-automations-pro' ) );
}
return $this->success_message( __( 'All tags are added', 'wp-marketing-automations-pro' ) );
}
/** make new tag in case of not found in db **/
public function make_new_tags( $new_tags_to_make, $action_data ) {
if ( is_array( $new_tags_to_make ) && 0 < count( $new_tags_to_make ) ) {
foreach ( $new_tags_to_make as $new_tag_key => $new_tag_name ) {
$bwfcrm_term = new BWFCRM_Terms( $new_tag_name, 0 );
if ( ! $bwfcrm_term instanceof BWFCRM_Terms ) {
continue;
}
$action_data['processed_data']['new_tags'][ $new_tag_key ] = $bwfcrm_term->get_id();
}
}
return $action_data['processed_data']['new_tags'];
}
public function get_fields_schema() {
return [
[
"id" => 'tags',
"label" => __( 'Add Tags', 'wp-marketing-automations-pro' ),
"type" => 'search',
'autocompleter' => 'tags',
"class" => "bwfan-col-sm-9",
"tip" => __( "Can add new or available tags.", 'wp-marketing-automations-pro' ),
"hint" => __( "Note: Text with comma will break the word and add individually", "wp-marketing-automations-pro" ),
"allowFreeTextSearch" => true,
"required" => true,
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['tags'] ) || empty( $data['tags'] ) ) {
return '';
}
$tags = [];
foreach ( $data['tags'] as $tag ) {
if ( ! isset( $tag['name'] ) || empty( $tag['name'] ) ) {
continue;
}
$tags[] = $tag['name'];
}
return $tags;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_CRM_Add_Tag';

View File

@@ -0,0 +1,269 @@
<?php
final class BWFAN_CRM_Add_To_List extends BWFAN_Action {
private static $instance = null;
private function __construct() {
$this->action_name = __( 'Add Contact to List', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action adds a contact to the selected list', 'wp-marketing-automations-pro' );
$this->action_priority = 15;
$this->support_v2 = true;
$this->required_fields = array( 'lists', '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' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'list_id_options', $data );
}
}
public function get_view_data() {
$bwfcrm_lists = BWFCRM_Lists::get_lists();
$lists = array();
if ( empty( $bwfcrm_lists ) ) {
return $lists;
}
foreach ( $bwfcrm_lists as $key => $list ) {
$lists[ $list['ID'] ] = $list['name'];
}
return $lists;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_list_id = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'list_id')) ? data.actionSavedData.data.list_id : '';
#>
<label for="" class="bwfan-label-title">
<?php
echo esc_html__( 'Select List', 'wp-marketing-automations-pro' );
$message = __( 'Select list to add contact to and if unable to locate then sync the connector.', 'wp-marketing-automations-pro' );
echo $this->add_description( $message, '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<select required id="" class="bwfan-input-wrapper bwfan-single-select" name="bwfan[{{data.action_id}}][data][list_id][]">
<option value=""><?php echo esc_html__( 'Choose A List', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'list_id_options') && _.isObject(data.actionFieldsOptions.list_id_options) ) {
_.each( data.actionFieldsOptions.list_id_options, function( value, key ){
selected = _.contains(selected_list_id, key) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object BWFAN_Integration
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$user_input_lists = isset( $task_meta['data']['list_id'] ) ? $task_meta['data']['list_id'] : [];
$data_to_set['email'] = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
$data_to_set['user_id'] = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $task_meta['global']['phone'] ) ? $task_meta['global']['phone'] : '';
$db_lists = $this->get_view_data();
$final_list_data = array();
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
foreach ( $user_input_lists as $fkey => $input_list ) {
/** checking if the id already there than passing the id and value in array */
if ( isset( $db_lists[ $input_list ] ) && ! empty( $db_lists[ $input_list ] ) ) {
$final_list_data[ $fkey ] = array( 'id' => $input_list, 'value' => $db_lists[ $input_list ] );
unset( $user_input_lists[ $fkey ] );
continue;
}
}
$data_to_set['lists'] = $final_list_data;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$user_input_lists = isset( $step_data['list_id'] ) && is_array( $step_data['list_id'] ) ? $step_data['list_id'] : [];
$data_to_set['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $automation_data['global']['phone'] ) ? $automation_data['global']['phone'] : '';
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
$db_lists = $this->get_view_data();
$final_list_data = array_map( function ( $list ) {
$list['value'] = BWFAN_Common::decode_merge_tags( $list['name'] );
unset( $list['name'] );
return $list;
}, $user_input_lists );
$data_to_set['lists'] = $final_list_data;
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->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();
}
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ),
);
}
}
$lists = $this->data['lists'];
/** adding lists to contact **/
$lists_added = $contact->add_lists( $lists );
if ( $lists_added instanceof WP_Error ) {
return array(
'status' => 4,
'message' => __( $lists_added->get_error_message(), 'wp-marketing-automations-pro' ),
);
}
if ( empty( $lists_added ) ) {
return array(
'status' => 3,
'message' => __( 'List applied already.', 'wp-marketing-automations-pro' ),
);
}
return array(
'status' => 3,
'message' => __( 'list added', 'wp-marketing-automations-pro' ),
);
}
public function process_v2() {
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ) );
}
}
/** Checking for comma in tag values */
$lists = BWFAN_PRO_Common::check_for_comma_seperated( $this->data['lists'] );
/** adding lists to contact **/
$lists_added = $contact->add_lists( $lists );
if ( $lists_added instanceof WP_Error ) {
return $this->error_response( $lists_added->get_error_message() );
}
if ( empty( $lists_added ) ) {
$this->success_message( __( 'List already assigned.', 'wp-marketing-automations-pro' ) );
}
return $this->success_message( __( 'List assigned successfully.', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
"id" => 'list_id',
"label" => __( 'Add list', 'wp-marketing-automations-pro' ),
"type" => 'search',
'autocompleter' => 'lists',
"class" => "bwfan-col-sm-9",
"tip" => __( "Can add new or available lists.", 'wp-marketing-automations-pro' ),
"hint" => __( "Note: Text with comma will break the word and add individually", "wp-marketing-automations-pro" ),
"allowFreeTextSearch" => true,
"required" => true,
],
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['list_id'] ) || empty( $data['list_id'] ) ) {
return '';
}
$lists = [];
foreach ( $data['list_id'] as $list ) {
if ( ! isset( $list['name'] ) || empty( $list['name'] ) ) {
continue;
}
$lists[] = $list['name'];
}
return $lists;
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_CRM_Add_To_List';

View File

@@ -0,0 +1,258 @@
<?php
final class BWFAN_CRM_Change_Contact_Status extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Change Status', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action changes the Contact status', 'wp-marketing-automations-pro' );
$this->required_fields = array( 'email', 'status' );
$this->action_priority = 50;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Localize data for html fields for the current action.
*/
public function admin_enqueue_assets() {
if ( false === BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
return;
}
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'status_options', $data );
}
public function get_view_data() {
if ( method_exists( 'BWFAN_Common', 'get_contact_status_array_list' ) ) {
$status = BWFAN_Common::get_contact_status_array_list();
$arr = [];
foreach ( $status as $key => $value ) {
$arr[ $key ] = $value['name'];
}
return $arr;
}
return array(
'0' => 'Unverified',
'1' => 'Subscribed',
'3' => 'Unsubscribed',
'2' => 'Bounced',
);
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
?>
<script type="text/html" id="tmpl-action-<?php echo esc_html__( $this->get_slug() ); ?>">
<#
selected_status = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'status')) ? data.actionSavedData.data.status : '';
#>
<div data-element-type="bwfan-select" class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?> bwfan-mb-15">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Status', 'woocommerce' ); ?></label>
<select data-element-type="bwfan-select" required id="" class="bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][status]">
<option value=""><?php echo esc_html__( 'Select', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'status_options') &&
_.isObject(data.actionFieldsOptions.status_options) ) {
_.each( data.actionFieldsOptions.status_options, function( value, key ){
selected = (key == selected_status) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data['status'] = isset( $task_meta['data']['status'] ) ? $task_meta['data']['status'] : 0;
$data['contact_id'] = isset( $task_meta['global']['contact_id'] ) ? $task_meta['global']['contact_id'] : 0;
$data['user_id'] = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : 0;
$data['email'] = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
/** check if email not exists in global then get from user is if available */
if ( empty( $data['email'] ) && ! empty( $data['user_id'] ) ) {
$user_data = get_userdata( $data['user_id'] );
$data['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
return $data;
}
public function make_v2_data( $automation_data, $step_data ) {
$data['status'] = isset( $step_data['status'] ) ? $step_data['status'] : 0;
$data['contact_id'] = isset( $automation_data['global']['contact_id'] ) ? $automation_data['global']['contact_id'] : 0;
$data['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
$data['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
/** check if email not exists in global then get from user is if available */
if ( empty( $data['email'] ) && ! empty( $data['user_id'] ) ) {
$user_data = get_userdata( $data['user_id'] );
$data['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
return $data;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->set_data( $action_data['processed_data'] );
return $this->process();
}
/**
* Change Contact status.
*
* @return array|string|string[]
*/
public function process() {
$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
if ( false === $is_required_fields_present ) {
return $this->show_fields_error();
}
$id_or_email = isset( $this->data['contact_id'] ) && absint( $this->data['contact_id'] ) > 0 ? $this->data['contact_id'] : $this->data['email'];
$status = absint( $this->data['status'] );
$contact = new BWFCRM_Contact( $id_or_email );
/** check contact exists */
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Contact does not exists with ' . $this->data['email'], 'wp-marketing-automations-pro' ),
);
}
$old_status = $contact->contact->get_status();
/** @var checking $unsubscribe_status of the contact */
$unsubscribe_status = $contact->check_contact_unsubscribed();
if ( ! empty( $unsubscribe_status['ID'] ) && absint( $unsubscribe_status['ID'] ) > 0 ) {
$old_status = 3;
}
/** check if old status already set */
if ( absint( $status ) === absint( $old_status ) ) {
$statuses = $this->get_view_data();
$status_name = isset( $statuses[ strval( $old_status ) ] ) ? $statuses[ strval( $old_status ) ] : $old_status;
return array(
'status' => 3,
'message' => __( 'Contact already has status: ' . $status_name . '.', 'wp-marketing-automations-pro' ),
);
}
if ( 3 === $status ) {
$contact->unsubscribe();
} else {
$contact->remove_unsubscribe_status();
$contact->contact->set_status( $status );
$contact->save();
}
return array(
'status' => 3,
'message' => __( 'Contact status changed.', 'wp-marketing-automations-pro' ),
);
}
public function process_v2() {
$id_or_email = isset( $this->data['contact_id'] ) && absint( $this->data['contact_id'] ) > 0 ? $this->data['contact_id'] : $this->data['email'];
$status = intval( $this->data['status'] );
$contact = new BWFCRM_Contact( $id_or_email );
/** check contact exists */
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Contact does not exists with ' . $this->data['email'], 'wp-marketing-automations-pro' ) );
}
$old_status = $contact->contact->get_status();
/** check if old status already set */
if ( $status === intval( $old_status ) ) {
$statuses = $this->get_view_data();
$status_name = isset( $statuses[ strval( $old_status ) ] ) ? $statuses[ strval( $old_status ) ] : $old_status;
$this->success_message( __( 'Contact already has status: ' . $status_name . '.', 'wp-marketing-automations-pro' ) );
}
$contact->update_status( $status );
return $this->success_message( __( 'Contact status changed.', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
$status = $this->get_view_data();
$status = BWFAN_PRO_Common::prepared_field_options( $status );
return [
[
'id' => 'status',
'type' => 'wp_select',
'label' => __( 'Status', 'woocommerce' ),
'options' => $status,
'placeholder' => __( 'Choose Status', 'wp-marketing-automations-pro' ),
'tip' => "",
"description" => "",
"required" => true,
]
];
}
/** set default values */
public function get_default_values() {
return [
'status' => 1,
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
$nice_name = $this->get_view_data();
if ( ! isset( $data['status'] ) || empty( $data['status'] ) ) {
return $nice_name[0];
}
return $nice_name[ $data['status'] ];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_CRM_Change_Contact_Status';

View File

@@ -0,0 +1,341 @@
<?php
final class BWFAN_CRM_Create_Contact extends BWFAN_Action {
private static $instance = null;
private function __construct() {
$this->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();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_first_name = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'first_name')) ? data.actionSavedData.data.first_name : '';
selected_last_name = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'last_name')) ? data.actionSavedData.data.last_name : '';
selected_email = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'email')) ? data.actionSavedData.data.email : '';
selected_phone = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'phone')) ? data.actionSavedData.data.phone : '';
selected_status = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'status')) ? data.actionSavedData.data.status : 1;
#>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Email', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text" class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>" name="bwfan[{{data.action_id}}][data][email]" placeholder="Email" value="{{selected_email}}"/>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'First Name (optional)', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text" class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>" name="bwfan[{{data.action_id}}][data][first_name]" placeholder="First Name" value="{{selected_first_name}}"/>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Last Name (optional)', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text" class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>" name="bwfan[{{data.action_id}}][data][last_name]" placeholder="Last Name" value="{{selected_last_name}}"/>
</div>
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Phone (optional)', 'wp-marketing-automations-pro' ); ?>
<?php echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput ?>
</label>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan-pr-0 bwfan-mb-15">
<input required type="text" class="bwfan-input-wrapper bwfan-field-<?php esc_html_e( $unique_slug ); ?>" name="bwfan[{{data.action_id}}][data][phone]" placeholder="Phone" value="{{selected_phone}}"/>
</div>
<div data-element-type="bwfan-select" class="bwfan-<?php echo esc_html__( $this->get_slug() ); ?> bwfan-mb-15">
<label for="" class="bwfan-label-title"><?php echo esc_html__( 'Status', 'woocommerce' ); ?></label>
<select data-element-type="bwfan-select" required id="" class="bwfan-input-wrapper" name="bwfan[{{data.action_id}}][data][status]">
<#
if(_.has(data.actionFieldsOptions, 'contact_status') && _.isObject(data.actionFieldsOptions.contact_status) ) {
_.each( data.actionFieldsOptions.contact_status, function( value, key ){
selected = (key == selected_status) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</div>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object BWFAN_Integration
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$fname = isset( $task_meta['data']['first_name'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['first_name'] ) : '';
if ( ! empty( $fname ) ) {
$data_to_set['f_name'] = $fname;
}
$lname = isset( $task_meta['data']['last_name'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['last_name'] ) : '';
if ( ! empty( $lname ) ) {
$data_to_set['l_name'] = $lname;
}
$contact_no = isset( $task_meta['data']['phone'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['phone'] ) : '';
if ( ! empty( $contact_no ) ) {
$data_to_set['contact_no'] = $contact_no;
}
$email = isset( $task_meta['data']['email'] ) ? BWFAN_Common::decode_merge_tags( $task_meta['data']['email'] ) : '';
if ( ! empty( $email ) ) {
$data_to_set['email'] = $email;
}
$data_to_set['status'] = isset( $task_meta['data']['status'] ) ? $task_meta['data']['status'] : 0;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$fname = isset( $step_data['first_name'] ) ? BWFAN_Common::decode_merge_tags( $step_data['first_name'] ) : '';
if ( ! empty( $fname ) ) {
$data_to_set['f_name'] = $fname;
}
$lname = isset( $step_data['last_name'] ) ? BWFAN_Common::decode_merge_tags( $step_data['last_name'] ) : '';
if ( ! empty( $lname ) ) {
$data_to_set['l_name'] = $lname;
}
$email = isset( $step_data['email'] ) ? BWFAN_Common::decode_merge_tags( $step_data['email'] ) : '';
if ( ! empty( $email ) ) {
$data_to_set['email'] = $email;
}
$contact_no = isset( $step_data['contact_no'] ) ? BWFAN_Common::decode_merge_tags( $step_data['contact_no'] ) : '';
if ( ! empty( $contact_no ) ) {
$data_to_set['contact_no'] = $contact_no;
}
$data_to_set['status'] = isset( $step_data['status'] ) ? $step_data['status'] : 0;
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->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';

View File

@@ -0,0 +1,63 @@
<?php
final class BWFAN_CRM_Delete_Contact extends BWFAN_Action {
private static $ins = null;
protected function __construct() {
$this->action_name = __( 'Delete Contact', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action deletes the Contact', 'wp-marketing-automations-pro' );
$this->action_priority = 50;
$this->support_v2 = true;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function make_v2_data( $automation_data, $step_data ) {
$data['contact_id'] = isset( $automation_data['global']['contact_id'] ) ? $automation_data['global']['contact_id'] : 0;
$data['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
return $data;
}
public function process_v2() {
$id_or_email = isset( $this->data['contact_id'] ) && absint( $this->data['contact_id'] ) > 0 ? $this->data['contact_id'] : $this->data['email'];
$contact = new BWFCRM_Contact( $id_or_email );
if ( ! $contact->is_contact_exists() ) {
return $this->success_message( __( 'Contact not found', 'wp-marketing-automations-pro' ) );
}
$status = BWFCRM_Model_Contact::delete_contact( $contact->get_id() );
if ( ! $status ) {
$this->response_code = 404;
return $this->error_response( __( 'Error occurred during contact deletion', 'wp-marketing-automations-pro' ) );
}
BWFAN_Common::$end_v2_current_contact_automation = true;
return $this->success_message( __( 'Contact deleted', 'wp-marketing-automations-pro' ) );
}
public function get_fields_schema() {
return [
[
'id' => 'title',
'type' => 'para',
"children" => __( 'Note: This action will delete the contact and all related data, including engagements and conversions. If the contact is active in any automation, it will also be removed.', 'wp-marketing-automations-pro' ),
],
];
}
}
/**
* Register this action. Registering the action will make it eligible to see it on single automation screen in select actions dropdown.
*/
return 'BWFAN_CRM_Delete_Contact';

View File

@@ -0,0 +1,261 @@
<?php
final class BWFAN_CRM_Rmv_From_List extends BWFAN_Action {
private static $instance = null;
private function __construct() {
$this->action_name = __( 'Remove Contact From List', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action removes a contact from the selected list', 'wp-marketing-automations-pro' );
$this->action_priority = 20;
$this->support_v2 = true;
$this->required_fields = array( 'lists', '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' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'list_id_options', $data );
}
}
public function get_view_data() {
$lists = BWFCRM_Common::get_all_lists();
return $lists;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_list_id = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'list_id')) ? data.actionSavedData.data.list_id : '';
#>
<label for="" class="bwfan-label-title">
<?php
echo esc_html__( 'Select List', 'wp-marketing-automations-pro' );
$message = __( 'Select list to remove contact from and if unable to locate then sync the connector..', 'wp-marketing-automations-pro' );
echo $this->add_description( $message, '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<select required id="" class="bwfan-input-wrapper bwfan-single-select" name="bwfan[{{data.action_id}}][data][list_id][]">
<option value=""><?php echo esc_html__( 'Choose A List', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'list_id_options') && _.isObject(data.actionFieldsOptions.list_id_options) ) {
_.each( data.actionFieldsOptions.list_id_options, function( value, key ){
selected = _.contains(selected_list_id, key) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object BWFAN_Integration
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['lists'] = isset( $task_meta['data']['list_id'] ) ? $task_meta['data']['list_id'] : [];
$data_to_set['email'] = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
$data_to_set['user_id'] = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $task_meta['global']['phone'] ) ? $task_meta['global']['phone'] : '';
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$user_input_lists = isset( $step_data['list_id'] ) && is_array( $step_data['list_id'] ) ? $step_data['list_id'] : [];
$data_to_set['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $automation_data['global']['phone'] ) ? $automation_data['global']['phone'] : '';
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
$data_to_set['lists'] = array_map( function ( $list ) {
if ( absint( $list['id'] ) > 0 ) {
return $list['id'];
}
/**If mergetag in list name */
$list['name'] = BWFAN_Common::decode_merge_tags( $list['name'] );
$list = BWFAN_PRO_Common::get_or_create_terms( [ $list ], BWFCRM_Term_Type::$LIST );
return isset( $list[0]['id'] ) ? absint( $list[0]['id'] ) : 0;
}, $user_input_lists );
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->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();
}
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ),
);
}
}
$lists_removed = $contact->remove_lists( $this->data['lists'] );
$contact->save();
$not_removed_lists = array_filter( array_values( array_diff( $this->data['lists'], $lists_removed ) ) );
$not_removed_terms = array();
if ( ! empty( $not_removed_lists ) ) {
$not_removed_terms = BWFCRM_Term::get_terms( 2, $not_removed_lists );
}
$nice_names = array_map( function ( $tag ) {
if ( isset( $tag['name'] ) ) {
return $tag['name'];
}
}, $not_removed_terms );
$list_message = is_array( $nice_names ) ? implode( ',', $nice_names ) : '';
if ( ! empty( $list_message ) ) {
$message = 'Not all List removed, except of ' . $list_message;
} else {
$message = 'All Lists removed';
}
return array(
'status' => 3,
'message' => __( $message, 'autonami-automation-connector' ),
);
}
public function process_v2() {
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ) );
}
}
$lists_removed = $contact->remove_lists( $this->data['lists'] );
$contact->save();
$not_removed_lists = array_filter( array_values( array_diff( $this->data['lists'], $lists_removed ) ) );
$not_removed_terms = array();
if ( ! empty( $not_removed_lists ) ) {
$not_removed_terms = BWFCRM_Term::get_terms( 2, $not_removed_lists );
}
$nice_names = array_map( function ( $tag ) {
if ( isset( $tag['name'] ) ) {
return $tag['name'];
}
}, $not_removed_terms );
$list_message = is_array( $nice_names ) ? implode( ',', $nice_names ) : '';
if ( ! empty( $list_message ) ) {
$message = __( 'Not all List removed, except of ' . $list_message, 'wp-marketing-automations-pro' );
} else {
$message = __( 'All Lists removed', 'wp-marketing-automations-pro' );
}
return $this->success_message( $message );
}
public function get_fields_schema() {
return [
[
'id' => 'list_id',
"label" => __( 'Select list', 'wp-marketing-automations-pro' ),
"type" => 'search',
'autocompleter' => 'lists',
"class" => "bwfan-col-sm-9",
"tip" => __( "Can add available lists.", 'wp-marketing-automations-pro' ),
"allowFreeTextSearch" => true,
"required" => true,
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['list_id'] ) || empty( $data['list_id'] ) ) {
return '';
}
$lists = [];
foreach ( $data['list_id'] as $list ) {
if ( ! isset( $list['name'] ) || empty( $list['name'] ) ) {
continue;
}
$lists[] = $list['name'];
}
return $lists;
}
}
return 'BWFAN_CRM_Rmv_From_List';

View File

@@ -0,0 +1,346 @@
<?php
final class BWFAN_CRM_Rmv_Tag extends BWFAN_Action {
private static $instance = null;
private function __construct() {
$this->action_name = __( 'Remove Tags', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action removes the selected tags from the contact', 'wp-marketing-automations-pro' );
$this->action_priority = 10;
$this->support_v2 = true;
$this->required_fields = array( 'remove_tags', 'email' );
}
/**
* Ensures only one instance of the class is loaded or can be loaded.
*
* @return BWFAN_BWFCRM_Rmv_Tag|null
*/
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' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'tags_options', $data );
}
}
public function get_view_data() {
$bwfcrm_tags = BWFCRM_Tag::get_tags();
$tags = array();
if ( empty( $bwfcrm_tags ) ) {
return $tags;
}
foreach ( $bwfcrm_tags as $key => $tag ) {
$tags[ $tag['ID'] ] = $tag['name'];
}
return $tags;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<#
selected_tags = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data, 'tags')) ? data.actionSavedData.data.tags : {};
if(_.has(data.actionFieldsOptions, 'tags_options') && _.isObject(data.actionFieldsOptions.tags_options) ) {
tags_options_clone = data.actionFieldsOptions.tags_options;
if( _.size(selected_tags) > 0 ) {
diffTags = _.difference(selected_tags,_.keys(tags_options_clone));
if(_.size(diffTags) > 0) {
_.each( diffTags, function( value, key ){
tags_options_clone[value] = value;
});
}
}
}
#>
<label for="" class="bwfan-label-title">
<?php
echo esc_html__( 'Select Tags', 'wp-marketing-automations-pro' );
$message = __( 'Select available tags and if unable to locate then sync the connector.', 'wp-marketing-automations-pro' );
echo $this->add_description( $message, '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="bwfan_add_tags">
<div class="bwfan_tags_wrap">
<input list="tags" type="text" id="new-tag-bwfan_tag" class="bwfan-input-wrapper" autocomplete="on">
<input type="button" class="button bwfan-tag-add" value="Add">
</div>
<ul class="tagchecklist" role="list"></ul>
<select style="display: none" name="bwfan[{{data.action_id}}][data][tags][]" multiple class="bwfan_add_tags_final_value" data-name="tags" data-action="<?php echo esc_attr( $unique_slug ) ?>">
</select>
</div>
</script>
<?php
}
/**
* Overrides the parent class method to return new array type values
*
* @param $dynamic_array
* @param $integration_data
*
* @return array
*/
public function parse_merge_tags( $dynamic_array, $integration_data ) {
return $this->parse_tags_fields( $dynamic_array, $integration_data );
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object BWFAN_Integration
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$this->is_action_tag = true;
$final_tags = [];
$user_input_tags = isset( $task_meta['data']['tags'] ) ? $task_meta['data']['tags'] : [];
$data_to_set = array();
$data_to_set['email'] = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
$data_to_set['phone'] = isset( $task_meta['global']['phone'] ) ? $task_meta['global']['phone'] : '';
$data_to_set['user_id'] = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : 0;
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
$db_tags = $this->get_view_data();
if ( empty( $user_input_tags ) ) {
$data_to_set['remove_tags'] = array();
return $data_to_set;
}
foreach ( $user_input_tags as $tag_value ) {
$tags_response = BWFAN_Common::decode_merge_tags( $tag_value );
$tags = json_decode( $tags_response );
if ( is_array( $tags ) && count( $tags ) > 0 ) {
foreach ( $tags as $single_tag ) {
$final_tags[] = $single_tag;
}
continue;
}
$final_tags[] = $tags_response;
}
foreach ( $final_tags as $fkey => $final_tag ) {
/** checking if the id already there than passing the id and value in array */
if ( ! isset( $db_tags[ $final_tag ] ) ) {
$tag = BWFCRM_Tag::get_tags( array(), $final_tag );
if ( empty( $tag ) || ! isset( $tag[0]['ID'] ) ) {
unset( $final_tags[ $fkey ] );
continue;
}
$final_tag = $tag[0]['ID'];
}
$final_tag_data[ $fkey ] = $final_tag;
unset( $final_tags[ $fkey ] );
}
$data_to_set['remove_tags'] = $final_tag_data;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$this->is_action_tag = true;
$final_tags = [];
$user_input_tags = isset( $step_data['tags'] ) && is_array( $step_data['tags'] ) ? $step_data['tags'] : [];
$data_to_set = array();
$data_to_set['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$data_to_set['phone'] = isset( $automation_data['global']['phone'] ) ? $automation_data['global']['phone'] : '';
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
$db_tags = $this->get_view_data();
if ( empty( $user_input_tags ) ) {
$data_to_set['remove_tags'] = array();
return $data_to_set;
}
$data_to_set['remove_tags'] = array_map( function ( $tag ) {
if ( absint( $tag['id'] ) > 0 ) {
return $tag['id'];
}
/**If mergetag in tag name */
$tag['name'] = BWFAN_Common::decode_merge_tags( $tag['name'] );
$tag = BWFAN_PRO_Common::get_or_create_terms( [ $tag ], BWFCRM_Term_Type::$TAG );
return isset( $tag[0]['id'] ) ? absint( $tag[0]['id'] ) : 0;
}, $user_input_tags );
return $data_to_set;
}
/**
* Execute the current action.
* Return 3 for successful execution , 4 for permanent failure.
*
* @param $action_data
*
* @return array
*/
public function execute_action( $action_data ) {
$this->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();
}
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ),
);
}
}
$tags_removed = $contact->remove_tags( $this->data['remove_tags'] );
$contact->save();
$not_removed_tags = array_filter( array_values( array_diff( $this->data['remove_tags'], $tags_removed ) ) );
$not_removed_terms = array();
if ( ! empty( $not_removed_tags ) ) {
$not_removed_terms = BWFCRM_Term::get_terms( 1, $not_removed_tags );
}
$nice_names = array_map( function ( $tag ) {
if ( isset( $tag['name'] ) ) {
return $tag['name'];
}
}, $not_removed_terms );
$tag_message = is_array( $nice_names ) ? implode( ',', $nice_names ) : '';
if ( ! empty( $tag_message ) ) {
$message = 'Not all tags removed, except of ' . $tag_message;
} else {
$message = 'All Tags removed';
}
return array(
'status' => 3,
'message' => __( $message, 'autonami-automation-connector' ),
);
}
public function process_v2() {
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ) );
}
}
$tags_removed = $contact->remove_tags( $this->data['remove_tags'] );
$contact->save();
$not_removed_tags = array_filter( array_values( array_diff( $this->data['remove_tags'], $tags_removed ) ) );
$not_removed_terms = array();
if ( ! empty( $not_removed_tags ) ) {
$not_removed_terms = BWFCRM_Term::get_terms( 1, $not_removed_tags );
}
$nice_names = array_map( function ( $tag ) {
if ( isset( $tag['name'] ) ) {
return $tag['name'];
}
}, $not_removed_terms );
$tag_message = is_array( $nice_names ) ? implode( ',', $nice_names ) : '';
if ( ! empty( $tag_message ) ) {
$message = __( 'Not all tags removed, except of ' . $tag_message, 'wp-marketing-automations-pro' );
} else {
$message = __( 'All Tags removed', 'wp-marketing-automations-pro' );
}
return $this->success_message( $message );
}
public function get_fields_schema() {
return [
[
'id' => 'tags',
"label" => __( 'Select tag', 'wp-marketing-automations-pro' ),
"type" => 'search',
'autocompleter' => 'tags',
"class" => "bwfan-col-sm-9",
"tip" => __( "Can add available tags.", 'wp-marketing-automations-pro' ),
"required" => true,
"allowFreeTextSearch" => true,
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['tags'] ) || empty( $data['tags'] ) ) {
return '';
}
$tags = [];
foreach ( $data['tags'] as $tag ) {
if ( ! isset( $tag['name'] ) || empty( $tag['name'] ) ) {
continue;
}
$tags[] = $tag['name'];
}
return $tags;
}
}
return 'BWFAN_CRM_Rmv_Tag';

View File

@@ -0,0 +1,549 @@
<?php
final class BWFAN_CRM_Update_CustomFields extends BWFAN_Action {
private static $instance = null;
private function __construct() {
$this->action_name = __( 'Update Fields', 'wp-marketing-automations-pro' );
$this->action_desc = __( 'This action adds/ updates the custom fields of the contact', 'wp-marketing-automations-pro' );
$this->action_priority = 60;
$this->support_v2 = true;
$this->required_fields = array( 'email', 'custom_fields' );
}
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' ) ) {
$data = $this->get_view_data();
BWFAN_Core()->admin->set_actions_js_data( $this->get_class_slug(), 'custom_fields_options', $data );
}
}
public function get_view_data() {
$custom_fields = BWFCRM_Fields::get_fields( null, 1 );
/** removed array_merge and used array_replace instead, array_merge reindexes the numeric index */
$custom_fields = is_array( $custom_fields ) ? array_replace( [ 'email' => 'Email' ], $custom_fields ) : [];
$fields = [
[
'value' => '',
'label' => __( 'Select', 'wp-marketing-automations-pro' )
]
];
foreach ( $custom_fields as $field_id => $field ) {
if ( 'status' === $field_id ) {
continue;
}
$hint = '';
/** Get options if field types are select(4), radio(5) and checkbox(6) */
$field_types = [ 4, 5, 6 ];
if ( isset( $field['type'] ) && in_array( intval( $field['type'] ), $field_types, true ) ) {
$options = isset( $field['meta']['options'] ) && is_array( $field['meta']['options'] ) ? implode( ', ', $field['meta']['options'] ) : '';
if ( intval( $field['type'] ) === 6 ) {
$hint = "Enter any of these options: $options. Use comma seperated values for multiple options.";
} else {
$hint = "Enter one of these options: $options";
}
}
/** Set hint for country field */
if ( 'country' === $field_id ) {
$hint = "Enter two digit ISO Code";
}
/** Set hint for date field type */
if ( isset( $field['type'] ) && 7 === intval( $field['type'] ) ) {
$hint = "Enter date in Y-m-d format";
}
$type_datetime = property_exists( 'BWFCRM_Fields', 'TYPE_DATETIME' ) && BWFCRM_Fields::$TYPE_DATETIME ? BWFCRM_Fields::$TYPE_DATETIME : 8;
$type_time = property_exists( 'BWFCRM_Fields', 'TYPE_TIME' ) && BWFCRM_Fields::$TYPE_TIME ? BWFCRM_Fields::$TYPE_TIME : 9;
/** Set hint for datetime field type */
if ( isset( $field['type'] ) && $type_datetime === intval( $field['type'] ) ) {
$hint = "Enter date in Y-m-d H:i format";
}
/** Set hint for time field type */
if ( isset( $field['type'] ) && $type_time === intval( $field['type'] ) ) {
$hint = "Enter time in H:i format";
}
/** Set hint for number field type */
if ( isset( $field['type'] ) && 2 === intval( $field['type'] ) ) {
$hint = "Enter value in number";
}
$fields[] = [
'value' => $field_id,
'label' => isset( $field['name'] ) ? $field['name'] : $field,
'hint' => $hint,
'type' => isset( $field['type'] ) ? $field['type'] : 1,
];
}
return $fields;
}
/**
* Show the html fields for the current action.
*/
public function get_view() {
$unique_slug = $this->get_slug();
?>
<script type="text/html" id="tmpl-repeater-ui-<?php echo esc_attr__( $unique_slug ); ?>">
<div class="bwfan-input-form clearfix gs-repeater-fields">
<div class="bwfan-col-sm-5 bwfan-p-0">
<select data-parent-slug="<?php echo esc_attr__( $unique_slug ); ?>" required
class="bwfan-input-wrapper wfacp_ac_custom_field"
name="bwfan[{{data.action_id}}][data][custom_fields][field][{{data.index}}]">
<option value=""><?php echo esc_html__( 'Choose Field', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'custom_fields_options') &&
_.isObject(data.actionFieldsOptions.custom_fields_options) ) {
_.each( data.actionFieldsOptions.custom_fields_options, function( value, key ){
#>
<option value="{{value.value}}">{{value.label}}</option>
<# })
}
#>
</select>
</div>
<div class="bwfan-col-sm-6 bwfan-pr-0">
<input required type="text" class="bwfan-input-wrapper bwfan-input-merge-tags" value=""
name="bwfan[{{data.action_id}}][data][custom_fields][field_value][{{data.index}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
</script>
<script type="text/html" id="tmpl-action-<?php echo esc_attr__( $unique_slug ); ?>">
<div class="bwfan-repeater-wrap">
<label for="" class="bwfan-label-title">
<?php
echo esc_html__( 'Select Custom Fields', 'wp-marketing-automations-pro' );
$message = __( 'Select available fields to update and if unable to locate then sync the connector.', 'wp-marketing-automations-pro' );
echo $this->add_description( $message, '2xl', 'right' ); //phpcs:ignore WordPress.Security.EscapeOutput
echo $this->inline_merge_tag_invoke(); //phpcs:ignore WordPress.Security.EscapeOutput
?>
</label>
<div class="clearfix bwfan-input-repeater bwfan_mb10">
<#
repeaterArr = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data,
'custom_fields')) ? data.actionSavedData.data.custom_fields : {};
repeaterCount = _.size(repeaterArr.field);
validate_field = (_.has(data.actionSavedData, 'data') && _.has(data.actionSavedData.data,
'validate_fields')) ? data.actionSavedData.data.validate_fields : 0;
validate_field = 1=== parseInt(validate_field)?'checked':0;
if(repeaterCount == 0) {
repeaterArr = {field:{0:''}, field_value:{0:''}};
}
if(repeaterCount >= 0) {
h=0;
_.each( repeaterArr.field, function( value, key ){
#>
<div class="bwfan-input-form clearfix gs-repeater-fields">
<div class="bwfan-col-sm-5 bwfan-p-0">
<select required class="bwfan-input-wrapper wfacp_ac_custom_field"
name="bwfan[{{data.action_id}}][data][custom_fields][field][{{h}}]">
<option value=""><?php echo esc_html__( 'Choose Field', 'wp-marketing-automations-pro' ); ?></option>
<#
if(_.has(data.actionFieldsOptions, 'custom_fields_options') &&
_.isObject(data.actionFieldsOptions.custom_fields_options) ) {
_.each( data.actionFieldsOptions.custom_fields_options, function( column_option_value,
column_option_key ){
selected = (column_option_value.value == value) ? 'selected' : '';
#>
<option value="{{column_option_value.value}}" {{selected}}>
{{column_option_value.label}}
</option>
<# })
}
#>
</select>
</div>
<div class="bwfan-col-sm-6 bwfan-pr-0">
<input required type="text" class="bwfan-input-wrapper"
value="{{repeaterArr.field_value[key]}}"
name="bwfan[{{data.action_id}}][data][custom_fields][field_value][{{h}}]"/>
</div>
<div class="bwfan-col-sm-1 bwfan-pr-0">
<span class="bwfan-remove-repeater-field" data-groupid="{{data.action_id}}">&#10006;</span>
</div>
</div>
<# h++;
});
}
repeaterCount = repeaterCount + 1;
#>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0">
<a href="#"
class="bwfan-add-repeater-data bwfan-repeater-ui-<?php echo esc_attr__( $unique_slug ); ?>"
data-groupid="{{data.action_id}}"
data-count="{{repeaterCount}}"><?php esc_html_e( 'Add More', 'wp-marketing-automations-pro' ); ?></a>
</div>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan_mt15 bwfan-mb-15">
<label for="bwfan-validate_fields" class="bwfan-label-title">Advanced</label>
<input type="checkbox" name="bwfan[{{data.action_id}}][data][validate_fields]"
id="bwfan-ac_validate_fields" value="1" class="validate_fields_1" {{validate_field}}>
<label for="bwfan-ac_validate_fields" class="bwfan-checkbox-label">Do not update custom field(s) when
passed value is blank</label>
</div>
</script>
<script>
jQuery(document).ready(function ($) {
/** Generate repeater UI by calling script template */
$('body').on('click', '.bwfan-repeater-ui-<?php echo esc_attr__( $unique_slug ); ?>', function (event) {
event.preventDefault();
var $this = $(this);
var index = Number($this.attr('data-count'));
var action_id = $this.attr('data-groupid');
var template = wp.template('repeater-ui-<?php echo esc_attr__( $unique_slug ); ?>');
var actionFieldsOptions = {
custom_fields_options: bwfan_set_actions_js_data['<?php echo esc_attr__( $this->get_class_slug() ); ?>']['custom_fields_options']
};
$this.parents('.bwfan-repeater-wrap').find('.bwfan-input-repeater').append(template({
action_id: action_id,
index: index,
actionFieldsOptions: actionFieldsOptions
}));
index = index + 1;
$this.attr('data-count', index);
});
});
</script>
<?php
}
/**
* Make all the data which is required by the current action.
* This data will be used while executing the task of this action.
*
* @param $integration_object BWFAN_Integration
* @param $task_meta
*
* @return array|void
*/
public function make_data( $integration_object, $task_meta ) {
$data_to_set = array();
$data_to_set['email'] = isset( $task_meta['global']['email'] ) ? $task_meta['global']['email'] : '';
$data_to_set['user_id'] = isset( $task_meta['global']['user_id'] ) ? $task_meta['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $task_meta['global']['phone'] ) ? $task_meta['global']['phone'] : '';
$fields = $task_meta['data']['custom_fields']['field'];
$fields_value = $task_meta['data']['custom_fields']['field_value'];
$custom_fields = array();
$is_validate = isset( $task_meta['data']['validate_fields'] ) ? $task_meta['data']['validate_fields'] : '';
/** check if email not exists in global then get from user is if available */
if ( empty( $data_to_set['email'] ) && ! empty( $data_to_set['user_id'] ) ) {
$user_data = get_userdata( $data_to_set['user_id'] );
$data_to_set['email'] = $user_data instanceof WP_User ? $user_data->user_email : '';
}
add_filter( 'bwfan_order_date_format', [ $this, 'get_ymd' ] );
foreach ( $fields as $key1 => $field_id ) {
if ( isset( $this->default_fields[ $field_id ] ) ) {
$data_to_set['have_default_field'] = true;
$data_to_set[ $field_id ] = BWFAN_Common::decode_merge_tags( $fields_value[ $key1 ] );
continue;
}
$custom_fields[ $field_id ] = BWFAN_Common::decode_merge_tags( $fields_value[ $key1 ] );
}
remove_filter( 'bwfan_order_date_format', [ $this, 'get_ymd' ] );
//filter custom fields to remove blank
if ( 1 === intval( $is_validate ) ) {
foreach ( $custom_fields as $key => $fields ) {
if ( empty( $fields ) ) {
unset( $custom_fields[ $key ] );
}
}
}
$data_to_set['custom_fields'] = $custom_fields;
return $data_to_set;
}
public function make_v2_data( $automation_data, $step_data ) {
$data_to_set = array();
$data_to_set['email'] = isset( $automation_data['global']['email'] ) ? $automation_data['global']['email'] : '';
$data_to_set['user_id'] = isset( $automation_data['global']['user_id'] ) ? $automation_data['global']['user_id'] : 0;
$data_to_set['phone'] = isset( $automation_data['global']['phone'] ) ? $automation_data['global']['phone'] : '';
$fields = $step_data['custom_fields'];
$is_validate = isset( $step_data['validate_fields'] ) ? $step_data['validate_fields'] : '';
if ( empty( $data_to_set['email'] ) ) {
$user = ! empty( $automation_data['global']['user_id'] ) ? get_user_by( 'ID', $automation_data['global']['user_id'] ) : false;
$data_to_set['email'] = $user instanceof WP_User ? $user->user_email : '';
}
if ( empty( $fields ) ) {
return $data_to_set;
}
$custom_fields = [];
foreach ( $fields as $field ) {
$field_value = '';
/** Checking if operator is set in field value */
if ( isset( $field['field_value']['operator'] ) ) {
$field_value = BWFAN_Common::decode_merge_tags( $field['field_value']['value'] );
$cid = $automation_data['global']['cid'];
/**Get field value from contact*/
$contact = new BWFCRM_Contact( $cid );
$contact_field_value = isset( $contact->fields[ $field['field'] ] ) ? $contact->fields[ $field['field'] ] : 0;
/**Set field value according to operator*/
switch ( $field['field_value']['operator'] ) {
case '+':
$field_value = (float) $contact_field_value + (float) $field_value;
break;
case '-':
$field_value = (float) $contact_field_value - (float) $field_value;
break;
}
}
$field_value = ! isset( $field['field_value']['value'] ) ? BWFAN_Common::decode_merge_tags( $field['field_value'] ) : $field_value;
$contact_columns = array_merge( BWFCRM_Fields::$contact_columns, BWFCRM_Fields::$contact_address_columns );
/** Checking if the field is exists in bwf_contact table's column then avoid further processing */
if ( isset( $contact_columns[ $field['field'] ] ) || 'email' === $field['field'] ) {
$custom_fields[ $field['field'] ] = $field_value;
continue;
}
/** check if the field is date, checking and formatting should be after decoding */
$field_type = BWFAN_Model_Fields::get_field_type( $field['field'] );
if ( BWFCRM_Fields::$TYPE_DATE === intval( $field_type ) ) {
$field_value = BWFCRM_Contact::get_date_value( $field_value );
if ( ! $this->validate_date( $field_value ) ) {
continue;
}
}
$type_datetime = property_exists( 'BWFCRM_Fields', 'TYPE_DATETIME' ) && BWFCRM_Fields::$TYPE_DATETIME ? BWFCRM_Fields::$TYPE_DATETIME : 8;
$type_time = property_exists( 'BWFCRM_Fields', 'TYPE_TIME' ) && BWFCRM_Fields::$TYPE_TIME ? BWFCRM_Fields::$TYPE_TIME : 9;
/** Check if the field is datetime, checking and formatting should be after decoding */
if ( $type_datetime === intval( $field_type ) ) {
$field_value = BWFCRM_Contact::get_date_value( $field_value, 'Y-m-d H:i' );
if ( ! $this->validate_date( $field_value, 'Y-m-d H:i' ) ) {
continue;
}
}
/** Check if the field is time, checking and formatting should be after decoding */
if ( $type_time === intval( $field_type ) ) {
$field_value = BWFCRM_Contact::get_date_value( $field_value, 'H:i' );
}
$custom_fields[ $field['field'] ] = $field_value;
}
/** Filter custom fields to remove blank in case validate empty enabled */
if ( 1 === intval( $is_validate ) ) {
foreach ( $custom_fields as $key => $value ) {
if ( empty( trim( $value ) ) ) {
unset( $custom_fields[ $key ] );
}
}
}
$data_to_set['custom_fields'] = $custom_fields;
return $data_to_set;
}
public function get_ymd() {
return 'Y-m-d';
}
public function execute_action( $action_data ) {
$this->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();
}
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return array(
'status' => 4,
'message' => __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ),
);
}
}
$custom_field_updated = $contact->update_custom_fields( $this->data['custom_fields'] );
if ( false === $custom_field_updated ) {
return array(
'status' => 4,
'message' => __( 'Unable to Update Custom Fields', 'autonami-automation-connector' ),
);
}
return array(
'status' => 3,
'message' => __( 'Contact fields updated ', 'autonami-automation-connector' ),
);
}
public function process_v2() {
$contact = new BWFCRM_Contact( $this->data['email'], true );
if ( ! $contact->is_contact_exists() && ! empty( $this->data['phone'] ) ) {
/** get contact id using the phone number */
$contact_id = BWFAN_Pro_Common::get_contact_id_by_phone( $this->data['phone'] );
$contact = new BWFCRM_Contact( $contact_id, false );
if ( ! $contact->is_contact_exists() ) {
return $this->error_response( __( 'Unable to fetch contact', 'wp-marketing-automations-pro' ) );
}
}
$message = '';
if ( isset( $this->data['custom_fields']['email'] ) && $contact->contact->get_email() !== $this->data['custom_fields']['email'] ) {
if ( ! is_email( $this->data['custom_fields']['email'] ) ) {
return $this->error_response( __( 'New email is not valid.', 'wp-marketing-automations-pro' ) );
}
$check_contact = new BWFCRM_Contact( $this->data['custom_fields']['email'] );
/** If email is already exists with other contacts*/
if ( $check_contact->is_contact_exists() ) {
$message = __( 'given email is already associated with another contact.', 'wp-marketing-automations-pro' );
/** If Only email field to be updated then return error response */
if ( 1 === count( $this->data['custom_fields'] ) ) {
return $this->error_response( $message );
}
/** If other fields also available for update then unset the email */
unset( $this->data['custom_fields']['email'] );
}
}
/** If no data to update contact field then return with success message */
if ( empty( $this->data['custom_fields'] ) ) {
return $this->success_message( __( 'Contact fields updated.', 'wp-marketing-automations-pro' ) );
}
$custom_field_updated = $contact->update_custom_fields( $this->data['custom_fields'] );
if ( false === $custom_field_updated ) {
return $this->error_response( __( 'Unable to Update Custom Fields', 'wp-marketing-automations-pro' ) );
}
$default_msg = __( 'Contact fields updated.', 'wp-marketing-automations-pro' );
$message = ! empty( $message ) ? 'Contact fields updated but ' . $message : $default_msg;
return $this->success_message( $message );
}
public function get_fields_schema() {
$field_options = $this->get_view_data();
return [
[
'id' => 'custom_fields',
'type' => 'repeater',
'label' => __( 'Select Custom Fields', 'wp-marketing-automations-pro' ),
"fields" => [
[
'id' => 'field',
'type' => 'wp_select',
'options' => $field_options,
'placeholder' => __( 'Select field', 'wp-marketing-automations-pro' ),
'label' => "",
'tip' => "",
"description" => "",
"required" => false,
],
[
"id" => 'field_value',
"label" => "",
"type" => 'text_with_button',
"text_with_symbol" => true,
"class" => 'bwfan-input-wrapper',
"description" => "",
"required" => false,
"automation_merge_tags" => true
]
],
'tip' => __( "Select available fields to update and if unable to locate then sync the connector.", 'wp-marketing-automations-pro' ),
"required" => false,
],
[
'id' => 'validate_fields',
'type' => 'checkbox',
'label' => __( 'Advanced', 'wp-marketing-automations-pro' ),
'checkboxlabel' => __( 'Do not update field(s) when passed value is blank', 'wp-marketing-automations-pro' ),
"description" => ""
]
];
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['custom_fields'] ) || empty( $data['custom_fields'] ) ) {
return '';
}
$count = count( $data['custom_fields'] );
return ( $count > 1 ) ? $count . ' fields' : $count . ' field';
}
/**
* Validate date
*
* @param $value
* @param $format
*
* @return bool
*/
public function validate_date( $value, $format = 'Y-m-d' ) {
try {
$date = $value !== null ? DateTime::createFromFormat( $format, $value ) : '';
$validated = $date && ( $date->format( $format ) === $value );
} catch ( Error $e ) {
return false;
} catch ( Exception $e ) {
return false;
}
return $validated;
}
}
return 'BWFAN_CRM_Update_CustomFields';

View File

@@ -0,0 +1,3 @@
<?php
//silence is golden

View File

@@ -0,0 +1,61 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Add lists call class
*/
class Add_To_Lists extends Base {
/**
* Add list to contact
*
* @param \BWFCRM_Contact $contact
* @param $data
*/
public function process_call( $contact, $data ) {
if ( ! is_array( $data ) || empty( $data ) ) {
return [
'skipped' => [],
'assigned' => []
];
}
$existing_lists = $contact->get_lists();
$contact->set_lists_v2( $data );
$new_lists = $contact->get_lists();
/** Check if any lists to add */
$assigned_ids = array_diff( $new_lists, $existing_lists );
if ( ! empty( $assigned_ids ) ) {
$contact->save();
}
$skipped_ids = array_diff( $data, $assigned_ids );
$all_ids = array_unique( array_merge( $assigned_ids, $skipped_ids ) );
$term_data = \BWFCRM_Term::get_terms( \BWFCRM_Term_Type::$LIST, $all_ids, '', 0, 0, OBJECT );
$term_map = [];
foreach ( $term_data as $term ) {
$term_map[ $term->get_id() ] = $term;
}
return [
'assigned' => array_map( function ( $id ) use ( $term_map ) {
return $term_map[ $id ] ?? null;
}, array_values( $assigned_ids ) ),
'skipped' => array_map( function ( $id ) use ( $term_map ) {
return $term_map[ $id ] ?? null;
}, array_values( $skipped_ids ) )
];
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'add_to_lists', 'BWFCRM\Calls\Autonami\Add_To_Lists' );

View File

@@ -0,0 +1,63 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Add tags call class
*/
class Add_Tags extends Base {
/**
* Add tags to contact
*
* @param \BWFCRM_Contact $contact
* @param $data
*
* @return mixed
*/
public function process_call( $contact, $data ) {
if ( ! is_array( $data ) || empty( $data ) ) {
return [
'skipped' => [],
'assigned' => []
];
}
$existing_tags = $contact->get_tags();
$contact->set_tags_v2( $data );
$new_tags = $contact->get_tags();
/** Check if any tags to add */
$assigned_ids = array_diff( $new_tags, $existing_tags );
if ( ! empty( $assigned_ids ) ) {
$contact->save();
}
$skipped_ids = array_diff( $data, $assigned_ids );
$all_ids = array_unique( array_merge( $assigned_ids, $skipped_ids ) );
$term_data = \BWFCRM_Term::get_terms( \BWFCRM_Term_Type::$TAG, $all_ids, '', 0, 0, OBJECT );
$term_map = [];
foreach ( $term_data as $term ) {
$term_map[ $term->get_id() ] = $term;
}
return [
'assigned' => array_filter( array_map( function ( $id ) use ( $term_map ) {
return $term_map[ $id ] ?? null;
}, array_values( $assigned_ids ) ) ),
'skipped' => array_filter( array_map( function ( $id ) use ( $term_map ) {
return $term_map[ $id ] ?? null;
}, array_values( $skipped_ids ) ) )
];
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'add_tags', 'BWFCRM\Calls\Autonami\Add_Tags' );

View File

@@ -0,0 +1,33 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Delete Contacts call class
*/
class Delete_Contacts extends Base {
/**
* @param \BWFCRM_Contact $contact
* @param $data
*
* @return bool
*/
public function process_call( $contact, $data ) {
$contact_id = $contact->contact->get_id();
// rechecking just to make sure if this function get invoked directly using namespace
if ( empty( $contact_id ) ) {
return false;
}
return \BWFCRM_Model_Contact::delete_contact( $contact_id );
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'delete_contacts', 'BWFCRM\Calls\Autonami\Delete_Contacts' );

View File

@@ -0,0 +1,161 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFAN_Common;
use BWFAN_Model_Automations;
use BWFCRM\Calls\Base;
/**
* End Automation call class
*/
class End_Automation extends Base {
/**
* End Automation for contact
*
* @param $contact
* @param $data
*
* @return mixed
*/
public function process_call( $contact, $data ) {
/**
* End Automation for contact
*/
if ( empty( $data ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'End Automation (Error): Invalid data', 'wp-marketing-automations-pro' ),
);
}
$automations = array();
$from = isset( $data['from'] ) ? $data['from'] : '';
$from_id = isset( $data['link_id'] ) ? $data['link_id'] : 0;
if ( empty( $from_id ) && isset( $data['bulk_action_id'] ) ) {
$from_id = isset( $data['bulk_action_id'] ) ? $data['bulk_action_id'] : 0;
unset( $data['bulk_action_id'] );
}
if ( isset( $data['from'] ) ) {
unset( $data['from'] );
}
if ( isset( $data['link_id'] ) ) {
unset( $data['link_id'] );
}
/** Form automation array */
foreach ( $data as $automation ) {
if ( empty( $automation['id'] ) ) {
continue;
}
/** Automation id */
$automation_id = absint( $automation['id'] );
/** Get Automation data */
$automation_data = BWFAN_Model_Automations::get( $automation_id );
/** if automation data not found */
if ( empty( $automation_data ) ) {
continue;
}
/** checking automation status before deleting tasks */
if ( isset( $automation_data['status'] ) && 2 === absint( $automation_data['status'] ) ) {
continue;
}
/** Set automation id */
$automations[] = $automation_id;
}
/** Contact email */
$email = $contact->contact->get_email();
/** Contact phone */
$phone = $contact->contact->get_phone();
$tasks = [];
/** Get task from email id */
if ( ! empty( $email ) && is_email( $email ) ) {
$email_tasks = BWFAN_Common::get_schedule_task_by_email( $automations, $email );
foreach ( $email_tasks as $etask ) {
if ( ! empty( $etask ) ) {
$tasks = array_merge( $tasks, $etask );
}
}
}
/** Get task from phone no */
if ( ! empty( $phone ) ) {
$phone_tasks = BWFAN_Common::get_schedule_task_by_phone( $automations, $phone );
foreach ( $phone_tasks as $ptask ) {
if ( ! empty( $ptask ) ) {
$tasks = array_merge( $tasks, $ptask );
}
}
}
/**Get Automation contact for v2 automation */
$automation_contact = \BWFAN_Model_Automation_Contact::get_multiple_automation_contact( $automations, $contact->get_id() );
/** If empty task skip action */
if ( ( ! is_array( $tasks ) || 0 === count( $tasks ) ) && ( ! is_array( $automation_contact ) || 0 === count( $automation_contact ) ) ) {
return array(
'status' => self::$RESPONSE_SKIPPED,
'message' => __( 'End Automation (Skipped): No Tasks Found', 'wp-marketing-automations-pro' ),
);
}
/** Form task id array */
$delete_tasks = array();
foreach ( $tasks as $task ) {
$delete_tasks[] = absint( $task['ID'] );
}
/** skip if delete task is empty */
if ( 0 === count( $delete_tasks ) && 0 === count( $automation_contact ) ) {
return array(
'status' => self::$RESPONSE_SKIPPED,
'message' => __( 'End Automation (Skipped): No Tasks Found', 'wp-marketing-automations-pro' ),
);
}
if ( count( $delete_tasks ) > 0 ) {
/** Delete tasks */
BWFAN_Core()->tasks->delete_tasks( $delete_tasks );
}
if ( count( $automation_contact ) > 0 ) {
/** End v2 automations */
foreach ( $automation_contact as $data ) {
/** Add automation ended from */
$type = 'Bulk Action' === $from ? \BWFAN_Automation_Controller::$BULK_ACTION_END : \BWFAN_Automation_Controller::$LINK_TRIGGER_END;
$reason = [
'type' => $type,
'data' => [
'id' => $from_id,
]
];
$data = \BWFAN_PRO_Common::set_automation_ended_reason( $reason, $data );
\BWFAN_Common::end_v2_automation( 0, $data );
/** Update status as success for any step trail where status was waiting */
\BWFAN_Model_Automation_Contact_Trail::update_all_step_trail_status_complete( $data['trail'] );
\BWFAN_Common::update_automation_contact_fields( $data['cid'], $data['aid'] );
}
}
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'End Automation (Tasks Deleted): ', 'wp-marketing-automations-pro' ) . implode( ',', $delete_tasks ),
);
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'end_automation', 'BWFCRM\Calls\Autonami\End_Automation' );

View File

@@ -0,0 +1,28 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Remove lists call class
*/
class Remove_Lists extends Base {
/**
* Remove lists to contact
*
* @param \BWFCRM_Contact $contact
* @param $data
*
* @return mixed
*/
public function process_call( $contact, $data ) {
return $contact->remove_lists( $data );
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'remove_lists', 'BWFCRM\Calls\Autonami\Remove_Lists' );

View File

@@ -0,0 +1,31 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Remove tags call class
*/
class Remove_Tags extends Base {
/**
* Remove tags to contact
*
* @param \BWFCRM_Contact $contact
* @param $data
*
* @return mixed
*/
public function process_call( $contact, $data ) {
$response = $contact->remove_tags( $data );
$contact->save();
return $response;
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'remove_tags', 'BWFCRM\Calls\Autonami\Remove_Tags' );

View File

@@ -0,0 +1,25 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Subscribe Contacts call class
*/
class Subscribe_Contacts extends Base {
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_call( $contact, $data ) {
return $contact->resubscribe();
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'subscribe_contacts', 'BWFCRM\Calls\Autonami\Subscribe_Contacts' );

View File

@@ -0,0 +1,26 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Unsubscribe Contacts call class
*/
class Unsubscribe_Contacts extends Base {
/**
* @param \BWFCRM_Contact $contact
* @param $data
*
* @return mixed
*/
public function process_call( $contact, $data ) {
return $contact->unsubscribe();
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'unsubscribe_contacts', 'BWFCRM\Calls\Autonami\Unsubscribe_Contacts' );

View File

@@ -0,0 +1,40 @@
<?php
namespace BWFCRM\Calls\Autonami;
use BWFCRM\Calls\Base;
/**
* Remove lists call class
*/
class Update_Fields extends Base {
/**
* Remove lists to contact
*
* @param \BWFCRM_Contact $contact
* @param $data
*
* @return mixed
*/
public function process_call( $contact, $data ) {
/** Handle status change */
if ( isset( $data['status'] ) ) {
if ( intval( $data['status'] ) === 3 ) {
/** unsubscribe contact */
$contact->unsubscribe( false );
unset( $data['status'] );
} else {
/** remove unsubscribe entry - maybe exists */
$contact->remove_unsubscribe_status();
}
}
return $contact->update_custom_fields( $data );
}
}
/**
* Register call
*/
BWFCRM_Core()->calls->register_call( 'update_fields', 'BWFCRM\Calls\Autonami\Update_Fields' );

View File

@@ -0,0 +1,29 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_Autonami_Integration extends BWFAN_Integration {
public static $integration_type = null;
public static $headers = null;
private static $ins = null;
private function __construct() {
$this->action_dir = __DIR__;
$this->nice_name = __( 'Contact', 'wp-marketing-automations-pro' );
$this->group_name = __( 'Automations', 'wp-marketing-automations-pro' );
$this->group_slug = 'autonami';
$this->priority = 15;
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
}
/**
* Register this class as an integration.
*/
BWFAN_Load_Integrations::register( 'BWFAN_Autonami_Integration' );

View File

@@ -0,0 +1,29 @@
<?php
final class BWFAN_Autonami_Source extends BWFAN_Source {
private static $instance = null;
public function __construct() {
$this->event_dir = __DIR__;
$this->nice_name = __( 'Automations', 'wp-marketing-automations-pro' );
$this->group_name = __( 'Automations', 'wp-marketing-automations-pro' );
$this->group_slug = 'autonami';
$this->priority = 5;
}
/**
* Ensures only one instance of the class is loaded or can be loaded.
*
* @return BWFAN_Autonami_Source|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
BWFAN_Load_Sources::register( 'BWFAN_Autonami_Source' );

View File

@@ -0,0 +1,128 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
use WP_Error;
/**
* Add lists action class
*/
class Add_To_Lists extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'add_to_lists';
$this->nice_name = __( 'Add Lists', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
$this->event_slug = 'crm_assigned_list';
}
/**
* Add list field schema
*
* @return array
*/
public function get_action_schema() {
return [
'type' => 'search',
'meta' => [
'autocompleter' => 'list',
'addnew' => true,
]
];
}
/**
* process action
*
* @param $contact \BWFCRM_Contact
* @param $lists
*
* @return array|false
*/
public function handle_action( $contact, $lists ) {
$lists = array_column( $lists, 'id' );
if ( empty( $lists ) ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Add_To_Lists' ) ) {
return false;
}
$call_obj = new Calls\Add_To_Lists();
/**
* Process call
*/
$lists_added = $call_obj->process_call( $contact, $lists );
return $this->get_response( $lists, $lists_added );
}
public function get_response( $lists, $lists_added ) {
$message = array();
if ( $lists_added instanceof WP_Error ) {
$message[] = __( 'List(s) Add Failed: ', 'wp-marketing-automations-pro' ) . array_reduce( $lists, function ( $carry, $item ) {
$name = $item['value'];
if ( empty( $carry ) ) {
return $name;
}
return ! empty( $name ) ? $carry . ', ' . $name : $carry;
}, '' );
$message[] = __( 'List(s) Add Failed. Error: ', 'wp-marketing-automations-pro' ) . $lists_added->get_error_message();
return array(
'status' => self::$RESPONSE_FAILED,
'message' => $message,
);
}
if ( isset( $lists_added['skipped'] ) && count( $lists_added['skipped'] ) > 0 ) {
$message[] = __( 'List(s) Add Skipped: ', 'wp-marketing-automations-pro' ) . array_reduce( array_filter( $lists_added['skipped'] ), function ( $carry, $item ) {
$name = $item->get_name();
if ( empty( $carry ) ) {
return $name;
}
return ! empty( $name ) ? $carry . ', ' . $name : $carry;
}, '' );
}
if ( isset( $lists_added['assigned'] ) && count( $lists_added['assigned'] ) > 0 ) {
$message[] = __( 'List(s) Added: ', 'wp-marketing-automations-pro' ) . array_reduce( array_filter( $lists_added['assigned'] ), function ( $carry, $item ) {
$name = $item->get_name();
if ( empty( $carry ) ) {
return $name;
}
return ! empty( $name ) ? $carry . ', ' . $name : $carry;
}, '' );
}
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => $message,
);
}
}
/**
* Register action
*/
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'add_to_lists', 'BWFCRM\Actions\Autonami\Add_To_Lists', __( 'Add Lists', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,125 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
use WP_Error;
/**
* Add tags action class
*/
class Add_Tags extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'add_tags';
$this->nice_name = __( 'Add Tags', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
$this->event_slug = 'crm_assigned_tag';
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return [
'type' => 'search',
'meta' => [
'autocompleter' => 'tag',
'addnew' => true,
]
];
}
/**
* process action
*
* @param $contact \BWFCRM_Contact
* @param $tags
*
* @return array|false
*/
public function handle_action( $contact, $tags ) {
$tags = array_column( $tags, 'id' );
if ( empty( $tags ) ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Add_Tags' ) ) {
return false;
}
$call_obj = new Calls\Add_Tags;
/**
* Process call
*/
$tags_added = $call_obj->process_call( $contact, $tags );
return $this->get_response( $tags, $tags_added );
}
public function get_response( $tags, $tags_added ) {
$message = array();
if ( $tags_added instanceof WP_Error ) {
$message[] = __( 'Tag(s) Add Failed: ', 'wp-marketing-automations-pro' ) . array_reduce( $tags, function ( $carry, $item ) {
$name = $item['value'];
if ( empty( $carry ) ) {
return $name;
}
return ! empty( $name ) ? $carry . ', ' . $name : $carry;
}, '' );
$message[] = __( 'Tag(s) Add Failed. Error: ', 'wp-marketing-automations-pro' ) . $tags_added->get_error_message();
return array(
'status' => self::$RESPONSE_FAILED,
'message' => $message,
);
}
if ( isset( $tags_added['skipped'] ) && count( $tags_added['skipped'] ) > 0 ) {
$message[] = __( 'Tag(s) Add Skipped: ', 'wp-marketing-automations-pro' ) . array_reduce( array_filter( $tags_added['skipped'] ), function ( $carry, $item ) {
$name = $item->get_name();
if ( empty( $carry ) ) {
return $name;
}
return ! empty( $name ) ? $carry . ', ' . $name : $carry;
}, '' );
}
if ( isset( $tags_added['assigned'] ) && count( $tags_added['assigned'] ) > 0 ) {
$message[] = __( 'Tag(s) Added: ', 'wp-marketing-automations-pro' ) . array_reduce( array_filter( $tags_added['assigned'] ), function ( $carry, $item ) {
$name = $item->get_name();
if ( empty( $carry ) ) {
return $name;
}
return ! empty( $name ) ? $carry . ', ' . $name : $carry;
}, '' );
}
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => $message,
);
}
}
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'add_tags', 'BWFCRM\Actions\Autonami\Add_Tags', __( 'Add Tags', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,80 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/**
* Delete contacts action class
*/
class Delete_Contacts extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'delete_contacts';
$this->nice_name = __( 'Delete Contacts', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return [];
}
/**
* process action
*
* @param $contact \BWFCRM_Contact
* @param $data
*
* @return array|false|string
*/
public function handle_action( $contact, $data ) {
// skip if contact doesn't exist
if ( ! $contact->is_contact_exists() ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Delete_Contacts' ) ) {
return false;
}
$call_obj = new Calls\Delete_Contacts;
/**
* Process call
*/
$contact_deleted = $call_obj->process_call( $contact, $data );
if ( true === $contact_deleted ) {
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'Contact(s) Deleted: ', 'wp-marketing-automations-pro' ),
);
}
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Contact(s) Deleted Failed: ', 'wp-marketing-automations-pro' ),
);
}
}
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'delete_contacts', 'BWFCRM\Actions\Autonami\Delete_Contacts', __( 'Delete Contacts', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,71 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/**
* End Automation action
*/
class End_Automation extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'end_automation';
$this->nice_name = __( 'End Automation', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return [
'type' => 'search',
'meta' => [
'autocompleter' => 'automation',
'addnew' => false,
]
];
}
/**
* process action
*
* @param $contact
* @param $data
*
* @return array|mixed
*/
public function handle_action( $contact, $data ) {
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\End_Automation' ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'End Automation: Call not found', 'wp-marketing-automations-pro' ),
);
}
$call_obj = new Calls\End_Automation;
/**
* Process call
*/
return $call_obj->process_call( $contact, $data );
}
}
/**
* Register Action
*/
BWFCRM_Core()->actions->register_action( 'end_automation', 'BWFCRM\Actions\Autonami\End_Automation', __( 'End Automation', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,100 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/**
* Remove lists action
*/
class Remove_Lists extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'remove_lists';
$this->nice_name = __( 'Remove Lists', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
$this->event_slug = 'crm_unassigned_list';
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return array(
'type' => 'search',
'meta' => array(
'autocompleter' => 'list',
'addnew' => false,
),
);
}
/**
* process action
*
* @param $contact
* @param $lists
*
* @return array|string
*/
public function handle_action( $contact, $lists ) {
$contact_lists = $contact->get_lists();
// format list data
$lists = array_filter( array_map( function ( $list ) use ( $contact_lists ) {
return in_array( $list['id'], $contact_lists ) ? $list['id'] : false;
}, $lists ) );
if ( empty( $lists ) ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Remove_Lists' ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'List(s) Remove Failed: Call not found', 'wp-marketing-automations-pro' ),
);
}
$call_obj = new Calls\Remove_Lists();
/**
* Process call
*/
$removed_lists = $call_obj->process_call( $contact, $lists );
if ( empty( $removed_lists ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'List(s) Remove Skipped: Lists were not assigned on the contact', 'wp-marketing-automations-pro' ),
);
}
$removed_lists = \BWFCRM_Lists::get_lists( $removed_lists );
$removed_lists = array_map( function ( $list ) {
return $list['name'];
}, $removed_lists );
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'List(s) Removed: ', 'wp-marketing-automations-pro' ) . implode( ',', $removed_lists ),
);
}
}
/**
* Register Action
*/
BWFCRM_Core()->actions->register_action( 'remove_lists', 'BWFCRM\Actions\Autonami\Remove_Lists', __( 'Remove Lists', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,98 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/*
* Remove tags action class
*/
class Remove_Tags extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'remove_tags';
$this->nice_name = __( 'Remove Tags', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
$this->event_slug = 'crm_unassigned_tag';
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return array(
'type' => 'search',
'meta' => array(
'autocompleter' => 'tag',
'addnew' => false,
),
);
}
/**
* @param $contact \BWFCRM_Contact
* @param $tags
*
* @return array|string
*/
public function handle_action( $contact, $tags ) {
$contact_tags = $contact->get_tags();
// format tag data
$tags = array_filter( array_map( function ( $tag ) use ( $contact_tags ) {
return in_array( $tag['id'], $contact_tags ) ? $tag['id'] : false;
}, $tags ) );
if ( empty( $tags ) ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Remove_Tags' ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Tag(s) Remove Failed: Call not found', 'wp-marketing-automations-pro' ),
);
}
$call_obj = new Calls\Remove_Tags();
/**
* Process call
*/
$removed_tags = $call_obj->process_call( $contact, $tags );
if ( empty( $removed_tags ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Tag(s) Remove Skipped: Tags were not present on the contact', 'wp-marketing-automations-pro' ),
);
}
$removed_tags = \BWFCRM_Tag::get_tags( $removed_tags );
$removed_tags = array_map( function ( $tag ) {
return $tag['name'];
}, $removed_tags );
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'Tag(s) Removed: ', 'wp-marketing-automations-pro' ) . implode( ',', $removed_tags ),
);
}
}
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'remove_tags', 'BWFCRM\Actions\Autonami\Remove_Tags', __( 'Remove Tags', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,81 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/**
* Subscribe contacts action class
*/
class Subscribe_Contacts extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'subscribe_contacts';
$this->nice_name = __( 'Subscribe Contacts', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
$this->event_slug = 'crm_contact_subscribed';
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return [];
}
/**
* process action
*
* @param $contact \BWFCRM_Contact
* @param $data
*
* @return array|false|string
*/
public function handle_action( $contact, $data ) {
// skip if already subscribed
if ( $contact->contact->is_subscribed ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Subscribe_Contacts' ) ) {
return false;
}
$call_obj = new Calls\Subscribe_Contacts;
/**
* Process call
*/
$constact_subsribed = $call_obj->process_call( $contact, $data );
if ( true === $constact_subsribed ) {
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'Contact(s) Subscribed: ', 'wp-marketing-automations-pro' ),
);
}
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Contact(s) Subscribed Failed: ', 'wp-marketing-automations-pro' ),
);
}
}
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'subscribe_contacts', 'BWFCRM\Actions\Autonami\Subscribe_Contacts', __( 'Subscribe Contacts', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,81 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/**
* Unsubscribe contacts action class
*/
class Unsubscribe_Contacts extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'unsubscribe_contacts';
$this->nice_name = __( 'Unsubscribe Contacts', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
$this->event_slug = 'crm_contact_unsubscribed';
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return [];
}
/**
* process action
*
* @param $contact \BWFCRM_Contact
* @param $data
*
* @return array|false|string
*/
public function handle_action( $contact, $data ) {
// skip if already unsubscribed
if ( ! empty( $contact->check_contact_unsubscribed() ) ) {
return 'skip';
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Unsubscribe_Contacts' ) ) {
return false;
}
$call_obj = new Calls\Unsubscribe_Contacts;
/**
* Process call
*/
$contact_unsubscribed = $call_obj->process_call( $contact, $data );
if ( true === $contact_unsubscribed ) {
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'Contact(s) Unubscribed: ', 'wp-marketing-automations-pro' ),
);
}
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Contact(s) Unsubscribed Failed: ', 'wp-marketing-automations-pro' ),
);
}
}
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'unsubscribe_contacts', 'BWFCRM\Actions\Autonami\Unsubscribe_Contacts', __( 'Unsubscribe Contacts', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,143 @@
<?php
namespace BWFCRM\Actions\Autonami;
use BWFAN_Model_Fields;
use BWFCRM\Actions\Base;
use BWFCRM\Calls\Autonami as Calls;
/**
* Update Custom field action class
*/
class Update_Fields extends Base {
/**
* Class constructor
*/
public function __construct() {
$this->slug = 'update_fields';
$this->nice_name = __( 'Update field', 'wp-marketing-automations-pro' );
$this->group = 'autonami';
$this->group_label = __( 'FunnelKit Automations', 'wp-marketing-automations-pro' );
$this->priority = 10;
$this->support = [ 1, 2 ];
}
/**
* Returns action field schema
*
* @return array
*/
public function get_action_schema() {
return [
'type' => 'bwfupdatefield',
'meta' => [
'fields' => []
]
];
}
/**
* process action
*
* @param $contact
* @param $fields
*
* @return array
*/
public function handle_action( $contact, $fields ) {
$field_data = [];
/** Trim field keys before processing */
$fields = $this->trim_filter_data( $fields );
foreach ( $fields as $key => $value ) {
/** If status is 3 then contact unsubscribe */
if ( 'status' === trim( $key ) && 3 === absint( $value ) ) {
$contact->unsubscribe();
continue;
}
if ( isset( $value ) ) {
$field_data[ trim( $key ) ] = $value;
}
}
/**
* Check if call exists
*/
if ( ! class_exists( 'BWFCRM\Calls\Autonami\Update_Fields' ) ) {
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Update Fields (Error): Update Fields call not found', 'wp-marketing-automations-pro' ),
);
}
$call_obj = new Calls\Update_Fields();
$result = $call_obj->process_call( $contact, $field_data );
$fields = $this->get_affected_fields( $fields );
if ( true === $result ) {
return array(
'status' => self::$RESPONSE_SUCCESS,
'message' => __( 'Field(s) Updated: ', 'wp-marketing-automations-pro' ) . implode( ', ', $fields ),
);
}
return array(
'status' => self::$RESPONSE_FAILED,
'message' => __( 'Field(s) Update Failed: ', 'wp-marketing-automations-pro' ) . implode( ', ', $fields ),
);
}
/**
* Trim field key data
*
* @param $data
*
* @return array
*/
public function trim_filter_data( $data ) {
if ( empty( $data ) ) {
return [];
}
$result = [];
foreach ( $data as $key => $value ) {
$result[ trim( $key ) ] = $value;
}
return $result;
}
public function get_affected_fields( $fields ) {
$custom_fields = array();
$default_fields = array();
foreach ( array_keys( $fields ) as $id ) {
$id = trim( $id );
if ( is_numeric( $id ) ) {
$custom_fields[] = absint( $id );
continue;
}
$default_fields[] = $id;
}
/** If custom fields not found */
if ( empty( $custom_fields ) ) {
return $default_fields;
}
$custom_fields = BWFAN_Model_Fields::get_multiple_fields( $custom_fields );
$custom_fields = array_map( function ( $field ) {
return $field['name'];
}, $custom_fields );
return array_values( array_merge( $default_fields, $custom_fields ) );
}
}
/**
* Register action
*/
BWFCRM_Core()->actions->register_action( 'update_fields', 'BWFCRM\Actions\Autonami\Update_Fields', __( 'Update Fields', 'wp-marketing-automations-pro' ), 'autonami', __( 'FunnelKit Automations', 'wp-marketing-automations-pro' ) );

View File

@@ -0,0 +1,832 @@
<?php
/**
* Class BWFAN_Autonami_Webhook_Received
*/
#[AllowDynamicProperties]
final class BWFAN_Autonami_Webhook_Received extends BWFAN_Event {
private static $instance = null;
private $automation_id = null;
private $automation_key = '';
private $localized_automation_key = '';
private $webhook_data = array();
private $referer = '';
private $email = '';
private $email_map_key = '';
private $webhook_version = null;
private $webhook_automation_id = null;
private $logs = array();
private function __construct() {
$this->optgroup_label = __( 'Automation', 'wp-marketing-automations-pro' );
$this->event_name = __( 'Webhook Received', 'wp-marketing-automations-pro' );
$this->event_desc = __( 'This event runs after a webhook URL receives the data', 'wp-marketing-automations-pro' );
$this->event_merge_tag_groups = array( 'wp_webhook', 'bwf_contact' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast',
'bwf_webhook'
);
$this->customer_email_tag = '';
$this->v2 = true;
$this->need_unique_key = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function bwfan_add_webhook_endpoint() {
register_rest_route( 'autonami/v1', '/webhook(?:/(?P<bwfan_autonami_webhook_id>\d+))?', [
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'bwfan_capture_async_events' ),
'permission_callback' => '__return_true',
'args' => [
array( 'bwfan_autonami_webhook_id' => 0 ),
array( 'bwfan_autonami_webhook_key' => 0 ),
],
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'bwfan_capture_async_events' ),
'permission_callback' => '__return_true',
'args' => [
array( 'bwfan_autonami_webhook_id' => 0 ),
array( 'bwfan_autonami_webhook_key' => 0 ),
],
)
] );
}
public function load_hooks() {
add_action( 'rest_api_init', array( $this, 'bwfan_add_webhook_endpoint' ) );
add_action( 'bwfan_webhook_autonami', array( $this, 'process' ), 10, 6 );
add_action( 'wp_ajax_bwfan_get_refresh_data', array( $this, 'send_latest_webhook_data' ) );
}
public function bwfan_capture_async_events( WP_REST_Request $request ) {
BWFAN_PRO_Common::nocache_headers();
$content_type = $request->get_content_type();
$body_params = [];
if ( isset( $content_type['value'] ) && 'application/x-www-form-urlencoded' === $content_type['value'] ) {
$body = $request->get_body();
$body_params = ! empty( $body ) ? json_decode( $body, true ) : '';
$body_params = is_array( $body_params ) ? array_merge( $body_params, $request->get_query_params() ) : [];
}
$request_params = ! empty( $body_params ) ? $body_params : $request->get_params();
$this->set_log( 'fka webhook received' );
$this->set_log( $request_params );
$this->log();
if ( empty( $request_params ) ) {
wp_send_json( [ 'message' => 'Security failed.', 'success' => false ] );
}
//check request params contain both the key and id
if ( ( ! isset( $request_params['bwfan_autonami_webhook_key'] ) || empty( $request_params['bwfan_autonami_webhook_key'] ) ) && ( ! isset( $request_params['bwfan_autonami_webhook_id'] ) || empty( $request_params['bwfan_autonami_webhook_id'] ) ) ) {
wp_send_json( [ 'message' => 'Security failed.', 'success' => false ] );
}
//get automation key using automation id
$automation_id = $request_params['bwfan_autonami_webhook_id'];
$meta = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'event_meta' );
$automation_key = $meta['bwfan_unique_key'];
//check if the automation key exist in database
if ( empty( $automation_key ) ) {
wp_send_json( [ 'message' => 'Automation does not have the unique key.', 'success' => false ] );
}
//validate automation key
if ( $automation_key !== $request_params['bwfan_autonami_webhook_key'] ) {
wp_send_json( [ 'message' => 'Automation unique key is not matching.', 'success' => false ] );
}
$webhook_data = $request_params;
unset( $webhook_data['bwfan_autonami_webhook_key'] );
unset( $webhook_data['bwfan_autonami_webhook_id'] );
unset( $meta['bwfan_webhook_url'] );
$webhook_data = $this->convert_value_to_string( $webhook_data );
/** Set Webhook Data in args */
$args = array(
'webhook_data' => $webhook_data,
'received_at' => ( new DateTime() )->getTimestamp(),
'referer' => $request->get_header( 'referer' ),
);
/** Save the webhook data in event meta of automation */
$meta = array_replace( $meta, $args );
$data_to_update = array( 'meta_value' => maybe_serialize( $meta ) );
$where_to_update = array(
'bwfan_automation_id' => $automation_id,
'meta_key' => 'event_meta'
);
BWFAN_Model_Automationmeta::update( $data_to_update, $where_to_update );
/** as it disturb the passing of data in webhook process **/
if ( isset( $meta['bwfan_automation_run'] ) ) {
unset( $meta['bwfan_automation_run'] );
}
/** as it disturb the passing of data in webhook process **/
if ( isset( $meta['enter_automation_on_active_contact'] ) ) {
unset( $meta['enter_automation_on_active_contact'] );
}
/** Only run this when automation is active. (Check is necessary because of Capture Data perspective) */
$automation_data = BWFAN_Model_Automations::get( $automation_id );
$meta['webhook_version'] = isset( $automation_data['v'] ) ? $automation_data['v'] : 1;
$meta['webhook_automation_id'] = $automation_id;
if ( isset( $meta['bwfan_email_map_key'] ) ) {
unset( $meta['bwfan_email_map_key'] );
}
/** Deliberately send 0 as this was making every request different */
$meta['received_at'] = 0;
if ( 1 === absint( $automation_data['status'] ) ) {
$this->before_process_webhook( $meta );
}
/** Send back 200 response */
wp_send_json( array( 'success' => true ), 200 );
}
/**
* convert value in string
*
* @param $webhook_fields
*
* @return array
*/
public function convert_value_to_string( $webhook_values ) {
foreach ( $webhook_values as $key => $value ) {
if ( is_array( $value ) ) {
$this->convert_value_to_string( $webhook_values[ $key ] );
continue;
}
if ( is_bool( $value ) ) {
$value = $value ? 'true' : 'false';
}
$webhook_values[ $key ] = strval( $value );
}
return $webhook_values;
}
public function before_process_webhook( $args ) {
$hook = 'bwfan_webhook_autonami';
$group = 'autonami';
if ( bwf_has_action_scheduled( $hook, $args, $group ) ) {
return;
}
bwf_schedule_single_action( time(), $hook, $args, $group );
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
public function get_user_id_event() {
$user = is_email( $this->email ) ? get_user_by( 'email', $this->email ) : false;
return ( $user instanceof WP_User ) ? $user->ID : false;
}
public function admin_enqueue_assets() {
if ( ! BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
return;
}
$this->automation_id = filter_input( INPUT_GET, 'edit' );
$meta = BWFAN_Model_Automationmeta::get_meta( $this->automation_id, 'event_meta' );
if ( isset( $meta['bwfan_unique_key'] ) && ! empty( $meta['bwfan_unique_key'] ) ) {
$this->localized_automation_key = $meta['bwfan_unique_key'];
}
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'automation_id', $this->automation_id );
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'saved_localized_automation_key', $this->localized_automation_key );
/** Set Event UI Data, if webhook data received */
if ( isset( $meta['webhook_data'] ) ) {
$webhook_data_received_time = date( 'm-d-Y h:i A', absint( $meta['received_at'] ) );
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'webhook_data', $meta['webhook_data'] );
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'webhook_data_received_formatted', $webhook_data_received_time );
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'webhook_data_received', absint( $meta['received_at'] ) );
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'webhook_referer', $meta['referer'] );
if ( isset( $meta['bwfan_email_map_key'] ) ) {
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'bwfan_email_map_key', $meta['bwfan_email_map_key'] );
}
}
}
public function send_latest_webhook_data() {
BWFAN_PRO_Common::nocache_headers();
$nonce_check_failed = ( ! isset( $_POST['nonce'] ) || false === wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'bwfan-action-admin' ) );
if ( $nonce_check_failed || ! isset( $_POST['automation_id'] ) ) {
wp_send_json( array(
'status' => 'failed'
) );
}
$automation_id = absint( sanitize_text_field( $_POST['automation_id'] ) );
$meta = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'event_meta' );
if ( ! isset( $meta['bwfan_unique_key'] ) || ! isset( $meta['webhook_data'] ) ) {
wp_send_json( array(
'status' => 'failed'
) );
}
if ( isset( $_POST['isv2'] ) && $_POST['isv2'] == true ) {
wp_send_json( $meta );
exit;
}
$payload = array(
'saved_localized_automation_key' => $meta['bwfan_unique_key'],
'webhook_data' => $meta['webhook_data'],
'webhook_data_received_formatted' => date( 'm-d-Y h:i A', absint( $meta['received_at'] ) ),
'webhook_data_received' => $meta['received_at'],
'webhook_referer' => $meta['referer']
);
if ( isset( $meta['bwfan_email_map_key'] ) ) {
$payload['bwfan_email_map_key'] = $meta['bwfan_email_map_key'];
}
wp_send_json( $payload );
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
?>
<?php
if ( isset( $global_data['email'] ) && ! empty( $global_data['email'] ) ) { ?>
<li>
<strong><?php esc_html_e( 'Email: ', 'wp-marketing-automations-pro' ); ?> </strong>
<?php esc_html_e( $global_data['email'] ); ?>
</li>
<?php
}
if ( ! isset( $global_data['webhook_data'] ) || empty( $global_data['webhook_data'] ) || ! is_array( $global_data['webhook_data'] ) ) {
return ob_get_clean();
}
$i = 0;
foreach ( $global_data['webhook_data'] as $key => $value ) {
if ( $key === $global_data['email_map_key'] ) {
continue;
}
if ( $i >= 2 ) {
break;
}
if ( is_array( $value ) ) {
?>
<li>
<strong><?php echo esc_html( $key ); ?>: </strong><?php esc_html_e( 'JSON Object', 'wp-marketing-automations-pro' ); ?>
</li>
<?php
} else {
?>
<li>
<strong><?php echo esc_html( $key ); ?>: </strong><?php echo esc_html( $value ); ?>
</li>
<?php }
$i ++;
}
return ob_get_clean();
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_event_meta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-webhook-data-preview">
<table style="border: 1px solid #dadada;width: 100%;text-align: left; padding: 8px 15px;">
<# _.each( data.data, function( value, key ){
var newValue = value;
if(Array.isArray(value) || _.isObject(value)) {
var is_parent_array = Array.isArray(value);
newValue = autonami_webhook_preview_fn({data: value, input_name: `${data.input_name}[${key}]`, is_parent_array});
newValue = decodeHTML(newValue);
}
#>
<tr style="border-bottom: 1px solid #aaa;">
<td style="padding: 5px 0; vertical-align:top;"><b>{{key}}</b></td>
<td>
{{newValue}}
<# if(!Array.isArray(value) && !_.isObject(value)) { #>
<input type="hidden" value="{{value}}" name="{{data.input_name}}[{{!!data.is_parent_array ? '': key}}]"/>
<# } #>
</td>
</tr>
<# }); #>
</table>
</script>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<div class="bwfan_wp_webhook_wrapper">
<#
var eventslug = '<?php esc_html_e( $this->get_slug() ); ?>';
var eventData = bwfan_events_js_data[eventslug];
var event_save_unique_key = eventData.saved_localized_automation_key;
if(event_save_unique_key.length>0){
eventData.localized_automation_key = event_save_unique_key
}
var webhook_url = '<?php esc_attr_e( home_url( '/' ) ); ?>';
webhook_url = webhook_url + 'wp-json/autonami/v1/webhook?bwfan_autonami_webhook_id='+eventData.automation_id+'&bwfan_autonami_webhook_key='+eventData.localized_automation_key;
var webhook_data = _.has(eventData, 'webhook_data') ? eventData.webhook_data : false;
var webhook_data_received = _.has(eventData, 'webhook_data_received') ? eventData.webhook_data_received : false;
var webhook_data_received_formatted = _.has(eventData, 'webhook_data_received_formatted') ? eventData.webhook_data_received_formatted : false;
var webhook_referer = _.has(eventData, 'webhook_referer') ? eventData.webhook_referer : false;
var bwfan_email_map_key = _.has(eventData, 'bwfan_email_map_key') ? eventData.bwfan_email_map_key : '';
var isWebhookDataArray = Array.isArray(webhook_data);
var nestedPreviewData = decodeHTML(autonami_webhook_preview_fn({data: webhook_data, input_name:'event_meta[webhook_data]', is_parent_array: isWebhookDataArray} ));
#>
<div class="bwfan_mt15"></div>
<label for="bwfan-webhook-url" class="bwfan-label-title"><?php esc_html_e( 'Custom Webhook URL', 'wp-marketing-automations-pro' ); ?></label>
<div class="bwfan-textarea-box">
<textarea name="event_meta[bwfan_webhook_url]" class="bwfan-input-wrapper bwfan-webhook-url" id="bwfan-webhook-url" cols="45" rows="2" onclick="select();" readonly>{{webhook_url}}</textarea>
<input type="hidden" name="event_meta[bwfan_unique_key]" id="bwfan-unique-key" value={{eventData.localized_automation_key}}>
<div class="clearfix bwfan_field_desc bwfan-pt-5">
Use this custom webhook URL to send requests to.
</div>
</div>
<# if( false === webhook_data ) { #>
<div class="bwfan_mt20"></div>
<div class="clearfix bwfan-mb-15">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Connect With Webhook', 'wp-marketing-automations-pro' ); ?></label>
</div>
<# } #>
<!-- Test Data Display -->
<#
if( false !== webhook_data ) { #>
<div class="bwfan_mt20"></div>
<label for="bwfan-webhook-url" class="bwfan-label-title"><?php esc_html_e( 'Data Found', 'wp-marketing-automations-pro' ); ?></label>
<div id="bwfan_webhook_preview_data_container"></div>
<#
setTimeout(function(){
jQuery('#bwfan_webhook_preview_data_container').html(nestedPreviewData);
}, 10);
#>
<div class="bwfan_field_desc">
Received at {{webhook_data_received_formatted}}
</div>
<input type="hidden" value="{{webhook_referer}}" name="event_meta[referer]"/>
<input type="hidden" value="{{webhook_data_received}}" name="event_meta[received_at]"/>
<div class="bwfan_mt10"></div>
<a class="button bwfan_refresh_webhook_data_button"><?php esc_html_e( 'Refresh Data', 'wp-marketing-automations-pro' ); ?></a>
<!-- Select Email Map Field -->
<div class="bwfan_mt20"></div>
<label for="bwfan-webhook-url" class="bwfan-label-title">
<?php esc_html_e( 'Select Email Field', 'wp-marketing-automations-pro' ); ?>
<div class="bwfan_tooltip" data-size="2xl">
<span class="bwfan_tooltip_text" data-position="top"><?php esc_html_e( 'Map the email field to be used by appropriate Rules and Actions.', 'wp-marketing-automations-pro' ); ?></span>
</div>
</label>
<select class="bwfan-input-wrapper" id="bwfan_email_map_key_dropdown" name="event_meta[bwfan_email_map_key]">
<option value="">Select Data Key</option>
<# _.each( getFields(webhook_data), function( value, key ){
selected = key == bwfan_email_map_key ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{key}}</option>
<# }); #>
</select>
<!-- END: Select Email Map Field -->
<# } else { #>
<div align="center">
<img width="50" src="<?php esc_attr_e( BWFAN_PRO_PLUGIN_URL . '/admin/assets/webhook.png' ); ?>"/>
<label class="bwfan-label-title bwfan_mt10">Test Your Event</label>
<div class="bwfan_mt5 bwfan_mb10">Send a request to the webhook URL.</div>
<a class="button bwfan_refresh_webhook_data_button"><?php esc_html_e( 'Receive Webhook', 'wp-marketing-automations-pro' ); ?></a>
<div class="bwfan_mt10 bwfan_webhook_error"></div>
</div>
<# } #>
<!-- END: Test Data Display -->
</div>
</script>
<script>
/** To make sure that the email map value is not destroyed on mounting of another admin event or action component */
jQuery('body').on('change', '#bwfan_email_map_key_dropdown', function () {
bwfan_events_js_data['autonami_webhook_received']['bwfan_email_map_key'] = jQuery('#bwfan_email_map_key_dropdown').val();
});
/** TO Pass the saved fields data to the merge tags */
jQuery('body').on('bwfan-selected-merge-tag', function (e, v) {
if ('wp_webhook_data' !== v.tag) {
return;
}
var options = '';
var i = 1;
var selected = '';
const nestedFieldKeys = getFields(bwfan_events_js_data['autonami_webhook_received']['webhook_data']);
_.each(nestedFieldKeys, function (value, key) {
selected = (i == 1) ? 'selected' : '';
options += '<option value="' + key + '" ' + selected + '>' + key + '</option>';
i++;
});
jQuery('.bwfan_wp_webhook_keys').html(options);
jQuery('.bwfan_tag_select').trigger('change');
});
/** Get latest data, store it in 'bwfan_events_js_data' and Refresh the view to load the latest data */
jQuery('body').on('click', '.bwfan_refresh_webhook_data_button', function () {
const automation_id = bwfan_events_js_data['autonami_webhook_received']['automation_id'];
const payload = {
automation_id: automation_id,
action: 'bwfan_get_refresh_data',
nonce: bwfanParams.ajax_nonce
};
const thisButton = jQuery(this);
thisButton.addClass('bwfan_btn_spin_blue');
jQuery.post(bwfanParams.ajax_url, payload, function (data) {
thisButton.removeClass('bwfan_btn_spin_blue');
if (!_.isObject(data) || (_.has(data, 'status') || false === data.status) || !_.has(data, 'webhook_data')) {
/** Failure, no data fetched */
jQuery('.bwfan_webhook_error').html('No Data Found! Test Again.');
return;
}
_.each(data, function (value, key) {
bwfan_events_js_data['autonami_webhook_received'][key] = value;
});
let $iziWrap = jQuery("#modal_automation_success");
if ($iziWrap.length > 0) {
$iziWrap.iziModal('setTitle', 'Latest Data Loaded');
$iziWrap.iziModal('open');
}
jQuery('.bwfan_wp_webhook_wrapper').remove();
BWFAN_Actions.create_event_meta_ui('<?php esc_attr_e( $this->get_slug() ); ?>');
});
});
</script>
<script>
jQuery(document).ready(function () {
window.autonami_webhook_preview_fn = wp.template('event-webhook-data-preview');
window.decodeHTML = function (html) {
var txt = document.createElement('textarea');
txt.innerHTML = html;
return txt.value;
};
window.getFields = function (data, fields = {}, rootField = '') {
const isArray = Array.isArray(data);
for (const key in data) {
const fieldKey = !rootField ? key : `${rootField}.${key}`;
if (Array.isArray(data[key]) || _.isObject(data[key])) {
fields = getFields(data[key], fields, fieldKey);
continue;
}
fields[fieldKey] = data[key];
}
return fields;
}
});
</script>
<?php
}
public function pre_executable_actions( $automation_data ) {
$email_map = $automation_data['event_meta']['bwfan_email_map_key'];
$this->email_map_key = ! empty( $email_map ) ? $email_map : '';
$entry = $this->webhook_data;
$fieldKeys = explode( '.', $this->email_map_key );
foreach ( $fieldKeys as $val ) {
if ( isset( $entry[ $val ] ) ) {
$entry = $entry[ $val ];
}
}
$this->email = is_email( trim( $entry ) ) ? trim( $entry ) : '';
$contact_id = 0;
/** get contact id */
if ( ! empty( $this->email ) ) {
$woofunnel_contact = new WooFunnels_Contact( '', $this->email );
if ( $woofunnel_contact->get_id() > 0 ) {
$contact_id = $woofunnel_contact->get_id();
}
}
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $contact_id, 'contact_id' );
}
/**
* Action Scheduler action callback
*
* @param $webhook_key
* @param $webhook_data
* @param $referer
* @param $received_at
* @param $webhook_version
* @param $webhook_automation_id
*
* @return array|bool|void
*/
public function process( $webhook_key, $webhook_data, $referer, $received_at = '', $webhook_version = '', $webhook_automation_id = '' ) {
$this->set_log( 'webhook processing start' );
$this->set_log( $webhook_data );
$this->automation_key = $webhook_key;
$this->webhook_data = $webhook_data;
$this->referer = $referer;
/** Check if webhook automation id is not present */
$this->get_webhook_automation_id( $webhook_key );
if ( empty( $this->webhook_automation_id ) ) {
return;
}
/** Check if webhook automation version is not present */
$this->get_webhook_version();
/** For automation v2 */
if ( 2 === absint( $this->webhook_version ) ) {
$contact_data_v2 = array(
'automation_key' => $this->automation_key,
'webhook_data' => $this->webhook_data,
'referer' => $this->referer,
'webhook_automation_id' => $this->webhook_automation_id
);
/** Cache obj instance */
$WooFunnels_Cache_obj = WooFunnels_Cache::get_instance();
try {
$key = 'bwfan_active_automations_v2_' . $this->get_slug();
BWFAN_Core()->automations->get_active_automations( 2, $this->get_slug() );
/** DB rows */
$v2_all_active_automations = $WooFunnels_Cache_obj->get_cache( $key, 'autonami' );
$webhook_automation = [ $this->webhook_automation_id ];
$v2_active_automations = array_values( array_filter( $v2_all_active_automations, function ( $single_automation ) use ( $webhook_automation ) {
return in_array( $single_automation['ID'], $webhook_automation );
} ) );
/** If automation is not active */
if ( empty( $v2_active_automations ) ) {
return;
}
/** Set current running automation in cache */
$WooFunnels_Cache_obj->set_cache( $key, $v2_active_automations, 'autonami' );
/** Set active v2 automations prop empty */
BWFAN_Core()->public->active_v2_automations = [];
BWFAN_Common::maybe_run_v2_automations( $this->get_slug(), $contact_data_v2 );
/** Set all active v2 automations in object cache after running the filtered automation */
$WooFunnels_Cache_obj->set_cache( $key, $v2_all_active_automations, 'autonami' );
} catch ( Error $e ) {
$WooFunnels_Cache_obj->reset_cache();
BWFAN_Common::log_test_data( 'FunnelKit Automations webhook received error: ' . $e->getMessage(), 'webhook-execution-error', true );
}
/** Set active v2 automations prop empty */
BWFAN_Core()->public->active_v2_automations = [];
$this->set_log( 'v2 webhook automation ends' );
$this->log();
return;
}
/** For automation v1 */
return $this->run_automations();
}
/**
* Run v1 automations and create tasks
*
* @return array|bool
*/
public function run_automations() {
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
if ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 || ! isset( $this->automations_arr[ $this->webhook_automation_id ] ) ) {
BWFAN_Core()->logger->log( 'Async callback: No active automations found. Event - ' . $this->get_slug(), $this->log_type );
return false;
}
$automation_actions = [];
$ran_actions = [];
$automation_data = $this->automations_arr[ $this->webhook_automation_id ];
if ( $this->get_slug() !== $automation_data['event'] || 0 !== intval( $automation_data['requires_update'] ) ) {
return false;
}
/** Check if the automation_key match with the post data */
if ( isset( $automation_data['event_meta']['bwfan_unique_key'] ) && $this->automation_key === $automation_data['event_meta']['bwfan_unique_key'] ) {
$ran_actions = $this->handle_single_automation_run( $automation_data, $this->webhook_automation_id );
}
$automation_actions[ $this->webhook_automation_id ] = $ran_actions;
$this->set_log( 'v1 webhook automation ends' );
$this->log();
return $automation_actions;
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$meta = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'event_meta' );
if ( '' === $meta || ! is_array( $meta ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['automation_key'] = $this->automation_key;
$data_to_send['global']['webhook_data'] = $this->webhook_data;
$data_to_send['global']['referer'] = $this->referer;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['email_map_key'] = $this->email_map_key;
return $data_to_send;
}
public function set_merge_tags_data( $task_meta ) {
$merge_data = [];
$merge_data['webhook_data'] = $task_meta['global']['webhook_data'];
$merge_data['referer'] = $task_meta['global']['referer'];
$merge_data['email'] = $task_meta['global']['email'];
BWFAN_Merge_Tag_Loader::set_data( $merge_data );
}
public function capture_v2_data( $automation_data ) {
$email_map = $automation_data['event_meta']['bwfan_email_map_key'];
$this->email_map_key = ! empty( $email_map ) ? $email_map : '';
$data = isset( $automation_data['webhook_data'] ) ? $automation_data['webhook_data'] : [];
if ( empty( $data ) ) {
$automation_data['email'] = '';
return $automation_data;
}
if ( is_array( $data ) && isset( $data[ $this->email_map_key ] ) ) {
$data = $data[ $this->email_map_key ];
} else {
$fieldKeys = explode( '.', $this->email_map_key );
foreach ( $fieldKeys as $val ) {
if ( isset( $data[ $val ] ) ) {
$data = $data[ $val ];
}
}
}
$this->email = ( ! is_array( $data ) && is_email( $data ) ) ? $data : '';
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [
[
'id' => 'bwfan_email_map_key',
'type' => 'webhook',
'label' => __( 'Select Email Field', 'wp-marketing-automations-pro' ),
'webhook_url' => rest_url( 'autonami/v1/webhook/' ) . '?bwfan_autonami_webhook_id={{automationId}}&bwfan_autonami_webhook_key={{uniqueKey}}',
'required' => false,
'hint' => "",
'showmap' => true,
]
];
}
/**
* Get automation id from webhook unique key
*
* @param $automation_key
*
* @return void
*/
public function get_webhook_automation_id( $automation_key ) {
global $wpdb;
$table = $wpdb->prefix . 'bwfan_automationmeta';
$automation_id = $wpdb->get_col( "SELECT `bwfan_automation_id` FROM $table WHERE `meta_key` = 'event_meta' AND `meta_value` LIKE '%" . $automation_key . "%'" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$this->webhook_automation_id = is_array( $automation_id ) && isset( $automation_id[0] ) ? $automation_id[0] : 0;
}
/**
* Get automation version
*
* @return void
*/
public function get_webhook_version() {
if ( ! empty( $this->webhook_version ) ) {
return;
}
$automation_data = BWFAN_Model_Automations::get( $this->webhook_automation_id );
if ( ! isset( $automation_data['v'] ) ) {
$this->webhook_version = 1;
return;
}
$this->webhook_version = $automation_data['v'];
}
public function set_log( $log ) {
if ( empty( $log ) ) {
return;
}
$this->logs[] = array(
't' => microtime( true ),
'm' => $log,
);
}
protected function log() {
if ( ! is_array( $this->logs ) || 0 === count( $this->logs ) ) {
return;
}
if ( false === apply_filters( 'bwfan_allow_webhook_logging', BWFAN_PRO_Common::is_log_enabled( 'bwfan_webhook_received_logging' ) ) ) {
return;
}
BWFAN_Common::log_test_data( $this->logs, 'fka-webhook-logs', true );
$this->logs = [];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
return 'BWFAN_Autonami_Webhook_Received';

View File

@@ -0,0 +1,293 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_Birthday_Reminder extends BWFAN_Event {
private static $instance = null;
public $contact_id = null;
/** @var WooFunnels_Contact $contact */
public $contact = null;
public $birthday = null;
public $user_id = null;
public $email = '';
private function __construct() {
$this->event_merge_tag_groups = array( 'wc_customer', 'bwf_contact' );
$this->event_name = esc_html__( 'Birthday Reminder', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs every day and checks for contact birth date which are about to come.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_field',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->support_lang = true;
$this->priority = 40;
$this->is_time_independent = true;
$this->v2 = true;
$this->support_v1 = false;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfan_birthday_triggered', array( $this, 'trigger_event' ), 10, 2 );
}
/**
* This is a time independent event. A cron is run once a day and it makes all the tasks for the current event.
*
* @param $automation_id
* @param $automation_details
*
* @throws Exception
*/
public function make_task_data( $automation_id, $automation_details ) {
global $wpdb;
$date_time = new DateTime();
$current_day = $date_time->format( 'Y-m-d' );
$last_run = BWFAN_Model_Automationmeta::get_meta( $automation_id, 'last_run' );
if ( false !== $last_run ) {
$where = array(
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
);
$data = array(
'meta_value' => $current_day,
);
BWFAN_Model_Automationmeta::update( $data, $where );
} else {
$meta = array(
'bwfan_automation_id' => $automation_id,
'meta_key' => 'last_run',
'meta_value' => $current_day,
);
BWFAN_Model_Automationmeta::insert( $meta );
}
$field_id = BWFCRM_Fields::get_field_id_by_slug( 'dob' );
if ( empty( $field_id ) ) {
return;
}
/** Check if active v2 automations found */
BWFAN_Core()->public->load_active_v2_automations( $this->get_slug() );
if ( ( ! is_array( $this->automations_v2_arr ) || count( $this->automations_v2_arr ) === 0 ) ) {
return;
}
$days_before = isset( $automation_details['meta']['event_meta']['days_before'] ) ? $automation_details['meta']['event_meta']['days_before'] : 0;
$days_before_birthday = isset( $automation_details['meta']['event_meta']['birthday_option'] ) && 'on_birthday' === $automation_details['meta']['event_meta']['birthday_option'] ? 0 : $days_before;
$date = new DateTime();
BWFAN_Common::convert_from_gmt( $date );
$date->modify( "+$days_before_birthday days" );
$dob = $date->format( 'm-d' );
$query = "SELECT `cid`, f{$field_id} as `dob` FROM `{$wpdb->prefix}bwf_contact_fields` WHERE DATE_FORMAT(`f{$field_id}`, '%m-%d') = '$dob'";
$contacts = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $contacts ) ) {
return;
}
/**
* recurring action schedule 2 mins interval
* event slug as option key - bwf_async_event_{slug} = birthdays
*/
$key = 'bwf_async_event_' . $this->get_slug() . '_' . $automation_id;
$args = [ 'key' => $key, 'aid' => $automation_id ];
if ( ! bwf_has_action_scheduled( 'bwfan_birthday_triggered', $args ) ) {
bwf_schedule_recurring_action( time(), 120, 'bwfan_birthday_triggered', $args );
bwf_options_update( $key, $contacts );
}
}
/**
* Birthday reminder event callback
*/
public function trigger_event( $option_key, $automation_id ) {
$contacts = bwf_options_get( $option_key );
if ( empty( $contacts ) ) {
bwf_options_delete( $option_key );
bwf_unschedule_actions( 'bwfan_birthday_triggered', [ 'key' => $option_key, 'aid' => $automation_id ] );
return;
}
/** Validate automation */
$automation = BWFAN_Model_Automations::get_automation_with_data( $automation_id );
if ( isset( $automation['meta'] ) ) {
$meta = $automation['meta'];
unset( $automation['meta'] );
$automation = array_merge( $automation, $meta );
}
if ( 1 !== intval( $automation['status'] ) || ( 0 === absint( $automation['start'] ) && 2 === absint( $automation['v'] ) ) ) {
bwf_options_delete( $option_key );
bwf_unschedule_actions( 'bwfan_birthday_triggered', [ 'key' => $option_key, 'aid' => $automation_id ] );
return;
}
$updated_contacts = $contacts;
$start_time = time();
foreach ( $contacts as $index => $contact ) {
if ( ! is_array( $contact ) || ! isset( $contact['cid'] ) ) {
continue;
}
/** Checking 10 seconds of processing */
if ( ( time() - $start_time ) > 10 ) {
return;
}
$this->process( $contact, $automation );
unset( $updated_contacts[ $index ] );
bwf_options_update( $option_key, $updated_contacts );
}
}
private function process( $contacts, $automation ) {
$this->birthday = $contacts['dob'];
$this->contact_id = $contacts['cid'];
$contact = new WooFunnels_Contact( '', '', '', $this->contact_id );
$this->email = $contact->get_email();
$this->user_id = $contact->get_wpid();
return $this->run_v2( $automation, $this->get_slug() );
}
public function get_event_data() {
$data_to_send = array();
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['birthday'] = $this->birthday;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['user_id'] = $this->user_id;
return $data_to_send;
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
public function get_user_id_event() {
return ! empty( intval( $this->user_id ) ) ? intval( $this->user_id ) : false;
}
/**
* v2 Method: Get fields schema
* @return array
*/
public function get_fields_schema() {
return [
[
'id' => 'birthday_option',
'label' => __( 'Runs', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( "Before contact's Birthday", 'wp-marketing-automations-pro' ),
'value' => 'before_birthday'
],
[
'label' => __( "On contact's Birthday", 'wp-marketing-automations-pro' ),
'value' => 'on_birthday'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => true,
"description" => ""
],
[
'id' => 'days_before',
'type' => 'number',
'value' => "15",
"min" => '0',
'label' => __( 'Days before birthday', 'wp-marketing-automations-pro' ),
"description" => "",
'toggler' => [
'fields' => [
[
'id' => 'birthday_option',
'value' => 'before_birthday',
]
]
],
],
[
'id' => 'scheduled-everyday-at',
'type' => 'expression',
'expression' => " {{hours/}} {{minutes /}}",
'label' => __( 'Schedule this automation to run everyday at', 'wp-marketing-automations-pro' ),
'fields' => [
[
"id" => 'hours',
"label" => '',
"type" => 'number',
"max" => '23',
"min" => '0',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "HH",
"description" => "",
"required" => false,
],
[
"id" => 'minutes',
"label" => '',
"type" => 'number',
"max" => '59',
"min" => '0',
"class" => 'bwfan-input-wrapper bwfan-input-s',
"placeholder" => "MM",
"description" => "",
"required" => false,
]
],
"description" => ""
],
];
}
/** Set default values */
public function get_default_values() {
return [
'birthday_option' => 'before_birthday',
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
return 'BWFAN_Birthday_Reminder';

View File

@@ -0,0 +1,639 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Assigned_List extends BWFAN_Event {
private static $instance = null;
public $lists = null;
public $list_id = null;
public $contact_id = null;
public $list_name = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwfan_crm_lists', 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Added to List', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after an list assigned to contact.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 20;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->is_goal = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfan_contact_added_to_lists', array( $this, 'process' ), 10, 2 );
}
public function process( $lists, $contact ) {
$this->lists = [];
if ( ! is_array( $lists ) && $lists instanceof BWFCRM_Lists ) {
$this->lists[] = $lists;
} else {
$this->lists = $lists;
}
$this->contact_id = ( $contact instanceof BWFCRM_Contact ) ? $contact->get_id() : 0;
$this->email = ( $contact instanceof BWFCRM_Contact ) ? $contact->contact->get_email() : '';
$this->run_automations();
}
public function run_automations() {
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_PRO_Common::disable_run_v2_automation_immediately();
$automation_actions = [];
foreach ( $this->lists as $list ) {
if ( ! $list instanceof BWFCRM_Lists ) {
continue;
}
$this->list_id = $list->get_id();
$this->list_name = $list->get_name();
/** v2 run starts */
$contact_data_v2 = array(
'contact_id' => absint( $this->contact_id ),
'email' => $this->email,
'list_id' => $this->list_id,
'list_name' => $this->list_name
);
BWFAN_Common::maybe_run_v2_automations( $this->get_slug(), $contact_data_v2 );
/** v1 run starts */
if ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) {
continue;
}
/** Checking for event settings */
$automation_arr = $this->validate_event_data_before_creating_task( $this->automations_arr );
if ( ! is_array( $automation_arr ) || count( $automation_arr ) === 0 ) {
continue;
}
foreach ( $automation_arr as $automation_id => $automation_data ) {
if ( $this->get_slug() !== $automation_data['event'] || 0 !== intval( $automation_data['requires_update'] ) ) {
continue;
}
$ran_actions = $this->handle_single_automation_run( $automation_data, $automation_id );
$automation_actions[ $automation_id ] = $ran_actions;
}
}
return $automation_actions;
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
$list = ! empty( $automation_data['event_meta']['list'] ) ? $automation_data['event_meta']['list'] : 'any';
$runs = isset( $automation_data['event_meta']['bwfan_automation_run'] ) ? $automation_data['event_meta']['bwfan_automation_run'] : 'multiple';
$run_count = 0;
if ( 'once' === $runs ) {
$run_count = BWFAN_Model_Automations::get_contact_automation_run_count( $automation_id, $this->contact_id );
}
/** selected any list and runs multiple times selected */
if ( 'any' === $list && 'multiple' === $runs ) {
continue;
}
/** selected list id and run multiple times selected */
if ( absint( $this->list_id ) === absint( $list ) && 'multiple' === $runs ) {
continue;
}
if ( 'any' === $list && 'once' === $runs && 0 === $run_count ) {
continue;
}
if ( absint( $this->list_id ) === absint( $list ) && 'once' === $runs && 0 === $run_count ) {
continue;
}
unset( $automations_arr_temp[ $automation_id ] );
}
return $automations_arr_temp;
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['list_id'] = $this->list_id;
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['list_name'] = $this->list_name;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$list_id = isset( $global_data['list_id'] ) ? $global_data['list_id'] : 0;
$list_url = admin_url( "admin.php?page=autonami&path=/manage/lists&s=" . $global_data['list_name'] );
$contact_url = admin_url( "admin.php?page=autonami&path=/contact/" . $global_data['contact_id'] );
?>
<li>
<strong><?php echo esc_html__( 'List Id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo esc_attr( $list_url ); ?>"><?php echo esc_html__( $list_id ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'List Name:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['list_name'] ); ?></span>
</li>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a href="<?php echo esc_attr( $contact_url ); ?>"><?php echo esc_html__( $global_data['contact_id'] ); ?></a>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/javascript" defer>
jQuery(document).ready(function ($) {
$(document.body).on('click', '.item_modify_trigger', function () {
setTimeout(function () {
$('#bwfcrm-select-list').select2({
placeholder: 'Search list',
minimumInputLength: 2,
tags: false,
data: [],
escapeMarkup(m) {
return m;
},
ajax: {
url: ajaxurl,
dataType: 'json',
type: 'POST',
delay: 250,
data(term) {
return {
search_term: term,
action: 'bwfan_select2ajax',
type: $('#bwfcrm-select-list').attr('data-search'),
_wpnonce: bwfanParams.ajax_nonce,
};
},
processResults: function (data) {
var options = [];
if (data) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each(data, function (index, text) { // do not forget that "index" is just auto incremented value
options.push({id: text.id, text: text.text});
});
}
return {
results: options
};
},
cache: true
}
});
}, 300)
});
$('body').on('change', '.bwfan-list-search', function () {
var temp_list = {id: $(this).val(), name: $(this).find(':selected').text()};
$(this).parent().find('.bwfan_searched_list').val(JSON.stringify(temp_list));
});
});
</script>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
selected_list = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'list')) ? data.eventSavedData.list : '';
selected_runs = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'runs')) ? data.eventSavedData.runs : '';
searched_list = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'searched_list')) ? data.eventSavedData.searched_list : '';
if(!_.isEmpty(searched_list)) {
try {
searched_list = JSON.parse(searched_list);
}
catch(e) {
//Do Nothing
}
}
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Select List', 'wp-marketing-automations-pro' ); ?>
</label>
<select id="bwfcrm-select-list" data-search="list" data-search-text="<?php esc_attr_e( 'Select Tag', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-list-search bwfan-input-wrapper" name="event_meta[list]">
<#
if(_.size(searched_list) >0) {
temp_selected_list = _.isObject(searched_list) ? searched_list : JSON.parse(searched_list);
if(temp_selected_list.id == selected_list){
#>
<option value="{{temp_selected_list.id}}" selected>{{temp_selected_list.name}}</option>
<#
}
}
stringify_searched_list = _.isObject(searched_list) ? JSON.stringify(searched_list) :
searched_list;
#>
</select>
<input type="hidden" class="bwfan_searched_list" name="event_meta[searched_list]" value="{{stringify_searched_list}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Runs', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[runs]">
<option value="once" {{
'once' == selected_runs?'selected':''
}}><?php esc_html_e( 'Once', 'wp-marketing-automations-pro' ); ?></option>
<option value="multiple_times" {{
'multiple_times' == selected_runs?'selected':''
}}><?php esc_html_e( 'Multiple Times', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<?php
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'list_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['list_id'] ) ) ) {
$set_data = array(
'list_id' => intval( $task_meta['global']['list_id'] ),
'email' => $task_meta['global']['email'],
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'list_name' => $task_meta['global']['list_name'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->list_id, 'list_id' );
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->list_name, 'list_name' );
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_list( $data );
}
public function validate_list( $data ) {
if ( ! isset( $data['list_id'] ) || ! isset( $data['contact_id'] ) ) {
return false;
}
$crm_list = new BWFCRM_Lists( $data['list_id'] );
if ( ! $crm_list->is_exists() ) {
$this->message_validate_event = __( 'List not exist.', 'wp-marketing-automations-pro' );
return false;
}
$contact = new BWFCRM_Contact( $data['contact_id'] );
$lists = $contact->get_lists();
if ( ! in_array( $data['list_id'], $lists ) ) {
$this->message_validate_event = __( 'Contact is not in the list.', 'wp-marketing-automations-pro' );
return false;
}
return true;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
if ( ! isset( $automation_data['event_meta'] ) ) {
return true;
}
/** If key is not set, then we will assign */
if ( ! isset( $automation_data['event_meta']['list-contains'] ) ) {
if ( ! isset( $automation_data['event_meta']['list'] ) || empty( $automation_data['event_meta']['list'] ) ) {
$automation_data['event_meta']['list-contains'] = 'any';
}
}
if ( isset( $automation_data['event_meta']['list-contains'] ) && 'any' === $automation_data['event_meta']['list-contains'] ) {
return true;
}
/** Selected lists */
$list_ids = array_map( function ( $list ) {
return intval( $list['id'] );
}, $automation_data['event_meta']['list'] );
if ( empty( $list_ids ) ) {
return true;
}
$assigned_list = intval( $automation_data['list_id'] );
return in_array( $assigned_list, $list_ids, true );
}
/**
* v2 Method: Get fields schema
* @return array[][]
*/
public function get_fields_schema() {
return [
[
'id' => 'list-contains',
'label' => __( 'List Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Specific List', 'wp-marketing-automations-pro' ),
'value' => 'selected_list'
],
[
'label' => __( 'Any List', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => true,
"description" => ""
],
[
'id' => 'list',
'label' => __( 'Select List', 'wp-marketing-automations-pro' ),
'type' => 'search',
'autocompleter' => 'lists',
'class' => 'bwfan-col-sm-9',
'placeholder' => '',
'required' => true,
'toggler' => [
'fields' => [
[
'id' => 'list-contains',
'value' => 'selected_list',
]
]
],
],
];
}
public function get_view_data() {
$all_lists = BWFCRM_Lists::get_lists();
if ( empty( $all_lists ) ) {
return array();
}
$list_data = array();
foreach ( $all_lists as $list ) {
$list_data[ $list['ID'] ] = $list['name'];
}
return $list_data;
}
public function validate_goal_settings( $automation_settings, $automation_data ) {
$list_contain = isset( $automation_settings['list-contains'] ) ? $automation_settings['list-contains'] : 'any';
if ( 'any' === $list_contain ) {
return true;
}
/** Goal settings lists not set */
if ( empty ( $automation_settings['list'] ) || 0 === count( $automation_settings['list'] ) ) {
return false;
}
/** Goal settings lists */
$lists = array_column( $automation_settings['list'], 'id' );
if ( empty( $lists ) ) {
return false;
}
return in_array( $automation_data['list_id'], $lists );
}
/**
* @param $lists
* @param $automation_data
* @param $v
*
* @return bool|void
*/
public function validate_bulk_action_event_settings( $lists, $automation_data, $v ) {
$list_ids = array_map( function ( $list ) {
return absint( $list['id'] );
}, $lists );
if ( 1 === absint( $v ) ) {
$list = $automation_data['meta']['event_meta']['list'];
if ( 'any' === $list ) {
return true;
}
return in_array( $list, $list_ids );
}
if ( 2 === absint( $v ) ) {
$list_contain = isset( $automation_data['meta']['event_meta']['list-contains'] ) ? $automation_data['meta']['event_meta']['list-contains'] : 'any';
if ( 'any' === $list_contain ) {
return true;
}
$lists = $automation_data['meta']['event_meta']['list'];
$event_list_ids = empty( $lists ) ? [] : array_column( $lists, 'id' );
return ( count( array_intersect( $list_ids, $event_list_ids ) ) > 0 );
}
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['list'] ) || empty( $data['list'] ) ) {
return '';
}
$lists = [];
foreach ( $data['list'] as $list ) {
if ( ! isset( $list['name'] ) || empty( $list['name'] ) ) {
continue;
}
$lists[] = $list['name'];
}
return $lists;
}
/** set default values */
public function get_default_values() {
return [
'list-contains' => 'selected_list',
];
}
/** Set default goal values */
public function get_default_goal_values() {
return [
'list-contains' => 'any',
];
}
public function pre_validate_goal( $step_data, $contact_id ) {
if ( empty( $contact_id ) ) {
return false;
}
$list_contain = $step_data['sidebarData']['list-contains'] ?? 'any';
if ( 'any' === $list_contain ) {
return false;
}
$contact = new WooFunnels_Contact( '', '', '', $contact_id );
$contact_lists = ! empty( $contact->get_lists() ) ? $contact->get_lists() : [];
$goal_lists = isset( $step_data['sidebarData']['list'] ) ? array_column( $step_data['sidebarData']['list'], 'id' ) : [];
$common_lists = array_intersect( $goal_lists, $contact_lists );
return ( count( $common_lists ) > 0 );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$contact_lists = $contact->get_lists();
/** return if no lists available as event is added to list */
if ( empty( $contact_lists ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'There are no assigned lists for the contact', 'wp-marketing-automations-pro' ) ];
}
if ( isset( $automation_data['event_meta']['list-contains'] ) && 'selected_list' === $automation_data['event_meta']['list-contains'] ) {
$event_lists = $automation_data['event_meta']['list'];
$matched_lists = array_values( array_filter( $event_lists, function ( $list ) use ( $contact_lists ) {
return in_array( $list['id'], $contact_lists, true );
} ) );
/** return if no lists are matched with contact lists */
if ( empty( $matched_lists ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No contact lists are matching with event lists' . 'wp-marketing-automations-pro' ) ];
}
$contact_lists = array_column( $matched_lists, 'id' );
}
$this->contact_id = $cid;
$this->email = $contact->get_email();
$this->list_id = $contact_lists[0];
$this->list_name = BWFCRM_Lists::get_lists( array( $contact_lists[0] ) )[0]['name'];
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email, 'list_id' => $this->list_id, 'list_name' => $this->list_name ] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Lists' ) ) {
return 'BWFAN_CRM_Assigned_List';
}

View File

@@ -0,0 +1,650 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Assigned_Tag extends BWFAN_Event {
private static $instance = null;
public $tag_id = null;
public $tags = null;
public $contact_id = null;
public $tag_name = null;
public $email = null;
public $user_id = null;
/** v2 */
public $contact_data_v2 = array();
private function __construct() {
$this->event_merge_tag_groups = array( 'bwfan_crm_tags', 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Tag is Added', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after an tag assigned to contact.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_field',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 10;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->is_goal = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfan_tags_added_to_contact', array( $this, 'process' ), 10, 2 );
}
public function process( $tags, $contact ) {
$this->tags = [];
if ( ! is_array( $tags ) && $tags instanceof BWFCRM_Tag ) {
$this->tags[] = $tags;
} else {
$this->tags = $tags;
}
$this->contact_id = ( $contact instanceof BWFCRM_Contact ) ? $contact->get_id() : 0;
$this->email = ( $contact instanceof BWFCRM_Contact ) ? $contact->contact->get_email() : '';
$this->run_automations();
}
public function run_automations() {
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_PRO_Common::disable_run_v2_automation_immediately();
$automation_actions = [];
foreach ( $this->tags as $tag ) {
if ( ! $tag instanceof BWFCRM_Tag ) {
continue;
}
$this->tag_id = $tag->get_id();
$this->tag_name = $tag->get_name();
/** v2 run starts */
$contact_data_v2 = array(
'contact_id' => absint( $this->contact_id ),
'email' => $this->email,
'tag_id' => $this->tag_id,
'tag_name' => $this->tag_name
);
BWFAN_Common::maybe_run_v2_automations( $this->get_slug(), $contact_data_v2 );
/** v1 run starts */
if ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) {
continue;
}
/** Checking for event settings */
$automation_arr = $this->validate_event_data_before_creating_task( $this->automations_arr );
if ( ! is_array( $automation_arr ) || count( $automation_arr ) === 0 ) {
continue;
}
foreach ( $automation_arr as $automation_id => $automation_data ) {
if ( $this->get_slug() !== $automation_data['event'] || 0 !== intval( $automation_data['requires_update'] ) ) {
continue;
}
$ran_actions = $this->handle_single_automation_run( $automation_data, $automation_id );
$automation_actions[ $automation_id ] = $ran_actions;
}
}
return $automation_actions;
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
$tag = ! empty( $automation_data['event_meta']['tag'] ) ? $automation_data['event_meta']['tag'] : 'any';
$runs = isset( $automation_data['event_meta']['bwfan_automation_run'] ) ? $automation_data['event_meta']['bwfan_automation_run'] : 'multiple';
$run_count = 0;
if ( 'once' === $runs ) {
$run_count = BWFAN_Model_Automations::get_contact_automation_run_count( $automation_id, $this->contact_id );
}
/** selected any tag and runs multiple times selected */
if ( 'any' === $tag && 'multiple' === $runs ) {
continue;
}
/** selected tag id and run multiple times selected */
if ( absint( $this->tag_id ) === absint( $tag ) && 'multiple' === $runs ) {
continue;
}
if ( 'any' === $tag && 'once' === $runs && 0 === $run_count ) {
continue;
}
if ( absint( $this->tag_id ) === absint( $tag ) && 'once' === $runs && 0 === $run_count ) {
continue;
}
unset( $automations_arr_temp[ $automation_id ] );
}
return $automations_arr_temp;
}
public function get_view_data() {
$all_tags = BWFCRM_Tag::get_tags();
if ( empty( $all_tags ) ) {
return array();
}
$tag_data = array();
foreach ( $all_tags as $tag ) {
$tag_data[ $tag['ID'] ] = $tag['name'];
}
return $tag_data;
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['tag_id'] = $this->tag_id;
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['tag_name'] = $this->tag_name;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$tag_id = isset( $global_data['tag_id'] ) ? $global_data['tag_id'] : 0;
$tag_url = admin_url( "admin.php?page=autonami&path=/manage/tags&s=" . $global_data['tag_name'] );
$contact_url = admin_url( "admin.php?page=autonami&path=/contact/" . $global_data['contact_id'] );
?>
<li>
<strong><?php echo esc_html__( 'Tag Id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo esc_attr( $tag_url ); ?>"><?php echo esc_html__( $tag_id ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Tag Name:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['tag_name'] ); ?></span>
</li>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a href="<?php echo esc_attr( $contact_url ); ?>"><?php echo esc_html__( $global_data['contact_id'] ); ?></a>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script>
jQuery(document).ready(function ($) {
$(document.body).on('click', '.item_modify_trigger', function () {
setTimeout(select2Tag, 300);
function select2Tag() {
$('#bwfcrm-select-tag').select2({
placeholder: 'Search tag',
minimumInputLength: 2,
tags: false,
data: [],
escapeMarkup(m) {
return m;
},
ajax: {
url: ajaxurl,
dataType: 'json',
type: 'POST',
delay: 250,
data(term) {
return {
search_term: term,
action: 'bwfan_select2ajax',
type: $('#bwfcrm-select-tag').attr('data-search'),
_wpnonce: bwfanParams.ajax_nonce,
};
},
processResults: function (data) {
var options = [];
if (data) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each(data, function (index, text) { // do not forget that "index" is just auto incremented value
options.push({id: text.id, text: text.text});
});
}
return {
results: options
};
},
cache: true
}
});
}
});
$('body').on('change', '.bwfan-tag-search', function () {
var temp_tag = {id: $(this).val(), name: $(this).find(':selected').text()};
$(this).parent().find('.bwfan_searched_tag').val(JSON.stringify(temp_tag));
});
});
</script>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
selected_runs = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'runs')) ? data.eventSavedData.runs : '';
selected_tag = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'tag')) ? data.eventSavedData.tag : '';
searched_tag = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'searched_tag')) ? data.eventSavedData.searched_tag : '';
if(!_.isEmpty(searched_tag)) {
try {
searched_tag = JSON.parse(searched_tag);
}
catch(e) {
//Do Nothing
}
}
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Select Tag', 'wp-marketing-automations-pro' ); ?>
</label>
<select id="bwfcrm-select-tag" data-search="tag" data-search-text="<?php esc_attr_e( 'Select Tag', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-tag-search bwfan-input-wrapper" name="event_meta[tag]">
<#
if(_.size(searched_tag) >0) {
temp_selected_tag = _.isObject(searched_tag) ? searched_tag : JSON.parse(searched_tag);
if(temp_selected_tag.id == selected_tag){
#>
<option value="{{temp_selected_tag.id}}" selected>{{temp_selected_tag.name}}</option>
<#
}
}
stringify_searched_tag = _.isObject(searched_tag) ? JSON.stringify(searched_tag) :
searched_tag;
#>
</select>
<input type="hidden" class="bwfan_searched_tag" name="event_meta[searched_tag]" value="{{stringify_searched_tag}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Runs', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[runs]">
<option value="once" {{
'once' == selected_runs?'selected':''
}}><?php esc_html_e( 'Once', 'wp-marketing-automations-pro' ); ?></option>
<option value="multiple_times" {{
'multiple_times' == selected_runs?'selected':''
}}><?php esc_html_e( 'Multiple Times', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<?php
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'tag_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['tag_id'] ) ) ) {
$set_data = array(
'tag_id' => intval( $task_meta['global']['tag_id'] ),
'email' => $task_meta['global']['email'],
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'tag_name' => $task_meta['global']['tag_name'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->tag_id, 'tag_id' );
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->tag_name, 'tag_name' );
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_tag( $data );
}
public function validate_tag( $data ) {
if ( ! isset( $data['tag_id'] ) || ! isset( $data['contact_id'] ) ) {
return false;
}
$crm_tag = new BWFCRM_Tag( $data['tag_id'] );
if ( ! $crm_tag->is_exists() ) {
$this->message_validate_event = __( 'Tag not exist.', 'wp-marketing-automations-pro' );
return false;
}
$contact = new BWFCRM_Contact( $data['contact_id'] );
$tags = $contact->get_tags();
if ( ! in_array( $data['tag_id'], $tags ) ) {
$this->message_validate_event = __( 'Contact is not in the tag.', 'wp-marketing-automations-pro' );
return false;
}
return true;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
if ( ! isset( $automation_data['event_meta'] ) ) {
return true;
}
/** If key is not set, then we will assign */
if ( ! isset( $automation_data['event_meta']['tag-contains'] ) ) {
if ( ! isset( $automation_data['event_meta']['tags'] ) || empty( $automation_data['event_meta']['tags'] ) ) {
$automation_data['event_meta']['tag-contains'] = 'any';
}
}
if ( isset( $automation_data['event_meta']['tag-contains'] ) && 'any' === $automation_data['event_meta']['tag-contains'] ) {
return true;
}
/** Selected tags */
$tag_ids = array_map( function ( $tag ) {
return intval( $tag['id'] );
}, $automation_data['event_meta']['tags'] );
if ( empty( $tag_ids ) ) {
return true;
}
$assigned_tag = intval( $automation_data['tag_id'] );
return in_array( $assigned_tag, $tag_ids, true );
}
/**
* v2 Method: Get fields schema
* @return array[][]
*/
public function get_fields_schema() {
return [
[
'id' => 'tag-contains',
'label' => __( 'Tag Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Specific Tag', 'wp-marketing-automations-pro' ),
'value' => 'selected_tag'
],
[
'label' => __( 'Any Tag', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => true,
"description" => ""
],
[
'id' => 'tags',
'label' => __( 'Select Tags', 'wp-marketing-automations-pro' ),
'type' => 'search',
'autocompleter' => 'tags',
'class' => 'bwfan-col-sm-9',
'placeholder' => '',
'required' => true,
'toggler' => [
'fields' => [
[
'id' => 'tag-contains',
'value' => 'selected_tag',
]
]
],
],
];
}
public function validate_goal_settings( $automation_settings, $automation_data ) {
$tag_contain = isset( $automation_settings['tag-contains'] ) ? $automation_settings['tag-contains'] : 'any';
if ( 'any' === $tag_contain ) {
return true;
}
/** Goal settings tags not set */
if ( empty( $automation_settings['tags'] ) || 0 === count( $automation_settings['tags'] ) ) {
return false;
}
/** Goal settings tags */
$tags = array_column( $automation_settings['tags'], 'id' );
if ( empty( $tags ) ) {
return false;
}
return in_array( $automation_data['tag_id'], $tags );
}
/**
* @param $tags
* @param $automation_data
* @param $v
*
* @return bool|void
*/
public function validate_bulk_action_event_settings( $tags, $automation_data, $v ) {
$tag_ids = array_map( function ( $tag ) {
return absint( $tag['id'] );
}, $tags );
if ( 1 === absint( $v ) ) {
$tag = $automation_data['meta']['event_meta']['tag'];
if ( 'any' === $tag ) {
return true;
}
return in_array( $tag, $tag_ids );
}
if ( 2 === absint( $v ) ) {
$tag_contain = isset( $automation_data['meta']['event_meta']['tag-contains'] ) ? $automation_data['meta']['event_meta']['tag-contains'] : 'any';
if ( 'any' === $tag_contain ) {
return true;
}
$tags = $automation_data['meta']['event_meta']['tags'];
$event_tag_ids = empty( $tags ) ? [] : array_column( $tags, 'id' );
return ( count( array_intersect( $tag_ids, $event_tag_ids ) ) > 0 );
}
}
/**
* Returns description text
*
* @param $data
*
* @return array|string
*/
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['tags'] ) || empty( $data['tags'] ) ) {
return '';
}
$tags = [];
foreach ( $data['tags'] as $tag ) {
if ( ! isset( $tag['name'] ) || empty( $tag['name'] ) ) {
continue;
}
$tags[] = $tag['name'];
}
return $tags;
}
/** set default values */
public function get_default_values() {
return [
'tag-contains' => 'selected_tag',
];
}
/** Set default goal values */
public function get_default_goal_values() {
return [
'tag-contains' => 'any',
];
}
public function pre_validate_goal( $step_data, $contact_id ) {
if ( empty( $contact_id ) ) {
return false;
}
$tag_contain = $step_data['sidebarData']['tag-contains'] ?? 'any';
if ( 'any' === $tag_contain ) {
return false;
}
$contact = new WooFunnels_Contact( '', '', '', $contact_id );
$contact_tags = ! empty( $contact->get_tags() ) ? $contact->get_tags() : [];
$goal_tags = isset( $step_data['sidebarData']['tags'] ) ? array_column( $step_data['sidebarData']['tags'], 'id' ) : [];
$common_tags = array_intersect( $goal_tags, $contact_tags );
return ( count( $common_tags ) > 0 );
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$contact_tags = $contact->get_tags();
/** return if no tags available as event is tag added */
if ( empty( $contact_tags ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'There are no assigned tags for the contact', 'wp-marketing-automations-pro' ) ];
}
if ( isset( $automation_data['event_meta']['tag-contains'] ) && 'selected_tag' === $automation_data['event_meta']['tag-contains'] ) {
$event_tags = $automation_data['event_meta']['tags'];
$matched_tags = array_values( array_filter( $event_tags, function ( $tag ) use ( $contact_tags ) {
return in_array( $tag['id'], $contact_tags, true );
} ) );
/** return if no tags are matched with contact tags */
if ( empty( $matched_tags ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No contact tags are matching with event tags', 'wp-marketing-automations-pro' ) ];
}
$contact_tags = array_column( $matched_tags, 'id' );
}
$this->contact_id = $cid;
$this->email = $contact->get_email();
$this->tag_id = $contact_tags[0];
$this->tag_name = BWFCRM_Tag::get_tags( array( $contact_tags[0] ) )[0]['name'];
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email, 'tag_id' => $this->tag_id, 'tag_name' => $this->tag_name ] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Tag' ) ) {
return 'BWFAN_CRM_Assigned_Tag';
}

View File

@@ -0,0 +1,108 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Contact_Bounced extends BWFAN_Event {
private static $instance = null;
public $contact_id = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Contact Bounced', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after contact is bounced.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 31;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->support_v1 = false;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfcrm_after_contact_bounced', [ $this, 'process' ], 10 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $contact
*/
public function process( $contact ) {
if ( ! $contact instanceof WooFunnels_Contact ) {
return;
}
$data = $this->get_default_data();
$data['contact_id'] = $contact->get_id();
$data['email'] = $contact->get_email();
$this->send_async_call( $data );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['contact_id'] = $this->contact_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Contact' ) ) {
return 'BWFAN_CRM_Contact_Bounced';
}

View File

@@ -0,0 +1,244 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Contact_Subscribed extends BWFAN_Event {
private static $instance = null;
public $contact_id = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Contact Subscribes', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after contact is subscribed.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 30;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->is_goal = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfcrm_after_contact_subscribed', [ $this, 'process' ], 10 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $contact
*/
public function process( $contact ) {
if ( ! $contact instanceof WooFunnels_Contact ) {
return;
}
$data = $this->get_default_data();
$data['contact_id'] = $contact->get_id();
$data['email'] = $contact->get_email();
$this->send_async_call( $data );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$contact_id = isset( $global_data['contact_id'] ) ? $global_data['contact_id'] : 0;
$contact_url = admin_url( "admin.php?page=autonami&path=/contact/" . $contact_id );
?>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a href="<?php echo esc_attr( $contact_url ); ?>"><?php echo esc_html__( $global_data['contact_id'] ); ?></a>
</li>
<?php
return ob_get_clean();
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'contact_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['contact_id'] ) ) ) {
$set_data = array(
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'email' => $task_meta['global']['email'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['contact_id'] = $this->contact_id;
$automation_data['email'] = $this->email;
return $automation_data;
}
public function get_fields_schema() {
return [];
}
public function validate_goal_settings( $automation_settings, $automation_data ) {
/** settings not found */
if ( ! isset( $automation_data['event'] ) ) {
return false;
}
return $automation_data['event'] === $this->get_slug();
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new BWFCRM_Contact( $cid );
/** Check if contact exists */
if ( ! $contact->is_contact_exists() ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
/** Return if contact is not subscribed */
if ( BWFCRM_Contact::$DISPLAY_STATUS_SUBSCRIBED !== intval( $contact->get_display_status() ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'Contact status is not Subscribed', 'wp-marketing-automations-pro' ) ];
}
$this->contact_id = $contact->get_id();
$this->email = $contact->contact->get_email();
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email ] );
}
public function pre_validate_goal( $step_data, $contact_id ) {
if ( empty( $contact_id ) ) {
return false;
}
$contact = new WooFunnels_Contact( '', '', '', $contact_id );
$is_unsubscribed = $this->check_contact_unsubscribed( $contact );
return ( 1 === intval( $contact->get_status() ) && empty( $is_unsubscribed ) );
}
/**
* @param $contact WooFunnels_Contact
*
* @return array|object|string|null
*/
public function check_contact_unsubscribed( $contact ) {
$email = $contact->get_email();
$contact_no = $contact->get_contact_no();
$data = array(
'recipient' => array( $email, $contact_no ),
);
return BWFAN_Model_Message_Unsubscribe::get_message_unsubscribe_row( $data, true );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Contact' ) ) {
return 'BWFAN_CRM_Contact_Subscribed';
}

View File

@@ -0,0 +1,265 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Contact_UnSubscribed extends BWFAN_Event {
private static $instance = null;
public $phone = null;
public $email = null;
public $object_id = null;
public $object_type = null;
public $contact_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Contact Unsubscribes', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after contact is unsubscribed.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 30;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfcrm_after_contact_unsubscribed', [ $this, 'process' ], 10, 1 );
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $contact
*/
public function process( $insert_data ) {
if ( ! is_array( $insert_data ) ) {
return;
}
$data = $this->get_default_data();
foreach ( $insert_data as $data_key => $unsubscribe_data ) {
$mode = is_email( trim( $unsubscribe_data['recipient'] ) ) ? 1 : 2;
$data['email'] = 1 === absint( $mode ) ? $unsubscribe_data['recipient'] : '';
$data['phone'] = 2 === absint( $mode ) ? $unsubscribe_data['recipient'] : '';
$data['object_id'] = isset( $unsubscribe_data['automation_id'] ) ? $unsubscribe_data['automation_id'] : 0;
$data['object_type'] = isset( $unsubscribe_data['c_type'] ) ? $unsubscribe_data['c_type'] : 3;
$data['contact_id'] = ( new WooFunnels_Contact( '', $data['email'], $data['phone'] ) )->get_id();
if ( empty( $data['email'] ) ) {
$data['email'] = $this->email;
}
$this->send_async_call( $data );
}
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['phone'] = $this->phone;
$data_to_send['global']['object_id'] = $this->object_id;
$data_to_send['global']['object_type'] = $this->object_type;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$contact_id = isset( $global_data['contact_id'] ) ? $global_data['contact_id'] : 0;
$contact_url = admin_url( "admin.php?page=autonami&path=/contact/" . $contact_id );
if ( ! empty( $contact_id ) ) {
?>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a href="<?php echo esc_attr( $contact_url ); ?>"><?php echo esc_html__( $global_data['contact_id'] ); ?></a>
</li>
<?php
}
if ( isset( $global_data['email'] ) && ! empty( $global_data['email'] ) ) {
?>
<li>
<strong><?php echo esc_html__( 'Email:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['email'] ); ?>
</li>
<?php
}
if ( isset( $global_data['phone'] ) && ! empty( $global_data['phone'] ) ) {
?>
<li>
<strong><?php echo esc_html__( 'Phone:', 'wp-marketing-automations-pro' ); ?> </strong>
<?php echo esc_html__( $global_data['phone'] ); ?>
</li>
<?php
}
return ob_get_clean();
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$this->phone = BWFAN_Common::$events_async_data['phone'];
$this->object_id = BWFAN_Common::$events_async_data['object_id'];
$this->object_type = BWFAN_Common::$events_async_data['object_type'];
if ( empty( $this->email ) ) {
return;
}
return $this->run_automations();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'contact_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['contact_id'] ) ) ) {
$set_data = array(
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'email' => $task_meta['global']['email'],
'phone' => $task_meta['global']['phone'],
'object_type' => $task_meta['global']['object_type'],
'object_id' => $task_meta['global']['object_id'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->phone, 'phone' );
}
/**
* Capture the async data for the current event.
*
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$this->phone = BWFAN_Common::$events_async_data['phone'];
$this->object_id = BWFAN_Common::$events_async_data['object_id'];
$this->object_type = BWFAN_Common::$events_async_data['object_type'];
if ( empty( $this->email ) ) {
return false;
}
$automation_data['contact_id'] = $this->contact_id;
$automation_data['email'] = $this->email;
$automation_data['phone'] = $this->phone;
$automation_data['object_id'] = $this->object_id;
$automation_data['object_type'] = $this->object_type;
return $automation_data;
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new BWFCRM_Contact( $cid );
/** Check if contact exists */
if ( ! $contact->is_contact_exists() ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
/** Return if contact is un-subscribed */
if ( BWFCRM_Contact::$DISPLAY_STATUS_SUBSCRIBED !== intval( $contact->get_display_status() ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'Contact status is not Un-subscribed', 'wp-marketing-automations-pro' ) ];
}
$this->contact_id = $contact->get_id();
$this->email = $contact->contact->get_email();
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email ] );
}
public function get_fields_schema() {
return [];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
return 'BWFAN_CRM_Contact_UnSubscribed';

View File

@@ -0,0 +1,512 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Field_Updated extends BWFAN_Event {
private static $instance = null;
public $field_id = null;
public $contact_id = null;
public $old_value = null;
public $new_value = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwfan_crm_fields', 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Field Updated', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after an contact field updated.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 25;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfcrm_contact_field_updated', array( $this, 'process' ), 10, 4 );
add_action( 'bwfcrm_contact_custom_field_updated', array( $this, 'process' ), 10, 4 );
}
/**
* Localize data for html fields for the current event.
*/
public function admin_enqueue_assets() {
if ( false === BWFAN_Common::is_load_admin_assets( 'automation' ) ) {
return;
}
$fields = $this->get_view_data();
BWFAN_Core()->admin->set_events_js_data( $this->get_slug(), 'fields', $fields );
}
public function get_view_data() {
$all_fields = BWFCRM_Fields::get_fields();
if ( empty( $all_fields ) ) {
return array();
}
$field_data = array();
foreach ( $all_fields as $field_key => $field ) {
$field_data[ $field_key ] = ! empty( $field['name'] ) ? $field['name'] : $field;
}
return $field_data;
}
/**
* Make the required data for the current event and send it asynchronously.
*
* @param $field_id
* @param $new_value
* @param $old_value
* @param $contact_id
*
* @return void
*/
public function process( $field_id, $new_value, $old_value, $contact_id ) {
if ( empty( $field_id ) ) {
return;
}
$data = $this->get_default_data();
$data['field_id'] = $field_id;
$data['new_value'] = $new_value;
$data['old_value'] = $old_value;
$data['contact_id'] = $contact_id;
/** class exists than get the bwfcrm contact object */
$contact_object = new BWFCRM_Contact( $contact_id );
$data['email'] = ( $contact_object instanceof BWFCRM_Contact ) ? $contact_object->contact->get_email() : '';
$this->send_async_call( $data );
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['field_id'] = $this->field_id;
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['new_value'] = $this->new_value;
$data_to_send['global']['old_value'] = $this->old_value;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$field_id = isset( $global_data['field_id'] ) ? $global_data['field_id'] : 0;
?>
<li>
<strong><?php echo esc_html__( 'Field Id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href=""><?php echo esc_html__( $field_id ); ?></a>
</li>
<?php
if ( ! empty( $global_data['old_value'] ) ) {
?>
<li>
<strong><?php echo esc_html__( 'Old Value:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['old_value'] ); ?></span>
</li>
<?php
}
?>
<li>
<strong><?php echo esc_html__( 'New Value:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['new_value'] ); ?></span>
</li>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['contact_id'] ); ?></span>
</li>
<?php
return ob_get_clean();
}
/**
* Capture the async data for the current event.
* @return array|bool
*/
public function capture_async_data() {
$this->field_id = BWFAN_Common::$events_async_data['field_id'];
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->old_value = BWFAN_Common::$events_async_data['old_value'];
$this->new_value = BWFAN_Common::$events_async_data['new_value'];
$this->email = BWFAN_Common::$events_async_data['email'];
return $this->run_automations();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
selected_field = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'field')) ? data.eventSavedData.field : '';
selected_runs = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'runs')) ? data.eventSavedData.runs : '';
selected_from = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'bwfan_field_from')) ? data.eventSavedData.bwfan_field_from : 'any';
selected_to = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'bwfan_field_to')) ? data.eventSavedData.bwfan_field_to : 'any';
selected_from_specific = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'bwfan_from_specific_value')) ? data.eventSavedData.bwfan_from_specific_value : '';
selected_to_specific = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'bwfan_to_specific_value')) ? data.eventSavedData.bwfan_to_specific_value : '';
selected_field_from_any = selected_from==='any' || _.isEmpty(selected_from)?'checked':'';
selected_field_from_specific = selected_from==='specific'?'checked':'';
selected_field_to_any = selected_to==='any' || _.isEmpty(selected_to)?'checked':'';
selected_field_to_specific = selected_to==='specific'?'checked':'';
hide_from_specific_value = selected_from =='any'?'bwfan-display-none':'';
hide_to_specific_value = selected_to =='any'?'bwfan-display-none':'';
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Field', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[field]">
<#
if(_.has(data.eventFieldsOptions, 'fields') && _.isObject(data.eventFieldsOptions.fields) ) {
_.each( data.eventFieldsOptions.fields, function( value, key ){
selected = (key == selected_field) ? 'selected' : '';
#>
<option value="{{key}}" {{selected}}>{{value}}</option>
<# })
}
#>
</select>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'FROM', 'wp-marketing-automations-pro' ); ?></label>
</div>
<div class="bwfan_label_val">
<div class="radio-list">
<input type="radio" id="bwfan_field_from" name="event_meta[bwfan_field_from]" value="any" {{selected_field_from_any}}><?php esc_html_e( 'Any Value', 'wp-marketing-automations-pro' ); ?>
<input type="radio" id="bwfan_field_from" name="event_meta[bwfan_field_from]" value="specific" {{selected_field_from_specific}}><?php esc_html_e( 'A Specific Value', 'wp-marketing-automations-pro' ); ?>
</div>
</div>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan_mt15 bwfan-field-from-specific {{hide_from_specific_value}}">
<input type="text" name="event_meta[bwfan_from_specific_value]" id="bwfan_from_specific_value" value="{{selected_from_specific}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<div class="bwfan_label">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'TO', 'wp-marketing-automations-pro' ); ?></label>
</div>
<div class="bwfan_label_val">
<div class="radio-list">
<input type="radio" id="bwfan_field_to" name="event_meta[bwfan_field_to]" value="any" {{selected_field_to_any}}><?php esc_html_e( 'Any Value', 'wp-marketing-automations-pro' ); ?>
<input type="radio" id="bwfan_field_to" name="event_meta[bwfan_field_to]" value="specific" {{selected_field_to_specific}}><?php esc_html_e( 'A Specific Value', 'wp-marketing-automations-pro' ); ?>
</div>
</div>
</div>
<div class="bwfan-col-sm-12 bwfan-pl-0 bwfan_mt15 bwfan-field-to-specific {{hide_to_specific_value}} ">
<input type="text" name="event_meta[bwfan_to_specific_value]" id="bwfan_to_specific_value" value="{{selected_to_specific}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Runs', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[runs]">
<option value="first_time" {{
'first_time' == selected_runs?'selected':'' }}><?php esc_html_e( 'the first time', 'wp-marketing-automations-pro' ); ?></option>
<option value="every_time" {{
'every_time' == selected_runs?'selected':'' }}><?php esc_html_e( 'every time', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<script>
jQuery(document).on('change', '#bwfan_field_to', function () {
var field_to = jQuery(this).val();
if ("specific" === field_to) {
jQuery(".bwfan-field-to-specific").removeClass("bwfan-display-none");
} else {
jQuery(".bwfan-field-to-specific").addClass("bwfan-display-none");
jQuery("#bwfan_to_specific_value").val("");
}
});
jQuery(document).on('change', '#bwfan_field_from', function () {
var field_from = jQuery(this).val();
if ("specific" === field_from) {
jQuery(".bwfan-field-from-specific").removeClass("bwfan-display-none");
} else {
jQuery(".bwfan-field-from-specific").addClass("bwfan-display-none");
jQuery("#bwfan_from_specific_value").val("");
}
});
</script>
<?php
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'field_id' );
if ( ( empty( $get_data ) || $get_data !== $task_meta['global']['field_id'] ) ) {
$set_data = array(
'field_id' => $task_meta['global']['field_id'],
'email' => $task_meta['global']['email'],
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'new_value' => $task_meta['global']['new_value'],
'old_value' => $task_meta['global']['old_value'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->field_id, 'field_id' );
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->new_value, 'new_value' );
BWFAN_Core()->rules->setRulesData( $this->old_value, 'old_value' );
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
$field = $automation_data['event_meta']['field'];
$runs = $automation_data['event_meta']['runs'];
$field_from = $automation_data['event_meta']['bwfan_field_from'];
$field_to = $automation_data['event_meta']['bwfan_field_to'];
$field_from_value = $automation_data['event_meta']['bwfan_from_specific_value'];
$field_to_value = $automation_data['event_meta']['bwfan_to_specific_value'];
$run_count = 0;
if ( 'once' === $runs ) {
$run_count = BWFAN_Model_Automations::get_contact_automation_run_count( $automation_id, $this->contact_id );
}
/** checking condition for every time */
if ( $this->field_id === $field && 'every_time' === $runs && 'any' === $field_from && 'any' === $field_to ) {
continue;
}
if ( $this->field_id === $field && 'every_time' === $runs && 'specific' === $field_from && $field_from_value === $this->old_value && 'specific' === $field_to && $field_to_value === $this->new_value ) {
continue;
}
if ( $this->field_id === $field && 'every_time' === $runs && 'specific' === $field_from && $field_from_value === $this->old_value && 'any' === $field_to ) {
continue;
}
if ( $this->field_id === $field && 'every_time' === $runs && 'any' === $field_from && 'specific' === $field_to && $field_to_value === $this->new_value ) {
continue;
}
/** checking condition for first time */
if ( $this->field_id === $field && 'first_time' === $runs && 'any' === $field_from && 'any' === $field_to && 0 === $run_count ) {
continue;
}
if ( $this->field_id === $field && 'first_time' === $runs && 'specific' === $field_from && $field_from_value === $this->old_value && 'specific' === $field_to && $field_to_value === $this->new_value && 0 === $run_count ) {
continue;
}
if ( $this->field_id === $field && 'first_time' === $runs && 'specific' === $field_from && $field_from_value === $this->old_value && 'any' === $field_to && 0 === $run_count ) {
continue;
}
if ( $this->field_id === $field && 'first_time' === $runs && 'any' === $field_from && 'specific' === $field_to && $field_to_value === $this->new_value && 0 === $run_count ) {
continue;
}
unset( $automations_arr_temp[ $automation_id ] );
}
return $automations_arr_temp;
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_field( $data );
}
public function validate_field( $data ) {
if ( ! isset( $data['field_id'] ) || ! isset( $data['contact_id'] ) ) {
return false;
}
if ( ! BWFCRM_Fields::is_field_exists( $data['field_id'] ) ) {
$this->message_validate_event = __( 'Updated field not exist.', 'wp-marketing-automations-pro' );
return false;
}
$contact = new BWFCRM_Contact( $data['contact_id'] );
if ( ! $contact->is_contact_exists() ) {
$this->message_validate_event = __( 'Contact not exist.', 'wp-marketing-automations-pro' );
return false;
}
return true;
}
/**
* Capture v2 event.
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->field_id = BWFAN_Common::$events_async_data['field_id'];
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->old_value = BWFAN_Common::$events_async_data['old_value'];
$this->new_value = BWFAN_Common::$events_async_data['new_value'];
$this->email = BWFAN_Common::$events_async_data['email'];
$automation_data['field_id'] = $this->field_id;
$automation_data['contact_id'] = $this->contact_id;
$automation_data['email'] = $this->email;
$automation_data['old_value'] = $this->old_value;
$automation_data['new_value'] = $this->new_value;
return $automation_data;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
return $this->validate_field( $automation_data );
}
/**
* v2 Method: Get field schema
* @return array[][]
*/
public function get_fields_schema() {
$field_options = $this->get_view_data();
return [
[
'id' => 'bwfan-crm_select_field',
'type' => 'wp_select',
'options' => $field_options,
'label' => __( 'Field', 'wp-marketing-automations-pro' ),
"class" => 'bwfan-input-wrapper',
"required" => true,
"description" => ""
],
[
'id' => 'bwfan_from_crm_field_value',
'label' => __( "FROM", 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( "Any Value", 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( " A Specific Value", 'wp-marketing-automations-pro' ),
'value' => 'specific',
'tooltip' => __( "This will attach the image of highest price product of a cart/ order in the message.", 'wp-marketing-automations-pro' )
]
],
'description' => ""
],
[
'id' => 'bwfan_to_crm_field_value',
'label' => __( "TO", 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( "Any Value", 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( " A Specific Value", 'wp-marketing-automations-pro' ),
'value' => 'specific',
'tooltip' => __( "This will attach the image of highest price product of a cart/ order in the message.", 'wp-marketing-automations-pro' )
]
],
'description' => ""
],
];
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Fields' ) ) {
//return 'BWFAN_CRM_Field_Updated';
}

View File

@@ -0,0 +1,528 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_Link_Trigger extends BWFAN_Event {
private static $instance = null;
public $link_id = null;
public $contact_id = null;
public $link_name = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Link is Clicked', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after a link is click or visited.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 35;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->is_goal = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfan_link_triggered', array( $this, 'process' ), 10, 2 );
}
public function process( $link_id, $contact ) {
if ( ! $contact instanceof BWFCRM_Contact || 0 === $contact->get_id() || empty( $link_id ) ) {
return;
}
$data = $this->get_default_data();
$data['contact_id'] = $contact->get_id();
$data['link_id'] = $link_id;
$link_data = BWFAN_Model_Link_Triggers::bwfan_get_link_trigger( $link_id );
if ( empty( $link_data ) ) {
return;
}
$data['email'] = $contact->contact->get_email();
$data['user_id'] = $contact->contact->get_wpid();
$data['link_name'] = $link_data['title'];
$this->send_async_call( $data );
}
public function capture_async_data() {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->link_id = BWFAN_Common::$events_async_data['link_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$this->user_id = ! empty( BWFAN_Common::$events_async_data['user_id'] ) ? BWFAN_Common::$events_async_data['user_id'] : $this->get_user_id_event();
$this->link_name = BWFAN_Common::$events_async_data['link_name'];
return $this->run_automations();
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['link_id'] = $this->link_id;
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['email'] = $this->email;
$data_to_send['global']['user_id'] = $this->user_id;
$data_to_send['global']['link_name'] = $this->link_name;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$link_id = isset( $global_data['link_id'] ) ? $global_data['link_id'] : 0;
$link_url = admin_url( "admin.php?page=autonami&path=/link-trigger/" . $global_data['link_id'] );
$contact_url = admin_url( "admin.php?page=autonami&path=/contact/" . $global_data['contact_id'] );
?>
<li>
<strong><?php echo esc_html__( 'Link Id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href="<?php echo esc_attr( $link_url ); ?>"><?php echo esc_html__( $link_id ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Link Name:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['link_name'] ); ?></span>
</li>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a href="<?php echo esc_attr( $contact_url ); ?>"><?php echo esc_html__( $global_data['contact_id'] ); ?></a>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/javascript" defer>
jQuery(document).ready(function ($) {
$(document.body).on('click', '.item_modify_trigger', function () {
setTimeout(function () {
$('#bwfcrm-select-link_triggers').select2({
placeholder: 'Search Link Triggers',
tags: false,
data: [],
escapeMarkup(m) {
return m;
},
ajax: {
url: ajaxurl,
dataType: 'json',
type: 'POST',
delay: 250,
data(term) {
return {
search_term: term,
action: 'bwfan_select2ajax',
type: $('#bwfcrm-select-link_triggers').attr('data-search'),
_wpnonce: bwfanParams.ajax_nonce,
};
},
processResults: function (data) {
var options = [];
if (data) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each(data, function (index, text) { // do not forget that "index" is just auto incremented value
options.push({id: text.id, text: text.text});
});
}
return {
results: options
};
},
cache: true
}
});
}, 300)
});
$('body').on('change', '.bwfan-link_trigger-search', function () {
var temp_list = {id: $(this).val(), name: $(this).find(':selected').text()};
$(this).parent().find('.bwfan_searched_link_trigger').val(JSON.stringify(temp_list));
});
});
</script>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
selected_link_trigger = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'link_trigger')) ? data.eventSavedData.link_trigger : '';
selected_runs = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'runs')) ? data.eventSavedData.runs : '';
searched_link_trigger = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'searched_link_trigger')) ? data.eventSavedData.searched_link_trigger : '';
if(!_.isEmpty(searched_link_trigger)) {
try {
searched_link_trigger = JSON.parse(searched_link_trigger);
}
catch(e) {
//Do Nothing
}
}
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Select Link Triggers', 'wp-marketing-automations-pro' ); ?>
</label>
<select id="bwfcrm-select-link_triggers" data-search="link_trigger" data-search-text="<?php esc_attr_e( 'Select Link Trigger', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-link_trigger-search bwfan-input-wrapper" name="event_meta[link_trigger]">
<#
if(_.size(searched_link_trigger) >0) {
temp_selected_link_trigger = _.isObject(searched_link_trigger) ? searched_link_trigger : JSON.parse(searched_link_trigger);
if(temp_selected_link_trigger.id == selected_link_trigger){
#>
<option value="{{temp_selected_link_trigger.id}}" selected>{{temp_selected_link_trigger.name}}</option>
<#
}
}
stringify_searched_link_trigger = _.isObject(searched_link_trigger) ? JSON.stringify(searched_link_trigger) :
searched_link_trigger;
#>
</select>
<input type="hidden" class="bwfan_searched_link_trigger" name="event_meta[searched_link_trigger]" value="{{stringify_searched_link_trigger}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Runs', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[runs]">
<option value="once" {{
'once' == selected_runs?'selected':''
}}><?php esc_html_e( 'Once', 'wp-marketing-automations-pro' ); ?></option>
<option value="multiple_times" {{
'multiple_times' == selected_runs?'selected':''
}}><?php esc_html_e( 'Multiple Times', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<?php
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'link_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['link_id'] ) ) ) {
$set_data = array(
'link_id' => intval( $task_meta['global']['link_id'] ),
'email' => $task_meta['global']['email'],
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'link_name' => $task_meta['global']['link_name'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->link_id, 'link_id' );
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->link_name, 'link_name' );
BWFAN_Core()->rules->setRulesData( $this->user_id, 'user_id' );
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
$link_trigger = $automation_data['event_meta']['link_trigger'];
$runs = $automation_data['event_meta']['runs'];
/** selected any list and runs multiple times selected */
if ( 'any' === $link_trigger && 'multiple_times' === $runs ) {
continue;
}
if ( 'any' !== $link_trigger && absint( $this->link_id ) !== absint( $link_trigger ) ) {
unset( $automations_arr_temp[ $automation_id ] );
continue;
}
if ( 'multiple_times' === $runs ) {
continue;
}
$run_count = BWFAN_Model_Automations::get_contact_automation_run_count( $automation_id, $this->contact_id );
/** Not run yet */
if ( 0 === $run_count ) {
continue;
}
unset( $automations_arr_temp[ $automation_id ] );
}
return $automations_arr_temp;
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_link_trigger( $data );
}
public function validate_link_trigger( $data ) {
/** Selected Links */
$links_ids = array_map( function ( $link ) {
return intval( $link['id'] );
}, $data['event_meta']['link_trigger'] );
if ( empty( $links_ids ) ) {
return false;
}
$trigger_link_id = intval( $data['link_id'] );
return in_array( $trigger_link_id, $links_ids, true );
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
$trigger_contains = isset( $automation_data['event_meta']['link-contains'] ) ? $automation_data['event_meta']['link-contains'] : 'selected_link';
if ( 'any' === $trigger_contains ) {
return true;
}
if ( ! isset( $automation_data['event_meta']['link_trigger'] ) || ! isset( $automation_data['link_id'] ) ) {
return false;
}
return $this->validate_link_trigger( $automation_data );
}
/**
* Capture the async v2 data for the current event.
* @return array|bool
*/
public function capture_v2_data( $automation_data ) {
$this->contact_id = BWFAN_Common::$events_async_data['contact_id'];
$this->link_id = BWFAN_Common::$events_async_data['link_id'];
$this->email = BWFAN_Common::$events_async_data['email'];
$this->user_id = ! empty( BWFAN_Common::$events_async_data['user_id'] ) ? BWFAN_Common::$events_async_data['user_id'] : $this->get_user_id_event();
$this->link_name = BWFAN_Common::$events_async_data['link_name'];
$automation_data['contact_id'] = $this->contact_id;
$automation_data['link_id'] = $this->link_id;
$automation_data['email'] = $this->email;
$automation_data['user_id'] = $this->user_id;
$automation_data['link_name'] = $this->link_name;
return $automation_data;
}
/**
* v2 Method: Get fields schema
* @return array[][]
*/
public function get_fields_schema() {
return [
[
'id' => 'link-contains',
'label' => __( 'Link Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any link', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific Link', 'wp-marketing-automations-pro' ),
'value' => 'selected_link'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => true,
"description" => ""
],
[
'id' => 'link_trigger',
'type' => 'search',
'autocompleter' => 'link_trigger',
'label' => __( 'Select Link(s) Trigger', 'wp-marketing-automations-pro' ),
"class" => "bwfan-col-sm-9",
"allowFreeTextSearch" => false,
"required" => true,
'toggler' => [
'fields' => [
[
'id' => 'link-contains',
'value' => 'selected_link',
]
]
],
]
];
}
public function get_default_values() {
return [
'link-contains' => 'selected_link',
];
}
public function get_view_data() {
$all_links = BWFAN_Model_Link_Triggers::get_link_triggers();
if ( empty( $all_links['links'] ) ) {
return array();
}
$links_data = array();
foreach ( $all_links['links'] as $link ) {
$links_data[ $link['ID'] ] = $link['title'];
}
return $links_data;
}
public function validate_goal_settings( $automation_settings, $automation_data ) {
/** settings not found */
if ( ! isset( $automation_data['link_id'] ) ) {
return false;
}
/** If empty */
if ( empty( $automation_settings['link_trigger'] ) ) {
return false;
}
/** Goal settings tags */
$tags = array_column( $automation_settings['link_trigger'], 'id' );
return in_array( $automation_data['link_id'], $tags );
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['link_trigger'] ) || empty( $data['link_trigger'] ) ) {
return '';
}
$links = [];
foreach ( $data['link_trigger'] as $link ) {
if ( ! isset( $link['name'] ) || empty( $link['name'] ) ) {
continue;
}
$links[] = $link['name'];
}
return $links;
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new BWFCRM_Contact( $cid );
/** Check if contact exists */
if ( ! $contact->is_contact_exists() ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$clicked_link = $contact->get_field_by_slug( 'link-trigger-click' );
$clicked_link = json_decode( $clicked_link, true );
/** Return if contact is not subscribed */
if ( empty( $clicked_link ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( "Contact doesn't have any link trigger click", 'wp-marketing-automations-pro' ) ];
}
$last_clicked_link = end( $clicked_link );
$link_data = BWFAN_Model_Link_Triggers::bwfan_get_link_trigger( $last_clicked_link );
if ( empty( $link_data ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( "Link trigger is not exist", 'wp-marketing-automations-pro' ) ];
}
$this->contact_id = $contact->get_id();
$this->link_id = $last_clicked_link;
$this->email = $contact->contact->get_email();
$this->link_name = $link_data['title'];
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email, 'link_id' => $this->link_id, 'link_name' => $this->link_name ] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Link_Trigger_Handler' ) ) {
return 'BWFAN_CRM_Link_Trigger';
}

View File

@@ -0,0 +1,611 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_UnAssigned_Tag extends BWFAN_Event {
private static $instance = null;
public $tags = [];
public $tag_id = null;
public $contact_id = null;
public $tag_name = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwfan_crm_tags', 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Tag is Removed', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after an tag unassigned from contact.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 15;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->is_goal = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfan_tags_removed_from_contact', array( $this, 'process' ), 10, 2 );
}
public function process( $tags, $contact ) {
$this->tags = BWFCRM_Tag::get_tags( $tags );
$this->contact_id = ( $contact instanceof BWFCRM_Contact ) ? $contact->get_id() : 0;
$this->email = ( $contact instanceof BWFCRM_Contact ) ? $contact->contact->get_email() : '';
$this->run_automations();
}
public function run_automations() {
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_PRO_Common::disable_run_v2_automation_immediately();
$automation_actions = [];
foreach ( $this->tags as $tag ) {
if ( empty( $tag ) ) {
continue;
}
$this->tag_id = $tag['ID'];
$this->tag_name = $tag['name'];
/** v2 run starts */
$contact_data_v2 = array(
'contact_id' => absint( $this->contact_id ),
'email' => $this->email,
'tag_id' => $this->tag_id,
'tag_name' => $this->tag_name
);
BWFAN_Common::maybe_run_v2_automations( $this->get_slug(), $contact_data_v2 );
/** v1 run starts */
if ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) {
continue;
}
/** Checking for event settings */
$automation_arr = $this->validate_event_data_before_creating_task( $this->automations_arr );
if ( ! is_array( $automation_arr ) || count( $automation_arr ) === 0 ) {
continue;
}
foreach ( $automation_arr as $automation_id => $automation_data ) {
if ( $this->get_slug() !== $automation_data['event'] || 0 !== intval( $automation_data['requires_update'] ) ) {
continue;
}
$ran_actions = $this->handle_single_automation_run( $automation_data, $automation_id );
$automation_actions[ $automation_id ] = $ran_actions;
}
}
return $automation_actions;
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
$tag = ! empty( $automation_data['event_meta']['tag'] ) ? $automation_data['event_meta']['tag'] : 'any';
$runs = isset( $automation_data['event_meta']['bwfan_automation_run'] ) ? $automation_data['event_meta']['runs'] : 'multiple';
$run_count = 0;
if ( 'once' === $runs ) {
$run_count = BWFAN_Model_Automations::get_contact_automation_run_count( $automation_id, $this->contact_id );
}
/** selected any tag and runs multiple times selected */
if ( 'any' === $tag && 'multiple' === $runs ) {
continue;
}
/** selected list id and run multiple times selected */
if ( absint( $this->tag_id ) === absint( $tag ) && 'multiple' === $runs ) {
continue;
}
if ( 'any' === $tag && 'once' === $runs && 0 === $run_count ) {
continue;
}
if ( absint( $this->tag_id ) === absint( $tag ) && 'once' === $runs && 0 === $run_count ) {
continue;
}
unset( $automations_arr_temp[ $automation_id ] );
}
return $automations_arr_temp;
}
public function get_view_data() {
$all_tags = BWFCRM_tag::get_tags();
if ( empty( $all_tags ) ) {
return array();
}
$tag_data = array();
foreach ( $all_tags as $tag ) {
$tag_data[ $tag['ID'] ] = $tag['name'];
}
return $tag_data;
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['tag_id'] = $this->tag_id;
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['tag_name'] = $this->tag_name;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$tag_id = isset( $global_data['tag_id'] ) ? $global_data['tag_id'] : 0;
?>
<li>
<strong><?php echo esc_html__( 'Tag Id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href=""><?php echo esc_html__( $tag_id ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'Tag Name:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['tag_name'] ); ?></span>
</li>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['contact_id'] ); ?></span>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script>
jQuery(document).ready(function ($) {
$(document.body).on('click', '.item_modify_trigger', function () {
setTimeout(function () {
$('#bwfcrm-select-tag').select2({
placeholder: 'Search tag',
minimumInputLength: 2,
tags: false,
data: [],
escapeMarkup(m) {
return m;
},
ajax: {
url: ajaxurl,
dataType: 'json',
type: 'POST',
delay: 250,
data(term) {
return {
search_term: term,
action: 'bwfan_select2ajax',
type: $('#bwfcrm-select-tag').attr('data-search'),
_wpnonce: bwfanParams.ajax_nonce,
};
},
processResults: function (data) {
var options = [];
if (data) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each(data, function (index, text) { // do not forget that "index" is just auto incremented value
options.push({id: text.id, text: text.text});
});
}
return {
results: options
};
},
cache: true
}
});
}, 300);
});
$('body').on('change', '.bwfan-tag-search', function () {
var temp_tag = {id: $(this).val(), name: $(this).find(':selected').text()};
$(this).parent().find('.bwfan_searched_tag').val(JSON.stringify(temp_tag));
});
});
</script>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
selected_tag = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'tag')) ? data.eventSavedData.tag : '';
selected_runs = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'runs')) ? data.eventSavedData.runs : '';
searched_tag = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'searched_tag')) ? data.eventSavedData.searched_tag : '';
if(!_.isEmpty(searched_tag)) {
try {
searched_tag = JSON.parse(searched_tag);
}
catch(e) {
//Do Nothing
}
}
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Select Tag', 'wp-marketing-automations-pro' ); ?>
</label>
<select id="bwfcrm-select-tag" data-search="tag" data-search-text="<?php esc_attr_e( 'Select Tag', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-tag-search bwfan-input-wrapper" name="event_meta[tag]">
<#
if(_.size(searched_tag) >0) {
temp_selected_tag = _.isObject(searched_tag) ? searched_tag : JSON.parse(searched_tag);
if(temp_selected_tag.id == selected_tag){
#>
<option value="{{temp_selected_tag.id}}" selected>{{temp_selected_tag.name}}</option>
<#
}
}
stringify_searched_tag = _.isObject(searched_tag) ? JSON.stringify(searched_tag) :
searched_tag;
#>
</select>
<input type="hidden" class="bwfan_searched_tag" name="event_meta[searched_tag]" value="{{stringify_searched_tag}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Runs', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[runs]">
<option value="once" {{
'once' == selected_runs?'selected':''
}}><?php esc_html_e( 'Once', 'wp-marketing-automations-pro' ); ?></option>
<option value="multiple_times" {{
'multiple_times' == selected_runs?'selected':''
}}><?php esc_html_e( 'Multiple Times', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<?php
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'tag_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['tag_id'] ) ) ) {
$set_data = array(
'tag_id' => intval( $task_meta['global']['tag_id'] ),
'email' => $task_meta['global']['email'],
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'tag_name' => $task_meta['global']['tag_name'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->tag_id, 'tag_id' );
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->tag_name, 'tag_name' );
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_tag( $data );
}
public function validate_tag( $data ) {
if ( ! isset( $data['tag_id'] ) || ! isset( $data['contact_id'] ) ) {
return false;
}
$crm_tag = new BWFCRM_Tag( $data['tag_id'] );
if ( ! $crm_tag->is_exists() ) {
$this->message_validate_event = __( 'Tag not exist.', 'wp-marketing-automations-pro' );
return false;
}
$contact = new BWFCRM_Contact( $data['contact_id'] );
$tags = $contact->get_tags();
if ( in_array( $data['tag_id'], $tags ) ) {
$this->message_validate_event = __( 'Tag is assigned to contact.', 'wp-marketing-automations-pro' );
return false;
}
return true;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
if ( ! isset( $automation_data['event_meta'] ) ) {
return true;
}
/** If key is not set, then we will assign */
if ( ! isset( $automation_data['event_meta']['tag-contains'] ) ) {
if ( ! isset( $automation_data['event_meta']['tags'] ) || empty( $automation_data['event_meta']['tags'] ) ) {
$automation_data['event_meta']['tag-contains'] = 'any';
}
}
if ( isset( $automation_data['event_meta']['tag-contains'] ) && 'any' === $automation_data['event_meta']['tag-contains'] ) {
return true;
}
/** Selected tags */
$tag_ids = array_map( function ( $tag ) {
return intval( $tag['id'] );
}, $automation_data['event_meta']['tags'] );
if ( empty( $tag_ids ) ) {
return true;
}
$unassigned_tag = intval( $automation_data['tag_id'] );
return in_array( $unassigned_tag, $tag_ids, true );
}
/**
* v2 Method: Get fields schema
* @return array[][]
*/
public function get_fields_schema() {
return [
[
'id' => 'tag-contains',
'label' => __( 'Tag Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Specific Tag', 'wp-marketing-automations-pro' ),
'value' => 'selected_tag'
],
[
'label' => __( 'Any Tag', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => true,
"description" => ""
],
[
'id' => 'tags',
'label' => __( 'Select Tags', 'wp-marketing-automations-pro' ),
'type' => 'search',
'autocompleter' => 'tags',
'class' => 'bwfan-col-sm-9',
'placeholder' => '',
'required' => true,
'toggler' => [
'fields' => [
[
'id' => 'tag-contains',
'value' => 'selected_tag',
]
]
],
],
];
}
public function validate_goal_settings( $automation_settings, $automation_data ) {
$tag_contain = isset( $automation_settings['tag-contains'] ) ? $automation_settings['tag-contains'] : 'any';
if ( 'any' === $tag_contain ) {
return true;
}
/** Goal settings tags not set */
if ( empty( $automation_settings['tags'] ) || 0 === count( $automation_settings['tags'] ) ) {
return false;
}
/** Goal settings tags */
$tags = array_column( $automation_settings['tags'], 'id' );
if ( empty( $tags ) ) {
return false;
}
return in_array( $automation_data['tag_id'], $tags );
}
/**
* @param $tags
* @param $automation_data
* @param $v
*
* @return bool|void
*/
public function validate_bulk_action_event_settings( $tags, $automation_data, $v ) {
$tag_ids = array_map( function ( $tag ) {
return absint( $tag['id'] );
}, $tags );
if ( 1 === absint( $v ) ) {
$tag = $automation_data['meta']['event_meta']['tag'];
if ( 'any' === $tag ) {
return true;
}
return in_array( $tag, $tag_ids );
}
if ( 2 === absint( $v ) ) {
$tag_contain = isset( $automation_data['meta']['event_meta']['tag-contains'] ) ? $automation_data['meta']['event_meta']['tag-contains'] : 'any';
if ( 'any' === $tag_contain ) {
return true;
}
$tags = $automation_data['meta']['event_meta']['tags'];
$event_tag_ids = empty( $tags ) ? [] : array_column( $tags, 'id' );
return ( count( array_intersect( $tag_ids, $event_tag_ids ) ) > 0 );
}
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['tags'] ) || empty( $data['tags'] ) ) {
return '';
}
$tags = [];
foreach ( $data['tags'] as $tag ) {
if ( ! isset( $tag['name'] ) || empty( $tag['name'] ) ) {
continue;
}
$tags[] = $tag['name'];
}
return $tags;
}
/** set default values */
public function get_default_values() {
return [
'tag-contains' => 'selected_tag',
];
}
/** Set default goal values */
public function get_default_goal_values() {
return [
'tag-contains' => 'any',
];
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$contact_tags = $contact->get_tags();
/** return if no tags available as event is tag added */
if ( empty( $contact_tags ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'There are no assigned tags for the contact', 'wp-marketing-automations-pro' ) ];
}
if ( isset( $automation_data['event_meta']['tag-contains'] ) && 'selected_tag' === $automation_data['event_meta']['tag-contains'] ) {
$event_tags = $automation_data['event_meta']['tags'];
$matched_tags = array_values( array_filter( $event_tags, function ( $tag ) use ( $contact_tags ) {
return ! in_array( $tag['id'], $contact_tags, true );
} ) );
/** return if no tags are matched with contact tags */
if ( empty( $matched_tags ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No contact tags are matching with event tags', 'wp-marketing-automations-pro' ) ];
}
$contact_tags = array_column( $matched_tags, 'id' );
}
$this->contact_id = $cid;
$this->email = $contact->get_email();
$this->tag_id = $contact_tags[0];
$this->tag_name = BWFCRM_Tag::get_tags( array( $contact_tags[0] ) )[0]['name'];
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email, 'tag_id' => $this->tag_id, 'tag_name' => $this->tag_name ] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Tag' ) ) {
return 'BWFAN_CRM_UnAssigned_Tag';
}

View File

@@ -0,0 +1,617 @@
<?php
#[AllowDynamicProperties]
final class BWFAN_CRM_UnAssigned_List extends BWFAN_Event {
private static $instance = null;
public $lists = [];
public $list_id = null;
public $contact_id = null;
public $list_name = null;
public $email = null;
public $user_id = null;
private function __construct() {
$this->event_merge_tag_groups = array( 'bwfan_crm_lists', 'bwf_contact' );
$this->optgroup_label = esc_html__( 'Contact', 'wp-marketing-automations-pro' );
$this->event_name = esc_html__( 'Removed from List', 'wp-marketing-automations-pro' );
$this->event_desc = esc_html__( 'This event runs after an list unassigned from contact.', 'wp-marketing-automations-pro' );
$this->event_rule_groups = array(
'bwf_contact_segments',
'bwf_contact',
'bwf_contact_fields',
'bwf_contact_user',
'bwf_contact_wc',
'bwf_contact_geo',
'bwf_engagement',
'bwf_broadcast'
);
$this->priority = 25;
$this->customer_email_tag = '{{contact_email}}';
$this->v2 = true;
$this->is_goal = true;
$this->automation_add = true;
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function load_hooks() {
add_action( 'bwfan_contact_removed_from_lists', array( $this, 'process' ), 10, 2 );
}
public function process( $lists, $contact ) {
if ( empty( $lists ) ) {
return;
}
$this->lists = [];
if ( ! is_array( $lists ) && $lists instanceof BWFCRM_Lists ) {
$this->lists[] = $lists;
} else {
$this->lists = $lists;
}
$this->lists = BWFCRM_Lists::get_lists( $lists );
$this->contact_id = ( $contact instanceof BWFCRM_Contact ) ? $contact->get_id() : 0;
$this->email = ( $contact instanceof BWFCRM_Contact ) ? $contact->contact->get_email() : '';
$this->run_automations();
}
public function run_automations() {
BWFAN_Core()->public->load_active_automations( $this->get_slug() );
BWFAN_PRO_Common::disable_run_v2_automation_immediately();
$automation_actions = [];
foreach ( $this->lists as $list ) {
if ( empty( $list ) ) {
continue;
}
$this->list_id = $list['ID'];
$this->list_name = $list['name'];
/** v2 run starts */
$contact_data_v2 = array(
'contact_id' => absint( $this->contact_id ),
'email' => $this->email,
'list_id' => $this->list_id,
'list_name' => $this->list_name
);
BWFAN_Common::maybe_run_v2_automations( $this->get_slug(), $contact_data_v2 );
/** v1 run starts */
if ( ! is_array( $this->automations_arr ) || count( $this->automations_arr ) === 0 ) {
continue;
}
/** Checking for event settings */
$automation_arr = $this->validate_event_data_before_creating_task( $this->automations_arr );
if ( ! is_array( $automation_arr ) || count( $automation_arr ) === 0 ) {
continue;
}
foreach ( $automation_arr as $automation_id => $automation_data ) {
if ( $this->get_slug() !== $automation_data['event'] || 0 !== intval( $automation_data['requires_update'] ) ) {
continue;
}
$ran_actions = $this->handle_single_automation_run( $automation_data, $automation_id );
$automation_actions[ $automation_id ] = $ran_actions;
}
}
return $automation_actions;
}
/**
* Validating form id after submission with the selected form id in the event
*
* @param $automations_arr
*
* @return mixed
*/
public function validate_event_data_before_creating_task( $automations_arr ) {
$automations_arr_temp = $automations_arr;
foreach ( $automations_arr as $automation_id => $automation_data ) {
$list = ! empty( $automation_data['event_meta']['list'] ) ? $automation_data['event_meta']['list'] : 'any';
$runs = isset( $automation_data['event_meta']['bwfan_automation_run'] ) ? $automation_data['event_meta']['runs'] : 'multiple';
$run_count = 0;
if ( 'once' === $runs ) {
$run_count = BWFAN_Model_Automations::get_contact_automation_run_count( $automation_id, $this->contact_id );
}
/** selected any list and runs multiple times selected */
if ( 'any' === $list && 'multiple' === $runs ) {
continue;
}
/** selected list id and run multiple times selected */
if ( absint( $this->list_id ) === absint( $list ) && 'multiple' === $runs ) {
continue;
}
if ( 'any' === $list && 'once' === $runs && 0 === $run_count ) {
continue;
}
if ( absint( $this->list_id ) === absint( $list ) && 'once' === $runs && 0 === $run_count ) {
continue;
}
unset( $automations_arr_temp[ $automation_id ] );
}
return $automations_arr_temp;
}
/**
* Registers the tasks for current event.
*
* @param $automation_id
* @param $integration_data
* @param $event_data
*/
public function register_tasks( $automation_id, $integration_data, $event_data ) {
if ( ! is_array( $integration_data ) ) {
return;
}
$data_to_send = $this->get_event_data();
$this->create_tasks( $automation_id, $integration_data, $event_data, $data_to_send );
}
public function get_event_data() {
$data_to_send = [ 'global' => [] ];
$data_to_send['global']['list_id'] = $this->list_id;
$data_to_send['global']['contact_id'] = $this->contact_id;
$data_to_send['global']['list_name'] = $this->list_name;
$data_to_send['global']['email'] = $this->email;
return $data_to_send;
}
/**
* Make the view data for the current event which will be shown in task listing screen.
*
* @param $global_data
*
* @return false|string
*/
public function get_task_view( $global_data ) {
ob_start();
$list_id = isset( $global_data['list_id'] ) ? $global_data['list_id'] : 0;
?>
<li>
<strong><?php echo esc_html__( 'List Id:', 'wp-marketing-automations-pro' ); ?> </strong>
<a target="_blank" href=""><?php echo esc_html__( $list_id ); ?></a>
</li>
<li>
<strong><?php echo esc_html__( 'List Name:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['list_name'] ); ?></span>
</li>
<li>
<strong><?php echo esc_html__( 'Contact_id:', 'wp-marketing-automations-pro' ); ?> </strong>
<span><?php echo esc_html__( $global_data['contact_id'] ); ?></span>
</li>
<?php
return ob_get_clean();
}
public function get_email_event() {
return is_email( $this->email ) ? $this->email : false;
}
/**
* Show the html fields for the current event.
*/
public function get_view( $db_eventmeta_saved_value ) {
?>
<script type="text/javascript" defer>
jQuery(document).ready(function ($) {
$(document.body).on('click', '.item_modify_trigger', function () {
setTimeout(function () {
$('#bwfcrm-select-list').select2({
placeholder: 'Search list',
minimumInputLength: 2,
tags: false,
data: [],
escapeMarkup(m) {
return m;
},
ajax: {
url: ajaxurl,
dataType: 'json',
type: 'POST',
delay: 250,
data(term) {
return {
search_term: term,
action: 'bwfan_select2ajax',
type: $('#bwfcrm-select-list').attr('data-search'),
_wpnonce: bwfanParams.ajax_nonce,
};
},
processResults: function (data) {
var options = [];
if (data) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each(data, function (index, text) { // do not forget that "index" is just auto incremented value
options.push({id: text.id, text: text.text});
});
}
return {
results: options
};
},
cache: true
}
});
}, 300)
});
$('body').on('change', '.bwfan-list-search', function () {
var temp_list = {id: $(this).val(), name: $(this).find(':selected').text()};
$(this).parent().find('.bwfan_searched_list').val(JSON.stringify(temp_list));
});
});
</script>
<script type="text/html" id="tmpl-event-<?php esc_attr_e( $this->get_slug() ); ?>">
<#
selected_list = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'list')) ? data.eventSavedData.list : '';
selected_runs = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'runs')) ? data.eventSavedData.runs : '';
searched_list = (_.has(data, 'eventSavedData') &&_.has(data.eventSavedData, 'searched_list')) ? data.eventSavedData.searched_list : '';
if(!_.isEmpty(searched_list)) {
try {
searched_list = JSON.parse(searched_list);
}
catch(e) {
//Do Nothing
}
}
#>
<div class="bwfan_mt15"></div>
<div class="bwfan-input-form clearfix">
<label for="" class="bwfan-label-title">
<?php esc_html_e( 'Select List', 'wp-marketing-automations-pro' ); ?>
</label>
<select id="bwfcrm-select-list" data-search="list" data-search-text="<?php esc_attr_e( 'Select Tag', 'wp-marketing-automations-pro' ); ?>" class="bwfan-select2ajax-single bwfan-list-search bwfan-input-wrapper" name="event_meta[list]">
<#
if(_.size(searched_list) >0) {
temp_selected_list = _.isObject(searched_list) ? searched_list : JSON.parse(searched_list);
if(temp_selected_list.id == selected_list){
#>
<option value="{{temp_selected_list.id}}" selected>{{temp_selected_list.name}}</option>
<#
}
}
stringify_searched_list = _.isObject(searched_list) ? JSON.stringify(searched_list) :
searched_list;
#>
</select>
<input type="hidden" class="bwfan_searched_list" name="event_meta[searched_list]" value="{{stringify_searched_list}}"/>
</div>
<div class="bwfan_mt15"></div>
<div class="bwfan-col-sm-15 bwfan-pl-0">
<label for="" class="bwfan-label-title"><?php esc_html_e( 'Runs', 'wp-marketing-automations-pro' ); ?></label>
<select required id="" class="bwfan-input-wrapper" name="event_meta[runs]">
<option value="once" {{
'once' == selected_runs?'selected':'' }}><?php esc_html_e( 'Once', 'wp-marketing-automations-pro' ); ?></option>
<option value="multiple_times" {{
'multiple_times' == selected_runs?'selected':'' }}><?php esc_html_e( 'Multiple Times', 'wp-marketing-automations-pro' ); ?></option>
</select>
</div>
</script>
<?php
}
public function get_user_id_event() {
if ( is_email( $this->email ) ) {
$user = get_user_by( 'email', absint( $this->email ) );
return ( $user instanceof WP_User ) ? $user->ID : false;
}
return ! empty( absint( $this->user_id ) ) ? absint( $this->user_id ) : false;
}
/**
* Set global data for all the merge tags which are supported by this event.
*
* @param $task_meta
*/
public function set_merge_tags_data( $task_meta ) {
$get_data = BWFAN_Merge_Tag_Loader::get_data( 'list_id' );
if ( ( empty( $get_data ) || intval( $get_data ) !== intval( $task_meta['global']['list_id'] ) ) ) {
$set_data = array(
'list_id' => intval( $task_meta['global']['list_id'] ),
'email' => $task_meta['global']['email'],
'contact_id' => intval( $task_meta['global']['contact_id'] ),
'list_name' => $task_meta['global']['list_name'],
);
BWFAN_Merge_Tag_Loader::set_data( $set_data );
}
}
/**
* Set up rules data
*
* @param $value
*/
public function pre_executable_actions( $value ) {
BWFAN_Core()->rules->setRulesData( $this->list_id, 'list_id' );
BWFAN_Core()->rules->setRulesData( $this->contact_id, 'contact_id' );
BWFAN_Core()->rules->setRulesData( $this->email, 'email' );
BWFAN_Core()->rules->setRulesData( $this->list_name, 'list_name' );
}
public function validate_event_data_before_executing_task( $data ) {
return $this->validate_list( $data );
}
public function validate_list( $data ) {
if ( ! isset( $data['list_id'] ) || ! isset( $data['contact_id'] ) ) {
return false;
}
$crm_list = new BWFCRM_Lists( $data['list_id'] );
if ( ! $crm_list->is_exists() ) {
$this->message_validate_event = __( 'List not exist.', 'wp-marketing-automations-pro' );
return false;
}
$contact = new BWFCRM_Contact( $data['contact_id'] );
$lists = $contact->get_lists();
if ( in_array( $data['list_id'], $lists ) ) {
$this->message_validate_event = __( 'List is assigned to list.', 'wp-marketing-automations-pro' );
return false;
}
return true;
}
/**
* v2 Method: Validate event settings
*
* @param $automation_data
*
* @return bool
*/
public function validate_v2_event_settings( $automation_data ) {
if ( ! isset( $automation_data['event_meta'] ) ) {
return true;
}
/** If key is not set, then we will assign */
if ( ! isset( $automation_data['event_meta']['list-contains'] ) ) {
if ( ! isset( $automation_data['event_meta']['list'] ) || empty( $automation_data['event_meta']['list'] ) ) {
$automation_data['event_meta']['list-contains'] = 'any';
}
}
if ( isset( $automation_data['event_meta']['list-contains'] ) && 'any' === $automation_data['event_meta']['list-contains'] ) {
return true;
}
/** Selected lists */
$list_ids = array_map( function ( $list ) {
return intval( $list['id'] );
}, $automation_data['event_meta']['list'] );
if ( empty( $list_ids ) ) {
return true;
}
$unassigned_list = intval( $automation_data['list_id'] );
return in_array( $unassigned_list, $list_ids, true );
}
/**
* v2 Method: Get field schema
* @return array[][]
*/
public function get_fields_schema() {
return [
[
'id' => 'list-contains',
'label' => __( 'List Contains', 'wp-marketing-automations-pro' ),
'type' => 'radio',
'options' => [
[
'label' => __( 'Any List', 'wp-marketing-automations-pro' ),
'value' => 'any'
],
[
'label' => __( 'Specific List', 'wp-marketing-automations-pro' ),
'value' => 'selected_list'
],
],
"class" => 'bwfan-input-wrapper',
"tip" => "",
"required" => true,
"description" => ""
],
[
'id' => 'list',
'label' => __( 'Select List', 'wp-marketing-automations-pro' ),
'type' => 'search',
'autocompleter' => 'lists',
'class' => 'bwfan-col-sm-9',
'placeholder' => '',
'required' => true,
'toggler' => [
'fields' => [
[
'id' => 'list-contains',
'value' => 'selected_list',
]
]
],
],
];
}
public function get_view_data() {
$all_lists = BWFCRM_Lists::get_lists();
if ( empty( $all_lists ) ) {
return array();
}
$list_data = array();
foreach ( $all_lists as $list ) {
$list_data[ $list['ID'] ] = $list['name'];
}
return $list_data;
}
public function validate_goal_settings( $automation_settings, $automation_data ) {
$list_contain = isset( $automation_settings['list-contains'] ) ? $automation_settings['list-contains'] : 'any';
if ( 'any' === $list_contain ) {
return true;
}
/** Goal settings lists not set */
if ( empty( $automation_settings['list'] ) || 0 === count( $automation_settings['list'] ) ) {
return false;
}
/** Goal settings lists */
$lists = array_column( $automation_settings['list'], 'id' );
if ( empty( $lists ) ) {
return false;
}
return in_array( $automation_data['list_id'], $lists );
}
/**
* @param $lists
* @param $automation_data
* @param $v
*
* @return bool|void
*/
public function validate_bulk_action_event_settings( $lists, $automation_data, $v ) {
$list_ids = array_map( function ( $list ) {
return absint( $list['id'] );
}, $lists );
if ( 1 === absint( $v ) ) {
$list = $automation_data['meta']['event_meta']['list'];
if ( 'any' === $list ) {
return true;
}
return in_array( $list, $list_ids );
}
if ( 2 === absint( $v ) ) {
$list_contain = isset( $automation_data['meta']['event_meta']['list-contains'] ) ? $automation_data['meta']['event_meta']['list-contains'] : 'any';
if ( 'any' === $list_contain ) {
return true;
}
$lists = $automation_data['meta']['event_meta']['list'];
$event_list_ids = empty( $lists ) ? [] : array_column( $lists, 'id' );
return ( count( array_intersect( $list_ids, $event_list_ids ) ) > 0 );
}
}
public function get_desc_text( $data ) {
$data = json_decode( wp_json_encode( $data ), true );
if ( ! isset( $data['list'] ) || empty( $data['list'] ) ) {
return '';
}
$lists = [];
foreach ( $data['list'] as $list ) {
if ( ! isset( $list['name'] ) || empty( $list['name'] ) ) {
continue;
}
$lists[] = $list['name'];
}
return $lists;
}
/** set default values */
public function get_default_values() {
return [
'list-contains' => 'selected_list',
];
}
/** Set default goal values */
public function get_default_goal_values() {
return [
'list-contains' => 'any',
];
}
/**
* get contact automation data
*
* @param $automation_data
* @param $cid
*
* @return array|null[]
*/
public function get_manually_added_contact_automation_data( $automation_data, $cid ) {
$contact = new WooFunnels_Contact( '', '', '', $cid );
/** Check if contact exists */
if ( ! $contact instanceof WooFunnels_Contact || empty( $contact->get_id() ) ) {
return [ 'status' => 0, 'type' => 'contact_not_found' ];
}
$contact_lists = $contact->get_lists();
/** return if no lists available as event is added to list */
if ( empty( $contact_lists ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'There are no assigned lists for the contact', 'wp-marketing-automations-pro' ) ];
}
if ( isset( $automation_data['event_meta']['list-contains'] ) && 'selected_list' === $automation_data['event_meta']['list-contains'] ) {
$event_lists = $automation_data['event_meta']['list'];
$matched_lists = array_values( array_filter( $event_lists, function ( $list ) use ( $contact_lists ) {
return ! in_array( $list['id'], $contact_lists, true );
} ) );
/** return if no lists are matched with contact lists */
if ( empty( $matched_lists ) ) {
return [ 'status' => 0, 'type' => '', 'message' => __( 'No contact lists are matching with event lists', 'wp-marketing-automations-pro' ) ];
}
$contact_lists = array_column( $matched_lists, 'id' );
}
$this->contact_id = $cid;
$this->email = $contact->get_email();
$this->list_id = $contact_lists[0];
$this->list_name = BWFCRM_Lists::get_lists( array( $contact_lists[0] ) )[0]['name'];
return array_merge( $automation_data, [ 'contact_id' => $this->contact_id, 'email' => $this->email, 'list_id' => $this->list_id, 'list_name' => $this->list_name ] );
}
}
/**
* Register this event to a source.
* This will show the current event in dropdown in single automation screen.
*/
if ( class_exists( 'BWFCRM_Lists' ) ) {
return 'BWFAN_CRM_UnAssigned_List';
}

View File

@@ -0,0 +1,2 @@
<?php
//silence is golden

View File

@@ -0,0 +1,62 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'BWFAN_Allowed_Automations' ) ) {
class BWFAN_Allowed_Automations {
private static $ins = null;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function get_slug() {
return 'allowed_automations';
}
/**
* Get options
*
* @param $search
*
* @return array
*/
public function get_options( $search ) {
$events = BWFAN_Core()->sources->get_events_to_add_contact_manually();
if ( empty( $events ) ) {
return [];
}
$placeholder = array_fill( 0, count( $events ), '%s' );
$placeholder = implode( ", ", $placeholder );
global $wpdb;
$query = $wpdb->prepare( "SELECT `ID` as `key`,`title` as `value` FROM `{$wpdb->prefix}bwfan_automations` WHERE `event` IN ($placeholder)", $events );
if ( ! empty( $search ) ) {
$query .= $wpdb->prepare( " AND `title` LIKE %s", '%' . $search . '%' );
}
$query .= " AND `status` = 1 AND `v` = 2 LIMIT 0, 25";
$automations = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
$formatted_automations = [];
foreach ( $automations as $automation ) {
$formatted_automations[ $automation['key'] ] = $automation['value'] . ' (#' . $automation['key'] . ')';
}
return $formatted_automations;
}
}
if ( class_exists( 'BWFAN_Load_Custom_Search' ) ) {
BWFAN_Load_Custom_Search::register( 'BWFAN_Allowed_Automations' );
}
}

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

View File

@@ -0,0 +1,115 @@
<?php
defined( 'ABSPATH' ) || exit;
global $wp_locale;
$global_settings = BWFAN_Common::get_global_settings();
$required = ! empty( $wfacp_id ) || ! isset( $global_settings['bwfan_dob_required'] ) ? $required : $global_settings['bwfan_dob_required'];
if ( empty( $birthday_label ) ) {
$birthday_label = ! empty( $global_settings['bwfan_birthday_field_label'] ) ? $global_settings['bwfan_birthday_field_label'] : __( 'Date of Birth', 'wp-marketing-automations-pro' );
}
$birthday_date = '';
$selected_date = '';
$selected_month = '';
$selected_year = '';
$plugin_version = defined( 'BWFAN_VERSION' ) ? BWFAN_VERSION : '0.0.0';
$use_dropdown_ui = version_compare( $plugin_version, '3.5.3', '>' );
if ( is_user_logged_in() ) {
$user = get_user_by( 'id', get_current_user_id() );
$contact = new BWFCRM_Contact( $user->user_email );
/** Get DOB from contact */
$dob = $contact->get_field_by_slug( 'dob' );
/** Get Dob from user meta */
$user_dob = get_user_meta( get_current_user_id(), 'bwfan_birthday_date', true );
$birthday_date = empty( $dob ) ? $user_dob : $dob;
if ( ! empty( $birthday_date ) ) {
$date_parts = explode( '-', $birthday_date );
if ( count( $date_parts ) === 3 ) {
$selected_year = $date_parts[0];
$selected_month = $date_parts[1];
$selected_date = $date_parts[2];
}
}
$hide_field = ! empty( $global_settings['bwfan_hide_dob_field'] ) && ! empty( $birthday_date );
if ( is_account_page() && ! empty( $hide_field ) ) {
BWFAN_Birthday::show_birthday_on_my_profile( $user );
}
// If field should be hidden on account page, don't display anything
if ( $hide_field ) {
return;
}
if ( ! $use_dropdown_ui ) {
$max = BWFAN_PRO_Common::get_birthday_max_value();
$min = BWFAN_PRO_Common::get_birthday_min_value();
$input_args = 'type=date';
$placeholder = '';
if ( 'wfacp-modern-label' === $label_position ) {
if ( empty( $birthday_date ) ) {
$input_args = 'type=text';
}
$input_args .= ' onfocus="(this.type=\'date\')" onblur="(this.value == \'\' ? this.type=\'text\' : \'\')"';
$placeholder = ! empty( $placeholder_label ) ? $placeholder_label : $birthday_label;
}
}
}
/** If year, month and day are empty */
if ( empty( $selected_year ) && empty( $selected_date ) && empty( $selected_month ) && ! is_null( WC()->session ) ) {
$selected_year = WC()->session->get( 'bwfan_birthday_date_yy', '' );
$selected_date = WC()->session->get( 'bwfan_birthday_date_dd', '' );
$selected_month = WC()->session->get( 'bwfan_birthday_date_mm', '' );
}
$dob_class = '';
if ( $use_dropdown_ui ) {
$dob_class = 'bwfan-dob-wrapper';
}
if ( empty( $wfacp_id ) && ! empty( $required ) ) {
$class .= " validate-required";
}
?>
<div class="bwfan-birthday-section bwfan-birthday-section--checkout form-row form-row-wide <?php echo $label_position; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>">
<p class="form-row form-row-wide wfacp-anim-wrap <?php echo $class; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" id="bwfan_birthday_field">
<label for="bwfan_birthday_date" class="<?php echo $label_class; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> bwfan-birthday-label"><?php echo $birthday_label; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php if ( ! empty( $required ) ) { ?>
<abbr class="required" title="required">*</abbr>
<?php } ?>
</label>
<span class="woocommerce-input-wrapper <?php echo $dob_class; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>">
<?php
if ( $use_dropdown_ui ) :
?>
<select name="bwfan_birthday_date_mm" id="bwfan_birthday_date_mm" class="bwfan-dob-field bwf-wid-90 wfacp_dob wfacp_month wfacp-form-control" data-field="<?php echo esc_attr( $birthday_label ); ?>" data-label="month">
<option value=""><?php esc_html_e( 'Select Month', 'wp-marketing-automations-pro' ); ?></option>
<?php foreach ( BWFAN_Birthday::get_date_select_options( 'month' ) as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected_month, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<select name="bwfan_birthday_date_dd" id="bwfan_birthday_date_dd" class="bwfan-dob-field bwf-wid-90 wfacp_dob wfacp_da wfacp-form-control" data-field="<?php echo esc_attr( $birthday_label ); ?>" data-label="day">
<option value=""><?php esc_html_e( 'Select Day', 'wp-marketing-automations-pro' ); ?></option>
<?php foreach ( BWFAN_Birthday::get_date_select_options( 'date' ) as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected_date, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<select name="bwfan_birthday_date_yy" id="bwfan_birthday_date_yy" class="bwfan-dob-field bwf-wid-90 wfacp_dob wfacp_year wfacp-form-control" data-field="<?php echo esc_attr( $birthday_label ); ?>" data-label="year">
<option value=""><?php esc_html_e( 'Select Year', 'wp-marketing-automations-pro' ); ?></option>
<?php foreach ( BWFAN_Birthday::get_date_select_options( 'year' ) as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected_year, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<?php else : ?>
<input <?php echo esc_attr( $input_args ) ?> class="input-text <?php echo esc_attr( $input_class ) ?>" name="bwfan_birthday_date" id="bwfan_birthday_date" placeholder="<?php echo esc_html( $placeholder ) ?>" value="<?php echo esc_sql( $birthday_date ) ?>" <?php echo $max; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> <?php echo $min; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> >
<?php endif; ?>
</span>
</p>
</div>

View File

@@ -0,0 +1,174 @@
<?php
defined( 'ABSPATH' ) || exit; //Exit if accessed directly
if ( ! class_exists( 'WFFN_Optin_Form_Field_Birthday' ) ) {
/**
* This class will control all Optin Birthday mapping functionality on optin submission.
* Class WFFN_Optin_Form_Field_Birthday
*/
class WFFN_Optin_Form_Field_Birthday extends WFFN_Optin_Form_Field {
private static $ins = null;
public static $slug = 'optin_birthday';
public $index = 30;
/**
* WFFN_Optin_Form_Field_Birthday constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* @return WFFN_Optin_Form_Field_Birthday|null
*/
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self;
}
return self::$ins;
}
/**
* @return string
*/
public static function get_slug() {
return self::$slug;
}
/**
* Return title of this form field
*/
public function get_title() {
return __( 'Birthday', 'wp-marketing-automations-pro' );
}
/**
* @param $field_data
*
* @return string|void
*/
public function get_field_output( $field_data ) {
$field_data = wp_parse_args( $field_data, $this->get_field_format() );
$global_settings = BWFAN_Common::get_global_settings();
$name = $this->get_prefix() . $this::get_slug();
$width = isset( $field_data['width'] ) ? esc_attr( $field_data['width'] ) : '';
$label = isset( $field_data['label'] ) ? esc_attr( $field_data['label'] ) : '';
$required = isset( $field_data['required'] ) ? esc_attr( $field_data['required'] ) : false;
$hash = isset( $field_data['hash_key'] ) ? esc_attr( $field_data['hash_key'] ) : '';
$value = $this->get_default_value( $field_data );
$class = $this->get_input_class( $field_data );
if ( ! empty( $global_settings['bwfan_hide_dob_field'] ) && ! empty( $value ) ) {
return;
}
// Parse date for the dropdowns if we have a value
$selected_date = '';
$selected_month = '';
$selected_year = '';
if ( ! empty( $value ) ) {
$date_parts = explode( '-', $value );
if ( count( $date_parts ) === 3 ) {
$selected_year = $date_parts[0];
$selected_month = $date_parts[1];
$selected_date = $date_parts[2];
}
}
?>
<style>
.bwfac_form_field_birthday .wffn-optin-input,
.bwfac_form_field_birthday .bwfan-dob-wrapper {
width: 100%;
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.bwfac_form_field_birthday .bwfan-dob-field {
flex: 1;
min-width: 80px;
}
</style>
<div class="bwfac_form_sec bwfac_form_field_birthday <?php echo esc_attr( $width ); ?>">
<?php if ( ! empty( $label ) ) { ?>
<label for="wfop_id_<?php echo esc_attr( $name ) . '_' . esc_attr( $hash ); ?>"><?php echo esc_html( $label );
echo ( $required ) ? '<span>*</span>' : ''; ?> </label>
<?php } ?>
<div class="wfop_input_cont bwfan-dob-wrapper">
<select name="<?php echo esc_attr( $name ); ?>_mm" class="bwfan-dob-field wffn-optin-input <?php echo esc_attr( $class ) ?>" data-field="<?php echo esc_attr( $label ); ?>" data-label="month">
<option value=""><?php esc_html_e( 'Select Month', 'wp-marketing-automations-pro' ); ?></option>
<?php foreach ( BWFAN_Birthday::get_date_select_options( 'month' ) as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected_month, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<select name="<?php echo esc_attr( $name ); ?>_dd" class="bwfan-dob-field wffn-optin-input <?php echo esc_attr( $class ) ?>" data-field="<?php echo esc_attr( $label ); ?>" data-label="day">
<option value=""><?php esc_html_e( 'Select Day', 'wp-marketing-automations-pro' ); ?></option>
<?php foreach ( BWFAN_Birthday::get_date_select_options( 'date' ) as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected_date, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<select name="<?php echo esc_attr( $name ); ?>_yy" class="bwfan-dob-field wffn-optin-input <?php echo esc_attr( $class ) ?>" data-field="<?php echo esc_attr( $label ); ?>" data-label="year">
<option value=""><?php esc_html_e( 'Select Year', 'wp-marketing-automations-pro' ); ?></option>
<?php foreach ( BWFAN_Birthday::get_date_select_options( 'year' ) as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected_year, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<input type="hidden" id="wfop_id_<?php echo esc_attr( $name ) . '_' . esc_attr( $hash ); ?>" class="<?php echo esc_attr( $class ) ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>">
</div>
</div>
<?php
}
/**
* @return array
*/
public function get_field_format() {
return array(
'width' => 'wffn-sm-100',
'type' => $this::get_slug(),
'label' => __( 'Birthday', 'wp-marketing-automations-pro' ),
'placeholder' => '',
'required' => false,
'InputName' => $this->get_prefix() . $this::get_slug(),
'default' => '',
);
}
public function get_sanitized_value( $data, $field ) {
// Handle new format with multiple selects
$base_name = $field['InputName'];
$year = isset( $data[ $base_name . '_yy' ] ) ? wffn_clean( $data[ $base_name . '_yy' ] ) : '';
$month = isset( $data[ $base_name . '_mm' ] ) ? wffn_clean( $data[ $base_name . '_mm' ] ) : '';
$day = isset( $data[ $base_name . '_dd' ] ) ? wffn_clean( $data[ $base_name . '_dd' ] ) : '';
// If all parts are present, create the date
if ( ! empty( $year ) && ! empty( $month ) && ! empty( $day ) ) {
return $year . '-' . $month . '-' . $day;
}
// Fall back to old format if new format isn't present
return isset( $data[ $base_name ] ) ? wffn_clean( $data[ $base_name ] ) : '';
}
public function get_default_value( $field_data ) {
if ( ! WFFN_Common::is_page_builder_editor() && true === apply_filters( 'wffn_optin_default_login_data', true, $field_data ) ) {
global $current_user;
if ( ! $current_user instanceof WP_User ) {
return '';
}
return get_user_meta( $current_user->ID, 'bwfan_birthday_date', true );
}
return '';
}
}
if ( class_exists( 'WFOPP_Core' ) ) {
WFOPP_Core()->form_fields->register( WFFN_Optin_Form_Field_Birthday::get_instance() );
}
}

View File

@@ -0,0 +1,3 @@
<?php
//silence is golden