| 1 |
<?php |
| 2 |
namespace EmailKit\Admin; |
| 3 |
|
| 4 |
defined("ABSPATH") || exit; |
| 5 |
// use Dependency; |
| 6 |
|
| 7 |
class EmailKitAjax { |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_action('wp_ajax_emailkit_get_email_template_type', [$this, 'get_email_template_type']); |
| 11 |
add_action('wp_ajax_emailkit_template_data', [$this, 'get_template_data']); |
| 12 |
add_action('wp_ajax_emailkit_update_template_data', [$this, 'update_template_data']); |
| 13 |
add_action('wp_ajax_emailkit_filter_save_as_template', [$this, 'emailkit_filter_save_as_template']); |
| 14 |
} |
| 15 |
|
| 16 |
function get_email_template_type() { |
| 17 |
|
| 18 |
// verify request |
| 19 |
if(empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'emailkit_nonce')) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
$template_types = []; |
| 24 |
$email_type = ''; |
| 25 |
if(isset($_POST['data'])) { |
| 26 |
$email_type = sanitize_text_field(wp_unslash($_POST['data'])); |
| 27 |
} |
| 28 |
|
| 29 |
$dependency = \EmailKit\Admin\Dependency::check($email_type); |
| 30 |
|
| 31 |
|
| 32 |
if(true !== $dependency){ |
| 33 |
$need_plugin = ['dependency_status' => false, 'need_plugin' => $dependency ]; |
| 34 |
wp_send_json_success($need_plugin); |
| 35 |
exit; |
| 36 |
} |
| 37 |
|
| 38 |
$template_name = []; // Set data for dropdown |
| 39 |
foreach(self::get_type_wise_mails($email_type) as $key => $value) { |
| 40 |
if($email_type == 'saved-templates'){ |
| 41 |
$template_name[] = ["id" => $value['id'], "text" => '('.$value['template_type'].') '.$value['title']]; |
| 42 |
}else{ |
| 43 |
$template_name[] = ["id" => $key, "text" => $value]; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
$templates = []; |
| 50 |
foreach(TemplateList::get_templates() as $template): |
| 51 |
|
| 52 |
if(isset($template['mail_type']) && $email_type == $template['mail_type']) { |
| 53 |
ob_start(); |
| 54 |
|
| 55 |
include EMAILKIT_DIR.'includes/views/modal-form-template-item.php'; |
| 56 |
|
| 57 |
$templates[] = ob_get_clean(); |
| 58 |
} |
| 59 |
|
| 60 |
endforeach; |
| 61 |
|
| 62 |
$data = ['templates' => $templates, 'template_name' => $template_name]; |
| 63 |
|
| 64 |
|
| 65 |
wp_send_json_success($data); |
| 66 |
} |
| 67 |
|
| 68 |
static function get_type_wise_mails($type) { |
| 69 |
|
| 70 |
$type_list = [ |
| 71 |
'woocommerce' => \EmailKit\Admin\Emails\EmailLists::woocommerce_email(), |
| 72 |
'wordpress' => \EmailKit\Admin\Emails\EmailLists::wordpress_email(), |
| 73 |
'saved-templates' => \EmailKit\Admin\Emails\EmailLists::save_templates(), |
| 74 |
]; |
| 75 |
return $type_list[$type] ?? []; |
| 76 |
} |
| 77 |
|
| 78 |
function get_template_data() { |
| 79 |
// verify request |
| 80 |
if(empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'emailkit_nonce')) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$ID = isset($_POST['ID']) ? sanitize_text_field(wp_unslash($_POST['ID'])) : null; |
| 85 |
|
| 86 |
if($ID) { |
| 87 |
wp_send_json_success(get_the_title($ID), 200); |
| 88 |
} |
| 89 |
|
| 90 |
wp_send_json_error(esc_html__('Failed to get template', 'emailkit'), 400); |
| 91 |
|
| 92 |
exit; |
| 93 |
} |
| 94 |
|
| 95 |
function emailkit_filter_save_as_template() { |
| 96 |
|
| 97 |
// verify request |
| 98 |
if(empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'emailkit_nonce')) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
$template_id = isset($_POST['template_id']) ? sanitize_text_field(wp_unslash($_POST['template_id'])) : null; |
| 103 |
$saved_template_type = isset($_POST['email_type']) ? sanitize_text_field(wp_unslash($_POST['email_type'])) : null; |
| 104 |
|
| 105 |
|
| 106 |
$email_template_id = get_post_meta($template_id, 'emailkit_template_id', true); |
| 107 |
$email_type = get_post_meta($email_template_id, 'emailkit_email_type', true); |
| 108 |
$template_type = get_post_meta($email_template_id, 'emailkit_template_type', true); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
$dependency = \EmailKit\Admin\Dependency::check($email_type); |
| 113 |
|
| 114 |
if(true !== $dependency){ |
| 115 |
$need_plugin = ['dependency_status' => false, 'need_plugin' => $dependency ]; |
| 116 |
wp_send_json_success($need_plugin); |
| 117 |
exit; |
| 118 |
} |
| 119 |
if(true == $dependency){ |
| 120 |
$need_plugin = ['dependency_status' => true, 'need_plugin' => $dependency ]; |
| 121 |
wp_send_json_success($need_plugin); |
| 122 |
exit; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
function update_template_data() { |
| 127 |
// verify request |
| 128 |
if(empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'emailkit_nonce')) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
$ID = isset($_POST['id']) ? sanitize_text_field(wp_unslash($_POST['id'])) : null; |
| 133 |
$new_title = isset($_POST['title']) ? sanitize_text_field(wp_unslash($_POST['title'])) : ''; |
| 134 |
$data = []; |
| 135 |
|
| 136 |
$post_update = array( |
| 137 |
'ID' => $ID, |
| 138 |
'post_title' => $new_title |
| 139 |
); |
| 140 |
|
| 141 |
$ID = wp_update_post($post_update); |
| 142 |
$data['templateId'] = $ID; |
| 143 |
$data['title'] = get_the_title($ID); |
| 144 |
|
| 145 |
if(isset($_POST['action_type']) && sanitize_text_field(wp_unslash($_POST['action_type'])) == 'loadbuilder') { |
| 146 |
$data['builder_url'] = admin_url("post.php?post={$ID}&action=emailkit-builder"); |
| 147 |
} |
| 148 |
|
| 149 |
if($ID) { |
| 150 |
wp_send_json_success($data, 200); |
| 151 |
} |
| 152 |
wp_send_json_error(esc_html__('Failed to get template', 'emailkit'), 400); |
| 153 |
|
| 154 |
exit; |
| 155 |
} |
| 156 |
|
| 157 |
} |