PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.0
EmailKit – Email Customizer for WooCommerce & WP v1.6.0
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / EmailKitAjax.php

EmailKitAjax.php in EmailKit – Email Customizer for WooCommerce & WP 1.6.0, at includes/Admin/EmailKitAjax.php

182 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace EmailKit\Admin;
3
4 defined("ABSPATH" ) || exit;
5 use EmailKit\Admin\Emails\EmailLists;
6 use EmailKit\Admin\Dependency;
7
8 /**
9 * EmailKitAjax is responsinle for all ajax request and responses
10 */
11 class EmailKitAjax {
12
13 public function __construct() {
14 add_action( 'wp_ajax_emailkit_get_email_template_type', [ $this, 'get_email_template_type' ] );
15 add_action( 'wp_ajax_emailkit_template_data', [ $this, 'get_template_data' ] );
16 add_action( 'wp_ajax_emailkit_update_template_data', [ $this, 'update_template_data' ] );
17 add_action( 'wp_ajax_emailkit_filter_save_as_template', [ $this, 'emailkit_filter_save_as_template' ] );
18 }
19
20 /**
21 * get email type & send email template json response
22 */
23 function get_email_template_type() {
24
25 // verify request
26 if( empty( $_POST[ 'nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ), 'emailkit_nonce' ) ) {
27 return false;
28 }
29
30 $template_types = [];
31 $email_type = '';
32 if( isset( $_POST[ 'data' ] ) ) {
33 $email_type = sanitize_text_field( wp_unslash( $_POST[ 'data' ] ) );
34 }
35
36 $dependency = Dependency::check( $email_type );
37
38
39 if( true !== $dependency ){
40 $need_plugin = [ 'dependency_status' => false, 'need_plugin' => $dependency ];
41 wp_send_json_success( $need_plugin );
42 exit;
43 }
44
45 $template_name = []; // Set data for dropdown
46 foreach( self::get_type_wise_mails( $email_type ) as $key => $value ) {
47 if( $email_type == 'saved-templates' ){
48 $type_label = isset( $value['template_type'] ) ? ucwords(str_replace('_', ' ', $value['template_type'])) : '';
49 $template_name[] = [ "id" => $value[ 'id'], "text" => $value[ 'title' ] .' - ' . $type_label ];
50 }else{
51 $template_name[] = [ "id" => $key, "text" => $value ];
52 }
53 }
54
55
56 $templates = [ ];
57 foreach( TemplateList::get_templates( ) as $template ):
58
59 if( isset( $template[ 'mail_type' ] ) && $email_type == $template[ 'mail_type' ] ) {
60 ob_start();
61
62 include EMAILKIT_DIR.'includes/views/modal-form-template-item.php';
63
64 $templates[ ] = ob_get_clean();
65 }
66
67 endforeach;
68
69 $data = [ 'templates' => $templates, 'template_name' => $template_name];
70
71
72 wp_send_json_success( $data );
73
74 exit;
75 }
76
77 /**
78 * It fetch exact mail lists from all mail template
79 * @return array
80 */
81 static function get_type_wise_mails( $type ) : array {
82
83 $type_list = [
84 'woocommerce' => EmailLists::woocommerce_email(),
85 'wordpress' => EmailLists::wordpress_email(),
86 'saved-templates' => EmailLists::saved_templates(),
87 'metform' => EmailLists::metform_email(),
88 ];
89
90 return $type_list[ $type ] ?? [];
91 }
92
93 /**
94 * Get single template title and send json response
95 */
96 function get_template_data() {
97
98 // verify request
99 if( empty( $_POST[ 'nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ), 'emailkit_nonce' ) ) {
100 return false;
101 }
102
103 $ID = isset( $_POST[ 'ID' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'ID' ] ) ) : null;
104
105 if( $ID ) {
106 wp_send_json_success( get_the_title( $ID ), 200 );
107 }
108
109 wp_send_json_error( esc_html__( 'Failed to get template', 'emailkit' ), 400 );
110
111 exit;
112 }
113
114 /**
115 * Filter saved templates and send json response
116 */
117 function emailkit_filter_save_as_template( ) {
118
119 // verify request
120 if( empty( $_POST['nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce' ] ) ), 'emailkit_nonce' ) ) {
121 return false;
122 }
123
124 $template_id = isset( $_POST[ 'template_id' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'template_id' ] ) ) : null;
125 $saved_template_type = isset( $_POST[ 'email_type' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'email_type' ] ) ) : null;
126
127
128 $email_template_id = get_post_meta( $template_id, 'emailkit_template_id', true );
129 $email_type = get_post_meta( $email_template_id, 'emailkit_email_type', true );
130 $template_type = get_post_meta( $email_template_id, 'emailkit_template_type', true );
131
132
133
134 $dependency = Dependency::check( $email_type );
135
136 if( true !== $dependency ){
137 $need_plugin = [ 'dependency_status' => false, 'need_plugin' => $dependency ];
138 wp_send_json_success( $need_plugin );
139 exit;
140 }
141 if( true == $dependency ){
142 $need_plugin = [ 'dependency_status' => true, 'need_plugin' => $dependency ];
143 wp_send_json_success( $need_plugin );
144 exit;
145 }
146 }
147
148 /**
149 * Update individual template title and return json response
150 */
151 function update_template_data( ) {
152 // verify request
153 if( empty( $_POST[ 'nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ), 'emailkit_nonce' ) ) {
154 return false;
155 }
156
157 $ID = isset( $_POST[ 'id' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'id' ] ) ) : null;
158 $new_title = isset( $_POST[ 'title' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'title' ] ) ) : '';
159 $data = [];
160
161 $post_update = array(
162 'ID' => $ID,
163 'post_title' => $new_title
164 );
165
166 $ID = wp_update_post( $post_update );
167 $data[ 'templateId' ] = $ID;
168 $data[ 'title' ] = get_the_title( $ID );
169
170 if( isset( $_POST[ 'action_type' ] ) && sanitize_text_field( wp_unslash( $_POST[ 'action_type' ] ) ) == 'loadbuilder' ) {
171 $data[ 'builder_url' ] = admin_url( "post.php?post={$ID}&action=emailkit-builder" ); // building the editor loading url if user submitted for 'update and edit'
172 }
173
174 if( $ID ) {
175 wp_send_json_success( $data, 200 );
176 }
177 wp_send_json_error( esc_html__( 'Failed to get template', 'emailkit' ), 400 );
178
179 exit;
180 }
181
182 }