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