Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails;
|
||||
|
||||
use WPForms\Tasks\Task;
|
||||
|
||||
/**
|
||||
* Action Scheduler task to fetch and cache Email Summaries Info Blocks.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
class FetchInfoBlocksTask extends Task {
|
||||
|
||||
/**
|
||||
* Action name for this task.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
const ACTION = 'wpforms_email_summaries_fetch_info_blocks';
|
||||
|
||||
/**
|
||||
* Option name to store the timestamp of the last run.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
const LAST_RUN = 'wpforms_email_summaries_fetch_info_blocks_last_run';
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
parent::__construct( self::ACTION );
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the task with all the proper checks.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
public function init() {
|
||||
|
||||
$this->hooks();
|
||||
|
||||
$tasks = wpforms()->get( 'tasks' );
|
||||
|
||||
// Add new if none exists.
|
||||
if ( $tasks->is_scheduled( self::ACTION ) !== false ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->recurring( $this->generate_start_date(), WEEK_IN_SECONDS )->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hooks.
|
||||
*
|
||||
* @since 1.7.3
|
||||
*/
|
||||
private function hooks() {
|
||||
|
||||
// Register the action handler.
|
||||
add_action( self::ACTION, [ $this, 'process' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly pick a timestamp which is not more than 1 week in the future
|
||||
* starting before Email Summaries dispatch happens.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function generate_start_date() {
|
||||
|
||||
$tracking = [];
|
||||
|
||||
$tracking['days'] = wp_rand( 0, 6 ) * DAY_IN_SECONDS;
|
||||
$tracking['hours'] = wp_rand( 0, 23 ) * HOUR_IN_SECONDS;
|
||||
$tracking['minutes'] = wp_rand( 0, 59 ) * MINUTE_IN_SECONDS;
|
||||
$tracking['seconds'] = wp_rand( 0, 59 );
|
||||
|
||||
return strtotime( 'previous monday 1pm' ) + array_sum( $tracking );
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the task.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
public function process() {
|
||||
|
||||
$last_run = get_option( self::LAST_RUN );
|
||||
|
||||
// Make sure we do not run it more than once a day.
|
||||
if (
|
||||
$last_run !== false &&
|
||||
( time() - $last_run ) < DAY_IN_SECONDS
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
( new InfoBlocks() )->cache_all();
|
||||
|
||||
// Update the last run option to the current timestamp.
|
||||
update_option( self::LAST_RUN, time() );
|
||||
}
|
||||
}
|
||||
274
wp-content/plugins/wpforms-lite/src/Emails/Helpers.php
Normal file
274
wp-content/plugins/wpforms-lite/src/Emails/Helpers.php
Normal file
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails;
|
||||
|
||||
/**
|
||||
* Helper class for the email templates.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Helpers {
|
||||
|
||||
/**
|
||||
* Get Email template choices.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param bool $include_legacy Whether to include a Legacy template into the list.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_email_template_choices( $include_legacy = true ) {
|
||||
|
||||
$choices = [];
|
||||
$templates = Notifications::get_all_templates();
|
||||
|
||||
// If there are no templates, return empty choices.
|
||||
if ( empty( $templates ) || ! is_array( $templates ) ) {
|
||||
return $choices;
|
||||
}
|
||||
|
||||
// Add legacy template to the choices as the first option.
|
||||
if ( $include_legacy && self::is_legacy_html_template() ) {
|
||||
$choices['default'] = [
|
||||
'name' => esc_html__( 'Legacy', 'wpforms-lite' ),
|
||||
];
|
||||
}
|
||||
|
||||
// Iterate through templates and build $choices array.
|
||||
foreach ( $templates as $template_key => $template ) {
|
||||
// Skip if the template name is empty.
|
||||
if ( empty( $template['name'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$choices[ $template_key ] = $template;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the current email template name.
|
||||
* If the current template is not found, the default template will be returned.
|
||||
*
|
||||
* This method respects backward compatibility and will return the old "Legacy" template if it is set.
|
||||
* If a template name is provided, the function will attempt to validate and return it. If validation fails,
|
||||
* it will default to the email template name "Classic."
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $template_name Optional. The name of the email template to evaluate.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_current_template_name( $template_name = '' ) {
|
||||
|
||||
// If a template name is provided, sanitize it. Otherwise, use the default template name from settings.
|
||||
$settings_template = wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE );
|
||||
$template = ! empty( $template_name ) ? trim( sanitize_text_field( $template_name ) ) : $settings_template;
|
||||
|
||||
// If the user has set the legacy template, return it.
|
||||
if ( $template === Notifications::LEGACY_TEMPLATE && self::is_legacy_html_template() ) {
|
||||
return Notifications::LEGACY_TEMPLATE;
|
||||
}
|
||||
|
||||
// In case the user has changed the general settings template,
|
||||
// but the form submitted still uses the “Legacy” template,
|
||||
// we need to revert to the general settings template.
|
||||
if ( $template === Notifications::LEGACY_TEMPLATE && ! self::is_legacy_html_template() ) {
|
||||
$template = wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE );
|
||||
}
|
||||
|
||||
// Check if the given template name is valid by looking into available templates.
|
||||
$current_template = Notifications::get_available_templates( $template );
|
||||
|
||||
// If the current template is not found or its corresponding class does not exist, return the default template.
|
||||
if ( ! isset( $current_template['path'] ) || ! class_exists( $current_template['path'] ) ) {
|
||||
|
||||
// Last resort, check if the template defined in the settings can be used.
|
||||
// This would be helpful when user downgrades from Pro to Lite version and the template is not available anymore.
|
||||
if ( isset( $current_template[ $settings_template ] ) ) {
|
||||
return $settings_template;
|
||||
}
|
||||
|
||||
return Notifications::DEFAULT_TEMPLATE;
|
||||
}
|
||||
|
||||
// The provided template is valid, so return it.
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current email template class path.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $template_name Optional. The name of the email template to evaluate.
|
||||
* @param string $fallback_class Optional. The class to use if the template is not found.
|
||||
* This argument most likely will be used for backward compatibility and supporting the "Legacy" template.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_current_template_class( $template_name = '', $fallback_class = '' ) {
|
||||
|
||||
$template_name = self::get_current_template_name( $template_name );
|
||||
|
||||
// If the user has set the legacy template, return the "General" template.
|
||||
if ( $template_name === Notifications::LEGACY_TEMPLATE ) {
|
||||
return ! empty( $fallback_class ) && class_exists( $fallback_class ) ? $fallback_class : __NAMESPACE__ . '\Templates\General';
|
||||
}
|
||||
|
||||
// Check if the given template name is valid by looking into available templates.
|
||||
$current_template = Notifications::get_available_templates( $template_name );
|
||||
|
||||
// If the current template is not found or its corresponding class does not exist, return the "Classic" template.
|
||||
if ( ! isset( $current_template['path'] ) || ! class_exists( $current_template['path'] ) ) {
|
||||
return Notifications::get_available_templates( Notifications::DEFAULT_TEMPLATE )['path'];
|
||||
}
|
||||
|
||||
// The provided template is valid, so return it.
|
||||
return $current_template['path'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the style overrides for the current email template.
|
||||
*
|
||||
* This function retrieves the style overrides for the email template, including background color,
|
||||
* body color, text color, link color, and typography. It provides default values and handles
|
||||
* different settings for both the free and Pro versions of the plugin.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_current_template_style_overrides() {
|
||||
|
||||
$header_image_size = self::get_template_header_image_size();
|
||||
$defaults = [
|
||||
'email_background_color' => '#e9eaec',
|
||||
'email_body_color' => '#ffffff',
|
||||
'email_text_color' => '#333333',
|
||||
'email_links_color' => '#e27730',
|
||||
'email_typography' => self::get_template_typography(),
|
||||
'header_image_max_width' => $header_image_size['width'],
|
||||
'header_image_max_height' => $header_image_size['height'],
|
||||
];
|
||||
|
||||
// This option will retrieve the old background color setting from the Lite version.
|
||||
$lite_background_color = wpforms_setting( 'email-background-color', $defaults['email_background_color'] );
|
||||
|
||||
// Return the color scheme if the user has the Pro version.
|
||||
if ( ! wpforms()->is_pro() ) {
|
||||
// Override the background color with the old setting.
|
||||
$defaults['email_background_color'] = $lite_background_color;
|
||||
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
// Get the color scheme from the settings.
|
||||
// Default to an empty array if the setting is not found as we will merge it with the defaults later.
|
||||
$color_scheme = wpforms_setting( 'email-color-scheme', [] );
|
||||
|
||||
// If the user has the Pro version, but the background color is the old setting, override it.
|
||||
if ( empty( $color_scheme['email_background_color'] ) && ! empty( $lite_background_color ) ) {
|
||||
$color_scheme['email_background_color'] = $lite_background_color;
|
||||
}
|
||||
|
||||
return wp_parse_args( $color_scheme, $defaults );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current email template is plain text.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $template_name Optional. The name of the email template to compare.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_plain_text_template( $template_name = '' ) {
|
||||
|
||||
// Leave early in case the given template name is not empty, and we can resolve it early.
|
||||
if ( ! empty( $template_name ) ) {
|
||||
return $template_name === Notifications::PLAIN_TEMPLATE;
|
||||
}
|
||||
|
||||
return wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE ) === Notifications::PLAIN_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current template is legacy.
|
||||
* Legacy template is the one that its value is 'default'.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_legacy_html_template() {
|
||||
|
||||
return wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE ) === Notifications::LEGACY_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current template's typography.
|
||||
*
|
||||
* This function retrieves the typography setting for email templates and returns the corresponding font family.
|
||||
*
|
||||
* If the user has the Pro version, the font-family is determined based on the current template.
|
||||
* For free users, the font-family defaults to "Sans Serif" because the available templates
|
||||
* ("Classic" and "Compact") use this font-family in their design.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function get_template_typography() {
|
||||
|
||||
$font_families = [
|
||||
'sans_serif' => '-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif',
|
||||
'serif' => 'Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol',
|
||||
];
|
||||
|
||||
// In case user downgraded to the free version return the "Sans Serif" font-family.
|
||||
// The only available templates for free users are "Classic" and "Compact" which both uses the "Sans Serif" font-family in their design.
|
||||
if ( ! wpforms()->is_pro() ) {
|
||||
return $font_families['sans_serif'];
|
||||
}
|
||||
|
||||
$template_typography = wpforms_setting( 'email-typography', 'sans-serif' );
|
||||
|
||||
return isset( $font_families[ $template_typography ] ) ? $font_families[ $template_typography ] : $font_families['sans_serif'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the header image size based on the specified size or 'medium' by default.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_template_header_image_size() {
|
||||
|
||||
$sizes = [
|
||||
'small' => [
|
||||
'width' => '240',
|
||||
'height' => '120',
|
||||
],
|
||||
'medium' => [
|
||||
'width' => '350',
|
||||
'height' => '180',
|
||||
],
|
||||
'large' => [
|
||||
'width' => '500',
|
||||
'height' => '240',
|
||||
],
|
||||
];
|
||||
|
||||
// The desired image size ('small', 'medium', or 'large').
|
||||
$header_image_size = wpforms_setting( 'email-header-image-size', 'medium' );
|
||||
|
||||
return ! empty( $sizes[ $header_image_size ] ) ? $sizes[ $header_image_size ] : $sizes['medium'];
|
||||
}
|
||||
}
|
||||
277
wp-content/plugins/wpforms-lite/src/Emails/InfoBlocks.php
Normal file
277
wp-content/plugins/wpforms-lite/src/Emails/InfoBlocks.php
Normal file
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
namespace WPForms\Emails;
|
||||
|
||||
/**
|
||||
* Fetching and formatting Info Blocks for Email Summaries class.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
class InfoBlocks {
|
||||
|
||||
/**
|
||||
* Source of info blocks content.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
const SOURCE_URL = 'https://wpforms.com/wp-content/email-summaries.json';
|
||||
|
||||
/**
|
||||
* Get info blocks info from the cache file or remote.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_all() {
|
||||
|
||||
$cache_file = $this->get_cache_file_path();
|
||||
|
||||
if ( empty( $cache_file ) || ! is_readable( $cache_file ) ) {
|
||||
return $this->fetch_all();
|
||||
}
|
||||
|
||||
$contents = file_get_contents( $cache_file );
|
||||
$contents = json_decode( $contents, true );
|
||||
|
||||
return $this->verify_fetched( $contents );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch info blocks info from remote.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetch_all() {
|
||||
|
||||
$info = [];
|
||||
|
||||
$res = wp_remote_get(
|
||||
self::SOURCE_URL,
|
||||
[
|
||||
'timeout' => 10,
|
||||
'user-agent' => wpforms_get_default_user_agent(),
|
||||
]
|
||||
);
|
||||
|
||||
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 = [];
|
||||
|
||||
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->get_all();
|
||||
$filtered = [];
|
||||
|
||||
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 [];
|
||||
}
|
||||
|
||||
foreach ( $data as $item ) {
|
||||
$item_id = \absint( $item['id'] );
|
||||
if ( ! empty( $item_id ) ) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = [];
|
||||
|
||||
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, [ $block_id ] );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ( in_array( $block_id, $blocks, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$blocks[] = $block_id;
|
||||
|
||||
update_option( $option_name, $blocks );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a path of the blocks cache file.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_cache_file_path() {
|
||||
|
||||
$upload_dir = wpforms_upload_dir();
|
||||
|
||||
if ( ! isset( $upload_dir['path'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$cache_dir = trailingslashit( $upload_dir['path'] ) . 'cache';
|
||||
|
||||
return wp_normalize_path( trailingslashit( $cache_dir ) . 'email-summaries.json' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and cache blocks in a file.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
public function cache_all() {
|
||||
|
||||
$file_path = $this->get_cache_file_path();
|
||||
|
||||
if ( empty( $file_path ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dir = dirname( $file_path );
|
||||
|
||||
if ( ! wp_mkdir_p( $dir ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wpforms_create_index_html_file( $dir );
|
||||
wpforms_create_upload_dir_htaccess_file();
|
||||
|
||||
$info_blocks = $this->fetch_all();
|
||||
|
||||
file_put_contents( $file_path, wp_json_encode( $info_blocks ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
|
||||
}
|
||||
}
|
||||
552
wp-content/plugins/wpforms-lite/src/Emails/Mailer.php
Normal file
552
wp-content/plugins/wpforms-lite/src/Emails/Mailer.php
Normal file
@@ -0,0 +1,552 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails;
|
||||
|
||||
use WPForms\Emails\Templates\General;
|
||||
|
||||
/**
|
||||
* Mailer class to wrap wp_mail().
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
class Mailer {
|
||||
|
||||
/**
|
||||
* Array or comma-separated list of email addresses to send message.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @var string|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.
|
||||
*
|
||||
* @uses \wpforms_decode_string()
|
||||
*
|
||||
* @since 1.5.4
|
||||
* @since 1.6.0 Deprecated param: $linebreaks. This is handled by wpforms_decode_string().
|
||||
*
|
||||
* @param string $string String that may contain tags.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function sanitize( $string = '' ) {
|
||||
|
||||
return \wpforms_decode_string( $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 = ! Helpers::is_plain_text_template();
|
||||
|
||||
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|string[] $email Array or comma-separated list of email addresses to send message.
|
||||
*
|
||||
* @return Mailer
|
||||
*/
|
||||
public function to_email( $email ) {
|
||||
|
||||
if ( is_string( $email ) ) {
|
||||
$email = explode( ',', $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 = [];
|
||||
|
||||
foreach ( (array) $this->to_email as $email ) {
|
||||
if ( ! is_email( $email ) ) {
|
||||
$errors[] = sprintf( /* translators: %1$s - namespaced class name, %2$s - invalid email. */
|
||||
esc_html__( '%1$s Invalid email address %2$s.', 'wpforms-lite' ),
|
||||
'[WPForms\Emails\Mailer]',
|
||||
$email
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $this->subject ) ) {
|
||||
$errors[] = sprintf( /* translators: %s - namespaced class name. */
|
||||
esc_html__( '%s Empty subject line.', 'wpforms-lite' ),
|
||||
'[WPForms\Emails\Mailer]'
|
||||
);
|
||||
}
|
||||
|
||||
if ( empty( $this->get_message() ) ) {
|
||||
$errors[] = sprintf( /* translators: %s - namespaced class name. */
|
||||
esc_html__( '%s Empty message.', 'wpforms-lite' ),
|
||||
'[WPForms\Emails\Mailer]'
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
[
|
||||
'to_email' => $this->to_email,
|
||||
'subject' => $this->subject,
|
||||
'message' => \wp_trim_words( $this->get_message() ),
|
||||
],
|
||||
[
|
||||
'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', [ $this, 'get_from_address' ] );
|
||||
add_filter( 'wp_mail_from_name', [ $this, 'get_from_name' ] );
|
||||
add_filter( 'wp_mail_content_type', [ $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', [ $this, 'get_from_address' ] );
|
||||
remove_filter( 'wp_mail_from_name', [ $this, 'get_from_name' ] );
|
||||
remove_filter( 'wp_mail_content_type', [ $this, 'get_content_type' ] );
|
||||
}
|
||||
}
|
||||
860
wp-content/plugins/wpforms-lite/src/Emails/Notifications.php
Normal file
860
wp-content/plugins/wpforms-lite/src/Emails/Notifications.php
Normal file
@@ -0,0 +1,860 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails;
|
||||
|
||||
use WPForms_WP_Emails;
|
||||
use WPForms\Tasks\Actions\EntryEmailsTask;
|
||||
|
||||
/**
|
||||
* Class Notifications.
|
||||
* Used to send email notifications.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Notifications extends Mailer {
|
||||
|
||||
/**
|
||||
* List of submitted fields.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $fields = [];
|
||||
|
||||
/**
|
||||
* Form data.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $form_data = [];
|
||||
|
||||
/**
|
||||
* Entry id.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $entry_id;
|
||||
|
||||
/**
|
||||
* Notification ID that is currently being processed.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $notification_id = '';
|
||||
|
||||
/**
|
||||
* Current email template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $current_template;
|
||||
|
||||
/**
|
||||
* Field template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $field_template;
|
||||
|
||||
/**
|
||||
* Default email template name.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const DEFAULT_TEMPLATE = 'classic';
|
||||
|
||||
/**
|
||||
* Plain/Text email template name.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const PLAIN_TEMPLATE = 'none';
|
||||
|
||||
/**
|
||||
* Legacy email template name.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const LEGACY_TEMPLATE = 'default';
|
||||
|
||||
/**
|
||||
* This method will initialize the class.
|
||||
*
|
||||
* Maybe use the old class for backward compatibility.
|
||||
* The old class might be removed in the future.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $template Email template name.
|
||||
*
|
||||
* @return $this|WPForms_WP_Emails
|
||||
*/
|
||||
public function init( $template = '' ) {
|
||||
|
||||
// Assign the current template.
|
||||
$this->current_template = Helpers::get_current_template_name( $template );
|
||||
|
||||
// If the old class doesn't exist, return the current class.
|
||||
// The old class might be removed in the future.
|
||||
if ( ! class_exists( 'WPForms_WP_Emails' ) ) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
// In case user is still using the old "Legacy" default template, use the old class.
|
||||
// Use the old class if the current template is "Legacy".
|
||||
if ( $this->current_template === self::LEGACY_TEMPLATE ) {
|
||||
return new WPForms_WP_Emails();
|
||||
}
|
||||
|
||||
// Plain text and other html templates will use the current class.
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe send an email right away or schedule it.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return bool Whether the email was sent successfully.
|
||||
*/
|
||||
public function send() {
|
||||
|
||||
// Leave the method if the arguments are empty.
|
||||
// We will be looking for 3 arguments: $to, $subject, $message.
|
||||
// The primary reason for this method not to take any direct arguments is to make it compatible with the parent class.
|
||||
if ( empty( func_get_args() ) || count( func_get_args() ) < 3 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't send anything if emails have been disabled.
|
||||
if ( $this->is_email_disabled() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the arguments.
|
||||
list( $to, $subject, $message ) = func_get_args();
|
||||
|
||||
// Don't send if email address is invalid.
|
||||
if ( ! is_email( $to ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires before the email is sent.
|
||||
*
|
||||
* The filter has been ported from "class-emails.php" to maintain backward compatibility
|
||||
* and avoid unintended breaking changes where these hooks may have been used.
|
||||
*
|
||||
* @since 1.8.5.2
|
||||
*
|
||||
* @param Notifications $this An instance of the "Notifications" class.
|
||||
*/
|
||||
do_action( 'wpforms_email_send_before', $this ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
|
||||
// Set the attachments to an empty array.
|
||||
// We will set the attachments later in the filter.
|
||||
$attachments = [];
|
||||
|
||||
/**
|
||||
* Filter the email data before sending.
|
||||
*
|
||||
* The filter has been ported from "class-emails.php" to maintain backward compatibility
|
||||
* and avoid unintended breaking changes where these hooks may have been used.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param array $data Email data.
|
||||
* @param Notifications $this An instance of the "Notifications" class.
|
||||
*/
|
||||
$data = (array) apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
'wpforms_emails_send_email_data',
|
||||
[
|
||||
'to' => $to,
|
||||
'subject' => $subject,
|
||||
'message' => $message,
|
||||
'headers' => $this->get_headers(),
|
||||
'attachments' => $attachments,
|
||||
],
|
||||
$this
|
||||
);
|
||||
|
||||
// Set the recipient email address.
|
||||
$this->to_email( $data['to'] );
|
||||
|
||||
// Set the email subject.
|
||||
$this->subject( $this->process_subject( $data['subject'] ) );
|
||||
|
||||
// Process the email template.
|
||||
$this->process_email_template( $data['message'] );
|
||||
|
||||
// Set the attachments to the email.
|
||||
$this->__set( 'attachments', $data['attachments'] );
|
||||
|
||||
/**
|
||||
* Filter whether to send the email in the same process.
|
||||
*
|
||||
* The filter has been ported from "class-emails.php" to maintain backward compatibility
|
||||
* and avoid unintended breaking changes where these hooks may have been used.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param bool $send_same_process Whether to send the email in the same process.
|
||||
* @param array $fields List of submitted fields.
|
||||
* @param array $entry Entry data.
|
||||
* @param array $form_data Form data.
|
||||
* @param int $entry_id Entry ID.
|
||||
* @param string $type Email type.
|
||||
*/
|
||||
$send_same_process = (bool) apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName, WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
|
||||
'wpforms_tasks_entry_emails_trigger_send_same_process',
|
||||
false,
|
||||
$this->fields,
|
||||
! empty( wpforms()->get( 'entry' ) ) ? wpforms()->get( 'entry' )->get( $this->entry_id ) : [],
|
||||
$this->form_data,
|
||||
$this->entry_id,
|
||||
'entry'
|
||||
);
|
||||
|
||||
// Send the email immediately.
|
||||
if ( $send_same_process || ! empty( $this->form_data['settings']['disable_entries'] ) ) {
|
||||
$results = parent::send();
|
||||
} else {
|
||||
$results = (bool) ( new EntryEmailsTask() )
|
||||
->params(
|
||||
$this->__get( 'to_email' ),
|
||||
$this->__get( 'subject' ),
|
||||
$this->get_message(),
|
||||
$this->get_headers(),
|
||||
$this->get_attachments()
|
||||
)
|
||||
->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after the email has been sent.
|
||||
*
|
||||
* The filter has been ported from "class-emails.php" to maintain backward compatibility
|
||||
* and avoid unintended breaking changes where these hooks may have been used.
|
||||
*
|
||||
* @since 1.8.5.2
|
||||
*
|
||||
* @param Notifications $this An instance of the "Notifications" class.
|
||||
*/
|
||||
do_action( 'wpforms_email_send_after', $this ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the email template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $message Email message.
|
||||
*/
|
||||
private function process_email_template( $message ) {
|
||||
|
||||
$template = self::get_available_templates( $this->current_template );
|
||||
|
||||
// Return if the template is not set.
|
||||
// This can happen if the template is not found or if the template class doesn't exist.
|
||||
if ( ! isset( $template['path'] ) || ! class_exists( $template['path'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the email template, i.e. WPForms\Emails\Templates\Classic.
|
||||
$this->template( new $template['path']( '', false, $this->current_template ) );
|
||||
|
||||
// Set the field template.
|
||||
$this->field_template = $this->template->get_field_template();
|
||||
|
||||
// Set the email template fields.
|
||||
$this->template->set_field( $this->process_message( $message ) );
|
||||
|
||||
$content = $this->template->get();
|
||||
|
||||
// Return if the template is empty.
|
||||
if ( ! $content ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->message( $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Format and process the email subject.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $subject Email subject.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_subject( $subject ) {
|
||||
|
||||
$subject = $this->process_tag( $subject );
|
||||
$subject = trim( str_replace( [ "\r\n", "\r", "\n" ], ' ', $subject ) );
|
||||
|
||||
return wpforms_decode_string( $subject );
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the email message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $message Email message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_message( $message ) {
|
||||
|
||||
$message = $this->process_tag( $message );
|
||||
$message = str_replace( '{all_fields}', $this->process_field_values(), $message );
|
||||
|
||||
/**
|
||||
* Filter and modify the email message content before sending.
|
||||
* This filter allows customizing the email message content for notifications.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $message The email message to be sent out.
|
||||
* @param string $template The email template name.
|
||||
* @param Notifications $this The instance of the "Notifications" class.
|
||||
*/
|
||||
$message = apply_filters( 'wpforms_emails_notifications_message', $message, $this->current_template, $this );
|
||||
|
||||
// Leave early if the template is set to plain text.
|
||||
if ( Helpers::is_plain_text_template( $this->current_template ) ) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
return make_clickable( str_replace( "\r\n", '<br/>', $message ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the field values.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_field_values() {
|
||||
|
||||
// If fields are empty, return an empty message.
|
||||
if ( empty( $this->fields ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// If no message was generated, create an empty message.
|
||||
$default_message = esc_html__( 'An empty form was submitted.', 'wpforms-lite' );
|
||||
|
||||
/**
|
||||
* Filter whether to display empty fields in the email.
|
||||
*
|
||||
* @since 1.8.5
|
||||
* @deprecated 1.8.5.2
|
||||
*
|
||||
* @param bool $show_empty_fields Whether to display empty fields in the email.
|
||||
*/
|
||||
$show_empty_fields = apply_filters_deprecated( // phpcs:disable WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
|
||||
'wpforms_emails_notifications_display_empty_fields',
|
||||
[ false ],
|
||||
'1.8.5.2 of the WPForms plugin',
|
||||
'wpforms_email_display_empty_fields'
|
||||
);
|
||||
|
||||
/** This filter is documented in /includes/emails/class-emails.php */
|
||||
$show_empty_fields = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
'wpforms_email_display_empty_fields',
|
||||
false
|
||||
);
|
||||
|
||||
// Process either plain text or HTML message based on the template type.
|
||||
if ( Helpers::is_plain_text_template( $this->current_template ) ) {
|
||||
$message = $this->process_plain_message( $show_empty_fields );
|
||||
} else {
|
||||
$message = $this->process_html_message( $show_empty_fields );
|
||||
}
|
||||
|
||||
return empty( $message ) ? $default_message : $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the plain text email message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param bool $show_empty_fields Whether to display empty fields in the email.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_plain_message( $show_empty_fields = false ) {
|
||||
|
||||
$message = '';
|
||||
|
||||
foreach ( $this->fields as $field ) {
|
||||
|
||||
if ( ! $show_empty_fields && ( ! isset( $field['value'] ) || (string) $field['value'] === '' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field_name = isset( $field['name'] ) ? $field['name'] : '';
|
||||
$field_val = empty( $field['value'] ) && ! is_numeric( $field['value'] ) ? esc_html__( '(empty)', 'wpforms-lite' ) : $field['value'];
|
||||
|
||||
// Set a default field name if empty.
|
||||
if ( empty( $field_name ) && $field_name !== null ) {
|
||||
$field_name = $this->get_default_field_name( $field['id'] );
|
||||
}
|
||||
|
||||
$message .= '--- ' . $field_name . " ---\r\n\r\n";
|
||||
$field_value = wpforms_decode_string( $field_val ) . "\r\n\r\n";
|
||||
|
||||
/**
|
||||
* Filter the field value before it is added to the email message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $field_value Field value.
|
||||
* @param array $field Field data.
|
||||
* @param array $form_data Form data.
|
||||
*/
|
||||
$message .= apply_filters( 'wpforms_emails_notifications_plaintext_field_value', $field_value, $field, $this->form_data );
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the HTML email message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param bool $show_empty_fields Whether to display empty fields in the email.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_html_message( $show_empty_fields = false ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity
|
||||
|
||||
$message = '';
|
||||
|
||||
/**
|
||||
* Filter the list of field types to display in the email.
|
||||
*
|
||||
* @since 1.8.5
|
||||
* @deprecated 1.8.5.2
|
||||
*
|
||||
* @param array $other_fields List of field types.
|
||||
* @param array $form_data Form data.
|
||||
*/
|
||||
$other_fields = apply_filters_deprecated( // phpcs:disable WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
|
||||
'wpforms_emails_notifications_display_other_fields',
|
||||
[ [], $this->form_data ],
|
||||
'1.8.5.2 of the WPForms plugin',
|
||||
'wpforms_email_display_other_fields'
|
||||
);
|
||||
|
||||
/** This filter is documented in /includes/emails/class-emails.php */
|
||||
$other_fields = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
'wpforms_email_display_other_fields',
|
||||
[],
|
||||
$this
|
||||
);
|
||||
|
||||
foreach ( $this->form_data['fields'] as $field_id => $field ) {
|
||||
$field_type = ! empty( $field['type'] ) ? $field['type'] : '';
|
||||
|
||||
// Check if the field is empty in $this->fields.
|
||||
if ( empty( $this->fields[ $field_id ] ) ) {
|
||||
// Check if the field type is in $other_fields, otherwise skip.
|
||||
if ( empty( $other_fields ) || ! in_array( $field_type, $other_fields, true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle specific field types.
|
||||
list( $field_name, $field_val ) = $this->process_special_field_values( $field );
|
||||
} else {
|
||||
// Handle fields that are not empty in $this->fields.
|
||||
if ( ! $show_empty_fields && ( ! isset( $this->fields[ $field_id ]['value'] ) || (string) $this->fields[ $field_id ]['value'] === '' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field_name = isset( $this->fields[ $field_id ]['name'] ) ? $this->fields[ $field_id ]['name'] : '';
|
||||
$field_val = empty( $this->fields[ $field_id ]['value'] ) && ! is_numeric( $this->fields[ $field_id ]['value'] ) ? '<em>' . esc_html__( '(empty)', 'wpforms-lite' ) . '</em>' : $this->fields[ $field_id ]['value'];
|
||||
}
|
||||
|
||||
// Set a default field name if empty.
|
||||
if ( empty( $field_name ) && $field_name !== null ) {
|
||||
$field_name = $this->get_default_field_name( $field_id );
|
||||
}
|
||||
|
||||
/** This filter is documented in src/SmartTags/SmartTag/FieldHtmlId.php.*/
|
||||
$field_val = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
'wpforms_html_field_value',
|
||||
$field_val,
|
||||
isset( $this->fields[ $field_id ] ) ? $this->fields[ $field_id ] : $field,
|
||||
$this->form_data,
|
||||
'email-html'
|
||||
);
|
||||
|
||||
// Append the field item to the message.
|
||||
$message .= str_replace(
|
||||
[ '{field_type}', '{field_name}', '{field_value}' ],
|
||||
[ $field_type, $field_name, $field_val ],
|
||||
$this->field_template
|
||||
);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a smart tag.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $input Smart tag.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_tag( $input = '' ) {
|
||||
|
||||
return wpforms_process_smart_tags( $input, $this->form_data, $this->fields, $this->entry_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Process special field types.
|
||||
* This is used for fields such as Page Break, HTML, Content, etc.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param array $field Field data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function process_special_field_values( $field ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity
|
||||
|
||||
$field_name = null;
|
||||
$field_val = null;
|
||||
|
||||
// Use a switch-case statement to handle specific field types.
|
||||
switch ( $field['type'] ) {
|
||||
case 'divider':
|
||||
$field_name = ! empty( $field['label'] ) ? str_repeat( '—', 3 ) . ' ' . $field['label'] . ' ' . str_repeat( '—', 3 ) : null;
|
||||
$field_val = ! empty( $field['description'] ) ? $field['description'] : '';
|
||||
break;
|
||||
|
||||
case 'pagebreak':
|
||||
// Skip if position is 'bottom'.
|
||||
if ( ! empty( $field['position'] ) && $field['position'] === 'bottom' ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$title = ! empty( $field['title'] ) ? $field['title'] : esc_html__( 'Page Break', 'wpforms-lite' );
|
||||
$field_name = str_repeat( '—', 6 ) . ' ' . $title . ' ' . str_repeat( '—', 6 );
|
||||
break;
|
||||
|
||||
case 'html':
|
||||
// Skip if the field is conditionally hidden.
|
||||
if ( $this->is_field_conditionally_hidden( $field['id'] ) ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$field_name = ! empty( $field['name'] ) ? $field['name'] : esc_html__( 'HTML / Code Block', 'wpforms-lite' );
|
||||
$field_val = $field['code'];
|
||||
break;
|
||||
|
||||
case 'content':
|
||||
// Skip if the field is conditionally hidden.
|
||||
if ( $this->is_field_conditionally_hidden( $field['id'] ) ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$field_name = esc_html__( 'Content', 'wpforms-lite' );
|
||||
$field_val = $field['content'];
|
||||
break;
|
||||
|
||||
default:
|
||||
$field_name = '';
|
||||
$field_val = '';
|
||||
break;
|
||||
}
|
||||
|
||||
return [ $field_name, $field_val ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if conditional_logic is enabled and a field is conditionally hidden in the form.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param int $field_id The ID of the field to check.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_field_conditionally_hidden( $field_id ) {
|
||||
|
||||
return ! empty( $this->form_data['fields'][ $field_id ]['conditionals'] ) && ! wpforms_conditional_logic_fields()->field_is_visible( $this->form_data, $field_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the email reply to address.
|
||||
* This method has been overridden to add support for the Reply-to Name.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_reply_to_address() {
|
||||
|
||||
$reply_to = $this->__get( 'reply_to' );
|
||||
$reply_to_name = false;
|
||||
|
||||
if ( ! empty( $reply_to ) ) {
|
||||
|
||||
// Optional custom format with a Reply-to Name specified: John Doe <john@doe.com>
|
||||
// - starts with anything,
|
||||
// - followed by space,
|
||||
// - ends with <anything> (expected to be an email, validated later).
|
||||
$regex = '/^(.+) (<.+>)$/';
|
||||
$matches = [];
|
||||
|
||||
if ( preg_match( $regex, $reply_to, $matches ) ) {
|
||||
$reply_to_name = $this->sanitize( $matches[1] );
|
||||
$reply_to = trim( $matches[2], '<> ' );
|
||||
}
|
||||
|
||||
$reply_to = $this->process_tag( $reply_to );
|
||||
|
||||
if ( ! is_email( $reply_to ) ) {
|
||||
$reply_to = false;
|
||||
$reply_to_name = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $reply_to_name ) {
|
||||
$reply_to = "$reply_to_name <{$reply_to}>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the email reply-to address.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $reply_to Email reply-to address.
|
||||
* @param object $this Instance of the Notifications class.
|
||||
*/
|
||||
return apply_filters( 'wpforms_emails_notifications_get_reply_to_address', $reply_to, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the string.
|
||||
* This method has been overridden to add support for processing smart tags.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $input String to sanitize and process for smart tags.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function sanitize( $input = '' ) {
|
||||
|
||||
return wpforms_decode_string( $this->process_tag( $input ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the email content type.
|
||||
* This method has been overridden to better declare email template assigned to each notification.
|
||||
*
|
||||
* @since 1.8.5.2
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_content_type() {
|
||||
|
||||
$content_type = 'text/html';
|
||||
|
||||
if ( Helpers::is_plain_text_template( $this->current_template ) ) {
|
||||
$content_type = 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the email content type.
|
||||
*
|
||||
* @since 1.8.5.2
|
||||
*
|
||||
* @param string $content_type The email content type.
|
||||
* @param Notifications $this An instance of the "Notifications" class.
|
||||
*/
|
||||
$content_type = apply_filters( 'wpforms_emails_notifications_get_content_type', $content_type, $this );
|
||||
|
||||
// Set the content type.
|
||||
$this->__set( 'content_type', $content_type );
|
||||
|
||||
// Return the content type.
|
||||
return $content_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if all emails are disabled.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_email_disabled() {
|
||||
|
||||
/**
|
||||
* Filter to control email disabling.
|
||||
*
|
||||
* The "Notifications" class is designed to mirror the properties and methods
|
||||
* provided by the "WPForms_WP_Emails" class for backward compatibility.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param bool $is_disabled Whether to disable all emails.
|
||||
* @param Notifications $this An instance of the "Notifications" class.
|
||||
*/
|
||||
return (bool) apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
'wpforms_disable_all_emails',
|
||||
false,
|
||||
$this
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default field name as a fallback.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param int $field_id Field ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_default_field_name( $field_id ) {
|
||||
|
||||
return sprintf( /* translators: %1$d - field ID. */
|
||||
esc_html__( 'Field ID #%1$d', 'wpforms-lite' ),
|
||||
absint( $field_id )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of available email templates.
|
||||
*
|
||||
* Given a template name, this method will return the template data.
|
||||
* If no template name is provided, all available templates will be returned.
|
||||
*
|
||||
* Templates will go through a conditional check to make sure they are available for the current plugin edition.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $template Template name. If empty, all available templates will be returned.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_available_templates( $template = '' ) {
|
||||
|
||||
$templates = self::get_all_templates();
|
||||
|
||||
// Filter the list of available email templates based on the edition of WPForms.
|
||||
if ( ! wpforms()->is_pro() ) {
|
||||
$templates = array_filter(
|
||||
$templates,
|
||||
static function ( $instance ) {
|
||||
|
||||
return ! $instance['is_pro'];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return isset( $templates[ $template ] ) ? $templates[ $template ] : $templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of all email templates.
|
||||
*
|
||||
* Given the name of a template, this method will return the template data.
|
||||
* If the template is not found, all available templates will be returned.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $template Template name. If empty, all templates will be returned.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_all_templates( $template = '' ) {
|
||||
|
||||
$templates = [
|
||||
'classic' => [
|
||||
'name' => esc_html__( 'Classic', 'wpforms-lite' ),
|
||||
'path' => __NAMESPACE__ . '\Templates\Classic',
|
||||
'is_pro' => false,
|
||||
],
|
||||
'compact' => [
|
||||
'name' => esc_html__( 'Compact', 'wpforms-lite' ),
|
||||
'path' => __NAMESPACE__ . '\Templates\Compact',
|
||||
'is_pro' => false,
|
||||
],
|
||||
'modern' => [
|
||||
'name' => esc_html__( 'Modern', 'wpforms-lite' ),
|
||||
'path' => 'WPForms\Pro\Emails\Templates\Modern',
|
||||
'is_pro' => true,
|
||||
],
|
||||
'elegant' => [
|
||||
'name' => esc_html__( 'Elegant', 'wpforms-lite' ),
|
||||
'path' => 'WPForms\Pro\Emails\Templates\Elegant',
|
||||
'is_pro' => true,
|
||||
],
|
||||
'tech' => [
|
||||
'name' => esc_html__( 'Tech', 'wpforms-lite' ),
|
||||
'path' => 'WPForms\Pro\Emails\Templates\Tech',
|
||||
'is_pro' => true,
|
||||
],
|
||||
'none' => [
|
||||
'name' => esc_html__( 'Plain Text', 'wpforms-lite' ),
|
||||
'path' => __NAMESPACE__ . '\Templates\Plain',
|
||||
'is_pro' => false,
|
||||
],
|
||||
];
|
||||
|
||||
// Make sure the current user can preview templates.
|
||||
if ( wpforms_current_user_can() ) {
|
||||
// Add a preview key to each template.
|
||||
foreach ( $templates as $key => &$tmpl ) {
|
||||
$tmpl['preview'] = wp_nonce_url(
|
||||
add_query_arg(
|
||||
[
|
||||
'wpforms_email_preview' => '1',
|
||||
'wpforms_email_template' => $key,
|
||||
],
|
||||
admin_url()
|
||||
),
|
||||
Preview::PREVIEW_NONCE_NAME
|
||||
);
|
||||
}
|
||||
|
||||
// Make sure to unset the reference to avoid unintended changes later.
|
||||
unset( $tmpl );
|
||||
}
|
||||
|
||||
return isset( $templates[ $template ] ) ? $templates[ $template ] : $templates;
|
||||
}
|
||||
}
|
||||
216
wp-content/plugins/wpforms-lite/src/Emails/Preview.php
Normal file
216
wp-content/plugins/wpforms-lite/src/Emails/Preview.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails;
|
||||
|
||||
/**
|
||||
* Class Preview.
|
||||
* Handles previewing email templates.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Preview {
|
||||
|
||||
/**
|
||||
* List of preview fields.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $fields = [];
|
||||
|
||||
/**
|
||||
* Current email template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $current_template;
|
||||
|
||||
/**
|
||||
* Field template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $field_template;
|
||||
|
||||
/**
|
||||
* Content is plain text type.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $plain_text;
|
||||
|
||||
/**
|
||||
* Preview nonce name.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const PREVIEW_NONCE_NAME = 'wpforms_email_preview';
|
||||
|
||||
/**
|
||||
* Initialize class.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
public function init() {
|
||||
|
||||
// Leave if user can't access.
|
||||
if ( ! wpforms_current_user_can() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Leave early if nonce verification failed.
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), self::PREVIEW_NONCE_NAME ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Leave early if preview is not requested.
|
||||
if ( ! isset( $_GET['wpforms_email_preview'], $_GET['wpforms_email_template'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->current_template = sanitize_key( $_GET['wpforms_email_template'] );
|
||||
$this->plain_text = $this->current_template === 'none';
|
||||
|
||||
$this->preview();
|
||||
}
|
||||
|
||||
/**
|
||||
* Preview email template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
private function preview() {
|
||||
|
||||
$template = Notifications::get_available_templates( $this->current_template );
|
||||
|
||||
/**
|
||||
* Filter the email template to be previewed.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param array $template Email template.
|
||||
*/
|
||||
$template = apply_filters( 'wpforms_emails_preview_template', $template );
|
||||
|
||||
// Redirect to the email settings page if the template is not set.
|
||||
if ( ! isset( $template['path'] ) || ! class_exists( $template['path'] ) ) {
|
||||
wp_safe_redirect(
|
||||
add_query_arg(
|
||||
[
|
||||
'page' => 'wpforms-settings',
|
||||
'view' => 'email',
|
||||
],
|
||||
admin_url( 'admin.php' )
|
||||
)
|
||||
);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Set the email template, i.e. WPForms\Emails\Templates\Classic.
|
||||
$template = new $template['path']( '', true );
|
||||
|
||||
// Set the field template.
|
||||
// This is used to replace the placeholders in the email template.
|
||||
$this->field_template = $template->get_field_template();
|
||||
|
||||
// Set the email template fields.
|
||||
$template->set_field( $this->get_placeholder_message() );
|
||||
|
||||
// Get the email template content.
|
||||
$content = $template->get();
|
||||
|
||||
// Return if the template is empty.
|
||||
if ( ! $content ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Echo the email template content.
|
||||
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
|
||||
exit; // No need to continue. WordPress will die() after this.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get preview content.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string Placeholder message.
|
||||
*/
|
||||
private function get_placeholder_message() {
|
||||
|
||||
$this->fields = [
|
||||
[
|
||||
'type' => 'name',
|
||||
'name' => __( 'Name', 'wpforms-lite' ),
|
||||
'value' => 'Sullie Eloso',
|
||||
],
|
||||
[
|
||||
'type' => 'email',
|
||||
'name' => __( 'Email', 'wpforms-lite' ),
|
||||
'value' => 'sullie@wpforms.com',
|
||||
],
|
||||
[
|
||||
'type' => 'textarea',
|
||||
'name' => __( 'Comment or Message', 'wpforms-lite' ),
|
||||
'value' => "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Odio ut sem nulla pharetra diam sit amet. Sed risus pretium quam vulputate dignissim suspendisse in est ante. Risus ultricies tristique nulla aliquet enim tortor at auctor. Nisl tincidunt eget nullam non nisi est sit amet facilisis. Duis at tellus at urna condimentum mattis pellentesque id nibh. Curabitur vitae nunc sed velit dignissim.\r\n\r\nLeo urna molestie at elementum eu facilisis sed odio. Scelerisque mauris pellentesque pulvinar pellentesque habitant morbi. Volutpat maecenas volutpat blandit aliquam. Libero id faucibus nisl tincidunt. Et malesuada fames ac turpis egestas.",
|
||||
],
|
||||
];
|
||||
|
||||
// Early return if the template is plain text.
|
||||
if ( $this->plain_text ) {
|
||||
return $this->process_plain_message();
|
||||
}
|
||||
|
||||
return $this->process_html_message();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the HTML email message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_html_message() {
|
||||
|
||||
$message = '';
|
||||
|
||||
foreach ( $this->fields as $field ) {
|
||||
$message .= str_replace(
|
||||
[ '{field_type}', '{field_name}', '{field_value}', "\r\n" ],
|
||||
[ $field['type'], $field['name'], $field['value'], '<br>' ],
|
||||
$this->field_template
|
||||
);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the plain text email message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function process_plain_message() {
|
||||
|
||||
$message = '';
|
||||
|
||||
foreach ( $this->fields as $field ) {
|
||||
$message .= '--- ' . $field['name'] . " ---\r\n\r\n" . str_replace( [ "\n", "\r" ], '', $field['value'] ) . "\r\n\r\n";
|
||||
}
|
||||
|
||||
return nl2br( $message );
|
||||
}
|
||||
}
|
||||
136
wp-content/plugins/wpforms-lite/src/Emails/Styler.php
Normal file
136
wp-content/plugins/wpforms-lite/src/Emails/Styler.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails;
|
||||
|
||||
use WPForms\Vendor\TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
|
||||
use WPForms\Helpers\Templates;
|
||||
|
||||
/**
|
||||
* Styler class inline style email templates.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
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 : [];
|
||||
$this->style_overrides = is_array( $style_overrides ) ? $style_overrides : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Template style overrides.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_style_overrides() {
|
||||
|
||||
$defaults = Helpers::get_current_template_style_overrides();
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
345
wp-content/plugins/wpforms-lite/src/Emails/Summaries.php
Normal file
345
wp-content/plugins/wpforms-lite/src/Emails/Summaries.php
Normal file
@@ -0,0 +1,345 @@
|
||||
<?php
|
||||
namespace WPForms\Emails;
|
||||
|
||||
/**
|
||||
* Email Summaries main class.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
class Summaries {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->hooks();
|
||||
|
||||
$summaries_disabled = $this->is_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', [ $this, 'disable_summaries_setting' ] );
|
||||
add_action( 'wpforms_settings_updated', [ $this, 'deregister_fetch_info_blocks_task' ] );
|
||||
|
||||
if ( ! $this->is_disabled() ) {
|
||||
add_action( 'init', [ $this, 'preview' ] );
|
||||
add_filter( 'cron_schedules', [ $this, 'add_weekly_cron_schedule' ] );
|
||||
add_action( 'wpforms_email_summaries_cron', [ $this, 'cron' ] );
|
||||
add_filter( 'wpforms_tasks_get_tasks', [ $this, 'register_fetch_info_blocks_task' ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Email Summaries are disabled in settings.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_disabled() {
|
||||
|
||||
return (bool) apply_filters( 'wpforms_emails_summaries_is_disabled', (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 ) {
|
||||
|
||||
if ( (bool) apply_filters( 'wpforms_emails_summaries_is_disabled', false ) ) {
|
||||
return $settings;
|
||||
}
|
||||
|
||||
$url = add_query_arg(
|
||||
[
|
||||
'wpforms_email_template' => 'summary',
|
||||
'wpforms_email_preview' => '1',
|
||||
],
|
||||
admin_url()
|
||||
);
|
||||
|
||||
$desc = esc_html__( 'Disable Email Summaries weekly delivery.', 'wpforms-lite' );
|
||||
|
||||
if ( ! $this->is_disabled() ) {
|
||||
$desc .= ' <a href="' . $url . '" target="_blank">' . esc_html__( 'View Email Summary Example', 'wpforms-lite' ) . '</a>.';
|
||||
}
|
||||
|
||||
// Get the uninstall data setting.
|
||||
$uninstall_data = $settings['misc']['uninstall-data'];
|
||||
|
||||
// Remove the uninstall data setting.
|
||||
unset( $settings['misc']['uninstall-data'] );
|
||||
|
||||
// Add the email summaries setting.
|
||||
$settings['misc']['email-summaries-disable'] = [
|
||||
'id' => 'email-summaries-disable',
|
||||
'name' => esc_html__( 'Disable Email Summaries', 'wpforms-lite' ),
|
||||
'desc' => $desc,
|
||||
'type' => 'toggle',
|
||||
'status' => true,
|
||||
];
|
||||
|
||||
// Add the uninstall data setting to the end.
|
||||
$settings['misc']['uninstall-data'] = $uninstall_data;
|
||||
|
||||
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 ( $_GET['wpforms_email_template'] !== 'summary' ) { // phpcs:ignore
|
||||
return;
|
||||
}
|
||||
|
||||
$args = [
|
||||
'body' => [
|
||||
'entries' => $this->get_entries(),
|
||||
'info_block' => ( new InfoBlocks() )->get_next(),
|
||||
],
|
||||
];
|
||||
|
||||
$template = ( new Templates\Summary() )->set_args( $args );
|
||||
|
||||
/**
|
||||
* Filters the summaries email template.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @param Templates\Summary $template Default summaries email template.
|
||||
*/
|
||||
$template = apply_filters( 'wpforms_emails_summaries_template', $template );
|
||||
|
||||
$content = $template->get();
|
||||
|
||||
if ( Helpers::is_plain_text_template() ) {
|
||||
$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'] = [
|
||||
'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 = [
|
||||
'body' => [
|
||||
'entries' => $entries,
|
||||
'info_block' => $next_block,
|
||||
],
|
||||
];
|
||||
|
||||
$template = ( new Templates\Summary() )->set_args( $args );
|
||||
|
||||
/** This filter is documented in preview() method above. */
|
||||
$template = apply_filters( 'wpforms_emails_summaries_template', $template );
|
||||
|
||||
$content = $template->get();
|
||||
|
||||
if ( ! $content ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parsed_home_url = wp_parse_url( home_url() );
|
||||
$site_domain = $parsed_home_url['host'];
|
||||
|
||||
if ( is_multisite() && isset( $parsed_home_url['path'] ) ) {
|
||||
$site_domain .= $parsed_home_url['path'];
|
||||
}
|
||||
|
||||
$subject = sprintf(
|
||||
/* translators: %s - site domain. */
|
||||
esc_html__( 'Your Weekly WPForms Summary for %s', 'wpforms-lite' ),
|
||||
$site_domain
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the summaries email subject.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @param string $subject Default summaries email subject.
|
||||
*/
|
||||
$subject = apply_filters( 'wpforms_emails_summaries_cron_subject', $subject );
|
||||
|
||||
/**
|
||||
* Filters the summaries recipient email address.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @param string $option Default summaries recipient email address.
|
||||
*/
|
||||
$to_email = apply_filters( 'wpforms_emails_summaries_cron_to_email', get_option( 'admin_email' ) );
|
||||
|
||||
$sent = ( new Mailer() )
|
||||
->template( $template )
|
||||
->subject( $subject )
|
||||
->to_email( $to_email )
|
||||
->send();
|
||||
|
||||
if ( $sent === true ) {
|
||||
$info_blocks->register_sent( $next_block );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get form entries.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_entries() {
|
||||
|
||||
if ( wpforms()->is_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Action Scheduler task to fetch and cache Info Blocks.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*
|
||||
* @param \WPForms\Tasks\Task[] $tasks List of task classes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function register_fetch_info_blocks_task( $tasks ) {
|
||||
|
||||
$tasks[] = FetchInfoBlocksTask::class;
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deregister Action Scheduler task to fetch and cache Info Blocks.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
public function deregister_fetch_info_blocks_task() {
|
||||
|
||||
if ( ! $this->is_disabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Deregister the task.
|
||||
( new FetchInfoBlocksTask() )->cancel();
|
||||
|
||||
// Delete last run time record.
|
||||
delete_option( FetchInfoBlocksTask::LAST_RUN );
|
||||
|
||||
// Remove the cache file if it exists.
|
||||
$file_name = ( new InfoBlocks() )->get_cache_file_path();
|
||||
if ( file_exists( $file_name ) ) {
|
||||
@unlink( $file_name ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails\Templates;
|
||||
|
||||
/**
|
||||
* Class Classic.
|
||||
* This is an updated version of our standard email template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Classic extends Notifications {
|
||||
|
||||
/**
|
||||
* Template slug.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TEMPLATE_SLUG = 'classic';
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails\Templates;
|
||||
|
||||
/**
|
||||
* Class Compact.
|
||||
* As the name suggests, it's a compact variant of the Classic template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Compact extends Notifications {
|
||||
|
||||
/**
|
||||
* Template slug.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TEMPLATE_SLUG = 'compact';
|
||||
}
|
||||
465
wp-content/plugins/wpforms-lite/src/Emails/Templates/General.php
Normal file
465
wp-content/plugins/wpforms-lite/src/Emails/Templates/General.php
Normal file
@@ -0,0 +1,465 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails\Templates;
|
||||
|
||||
use WPForms\Emails\Helpers;
|
||||
use WPForms\Emails\Styler;
|
||||
use WPForms\Helpers\Templates;
|
||||
|
||||
/**
|
||||
* Base email template class.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
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 = Helpers::is_plain_text_template();
|
||||
|
||||
$this->set_initial_args();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set initial arguments to use in a template.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
public function set_initial_args() {
|
||||
|
||||
$header_args = [
|
||||
'title' => \esc_html__( 'WPForms', 'wpforms-lite' ),
|
||||
];
|
||||
|
||||
if ( ! $this->plain_text ) {
|
||||
$header_args['header_image'] = $this->get_header_image();
|
||||
}
|
||||
|
||||
$args = [
|
||||
'header' => $header_args,
|
||||
'body' => [ 'message' => $this->get_message() ],
|
||||
'footer' => [],
|
||||
'style' => [],
|
||||
];
|
||||
|
||||
$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 ) : [];
|
||||
}
|
||||
|
||||
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 ] = [];
|
||||
}
|
||||
|
||||
$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 = [
|
||||
* '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(
|
||||
* [
|
||||
* 'style' => [
|
||||
* '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 = [
|
||||
'url' => wpforms_setting( 'email-header-image' ),
|
||||
'size' => wpforms_setting( 'email-header-image-size', 'medium' ),
|
||||
];
|
||||
|
||||
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 = [
|
||||
'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 = [
|
||||
'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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails\Templates;
|
||||
|
||||
use WPForms\Emails\Helpers;
|
||||
|
||||
/**
|
||||
* Class Notifications.
|
||||
*
|
||||
* This is a wrapper for the General template to extend it.
|
||||
* This is the default template for all notifications.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Notifications extends General {
|
||||
|
||||
/**
|
||||
* Whether is preview or not.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $is_preview = false;
|
||||
|
||||
/**
|
||||
* Initialize class.
|
||||
* In case the class instance meant for preview, we need to set the plain text property to false.
|
||||
*
|
||||
* @since 1.8.5
|
||||
* @since 1.8.5.2 New param was added, $current_template
|
||||
*
|
||||
* @param string $message Optional. Message.
|
||||
* @param bool $is_preview Optional. Whether is preview or not. Default false.
|
||||
* @param string $current_template Optional. The name of the email template to evaluate.
|
||||
*/
|
||||
public function __construct( $message = '', $is_preview = false, $current_template = '' ) {
|
||||
|
||||
parent::__construct( $message );
|
||||
|
||||
$this->is_preview = $is_preview;
|
||||
$this->plain_text = ! $is_preview && Helpers::is_plain_text_template( $current_template );
|
||||
|
||||
// Call the parent method after to set the correct header properties.
|
||||
$this->set_initial_args();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set template message.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $message Message.
|
||||
*/
|
||||
public function set_field( $message ) {
|
||||
|
||||
// Leave if not a string.
|
||||
if ( ! is_string( $message ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the template message.
|
||||
$this->set_args(
|
||||
[
|
||||
'body' => [
|
||||
'message' => $message,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get field template.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_template() {
|
||||
|
||||
return $this->get_content_part( 'field' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails\Templates;
|
||||
|
||||
/**
|
||||
* Class Plain.
|
||||
* This template is used for the plain text email notifications.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*/
|
||||
class Plain extends Notifications {
|
||||
|
||||
/**
|
||||
* Template slug.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TEMPLATE_SLUG = 'plain';
|
||||
|
||||
/**
|
||||
* Initialize class.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param mixed ...$args Variable number of parameters to be passed to the parent class.
|
||||
*/
|
||||
public function __construct( ...$args ) {
|
||||
|
||||
// Ensure preparation for initialization by calling the parent class constructor with all passed arguments.
|
||||
parent::__construct( ...$args );
|
||||
|
||||
// We already know that this is a plain text template. No need for further evaluation.
|
||||
$this->plain_text = true;
|
||||
|
||||
// Call the parent method after to set the correct header properties.
|
||||
$this->set_initial_args();
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe prepare the content for the preview.
|
||||
*
|
||||
* @since 1.8.5
|
||||
*
|
||||
* @param string $content Content with no styling applied.
|
||||
*/
|
||||
protected function save_styled( $content ) {
|
||||
|
||||
// Leave early if we are not in preview mode.
|
||||
if ( ! $this->is_preview ) {
|
||||
// Call the parent method to handle the proper styling.
|
||||
parent::save_styled( $content );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Leave if content is empty.
|
||||
if ( empty( $content ) ) {
|
||||
$this->content = '';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop here as we don't need to apply any styling for the preview.
|
||||
// The only exception here is to keep the break tags to maintain the readability.
|
||||
$this->content = wp_kses( $content, [ 'br' => [] ] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace WPForms\Emails\Templates;
|
||||
|
||||
/**
|
||||
* Email Summaries email template class.
|
||||
*
|
||||
* @since 1.5.4
|
||||
*/
|
||||
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 = [
|
||||
'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(
|
||||
[
|
||||
'style' => [
|
||||
'header_image_max_width' => '45%',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// Set specific WPForms logo width in pixels for MS Outlook and old email clients.
|
||||
return [
|
||||
'url' => \WPFORMS_PLUGIN_URL . 'assets/images/logo.png',
|
||||
'width' => 250,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user