1,
'tuesday' => 2,
'wednesday' => 3,
'thursday' => 4,
'friday' => 5,
'saturday' => 6,
'sunday' => 7,
);
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'handle_track_open' ), 99 );
add_action( 'plugins_loaded', array( $this, 'handle_track_click' ), 100 );
add_action( 'plugins_loaded', array( $this, 'modify_unsubscribe_link' ), 101 );
add_filter( 'bwfan_sendemail_make_data', array( $this, 'get_subject_mergetags' ), 10, 2 );
add_action( 'bwfan_conversation_sendemail_action', array( $this, 'updating_email_conversation_status' ), 10, 3 );
add_action( 'bwfan_sendsms_action_response', array( $this, 'update_engagement_status' ), 10, 2 );
add_action( 'bwfan_external_global_settings', array( $this, 'bwfan_order_conversation_settings' ), 10, 1 );
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* Get subject and pre header merge tags and assign to class properties
*
* @param $data_to_set
* @param $task_meta
*
* @return mixed
*/
public function get_subject_mergetags( $data_to_set, $task_meta ) {
if ( empty( $task_meta['data']['subject'] ) && empty( $task_meta['data']['preheading'] ) ) {
return $data_to_set;
}
$subject = [];
$pre_header = [];
if ( ! empty( $task_meta['data']['subject'] ) ) {
$subject = method_exists( 'BWFAN_Common', 'fetch_merge_tags' ) ? BWFAN_Common::fetch_merge_tags( $task_meta['data']['subject'] ) : $task_meta['data']['subject'];
}
if ( ! empty( $task_meta['data']['preheading'] ) ) {
$pre_header = method_exists( 'BWFAN_Common', 'fetch_merge_tags' ) ? BWFAN_Common::fetch_merge_tags( $task_meta['data']['preheading'] ) : $task_meta['data']['preheading'];
}
self::$subject_merge_tag = array_merge( $subject, $pre_header );
return $data_to_set;
}
/**
* Add tracking code in email body
*
* @param $body
* @param $data
* @param $hash_code
* @param $automation_id
* @param false $automation_open_click
* @param int $mode
*
* @return array|mixed|string|string[]|null
*/
public function add_tracking_code( $body, $data, $hash_code, $automation_id, $automation_open_click = false, $mode = 1 ) {
$body = BWFAN_Common::bwfan_correct_protocol_url( $body );
/** Check for click tracking */
if ( $automation_open_click ) {
$this->track_click_utm_parameters['bwfan-track-action'] = 'click';
$this->track_click_utm_parameters['bwfan-track-id'] = $hash_code;
}
$cid = $data['cid'];
$contact = new BWFCRM_Contact( $cid );
if ( $contact->is_contact_exists() ) {
$uid = $contact->contact->get_uid();
$this->track_click_utm_parameters['bwfan-uid'] = $uid;
$this->contact = $contact;
}
$is_disable_click_tracking = apply_filters( 'bwfan_disable_click_tracking_for_automation_' . $automation_id, false );
/** Replace URLs for click tracking */
if ( ! empty( $this->track_click_utm_parameters ) && false === $is_disable_click_tracking ) {
$this->track_click_utm_parameters = apply_filters( 'bwfan_track_click_utm_parameters', $this->track_click_utm_parameters, $data );
$this->body = $body;
$this->engagement_mode = $mode === self::$MODE_EMAIL ? 'email' : ( $mode === self::$MODE_SMS ? 'sms' : 'whatsapp' );
$regex_pattern = BWFAN_PRO_Common::get_regex_pattern( 'email' !== $this->engagement_mode ? 3 : 1 );
$body = preg_replace_callback( $regex_pattern, function ( $matches ) use ( $hash_code ) {
/** According to Href (1) regex, URL is at 1 index. And for Link (3) Regex, 0 index. */
$url = 'email' !== $this->engagement_mode ? $matches[0] : $matches[1];
/** Check url needs to exclude from click track */
if ( BWFAN_PRO_Common::is_exclude_url( $url ) ) {
return $url;
}
/** Exclude click tracking for unsubscribe link and view email browser link*/
if ( false === strpos( $url, 'bwfan-action=unsubscribe' ) && false === strpos( $url, 'bwfan-action=view_in_browser' ) ) {
$url = $this->append_tracking_in_url( $url );
}
/** Add track id in unsubscribe link */
if ( false !== strpos( $url, 'bwfan-action=unsubscribe' ) ) {
$link = urlencode( str_replace( 'amp;', '', $url ) );
$url = add_query_arg( [
'bwfan-track-id' => $hash_code,
'bwfan-link' => $link
], home_url() );
}
/** Add hash_code if view email browser link */
if ( false !== strpos( $url, 'bwfan-action=view_in_browser' ) ) {
/** Get hash code from url */
$bwfan_ehash = filter_input( INPUT_GET, 'bwfan-ehash' );
/** If hashcode is set in url then add hash */
$url = empty( $bwfan_ehash ) ? add_query_arg( array(
'bwfan-ehash' => $hash_code
), $url ) : '#';
}
/** In case of Href regex, replace old URL with new one in 'HREF' string, otherwise return */
$url = 'email' !== $this->engagement_mode ? $url : str_replace( $matches[1], $url, $matches[0] );
return $url;
}, $body );
}
/** Add image for email open tracking */
if ( $automation_open_click && 1 === $mode ) {
$this->track_open_url = add_query_arg( array(
'bwfan-track-action' => 'open',
'bwfan-track-id' => $hash_code,
), home_url() );
$this->track_open_url = apply_filters( 'bwfan_track_open_url', $this->track_open_url, $data );
$old_body = $body;
$body = preg_replace_callback( '/<\/body[^>]*>/', array( $this, 'append_tracking_pixel' ), $body, 1 );
if ( $old_body === $body ) {
$body = $body . '';
}
}
$this->body = $body;
return $this->body;
}
/** modify email body content before sending
*
* @param $body
* @param $data
*
* @return string|string[]|null
*/
public function bwfan_modify_email_body_data( $body, $data ) {
$action_obj = BWFAN_Core()->integration->get_action( 'wp_sendemail' );
if ( true === $action_obj->is_preview ) {
$body = BWFAN_Common::decode_merge_tags( $body );
return $body;
}
$this->body = $this->insert_automation_conversation( $data, $body, self::$MODE_EMAIL );
$data['conversation_id'] = $this->track_id;
$data['subject_merge_tag'] = self::$subject_merge_tag;
$data['hash_code'] = $this->hash_code;
$action_obj->set_data( $data );
return $this->body;
}
public function bwfan_modify_sms_body_data( $sms_body, $data ) {
if ( ! empty( $data['test'] ) ) {
return $sms_body;
}
return $this->insert_automation_conversation( $data, $sms_body, self::$MODE_SMS );
}
public function insert_automation_conversation( $data, $body, $mode ) {
$automation_id = ! empty( $data['automation_id'] ) ? $data['automation_id'] : 0;
$task_id = ! empty( $data['task_id'] ) ? $data['task_id'] : 0;
$step_id = ! empty( $data['step_id'] ) ? $data['step_id'] : 0;
$email = isset( $data['email'] ) ? $data['email'] : '';
$phone = isset( $data['phone'] ) ? $data['phone'] : '';
$user_id = isset( $data['user_id'] ) ? $data['user_id'] : '';
$template_type = isset( $data['template'] ) ? $data['template'] : '';
$is_track_enable = self::is_automation_open_click_track( $automation_id );
$send_to = $mode === self::$MODE_SMS ? $phone : $email;
$hash_code = md5( time() . $send_to . $task_id );
$cid = BWFAN_PRO_Common::get_cid_from_contact( $email, $user_id, $phone );
$create_time = current_time( 'mysql', 1 );
/** Template Addition */
$subject = $mode === self::$MODE_EMAIL && isset( $data['subject'] ) ? $data['subject'] : '';
$data['cid'] = $cid;
$template_id = self::check_already_exists_template( $subject, $body, $mode, $data );
$insert_data = array(
'cid' => $cid,
'hash_code' => $hash_code,
'created_at' => $create_time,
'updated_at' => $create_time,
'mode' => $mode,
'send_to' => $send_to,
'type' => self::$TYPE_AUTOMATION,
'open' => 0,
'click' => 0,
'oid' => $automation_id,
'sid' => $step_id,
'author_id' => get_current_user_id(),
'tid' => $template_id,
'o_interaction' => '',
'c_interaction' => '',
'c_status' => self::$STATUS_DRAFT,
);
/** Insert Conversation, (Before adding merge tags) */
BWFAN_Model_Engagement_Tracking::insert( $insert_data );
$this->track_id = BWFAN_Model_Engagement_Tracking::insert_id();
/** Fetch Merge tags and add to Conversation Meta, for quick uses */
$subject_merge_tags = self::get_email_merge_tags( $subject, $template_type );
$merge_tags = self::get_email_merge_tags( $body, $template_type );
$merge_tags = ! empty( $subject_merge_tags ) ? array_merge( $merge_tags, $subject_merge_tags ) : $merge_tags;
ksort( $merge_tags );
self::insert_conversation_meta( $this->track_id, $merge_tags );
/** Merge tags replaced body */
if ( method_exists( 'BWFAN_Common', 'replace_merge_tags' ) ) {
$body = BWFAN_Common::replace_merge_tags( $body, $merge_tags, $cid );
} else {
foreach ( $merge_tags as $tag => $value ) {
$body = str_replace( $tag, $value, $body );
}
}
$global_settings = BWFAN_Common::get_global_settings();
$disable_click_tracking = isset( $global_settings['bwfan_disable_sms_tracking'] ) && ! empty( $global_settings['bwfan_disable_sms_tracking'] ) ? $global_settings['bwfan_disable_sms_tracking'] : 0;
/** return body without adding tracking detail in url if disabled and mode not email */
if ( $disable_click_tracking && $mode !== self::$MODE_EMAIL ) {
return $body;
}
/** Add tracking code */
$body = $this->add_tracking_code( $body, $data, $hash_code, $automation_id, $is_track_enable, $mode );
return $body;
}
/**
* Checking template exists
*
* @param $template_subject
* @param $template_body
* @param int $type
* @param array $data
* @param bool $create_if_canned
*
* @return int
*/
public static function check_already_exists_template( $template_subject, $template_body, $type = 1, $data = [], $create_if_canned = true ) {
global $wpdb;
$create_time = current_time( 'mysql', 1 );
$mode = isset( $data['template'] ) ? intval( $data['template'] ) : 1;
unset( $data['template'] );
$templates_data = array(
'template' => $template_body,
'subject' => $template_subject,
'type' => $type,
'mode' => $mode,
'created_at' => $create_time,
'updated_at' => $create_time,
);
$canned_query = '';
if ( true === $create_if_canned ) {
$canned_query = ' AND canned = 0';
}
$query = $wpdb->prepare( 'SELECT `ID` FROM {table_name} WHERE `template` = "%s" AND `subject` = "%s"' . $canned_query, $template_body, $template_subject );
$core_cache_obj = WooFunnels_Cache::get_instance();
$template_id = $core_cache_obj->get_cache( md5( $query ), 'fka-automation' );
if ( false === $template_id ) {
$template_data = BWFAN_Model_Templates::get_results( $query );
if ( ! empty( $template_data[0]['ID'] ) ) {
$template_id = intval( $template_data[0]['ID'] );
$core_cache_obj->set_cache( md5( $query ), $template_id, 'fka-automation' );
return $template_id;
}
BWFAN_Model_Templates::insert( $templates_data );
$template_id = BWFAN_Model_Templates::insert_id();
$core_cache_obj->set_cache( md5( $query ), $template_id, 'fka-automation' );
}
return $template_id;
}
public static function get_email_merge_tags( $body, $type = '' ) {
$stripped_merge_tags = array();
if ( is_array( $body ) ) {
return $stripped_merge_tags;
}
$merge_tags = array();
if ( method_exists( 'BWFAN_Common', 'fetch_merge_tags' ) ) {
$merge_tags = BWFAN_Common::fetch_merge_tags( $body );
} else {
preg_match_all( '/\{\{(.*?)\}\}/', $body, $more_merge_tags );
if ( is_array( $more_merge_tags[0] ) && count( $more_merge_tags[0] ) > 0 ) {
$merge_tags = array_filter( array_values( $more_merge_tags[0] ) );
}
}
if ( empty( $merge_tags ) ) {
return $stripped_merge_tags;
}
/** Checking if merge tag in available in subject */
if ( ! empty( self::$subject_merge_tag ) ) {
$merge_tags = array_unique( array_merge( $merge_tags, self::$subject_merge_tag ) );
}
foreach ( $merge_tags as $tag ) {
if ( isset( $stripped_merge_tags[ $tag ] ) ) {
continue;
}
$decoded_val = BWFAN_Common::decode_merge_tags( $tag );
/** Check for block editor inside mergetags */
if ( $type == 'block' || $type == 5 ) {
$decoded_val = BWFAN_Common::decode_merge_tags( $decoded_val );
}
$stripped_merge_tags[ $tag ] = $decoded_val;
}
return $stripped_merge_tags;
}
public static function insert_conversation_meta( $track_id, $merge_tags = array(), $err_msg = false ) {
$meta = array();
if ( ! empty( $merge_tags ) ) {
$meta['merge_tags'] = $merge_tags;
}
if ( ! empty( $err_msg ) ) {
$meta['error_msg'] = $err_msg;
}
foreach ( $meta as $key => $value ) {
$data = array(
'eid' => $track_id,
'meta_key' => $key,
'meta_value' => is_array( $value ) ? json_encode( $value ) : $value,
);
BWFAN_Model_Engagement_Trackingmeta::insert( $data );
}
}
/**
* @param $matches
*
* @return string
*/
public function append_tracking_pixel( $matches ) {
return '
' . $matches[0];
}
/**
* @param $url
*
* @return string
*/
public function append_tracking_in_url( $url ) {
if ( empty( $url ) ) {
return $url;
}
/** Check url needs to exclude */
if ( BWFAN_PRO_Common::is_exclude_url( $url ) ) {
return $url;
}
$is_link_trigger = apply_filters( 'bwfan_is_link_trigger_url', false, $url );
$home_url = $url;
if ( false === $is_link_trigger ) {
/** When multiple links and if unset because of link trigger URL, then needs to add again */
$this->track_click_utm_parameters['bwfan-track-action'] = 'click';
$this->track_click_utm_parameters['bwfan-link'] = urlencode( str_replace( 'amp;', '', $url ) );
} else {
unset( $this->track_click_utm_parameters['bwfan-track-action'] );
unset( $this->track_click_utm_parameters['bwfan-link'] );
/** Add the Auth Hash */
if ( ! is_null( $this->contact ) ) {
$auth_hash = BWFCRM_Core()->link_trigger_handler->get_auth_hash( $this->contact, $home_url );
if ( false !== $auth_hash ) {
$this->track_click_utm_parameters['bwfan-auth'] = $auth_hash;
}
}
}
return add_query_arg( $this->track_click_utm_parameters, $home_url );
}
public function get_email_tracking_details( $task_id ) {
if ( empty( $task_id ) ) {
return '';
}
global $wpdb;
$email_tracking_table = $wpdb->prefix . 'bwfan_email_tracking';
$track_data = $wpdb->get_results( "SELECT * FROM $email_tracking_table WHERE `task_id` = '" . $task_id . "' ORDER BY `c_date` DESC LIMIT 0,1", ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $track_data ) ) {
return '';
}
return $track_data[0];
}
public function handle_track_open() {
if ( ! isset( $_GET['bwfan-track-action'] ) || ! isset( $_GET['bwfan-track-id'] ) || 'open' !== $_GET['bwfan-track-action'] || empty( $_GET['bwfan-track-id'] ) ) {
return;
}
$track_id = $_GET['bwfan-track-id'];
$conversation_query = "SELECT * FROM {table_name} WHERE `hash_code` = '" . $track_id . "' AND `c_status` = 2 LIMIT 0,1";
$get_row = BWFAN_Model_Engagement_Tracking::get_results( $conversation_query );
/** If no engagement found */
if ( ! is_array( $get_row ) || 0 === count( $get_row ) ) {
exit;
}
if ( true === $this->should_skip_open_click_track( $track_id, $get_row ) ) {
exit;
}
$this->record_tracking( $track_id, 'open', $get_row );
$image_path = BWFAN_PLUGIN_DIR . '/admin/assets/img/blank.gif';
// render image
header( 'Content-Type: image/gif' );
header( 'Pragma: public' ); // required
header( 'Expires: 0' ); // no cache
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private', false );
header( 'Content-Disposition: attachment; filename="blank.gif"' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize( $image_path ) ); // provide file size
readfile( $image_path );
exit;
}
/** get tracking data of automation
*
* @param $automation_id
*
* @return array|mixed
*/
public function get_email_tracking_of_automation( $automation_id ) {
global $wpdb;
$tracking_table = $wpdb->prefix . 'bwfan_email_tracking';
$automation_tracking_data = $wpdb->get_results( "SELECT sum(click) as total_click , sum(open) as total_open FROM $tracking_table WHERE `aid`= '" . $automation_id . "'", ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( empty( $automation_tracking_data ) ) {
return array();
}
return $automation_tracking_data[0];
}
/** saving tracking details of email
*
* @param $bwfan_track_id
* @param $type
*/
public function record_tracking( $bwfan_track_id, $type, $get_row ) {
if ( empty( $bwfan_track_id ) || empty( $type ) ) {
return;
}
if ( empty( $get_row ) || ! is_array( $get_row ) || ! isset( $get_row[0][ $type ] ) ) {
return;
}
$day = $get_row[0]['day'];
$hour = $get_row[0]['hour'];
$count = $get_row[0][ $type ];
$first_o_c = $get_row[0][ 'f_' . $type ];
$action = ( $type === 'click' ) ? 'c' : 'o';
$interactions = ! empty( $get_row[0][ $action . '_interaction' ] ) ? json_decode( $get_row[0][ $action . '_interaction' ], true ) : array();
$current_time = current_time( 'mysql' );
$interactions[] = $current_time;
/** Checking last 10 interactions */
if ( count( $interactions ) > 10 ) {
/** Remove first interaction */
array_shift( $interactions );
}
$count ++;
$values = array(
'updated_at' => current_time( 'mysql', 1 ),
$type => $count,
$action . '_interaction' => wp_json_encode( $interactions ),
);
/** Check if action clicked and open count is zero */
if ( 'c' === $action && 0 === absint( $get_row[0]['open'] ) ) {
$o_time = current_time( 'timestamp' );
$o_time = $o_time - 20;
$o_time = date( "Y-m-d H:i:s", $o_time );
$o_interaction = [ $o_time ];
$values['open'] = 1;
$values['f_open'] = $o_time;
$values['o_interaction'] = wp_json_encode( $o_interaction );
}
if ( empty( $day ) ) {
$values['day'] = current_time( 'N' );
}
if ( empty( $hour ) ) {
$hour = current_time( 'H' );
$values['hour'] = $hour;
}
if ( empty( $first_o_c ) ) {
$values[ 'f_' . $type ] = $current_time;
}
$where = array(
'ID' => $get_row[0]['ID'],
);
BWFAN_Model_Engagement_Tracking::update( $values, $where );
if ( ! isset( $get_row[0]['cid'] ) || empty( $get_row[0]['cid'] ) ) {
return;
}
/** Set cookie */
if ( $type === 'click' ) {
$contact = new WooFunnels_Contact( '', '', '', $get_row[0]['cid'] );
if ( $contact instanceof WooFunnels_Contact ) {
$uid = $contact->get_uid();
if ( ! empty( $uid ) ) {
BWFAN_Common::set_cookie( '_fk_contact_uid', $uid, time() + 10 * 365 * 24 * 60 * 60 );
}
}
}
/** Set last open & click custom field */
$open_col = BWFAN_Model_Fields::get_field_by_slug( "last-open" );
$fields = [];
if ( isset( $open_col['ID'] ) ) {
$fields = array(
'f' . $open_col['ID'] => $current_time
);
}
if ( $type === 'click' ) {
$click_col = BWFAN_Model_Fields::get_field_by_slug( "last-click" );
if ( isset( $click_col['ID'] ) ) {
$fields[ 'f' . $click_col['ID'] ] = $current_time;
}
}
if ( empty( $fields ) ) {
return;
}
$where = array( 'cid' => absint( $get_row[0]['cid'] ) );
BWF_Model_Contact_Fields::update( $fields, $where );
}
/**
* handling email tracking on click
*/
public function handle_track_click() {
if ( ! isset( $_GET['bwfan-track-id'] ) || empty( $_GET['bwfan-track-id'] ) ) {
return;
}
if ( ! isset( $_GET['bwfan-track-action'] ) || 'click' !== $_GET['bwfan-track-action'] ) {
return;
}
$link = filter_input( INPUT_GET, 'bwfan-link' );
if ( is_null( $link ) || empty( $link ) ) {
$this->track_click_skip();
return;
}
$link = BWFAN_PRO_Common::validate_target_link( $link );
if ( false !== strpos( $link, 'bwfan-action=incentive' ) ) {
BWFAN_PRO_Common::wp_redirect( $link );
exit;
}
$link = urldecode( $link );
$link = str_replace( 'amp;', '', $link );
$link = str_replace( '&', '&', $link );
$link = BWFAN_Common::bwfan_correct_protocol_url( $link );
if ( false === wp_http_validate_url( $link ) ) {
$this->track_click_skip();
BWFAN_PRO_Common::wp_redirect( $link );
exit;
}
$link = BWFCRM_Core()->link_trigger_handler->may_append_query_arg( $link );
$engagement_data = $this->get_engagement_data();
/** Record tracking */
$this->track_click_skip( $link, $engagement_data );
$link = $this->url_decode( $link );
/** Filter to modify link after click tracking */
$link = apply_filters( 'bwfan_modify_target_link', $link );
BWFAN_PRO_Common::wp_redirect( $link );
exit;
}
/**
* Append step id in unsubscribe link
*
* @return void
*/
public function modify_unsubscribe_link() {
$track_id = filter_input( INPUT_GET, 'bwfan-track-id' );
$link = filter_input( INPUT_GET, 'bwfan-link' );
if ( empty( $track_id ) || empty( $link ) ) {
return;
}
if ( false === strpos( $link, 'bwfan-action=unsubscribe' ) ) {
return;
}
$link = BWFAN_PRO_Common::validate_target_link( $link );
$engagement_data = $this->get_engagement_data();
if ( empty( $engagement_data ) ) {
return;
}
/** Append sid in unsubscribe link */
$link = add_query_arg( array(
'sid' => $engagement_data[0]['sid'],
), $link );
BWFAN_PRO_Common::wp_redirect( $link );
exit;
}
public function track_click_skip( $target_url = '', $get_row = [] ) {
$track_id = filter_input( INPUT_GET, 'bwfan-track-id' );
if ( empty( $get_row ) ) {
$get_row = $this->get_engagement_data();
}
/** If no engagement found */
if ( ! is_array( $get_row ) || 0 === count( $get_row ) ) {
return;
}
/**
* Maybe machine interaction found
* Stop further processing and redirect to the given URL or home page
*/
if ( true === $this->should_skip_open_click_track( $track_id, $get_row ) ) {
$target_url = empty( $target_url ) ? home_url() : $target_url;
BWFAN_PRO_Common::wp_redirect( $target_url );
exit;
}
$this->record_tracking( $track_id, 'click', $get_row );
}
public function get_engagement_data() {
$track_id = filter_input( INPUT_GET, 'bwfan-track-id' );
$conversation_query = "SELECT * FROM {table_name} WHERE `hash_code`='" . $track_id . "' AND `c_status` = 2 LIMIT 0,1";
$query_md5 = md5( $conversation_query );
if ( isset( self::$query_cache[ $query_md5 ] ) ) {
return self::$query_cache[ $query_md5 ];
}
$data = BWFAN_Model_Engagement_Tracking::get_results( $conversation_query );
self::$query_cache[ $query_md5 ] = $data;
return $data;
}
/**
* @param $desc
* @param string $size
* @param string $position
*
* @return false|string
*/
public function add_description( $desc, $size = 'm', $position = 'top' ) {
if ( empty( $desc ) ) {
return '';
}
ob_start();
?>