Your IP : 216.73.216.162


Current Path : /home/x/b/o/xbodynamge/namtation/wp-content/
Upload File :
Current File : /home/x/b/o/xbodynamge/namtation/wp-content/Emails.tar

Mailer.php000066600000023737151134027270006510 0ustar00<?php

namespace WPForms\Emails;

use WPForms\Emails\Templates\General;

/**
 * Mailer class to wrap wp_mail().
 *
 * @package    WPForms\Emails
 * @author     WPForms
 * @since      1.5.4
 * @license    GPL-2.0+
 * @copyright  Copyright (c) 2019, WPForms LLC
 */
class Mailer {

	/**
	 * Email address to send to.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $to_email;

	/**
	 * CC addresses (comma delimited).
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $cc;

	/**
	 * From address.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $from_address;

	/**
	 * From name.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $from_name;

	/**
	 * Reply to address.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $reply_to;

	/**
	 * Email headers.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $headers;

	/**
	 * Email content type.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $content_type;

	/**
	 * Email attachments.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $attachments;

	/**
	 * Email subject.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $subject;

	/**
	 * Email message.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	private $message;

	/**
	 * Email template.
	 *
	 * @since 1.5.4
	 *
	 * @var General
	 */
	private $template;

	/**
	 * Set a property.
	 *
	 * @since 1.5.4
	 *
	 * @param string $key   Property name.
	 * @param string $value Property value.
	 */
	public function __set( $key, $value ) {

		$this->$key = $value;
	}

	/**
	 * Get a property.
	 *
	 * @since 1.5.4
	 *
	 * @param string $key Property name.
	 *
	 * @return string
	 */
	public function __get( $key ) {

		return $this->$key;
	}

	/**
	 * Check if a property exists.
	 *
	 * @since 1.5.4
	 *
	 * @param string $key Property name.
	 *
	 * @return bool
	 */
	public function __isset( $key ) {

		return isset( $this->key );
	}

	/**
	 * Unset a property.
	 *
	 * @since 1.5.4
	 *
	 * @param string $key Property name.
	 */
	public function __unset( $key ) {

		unset( $this->key );
	}

	/**
	 * Email kill switch if needed.
	 *
	 * @since 1.5.4
	 *
	 * @return bool
	 */
	public function is_email_disabled() {

		return (bool) \apply_filters( 'wpforms_emails_mailer_is_email_disabled', false, $this );
	}

	/**
	 * Sanitize the string.
	 *
	 * @since 1.5.4
	 *
	 * @param string $string     String that may contain tags.
	 * @param bool   $linebreaks Toggle to process linebreaks.
	 *
	 * @return string
	 */
	public function sanitize( $string = '', $linebreaks = false ) {

		$string = \wpforms_decode_string( $string );

		return $linebreaks ? \wpforms_sanitize_textarea_field( $string ) : \sanitize_text_field( $string );
	}

	/**
	 * Get the email from name.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_from_name() {

		$this->from_name = $this->from_name ? $this->sanitize( $this->from_name ) : \get_bloginfo( 'name' );

		return \apply_filters( 'wpforms_emails_mailer_get_from_name', $this->from_name, $this );
	}

	/**
	 * Get the email from address.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_from_address() {

		$this->from_address = $this->from_address ? $this->sanitize( $this->from_address ) : \get_option( 'admin_email' );

		return \apply_filters( 'wpforms_emails_mailer_get_from_address', $this->from_address, $this );
	}

	/**
	 * Get the email reply to address.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_reply_to_address() {

		if ( empty( $this->reply_to ) || ! \is_email( $this->reply_to ) ) {
			$this->reply_to = $this->from_address;
		}

		$this->reply_to = $this->sanitize( $this->reply_to );

		if ( empty( $this->reply_to ) || ! \is_email( $this->reply_to ) ) {
			$this->reply_to = \get_option( 'admin_email' );
		}

		return \apply_filters( 'wpforms_emails_mailer_get_reply_to_address', $this->reply_to, $this );
	}

	/**
	 * Get the email carbon copy addresses.
	 *
	 * @since 1.5.4
	 *
	 * @return string The email carbon copy addresses.
	 */
	public function get_cc_address() {

		if ( empty( $this->cc ) ) {
			return \apply_filters( 'wpforms_emails_mailer_get_cc_address', $this->cc, $this );
		}

		$this->cc = $this->sanitize( $this->cc );

		$addresses = \array_map( 'trim', \explode( ',', $this->cc ) );

		foreach ( $addresses as $key => $address ) {
			if ( ! \is_email( $address ) ) {
				unset( $addresses[ $key ] );
			}
		}

		$this->cc = \implode( ',', $addresses );

		return \apply_filters( 'wpforms_emails_mailer_get_cc_address', $this->cc, $this );
	}

	/**
	 * Get the email content type.
	 *
	 * @since 1.5.4
	 *
	 * @return string The email content type.
	 */
	public function get_content_type() {

		$is_html = 'default' === \wpforms_setting( 'email-template', 'default' );

		if ( ! $this->content_type && $is_html ) {
			$this->content_type = \apply_filters( 'wpforms_emails_mailer_get_content_type_default', 'text/html', $this );
		} elseif ( ! $is_html ) {
			$this->content_type = 'text/plain';
		}

		return \apply_filters( 'wpforms_emails_mailer_get_content_type', $this->content_type, $this );
	}

	/**
	 * Get the email message.
	 *
	 * @since 1.5.4
	 *
	 * @return string The email message.
	 */
	public function get_message() {

		if ( empty( $this->message ) && ! empty( $this->template ) ) {
			$this->message = $this->template->get();
		}

		return \apply_filters( 'wpforms_emails_mailer_get_message', $this->message, $this );
	}

	/**
	 * Get the email headers.
	 *
	 * @since 1.5.4
	 *
	 * @return string The email headers.
	 */
	public function get_headers() {

		if ( $this->headers ) {
			return \apply_filters( 'wpforms_emails_mailer_get_headers', $this->headers, $this );
		}

		$this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";

		if ( $this->get_reply_to_address() ) {
			$this->headers .= "Reply-To: {$this->get_reply_to_address()}\r\n";
		}

		if ( $this->get_cc_address() ) {
			$this->headers .= "Cc: {$this->get_cc_address()}\r\n";
		}

		$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";

		return \apply_filters( 'wpforms_emails_mailer_get_headers', $this->headers, $this );
	}

	/**
	 * Get the email attachments.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_attachments() {

		return \apply_filters( 'wpforms_emails_mailer_get_attachments', $this->attachments, $this );
	}

	/**
	 * Set email address to send to.
	 *
	 * @since 1.5.4
	 *
	 * @param string $email Email address.
	 *
	 * @return Mailer
	 */
	public function to_email( $email ) {

		$this->to_email = \apply_filters( 'wpforms_emails_mailer_to_email', $email, $this );

		return $this;
	}

	/**
	 * Set email subject.
	 *
	 * @since 1.5.4
	 *
	 * @param string $subject Email subject.
	 *
	 * @return Mailer
	 */
	public function subject( $subject ) {

		$subject = $this->sanitize( $subject );

		$this->subject = \apply_filters( 'wpforms_emails_mailer_subject', $subject, $this );

		return $this;
	}

	/**
	 * Set email message (body).
	 *
	 * @since 1.5.4
	 *
	 * @param string $message Email message.
	 *
	 * @return Mailer
	 */
	public function message( $message ) {

		$this->message = \apply_filters( 'wpforms_emails_mailer_message', $message, $this );

		return $this;
	}

	/**
	 * Set email template.
	 *
	 * @since 1.5.4
	 *
	 * @param General $template Email template.
	 *
	 * @return Mailer
	 */
	public function template( General $template ) {

		$this->template = \apply_filters( 'wpforms_emails_mailer_template', $template, $this );

		return $this;
	}

	/**
	 * Get email errors.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_errors() {

		$errors = array();

		if ( ! \is_email( $this->to_email ) ) {
			$errors[] = \esc_html__( '[WPForms\Emails\Mailer] Invalid email address.', 'wpforms-lite' );
		}

		if ( empty( $this->subject ) ) {
			$errors[] = \esc_html__( '[WPForms\Emails\Mailer] Empty subject line.', 'wpforms-lite' );
		}

		if ( empty( $this->get_message() ) ) {
			$errors[] = \esc_html__( '[WPForms\Emails\Mailer] Empty message.', 'wpforms-lite' );
		}

		return $errors;
	}

	/**
	 * Log given email errors.
	 *
	 * @since 1.5.4
	 *
	 * @param array $errors Errors to log.
	 */
	protected function log_errors( $errors ) {

		if ( empty( $errors ) || ! \is_array( $errors ) ) {
			return;
		}

		foreach ( $errors as $error ) {
			\wpforms_log(
				$error,
				array(
					'to_email' => $this->to_email,
					'subject'  => $this->subject,
					'message'  => \wp_trim_words( $this->get_message() ),
				),
				array(
					'type' => 'error',
				)
			);
		}
	}

	/**
	 * Send the email.
	 *
	 * @since 1.5.4
	 *
	 * @return bool
	 */
	public function send() {

		if ( ! \did_action( 'init' ) && ! \did_action( 'admin_init' ) ) {
			\_doing_it_wrong( __FUNCTION__, \esc_html__( 'You cannot send emails with WPForms\Emails\Mailer until init/admin_init has been reached.', 'wpforms-lite' ), null );

			return false;
		}

		// Don't send anything if emails have been disabled.
		if ( $this->is_email_disabled() ) {
			return false;
		}

		$errors = $this->get_errors();

		if ( $errors ) {
			$this->log_errors( $errors );
			return false;
		}

		$this->send_before();

		$sent = \wp_mail(
			$this->to_email,
			$this->subject,
			$this->get_message(),
			$this->get_headers(),
			$this->get_attachments()
		);

		$this->send_after();

		return $sent;
	}

	/**
	 * Add filters / actions before the email is sent.
	 *
	 * @since 1.5.4
	 */
	public function send_before() {

		\do_action( 'wpforms_emails_mailer_send_before', $this );
		\add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
		\add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
		\add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
	}

	/**
	 * Remove filters / actions after the email is sent.
	 *
	 * @since 1.5.4
	 */
	public function send_after() {

		\do_action( 'wpforms_emails_mailer_send_after', $this );
		\remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
		\remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
		\remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
	}
}
InfoBlocks.php000066600000007230151134027270007316 0ustar00<?php
namespace WPForms\Emails;

/**
 * Fetching and formatting Info Blocks for Email Summaries class.
 *
 * @package    WPForms\Emails
 * @author     WPForms
 * @since      1.5.4
 * @license    GPL-2.0+
 * @copyright  Copyright (c) 2019, WPForms LLC
 */
class InfoBlocks {

	/**
	 * Source of info blocks content.
	 *
	 * @since 1.5.4
	 */
	const SOURCE_URL = 'https://cdn.wpforms.com/wp-content/email-summaries.json';

	/**
	 * Fetch info blocks info from remote.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	public function fetch_all() {

		$info = array();

		// TODO: Change this URL once the infrastructure is ready.
		$res = \wp_remote_get( self::SOURCE_URL );

		if ( \is_wp_error( $res ) ) {
			return $info;
		}

		$body = \wp_remote_retrieve_body( $res );

		if ( empty( $body ) ) {
			return $info;
		}

		$body = \json_decode( $body, true );

		return $this->verify_fetched( $body );
	}

	/**
	 * Verify fetched blocks data.
	 *
	 * @since 1.5.4
	 *
	 * @param array $fetched Fetched blocks data.
	 *
	 * @return array
	 */
	protected function verify_fetched( $fetched ) {

		$info = array();

		if ( ! \is_array( $fetched ) ) {
			return $info;
		}

		foreach ( $fetched as $item ) {

			if ( empty( $item['id'] ) ) {
				continue;
			}

			$id = \absint( $item['id'] );

			if ( empty( $id ) ) {
				continue;
			}

			$info[ $id ] = $item;
		}

		return $info;
	}

	/**
	 * Get info blocks relevant to customer's licence.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_by_license() {

		$data     = $this->fetch_all();
		$filtered = array();

		if ( empty( $data ) || ! \is_array( $data ) ) {
			return $filtered;
		}

		$license_type = \wpforms_setting( 'type', false, 'wpforms_license' );

		foreach ( $data as $key => $item ) {

			if ( ! isset( $item['type'] ) || ! \is_array( $item['type'] ) ) {
				continue;
			}

			if ( ! \in_array( $license_type, $item['type'], true ) ) {
				continue;
			}

			$filtered[ $key ] = $item;
		}

		return $filtered;
	}

	/**
	 * Get the first block with a valid id.
	 * Needed to ignore blocks with invalid/missing ids.
	 *
	 * @since 1.5.4
	 *
	 * @param array $data Blocks array.
	 *
	 * @return array
	 */
	protected function get_first_with_id( $data ) {

		if ( empty( $data ) || ! \is_array( $data ) ) {
			return array();
		}

		foreach ( $data as $item ) {
			$item_id = \absint( $item['id'] );
			if ( ! empty( $item_id ) ) {
				return $item;
			}
		}

		return array();
	}

	/**
	 * Get next info block that wasn't sent yet.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	public function get_next() {

		$data  = $this->get_by_license();
		$block = array();

		if ( empty( $data ) || ! \is_array( $data ) ) {
			return $block;
		}

		$blocks_sent = \get_option( 'wpforms_emails_infoblocks_sent' );

		if ( empty( $blocks_sent ) || ! \is_array( $blocks_sent ) ) {
			$block = $this->get_first_with_id( $data );
		}

		if ( empty( $block ) ) {
			$data  = \array_diff_key( $data, \array_flip( $blocks_sent ) );
			$block = $this->get_first_with_id( $data );
		}

		return $block;
	}

	/**
	 * Register a block as sent.
	 *
	 * @since 1.5.4
	 *
	 * @param array $info_block Info block.
	 */
	public function register_sent( $info_block ) {

		$block_id = isset( $info_block['id'] ) ? \absint( $info_block['id'] ) : false;

		if ( empty( $block_id ) ) {
			return;
		}

		$option_name = 'wpforms_email_summaries_info_blocks_sent';
		$blocks      = \get_option( $option_name );

		if ( empty( $blocks ) || ! \is_array( $blocks ) ) {
			\update_option( $option_name, array( $block_id ) );
			return;
		}

		if ( \in_array( $block_id, $blocks, true ) ) {
			return;
		}

		$blocks[] = $block_id;

		\update_option( $option_name, $blocks );
	}
}
Templates/Summary.php000066600000002006151134027270010654 0ustar00<?php

namespace WPForms\Emails\Templates;

/**
 * Email Summaries email template class.
 *
 * @package    WPForms\Emails\Templates
 * @author     WPForms
 * @since      1.5.4
 * @license    GPL-2.0+
 * @copyright  Copyright (c) 2019, WPForms LLC
 */
class Summary extends General {

	/**
	 * Template slug.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	const TEMPLATE_SLUG = 'summary';

	/**
	 * Get header image URL from settings.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_header_image() {

		$img = array(
			'url' => \wpforms_setting( 'email-header-image' ),
		);

		if ( ! empty( $img['url'] ) ) {
			return $img;
		}

		// Set specific percentage WPForms logo width for modern email clients.
		$this->set_args(
			array(
				'style' => array(
					'header_image_max_width' => '45%',
				),
			)
		);

		// Set specific WPForms logo width in pixels for MS Outlook and old email clients.
		return array(
			'url'   => \WPFORMS_PLUGIN_URL . 'assets/images/logo.png',
			'width' => 250,
		);
	}
}
Templates/General.php000066600000021661151134027270010604 0ustar00<?php

namespace WPForms\Emails\Templates;

use WPForms\Emails\Styler;
use WPForms\Helpers\Templates;

/**
 * Base email template class.
 *
 * @package    WPForms\Emails\Templates
 * @author     WPForms
 * @since      1.5.4
 * @license    GPL-2.0+
 * @copyright  Copyright (c) 2019, WPForms LLC
 */
class General {

	/**
	 * Template slug.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	const TEMPLATE_SLUG = 'general';

	/**
	 * Email message.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	protected $message;

	/**
	 * Content is plain text type.
	 *
	 * @since 1.5.4
	 *
	 * @var bool
	 */
	protected $plain_text;

	/**
	 * Dynamic {{tags}}.
	 *
	 * @since 1.5.4
	 *
	 * @var array
	 */
	protected $tags;

	/**
	 * Header/footer/body arguments.
	 *
	 * @since 1.5.4
	 *
	 * @var array
	 */
	protected $args;

	/**
	 * Final email content.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	protected $content;

	/**
	 * Constructor.
	 *
	 * @since 1.5.4
	 *
	 * @param string $message Email message.
	 */
	public function __construct( $message = '' ) {

		$this->set_message( $message );

		$this->plain_text = 'default' !== \wpforms_setting( 'email-template', 'default' );

		$this->set_initial_args();
	}

	/**
	 * Set initial arguments to use in a template.
	 *
	 * @since 1.5.4
	 */
	public function set_initial_args() {

		$header_args = array(
			'title' => \esc_html__( 'WPForms', 'wpforms-lite' ),
		);

		if ( ! $this->plain_text ) {
			$header_args['header_image'] = $this->get_header_image();
		}

		$args = array(
			'header' => $header_args,
			'body'   => array( 'message' => $this->get_message() ),
			'footer' => array(),
			'style'  => array(),
		);

		$args = \apply_filters( 'wpforms_emails_templates_general_set_initial_args', $args, $this );

		$this->set_args( $args );
	}

	/**
	 * Get the template slug.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_slug() {

		return static::TEMPLATE_SLUG;
	}

	/**
	 * Get the template parent slug.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_parent_slug() {

		return self::TEMPLATE_SLUG;
	}

	/**
	 * Get the message.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get_message() {

		return \apply_filters( 'wpforms_emails_templates_general_get_message', $this->message, $this );
	}

	/**
	 * Get the dynamic tags.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	public function get_tags() {

		return \apply_filters( 'wpforms_emails_templates_general_get_tags', $this->tags, $this );
	}

	/**
	 * Get header/footer/body arguments
	 *
	 * @since 1.5.4
	 *
	 * @param string $type Header/footer/body.
	 *
	 * @return array
	 */
	public function get_args( $type ) {

		if ( ! empty( $type ) ) {
			return isset( $this->args[ $type ] ) ? \apply_filters( 'wpforms_emails_templates_general_get_args_' . $type, $this->args[ $type ], $this ) : array();
		}

		return \apply_filters( 'wpforms_emails_templates_general_get_args', $this->args, $this );
	}

	/**
	 * Set email message.
	 *
	 * @since 1.5.4
	 *
	 * @param string $message Email message.
	 *
	 * @return General
	 */
	public function set_message( $message ) {

		$message = \apply_filters( 'wpforms_emails_templates_general_set_message', $message, $this );

		if ( ! \is_string( $message ) ) {
			return $this;
		}

		$this->message = $message;

		return $this;
	}

	/**
	 * Set the dynamic tags.
	 *
	 * @since 1.5.4
	 *
	 * @param array $tags Tags to set.
	 *
	 * @return General
	 */
	public function set_tags( $tags ) {

		$tags = \apply_filters( 'wpforms_emails_templates_general_set_tags', $tags, $this );

		if ( ! \is_array( $tags ) ) {
			return $this;
		}

		$this->tags = $tags;

		return $this;
	}

	/**
	 * Set header/footer/body/style arguments to use in a template.
	 *
	 * @since 1.5.4
	 *
	 * @param array $args  Arguments to set.
	 * @param bool  $merge Merge the arguments with existing once or replace.
	 *
	 * @return General
	 */
	public function set_args( $args, $merge = true ) {

		$args = \apply_filters( 'wpforms_emails_templates_general_set_args', $args, $this );

		if ( empty( $args ) || ! \is_array( $args ) ) {
			return $this;
		}

		foreach ( $args as $type => $value ) {

			if ( ! \is_array( $value ) ) {
				continue;
			}

			if ( ! isset( $this->args[ $type ] ) || ! \is_array( $this->args[ $type ] ) ) {
				$this->args[ $type ] = array();
			}

			$this->args[ $type ] = $merge ? \array_merge( $this->args[ $type ], $value ) : $value;
		}

		return $this;
	}

	/**
	 * Process and replace any dynamic tags.
	 *
	 * @since 1.5.4
	 *
	 * @param string $content Content to make replacements in.
	 *
	 * @return string
	 */
	public function process_tags( $content ) {

		$tags = $this->get_tags();

		if ( empty( $tags ) ) {
			return $content;
		}

		foreach ( $tags as $tag => $value ) {
			$content = \str_replace( $tag, $value, $content );
		}

		return $content;
	}

	/**
	 * Conditionally modify email template name.
	 *
	 * @since 1.5.4
	 *
	 * @param string $name Base template name.
	 *
	 * @return string
	 */
	protected function get_full_template_name( $name ) {

		$name = \sanitize_file_name( $name );

		if ( $this->plain_text ) {
			$name .= '-plain';
		}

		$template = 'emails/' . $this->get_slug() . '-' . $name;

		if ( ! Templates::locate( $template . '.php' ) ) {
			$template = 'emails/' . $this->get_parent_slug() . '-' . $name;
		}

		return \apply_filters( 'wpforms_emails_templates_general_get_full_template_name', $template, $this );
	}

	/**
	 * Get header image URL from settings.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_header_image() {

		/**
		 * Additional 'width' key with an integer value can be added to $img array to control image's width in pixels.
		 * This setting helps to scale an image in some versions of MS Outlook and old email clients.
		 * Percentage 'width' values have no effect in MS Outlook and will be sanitized as integer by an email template..
		 *
		 * Example:
		 *
		 * $img = array(
		 *     'url'   => \wpforms_setting( 'email-header-image' ),
		 *     'width' => 150,
		 * );
		 *
		 *
		 * To set percentage values for the modern email clients, use $this->set_args() method:
		 *
		 * $this->set_args(
		 *     array(
		 *         'style' => array(
		 *             'header_image_max_width' => '45%',
		 *         ),
		 *    )
		 *);
		 *
		 * Both pixel and percentage approaches work well with 'wpforms_emails_templates_general_get_header_image' filter or this class extension.
		 */
		$img = array(
			'url' => \wpforms_setting( 'email-header-image' ),
		);

		return \apply_filters( 'wpforms_emails_templates_general_get_header_image', $img, $this );
	}

	/**
	 * Get content part HTML.
	 *
	 * @since 1.5.4
	 *
	 * @param string $name Name of the content part.
	 *
	 * @return string
	 */
	protected function get_content_part( $name ) {

		if ( ! \is_string( $name ) ) {
			return '';
		}

		$html = Templates::get_html(
			$this->get_full_template_name( $name ),
			$this->get_args( $name ),
			true
		);

		return \apply_filters( 'wpforms_emails_templates_general_get_content_part', $html, $name, $this );
	}

	/**
	 * Assemble all content parts in an array.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_content_parts() {

		$parts = array(
			'header' => $this->get_content_part( 'header' ),
			'body'   => $this->get_content_part( 'body' ),
			'footer' => $this->get_content_part( 'footer' ),
		);

		return \apply_filters( 'wpforms_emails_templates_general_get_content_parts', $parts, $this );
	}

	/**
	 * Apply inline styling and save email content.
	 *
	 * @since 1.5.4
	 *
	 * @param string $content Content with no styling applied.
	 */
	protected function save_styled( $content ) {

		if ( empty( $content ) ) {
			$this->content = '';
			return;
		}

		if ( $this->plain_text ) {
			$this->content = \wp_strip_all_tags( $content );
			return;
		}

		$style_templates = array(
			'style'   => $this->get_full_template_name( 'style' ),
			'queries' => $this->get_full_template_name( 'queries' ),
		);

		$styler = new Styler( $content, $style_templates, $this->get_args( 'style' ) );

		$this->content = \apply_filters( 'wpforms_emails_templates_general_save_styled_content', $styler->get(), $this );
	}

	/**
	 * Build an email including styling.
	 *
	 * @since 1.5.4
	 *
	 * @param bool $force Rebuild the content if it was already built and saved.
	 */
	protected function build( $force = false ) {

		if ( $this->content && ! $force ) {
			return;
		}

		$content = \implode( $this->get_content_parts() );

		if ( empty( $content ) ) {
			return;
		}

		$content = $this->process_tags( $content );

		if ( ! $this->plain_text ) {
			$content = \make_clickable( $content );
		}

		$content = \apply_filters( 'wpforms_emails_templates_general_build_content', $content, $this );

		$this->save_styled( $content );
	}

	/**
	 * Return final email.
	 *
	 * @since 1.5.4
	 *
	 * @param bool $force Rebuild the content if it was already built and saved.
	 *
	 * @return string
	 */
	public function get( $force = false ) {

		$this->build( $force );

		return $this->content;
	}
}
Styler.php000066600000005324151134027270006551 0ustar00<?php

namespace WPForms\Emails;

use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
use WPForms\Helpers\Templates;

/**
 * Styler class inline style email templates.
 *
 * @package    WPForms\Emails
 * @author     WPForms
 * @since      1.5.4
 * @license    GPL-2.0+
 * @copyright  Copyright (c) 2019, WPForms LLC
 */
class Styler {

	/**
	 * Email message with no styles.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	protected $email;

	/**
	 * Email style templates names.
	 *
	 * @since 1.5.4
	 *
	 * @var array
	 */
	protected $style_templates;

	/**
	 * Email style overrides.
	 *
	 * @since 1.5.4
	 *
	 * @var array
	 */
	protected $style_overrides;

	/**
	 * Email message with inline styles.
	 *
	 * @since 1.5.4
	 *
	 * @var string
	 */
	protected $styled_email;

	/**
	 * Constructor.
	 *
	 * @since 1.5.4
	 *
	 * @param string $email           Email with no styles.
	 * @param array  $style_templates Email style templates.
	 * @param array  $style_overrides Email style overrides.
	 */
	public function __construct( $email, $style_templates, $style_overrides ) {

		$this->email = $email;

		$this->style_templates = \is_array( $style_templates ) ? $style_templates : array();
		$this->style_overrides = \is_array( $style_overrides ) ? $style_overrides : array();
	}

	/**
	 * Template style overrides.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_style_overrides() {

		$defaults = array(
			'email_background_color' => \wpforms_setting( 'email-background-color', '#e9eaec' ),
		);

		$overrides = \wp_parse_args( $this->style_overrides, $defaults );

		return \apply_filters( 'wpforms_emails_mailer_get_style_overrides', $overrides, $this );
	}

	/**
	 * Locate template name matching styles.
	 *
	 * @since 1.5.4
	 *
	 * @param string $name Template file name part.
	 *
	 * @return string
	 */
	protected function get_styles( $name = 'style' ) {

		if ( ! \array_key_exists( $name, $this->style_templates ) ) {
			return '';
		}

		return Templates::get_html(
			$this->style_templates[ $name ],
			$this->get_style_overrides(),
			true
		);
	}

	/**
	 * Final processing of the template markup.
	 *
	 * @since 1.5.4
	 */
	public function process_markup() {

		$this->styled_email = ( new CssToInlineStyles() )->convert( $this->email, $this->get_styles() );

		$queries = '<style type="text/css">' . $this->get_styles( 'queries' ) . "</style>\n</head>";

		// Inject media queries, CssToInlineStyles strips them.
		$this->styled_email = \str_replace( '</head>', $queries, $this->styled_email );
	}

	/**
	 * Get an email with inline styles.
	 *
	 * @since 1.5.4
	 *
	 * @return string
	 */
	public function get() {

		if ( empty( $this->styled_email ) ) {
			$this->process_markup();
		}

		return $this->styled_email;
	}
}
Summaries.php000066600000012655151134027270007241 0ustar00<?php
namespace WPForms\Emails;

/**
 * Email Summaries main class.
 *
 * @package    WPForms\Emails
 * @author     WPForms
 * @since      1.5.4
 * @license    GPL-2.0+
 * @copyright  Copyright (c) 2019, WPForms LLC
 */
class Summaries {

	/**
	 * Constructor.
	 *
	 * @since 1.5.4
	 */
	public function __construct() {

		$this->hooks();

		$summaries_disabled = $this->is_summaries_disabled();

		if ( $summaries_disabled && \wp_next_scheduled( 'wpforms_email_summaries_cron' ) ) {
			\wp_clear_scheduled_hook( 'wpforms_email_summaries_cron' );
		}

		if ( ! $summaries_disabled && ! \wp_next_scheduled( 'wpforms_email_summaries_cron' ) ) {
			\wp_schedule_event( $this->get_first_cron_date_gmt(), 'wpforms_email_summaries_weekly', 'wpforms_email_summaries_cron' );
		}
	}

	/**
	 * Get the instance of a class and store it in itself.
	 *
	 * @since 1.5.4
	 */
	public static function get_instance() {

		static $instance;

		if ( ! $instance ) {
			$instance = new self();
		}

		return $instance;
	}

	/**
	 * Email Summaries hooks.
	 *
	 * @since 1.5.4
	 */
	public function hooks() {

		\add_filter( 'wpforms_settings_defaults', array( $this, 'disable_summaries_setting' ) );

		if ( ! $this->is_summaries_disabled() ) {
			\add_action( 'init', array( $this, 'preview' ) );
			\add_filter( 'cron_schedules', array( $this, 'add_weekly_cron_schedule' ) );
			\add_action( 'wpforms_email_summaries_cron', array( $this, 'cron' ) );
		}
	}

	/**
	 * Check if Email Summaries are disabled in settings.
	 *
	 * @since 1.5.4
	 *
	 * @return bool
	 */
	protected function is_summaries_disabled() {

		return (bool) \wpforms_setting( 'email-summaries-disable' );
	}

	/**
	 * Add "Disable Email Summaries" to WPForms settings.
	 *
	 * @since 1.5.4
	 *
	 * @param array $settings WPForms settings.
	 *
	 * @return mixed
	 */
	public function disable_summaries_setting( $settings ) {

		$url = \add_query_arg(
			array(
				'wpforms_email_template' => 'summary',
				'wpforms_email_preview'  => '1',
			),
			\admin_url()
		);

		$desc = \esc_html__( 'Disable Email Summaries weekly delivery.', 'wpforms-lite' );

		if ( ! $this->is_summaries_disabled() ) {
			$desc .= '<br><a href="' . $url . '" target="_blank">' . \esc_html__( 'View Email Summary Example', 'wpforms-lite' ) . '</a>';
		}

		$settings['misc']['email-summaries-disable'] = array(
			'id'   => 'email-summaries-disable',
			'name' => \esc_html__( 'Disable Email Summaries', 'wpforms-lite' ),
			'desc' => $desc,
			'type' => 'checkbox',
		);

		return $settings;
	}

	/**
	 * Preview Email Summary.
	 *
	 * @since 1.5.4
	 */
	public function preview() {

		if ( ! \wpforms_current_user_can() ) {
			return;
		}

		if ( ! isset( $_GET['wpforms_email_preview'], $_GET['wpforms_email_template'] ) ) { // phpcs:ignore
			return;
		}

		if ( 'summary' !== $_GET['wpforms_email_template'] ) { // phpcs:ignore
			return;
		}

		$args = array(
			'body' => array(
				'entries'    => $this->get_entries(),
				'info_block' => ( new InfoBlocks() )->get_next(),
			),
		);

		$template = ( new Templates\Summary() )->set_args( $args );
		$template = \apply_filters( 'wpforms_emails_summaries_template', $template );

		$content = $template->get();

		if ( 'default' !== \wpforms_setting( 'email-template', 'default' ) ) {
			$content = \wpautop( $content );
		}

		echo $content; // phpcs:ignore

		exit;
	}

	/**
	 * Get next cron occurrence date.
	 *
	 * @since 1.5.4
	 *
	 * @return int
	 */
	protected function get_first_cron_date_gmt() {

		$date = \absint( \strtotime( 'next monday 2pm' ) - ( \get_option( 'gmt_offset' ) * \HOUR_IN_SECONDS ) );

		return $date ? $date : \time();
	}

	/**
	 * Add custom Email Summaries cron schedule.
	 *
	 * @since 1.5.4
	 *
	 * @param array $schedules WP cron schedules.
	 *
	 * @return array
	 */
	public function add_weekly_cron_schedule( $schedules ) {

		$schedules['wpforms_email_summaries_weekly'] = array(
			'interval' => \WEEK_IN_SECONDS,
			'display'  => \esc_html__( 'Weekly WPForms Email Summaries', 'wpforms-lite' ),
		);

		return $schedules;
	}

	/**
	 * Email Summaries cron callback.
	 *
	 * @since 1.5.4
	 */
	public function cron() {

		$entries = $this->get_entries();

		// Email won't be sent if there are no form entries.
		if ( empty( $entries ) ) {
			return;
		}

		$info_blocks = new InfoBlocks();

		$next_block = $info_blocks->get_next();

		$args = array(
			'body' => array(
				'entries'    => $entries,
				'info_block' => $next_block,
			),
		);

		$template = ( new Templates\Summary() )->set_args( $args );
		$template = \apply_filters( 'wpforms_emails_summaries_template', $template );

		$content = $template->get();

		if ( ! $content ) {
			return;
		}

		$to_email = \apply_filters( 'wpforms_emails_summaries_cron_to_email', \get_option( 'admin_email' ) );
		$subject  = \apply_filters( 'wpforms_emails_summaries_cron_subject', \esc_html__( 'WPForms Summary', 'wpforms-lite' ) );

		$sent = ( new Mailer() )
			->template( $template )
			->subject( $subject )
			->to_email( $to_email )
			->send();

		if ( true === $sent ) {
			$info_blocks->register_sent( $next_block );
		}
	}

	/**
	 * Get form entries.
	 *
	 * @since 1.5.4
	 *
	 * @return array
	 */
	protected function get_entries() {

		if ( \wpforms()->pro ) {
			$entries_count = new \WPForms\Pro\Reports\EntriesCount();
			$results       = $entries_count->get_by( 'form', 0, 7, 'previous sunday' );
		} else {
			$entries_count = new \WPForms\Lite\Reports\EntriesCount();
			$results       = $entries_count->get_by_form();
		}

		return $results;
	}
}