is_pro() ) { // Add scheduler data add_filter( 'ig_es_broadcast_data', array( &$this, 'es_add_broadcast_scheduler_data' ), 10, 2 ); } // Check campaign wise open tracking is enabled. add_filter( 'ig_es_track_open', array( $this, 'is_open_tracking_enabled' ), 10, 4 ); add_action( 'ig_es_show_' . IG_CAMPAIGN_TYPE_NEWSLETTER . '_campaign_summary_action_buttons', array( $this, 'show_summary_actions_buttons' ) ); //add_action( 'ig_es_' . IG_CAMPAIGN_TYPE_NEWSLETTER . '_default_subject', array( $this, 'get_post_notification_default_subject' ) ); add_action( 'ig_es_' . IG_CAMPAIGN_TYPE_NEWSLETTER . '_default_content', array( $this, 'get_newsletter_default_content' ) ); } public static function set_screen( $status, $option, $value ) { return $value; } /** * Method to process broadcast submission. * * @since 4.4.7 */ public function process_broadcast_submission() { global $wpdb; $submitted = ig_es_get_request_data( 'ig_es_broadcast_submitted' ); $broadcast_data = ig_es_get_request_data( 'broadcast_data', array(), false ); if ( 'submitted' === $submitted ) { $broadcast_nonce = ig_es_get_request_data( 'ig_es_broadcast_nonce' ); // Verify nonce. if ( wp_verify_nonce( $broadcast_nonce, 'ig-es-broadcast-nonce' ) ) { $list_id = ! empty( $broadcast_data['list_ids'] ) ? $broadcast_data['list_ids'] : ''; $template_id = ! empty( $broadcast_data['template_id'] ) ? $broadcast_data['template_id'] : ''; $subject = ! empty( $broadcast_data['subject'] ) ? $broadcast_data['subject'] : ''; // Check if user has added required data for creating broadcast. if ( ! empty( $broadcast_data['subject'] ) && ! empty( $broadcast_data['body'] ) && ! empty( $subject ) ) { $broadcast_data['base_template_id'] = $template_id; $broadcast_data['list_ids'] = $list_id; $broadcast_data['status'] = IG_ES_CAMPAIGN_STATUS_SCHEDULED; $meta = ! empty( $broadcast_data['meta'] ) ? $broadcast_data['meta'] : array(); $meta['scheduling_option'] = ! empty( $broadcast_data['scheduling_option'] ) ? $broadcast_data['scheduling_option'] : 'schedule_now'; $meta['es_schedule_date'] = ! empty( $broadcast_data['es_schedule_date'] ) ? $broadcast_data['es_schedule_date'] : ''; $meta['es_schedule_time'] = ! empty( $broadcast_data['es_schedule_time'] ) ? $broadcast_data['es_schedule_time'] : ''; $meta['pre_header'] = ! empty( $broadcast_data['pre_header'] ) ? $broadcast_data['pre_header'] : ''; if ( ! empty( $meta['list_conditions'] ) ) { $meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] ); } $broadcast_data['meta'] = maybe_serialize( $meta ); self::es_send_email_callback( $broadcast_data ); $campaign_url = admin_url( 'admin.php?page=es_campaigns&action=broadcast_created' ); wp_safe_redirect( $campaign_url ); exit(); } } else { $message = __( 'Sorry, you are not allowed to add/edit broadcast.', 'email-subscribers' ); ES_Common::show_message( $message, 'error' ); } } } public function es_newsletters_settings_callback() { global $wpdb; $broadcast_id = ig_es_get_request_data( 'list' ); $submitted = ig_es_get_request_data( 'ig_es_broadcast_submitted' ); $broadcast_data = ig_es_get_request_data( 'broadcast_data', array(), false ); $message_data = array(); if ( 'submitted' === $submitted ) { // If form is submitted then broadcast id is sent in $broadcast_data array. $broadcast_id = ! empty( $broadcast_data['id'] ) ? $broadcast_data['id'] : ''; $subject = ! empty( $broadcast_data['subject'] ) ? $broadcast_data['subject'] : ''; if ( empty( $broadcast_data['subject'] ) ) { $message = __( 'Please add a broadcast subject.', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'error', ); } elseif ( empty( $broadcast_data['body'] ) ) { $message = __( 'Please add message body or select template', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'error', ); } elseif ( empty( $subject ) ) { $message = __( 'Please add the subject', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'error', ); } } if ( ! empty( $broadcast_id ) ) { $broadcast_query = $wpdb->prepare( ' id = %d LIMIT 0, 1', $broadcast_id ); $broadcasts = ES()->campaigns_db->get_by_conditions( $broadcast_query ); $broadcast = array_shift( $broadcasts ); $broadcast_data = array( 'id' => $broadcast['id'], 'name' => $broadcast['name'], 'subject' => $broadcast['subject'], 'from_name' => $broadcast['from_name'], 'from_email' => $broadcast['from_email'], 'reply_to_email' => $broadcast['reply_to_email'], 'body' => $broadcast['body'], 'list_ids' => $broadcast['list_ids'], 'template_id' => $broadcast['base_template_id'], 'status' => $broadcast['status'], 'meta' => maybe_unserialize( $broadcast['meta'] ), ); } $this->prepare_newsletter_settings_form( $broadcast_data, $message_data ); } /** * Method to display newsletter setting form * * @param array $broadcast_data Posted broadcast data * * @since 4.4.2 Added $broadcast_data param */ public function prepare_newsletter_settings_form( $broadcast_data = array(), $message_data = array() ) { $template_id = ! empty( $broadcast_data['template_id'] ) ? $broadcast_data['template_id'] : ''; $list_ids = ! empty( $broadcast_data['list_ids'] ) ? $broadcast_data['list_ids'] : ''; $templates = ES_Common::prepare_templates_dropdown_options( 'newsletter', $template_id ); $from_email = ES_Common::get_ig_option( 'from_email' ); $broadcast_id = ! empty( $broadcast_data['id'] ) ? $broadcast_data['id'] : 0; $broadcast_from_name = ! empty( $broadcast_data['from_name'] ) ? $broadcast_data['from_name'] : get_option( 'ig_es_from_name' ); $broadcast_email = ! empty( $broadcast_data['from_email'] ) ? $broadcast_data['from_email'] : $from_email; $broadcast_reply_to = ! empty( $broadcast_data['reply_to_email'] ) ? $broadcast_data['reply_to_email'] : $from_email; $broadcast_subject = ! empty( $broadcast_data['subject'] ) ? $broadcast_data['subject'] : ''; $broadcast_status = ! empty( $broadcast_data['status'] ) ? (int) $broadcast_data['status'] : 0; // Flag to check if broadcast is being send or already sent. $is_broadcast_processing = false; if ( ! empty( $broadcast_status ) ) { $broadcast_allowed_edit_statuses = array( IG_ES_CAMPAIGN_STATUS_ACTIVE, IG_ES_CAMPAIGN_STATUS_IN_ACTIVE, IG_ES_CAMPAIGN_STATUS_SCHEDULED, ); if ( ! in_array( $broadcast_status, $broadcast_allowed_edit_statuses ) ) { $is_broadcast_processing = true; } $scheduling_disabled_message = ''; if ( IG_ES_CAMPAIGN_STATUS_QUEUED === $broadcast_status ) { $scheduling_disabled_message = __( 'Scheduling is disabled for this broadcast since it is being sent.', 'email-subscribers' ); } elseif ( IG_ES_CAMPAIGN_STATUS_FINISHED === $broadcast_status ) { $scheduling_disabled_message = __( 'Scheduling is disabled for this broadcast since it has been sent already.', 'email-subscribers' ); } if ( ! empty( $scheduling_disabled_message ) ) { $message_data = array( 'message' => $scheduling_disabled_message, 'type' => 'error', ); } } $allowedtags = ig_es_allowed_html_tags_in_esc(); ?>

'broadcast_data[body]', 'textarea_rows' => 40, 'media_buttons' => true, 'tinymce' => true, 'quicktags' => true, 'editor_class' => 'wp-editor-boradcast', ); 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( $body, 'edit-es-broadcast-body', $editor_args ); ?>




campaigns_db->save_campaign( $data, $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 ); $campaign_meta = maybe_unserialize( $data['meta'] ); $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( $data['start_at'] ) ? $data['start_at'] : '', '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. if ( $selected_list_ids !== $existing_list_ids ) { $should_queue_emails = true; $data['count'] = 0; } else { $data['count'] = $notification['count']; } $notification = ES_DB_Mailing_Queue::update_notification( $mailing_queue_id, $data ); } } if ( ! empty( $mailing_queue_id ) ) { if ( $should_queue_emails ) { // Delete existing sending queue if any already present. ES_DB_Sending_Queue::delete_by_mailing_queue_id( array( $mailing_queue_id ) ); ES_DB_Sending_Queue::queue_emails( $mailing_queue_id, $mailing_queue_hash, $campaign_id, $list_id ); } $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 refresh_newsletter_content( $content, $args ) { $campaign_id = $args['campaign_id']; $template_id = ES()->campaigns_db->get_template_id_by_campaign( $campaign_id ); $content['subject'] = ES()->campaigns_db->get_column( 'subject', $campaign_id ); $content['body'] = ES()->campaigns_db->get_column( 'body', $campaign_id ); $content['body'] = ES_Common::es_process_template_body( $content['body'], $template_id, $campaign_id ); return $content; } public static function get_instance() { if ( ! isset( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } public function es_broadcast_preview_callback( $template_data ) { $template_id = $template_data['template_id']; $campaign_id = ! empty( $template_data['campaign_id'] ) ? $template_data['campaign_id'] : 0; if ( ! empty( $template_data['content'] ) ) { $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]; } } $es_template_body = $template_data['content']; $es_template_body = ES_Common::es_process_template_body( $es_template_body, $template_id, $campaign_id ); $es_template_body = ES_Common::replace_keywords_with_fallback( $es_template_body, array( 'FIRSTNAME' => $first_name, 'NAME' => $username, 'LASTNAME' => $last_name, 'EMAIL' => $useremail ) ); // 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 ); } $template_html = apply_filters( 'the_content', $es_template_body ); return $template_html; } } /** * Method to draft a broadcast * * @return $response Broadcast response. * * @since 4.4.7 */ public function draft_broadcast() { check_ajax_referer( 'ig-es-admin-ajax-nonce', 'security' ); $response = array(); $broadcast_data = ig_es_get_request_data( 'broadcast_data', array(), false ); /** * To allow insert of new broadcast data, * we are specifically setting $broadcast_id to null when id is empty in $broadcast_data */ $broadcast_id = ! empty( $broadcast_data['id'] ) ? $broadcast_data['id'] : null; $is_updating = ! empty( $broadcast_id ) ? true : false; $list_id = ! empty( $broadcast_data['list_ids'] ) ? $broadcast_data['list_ids'] : ''; $template_id = ! empty( $broadcast_data['template_id'] ) ? $broadcast_data['template_id'] : ''; $broadcast_data['base_template_id'] = $template_id; $broadcast_data['list_ids'] = $list_id; $broadcast_data['status'] = ! empty( $broadcast_data['status'] ) ? $broadcast_data['status'] : 0; $meta = ! empty( $broadcast_data['meta'] ) ? $broadcast_data['meta'] : array(); $meta['pre_header'] = ! empty( $broadcast_data['pre_header'] ) ? $broadcast_data['pre_header'] : ''; if ( ! empty( $meta['list_conditions'] ) ) { $meta['list_conditions'] = IG_ES_Campaign_Rules::remove_empty_conditions( $meta['list_conditions'] ); } $broadcast_data['meta'] = maybe_serialize( $meta ); $broadcast_data['type'] = 'newsletter'; $broadcast_data['name'] = $broadcast_data['subject']; $broadcast_data['slug'] = sanitize_title( sanitize_text_field( $broadcast_data['name'] ) ); $broadcast_data = apply_filters( 'ig_es_broadcast_data', $broadcast_data ); $result = ES()->campaigns_db->save_campaign( $broadcast_data, $broadcast_id ); if ( ! empty( $result ) ) { if ( ! $is_updating ) { // In case of insert, result is broadcast id. $response['broadcast_id'] = $result; } else { // In case of update, only update flag is returned. $response['broadcast_id'] = $broadcast_id; } wp_send_json_success( $response ); } else { wp_send_json_error(); } } /** * Method to get preview HTML for broadcast * * @return $response * * @since 4.4.7 */ public function preview_broadcast() { check_ajax_referer( 'ig-es-admin-ajax-nonce', 'security' ); $response = array(); $preview_type = ig_es_get_request_data( 'preview_type' ); $broadcast_data = ig_es_get_request_data( 'broadcast_data', array(), false ); $template_data = array(); $template_data['content'] = ! empty( $broadcast_data['body'] ) ? $broadcast_data['body'] : ''; $template_data['template_id'] = ! empty( $broadcast_data['template_id'] ) ? $broadcast_data['template_id'] : ''; $template_data['campaign_id'] = ! empty( $broadcast_data['id'] ) ? $broadcast_data['id'] : 0; $tempate_html = $this->es_broadcast_preview_callback( $template_data ); $response['template_html'] = $tempate_html; if ( 'inline' === $preview_type ) { $inline_preview_data = $this->get_broadcast_inline_preview_data( $broadcast_data ); $response = array_merge( $response, $inline_preview_data ); } if ( ! empty( $response ) ) { wp_send_json_success( $response ); } else { wp_send_json_error(); } } /** * Method to get broadcast inline preview data. * * @param array $broadcast_data Broadcast data. * * @return array $preview_data * * @since 4.4.7 */ public function get_broadcast_inline_preview_data( $broadcast_data = array() ) { $list_id = ! empty( $broadcast_data['list_ids'] ) ? $broadcast_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['broadcast_subject'] = ! empty( $broadcast_data['subject'] ) ? esc_html( $broadcast_data['subject'] ) : ''; $preview_data['contact_name'] = esc_html( $first_name . ' ' . $last_name ); $preview_data['contact_email'] = esc_html( $email ); return $preview_data; } /** * Add required broadcast schedule date/time data * * @param array $data * * @return array $data * * @since 4.4.7 */ public function es_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 = time(); } 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; } /** * Method to check if open tracking is enabled broadcast 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 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']; if ( in_array( $campaign_type, array( 'newsletter', 'workflow_email' ), true ) ) { $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 function show_summary_actions_buttons( $campaign_data ) { $campaign_status = ! empty( $campaign_data['status'] ) ? (int) $campaign_data['status'] : IG_ES_CAMPAIGN_STATUS_IN_ACTIVE; // Flag to check if broadcast is being send or already sent. $is_broadcast_processing = false; if ( ! empty( $campaign_status ) ) { $processing_status = array( IG_ES_CAMPAIGN_STATUS_QUEUED, IG_ES_CAMPAIGN_STATUS_FINISHED, ); $has_processing_status = in_array( $campaign_status, $processing_status, true ); if ( $has_processing_status ) { $is_broadcast_processing = true; } } // If broadcast is sent or being sent then don't allow scheduling to avoid conflicts. if ( ! $is_broadcast_processing ) { ?> get_classic_editor_default_content()); $content_data[IG_ES_DRAG_AND_DROP_EDITOR] = $this->get_dnd_editor_default_content(); return $content_data; } public function get_classic_editor_default_content() { $default_content = __( "Hello {{subscriber.name | fallback='there'}},", 'email-subscribers' ) . "\r\n\r\n"; $default_content .= __( 'Your content...', 'email-subscribers' ) . "\r\n\r\n"; $default_content .= __( 'Thanks & Regards', 'email-subscribers' ) . ",\r\n"; $default_content .= __( 'Admin', 'email-subscribers' ) . "\r\n\r\n"; $default_content .= __( 'You received this email because in the past you have provided us your email address : {{subscriber.email}} to receive notifications when new updates are posted.', 'email-subscribers' ); return $default_content; } public function get_dnd_editor_default_content() { $default_content = ' ' . __( "Hello {{subscriber.name | fallback='there'}},", 'email-subscribers' ) . ' Your content... @2023, ' . __( 'Your Brand Name.', 'email-subscribers' ) . ' You are receiving this email because you have visited our site or asked about our regular newsletter. If you wish to unsubscribe from our newsletter, click here '; return $default_content; } }