| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email template loader. |
| 4 |
* |
| 5 |
* @package SureForms. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SRFM\Inc\Email; |
| 9 |
|
| 10 |
use SRFM\Inc\Traits\Get_Instance; |
| 11 |
use SRFM\Inc\Helper; |
| 12 |
|
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Email Class |
| 20 |
* |
| 21 |
* @since 0.0.1 |
| 22 |
*/ |
| 23 |
class Email_Template { |
| 24 |
|
| 25 |
use Get_Instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Class Constructor |
| 29 |
* |
| 30 |
* @since 0.0.1 |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get email header. |
| 39 |
* |
| 40 |
* @since 0.0.1 |
| 41 |
* @return string|false |
| 42 |
*/ |
| 43 |
public function get_header() { |
| 44 |
ob_start(); ?> |
| 45 |
<html> |
| 46 |
|
| 47 |
<head> |
| 48 |
<meta charset="utf-8"> |
| 49 |
<title><?php echo esc_html__( 'New form submission', 'sureforms' ); ?></title> |
| 50 |
</head> |
| 51 |
|
| 52 |
<body style="margin: 0; padding: 0;"> |
| 53 |
<div id="srfm_wrapper" dir="ltr" style="margin: 0; background-color: #F8F8FC; padding: 40px 0 0 0; width: 100%"> |
| 54 |
<table border="0" cellpadding="0" cellspacing="0" width="100%"> |
| 55 |
<tbody> |
| 56 |
<tr> |
| 57 |
<td align="center" valign="top"> |
| 58 |
<table border="0" cellpadding="0" cellspacing="0" width="600" id="srfm_template_container" style="background-color: #ffffff;border: 1px solid #dce0e6;margin-bottom: 25px; |
| 59 |
"> |
| 60 |
<tbody> |
| 61 |
<tr> |
| 62 |
<td align="center" valign="top"> |
| 63 |
<table border="0" cellpadding="0" cellspacing="0" width="600" |
| 64 |
id="srfm_template_body"> |
| 65 |
<tbody> |
| 66 |
<tr> |
| 67 |
<td valign="top" id="srfm_body_content" |
| 68 |
style="background-color: #ffffff"> |
| 69 |
<table border="0" cellpadding="20" cellspacing="0" width="100%"> |
| 70 |
<tbody> |
| 71 |
<tr> |
| 72 |
<td valign="top" style="padding:32px"> |
| 73 |
<div id="srfm_body_content_inner" style="color: #384860;font-family: Roboto-Medium,Roboto,-apple-system,BlinkMacSystemFont,Helvetica Neue,Helvetica,Arial,sans-serif;font-size: 14px;line-height: 1;text-align: left;"> |
| 74 |
<?php |
| 75 |
return ob_get_clean(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get email footer. |
| 80 |
* |
| 81 |
* @since 0.0.1 |
| 82 |
* @return string|false footer tags. |
| 83 |
*/ |
| 84 |
public function get_footer() { |
| 85 |
// Translators: Site URL. |
| 86 |
$site_link = sprintf( __( '<a href=%1$s>%2$s</a>', 'sureforms' ), home_url( '/' ), get_bloginfo( 'name' ) ); |
| 87 |
ob_start(); |
| 88 |
?> |
| 89 |
</div> |
| 90 |
</td> |
| 91 |
</tr> |
| 92 |
</tbody> |
| 93 |
</table> |
| 94 |
</td> |
| 95 |
</tr> |
| 96 |
</tbody> |
| 97 |
</table> |
| 98 |
</td> |
| 99 |
</tr> |
| 100 |
</tbody> |
| 101 |
</table> |
| 102 |
</td> |
| 103 |
</tr> |
| 104 |
</tbody> |
| 105 |
</table> |
| 106 |
</div> |
| 107 |
</body> |
| 108 |
</html> |
| 109 |
<?php |
| 110 |
|
| 111 |
return ob_get_clean(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Render email template. |
| 116 |
* |
| 117 |
* @param array<mixed> $fields Submission fields. |
| 118 |
* @param string $email_body email body. |
| 119 |
* @since 0.0.1 |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function render( $fields, $email_body ) { |
| 123 |
$message = $this->get_header(); |
| 124 |
$excluded_fields = [ 'srfm-honeypot-field', 'g-recaptcha-response', 'srfm-sender-email-field' ]; |
| 125 |
|
| 126 |
$message .= $email_body; |
| 127 |
if ( strpos( $email_body, '{all_data}' ) !== false ) { |
| 128 |
|
| 129 |
ob_start(); |
| 130 |
|
| 131 |
?> |
| 132 |
<table class="srfm_all_data" width="536" cellpadding="0" cellspacing="0" style="border: 1px solid #dce0e6;border-radius: 6px;margin-top: 25px;margin-bottom: 25px;"> |
| 133 |
<tbody> |
| 134 |
<?php |
| 135 |
foreach ( $fields as $field_name => $value ) { |
| 136 |
if ( is_array( $value ) ) { |
| 137 |
$values_array = $value; |
| 138 |
} else { |
| 139 |
$value = Helper::get_string_value( $value ); |
| 140 |
} |
| 141 |
if ( in_array( $field_name, $excluded_fields, true ) || false === str_contains( $field_name, '-lbl-' ) ) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
$label = explode( '-lbl-', $field_name )[1]; |
| 146 |
$label = explode( '-', $label )[0]; |
| 147 |
$field_label = $label ? esc_html( Helper::decrypt( $label ) ) : ''; |
| 148 |
?> |
| 149 |
<tr class="field-label"> |
| 150 |
<th style="font-weight: 500;font-size: 14px;color: #1E293B;padding: 8px 16px;background-color: #F1F5F9;text-align: left;"> |
| 151 |
<strong><?php echo esc_html( $field_label ); ?><?php echo esc_html__( ':', 'sureforms' ); ?><strong/> |
| 152 |
</th> |
| 153 |
</tr> |
| 154 |
<tr class="field-value"> |
| 155 |
<td style="font-size: 14px;color: #475569;padding: 8px 16px 16px 16px;padding-bottom: 10px;"> |
| 156 |
<?php |
| 157 |
if ( ! empty( $values_array ) && is_array( $values_array ) ) { |
| 158 |
foreach ( $values_array as $value ) { |
| 159 |
$value = Helper::get_string_value( $value ); |
| 160 |
if ( ! empty( $value ) && is_string( $value ) ) { |
| 161 |
?> |
| 162 |
<a target="_blank" href="<?php echo esc_attr( urldecode( $value ) ); ?>"><?php echo esc_html__( 'View', 'sureforms' ); ?></a> |
| 163 |
<?php |
| 164 |
} |
| 165 |
} |
| 166 |
} else { |
| 167 |
if ( is_string( $value ) ) { |
| 168 |
echo false !== strpos( $value, PHP_EOL ) ? wp_kses_post( wpautop( $value ) ) : wp_kses( |
| 169 |
$value, |
| 170 |
[ |
| 171 |
'a' => [ |
| 172 |
'href' => [], |
| 173 |
'target' => [], |
| 174 |
], |
| 175 |
] |
| 176 |
); |
| 177 |
} |
| 178 |
} |
| 179 |
?> |
| 180 |
</td> |
| 181 |
</tr> |
| 182 |
<?php } ?> |
| 183 |
</tbody> |
| 184 |
</table> |
| 185 |
<?php |
| 186 |
$table_data = ob_get_clean(); |
| 187 |
$current_table_data = $table_data ? $table_data : ''; // This is done as str_replace expects array|string but ob_get_clean() returns string|false. |
| 188 |
$message = str_replace( '{all_data}', $current_table_data, $message ); |
| 189 |
} |
| 190 |
$message .= $this->get_footer(); |
| 191 |
return $message; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|