Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/wpforms-.../src/Emails
File: Helpers.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Emails;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Helper class for the email templates.
[5] Fix | Delete
*
[6] Fix | Delete
* @since 1.8.5
[7] Fix | Delete
*/
[8] Fix | Delete
class Helpers {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Get Email template choices.
[12] Fix | Delete
*
[13] Fix | Delete
* @since 1.8.5
[14] Fix | Delete
*
[15] Fix | Delete
* @param bool $include_legacy Whether to include a Legacy template into the list.
[16] Fix | Delete
*
[17] Fix | Delete
* @return array
[18] Fix | Delete
*/
[19] Fix | Delete
public static function get_email_template_choices( $include_legacy = true ) {
[20] Fix | Delete
[21] Fix | Delete
$choices = [];
[22] Fix | Delete
$templates = Notifications::get_all_templates();
[23] Fix | Delete
[24] Fix | Delete
// If there are no templates, return empty choices.
[25] Fix | Delete
if ( empty( $templates ) || ! is_array( $templates ) ) {
[26] Fix | Delete
return $choices;
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
// Add legacy template to the choices as the first option.
[30] Fix | Delete
if ( $include_legacy && self::is_legacy_html_template() ) {
[31] Fix | Delete
$choices['default'] = [
[32] Fix | Delete
'name' => esc_html__( 'Legacy', 'wpforms-lite' ),
[33] Fix | Delete
];
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
// Iterate through templates and build $choices array.
[37] Fix | Delete
foreach ( $templates as $template_key => $template ) {
[38] Fix | Delete
// Skip if the template name is empty.
[39] Fix | Delete
if ( empty( $template['name'] ) ) {
[40] Fix | Delete
continue;
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$choices[ $template_key ] = $template;
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
return $choices;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Retrieves the current email template name.
[51] Fix | Delete
* If the current template is not found, the default template will be returned.
[52] Fix | Delete
*
[53] Fix | Delete
* This method respects backward compatibility and will return the old "Legacy" template if it is set.
[54] Fix | Delete
* If a template name is provided, the function will attempt to validate and return it. If validation fails,
[55] Fix | Delete
* it will default to the email template name "Classic."
[56] Fix | Delete
*
[57] Fix | Delete
* @since 1.8.5
[58] Fix | Delete
*
[59] Fix | Delete
* @param string $template_name Optional. The name of the email template to evaluate.
[60] Fix | Delete
*
[61] Fix | Delete
* @return string
[62] Fix | Delete
*/
[63] Fix | Delete
public static function get_current_template_name( $template_name = '' ) {
[64] Fix | Delete
[65] Fix | Delete
// If a template name is provided, sanitize it. Otherwise, use the default template name from settings.
[66] Fix | Delete
$settings_template = wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE );
[67] Fix | Delete
$template = ! empty( $template_name ) ? trim( sanitize_text_field( $template_name ) ) : $settings_template;
[68] Fix | Delete
[69] Fix | Delete
// If the user has set the legacy template, return it.
[70] Fix | Delete
if ( $template === Notifications::LEGACY_TEMPLATE && self::is_legacy_html_template() ) {
[71] Fix | Delete
return Notifications::LEGACY_TEMPLATE;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
// In case the user has changed the general settings template,
[75] Fix | Delete
// but the form submitted still uses the “Legacy” template,
[76] Fix | Delete
// we need to revert to the general settings template.
[77] Fix | Delete
if ( $template === Notifications::LEGACY_TEMPLATE && ! self::is_legacy_html_template() ) {
[78] Fix | Delete
$template = wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
// Check if the given template name is valid by looking into available templates.
[82] Fix | Delete
$current_template = Notifications::get_available_templates( $template );
[83] Fix | Delete
[84] Fix | Delete
// If the current template is not found or its corresponding class does not exist, return the default template.
[85] Fix | Delete
if ( ! isset( $current_template['path'] ) || ! class_exists( $current_template['path'] ) ) {
[86] Fix | Delete
[87] Fix | Delete
// Last resort, check if the template defined in the settings can be used.
[88] Fix | Delete
// This would be helpful when user downgrades from Pro to Lite version and the template is not available anymore.
[89] Fix | Delete
if ( isset( $current_template[ $settings_template ] ) ) {
[90] Fix | Delete
return $settings_template;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
return Notifications::DEFAULT_TEMPLATE;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
// The provided template is valid, so return it.
[97] Fix | Delete
return $template;
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* Get the current email template class path.
[102] Fix | Delete
*
[103] Fix | Delete
* @since 1.8.5
[104] Fix | Delete
*
[105] Fix | Delete
* @param string $template_name Optional. The name of the email template to evaluate.
[106] Fix | Delete
* @param string $fallback_class Optional. The class to use if the template is not found.
[107] Fix | Delete
* This argument most likely will be used for backward compatibility and supporting the "Legacy" template.
[108] Fix | Delete
*
[109] Fix | Delete
* @return string
[110] Fix | Delete
*/
[111] Fix | Delete
public static function get_current_template_class( $template_name = '', $fallback_class = '' ) {
[112] Fix | Delete
[113] Fix | Delete
$template_name = self::get_current_template_name( $template_name );
[114] Fix | Delete
[115] Fix | Delete
// If the user has set the legacy template, return the "General" template.
[116] Fix | Delete
if ( $template_name === Notifications::LEGACY_TEMPLATE ) {
[117] Fix | Delete
return ! empty( $fallback_class ) && class_exists( $fallback_class ) ? $fallback_class : __NAMESPACE__ . '\Templates\General';
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
// Check if the given template name is valid by looking into available templates.
[121] Fix | Delete
$current_template = Notifications::get_available_templates( $template_name );
[122] Fix | Delete
[123] Fix | Delete
// If the current template is not found or its corresponding class does not exist, return the "Classic" template.
[124] Fix | Delete
if ( ! isset( $current_template['path'] ) || ! class_exists( $current_template['path'] ) ) {
[125] Fix | Delete
return Notifications::get_available_templates( Notifications::DEFAULT_TEMPLATE )['path'];
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
// The provided template is valid, so return it.
[129] Fix | Delete
return $current_template['path'];
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Get the style overrides for the current email template.
[134] Fix | Delete
*
[135] Fix | Delete
* This function retrieves the style overrides for the email template, including background color,
[136] Fix | Delete
* body color, text color, link color, and typography. It provides default values and handles
[137] Fix | Delete
* different settings for both the free and Pro versions of the plugin.
[138] Fix | Delete
*
[139] Fix | Delete
* @since 1.8.5
[140] Fix | Delete
*
[141] Fix | Delete
* @return array
[142] Fix | Delete
*/
[143] Fix | Delete
public static function get_current_template_style_overrides() {
[144] Fix | Delete
[145] Fix | Delete
// Get the header image size for the current template.
[146] Fix | Delete
list( $header_image_size, $header_image_size_dark ) = self::get_template_header_image_size();
[147] Fix | Delete
[148] Fix | Delete
// Get the typography for the current template.
[149] Fix | Delete
list( $email_typography, $email_typography_dark ) = self::get_template_typography();
[150] Fix | Delete
[151] Fix | Delete
// Default style overrides.
[152] Fix | Delete
$defaults = [
[153] Fix | Delete
'email_background_color' => '#e9eaec',
[154] Fix | Delete
'email_body_color' => '#ffffff',
[155] Fix | Delete
'email_text_color' => '#333333',
[156] Fix | Delete
'email_links_color' => '#e27730',
[157] Fix | Delete
'email_background_color_dark' => '#2d2f31',
[158] Fix | Delete
'email_body_color_dark' => '#1f1f1f',
[159] Fix | Delete
'email_text_color_dark' => '#dddddd',
[160] Fix | Delete
'email_links_color_dark' => '#e27730',
[161] Fix | Delete
'email_typography' => $email_typography,
[162] Fix | Delete
'email_typography_dark' => $email_typography_dark,
[163] Fix | Delete
'header_image_max_width' => $header_image_size['width'],
[164] Fix | Delete
'header_image_max_height' => $header_image_size['height'],
[165] Fix | Delete
'header_image_max_width_dark' => $header_image_size_dark['width'],
[166] Fix | Delete
'header_image_max_height_dark' => $header_image_size_dark['height'],
[167] Fix | Delete
];
[168] Fix | Delete
[169] Fix | Delete
// Retrieve old background colors setting from the Lite version.
[170] Fix | Delete
$lite_background_color = wpforms_setting( 'email-background-color', $defaults['email_background_color'] );
[171] Fix | Delete
$lite_background_color_dark = wpforms_setting( 'email-background-color-dark', $defaults['email_background_color_dark'] );
[172] Fix | Delete
[173] Fix | Delete
// Leave early if the user has the Lite version.
[174] Fix | Delete
if ( ! wpforms()->is_pro() ) {
[175] Fix | Delete
// Override the background colors with the old setting.
[176] Fix | Delete
$defaults['email_background_color'] = $lite_background_color;
[177] Fix | Delete
$defaults['email_background_color_dark'] = $lite_background_color_dark;
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Filter the style overrides for the current email template.
[181] Fix | Delete
*
[182] Fix | Delete
* @since 1.8.6
[183] Fix | Delete
*
[184] Fix | Delete
* @param array $overrides The current email template style overrides.
[185] Fix | Delete
*/
[186] Fix | Delete
return (array) apply_filters( 'wpforms_emails_helpers_style_overrides_args', $defaults );
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
// Get the color scheme from the settings.
[190] Fix | Delete
$color_scheme = wpforms_setting( 'email-color-scheme', [] );
[191] Fix | Delete
[192] Fix | Delete
// If the user has the Pro version, but the light mode background color is the old setting, override it.
[193] Fix | Delete
if ( empty( $color_scheme['email_background_color'] ) && ! empty( $lite_background_color ) ) {
[194] Fix | Delete
$color_scheme['email_background_color'] = $lite_background_color;
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
// Get the dark mode color scheme from the settings.
[198] Fix | Delete
$color_scheme_dark = wpforms_setting( 'email-color-scheme-dark', [] );
[199] Fix | Delete
[200] Fix | Delete
// If the user has the Pro version, but the dark mode background color is the old setting, override it.
[201] Fix | Delete
if ( empty( $color_scheme_dark['email_background_color_dark'] ) && ! empty( $lite_background_color_dark ) ) {
[202] Fix | Delete
$color_scheme_dark['email_background_color_dark'] = $lite_background_color_dark;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
// Merge the color schemes with the defaults.
[206] Fix | Delete
$overrides = wp_parse_args( $color_scheme + $color_scheme_dark, $defaults );
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Filter the style overrides for the current email template.
[210] Fix | Delete
*
[211] Fix | Delete
* @since 1.8.6
[212] Fix | Delete
*
[213] Fix | Delete
* @param array $overrides The current email template style overrides.
[214] Fix | Delete
*/
[215] Fix | Delete
return (array) apply_filters( 'wpforms_emails_helpers_style_overrides_args', $overrides );
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
/**
[219] Fix | Delete
* Check if the current email template is plain text.
[220] Fix | Delete
*
[221] Fix | Delete
* @since 1.8.5
[222] Fix | Delete
*
[223] Fix | Delete
* @param string $template_name Optional. The name of the email template to compare.
[224] Fix | Delete
*
[225] Fix | Delete
* @return bool
[226] Fix | Delete
*/
[227] Fix | Delete
public static function is_plain_text_template( $template_name = '' ) {
[228] Fix | Delete
[229] Fix | Delete
// Leave early in case the given template name is not empty, and we can resolve it early.
[230] Fix | Delete
if ( ! empty( $template_name ) ) {
[231] Fix | Delete
return $template_name === Notifications::PLAIN_TEMPLATE;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
return wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE ) === Notifications::PLAIN_TEMPLATE;
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Check if the current template is legacy.
[239] Fix | Delete
* Legacy template is the one that its value is 'default'.
[240] Fix | Delete
*
[241] Fix | Delete
* @since 1.8.5
[242] Fix | Delete
*
[243] Fix | Delete
* @return bool
[244] Fix | Delete
*/
[245] Fix | Delete
public static function is_legacy_html_template() {
[246] Fix | Delete
[247] Fix | Delete
return wpforms_setting( 'email-template', Notifications::DEFAULT_TEMPLATE ) === Notifications::LEGACY_TEMPLATE;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
/**
[251] Fix | Delete
* Get the current template's typography.
[252] Fix | Delete
*
[253] Fix | Delete
* This function retrieves the typography setting for email templates and returns the corresponding font family.
[254] Fix | Delete
*
[255] Fix | Delete
* If the user has the Pro version, the font-family is determined based on the current template.
[256] Fix | Delete
* For free users, the font-family defaults to "Sans Serif" because the available templates
[257] Fix | Delete
* ("Classic" and "Compact") use this font-family in their design.
[258] Fix | Delete
*
[259] Fix | Delete
* @since 1.8.5
[260] Fix | Delete
* @since 1.8.6 Added $typography argument.
[261] Fix | Delete
*
[262] Fix | Delete
* @param string $typography Optional. The typography setting to evaluate.
[263] Fix | Delete
*
[264] Fix | Delete
* @return array|string
[265] Fix | Delete
*/
[266] Fix | Delete
public static function get_template_typography( $typography = '' ) {
[267] Fix | Delete
[268] Fix | Delete
// Predefined font families for light and dark modes.
[269] Fix | Delete
$font_families = [
[270] Fix | Delete
'sans_serif' => '-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif',
[271] Fix | Delete
'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',
[272] Fix | Delete
];
[273] Fix | Delete
[274] Fix | Delete
// If the user is not using the Pro version, return "Sans Serif" font-family.
[275] Fix | Delete
if ( ! wpforms()->is_pro() ) {
[276] Fix | Delete
return [ $font_families['sans_serif'], $font_families['sans_serif'] ];
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
// Leave early if a specific typography is requested.
[280] Fix | Delete
if ( ! empty( $typography ) ) {
[281] Fix | Delete
// Validate the input and return the corresponding font family.
[282] Fix | Delete
return $font_families[ $typography ] ?? $font_families['sans_serif'];
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
// Get typography settings from email settings.
[286] Fix | Delete
$setting_typography = [
[287] Fix | Delete
// Light mode.
[288] Fix | Delete
wpforms_setting( 'email-typography', 'sans-serif' ),
[289] Fix | Delete
// Dark mode.
[290] Fix | Delete
wpforms_setting( 'email-typography-dark', 'sans-serif' ),
[291] Fix | Delete
];
[292] Fix | Delete
[293] Fix | Delete
// Map setting values to predefined font families, default to 'sans_serif' if not found.
[294] Fix | Delete
return array_map(
[295] Fix | Delete
static function ( $item ) use ( $font_families ) {
[296] Fix | Delete
[297] Fix | Delete
return $font_families[ $item ] ?? $font_families['sans_serif'];
[298] Fix | Delete
},
[299] Fix | Delete
$setting_typography
[300] Fix | Delete
);
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
[304] Fix | Delete
/**
[305] Fix | Delete
* Get the header image size based on the specified size or 'medium' by default.
[306] Fix | Delete
*
[307] Fix | Delete
* Note that when given a size input, this function will only validate the input and return the corresponding size.
[308] Fix | Delete
* Otherwise, it will return the header image size for the current template in both light and dark modes.
[309] Fix | Delete
*
[310] Fix | Delete
* @since 1.8.5
[311] Fix | Delete
* @since 1.8.6 Added $size argument.
[312] Fix | Delete
*
[313] Fix | Delete
* @param string $size Optional. The desired image size ('small', 'medium', or 'large').
[314] Fix | Delete
*
[315] Fix | Delete
* @return array
[316] Fix | Delete
*/
[317] Fix | Delete
public static function get_template_header_image_size( $size = '' ) {
[318] Fix | Delete
[319] Fix | Delete
// Predefined image sizes.
[320] Fix | Delete
$sizes = [
[321] Fix | Delete
'small' => [
[322] Fix | Delete
'width' => '240',
[323] Fix | Delete
'height' => '120',
[324] Fix | Delete
],
[325] Fix | Delete
'medium' => [
[326] Fix | Delete
'width' => '350',
[327] Fix | Delete
'height' => '180',
[328] Fix | Delete
],
[329] Fix | Delete
'large' => [
[330] Fix | Delete
'width' => '500',
[331] Fix | Delete
'height' => '240',
[332] Fix | Delete
],
[333] Fix | Delete
];
[334] Fix | Delete
[335] Fix | Delete
// Leave early if a specific size is requested.
[336] Fix | Delete
if ( ! empty( $size ) ) {
[337] Fix | Delete
// Validate the input and return the corresponding size.
[338] Fix | Delete
return $sizes[ $size ] ?? $sizes['medium'];
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
// Get header image sizes from settings.
[342] Fix | Delete
$setting_size = [
[343] Fix | Delete
// Light mode.
[344] Fix | Delete
wpforms_setting( 'email-header-image-size', 'medium' ),
[345] Fix | Delete
// Dark mode.
[346] Fix | Delete
wpforms_setting( 'email-header-image-size-dark', 'medium' ),
[347] Fix | Delete
];
[348] Fix | Delete
[349] Fix | Delete
// Map setting values to predefined sizes, default to 'medium' if not found.
[350] Fix | Delete
return array_map(
[351] Fix | Delete
static function ( $item ) use ( $sizes ) {
[352] Fix | Delete
[353] Fix | Delete
return $sizes[ $item ] ?? $sizes['medium'];
[354] Fix | Delete
},
[355] Fix | Delete
$setting_size
[356] Fix | Delete
);
[357] Fix | Delete
}
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function