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( 'admin_init', array( $this, 'process_submission' ) ); add_action( 'ig_es_before_template_settings', array( $this, 'show_template_setting_fields' ) ); // preview popup //add_action( 'ig_es_template_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_template_preview', array( $this, 'get_template_preview' ) ); //add_action( 'media_buttons', array( $this, 'add_tag_button' ) ); } public function setup() { $template_id = $this->get_template_id_from_url(); if ( ! empty( $template_id ) ) { $template = get_post( $template_id, ARRAY_A ); if ( $template ) { $template_meta = get_post_custom( $template_id ); if ( ! empty( $template_meta ) ) { foreach ( $template_meta as $meta_key => $meta_value ) { $template_meta[ $meta_key ] = $meta_value[0]; } } if ( empty( $template_meta['es_editor_type'] ) ) { $template_meta['es_editor_type'] = IG_ES_CLASSIC_EDITOR; } $template['meta'] = $template_meta; $this->template_data = $template; } } else { $this->template_data['meta']['es_editor_type'] = $this->get_editor_type_from_url(); } } public function get_template_id_from_url() { $template_id = ig_es_get_request_data( 'id' ); return $template_id; } 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; } public function render() { $data = ig_es_get_request_data( 'data', array(), false ); $message_data = array(); $template_action = ig_es_get_request_data( 'ig_es_template_action' ); if ( ! empty( $template_action ) ) { if ( empty( $data['subject'] ) ) { $message = __( 'Please add a subject.', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'error', ); } } $action = ig_es_get_request_data( 'action' ); if ( 'added' === $action ) { $message = __( 'Template added successfully.', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'success', ); } elseif ( 'updated' === $action ) { $message = __( 'Template updated successfully.', 'email-subscribers' ); $message_data = array( 'message' => $message, 'type' => 'success', ); } $this->show_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; } $template_type = isset( $this->campaign_data['type'] ) ? $this->campaign_data['type'] : ''; ?>
get_post_notification_tags(); $template_tags = array( 'post_notification' => $post_notification_tags, ); return apply_filters( 'ig_es_campaign_tags', $template_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 render_merge_tags( $merge_tags = array() ) { if ( empty( $merge_tags ) ) { return; } foreach ( $merge_tags as $tag_key => $tag ) { ?>
template_data; $template_id = ! empty( $template_data['ID'] ) ? $template_data['ID'] : 0; $template_subject = ! empty( $template_data['post_title'] ) ? $template_data['post_title'] : ''; $template_status = ! empty( $template_data['post_status'] ) ? $template_data['post_status'] : 'draft'; $template_type = ! empty( $template_data['meta']['es_template_type'] ) ? $template_data['meta']['es_template_type'] : IG_CAMPAIGN_TYPE_NEWSLETTER; $editor_type = ! empty( $template_data['meta']['es_editor_type'] ) ? $template_data['meta']['es_editor_type'] : ''; ?>

'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(); } ?>
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]; } } $template_body = $template_data['body']; $template_body = ES_Common::es_process_template_body( $template_body, $template_id ); $template_body = ES_Common::replace_keywords_with_fallback( $template_body, array( 'FIRSTNAME' => $first_name, 'NAME' => $username, 'LASTNAME' => $last_name, 'EMAIL' => $useremail ) ); $template_body = ES_Common::replace_keywords_with_fallback( $template_body, array( 'subscriber.first_name' => $first_name, 'subscriber.name' => $username, 'subscriber.last_name' => $last_name, 'subscriber.email' => $useremail ) ); $template_type = $template_data['meta']['es_template_type']; $template_data['body'] = $template_body; if ( IG_CAMPAIGN_TYPE_POST_NOTIFICATION === $template_type ) { $template_data = self::replace_post_notification_merge_tags_with_sample_post( $template_data ); } elseif ( IG_CAMPAIGN_TYPE_POST_DIGEST === $template_type ) { $template_data = self::replace_post_digest_merge_tags_with_sample_posts( $template_data ); } $template_body = ! empty( $template_data['body'] ) ? $template_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 ); } $template_body = apply_filters( 'the_content', $template_body ); $template_data['body'] = $template_body; return $template_data; } } /** * Method to get preview HTML for campaign * * @return $response * * @since 4.4.7 */ public function get_template_preview() { check_ajax_referer( 'ig-es-admin-ajax-nonce', 'security' ); $response = array(); $data = ig_es_get_request_data( 'data', array(), false ); $template_data = $this->add_campaign_body_data( $data ); $response['preview_html'] = $template_data['body']; if ( ! empty( $response ) ) { wp_send_json_success( $response ); } else { wp_send_json_error(); } } /** * Method to get campaign inline preview data. * * @param array $template_data Broadcast data. * * @return array $preview_data * * @since 4.4.7 */ public function get_campaign_inline_preview_data( $template_data = array() ) { $list_id = ! empty( $template_data['list_ids'] ) ? $template_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( $template_data['subject'] ) ? esc_html( $template_data['subject'] ) : ''; $preview_data['contact_name'] = esc_html( $first_name . ' ' . $last_name ); $preview_data['contact_email'] = esc_html( $email ); return $preview_data; } public function add_post_notification_data( $template_data ) { $categories = ! empty( $template_data['es_note_cat'] ) ? $template_data['es_note_cat'] : array(); $es_note_cat_parent = $template_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( $template_data['es_note_cpt'] ) ) { // Merge categories and selected custom post types. $categories = array_merge( $categories, $template_data['es_note_cpt'] ); } $template_data['categories'] = ES_Common::convert_categories_array_to_string( $categories ); return $template_data; } public static function replace_post_notification_merge_tags_with_sample_post( $template_data ) { if ( ! empty( $template_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 = $template_data['id']; $template_body = ! empty( $template_data['body'] ) ? $template_data['body'] : ''; $template_subject = ! empty( $template_data['subject'] ) ? $template_data['subject'] : ''; $template_subject = ES_Handle_Post_Notification::prepare_subject( $template_subject, $post ); $template_body = ES_Handle_Post_Notification::prepare_body( $template_body, $post_id, $template_id ); $template_data['subject'] = $template_subject; $template_data['body'] = $template_body; } } return $template_data; } public static function replace_post_digest_merge_tags_with_sample_posts( $template_data ) { if ( ! empty( $template_data['id'] ) && class_exists( 'ES_Post_Digest' ) ) { $ignore_stored_post_ids = true; $ignore_last_run = true; $template_id = $template_data['id']; $template_body = $template_data['body']; $post_ids = ES_Post_Digest::get_matching_post_ids( $template_id, $ignore_stored_post_ids, $ignore_last_run ); $template_body = ES_Post_Digest::process_post_digest_template( $template_body, $post_ids ); $template_data['body'] = $template_body; } return $template_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}}

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

{{/post.digest}}
$template_subject, 'post_content' => $template_body, 'post_type' => 'es_template', 'post_status' => $template_status, ); $action = ''; if ( empty( $template_id ) ) { $template_id = wp_insert_post( $data ); $action = 'added'; } else { $data['ID'] = $template_id; $template_id = wp_update_post( $data ); $action = 'updated'; } $is_template_added = ! ( $template_id instanceof WP_Error ); if ( $is_template_added ) { 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 ); } if ( ! empty( $template_id ) ) { $template_url = admin_url( 'admin.php?page=es_template&id=' . $template_id . '&action=' . $action ); wp_safe_redirect( $template_url ); exit(); } else { $message = __( 'An error has occured. Please try again later', 'email-subscribers' ); ES_Common::show_message( $message, 'error' ); } } } } /** * Method to load admin views * * @since 5.5.4 * * @param string $view View name. * @param array $imported_variables Passed variables. * @param mixed $path Path to view file. */ public static function get_view( $view, $imported_variables = array(), $path = false ) { if ( $imported_variables && is_array( $imported_variables ) ) { extract( $imported_variables ); // phpcs:ignore } if ( ! $path ) { $path = ES_PLUGIN_DIR . 'lite/admin/views/'; } include $path . $view . '.php'; } } }