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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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