PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.5
EmailKit – Email Customizer for WooCommerce & WP v1.4.5
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.4.5, at includes/Admin/EmailKitAjax.php

180 lines 6.0 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 $template_name[] = [ "id" => $value[ 'id'], "text" => '( '. $value[ 'template_type'] .' ) '. $value[ 'title' ] ];
49 }else{
50 $template_name[] = [ "id" => $key, "text" => $value ];
51 }
52 }
53
54
55 $templates = [ ];
56 foreach( TemplateList::get_templates( ) as $template ):
57
58 if( isset( $template[ 'mail_type' ] ) && $email_type == $template[ 'mail_type' ] ) {
59 ob_start();
60
61 include EMAILKIT_DIR.'includes/views/modal-form-template-item.php';
62
63 $templates[ ] = ob_get_clean();
64 }
65
66 endforeach;
67
68 $data = [ 'templates' => $templates, 'template_name' => $template_name];
69
70
71 wp_send_json_success( $data );
72
73 exit;
74 }
75
76 /**
77 * It fetch exact mail lists from all mail template
78 * @return array
79 */
80 static function get_type_wise_mails( $type ) : array {
81
82 $type_list = [
83 'woocommerce' => EmailLists::woocommerce_email(),
84 'wordpress' => EmailLists::wordpress_email(),
85 'saved-templates' => EmailLists::saved_templates(),
86 ];
87
88 return $type_list[ $type ] ?? [];
89 }
90
91 /**
92 * Get single template title and send json response
93 */
94 function get_template_data() {
95
96 // verify request
97 if( empty( $_POST[ 'nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ), 'emailkit_nonce' ) ) {
98 return false;
99 }
100
101 $ID = isset( $_POST[ 'ID' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'ID' ] ) ) : null;
102
103 if( $ID ) {
104 wp_send_json_success( get_the_title( $ID ), 200 );
105 }
106
107 wp_send_json_error( esc_html__( 'Failed to get template', 'emailkit' ), 400 );
108
109 exit;
110 }
111
112 /**
113 * Filter saved templates and send json response
114 */
115 function emailkit_filter_save_as_template( ) {
116
117 // verify request
118 if( empty( $_POST['nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce' ] ) ), 'emailkit_nonce' ) ) {
119 return false;
120 }
121
122 $template_id = isset( $_POST[ 'template_id' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'template_id' ] ) ) : null;
123 $saved_template_type = isset( $_POST[ 'email_type' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'email_type' ] ) ) : null;
124
125
126 $email_template_id = get_post_meta( $template_id, 'emailkit_template_id', true );
127 $email_type = get_post_meta( $email_template_id, 'emailkit_email_type', true );
128 $template_type = get_post_meta( $email_template_id, 'emailkit_template_type', true );
129
130
131
132 $dependency = Dependency::check( $email_type );
133
134 if( true !== $dependency ){
135 $need_plugin = [ 'dependency_status' => false, 'need_plugin' => $dependency ];
136 wp_send_json_success( $need_plugin );
137 exit;
138 }
139 if( true == $dependency ){
140 $need_plugin = [ 'dependency_status' => true, 'need_plugin' => $dependency ];
141 wp_send_json_success( $need_plugin );
142 exit;
143 }
144 }
145
146 /**
147 * Update individual template title and return json response
148 */
149 function update_template_data( ) {
150 // verify request
151 if( empty( $_POST[ 'nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ), 'emailkit_nonce' ) ) {
152 return false;
153 }
154
155 $ID = isset( $_POST[ 'id' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'id' ] ) ) : null;
156 $new_title = isset( $_POST[ 'title' ] ) ? sanitize_text_field( wp_unslash( $_POST[ 'title' ] ) ) : '';
157 $data = [];
158
159 $post_update = array(
160 'ID' => $ID,
161 'post_title' => $new_title
162 );
163
164 $ID = wp_update_post( $post_update );
165 $data[ 'templateId' ] = $ID;
166 $data[ 'title' ] = get_the_title( $ID );
167
168 if( isset( $_POST[ 'action_type' ] ) && sanitize_text_field( wp_unslash( $_POST[ 'action_type' ] ) ) == 'loadbuilder' ) {
169 $data[ 'builder_url' ] = admin_url( "post.php?post={$ID}&action=emailkit-builder" ); // building the editor loading url if user submitted for 'update and edit'
170 }
171
172 if( $ID ) {
173 wp_send_json_success( $data, 200 );
174 }
175 wp_send_json_error( esc_html__( 'Failed to get template', 'emailkit' ), 400 );
176
177 exit;
178 }
179
180 }