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_before_' . IG_CAMPAIGN_TYPE_POST_NOTIFICATION . '_content_settings', array( $this, 'show_save_as_template' ) ); add_action( 'ig_es_before_' . IG_CAMPAIGN_TYPE_POST_DIGEST . '_content_settings', array( $this, 'show_save_as_template' ) ); add_action( 'ig_es_before_' . IG_CAMPAIGN_TYPE_NEWSLETTER . '_content_settings', array( $this, 'show_save_as_template' ) ); // preview popup add_action( 'ig_es_campaign_preview_options_content', array( $this, 'show_campaign_preview_options_content' ) ); add_action( 'wp_ajax_ig_es_draft_campaign', array( $this, 'draft_campaign' ) ); add_action( 'wp_ajax_ig_es_get_campaign_preview', array( $this, 'get_campaign_preview' ) ); add_action( 'wp_ajax_ig_es_save_as_template', array( $this, 'save_as_template' ) ); add_action( 'media_buttons', array( $this, 'add_tag_button' ) ); } public function setup() { $campaign_id = $this->get_campaign_id_from_url(); if ( ! empty( $campaign_id ) ) { $campaign = new ES_Campaign( $campaign_id ); if ( $campaign->exists ) { $this->campaign_data = (array) $campaign; if ( empty( $this->campaign_data['meta']['editor_type'] ) ) { $this->campaign_data['meta']['editor_type'] = IG_ES_CLASSIC_EDITOR; } } } else { if ( empty( $this->campaign_data ) ) { $this->campaign_data['type'] = $this->get_campaign_type_from_url(); $this->campaign_data['meta']['editor_type'] = $this->get_editor_type_from_url(); } } } public function get_campaign_id_from_url() { $campaign_id = ig_es_get_request_data( 'id' ); return $campaign_id; } public function get_campaign_type_from_url() { $current_page = ig_es_get_request_data( 'page' ); $campaign_type = ''; if ( 'es_newsletters' === $current_page ) { $campaign_type = IG_CAMPAIGN_TYPE_NEWSLETTER; } elseif ( 'es_notifications' === $current_page ) { $campaign_type = IG_CAMPAIGN_TYPE_POST_NOTIFICATION; } return $campaign_type; } public function get_editor_type_from_url() { $editor_type = ig_es_get_request_data( 'editor-type' ); if ( empty( $editor_type ) ) { $editor_type = IG_ES_DRAG_AND_DROP_EDITOR; } return $editor_type; } public static function set_screen( $status, $option, $value ) { return $value; } /** * Method to process campaign submission. * * @since 4.4.7 * * @modify 5.6.4 */ public function process_submission() { $campaign_action = ig_es_get_request_data( 'ig_es_campaign_action' ); if ( ! empty( $campaign_action ) ) { $campaign_nonce = ig_es_get_request_data( 'ig_es_campaign_nonce' ); // Verify nonce. if ( wp_verify_nonce( $campaign_nonce, 'ig-es-campaign-nonce' ) ) { $campaign_data = ig_es_get_request_data( 'data', array(), false ); $list_id = ! empty( $campaign_data['list_ids'] ) ? $campaign_data['list_ids'] : ''; $template_id = ! empty( $campaign_data['template_id'] ) ? $campaign_data['template_id'] : ''; $subject = ! empty( $campaign_data['subject'] ) ? $campaign_data['subject'] : ''; // Check if user has added required data for creating campaign. if ( ! empty( $campaign_data['subject'] ) && ! empty( $campaign_data['body'] ) && ! empty( $subject ) ) { $campaign_data['subject'] = wp_strip_all_tags( $campaign_data['subject'] ); $is_updating_campaign = ! empty( $campaign_data['id'] ) ? true : false; $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'] ); } $meta = apply_filters( 'ig_es_before_save_campaign_meta', $meta, $campaign_data ); $campaign_data['meta'] = maybe_serialize( $meta ); if ( 'schedule' === $campaign_action ) { $campaign_data['status'] = IG_ES_CAMPAIGN_STATUS_SCHEDULED; } elseif ( 'activate' === $campaign_action ) { $campaign_data['status'] = IG_ES_CAMPAIGN_STATUS_ACTIVE; } $campaign_saved = self::save_campaign( $campaign_data ); $campaign_status = ! $is_updating_campaign && $campaign_saved ? 'campaign_created' : 'error'; $campaign_status = $is_updating_campaign && $campaign_saved ? 'campaign_updated' : 'error'; if ( 'schedule' === $campaign_action ) { if ( empty( $meta['list_conditions'] ) ) { $error_message = __( 'No recipients were found. Please add some recipients.', 'email-subscribers' ); } else { $scheduling_status = self::schedule_campaign( $campaign_data ); if ( 'success' !== $scheduling_status ) { if ( 'emails_not_queued' === $scheduling_status ) { $error_message = __( 'Campaign not scheduled. Please check if there are some recipients according to the selected campaign rules.', 'email-subscribers' ); } elseif ( '' === $scheduling_status ) { $error_message = __( 'Campaign not scheduled due to some error. Please try again later.', 'email-subscribers' ); } } } if ( ! empty( $error_message ) ) { $campaign_data['status'] = IG_ES_CAMPAIGN_STATUS_IN_ACTIVE; // Revert back camaign status to inactive(draft), if scheduling fails. self::save_campaign( $campaign_data ); $this->campaign_data = ig_es_get_request_data( 'data' ); ES_Common::show_message( $error_message, 'error' ); return; } } $campaign_url = admin_url( 'admin.php?page=es_campaigns&id=' . $campaign_data['id'] . '&action=' . $campaign_status ); wp_safe_redirect( $campaign_url ); exit(); } } else { $message = __( 'Sorry, you are not allowed to add/edit campaign.', 'email-subscribers' ); ES_Common::show_message( $message, 'error' ); return; } } } public function render() { global $wpdb; $campaign_id = ig_es_get_request_data( 'id' ); $submitted = ig_es_get_request_data( 'ig_es_campaign_submitted' ); $campaign_data = ig_es_get_request_data( 'data', array(), false ); $message_data = array(); $campaign_action = ig_es_get_request_data( 'ig_es_campaign_action' ); if ( ! empty( $campaign_action ) ) { if ( empty( $campaign_data['subject'] ) ) { $message = __( 'Please add a campaign subject.', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'error', ); } } $this->show_campaign_form( $message_data ); } /** * Add an Tag button to WP Editor * * @param string $editor_id Editor id * * @since 5.4.10 */ public function add_tag_button( $editor_id ) { if ( ! ES()->is_es_admin_screen() ) { return; } $campaign_type = isset( $this->campaign_data['type'] ) ? $this->campaign_data['type'] : ''; ?>
get_post_notification_tags(); $campaign_tags = array( 'post_notification' => $post_notification_tags, ); return apply_filters( 'ig_es_campaign_tags', $campaign_tags ); } public function get_post_notification_tags() { $post_notification_tags = array( '{{post.date}}', '{{post.title}}', '{{post.image}}', '{{post.excerpt}}', '{{post.description}}', '{{post.author}}', '{{post.link}}', '{{post.link_with_title}}', '{{post.link_only}}', '{{post.full}}', '{{post.cats}}', '{{post.more_tag}}', '{{post.image_url}}' ); return apply_filters( 'ig_es_post_notification_tags', $post_notification_tags ); } public function get_subscriber_tags() { $subscriber_tags = array( '{{subscriber.name}}', '{{subscriber.first_name}}', '{{subscriber.last_name}}', '{{subscriber.email}}', ); return apply_filters( 'ig_es_subscriber_tags', $subscriber_tags ); } public function get_site_tags() { $site_tags = array( '{{site.total_contacts}}', '{{site.url}}', '{{site.name}}', ); return apply_filters( 'ig_es_site_tags', $site_tags ); } public function get_dnd_campaign_tags() { $post_notification_tags = array( array( 'keyword' => 'post.title', 'label' => __( 'Post title', 'email-subscribers' ), 'icon' => '', 'description' => __( 'Show a post title', 'email-subscribers' ), ), array( 'keyword' => 'post.image', 'label' => __( 'Post image', 'email-subscribers' ), 'icon' => ' ', ), array( 'keyword' => 'post.date', 'label' => __( 'Post date', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.excerpt', 'label' => __( 'Post excerpt', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.description', 'label' => __( 'Post description', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.author', 'label' => __( 'Post author', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.link', 'label' => __( 'Post link', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.link_with_title', 'label' => __( 'Post link with title', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.link_only', 'label' => __( 'Post link only', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.full', 'label' => __( 'Post full', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.cats', 'label' => __( 'Post categories', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.more_tag', 'label' => __( 'Post more tag', 'email-subscribers' ), 'icon' => '', ), array( 'keyword' => 'post.image_url', 'label' => __( 'Post image URL', 'email-subscribers' ), 'icon' => '', 'description' => __( 'Show a post image URL', 'email-subscribers' ), ), ); $campaign_tags = array( 'post_notification' => $post_notification_tags, ); return apply_filters( 'ig_es_dnd_campaign_tags', $campaign_tags ); } public function get_dnd_subscriber_tags() { $subscriber_tags = array( array( 'keyword' => 'subscriber.name', 'label' => __( 'Name', 'email-subscribers' ), ), array( 'keyword' => 'subscriber.first_name', 'label' => __( 'First name', 'email-subscribers' ), ), array( 'keyword' => 'subscriber.last_name', 'label' => __( 'Last name', 'email-subscribers' ), ), array( 'keyword' => 'subscriber.email', 'label' => __( 'Email', 'email-subscribers' ), ), ); return apply_filters( 'ig_es_dnd_subscriber_tags', $subscriber_tags ); } public function get_dnd_site_tags() { $site_tags = array( array( 'keyword' => 'site.name', 'label' => __( 'Name', 'email-subscribers' ), ), array( 'keyword' => 'site.url', 'label' => __( 'URL', 'email-subscribers' ), ), array( 'keyword' => 'site.total_contacts', 'label' => __( 'Total contacts', 'email-subscribers' ), ), ); return apply_filters( 'ig_es_dnd_site_tags', $site_tags ); } public function show_merge_tags( $campaign_type, $target_elem_id ) { $subscriber_tags = $this->get_subscriber_tags(); ?>
render_merge_tags( $subscriber_tags ); ?>
get_site_tags(); if ( ! empty( $site_tags ) ) { ?>
render_merge_tags( $site_tags ); ?>
get_campaign_tags(); if ( ! empty( $campaign_tags ) ) { ?>
$tags ) : ?>
render_merge_tags( $tags ); ?>
get_subscriber_tags(); $site_tags = $this->get_site_tags(); $campaign_tags = $this->get_campaign_tags(); if ( $merge_tags === $campaign_tags['post_notification'] ) { ?> $tag ) { ?>
campaign_data; $campaign_id = ! empty( $campaign_data['id'] ) ? $campaign_data['id'] : 0; $campaign_from_name = ! empty( $campaign_data['from_name'] ) ? $campaign_data['from_name'] : get_option( 'ig_es_from_name' ); $campaign_email = ! empty( $campaign_data['from_email'] ) ? $campaign_data['from_email'] : $from_email; $campaign_reply_to = ! empty( $campaign_data['reply_to_email'] ) ? $campaign_data['reply_to_email'] : $from_email; $campaign_subject = ! empty( $campaign_data['subject'] ) ? $campaign_data['subject'] : $this->get_campaign_default_subject(); $campaign_status = ! empty( $campaign_data['status'] ) ? (int) $campaign_data['status'] : IG_ES_CAMPAIGN_STATUS_IN_ACTIVE; $campaign_type = ! empty( $campaign_data['type'] ) ? $campaign_data['type'] : ''; $editor_type = ! empty( $campaign_data['meta']['editor_type'] ) ? $campaign_data['meta']['editor_type'] : ''; $campaign_preheader = ! empty( $campaign_data['meta']['preheader'] ) ? $campaign_data['meta']['preheader'] : ''; $campaign_text = ''; $gallery_page_url = admin_url( 'admin.php?page=es_gallery' ); if ( IG_CAMPAIGN_TYPE_POST_NOTIFICATION === $campaign_type ) { $campaign_text = __( 'Post notification', 'email-subscribers' ); } elseif ( IG_CAMPAIGN_TYPE_POST_DIGEST === $campaign_type ) { $campaign_text = __( 'Post digest', 'email-subscribers' ); } elseif ( IG_CAMPAIGN_TYPE_NEWSLETTER === $campaign_type ) { $campaign_text = __( 'Broadcast', 'email-subscribers' ); } ?>

get_campaign_default_content(); $editor_settings = array( 'textarea_name' => 'data[body]', 'textarea_rows' => 40, 'media_buttons' => true, 'tinymce' => true, 'quicktags' => true, 'editor_class' => 'wp-campaign-body-editor', ); add_filter( 'tiny_mce_before_init', array( 'ES_Common', 'override_tinymce_formatting_options' ), 10, 2 ); add_filter( 'mce_external_plugins', array( 'ES_Common', 'add_mce_external_plugins' ) ); wp_editor( $editor_content, $editor_id, $editor_settings ); $this->show_avaialable_keywords(); } else { ?>
array( 'data-html-textarea-name' => 'data[body]', ), ); ( new ES_Drag_And_Drop_Editor() )->show_editor( $editor_settings ); ?>
show_avaialable_keywords(); } ?>
campaign_data; $campaign_type = $campaign_data['type']; $default_subject = apply_filters( 'ig_es_' . $campaign_type . '_default_subject', '', $campaign_data ); return $default_subject; } /** * Get default content for campaign * * @return string $default_content * * @since 5.3.3 */ public function get_campaign_default_content() { $campaign_data = $this->campaign_data; $campaign_type = $campaign_data['type']; $default_content = apply_filters( 'ig_es_' . $campaign_type . '_default_content', '', $campaign_data ); return $default_content; } /** * Show option to save campaign as template * * @return void * * @since 5.3.3 */ public function show_save_as_template() { ?>

campaigns_db->save_campaign( $campaign_data, $campaign_id ); } } return $campaign_saved; } /** * Schedule a campaign * * @param array $data * @return string $scheduling_status * * @since 5.3.3 */ public static function schedule_campaign( $data ) { $scheduling_status = ''; if ( ! empty( $data['id'] ) ) { $campaign_id = ! empty( $data['id'] ) ? $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 ); $data['body'] = ES_Common::es_process_template_body( $data['body'], $data['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' => $data['subject'], 'body' => $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. $data['hash'] = $notification['hash']; $data['campaign_id'] = $notification['campaign_id']; $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; $data['count'] = 0; $notification = ES_DB_Mailing_Queue::update_notification( $mailing_queue_id, $data ); } } if ( ! empty( $mailing_queue_id ) ) { if ( $should_queue_emails ) { $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 ) { $scheduling_status = 'success'; } else { $scheduling_status = 'emails_not_queued'; } } $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 ); } } } } return $scheduling_status; } public 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]; $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; } /** * Method to draft a campaign * * @return $response Broadcast response. * * @since 4.4.7 */ public function draft_campaign() { check_ajax_referer( 'ig-es-admin-ajax-nonce', 'security' ); $response = array(); $campaign_data = ig_es_get_request_data( 'data', array(), false ); /** * To allow insert of new campaign data, * we are specifically setting $campaign_id to null when id is empty in $campaign_data */ $campaign_id = ! empty( $campaign_data['id'] ) ? $campaign_data['id'] : null; $campaign_type = ! empty( $campaign_data['type'] ) ? $campaign_data['type'] : IG_ES_DRAG_AND_DROP_EDITOR; $is_updating = ! empty( $campaign_id ) ? true : false; $list_id = ! empty( $campaign_data['list_ids'] ) ? $campaign_data['list_ids'] : ''; $template_id = ! empty( $campaign_data['template_id'] ) ? $campaign_data['template_id'] : ''; if ( is_null( $campaign_id ) ) { unset( $campaign_data['id'] ); } $campaign_data['base_template_id'] = $template_id; $campaign_data['list_ids'] = $list_id; $campaign_data['status'] = ! empty( $campaign_data['status'] ) ? (int) $campaign_data['status'] : 0; $meta = ! empty( $campaign_data['meta'] ) ? $campaign_data['meta'] : array(); $meta['pre_header'] = ! empty( $campaign_data['pre_header'] ) ? $campaign_data['pre_header'] : ''; if ( ! empty( $meta['list_conditions'] ) ) { $meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] ); } $campaign_data['meta'] = maybe_serialize( $meta ); $campaign_data['name'] = wp_strip_all_tags( $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 ); $result = ES()->campaigns_db->save_campaign( $campaign_data, $campaign_id ); if ( ! empty( $result ) ) { if ( ! $is_updating ) { // In case of insert, result is campaign id. $response['campaign_id'] = $result; } else { // In case of update, only update flag is returned. $response['campaign_id'] = $campaign_id; } wp_send_json_success( $response ); } else { wp_send_json_error(); } } /** * Method to get preview HTML for campaign * * @return $response * * @since 4.4.7 */ public function get_campaign_preview() { check_ajax_referer( 'ig-es-admin-ajax-nonce', 'security' ); $response = array(); $preview_type = ig_es_get_request_data( 'preview_type' ); $campaign_data = ig_es_get_request_data( 'data', array(), false ); $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 = $this->add_campaign_body_data( $campaign_data ); $response['preview_html'] = $campaign_data['body']; if ( 'inline' === $preview_type ) { $inline_preview_data = $this->get_campaign_inline_preview_data( $campaign_data ); $response = array_merge( $response, $inline_preview_data ); } if ( ! empty( $response ) ) { wp_send_json_success( $response ); } else { wp_send_json_error(); } } /** * Method to get campaign inline preview data. * * @param array $campaign_data Broadcast data. * * @return array $preview_data * * @since 4.4.7 */ public function get_campaign_inline_preview_data( $campaign_data = array() ) { $list_id = ! empty( $campaign_data['list_ids'] ) ? $campaign_data['list_ids'] : 0; $preview_data = array(); $first_name = ''; $last_name = ''; $email = ''; if ( ! empty( $list_id ) ) { // Check if multiple lists selection is enabled. if ( is_array( $list_id ) && ! empty( $list_id ) ) { // Since we need to get only one sample email for showing the preview, we can get it from the first list itself. $list_id = $list_id[0]; } $subscribed_contacts = ES()->lists_contacts_db->get_subscribed_contacts_from_list( $list_id ); if ( ! empty( $subscribed_contacts ) ) { $subscribed_contact = array_shift( $subscribed_contacts ); $contact_id = ! empty( $subscribed_contact['contact_id'] ) ? $subscribed_contact['contact_id'] : 0; if ( ! empty( $contact_id ) ) { $subscriber_data = ES()->contacts_db->get_by_id( $contact_id ); if ( ! empty( $subscriber_data ) ) { $first_name = ! empty( $subscriber_data['first_name'] ) ? $subscriber_data['first_name'] : ''; $last_name = ! empty( $subscriber_data['last_name'] ) ? $subscriber_data['first_name'] : ''; $email = ! empty( $subscriber_data['email'] ) ? $subscriber_data['email'] : ''; } } } } $preview_data['campaign_subject'] = ! empty( $campaign_data['subject'] ) ? wp_strip_all_tags( $campaign_data['subject'] ) : ''; $preview_data['contact_name'] = esc_html( $first_name . ' ' . $last_name ); $preview_data['contact_email'] = esc_html( $email ); return $preview_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 ); $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']; $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 function show_avaialable_keywords() { ?>

{{subscriber.first_name | fallback:'there'}} {{subscriber.last_name}} {{subscriber.name | fallback:'there'}} {{subscriber.email}} {{DATE}} {{post.title}} {{post.image}} {{post.excerpt}} {{post.description}} {{post.author}} {{post.author_avatar}} {{post.author_avatar_link}} {{post.link}} {{post.link_with_title}} {{post.link_only}} {{post.full}}

{{subscriber.first_name | fallback:'there'}} {{subscriber.last_name}} {{subscriber.name | fallback:'there'}} {{subscriber.email}}

MAX  {{subscriber.first_name | fallback:'there'}} {{subscriber.last_name}} {{subscriber.name | fallback:'there'}}
{{post.digest}}

{{/post.digest}}
$campaign_subject, 'post_content' => $campaign_body, 'post_type' => 'es_template', 'post_status' => 'publish', ); $template_id = wp_insert_post( $template_data ); $is_template_added = ! ( $template_id instanceof WP_Error ); if ( $is_template_added ) { $editor_type = ! empty( $campaign_data['meta']['editor_type'] ) ? $campaign_data['meta']['editor_type'] : ''; $is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type; if ( $is_dnd_editor ) { $dnd_editor_data = array(); if ( ! empty( $campaign_data['meta']['dnd_editor_data'] ) ) { $dnd_editor_data = json_decode( $campaign_data['meta']['dnd_editor_data'] ); update_post_meta( $template_id, 'es_dnd_editor_data', $dnd_editor_data ); } } else { $custom_css = ! empty( $campaign_data['meta']['es_custom_css'] ) ? $campaign_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', $campaign_type ); $response['template_id'] = $template_id; } if ( ! empty( $response['template_id'] ) ) { wp_send_json_success( $response ); } else { wp_send_json_error(); } } return $response; } } } ES_Campaign_Admin::get_instance();