first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,809 @@
<?php
if ( ! class_exists( 'ES_Campaign_Controller' ) ) {
/**
* Class to handle single campaign options
*
* @class ES_Campaign_Controller
*/
class ES_Campaign_Controller {
// class instance
public static $instance;
// class constructor
public function __construct() {
$this->init();
}
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public function init() {
$this->register_hooks();
}
public function register_hooks() {
add_action( 'ig_es_' . IG_CAMPAIGN_TYPE_POST_NOTIFICATION . '_data', array( __CLASS__, 'add_post_notification_data' ) );
add_action( 'ig_es_' . IG_CAMPAIGN_TYPE_POST_DIGEST . '_data', array( __CLASS__, 'add_post_notification_data' ) );
if ( ! ES()->is_pro() ) {
// Add newsletter scheduler data
add_filter( 'ig_es_' . IG_CAMPAIGN_TYPE_NEWSLETTER . '_data', array( __CLASS__, 'add_broadcast_scheduler_data' ) );
}
add_filter( 'ig_es_campaign_data', array( __CLASS__, 'add_tracking_fields_data' ) );
// Check campaign wise open tracking is enabled.
add_filter( 'ig_es_track_open', array( __CLASS__, 'is_open_tracking_enabled' ), 10, 4 );
}
public static function save( $campaign_data ) {
$response = array();
$campaign_status = ! empty( $campaign_data['status'] ) ? (int) $campaign_data['status'] : 0;
if ( IG_ES_CAMPAIGN_STATUS_ACTIVE === $campaign_status || IG_ES_CAMPAIGN_STATUS_SCHEDULED === $campaign_status ) {
$meta = ! empty( $campaign_data['meta'] ) ? $campaign_data['meta'] : array();
if ( ! empty( $meta['list_conditions'] ) ) {
$meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] );
}
if ( empty( $meta['list_conditions'] ) ) {
$response['success'] = false;
$response['message'] = __( 'Please add recipients before activating.', 'email-subscribers' );
wp_send_json( $response );
}
}
$campaign_data = self::prepare_campaign_data( $campaign_data );
$saved_campaign_id = self::save_campaign( $campaign_data );
if ( $saved_campaign_id ) {
$response['campaign_id'] = $saved_campaign_id;
}
return $response;
}
public static function activate( $campaign_data ) {
$response = array();
$meta = ! empty( $campaign_data['meta'] ) ? $campaign_data['meta'] : array();
if ( ! empty( $meta['list_conditions'] ) ) {
$meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] );
}
if ( empty( $meta['list_conditions'] ) ) {
$response['success'] = false;
$response['message'] = __( 'Please add recipients before activating.', 'email-subscribers' );
return $response;
}
$response = self::save( $campaign_data );
return $response;
}
public static function save_and_schedule( $campaign_data ) {
$response = array();
$meta = ! empty( $campaign_data['meta'] ) ? $campaign_data['meta'] : array();
if ( ! empty( $meta['list_conditions'] ) ) {
$meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] );
}
if ( empty( $meta['list_conditions'] ) ) {
$response['success'] = false;
$response['message'] = __( 'Please add recipients before scheduling.', 'email-subscribers' );
wp_send_json( $response );
}
$saved_campaign_id = self::save( $campaign_data );
if ( $saved_campaign_id ) {
$response = self::schedule( $campaign_data );
}
return $response;
}
public static function schedule( $campaign_data ) {
$response = array(
'success' => false,
);
$scheduling_status = '';
if ( ! empty( $campaign_data['id'] ) ) {
$campaign_id = ! empty( $campaign_data['id'] ) ? $campaign_data['id'] : 0;
$campaign_meta = ES()->campaigns_db->get_campaign_meta_by_id( $campaign_id );
$notification = ES_DB_Mailing_Queue::get_notification_by_campaign_id( $campaign_id );
$base_template_id = ! empty( $campaign_data['base_template_id'] ) ? $campaign_data['base_template_id'] : 0;
$campaign_data['body'] = ES_Common::es_process_template_body( $campaign_data['body'], $base_template_id, $campaign_id );
$guid = ES_Common::generate_guid( 6 );
$meta = apply_filters( 'ig_es_before_save_campaign_notification_meta', array( 'type' => 'newsletter' ), $campaign_meta );
$data = array(
'hash' => $guid,
'campaign_id' => $campaign_id,
'subject' => $campaign_data['subject'],
'body' => $campaign_data['body'],
'status' => '',
'start_at' => ! empty( $campaign_meta['date'] ) ? $campaign_meta['date'] : '',
'finish_at' => '',
'created_at' => ig_get_current_date_time(),
'updated_at' => ig_get_current_date_time(),
'meta' => maybe_serialize( $meta ),
);
$should_queue_emails = false;
$mailing_queue_id = 0;
// Add notification to mailing queue if not already added.
if ( empty( $notification ) ) {
$data['count'] = 0;
$mailing_queue_id = ES_DB_Mailing_Queue::add_notification( $data );
$mailing_queue_hash = $guid;
$should_queue_emails = true;
} else {
$mailing_queue_id = $notification['id'];
$mailing_queue_hash = $notification['hash'];
$notification_status = $notification['status'];
// Check if notification is not sending or already sent then only update the notification.
if ( ! in_array( $notification_status, array( 'Sending', 'Sent' ), true ) ) {
// Don't update this data.
$campaign_data['hash'] = $notification['hash'];
$campaign_data['campaign_id'] = $notification['campaign_id'];
$campaign_data['created_at'] = $notification['created_at'];
// Check if list has been updated, if yes then we need to delete emails from existing lists and requeue the emails from the updated lists.
$should_queue_emails = true;
$campaign_data['count'] = 0;
$notification = ES_DB_Mailing_Queue::update_notification( $mailing_queue_id, $data );
}
}
if ( ! empty( $mailing_queue_id ) ) {
if ( $should_queue_emails ) {
$email_queued = self::queue_emails( $mailing_queue_id, $mailing_queue_hash, $campaign_id );
if ( $email_queued ) {
$response['success'] = true;
$response['data']['redirect_url'] = admin_url( 'admin.php?page=es_campaigns&id=' . $campaign_id . '&action=campaign_scheduled' );
}
}
self::maybe_send_mailing_queue( $mailing_queue_id, $mailing_queue_hash );
}
}
return $response;
}
public static function prepare_campaign_data( $campaign_data ) {
$list_id = ! empty( $campaign_data['list_ids'] ) ? $campaign_data['list_ids'] : '';
$template_id = ! empty( $campaign_data['template_id'] ) ? $campaign_data['template_id']: '';
$meta = ! empty( $campaign_data['meta'] ) ? $campaign_data['meta'] : array();
$campaign_data['subject'] = ! empty( $campaign_data['subject'] ) ? wp_strip_all_tags( $campaign_data['subject'] ) : '';
$campaign_data['base_template_id'] = $template_id;
$campaign_data['list_ids'] = $list_id;
$meta = ! empty( $campaign_data['meta'] ) ? $campaign_data['meta'] : array();
$meta['scheduling_option'] = ! empty( $campaign_data['scheduling_option'] ) ? $campaign_data['scheduling_option'] : 'schedule_now';
$meta['es_schedule_date'] = ! empty( $campaign_data['es_schedule_date'] ) ? $campaign_data['es_schedule_date'] : '';
$meta['es_schedule_time'] = ! empty( $campaign_data['es_schedule_time'] ) ? $campaign_data['es_schedule_time'] : '';
$meta['pre_header'] = ! empty( $campaign_data['pre_header'] ) ? $campaign_data['pre_header'] : '';
$meta['preheader'] = ! empty( $campaign_data['preheader'] ) ? $campaign_data['preheader'] : '';
if ( ! empty( $meta['list_conditions'] ) ) {
$meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] );
}
$campaign_type = $campaign_data['type'];
if ( self::is_post_campaign( $campaign_type ) ) {
$campaign_body = $campaign_data['body'];
if ( ! ES_Common::contains_posts_block( $campaign_body ) ) {
$campaign_body = ES_Common::wrap_post_keywords_between_campaign_posts_keyword( $campaign_body );
$campaign_data['body'] = $campaign_body;
}
}
$meta = apply_filters( 'ig_es_before_save_campaign_meta', $meta, $campaign_data );
$campaign_data['meta'] = maybe_serialize( $meta );
return $campaign_data;
}
public static function save_campaign( $campaign_data ) {
$campaign_id = ! empty( $campaign_data['id'] ) ? $campaign_data['id'] : 0;
$campaign_type = ! empty( $campaign_data['type'] ) ? $campaign_data['type'] : IG_ES_DRAG_AND_DROP_EDITOR;
$campaign_data['name'] = !empty($campaign_data['name']) ? $campaign_data['name'] : $campaign_data['subject'];
$campaign_data['slug'] = sanitize_title( sanitize_text_field( $campaign_data['name'] ) );
$campaign_data = apply_filters( 'ig_es_campaign_data', $campaign_data );
$campaign_data = apply_filters( 'ig_es_' . $campaign_type . '_data', $campaign_data );
if ( ! empty( $campaign_id ) ) {
$campaign_saved = ES()->campaigns_db->save_campaign( $campaign_data, $campaign_id );
if ( $campaign_saved ) {
return $campaign_id;
}
} else {
$campaign_saved = ES()->campaigns_db->save_campaign( $campaign_data );
if ( $campaign_saved ) {
$new_campaign_id = $campaign_saved;
$new_flow_campaign_ids = get_option( 'ig_es_new_category_format_campaign_ids', array() );
$new_flow_campaign_ids[] = $new_campaign_id;
update_option( 'ig_es_new_category_format_campaign_ids', $new_flow_campaign_ids, false );
return $new_campaign_id;
}
}
return false;
}
public static function add_post_notification_data( $campaign_data ) {
$new_flow_campaign_ids = get_option( 'ig_es_new_category_format_campaign_ids', array() );
if ( empty( $campaign_data['id'] ) || in_array( (int) $campaign_data['id'], $new_flow_campaign_ids, true ) ) {
$categories = ! empty( $campaign_data['es_note_cat'] ) ? $campaign_data['es_note_cat'] : array();
$es_note_cat_parent = ! empty( $campaign_data['es_note_cat_parent'] ) ? $campaign_data['es_note_cat_parent'] : array();
$category_array = array();
if ( '{a}None{a}' === $es_note_cat_parent ) {
$category_array[] = 'post:none';
} elseif ( '{a}All{a}' === $es_note_cat_parent ) {
$category_array[] = 'post:all';
} else {
$category_array[] = 'post:' . implode( ',', $categories );
}
$cpt_terms = ! empty( $campaign_data['cpt_terms'] ) ? $campaign_data['cpt_terms'] : array();
// Check if custom post types are selected.
if ( ! empty( $campaign_data['es_note_cpt'] ) ) {
foreach ( $campaign_data['es_note_cpt'] as $cpt ) {
$cpt = str_replace( '{T}', '', $cpt );
if ( ! empty( $cpt_terms[ $cpt ] ) ) {
$term_ids = array();
foreach ( $cpt_terms[ $cpt ] as $cpt_slug => $cpt_term_ids ) {
$term_ids = array_merge( $term_ids, $cpt_term_ids );
}
if ( ! empty( $term_ids ) ) {
$category_array[] = $cpt . ':' . implode( ',', $term_ids );
} else {
$category_array[] = $cpt . ':all';
}
} else {
$category_array[] = $cpt . ':all';
}
}
}
// Merge categories and selected custom post types.
$categories = '##' . implode( '|', $category_array ) . '##';
//$campaign_data['categories'] = $categories;
} else {
$categories = ! empty( $campaign_data['es_note_cat'] ) ? $campaign_data['es_note_cat'] : array();
$es_note_cat_parent = $campaign_data['es_note_cat_parent'];
$categories = ( ! empty( $es_note_cat_parent ) && in_array( $es_note_cat_parent, array( '{a}All{a}', '{a}None{a}' ), true ) ) ? array( $es_note_cat_parent ) : $categories;
// Check if custom post types are selected.
if ( ! empty( $campaign_data['es_note_cpt'] ) ) {
// Merge categories and selected custom post types.
$categories = array_merge( $categories, $campaign_data['es_note_cpt'] );
}
$campaign_data['categories'] = ES_Common::convert_categories_array_to_string( $categories );
}
return $campaign_data;
}
/**
* Add required broadcast schedule date/time data
*
* @param array $data
*
* @return array $data
*
* @since 4.4.7
*/
public static function add_broadcast_scheduler_data( $data ) {
$scheduling_option = ! empty( $data['scheduling_option'] ) ? $data['scheduling_option'] : 'schedule_now';
$schedule_str = '';
if ( 'schedule_now' === $scheduling_option ) {
// Get time without GMT offset, as we are adding later on.
$schedule_str = current_time( 'timestamp', false );
}
if ( ! empty( $schedule_str ) ) {
$gmt_offset_option = get_option( 'gmt_offset' );
$gmt_offset = ( ! empty( $gmt_offset_option ) ) ? $gmt_offset_option : 0;
$schedule_date = gmdate( 'Y-m-d H:i:s', $schedule_str - ( $gmt_offset * HOUR_IN_SECONDS ) );
$data['start_at'] = $schedule_date;
$meta = ! empty( $data['meta'] ) ? maybe_unserialize( $data['meta'] ) : array();
$meta['type'] = 'one_time';
$meta['date'] = $schedule_date;
$data['meta'] = maybe_serialize( $meta );
}
return $data;
}
/**
* Function to add values of checkbox fields incase they are not checked.
*
* @param array $campaign_data
*
* @return array $campaign_data
*
* @since 4.4.7
*/
public static function add_tracking_fields_data( $campaign_data = array() ) {
$campaign_meta = ! empty( $campaign_data['meta'] ) ? maybe_unserialize( $campaign_data['meta'] ) : array();
if ( empty( $campaign_meta['enable_open_tracking'] ) ) {
$campaign_meta['enable_open_tracking'] = 'no';
}
$campaign_data['meta'] = maybe_serialize( $campaign_meta );
return $campaign_data;
}
/**
* Method to check if open tracking is enabled campaign wise.
*
* @param bool $is_track_email_opens Is open tracking enabled.
* @param int $contact_id Contact ID.
* @param int $campaign_id Campaign ID.
* @param array $link_data Link data.
*
* @return bool $is_track_email_opens Is open tracking enabled.
*
* @since 4.4.7
*/
public static function is_open_tracking_enabled( $is_track_email_opens, $contact_id, $campaign_id, $link_data ) {
if ( ! empty( $link_data ) ) {
$campaign_id = ! empty( $link_data['campaign_id'] ) ? $link_data['campaign_id'] : 0;
if ( ! empty( $campaign_id ) ) {
$campaign = ES()->campaigns_db->get( $campaign_id );
if ( ! empty( $campaign ) ) {
$campaign_type = $campaign['type'];
$supported_campaign_types = array(
IG_CAMPAIGN_TYPE_NEWSLETTER,
IG_CAMPAIGN_TYPE_POST_NOTIFICATION,
IG_CAMPAIGN_TYPE_POST_DIGEST,
IG_CAMPAIGN_TYPE_WORKFLOW_EMAIL
);
$is_supported_type = in_array( $campaign_type, $supported_campaign_types, true );
if ( $is_supported_type ) {
$campaign_meta = maybe_unserialize( $campaign['meta'] );
$is_track_email_opens = ! empty( $campaign_meta['enable_open_tracking'] ) ? $campaign_meta['enable_open_tracking'] : $is_track_email_opens;
}
}
}
}
return $is_track_email_opens;
}
public static function queue_emails( $mailing_queue_id, $mailing_queue_hash, $campaign_id ) {
$list_ids = '';
// Delete existing sending queue if any already present.
ES_DB_Sending_Queue::delete_by_mailing_queue_id( array( $mailing_queue_id ) );
$emails_queued = ES_DB_Sending_Queue::queue_emails( $mailing_queue_id, $mailing_queue_hash, $campaign_id, $list_ids );
if ( $emails_queued ) {
return true;
} else {
return false;
}
}
public static function maybe_send_mailing_queue( $mailing_queue_id, $mailing_queue_hash ) {
$mailing_queue = ES_DB_Mailing_Queue::get_mailing_queue_by_id( $mailing_queue_id );
if ( ! empty( $mailing_queue ) ) {
$queue_start_at = $mailing_queue['start_at'];
$current_timestamp = time();
$sending_timestamp = strtotime( $queue_start_at );
// Check if campaign sending time has come.
if ( ! empty( $sending_timestamp ) && $sending_timestamp <= $current_timestamp ) {
$request_args = array(
'action' => 'ig_es_trigger_mailing_queue_sending',
'campaign_hash' => $mailing_queue_hash,
);
// Send an asynchronous request to trigger sending of campaign emails.
IG_ES_Background_Process_Helper::send_async_ajax_request( $request_args, true );
}
}
}
public static function is_using_new_category_format( $campaign_id ) {
$new_flow_campaign_ids = get_option( 'ig_es_new_category_format_campaign_ids', array() );
$using_new_category_format = false;
if ( empty( $campaign_id ) || in_array( (int) $campaign_id, $new_flow_campaign_ids, true ) ) {
$using_new_category_format = true;
}
return $using_new_category_format;
}
public static function add_to_new_category_format_campaign_ids( $campaign_id ) {
$new_flow_campaign_ids = get_option( 'ig_es_new_category_format_campaign_ids', array() );
$new_flow_campaign_ids[] = $campaign_id;
update_option( 'ig_es_new_category_format_campaign_ids', $new_flow_campaign_ids, false );
}
/**
* Method to handle campaign status change
*
* @return string JSON response of the request
*
* @since 4.4.4
*/
public static function toggle_status( $args ) {
$campaign_ids = $args['campaign_ids'];
$new_status = $args['new_status'];
if ( ! empty( $campaign_ids ) ) {
$status_updated = ES()->campaigns_db->update_status( $campaign_ids, $new_status );
return $status_updated;
}
return false;
}
/**
* Send Test Email
*
* @since 4.0.0
* @since 4.3.2 Call ES()->mailer->send_test_email() method to send test email
*/
public static function send_test_email( $campaign_data) {
check_ajax_referer( 'ig-es-admin-ajax-nonce', 'security' );
$response = array();
$email = $campaign_data['es_test_email'];
$campaign_id = $campaign_data['id'];
$campaign_type = $campaign_data['type'];
$template_id = $campaign_data['base_template_id'];
$subject = $campaign_data['subject'];
$content = $campaign_data['body'];
$attachments = $campaign_data['meta']['attachments'];
$preheader = $campaign_data['meta']['pre_header'];
if ( ! empty( $email ) ) {
$merge_tags = array( 'attachments' => $attachments );
if ( ! empty( $campaign_id ) ) {
$campaign_data = array(
'id' => $campaign_id,
'type' => $campaign_type,
'base_template_id' => $template_id,
'subject' => $subject,
'body' => $content,
);
if ( IG_CAMPAIGN_TYPE_POST_NOTIFICATION === $campaign_type ) {
$campaign_data = ES_Campaign_Admin::replace_post_notification_merge_tags_with_sample_post( $campaign_data );
} elseif ( IG_CAMPAIGN_TYPE_POST_DIGEST === $campaign_type ) {
$campaign_data = ES_Campaign_Admin::replace_post_digest_merge_tags_with_sample_posts( $campaign_data );
}
$merge_tags['campaign_id'] = $campaign_id;
$merge_tags['preheader'] = $preheader;
$subject = $campaign_data['subject'];
$content = $campaign_data['body'];
}
$content = ES_Common::es_process_template_body( $content, $template_id, $campaign_id );
$response = ES()->mailer->send_test_email( $email, $subject, $content, $merge_tags );
if ( $response && 'SUCCESS' === $response['status'] ) {
$response['message'] = __( 'Email has been sent. Please check your inbox', 'email-subscribers' );
} else {
$can_promote_ess = ES_Service_Email_Sending::can_promote_ess();
if ( $can_promote_ess ) {
$promotion_message_html = ES_Service_Email_Sending::get_ess_promotion_message_html();
if ( is_array( $response['message'] ) ) {
$response['message'][] = $promotion_message_html;
} else {
$response['message'] .= $promotion_message_html;
}
}
}
}
echo json_encode( $response );
exit;
}
public static function get_posts_block_preview ( $data ) {
$postsBlockContent = ! empty( $data['postsBlockContent'] ) ? $data['postsBlockContent'] : '';
$postsBlockSetting = ! empty( $data['postsBlockSetting'] ) ? $data['postsBlockSetting'] : '';
$response = array();
if ( ! empty( $postsBlockContent ) ) {
$postsBlockContent = str_replace( '{{post.title}}', 'Sample post', $postsBlockContent );
$response['content'] = $postsBlockContent;
}
return $response;
}
/**
* Method to get spam score
*
* @since 4.6.1
*/
public static function get_spam_score( $campaign_data) {
$response = [
'status' => 'error',
'error_message' => __('Something went wrong', 'email-subscribers'),
];
$admin_email = ES_Common::get_admin_email();
$sender_data = [
'from_name' => $campaign_data['from_name'],
'from_email' => $campaign_data['from_email'],
];
$header = self::get_email_headers($sender_data) . "\n";
if (!empty($campaign_data['subject'])) {
$header .= 'Subject: ' . $campaign_data['subject'] . "\n";
}
$header .= 'Date: ' . gmdate('r') . "\n";
$header .= 'To: ' . $admin_email . "\n";
$header .= 'Message-ID: <' . $admin_email . ">\n";
$header .= "MIME-Version: 1.0\n";
$data['email'] = $header . $campaign_data['body'];
$data['tasks'][] = 'spam-score';
$spam_score_service = new ES_Service_Spam_Score_Check();
$service_response = $spam_score_service->get_spam_score($data);
if (!empty($service_response['status']) && 'success' === $service_response['status'] ) {
$response['status'] = 'success';
$response['res'] = $service_response['data'];
}
return $response;
}
/**
* Method to get email header.
*
* @param array $sender_data .
*
* @return array $headers
*
* @since 4.6.1
*/
public static function get_email_headers( $sender_data = array()) {
$get_email_type = get_option('ig_es_email_type', true);
$site_title = get_bloginfo();
$admin_email = get_option('admin_email');
$from_name = isset($sender_data['from_name']) ? $sender_data['from_name'] : get_option('ig_es_from_name', true);
$from_email = isset($sender_data['from_email']) ? $sender_data['from_email'] : get_option('ig_es_from_email', true);
$sender_email = $from_email ? $from_email: $admin_email;
$sender_name = $from_name ? $from_name : $site_title;
$headers = [
"From: \"$sender_name\" <$sender_email>",
'Return-Path: <' . $sender_email . '>',
'Reply-To: "' . $sender_name . '" <' . $sender_email . '>',
];
$email_type_options = ['php_html_mail', 'php_plaintext_mail', 'wp_html_mail'];
if (in_array($get_email_type, $email_type_options, true)) {
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'X-Mailer: PHP' . phpversion();
}
$content_type = ( in_array($get_email_type, ['wp_html_mail', 'php_html_mail'], true) ) ? 'text/html' : 'text/plain';
$headers[] = 'Content-Type: ' . $content_type . '; charset="' . get_bloginfo('charset') . '"';
return implode("\n", $headers);
}
/**
* Method to get preview HTML for campaign
*
* @return $response
*
* @since 4.4.7
*/
public static function save_and_preview( $campaign_data ) {
$response = array();
$result = self::save( $campaign_data );
if ( ! empty( $result['campaign_id'] ) ) {
$campaign_data['id'] = $result['campaign_id'];
$template_data = array();
$template_data['content'] = ! empty( $campaign_data['body'] ) ? $campaign_data['body'] : '';
$template_data['template_id'] = ! empty( $campaign_data['template_id'] ) ? $campaign_data['template_id'] : '';
$template_data['campaign_id'] = ! empty( $campaign_data['id'] ) ? $campaign_data['id'] : 0;
$campaign_data = self::add_campaign_body_data( $campaign_data );
$response['preview_html'] = $campaign_data['body'];
$response['id'] = $campaign_data['id'];
}
return $response;
}
public static function add_campaign_body_data( $campaign_data ) {
$template_id = ! empty( $campaign_data['template_id'] ) ? $campaign_data['template_id'] : 0;
$campaign_id = ! empty( $campaign_data['id'] ) ? $campaign_data['id'] : 0;
if ( ! empty( $campaign_data['body'] ) ) {
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$useremail = $current_user->user_email;
$display_name = $current_user->display_name;
$contact_id = ES()->contacts_db->get_contact_id_by_email( $useremail );
$first_name = '';
$last_name = '';
// Use details from contacts data if present else fetch it from wp profile.
if ( ! empty( $contact_id ) ) {
$contact_data = ES()->contacts_db->get_by_id( $contact_id );
$first_name = $contact_data['first_name'];
$last_name = $contact_data['last_name'];
} elseif ( ! empty( $display_name ) ) {
$contact_details = explode( ' ', $display_name );
$first_name = $contact_details[0];
// Check if last name is set.
if ( ! empty( $contact_details[1] ) ) {
$last_name = $contact_details[1];
}
}
$campaign_body = $campaign_data['body'];
$campaign_body = ES_Common::es_process_template_body( $campaign_body, $template_id, $campaign_id );
$campaign_body = ES_Common::replace_keywords_with_fallback( $campaign_body, array(
'FIRSTNAME' => $first_name,
'NAME' => $username,
'LASTNAME' => $last_name,
'EMAIL' => $useremail
) );
$subscriber_tags = array(
'subscriber.first_name' => $first_name,
'subscriber.name' => $username,
'subscriber.last_name' => $last_name,
'subscriber.email' => $useremail
);
$custom_field_values = array();
foreach ( $contact_data as $merge_tag_key => $merge_tag_value ) {
if ( false !== strpos( $merge_tag_key, 'cf_' ) ) {
$merge_tag_key_parts = explode( '_', $merge_tag_key );
$merge_tag_key = $merge_tag_key_parts[2];
if ( is_null( $merge_tag_value ) ) {
$merge_tag_value = '';
}
$custom_field_values[ 'subscriber.' . $merge_tag_key ] = $merge_tag_value;
}
}
$subscriber_tags_values = array(
'subscriber.first_name' => $first_name,
'subscriber.name' => $username,
'subscriber.last_name' => $last_name,
'subscriber.email' => $useremail
);
$subscriber_tags_values = array_merge( $subscriber_tags_values, $custom_field_values );
$campaign_body = ES_Common::replace_keywords_with_fallback( $campaign_body, $subscriber_tags_values );
$campaign_type = $campaign_data['type'];
$campaign_data['body'] = $campaign_body;
if ( IG_CAMPAIGN_TYPE_POST_NOTIFICATION === $campaign_type ) {
$campaign_data = self::replace_post_notification_merge_tags_with_sample_post( $campaign_data );
} elseif ( IG_CAMPAIGN_TYPE_POST_DIGEST === $campaign_type ) {
$campaign_data = self::replace_post_digest_merge_tags_with_sample_posts( $campaign_data );
}
$campaign_body = ! empty( $campaign_data['body'] ) ? $campaign_data['body'] : '';
// If there are blocks in this content, we shouldn't run wpautop() on it.
$priority = has_filter( 'the_content', 'wpautop' );
if ( false !== $priority ) {
// Remove wpautop to avoid p tags.
remove_filter( 'the_content', 'wpautop', $priority );
}
$campaign_body = apply_filters( 'the_content', $campaign_body );
$campaign_data['body'] = $campaign_body;
}
return $campaign_data;
}
public static function replace_post_notification_merge_tags_with_sample_post( $campaign_data ) {
if ( ! empty( $campaign_data['id'] ) ) {
$args = array(
'numberposts' => '1',
'order' => 'DESC',
'post_status' => 'publish',
);
$recent_posts = wp_get_recent_posts( $args, OBJECT );
if ( count( $recent_posts ) > 0 ) {
$post = array_shift( $recent_posts );
$post_id = $post->ID;
$template_id = $campaign_data['id'];
$campaign_body = ! empty( $campaign_data['body'] ) ? $campaign_data['body'] : '';
$campaign_subject = ! empty( $campaign_data['subject'] ) ? $campaign_data['subject'] : '';
$campaign_subject = ES_Handle_Post_Notification::prepare_subject( $campaign_subject, $post );
if ( ES_Common::contains_posts_block( $campaign_body ) ) {
$campaign_body = ES_Common::replace_single_posts_block( $campaign_body, array( $post_id ) );
} else {
$campaign_body = ES_Handle_Post_Notification::prepare_body( $campaign_body, $post_id, $template_id );
}
$campaign_data['subject'] = $campaign_subject;
$campaign_data['body'] = $campaign_body;
}
}
return $campaign_data;
}
public static function replace_post_digest_merge_tags_with_sample_posts( $campaign_data ) {
if ( ! empty( $campaign_data['id'] ) && class_exists( 'ES_Post_Digest' ) ) {
$ignore_stored_post_ids = true;
$ignore_last_run = true;
$campaign_id = $campaign_data['id'];
$campaign_body = $campaign_data['body'];
if ( ES_Common::contains_posts_block( $campaign_body ) ) {
$campaign_post_ids = ES_Post_Digest::get_post_block_matching_post_ids( $campaign_id, $ignore_stored_post_ids, $ignore_last_run );
$campaign_body = ES_Common::replace_posts_blocks( $campaign_body, $campaign_post_ids );
} else {
$post_ids = ES_Post_Digest::get_matching_post_ids( $campaign_id, $ignore_stored_post_ids, $ignore_last_run );
$campaign_body = ES_Post_Digest::process_post_digest_template( $campaign_body, $post_ids );
}
$campaign_data['body'] = $campaign_body;
}
return $campaign_data;
}
public static function is_post_campaign( $campaign_type ) {
return in_array( $campaign_type, array( IG_CAMPAIGN_TYPE_POST_NOTIFICATION, IG_CAMPAIGN_TYPE_POST_DIGEST ), true );
}
}
}
ES_Campaign_Controller::get_instance();

View File

@@ -0,0 +1,226 @@
<?php
if ( ! class_exists( 'ES_Campaigns_Controller' ) ) {
/**
* Class to handle single campaign options
*
* @class ES_Campaigns_Controller
*/
class ES_Campaigns_Controller {
// class instance
public static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public static function get_campaigns_and_kpis( $args ) {
$campaigns = self::get_campaigns( $args );
$kpis = self::get_kpis( $args );
return array(
'campaigns' => $campaigns,
'kpis' => $kpis,
);
}
public static function get_campaigns_count( $args) {
$campaigns_table = ES_Campaigns_Table::get_instance();
$per_page = $campaigns_table->get_items_per_page(ES_Campaigns_Table::$option_per_page, 20);
$total_items = $campaigns_table->get_lists(0, 0, true);
$total_campaign_pages = ceil($total_items / $per_page);
$current_page=$args['current_page'];
return [$total_items,$total_campaign_pages,$current_page,$per_page];
}
public static function get_campaigns( $args ) {
$campaigns_table = ES_Campaigns_Table::get_instance();
$per_page = $campaigns_table->get_items_per_page(ES_Campaigns_Table::$option_per_page, 20);
$current_page = ! empty( $args['current_page'] ) ? $args['current_page'] : 1;
$total_items = $campaigns_table->get_lists(0, 0, true);
$offset = ( $current_page - 1 ) * $per_page;
$args['offset']=$offset;
$args['per_page']=$per_page;
$args['is_campaigns_listing']='is_campaigns_listing';
$campaigns = ES()->campaigns_db->get_campaigns($args);
if (!empty($campaigns)) {
foreach ($campaigns as $index => $campaign) {
$formatted_campaign = self::format_campaign_data($campaign);
$campaigns[$index] = $formatted_campaign;
}
}
$campaigns['campaigns']=$campaigns;
$campaigns['current_page'] = $current_page ? $current_page:1;
return $campaigns;
}
public static function format_campaign_data( $campaign ) {
$campaigns_table = ES_Campaigns_Table::get_instance();
if ( ! empty( $campaign ) ) {
$campaign['es_admin_email'] = ES_Common::get_admin_email();
$campaign_id = $campaign['id'];
$campaign_status = (int) $campaign['status'];
$campaign_type = $campaign['type'];
if ( self::is_post_campaign( $campaign_type ) ) {
$campaign['formatted_categories'] = self::format_categories( $campaign['categories'] );
}
foreach ( $campaign as $column_name => $item ) {
if ( method_exists( $campaigns_table, 'column_' . $column_name ) ) {
$output = call_user_func( array( $campaigns_table, 'column_' . $column_name ), $campaign );
} else {
$output = $campaigns_table->column_default( $campaign, $column_name );
}
$campaign [ $column_name ] = $output;
}
$campaign['status_text'] = $campaigns_table->column_status_text( $campaign );
$campaign['meta'] = maybe_unserialize( $campaign['meta'] );
$args = array(
'campaign_id' => $campaign_id,
'types' => array(
IG_MESSAGE_SENT,
IG_MESSAGE_OPEN,
IG_LINK_CLICK
)
);
$actions_count = ES()->actions_db->get_actions_count( $args );
$total_email_sent = $actions_count['sent'];
$total_email_opened = $actions_count['opened'];
$total_email_clicked = $actions_count['clicked'];
$open_rate = ! empty( $total_email_sent ) ? number_format_i18n( ( ( $total_email_opened * 100 ) / $total_email_sent ), 2 ) : 0 ;
$click_rate = ! empty( $total_email_sent ) ? number_format_i18n( ( ( $total_email_clicked * 100 ) / $total_email_sent ), 2 ) : 0;
$campaign['open_rate'] = $open_rate;
$campaign['click_rate'] = $click_rate;
$campaign['meta'] = maybe_unserialize($campaign['meta']);
if ( self::is_post_campaign( $campaign_type ) ) {
$report = ES_DB_Mailing_Queue::get_notification_by_campaign_id( $campaign_id );
if ( $report ) {
$campaign['report_link'] = admin_url( 'admin.php?page=' . esc_attr( 'es_reports' ) . '&campaign_id=' . esc_attr( $campaign_id ) );
}
} elseif ( IG_CAMPAIGN_TYPE_NEWSLETTER === $campaign_type ) {
$broadcast_allowed_report_statuses = array(
IG_ES_CAMPAIGN_STATUS_SCHEDULED,
IG_ES_CAMPAIGN_STATUS_QUEUED,
IG_ES_CAMPAIGN_STATUS_ACTIVE,
IG_ES_CAMPAIGN_STATUS_FINISHED,
IG_ES_CAMPAIGN_STATUS_PAUSED,
);
if ( in_array( $campaign_status, $broadcast_allowed_report_statuses, true ) ) {
$report = ES_DB_Mailing_Queue::get_notification_by_campaign_id( $campaign_id );
if ( $report ) {
$campaign['report_link'] = admin_url( 'admin.php?page=' . esc_attr( 'es_reports' ) . '&action=view&list=' . $report['hash'] );
$campaign['start_at'] = ig_es_format_date_time( $report['start_at'] );
}
}
} else {
$campaign['report_link'] = admin_url( 'admin.php?page=' . esc_attr( 'es_reports' ) . '&campaign_id=' . $campaign_id );
}
}
return $campaign;
}
public static function get_kpis( $args ) {
$page = 'es_campaigns';
$override_cache = true;
$reports_data = ES_Reports_Data::get_dashboard_reports_data( $page, $override_cache, $args );
return $reports_data;
}
public static function delete_campaigns( $args ) {
$campaign_ids = $args['campaign_ids'];
if ( ! empty( $campaign_ids ) ) {
return ES()->campaigns_db->delete_campaigns( $campaign_ids );
}
return false;
}
/**
* Method to Duplicate broadcast content
*
* @return void
*
* @since 4.6.3
*/
public static function duplicate_campaign( $campaign_id = 0 ) {
if ( empty( $campaign_id ) ) {
return false;
}
$duplicated_campaign_id = ES()->campaigns_db->duplicate_campaign( $campaign_id );
if ( empty( $duplicated_campaign_id ) ) {
return false;
}
$duplicated_campaign = ES()->campaigns_db->get( $duplicated_campaign_id );
if ( empty( $duplicated_campaign ) ) {
return false;
}
$duplicated_campaign = self::format_campaign_data( $duplicated_campaign );
return $duplicated_campaign;
}
public static function format_categories( $categories ) {
$categories = explode( '##', trim( trim( $categories, '##' ) ) );
$formatted_categories = array();
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
if ( ! empty( $category ) ) {
$post_categories = explode( '|', $category );
foreach ( $post_categories as $post_category ) {
list( $post_type, $categories_list ) = explode( ':', $post_category );
if ( 'none' !== $categories_list && 'all' !== $categories_list ) {
$categories_list = array_map( 'absint', explode( ',', $categories_list ) );
}
$formatted_categories[$post_type] = $categories_list;
}
}
}
}
return $formatted_categories;
}
public static function is_post_campaign( $campaign_type ) {
return in_array( $campaign_type, array( IG_CAMPAIGN_TYPE_POST_NOTIFICATION, IG_CAMPAIGN_TYPE_POST_DIGEST ), true );
}
public static function paginate_campaigns() {
$campaigns_table = ES_Campaigns_Table::get_instance();
$per_page = $campaigns_table->get_items_per_page( ES_Campaigns_Table::$option_per_page, 25 );
$current_page = $campaigns_table->get_pagenum();
$total_items = $campaigns_table->get_lists( 0, 0, true );
$campaigns_table->set_pagination_args(
array(
'total_items' => $total_items, // We have to calculate the total number of items
'per_page' => $per_page, // We have to determine how many items to show on a page
)
);
$campaigns = $campaigns_table->get_lists( $per_page, $current_page );
if ( ! empty( $campaigns ) ) {
foreach ( $campaigns as $index => $campaign ) {
$campaign = self::format_campaign_data( $campaign );
$campaigns[ $index ] = $campaign;
}
}
return $campaigns;
}
}
}
ES_Campaigns_Controller::get_instance();

View File

@@ -0,0 +1,548 @@
<?php
if ( ! class_exists( 'ES_Gallery_Controller' ) ) {
/**
* Class to handle single campaign options
*
* @class ES_Gallery_Controller
*/
class ES_Gallery_Controller {
// class instance
public static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Get campaign templates
*/
public static function get_gallery_items() {
$response = array();
$gallery_items = array();
$blog_charset = get_option( 'blog_charset' );
$campaign_templates = ES_Common::get_templates();
if ( !empty( $campaign_templates ) ) {
foreach ( $campaign_templates as $campaign_template) {
$template_slug = $campaign_template->post_name;
$editor_type = get_post_meta( $campaign_template->ID, 'es_editor_type', true );
$categories = array();
$gallery_item['ID'] = $campaign_template->ID;
$gallery_item['title'] = html_entity_decode( $campaign_template->post_title, ENT_QUOTES, $blog_charset );
$gallery_item['type'] = get_post_meta( $campaign_template->ID, 'es_template_type', true );
$gallery_item['editor_type'] = !empty($editor_type) ? $editor_type : IG_ES_CLASSIC_EDITOR;
$gallery_type = 'local';
$categories[] = !empty($gallery_item['type']) ? $gallery_item['type'] : IG_CAMPAIGN_TYPE_NEWSLETTER;
$categories[] = !empty($editor_type) ? $editor_type : IG_ES_CLASSIC_EDITOR;
$gallery_item['categories'] = $categories;
$thumbnail_url = ( ! empty( $campaign_template->ID ) ) ? get_the_post_thumbnail_url(
$campaign_template->ID,
array(
'200',
'200',
) ): '';
$gallery_item['thumbnail'] = ( !empty ($thumbnail_url) ) ? $thumbnail_url : '';
$gallery_item['gallery_type'] = $gallery_type;
$gallery_items[$template_slug] = $gallery_item;
}
}
$remote_gallery_items = self::get_remote_gallery_items();
if ( ! empty( $remote_gallery_items ) ) {
foreach ( $remote_gallery_items as $item ) {
$template_version = $item->template_version;
if ( in_array( $template_version, array('1.0.0', '1.0.1') ) ) {
$template_slug = $item->slug;
// Don't add remote template if local template with same slug already exists. This is to avoid duplicates.
if ( isset( $gallery_items[ $template_slug ] ) ) {
continue;
}
$item_id = $item->id;
$item_title = $item->title->rendered;
$item_title = html_entity_decode( $item_title, ENT_QUOTES, $blog_charset );
$thumbnail_url = ! empty( $item->thumbnail->guid ) ? $item->thumbnail->guid : '';
$editor_type = ! empty( $item->es_editor_type ) ? $item->es_editor_type : IG_ES_CLASSIC_EDITOR;
$campaign_type = ! empty( $item->es_template_type ) ? $item->es_template_type : IG_CAMPAIGN_TYPE_NEWSLETTER;
$es_plan = ! empty( $item->es_plan ) ? $item->es_plan : 'lite';
$gallery_type = 'remote';
$template_version = ! empty( $item->template_version ) ? $item->template_version : '1.0.0';
$categories = array(
$campaign_type,
$editor_type
);
if ( 'lite' !== $es_plan ) {
$categories[] = $es_plan;
}
$gallery_items[$template_slug] = array(
'ID' => $item_id,
'title' => $item_title,
'thumbnail' => $thumbnail_url,
'categories' => $categories,
'type' => $campaign_type,
'editor_type' => $editor_type,
'gallery_type' => 'remote',
'es_plan' => $es_plan,
'template_version' => $template_version,
);
}
}
}
$response['items'] = array_values( $gallery_items );
wp_send_json_success( $response );
}
public static function get_remote_gallery_items() {
$remote_gallery_items_updated = get_transient( 'ig_es_remote_gallery_items_updated' );
if ( ! $remote_gallery_items_updated ) {
$remote_gallery_items_url = 'https://icegram.com/gallery/wp-json/wp/v2/es_gallery_item?filter[posts_per_page]=200';
$response = wp_remote_get( $remote_gallery_items_url );
if ( ! is_wp_error( $response ) ) {
$json_response = wp_remote_retrieve_body( $response );
if ( ! empty( $json_response ) && ES_Common::is_valid_json( $json_response ) ) {
$gallery_items = json_decode( $json_response );
if ( is_array( $gallery_items ) ) {
$updated = update_option( 'ig_es_remote_gallery_items', $gallery_items, 'no' );
if ( $updated ) {
set_transient( 'ig_es_remote_gallery_items_updated', time(), 24 * HOUR_IN_SECONDS ); // 1 day
}
return $gallery_items;
}
}
}
}
$remote_gallery_items = get_option( 'ig_es_remote_gallery_items', array() );
return $remote_gallery_items;
}
public static function get_remote_gallery_item( $item_id ) {
$gallery_item = array();
if ( empty( $item_id ) ) {
return $gallery_item;
}
$remote_gallery_item_url = 'https://icegram.com/gallery/wp-json/wp/v2/es_gallery_item/' . $item_id;
$response = wp_remote_get( $remote_gallery_item_url );
if ( ! is_wp_error( $response ) ) {
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$json_response = wp_remote_retrieve_body( $response );
if ( ! empty( $json_response ) && ES_Common::is_valid_json( $json_response ) ) {
$gallery_item = json_decode( $json_response );
}
}
}
return $gallery_item;
}
public static function import_gallery_item_handler( $gallery_type, $template_id, $campaign_type, $campaign_id = 0 ) {
if ( 'remote' === $gallery_type ) {
$campaign_id = self::import_remote_gallery_item( $template_id, $campaign_type, $campaign_id );
} else {
$campaign_id = self::import_local_gallery_item( $template_id, $campaign_type, $campaign_id );
}
return $campaign_id;
}
public static function import_local_gallery_item( $template_id, $campaign_type, $campaign_id = 0 ) {
if ( ! empty( $template_id ) ) {
$template = get_post( $template_id );
if ( ! empty( $template ) ) {
$subject = $template->post_title;
$content = $template->post_content;
$from_email = ES_Common::get_ig_option( 'from_email' );
$from_name = ES_Common::get_ig_option( 'from_name' );
$editor_type = get_post_meta( $template_id, 'es_editor_type', true );
if ( empty( $editor_type ) ) {
$editor_type = IG_ES_CLASSIC_EDITOR;
}
$campaign_meta = array(
'editor_type' => $editor_type,
);
if ( IG_ES_DRAG_AND_DROP_EDITOR === $editor_type ) {
$dnd_editor_data = get_post_meta( $template_id, 'es_dnd_editor_data', true );
if ( ! empty( $dnd_editor_data ) ) {
$campaign_meta['dnd_editor_data'] = wp_json_encode( $dnd_editor_data );
}
} else {
if ( false === strpos( $content, '<html' ) ) {
// In classic edior, we need to add p tag to content when not already added.
$content = wpautop( $content );
}
$custom_css = get_post_meta( $template_id, 'es_custom_css', true );
if ( ! empty( $custom_css ) ) {
$campaign_meta['es_custom_css'] = $custom_css;
}
}
$campaign_meta = maybe_serialize( $campaign_meta );
$campaign_data = array(
'name' => $subject,
'subject' => $subject,
'slug' => sanitize_title( sanitize_text_field( $subject ) ),
'body' => $content,
'from_name' => $from_name,
'from_email' => $from_email,
'type' => $campaign_type,
'meta' => $campaign_meta,
);
if ( ! empty( $campaign_id ) ) {
ES()->campaigns_db->update( $campaign_id, $campaign_data );
} else {
$campaign_id = ES()->campaigns_db->save_campaign( $campaign_data );
if ( in_array( $campaign_type, array( IG_CAMPAIGN_TYPE_POST_NOTIFICATION, IG_CAMPAIGN_TYPE_POST_DIGEST ), true ) ) {
ES_Campaign_Controller::add_to_new_category_format_campaign_ids( $campaign_id );
}
}
}
}
return $campaign_id;
}
public static function import_remote_gallery_item( $template_id, $campaign_type, $campaign_id = 0 ) {
$gallery_item = self::get_remote_gallery_item( $template_id );
if ( empty( $gallery_item ) ) {
return $campaign_id;
}
$template_version = ! empty( $gallery_item->template_version ) ? $gallery_item->template_version : '';
if ( in_array( $template_version, array('1.0.0', '1.0.1') ) ) {
$subject = $gallery_item->title->rendered;
$content = $gallery_item->content->rendered;
$from_email = ES_Common::get_ig_option( 'from_email' );
$from_name = ES_Common::get_ig_option( 'from_name' );
$editor_type = ! empty( $gallery_item->es_editor_type ) ? $gallery_item->es_editor_type : IG_ES_CLASSIC_EDITOR;
$campaign_meta = array(
'editor_type' => $editor_type,
);
if ( IG_ES_DRAG_AND_DROP_EDITOR === $editor_type ) {
$dnd_editor_data = maybe_unserialize( $gallery_item->es_dnd_editor_data );
if ( ! empty( $dnd_editor_data ) ) {
$campaign_meta['dnd_editor_data'] = $gallery_item->es_dnd_editor_data;
}
} else {
if ( false === strpos( $content, '<html' ) ) {
// In classic edior, we need to add p tag to content when not already added.
$content = wpautop( $content );
}
$custom_css = ! empty( $gallery_item->es_custom_css ) ? $gallery_item->es_custom_css : '';
if ( ! empty( $custom_css ) ) {
$campaign_meta['es_custom_css'] = $custom_css;
}
}
$campaign_meta = maybe_serialize( $campaign_meta );
preg_match_all( '#<img\s+(?:[^>]*?\s+)?src=(\'|")?(https?[^\'"]+)(\'|")?#', $content, $image_urls );
$image_urls = ! empty( $image_urls[2] ) ? $image_urls[2] : array();
if ( ! empty( $image_urls ) ) {
foreach ( $image_urls as $image_url ) {
$is_ig_image_link = false !== strpos( $image_url , 'icegram.com' );
if ( $is_ig_image_link ) {
$new_image_url = ES_Common::download_image_from_url( $image_url );
if ( ! empty( $new_image_url ) ) {
$old_url = ' src="' . $image_url . '"';
$new_url = ' src="' . $new_image_url . '"';
$pos = strpos( $content, $old_url );
if ( false !== $pos ) {
$content = preg_replace( '/' . preg_quote( $old_url, '/' ) . '/', $new_url, $content, 1 );
}
}
}
}
}
$campaign_data = array(
'name' => $subject,
'subject' => $subject,
'slug' => sanitize_title( sanitize_text_field( $subject ) ),
'body' => $content,
'from_name' => $from_name,
'from_email' => $from_email,
'type' => $campaign_type,
'meta' => $campaign_meta,
);
if ( ! empty( $campaign_id ) ) {
ES()->campaigns_db->update( $campaign_id, $campaign_data );
} else {
$campaign_id = ES()->campaigns_db->save_campaign( $campaign_data );
if ( ! empty( $campaign_id ) ) {
$imported_gallery_template_ids = get_option( 'ig_es_imported_remote_gallery_template_ids', array() );
$imported_gallery_template_ids[] = $template_id;
update_option( 'ig_es_imported_remote_gallery_template_ids', $imported_gallery_template_ids );
if ( in_array( $campaign_type, array( IG_CAMPAIGN_TYPE_POST_NOTIFICATION, IG_CAMPAIGN_TYPE_POST_DIGEST ), true ) ) {
ES_Campaign_Controller::add_to_new_category_format_campaign_ids( $campaign_id );
}
}
}
}
return $campaign_id;
}
public static function import_remote_gallery_template( $template_id ) {
$imported_template_id = 0;
$gallery_item = self::get_remote_gallery_item( $template_id );
if ( empty( $gallery_item ) ) {
return $imported_template_id;
}
$template_version = ! empty( $gallery_item->template_version ) ? $gallery_item->template_version : '';
if ( in_array( $template_version, array('1.0.0', '1.0.1') ) ) {
$subject = $gallery_item->title->rendered;
$content = $gallery_item->content->rendered;
$editor_type = ! empty( $gallery_item->es_editor_type ) ? $gallery_item->es_editor_type : IG_ES_CLASSIC_EDITOR;
$template_type = ! empty( $gallery_item->es_template_type ) ? $gallery_item->es_template_type : IG_CAMPAIGN_TYPE_NEWSLETTER;
$campaign_meta = array(
'es_editor_type' => $editor_type,
);
if ( IG_ES_DRAG_AND_DROP_EDITOR === $editor_type ) {
$dnd_editor_data = maybe_unserialize( $gallery_item->es_dnd_editor_data );
if ( ! empty( $dnd_editor_data ) ) {
$campaign_meta['es_dnd_editor_data'] = $gallery_item->es_dnd_editor_data;
}
} else {
if ( false === strpos( $content, '<html' ) ) {
// In classic edior, we need to add p tag to content when not already added.
$content = wpautop( $content );
}
$custom_css = ! empty( $gallery_item->es_custom_css ) ? $gallery_item->es_custom_css : '';
if ( ! empty( $custom_css ) ) {
$campaign_meta['es_custom_css'] = $custom_css;
}
}
preg_match_all( '#<img\s+(?:[^>]*?\s+)?src=(\'|")?(https?[^\'"]+)(\'|")?#', $content, $image_urls );
$image_urls = ! empty( $image_urls[2] ) ? $image_urls[2] : array();
if ( ! empty( $image_urls ) ) {
foreach ( $image_urls as $image_url ) {
$is_ig_image_link = false !== strpos( $image_url , 'icegram.com' );
if ( $is_ig_image_link ) {
$new_image_url = ES_Common::download_image_from_url( $image_url );
if ( ! empty( $new_image_url ) ) {
$old_url = ' src="' . $image_url . '"';
$new_url = ' src="' . $new_image_url . '"';
$pos = strpos( $content, $old_url );
if ( false !== $pos ) {
$content = preg_replace( '/' . preg_quote( $old_url, '/' ) . '/', $new_url, $content, 1 );
}
}
}
}
}
$template_data = array(
'post_title' => $subject,
'post_content' => $content,
'post_type' => 'es_template',
'post_status' => 'draft',
);
$imported_template_id = wp_insert_post( $template_data );
$is_template_added = ! ( $imported_template_id instanceof WP_Error );
if ( $is_template_added ) {
$editor_type = ! empty( $campaign_meta['es_editor_type'] ) ? $campaign_meta['es_editor_type'] : '';
$is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type;
if ( $is_dnd_editor ) {
$dnd_editor_data = array();
if ( ! empty( $campaign_meta['es_dnd_editor_data'] ) ) {
$dnd_editor_data = $campaign_meta['es_dnd_editor_data'];
$dnd_editor_data = json_decode( $dnd_editor_data );
update_post_meta( $imported_template_id, 'es_dnd_editor_data', $dnd_editor_data );
}
} else {
$custom_css = ! empty( $campaign_meta['es_custom_css'] ) ? $campaign_meta['es_custom_css'] : '';
update_post_meta( $imported_template_id, 'es_custom_css', $custom_css );
}
update_post_meta( $imported_template_id, 'es_editor_type', $editor_type );
update_post_meta( $imported_template_id, 'es_template_type', $template_type );
}
}
return $imported_template_id;
}
public static function duplicate_template( $template_id ) {
// Get access to the database
global $wpdb;
// Get the post as an array
$duplicate = get_post( $template_id, 'ARRAY_A' );
// Modify some of the elements
$duplicate['post_title'] = $duplicate['post_title'] . ' ' . __( 'Copy', 'email-subscribers' );
$duplicate['post_status'] = 'publish';
// Set the post date
$timestamp = time();
$duplicate['post_date'] = gmdate( 'Y-m-d H:i:s', $timestamp );
// Remove some of the keys
unset( $duplicate['ID'] );
unset( $duplicate['guid'] );
unset( $duplicate['comment_count'] );
$current_user_id = get_current_user_id();
if ( ! empty( $current_user_id ) ) {
// Set post author to current logged in author.
$duplicate['post_author'] = $current_user_id;
}
// Insert the post into the database
$duplicate_id = wp_insert_post( $duplicate );
// Duplicate all taxonomies/terms
$taxonomies = get_object_taxonomies( $duplicate['post_type'] );
foreach ( $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $template_id, $taxonomy, array( 'fields' => 'names' ) );
wp_set_object_terms( $duplicate_id, $terms, $taxonomy );
}
// Duplicate all custom fields
$custom_fields = get_post_custom( $template_id );
foreach ( $custom_fields as $key => $value ) {
add_post_meta( $duplicate_id, $key, maybe_unserialize( $value[0] ) );
}
return $duplicate_id;
}
public static function preview_template( $args ) {
$template_id = $args['template_id'];
$gallery_type = $args['gallery_type'];
if ( 'remote' === $gallery_type ) {
$template = self::get_remote_gallery_item( $template_id );
$es_template_body = $template->content->rendered;
$es_template_type = $template->es_template_type;
$custom_css = $template->es_custom_css;
$es_template_body = $custom_css . $es_template_body;
} else {
$template = get_post( $template_id, ARRAY_A );
$es_template_body = $template['post_content'];
$es_template_type = get_post_meta( $template_id, 'es_template_type', true );
}
if ( $template ) {
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$useremail = $current_user->user_email;
$display_name = $current_user->display_name;
$contact_id = ES()->contacts_db->get_contact_id_by_email( $useremail );
$first_name = '';
$last_name = '';
// Use details from contacts data if present else fetch it from wp profile.
if ( ! empty( $contact_id ) ) {
$contact_data = ES()->contacts_db->get_by_id( $contact_id );
$first_name = $contact_data['first_name'];
$last_name = $contact_data['last_name'];
} elseif ( ! empty( $display_name ) ) {
$contact_details = explode( ' ', $display_name );
$first_name = $contact_details[0];
// Check if last name is set.
if ( ! empty( $contact_details[1] ) ) {
$last_name = $contact_details[1];
}
}
// Don't replace placeholder keywords in remote templates.
if ( 'remote' !== $gallery_type ) {
if ( 'post_notification' === $es_template_type ) {
$args = array(
'numberposts' => '1',
'order' => 'DESC',
'post_status' => 'publish',
);
$recent_posts = wp_get_recent_posts( $args );
if ( count( $recent_posts ) > 0 ) {
$recent_post = array_shift( $recent_posts );
$post_id = $recent_post['ID'];
$es_template_body = ES_Handle_Post_Notification::prepare_body( $es_template_body, $post_id, $template_id );
}
} else {
$es_template_body = ES_Common::es_process_template_body( $es_template_body, $template_id );
}
}
$es_template_body = ES_Common::replace_keywords_with_fallback( $es_template_body, array(
'FIRSTNAME' => $first_name,
'NAME' => $username,
'LASTNAME' => $last_name,
'EMAIL' => $useremail
) );
$es_template_body = ES_Common::replace_keywords_with_fallback( $es_template_body, array(
'subscriber.first_name' => $first_name,
'subscriber.name' => $username,
'subscriber.last_name' => $last_name,
'subscriber.email' => $useremail
) );
add_filter( 'safe_style_css', 'ig_es_allowed_css_style' );
$response['template_html'] = apply_filters( 'the_content', $es_template_body );
} else {
$response['template_html'] = __( 'Please publish it or save it as a draft.', 'email-subscribers' );
}
return $response;
}
public static function delete_template( $args ) {
$template_id = $args['template_id'];
if ( ! empty( $template_id ) ) {
$deleted = wp_delete_post( $template_id );
if ( $deleted ) {
return true;
}
}
return false;
}
}
}
ES_Gallery_Controller::get_instance();

View File

@@ -0,0 +1,108 @@
<?php
if ( ! class_exists( 'ES_Template_Controller' ) ) {
/**
* Class to handle single campaign options
*
* @class ES_Template_Controller
*/
class ES_Template_Controller {
// class instance
public static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public static function get_template( $data ) {
$response = array();
$template_id = $data['templateId'];
if ( ! empty( $template_id ) ) {
$template = get_post( $template_id );
if ( $template ) {
$template_meta = get_post_custom( $template_id );
if ( ! empty( $template_meta ) ) {
foreach ( $template_meta as $meta_key => $meta_value ) {
if ( 'es_dnd_editor_data' === $meta_key ) {
$template_meta[ $meta_key ] = wp_json_encode( maybe_unserialize( $meta_value[0] ) );
} else {
$template_meta[ $meta_key ] = $meta_value[0];
}
}
}
if ( empty( $template_meta['es_editor_type'] ) ) {
$template_meta['es_editor_type'] = IG_ES_CLASSIC_EDITOR;
}
$response['id'] = $template->ID;
$response['subject'] = $template->post_title;
$response['body'] = $template->post_content;
$response['meta'] = $template_meta;
}
}
return $response;
}
public static function save( $template_data ) {
$response = array();
$template_id = ! empty( $template_data['id'] ) ? $template_data['id'] : 0;
$template_type = ! empty( $template_data['meta']['es_template_type'] ) ? $template_data['meta']['es_template_type'] : IG_CAMPAIGN_TYPE_NEWSLETTER;
$template_body = ! empty( $template_data['body'] ) ? $template_data['body'] : '';
$template_subject = ! empty( $template_data['subject'] ) ? $template_data['subject'] : '';
$template_attachment_id = ! empty( $template_data['template_attachment_id'] ) ? $template_data['template_attachment_id'] : '';
$template_status = 'publish';
$data = array(
'post_title' => $template_subject,
'post_content' => $template_body,
'post_type' => 'es_template',
'post_status' => $template_status,
);
if ( empty( $template_id ) ) {
$template_id = wp_insert_post( $data );
} else {
$data['ID'] = $template_id;
$template_id = wp_update_post( $data );
}
$is_template_added = ! ( $template_id instanceof WP_Error );
if ( $is_template_added ) {
$response['templateId'] = $template_id;
if ( ! empty( $template_attachment_id ) ) {
set_post_thumbnail( $template_id, $template_attachment_id );
}
$editor_type = ! empty( $template_data['meta']['es_editor_type'] ) ? $template_data['meta']['es_editor_type'] : '';
$is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type;
if ( $is_dnd_editor ) {
$dnd_editor_data = array();
if ( ! empty( $template_data['meta']['es_dnd_editor_data'] ) ) {
$dnd_editor_data = $template_data['meta']['es_dnd_editor_data'];
$dnd_editor_data = json_decode( $dnd_editor_data );
update_post_meta( $template_id, 'es_dnd_editor_data', $dnd_editor_data );
}
} else {
$custom_css = ! empty( $template_data['meta']['es_custom_css'] ) ? $template_data['meta']['es_custom_css'] : '';
update_post_meta( $template_id, 'es_custom_css', $custom_css );
}
update_post_meta( $template_id, 'es_editor_type', $editor_type );
update_post_meta( $template_id, 'es_template_type', $template_type );
}
return $response;
}
}
}
ES_Template_Controller::get_instance();