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(); ?> get_subscriber_tags(); $site_tags = $this->get_site_tags(); $campaign_tags = $this->get_campaign_tags(); if ( $merge_tags === $campaign_tags['post_notification'] ) { ?> $tag ) { ?>