1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
<?php /** * ConnectionScreenController.php * * This file contains the ConnectionScreenController class which handles the connection screen logic * for the Solid SMTP plugin. * * @package Solid_SMTP\Controller */
namespace SolidWP\Mail\Admin;
use SolidWP\Mail\Container; use SolidWP\Mail\AbstractController; use SolidWP\Mail\Repository\ProvidersRepository; use SolidWP\Mail\Service\ConnectionService;
/** * Class ConnectionScreenController * * Handles the connection screen logic for the Solid SMTP plugin. */ class ScreenConnectors extends AbstractController {
/** * Nonce name for this screen. * * @var string */ protected string $nonce_name = 'solidwp_mail_connections_nonce';
/** * Store the email error if any. * * @var string */ protected string $email_error = '';
/** * The service for managing SMTP connections. * * @var ConnectionService */ protected ConnectionService $connection_service;
/** * Container. * * @var Container */ private Container $container;
/** * ConnectionScreenController constructor. * * @param ConnectionService $connection_service The service for managing SMTP connections. */ public function __construct( ConnectionService $connection_service, Container $container ) { $this->connection_service = $connection_service; $this->container = $container; }
/** * Registers the AJAX hooks for the connection screen. * * @return void */ public function register_hooks() { // record error for debug. add_action( 'wp_mail_failed', [ $this, 'record_error' ] );
// ajax functions. add_action( 'wp_ajax_solidwp_mail_save_connection', [ $this, 'save_connection' ] ); add_action( 'wp_ajax_solidwp_mail_delete_connection', [ $this, 'delete_connection' ] ); add_action( 'wp_ajax_solidwp_mail_make_provider_active', [ $this, 'make_connection_active' ] ); add_action( 'wp_ajax_solidwp_mail_send_test_email', [ $this, 'send_test_email' ] ); }
/** * Records an error message. * * This method handles recording an error message from a `WP_Error` object. * It extracts the error message from the `WP_Error` object and stores it in * the `email_error` property. * * @param \WP_Error $wp_error The `WP_Error` object containing the error message. * * @return void */ public function record_error( \WP_Error $wp_error ) { $this->email_error = $wp_error->get_error_message(); }
/** * Sends a test email. * * This method handles the AJAX request to send a test email. It validates the input, * attempts to send the email using the `wp_mail` function, and returns a JSON response * indicating success or failure. * * @return void */ public function send_test_email() { // Check if the current user has permission to perform this action. if ( ! $this->able_to_perform( 'send_test_email' ) ) { $this->bail_out_generic_error( __( 'User cannot send test emails.', 'LION' ) ); }
// Sanitize and retrieve input data. $data = [ 'to_email' => $this->get_and_sanitize_input( 'to_email' ), 'subject' => $this->get_and_sanitize_input( 'subject' ), 'message' => $this->get_and_sanitize_textarea( 'message' ), ];
$result = $this->connection_service->validate_test_email_input( $data ); if ( is_array( $result ) ) { wp_send_json_error( [ 'validation' => $result, ] ); }
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail $sent = wp_mail( $data['to_email'], $data['subject'], $data['message'] ); // Return a JSON response indicating success or failure. if ( $sent ) { wp_send_json_success( [ 'message' => __( 'Test email sent successfully.', 'LION' ) ] ); } else { $wp_error = $this->container->get( 'phpmailer_send_error' ); if ( is_wp_error( $wp_error ) ) { // error found, send more detailed version. $error_message = $wp_error->get_error_message(); wp_send_json_error( [ /* translators: %s: PHPMailer error */ 'message' => sprintf( __( 'Failed to send test email. Error: %s', 'LION' ), $error_message ), ] ); } // falling back. wp_send_json_error( [ 'message' => __( 'Failed to send test email.', 'LION' ), ] ); } }
/** * Deletes the connection. * * @return void */ public function delete_connection() { if ( ! $this->able_to_perform( 'delete_connection' ) ) { $this->bail_out_generic_error( __( 'User cannot delete providers.', 'LION' ) ); }
$provider_id = $this->get_and_sanitize_input( 'provider_id' ); if ( empty( $provider_id ) ) { $this->bail_out_generic_error( __( 'Provider ID cannot be empty.', 'LION' ) ); }
$model = $this->container->get( ProvidersRepository::class )->get_active_provider();
if ( is_object( $model ) && $model->get_id() === $provider_id ) { // this is the active provider, should not allow for delete. wp_send_json_error( [ 'message' => __( 'Cannot delete the connection because it is set as the default.', 'LION' ), ] ); }
wp_send_json_success( $this->connection_service->delete_connection( $provider_id ) ); }
/** * Saves a new SMTP connection. * * Validates the input data, processes it, and saves the SMTP connection model if validation succeeds. * If validation fails or the user lacks the necessary permissions, an error response is sent. * * @return void */ public function save_connection() { if ( ! $this->able_to_perform( 'save_connection' ) ) { $this->bail_out_generic_error( __( 'User cannot add providers.', 'LION' ) ); }
// populate data. $name = $this->get_and_sanitize_input( 'name' );
if ( empty( $name ) ) { $this->bail_out_generic_error( __( 'Name cannot be empty.', 'LION' ) ); }
//phpcs:ignore. $data = wp_unslash( $_POST );
$result = $this->connection_service->save_connection( $data );
if ( true === $result ) { wp_send_json_success( $this->container->get( ProvidersRepository::class )->get_all_providers_as_array() ); }
wp_send_json_error( $this->wp_error_to_array( $result ) ); }
/** * Makes a provider active. * * @return void */ public function make_connection_active() { if ( ! $this->able_to_perform( 'make_connection_active' ) ) { $this->bail_out_generic_error( __( 'User cannot change active providers.', 'LION' ) ); }
$provider_id = $this->get_and_sanitize_input( 'provider_id' ); if ( empty( $provider_id ) ) { $this->bail_out_generic_error( __( 'Provider cannot be empty.', 'LION' ) ); }
$this->connection_service->make_provider_active( $provider_id );
wp_send_json_success( $this->container->get( ProvidersRepository::class )->get_all_providers_as_array() ); } }
|