| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
if (!function_exists('str_contains')) { |
| 4 |
function str_contains($haystack, $needle) { |
| 5 |
return $needle !== '' && mb_strpos($haystack, $needle) !== false; |
| 6 |
} |
| 7 |
} |
| 8 |
if (! function_exists('str_ends_with')) { |
| 9 |
function str_ends_with(string $haystack, string $needle): bool |
| 10 |
{ |
| 11 |
$needle_len = strlen($needle); |
| 12 |
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len)); |
| 13 |
} |
| 14 |
} |
| 15 |
class Yeemail_Builder_Frontend_Functions { |
| 16 |
public static function cover_array_to_css($array){ |
| 17 |
$result = implode(PHP_EOL, array_map( |
| 18 |
function ($v, $k) { |
| 19 |
if($k == "font-family"){ |
| 20 |
$v = "'".$v."', sans-serif;"; |
| 21 |
} |
| 22 |
return sprintf("%s: %s;", $k, $v); }, |
| 23 |
$array, |
| 24 |
array_keys($array) |
| 25 |
)); |
| 26 |
$result = str_replace('"', "'", $result); |
| 27 |
return $result; |
| 28 |
} |
| 29 |
public static function get_settings_css($id_template){ |
| 30 |
$link_color = get_post_meta( $id_template,'_yeemail_link_color',true); |
| 31 |
$css = get_post_meta( $id_template,'_custom_css',true); |
| 32 |
ob_start(); |
| 33 |
?> |
| 34 |
<style type="text/css"> |
| 35 |
<?php |
| 36 |
echo wp_kses_post($css); |
| 37 |
?> |
| 38 |
.container-row a { |
| 39 |
color: <?php echo esc_attr( $link_color ) ?>; |
| 40 |
font-weight: normal; |
| 41 |
} |
| 42 |
.container-row h1 { |
| 43 |
color: <?php echo esc_attr( $link_color ); ?>; |
| 44 |
font-size: 30px; |
| 45 |
font-weight: 300; |
| 46 |
line-height: 150%; |
| 47 |
margin: 0; |
| 48 |
text-align: <?php echo is_rtl() ? 'right' : 'left'; ?>; |
| 49 |
} |
| 50 |
.container-row h2 { |
| 51 |
color: <?php echo esc_attr( $link_color ); ?>; |
| 52 |
display: block; |
| 53 |
font-size: 18px; |
| 54 |
font-weight: bold; |
| 55 |
line-height: 130%; |
| 56 |
margin: 0 0 18px; |
| 57 |
text-align: <?php echo is_rtl() ? 'right' : 'left'; ?>; |
| 58 |
} |
| 59 |
.container-row h3 { |
| 60 |
color: <?php echo esc_attr( $link_color ); ?>; |
| 61 |
display: block; |
| 62 |
font-size: 16px; |
| 63 |
font-weight: bold; |
| 64 |
line-height: 130%; |
| 65 |
margin: 16px 0 8px; |
| 66 |
text-align: <?php echo is_rtl() ? 'right' : 'left'; ?>; |
| 67 |
} |
| 68 |
</style> |
| 69 |
<?php |
| 70 |
$html= ob_get_clean(); |
| 71 |
return $html; |
| 72 |
} |
| 73 |
public static function creator_template($attrs){ |
| 74 |
$data_attrs = shortcode_atts(array( |
| 75 |
"id_template" => "", |
| 76 |
"type" => "preview", |
| 77 |
"html" => "", |
| 78 |
"datas" => array(), |
| 79 |
"params" => array() |
| 80 |
),$attrs); |
| 81 |
if (!function_exists('str_get_html')) { |
| 82 |
include YEEMAIL_PLUGIN_PATH."libs/simple_html_dom.php"; |
| 83 |
} |
| 84 |
$id_template = $data_attrs["id_template"]; |
| 85 |
$datas_check =$data_attrs["datas"]; |
| 86 |
if ( is_user_logged_in() ) { |
| 87 |
$datas_check["[yeemail_is_user_logged_in]"] = "true"; |
| 88 |
}else{ |
| 89 |
$datas_check["[yeemail_is_user_logged_in]"] = false; |
| 90 |
} |
| 91 |
if($data_attrs["html"] != "") { |
| 92 |
$template_html = $data_attrs["html"]; |
| 93 |
}else{ |
| 94 |
$template_html = self::get_html($id_template,$datas_check); |
| 95 |
} |
| 96 |
switch($data_attrs["type"]){ |
| 97 |
case "full": |
| 98 |
ob_start(); |
| 99 |
include YEEMAIL_PLUGIN_PATH."email-templates/header.php"; |
| 100 |
echo self::get_settings_css($id_template); // phpcs:ignore WordPress.Security.EscapeOutput |
| 101 |
echo do_shortcode($template_html); // phpcs:ignore WordPress.Security.EscapeOutput |
| 102 |
include YEEMAIL_PLUGIN_PATH."email-templates/footer.php"; |
| 103 |
$html= ob_get_clean(); |
| 104 |
return $html; |
| 105 |
break; |
| 106 |
case "content_no_shortcode": |
| 107 |
return $template_html; |
| 108 |
break; |
| 109 |
default: |
| 110 |
return do_shortcode($html); |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
public static function get_html( $id_template, $datas=""){ |
| 115 |
$width = get_post_meta( $id_template,'_mail_width',true); |
| 116 |
if($width < 480 || $width== "" ){ |
| 117 |
$width = "640"; |
| 118 |
} |
| 119 |
$html =""; |
| 120 |
$data_json = get_post_meta( $id_template,'data_email',true); |
| 121 |
$data_json = json_decode($data_json,true); |
| 122 |
if(!$data_json){ |
| 123 |
return ; |
| 124 |
} |
| 125 |
$container = $data_json["container"]; |
| 126 |
$container_css = self::cover_array_to_css($container); |
| 127 |
$data_contents = $data_json["rows"]; |
| 128 |
$datas_builder = apply_filters("yeemail_builder_block_html",array()); |
| 129 |
$class = "container"; |
| 130 |
$html = ""; |
| 131 |
ob_start(); |
| 132 |
?> |
| 133 |
<!--[if mso]><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td><?php echo ''; ?><![endif]--> |
| 134 |
<div data-yeemail-id="<?php echo esc_attr( $id_template ) ?>" class="wap" style="width:100%; <?php echo $container_css;// phpcs:ignore WordPress.Security.EscapeOutput ?>"> |
| 135 |
<?php |
| 136 |
foreach( $data_contents as $row){ |
| 137 |
$row_columns = $row["columns"]; |
| 138 |
if(isset($row["condition"])){ |
| 139 |
$row_condition = $row["condition"]; |
| 140 |
}else{ |
| 141 |
$row_condition = ''; |
| 142 |
} |
| 143 |
$show_row = apply_filters( "yeemail_conditional_logic_show", true, $row_condition,$datas ); |
| 144 |
if( $show_row ) { |
| 145 |
$row_style_array = $row["style"]; |
| 146 |
if(isset($row_style_array["background-image"]) && $row_style_array["background-image"] == "none"){ |
| 147 |
unset($row_style_array["background-position"]); |
| 148 |
unset($row_style_array["background-repeat"]); |
| 149 |
unset($row_style_array["background-size"]); |
| 150 |
unset($row_style_array["background-image"]); |
| 151 |
} |
| 152 |
$row_style = self::cover_array_to_css($row_style_array); |
| 153 |
$container_row_css = array(); |
| 154 |
$container_row_class = ""; |
| 155 |
if(isset($row["attr"]) && is_array($row["attr"])){ |
| 156 |
foreach($row["attr"] as $row_key => $row_colunm){ |
| 157 |
switch ($row_key){ |
| 158 |
case "background_full": |
| 159 |
if($row_colunm == "ok" && isset($row_style_array["background-color"]) && $row_style_array["background-color"] != ""){ |
| 160 |
$container_row_css["background-color"] = $row_style_array["background-color"]; |
| 161 |
} |
| 162 |
break; |
| 163 |
case "responsive": |
| 164 |
$container_row_class = "yeemail-responsive"; |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
$container_row_css = self::cover_array_to_css($container_row_css); |
| 171 |
$i=0; |
| 172 |
?> |
| 173 |
<div class="container-row <?php echo esc_attr( $container_row_class ) ?>" style="<?php echo $container_row_css; // phpcs:ignore WordPress.Security.EscapeOutput ?>"> |
| 174 |
<!--[if mso]><table role="presentation" align="center" cellpadding="0" cellspacing="0" border="0" width="<?php echo esc_attr( $width ) ?>"><tr><td><?php echo ''; ?><![endif]--> |
| 175 |
<table class="row" align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="<?php echo esc_attr( $width ) ?>" style="max-width:<?php echo esc_attr( $width ) ?>px; margin: 0 auto; <?php echo $row_style;// phpcs:ignore WordPress.Security.EscapeOutput ?>"> |
| 176 |
<tr> |
| 177 |
<?php |
| 178 |
foreach( $row_columns as $column ){ |
| 179 |
switch ($row["type"]){ |
| 180 |
case "row2": |
| 181 |
$col_width = "50%"; |
| 182 |
break; |
| 183 |
case "row3": |
| 184 |
if( $i == 0 ){ |
| 185 |
$col_width = "65%"; |
| 186 |
}else{ |
| 187 |
$col_width = "35%"; |
| 188 |
} |
| 189 |
break; |
| 190 |
case "row4": |
| 191 |
if( $i == 0 ){ |
| 192 |
$col_width = "35%"; |
| 193 |
}else{ |
| 194 |
$col_width = "35%"; |
| 195 |
} |
| 196 |
break; |
| 197 |
case "row5": |
| 198 |
$col_width = "33.33%"; |
| 199 |
break; |
| 200 |
case "row6": |
| 201 |
$col_width = "25%"; |
| 202 |
break; |
| 203 |
case "row7": |
| 204 |
$col_width = "20%"; |
| 205 |
break; |
| 206 |
case "row8": |
| 207 |
$col_width = "16.66%"; |
| 208 |
break; |
| 209 |
case "row9": |
| 210 |
$col_width = "14.28%"; |
| 211 |
break; |
| 212 |
default: |
| 213 |
$col_width = "100%"; |
| 214 |
break; |
| 215 |
} |
| 216 |
?> |
| 217 |
<td class="col" width="<?php echo esc_attr( $col_width ); ?>" valign="top" style="vertical-align:top;"> |
| 218 |
<?php |
| 219 |
$elements = $column["elements"]; |
| 220 |
if(count($elements) < 1 ){ |
| 221 |
echo ' '; // phpcs:ignore WordPress.Security.EscapeOutput |
| 222 |
}else{ |
| 223 |
foreach( $elements as $element ){ |
| 224 |
$element_html = self::cover_type_element_to_html($element,$datas_builder,$datas); |
| 225 |
echo $element_html; // phpcs:ignore WordPress.Security.EscapeOutput |
| 226 |
} |
| 227 |
} |
| 228 |
?> |
| 229 |
</td> |
| 230 |
<?php |
| 231 |
$i++; |
| 232 |
} ?> |
| 233 |
</tr> |
| 234 |
</table> |
| 235 |
<!--[if mso]></td></tr></table><![endif]--> |
| 236 |
</div> |
| 237 |
<?php |
| 238 |
} |
| 239 |
} |
| 240 |
?> |
| 241 |
</div> |
| 242 |
<!--[if mso]></td></tr></table><![endif]--> |
| 243 |
<?php |
| 244 |
$html= ob_get_clean(); |
| 245 |
return $html; |
| 246 |
} |
| 247 |
public static function cover_type_element_to_html($element,$datas_builder, $datas=array()){ |
| 248 |
$result = ""; |
| 249 |
$html = ""; |
| 250 |
$datas_builder = $datas_builder["block"]; |
| 251 |
$type = $element["type"]; |
| 252 |
if(!isset($datas_builder[ $type ]) ){ |
| 253 |
return ""; |
| 254 |
} |
| 255 |
if($type == "content"){ |
| 256 |
return '<div class="builder-content">Content Email</div>'; |
| 257 |
} |
| 258 |
$inner_attr = $element["inner_attr"]; |
| 259 |
$container_style_array = $element["container_style"]; |
| 260 |
if(isset($container_style_array["background-image"]) && $container_style_array["background-image"] == "none"){ |
| 261 |
unset($container_style_array["background-position"]); |
| 262 |
unset($container_style_array["background-repeat"]); |
| 263 |
unset($container_style_array["background-size"]); |
| 264 |
unset($container_style_array["background-image"]); |
| 265 |
} |
| 266 |
$container_style = self::cover_array_to_css($container_style_array); |
| 267 |
$inner_style = $element["inner_style"]; |
| 268 |
if(isset($element["condition"])){ |
| 269 |
$element_condition = $element["condition"]; |
| 270 |
}else{ |
| 271 |
$element_condition = ''; |
| 272 |
} |
| 273 |
$html_el = str_get_html($datas_builder[ $type ]["builder"]); |
| 274 |
$show = apply_filters( "yeemail_conditional_logic_show", true, $element_condition,$datas ); |
| 275 |
if( $show ){ |
| 276 |
$html_el->find('.builder-elements-content',0)->setAttribute("style",$container_style); |
| 277 |
if($show == "yeemail_show_desktop" || $show == "yeemail_show_mobile"){ |
| 278 |
$html_el->find('.builder-elements-content',0)->addClass($show); |
| 279 |
} |
| 280 |
foreach( $inner_attr as $key => $attrs ){ |
| 281 |
foreach( $attrs as $k => $v ){ |
| 282 |
if(!is_array($v)){ |
| 283 |
$v = do_shortcode($v); |
| 284 |
} |
| 285 |
switch( $type ){ |
| 286 |
case "qrcode": |
| 287 |
if( $k == "html_hide"){ |
| 288 |
$html_el->find( $key ,0)->removeClass('hidden'); |
| 289 |
$html_el->find( $key ,0)->innertext = '<div class="text-content"><img class="qrcode" src="[wp_builder_pdf_qrcode_new]'.strip_tags($v).'[/wp_builder_pdf_qrcode_new]" /></div>'; |
| 290 |
}elseif( $k == "html"){ |
| 291 |
$html_el->find( $key ,0)->remove(); |
| 292 |
}else{ |
| 293 |
$html_el->find( $key ,0)->setAttribute($k,$v); |
| 294 |
} |
| 295 |
break; |
| 296 |
case "barcode": |
| 297 |
if( $k == "html_hide"){ |
| 298 |
$html_el->find( $key ,0)->removeClass('hidden'); |
| 299 |
$html_el->find( $key ,0)->innertext = '<div class="text-content"><img src="[wp_builder_pdf_barcode_new]'.strip_tags($v).'[/wp_builder_pdf_barcode_new]" /></div>'; |
| 300 |
}elseif( $k == "html"){ |
| 301 |
$html_el->find( $key ,0)->remove(); |
| 302 |
}else{ |
| 303 |
$html_el->find( $key ,0)->setAttribute($k,$v); |
| 304 |
} |
| 305 |
break; |
| 306 |
case "image": |
| 307 |
if( isset($attrs["data-type"]) && $attrs["data-type"] == 1){ |
| 308 |
$change_data = str_replace('"',"'",$attrs["data-field"]); |
| 309 |
$html_el->find( "img" ,0)->setAttribute("src",$change_data); |
| 310 |
}else{ |
| 311 |
if( $v != ""){ |
| 312 |
$html_el->find( $key ,0)->setAttribute($k,$v); |
| 313 |
} |
| 314 |
} |
| 315 |
break; |
| 316 |
case "signature": |
| 317 |
if( $attrs["data-field"] != ""){ |
| 318 |
$change_data = str_replace('"',"'",$attrs["data-field"]); |
| 319 |
$html_el->find( "img" ,0)->setAttribute("src",$change_data); |
| 320 |
}else{ |
| 321 |
if( $v != ""){ |
| 322 |
$html_el->find( $key ,0)->setAttribute($k,$v); |
| 323 |
} |
| 324 |
} |
| 325 |
break; |
| 326 |
case "menu": |
| 327 |
if(is_array($v)){ |
| 328 |
$table_menu ='<table cellpadding="0" cellspacing="0" width="100%" class="yeemail-menu"><tr>'; |
| 329 |
foreach($v as $menu_key => $menu ){ |
| 330 |
$table_menu .='<td style="background-color:'.$menu["background"].';" align="center" valign="top"><a style="color:'.$menu["color"].';text-decoration: none;" target="_blank" href="'.$menu["href"].'">'.$menu["text"].'</a></td>'; |
| 331 |
} |
| 332 |
$table_menu .='</tr></table>'; |
| 333 |
$html_el->find( $key ,0)->setAttribute("outertext",$table_menu); |
| 334 |
} |
| 335 |
break; |
| 336 |
case "order_detail": |
| 337 |
if( $k == "html_hide"){ |
| 338 |
$html_el->find( $key ,0)->removeClass('hidden'); |
| 339 |
$table = $v; |
| 340 |
$table= preg_replace('/<td[^>]*\bhidden\b[^>]*>.*?<\/td>/is', '', $table); |
| 341 |
$table= preg_replace('/<th[^>]*\bhidden\b[^>]*>.*?<\/th>/is', '', $table); |
| 342 |
$html_el->find( $key ,0)->__set("innertext",$table); |
| 343 |
} |
| 344 |
elseif( $k == "html"){ |
| 345 |
$html_el->find( $key ,0)->remove(); |
| 346 |
} |
| 347 |
break; |
| 348 |
default: |
| 349 |
//text |
| 350 |
switch ($k){ |
| 351 |
case "html_hide": |
| 352 |
$html_el->find( $key ,0)->removeClass('hidden'); |
| 353 |
$html_el->find( $key ,0)->innertext = $v; |
| 354 |
break; |
| 355 |
case "html": |
| 356 |
$html_el->find( $key ,0)->remove(); |
| 357 |
break; |
| 358 |
case "text": |
| 359 |
$html_el->find( $key ,0)->setAttribute("innertext",$v); |
| 360 |
break; |
| 361 |
default: |
| 362 |
$html_el->find( $key ,0)->setAttribute($k,$v); |
| 363 |
break; |
| 364 |
} |
| 365 |
if( $k == "html_hide"){ |
| 366 |
$html_el->find( $key ,0)->removeClass('hidden'); |
| 367 |
$html_el->find( $key ,0)->innertext = $v; |
| 368 |
}elseif( $k == "html"){ |
| 369 |
} |
| 370 |
else{ |
| 371 |
$html_el->find( $key ,0)->setAttribute($k,$v); |
| 372 |
} |
| 373 |
break; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
$html_el = str_get_html($html_el); |
| 378 |
foreach( $inner_style as $key => $style ){ |
| 379 |
$inner_style_array = $style; |
| 380 |
if(isset($inner_style_array["background-image"]) && $inner_style_array["background-image"] == "none"){ |
| 381 |
unset($inner_style_array["background-position"]); |
| 382 |
unset($inner_style_array["background-repeat"]); |
| 383 |
unset($inner_style_array["background-size"]); |
| 384 |
unset($inner_style_array["background-image"]); |
| 385 |
} |
| 386 |
$in_style = self::cover_array_to_css($inner_style_array); |
| 387 |
foreach( $html_el->find( $key ) as $html_el_inner ){ |
| 388 |
$old_style = $html_el_inner->getAttribute ("style"); |
| 389 |
$html_el_inner->setAttribute("style",$old_style.$in_style); |
| 390 |
} |
| 391 |
} |
| 392 |
return $html_el; |
| 393 |
} |
| 394 |
return $html; |
| 395 |
} |
| 396 |
|
| 397 |
} |