| 1 |
<?php |
| 2 |
namespace ABlocks\Classes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
use Exception; |
| 8 |
class EmailTemplate { |
| 9 |
public string $email_template_id; |
| 10 |
public array $templates; |
| 11 |
|
| 12 |
public function __construct( string $email_template_id ) { |
| 13 |
$this->email_template_id = $email_template_id; |
| 14 |
$this->templates = $this->get_templates(); |
| 15 |
} |
| 16 |
|
| 17 |
public static function ins( string $email_template_id ) : self { |
| 18 |
return new self( $email_template_id ); |
| 19 |
} |
| 20 |
|
| 21 |
public function get_option_name() : string { |
| 22 |
return $this->email_template_id; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_templates( bool $status = false ) : array { |
| 26 |
return get_option( |
| 27 |
$this->get_option_name(), |
| 28 |
[ |
| 29 |
'default' => [ |
| 30 |
'from' => '{admin_email}', |
| 31 |
'from_name' => '{site_name}', |
| 32 |
'to' => ! $status ? '{admin_email}' : \get_option( 'admin_email' ), |
| 33 |
'subject' => 'New message', |
| 34 |
'body' => 'Tables: {all-fields}', |
| 35 |
'reply_to' => '', |
| 36 |
'cc' => '', |
| 37 |
'bcc' => '', |
| 38 |
'format' => 'html', |
| 39 |
'status' => ! $status, |
| 40 |
] |
| 41 |
] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function update_template( string $slug, array $data ) : bool { |
| 46 |
$data = wp_parse_args( $data, [ |
| 47 |
'from' => '', |
| 48 |
'from_name' => '', |
| 49 |
'to' => '', |
| 50 |
'subject' => '', |
| 51 |
'body' => '', |
| 52 |
'reply_to' => '', |
| 53 |
'cc' => '', |
| 54 |
'bcc' => '', |
| 55 |
'format' => 'html', |
| 56 |
'status' => false, |
| 57 |
] ); |
| 58 |
|
| 59 |
if ( |
| 60 |
empty( $slug ) || |
| 61 |
empty( $data['to'] ) || |
| 62 |
empty( $data['subject'] ) || |
| 63 |
empty( $data['body'] ) |
| 64 |
) { |
| 65 |
throw new Exception( |
| 66 |
esc_html__( 'Slug/Subject/Body/To is required', 'ablocks' ) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$this->templates[ $slug ] = $data; |
| 71 |
|
| 72 |
update_option( |
| 73 |
$this->get_option_name(), |
| 74 |
$this->templates, |
| 75 |
false |
| 76 |
); |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
public function delete_template( string $slug ) : bool { |
| 82 |
|
| 83 |
if ( |
| 84 |
! array_key_exists( $slug, $this->templates ) |
| 85 |
) { |
| 86 |
throw new Exception( |
| 87 |
esc_html__( 'Template does not exist', 'ablocks' ) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
unset( $this->templates[ $slug ] ); |
| 92 |
|
| 93 |
update_option( |
| 94 |
$this->get_option_name(), |
| 95 |
$this->templates, |
| 96 |
false |
| 97 |
); |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
} |
| 102 |
|