Noptin_Page::preview_email( $campaign_id )

Unsubscribes a user


Return Return

(array)


Top ↑

Source Source

File: includes/class-noptin-page.php

	public function preview_email( $campaign_id ) {

		// Ensure an email campaign is specified.
		if ( empty( $campaign_id ) ) {
			$this->print_paragraph( __( 'Invalid or missing campaign id.', 'newsletter-optin-box' ) );
			return;
		}

		// and that the current user is an administrator
		if ( ! current_user_can( get_noptin_capability() ) ) {
			$this->print_paragraph( __( 'Only administrators can preview email campaigns.', 'newsletter-optin-box' ) );
			return;
		}

		$campaign = get_post( $campaign_id );

		// Ensure this is a newsletter campaign.
		if ( empty( $campaign ) || 'noptin-campaign' !== $campaign->post_type || 'newsletter' !== get_post_meta( $campaign->ID, 'campaign_type', true ) ) {
			$this->print_paragraph( __( 'Cannot preview this campaign type.', 'newsletter-optin-box' ) );
			return;
		}

		// Fetch current user to use their details as merge tags.
		$user       = wp_get_current_user();
		$subscriber = get_noptin_subscriber_by_email( $user->user_email );
		$data       = array(
			'campaign_id'   => $campaign->ID,
			'email_subject' => $campaign->post_title,
			'email_body'    => $campaign->post_content,
			'preview_text'  => get_post_meta( $campaign->ID, 'preview_text', true ),
			'email'         => $user->user_email,
			'merge_tags'    => array(
				'email'       => $user->user_email,
				'first_name'  => $user->user_firstname,
				'second_name' => $user->user_lastname,
				'last_name'   => $user->user_lastname,
			),
		);

		// If the current user is a subscriber, use their subscriber data as merge tags.
		if ( $subscriber->exists() ) {
			$data['subscriber_id'] = $subscriber->id;
			$data['merge_tags']    = array_merge( $data['merge_tags'], get_noptin_subscriber_merge_fields( $subscriber ) );
		}

		// Generate and display the email.
		$data = noptin()->mailer->prepare( $data );
		echo $data['email_body'];
		exit;

	}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.2.2Introduced.

Top ↑

User Contributed Notes User Contributed Notes

30 day money back guarantee
Safe & Secure online payment
Custom CSS Slide Example
Custom Animation Example
Default Animation