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