| 1 |
<?php |
| 2 |
|
| 3 |
namespace ABlocks\Ajax; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use ABlocks\Classes\AbstractAjaxHandler; |
| 10 |
use ABlocks\Helper; |
| 11 |
use ABlocks\Classes\{ Sanitizer, EmailTemplate as EmailTemplateModel }; |
| 12 |
use Exception; |
| 13 |
class EmailTemplate extends AbstractAjaxHandler { |
| 14 |
public function __construct() { |
| 15 |
$this->actions = [ |
| 16 |
'get_templates' => [ |
| 17 |
'callback' => [ $this, 'get_templates' ], |
| 18 |
'capability' => 'manage_options', |
| 19 |
'fields' => [ |
| 20 |
'email_template_id' => 'string', |
| 21 |
'isSubscription' => 'boolean', |
| 22 |
] |
| 23 |
], |
| 24 |
'update_template' => [ |
| 25 |
'callback' => [ $this, 'update_template' ], |
| 26 |
'capability' => 'manage_options', |
| 27 |
'fields' => [ |
| 28 |
'from' => 'string', |
| 29 |
'email_template_id' => 'string', |
| 30 |
'slug' => 'string', |
| 31 |
'from_name' => 'string', |
| 32 |
'to' => 'string', |
| 33 |
'subject' => 'string', |
| 34 |
'body' => 'post', |
| 35 |
'reply_to' => 'string', |
| 36 |
'cc' => 'string', |
| 37 |
'bcc' => 'string', |
| 38 |
'format' => 'string', |
| 39 |
'status' => 'boolean', |
| 40 |
] |
| 41 |
], |
| 42 |
'delete_template' => [ |
| 43 |
'callback' => [ $this, 'delete_template' ], |
| 44 |
'capability' => 'manage_options', |
| 45 |
'fields' => [ |
| 46 |
'email_template_id' => 'string', |
| 47 |
'slug' => 'string', |
| 48 |
] |
| 49 |
], |
| 50 |
'remove_templates' => [ |
| 51 |
'callback' => [ $this, 'remove_templates' ], |
| 52 |
'capability' => 'manage_options', |
| 53 |
'fields' => [ |
| 54 |
'email_template_id' => 'string', |
| 55 |
] |
| 56 |
], |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public function get_templates( array $payload ) : void { |
| 61 |
$email_template_id = $payload['email_template_id'] ?? ''; |
| 62 |
$isSubscription = boolval( $payload['isSubscription'] ?? false ); |
| 63 |
try { |
| 64 |
wp_send_json_success( |
| 65 |
EmailTemplateModel::ins( $email_template_id ) |
| 66 |
->get_templates( $isSubscription ) |
| 67 |
); |
| 68 |
} catch ( Exception $e ) { |
| 69 |
wp_send_json_error( |
| 70 |
$e->getMessage() |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function update_template( array $payload ) : void { |
| 76 |
$email_template_id = $payload['email_template_id'] ?? ''; |
| 77 |
$slug = $payload['slug'] ?? ''; |
| 78 |
unset( $payload['email_template_id'], $payload['slug'] ); |
| 79 |
try { |
| 80 |
wp_send_json_success( |
| 81 |
EmailTemplateModel::ins( $email_template_id ) |
| 82 |
->update_template( $slug, $payload ) |
| 83 |
); |
| 84 |
} catch ( Exception $e ) { |
| 85 |
wp_send_json_error( |
| 86 |
$e->getMessage() |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function delete_template( array $payload ) : void { |
| 92 |
$email_template_id = $payload['email_template_id'] ?? ''; |
| 93 |
$slug = $payload['slug'] ?? ''; |
| 94 |
try { |
| 95 |
wp_send_json_success( |
| 96 |
EmailTemplateModel::ins( $email_template_id ) |
| 97 |
->delete_template( $slug ) |
| 98 |
); |
| 99 |
} catch ( Exception $e ) { |
| 100 |
wp_send_json_error( |
| 101 |
$e->getMessage() |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public function remove_templates( array $payload ) : void { |
| 107 |
$email_template_id = $payload['email_template_id'] ?? ''; |
| 108 |
delete_option( $email_template_id ); |
| 109 |
wp_send_json_success( [ |
| 110 |
'message' => __( 'Templates deleted.', 'ablocks' ) |
| 111 |
] ); |
| 112 |
} |
| 113 |
} |
| 114 |
|