Email::prepare_template()

Description #

Prepare template for email body based on event type.

Changelog #

VersionDescription
4.1.0Introduced.

Source #

File: addons/free/email/class-email.php

	public function prepare_template() {
		$page_id = ap_opt( 'email_template_' . $this->event );
		$_post = get_post( $page_id );

		if ( $_post ) {
			$this->template = apply_filters( 'the_content', $_post->post_content );
			$this->subject = $_post->post_title;
		}

		// If template not found use default one.
		if ( empty( $this->template ) ) {
			$default_template = Hooks::get_default_template( $this->event );
			$this->template = $default_template['body'];
			$this->subject = $default_template['subject'];
		}

		$this->body = strtr( $this->template, $this->template_tags );
		$this->subject = strtr( $this->subject, $this->template_tags );

		$main_tags = array(
			'email_title' => $this->subject,
			'email_body'  => $this->body,
			'style'       => file_get_contents( ap_get_theme_location( 'addons/email/style.css' ) ),
			'site_name'   => get_bloginfo( 'name' ),
		);

		/**
		 * This filter allows overriding `$main_tags`. Which is used in main
		 * email template.
		 *
		 * @param string $main_template Parsed template.
		 * @param object $email         Current email object.
		 * @since 4.1.0
		 * @return string
		 */
		$main_tags = apply_filters_ref_array( 'ap_email_main_tags', [ $main_tags, $this ] );

		foreach ( $main_tags as $key => $content ) {
			$this->add_template_tag( $key, $content );
		}

		$main_template = file_get_contents( ap_get_theme_location( 'addons/email/template.html' ) );
		$main_template = strtr( $main_template, $this->template_tags );

		/**
		 * Allows filtering email template after its prepared, passed be reference.
		 *
		 * @param string $main_template Parsed template.
		 * @param object $email         Current email object.
		 * @since 4.1.0
		 * @return string
		 */
		$main_template = apply_filters_ref_array( 'ap_email_template_prepared', [ $main_template, $this ] );

		return $main_template;
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment