1,
'tuesday' => 2,
'wednesday' => 3,
'thursday' => 4,
'friday' => 5,
'saturday' => 6,
'sunday' => 7,
);
public static $template = null;
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_Common::get_regex_pattern( 'email' !== $this->engagement_mode ? 3 : 1 );
$body = preg_replace_callback( $regex_pattern, function ( $matches ) use ( $hash_code, $data ) {
/** 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_Common::is_exclude_url( $url ) ) {
return $matches[0];
}
/** 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' ) ) {
BWFAN_Core()->conversation->template = self::$template;
$l_hash = BWFAN_Core()->conversation->get_link_hash( $url, $data );
$url = $this->append_tracking_in_url( $url, $l_hash );
}
/** Add step id in unsubscribe link */
if ( false !== strpos( $url, 'bwfan-action=unsubscribe' ) ) {
$link = str_replace( 'amp;', '', $url );
/** If step id exists in data then no need to pass track id */
if ( ! empty( $data['step_id'] ) ) {
$url = add_query_arg( [
'sid' => $data['step_id'],
], $link );
} else {
$url = add_query_arg( [
'bwfan-track-id' => $hash_code,
'bwfan-link' => urlencode( $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 */
return 'email' !== $this->engagement_mode ? $url : str_replace( $matches[1], $url, $matches[0] );
}, $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 );
}
/**
* Insert engagement in DB
*
* @param $data
* @param $body
* @param $mode
*
* @return array|mixed|string|string[]|null
*/
public function insert_automation_conversation( $data, $body, $mode ) {
$automation_id = ! empty( $data['automation_id'] ) ? $data['automation_id'] : 0;
$step_id = ! empty( $data['step_id'] ) ? $data['step_id'] : 0;
$email = isset( $data['email'] ) ? $data['email'] : '';
$phone = isset( $data['phone'] ) ? $data['phone'] : '';
$template_type = isset( $data['template'] ) ? $data['template'] : '';
$is_track_enable = self::is_automation_open_click_track( $automation_id );
$send_to = in_array( $mode, [ self::$MODE_SMS, self::$MODE_WHATSAPP ] ) ? $phone : $email;
$hash_code = md5( microtime( true ) . $send_to . $step_id );
$contact_email = '';
$cid = 0;
if ( isset( $data['contact_id'] ) && ! empty( $data['contact_id'] ) ) {
$contact = new WooFunnels_Contact( '', '', '', $data['contact_id'] );
$contact_email = $contact->get_email();
$cid = $contact->get_id();
}
$e_cid = ( $send_to !== $contact_email ) ? BWFAN_Common::get_cid_from_contact( $email, 0, $phone ) : $cid;
$create_time = current_time( 'mysql', 1 );
/** Template Addition */
$subject = $mode === self::$MODE_EMAIL && isset( $data['subject'] ) ? $data['subject'] : '';
if ( empty( $subject ) && $mode === self::$MODE_NOTIFICATION ) {
$subject = isset( $data['notification_title'] ) ? $data['notification_title'] : $subject;
}
$data['cid'] = $cid;
$template_id = self::check_already_exists_template( $subject, $body, $mode, $data );
$data['template_id'] = $template_id;
$insert_data = array(
'cid' => $e_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_ignore( $insert_data, [
'%d', // cid
'%s', // hash_code
'%s', // created_at
'%s', // updated_at
'%d', // mode
'%s', // send_to
'%d', // type
'%d', // open
'%d', // click
'%d', // oid
'%d', // sid
'%d', // author_id
'%d', // tid
'%s', // o_interaction
'%s', // c_interaction
'%d', // c_status
] );
$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 engagement type for links it will be use when saving links */
$data['type'] = self::$TYPE_AUTOMATION;
/** Add tracking code */
$body = $this->add_tracking_code( $body, $data, $hash_code, $automation_id, $is_track_enable, $mode );
if ( $mode === self::$MODE_NOTIFICATION ) {
return [
'body' => $body,
'url' => $this->add_tracking_code( $data['notification_url'], $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,
);
if ( ! empty( $data ) ) {
$templates_data['data'] = wp_json_encode( $data );
}
$canned_query = ( true === $create_if_canned ) ? ' AND canned = 0' : '';
$table_name = $wpdb->prefix . 'bwfan_templates';
$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 = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( false !== strpos( $wpdb->last_error, 'collations' ) ) {
$wp_collation = $wpdb->get_var( "SELECT @@collation_database" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( ! empty( $wp_collation ) && false !== strpos( $wp_collation, 'unicode' ) ) {
$wp_collation = str_replace( 'unicode', 'general', $wp_collation );
}
if ( empty( $wp_collation ) ) {
$wp_collation = 'utf8mb4_general_ci';
}
$charset = ( ! empty( $wpdb->charset ) && 'utf8mb3' !== $wpdb->charset ) ? $wpdb->charset : 'utf8mb4';
$wpdb->query( "SET NAMES '$charset' COLLATE '$wp_collation'" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$template_data = $wpdb->get_results( $query, ARRAY_A ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
if ( ! empty( $template_data[0]['ID'] ) ) {
$template_id = intval( $template_data[0]['ID'] );
$core_cache_obj->set_cache( md5( $query ), $template_id, 'fka-automation' );
self::$template = $template_data[0];
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' );
}
$templates_data['ID'] = $template_id;
self::$template = $templates_data;
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];
}
/**
* Append tracking code
*
* @param $url
* @param $l_hash
*
* @return mixed|string
*/
public function append_tracking_in_url( $url, $l_hash = '' ) {
if ( empty( $url ) ) {
return $url;
}
/** escape url with mailto **/
if ( false !== strpos( $url, 'mailto:' ) || false !== strpos( $url, 'tel:' ) ) {
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';
if ( ! empty( $l_hash ) ) {
$this->track_click_utm_parameters['l_hash'] = $l_hash;
}
$this->track_click_utm_parameters['bwfan-link'] = urlencode( str_replace( 'amp;', '', $url ) );
$home_url = home_url();
} else {
unset( $this->track_click_utm_parameters['bwfan-track-action'] );
unset( $this->track_click_utm_parameters['bwfan-link'] );
/** Add the Auth Hash */
if ( bwfan_is_autonami_pro_active() && ! 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 );
}
/**
* Handle open tracking code
*
* @return void
*/
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;
}
global $wpdb;
$track_id = filter_input( INPUT_GET, 'bwfan-track-id' );
$query = $wpdb->prepare( "SELECT * FROM {table_name} WHERE `hash_code` = %s AND `c_status` = 2 LIMIT 0,1", $track_id );
$get_row = BWFAN_Model_Engagement_Tracking::get_results( $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;
}
/**
* Saving tracking details of email against a contact
*
* @param $bwfan_track_id
* @param $type
* @param $get_row
*
* @return void
*/
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 && 1 === intval( $get_row[0]['mode'] ) && 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 );
$cid = $get_row[0]['cid'];
if ( empty( $cid ) ) {
return;
}
$contact = BWFCRM_Model_Contact::get_contact_id( $cid );
/** Set cookie */
if ( $type === 'click' && is_object( $contact ) && ! empty( $contact->uid ) ) {
BWFAN_Common::set_cookie( '_fk_contact_uid', $contact->uid, time() + 10 * 365 * 24 * 60 * 60 );
}
/** Set last open & click custom field */
$this->update_contact_engagement( $cid, $type );
/** Update contact status if needed */
$this->maybe_subscribe_contact( $contact );
/** Update link metrics */
if ( 'click' === $type ) {
self::update_link_metrics( $get_row[0] );
}
}
/**
* Update contact last open and click time
*
* @param $cid
* @param $type
*
* @return void
*/
public function update_contact_engagement( $cid, $type = 'open' ) {
$current_time = current_time( 'mysql' );
$fields = [];
$col = BWFAN_Model_Fields::get_field_by_slug( "last-open" );
if ( isset( $col['ID'] ) ) {
$fields[ 'f' . $col['ID'] ] = $current_time;
}
if ( $type === 'click' ) {
$col = BWFAN_Model_Fields::get_field_by_slug( "last-click" );
if ( isset( $col['ID'] ) ) {
$fields[ 'f' . $col['ID'] ] = $current_time;
}
}
if ( empty( $fields ) ) {
return;
}
$where = array( 'cid' => intval( $cid ) );
BWF_Model_Contact_Fields::update( $fields, $where );
}
/**
* Mark contact subscribed if status is bounce or soft-bounce
*
* @param $contact - db row
*
* @return void
*/
public function maybe_subscribe_contact( $contact ) {
$allowed_status = apply_filters( 'bwfan_contact_status_to_resubscribe', [ 2, 4 ] );
if ( ! is_array( $allowed_status ) || ! in_array( intval( $contact->status ), $allowed_status, true ) ) {
return;
}
global $wpdb;
$wpdb->update( "{$wpdb->prefix}bwf_contact", [ 'status' => 1 ], [ 'id' => $contact->id ] ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
/** Delete soft bounce count meta */
$ins = WooFunnels_DB_Operations::get_instance();
$ins->delete_contact_meta( $contact->id, 'soft_bounce_count' );
}
/**
* Handle click tracking code
*
* @return void
*/
public function handle_track_click() {
$track_id = filter_input( INPUT_GET, 'bwfan-track-id' );
$track_action = filter_input( INPUT_GET, 'bwfan-track-action' );
if ( empty( $track_id ) || empty( $track_action ) || ( 'click' !== $track_action ) ) {
return;
}
$link = filter_input( INPUT_GET, 'bwfan-link' );
$l_hash = filter_input( INPUT_GET, 'l_hash' );
if ( empty( $link ) && empty( $l_hash ) ) {
$this->track_click_skip();
return;
}
$engagement_data = $this->get_engagement_data();
$link = BWFAN_Common::validate_target_link( $link, $l_hash, $engagement_data );
/** Checking source of click */
if ( self::skipping_user_agent() ) {
BWFAN_Common::wp_redirect( $link );
exit;
}
/** Redirect if incentive email action */
if ( false !== strpos( $link, 'bwfan-action=incentive' ) ) {
BWFAN_Common::wp_redirect( $link );
exit;
}
$link = self::validate_link( $link );
if ( false === wp_http_validate_url( $link ) ) {
/** check if uid is available in the link */
if ( strpos( $link, 'uid' ) !== false ) {
/** Redirect to home */
$home_url = esc_url_raw( home_url( '/?' ) );
$link = str_replace( 'http://?', $home_url, $link );
$link = str_replace( 'https://?', $home_url, $link );
if ( false !== wp_http_validate_url( $link ) ) {
$home_url = add_query_arg( $_GET, home_url( '/' ) );
$home_url = add_query_arg( array(
'bwfan-link' => urlencode( $link )
), $home_url );
BWFAN_Common::wp_redirect( $home_url );
exit;
}
}
$this->track_click_skip();
BWFAN_Common::wp_redirect( $link );
exit;
}
$link = bwfan_is_autonami_pro_active() ? BWFCRM_Core()->link_trigger_handler->may_append_query_arg( $link ) : $link;
/** Record tracking */
$this->track_click_skip( $link, $engagement_data );
/** Filter to modify link after click tracking */
$link = apply_filters( 'bwfan_modify_target_link', $link );
BWFAN_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;
}
$engagement_data = $this->get_engagement_data();
$link = BWFAN_Common::validate_target_link( $link, '', $engagement_data );
if ( empty( $engagement_data ) ) {
return;
}
/** Append sid in unsubscribe link */
$link = add_query_arg( array(
'sid' => $engagement_data[0]['sid'],
), $link );
$link = self::validate_link( $link );
BWFAN_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_Common::wp_redirect( $target_url );
exit;
}
$this->record_tracking( $track_id, 'click', $get_row );
}
public function get_engagement_data() {
global $wpdb;
$track_id = filter_input( INPUT_GET, 'bwfan-track-id' );
$query = $wpdb->prepare( "SELECT * FROM {table_name} WHERE `hash_code`=%s AND `c_status` = 2 LIMIT 0,1", $track_id );
$query_md5 = md5( $query );
if ( isset( self::$query_cache[ $query_md5 ] ) ) {
return self::$query_cache[ $query_md5 ];
}
$data = BWFAN_Model_Engagement_Tracking::get_results( $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();
?>