/var/www/html_de/wp-content/plugins/wp-smtp/src/Mail/Admin/ScreenConnectors.php


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
<?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\Hooks\PHPMailer;
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;

    
/**
     * Holds the test from email if provided.
     *
     * @var string|null
     */
    
private ?string $test_from_email null;

    
/**
     * ConnectionScreenController constructor.
     *
     * @param ConnectionService $connection_service The service for managing SMTP connections.
     */
    
public function __constructConnectionService $connection_serviceContainer $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' ] );

        
// to test different outgoing emails
        
add_filter'wp_mail_from', [ $this'configure_outgoing_test_from_email' ] );

        
// ajax functions.
        
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 = [
            
'from_email' => $this->get_and_sanitize_input'from_email' ),
            
'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,
                ]
            );
        }

        if ( 
$data['from_email'] ) {
            
$this->test_from_email $data['from_email'];
        }

        
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
        
$sent wp_mail$data['to_email'], $data['subject'], $data['message'] );

        
$this->test_from_email null;

        if ( ! 
PHPMailer::is_solid_mail_configured() ) {
            
wp_send_json_error(
                [
                    
'message' => __'Solid Mail did not process the test email.''LION' ),
                ]
            );
        }

        
// 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' ),
                ]
            );
        }
    }

    
/**
     * Use a test "from email" if provided by the user or preserve the value provided by WordPress.
     *
     * @param $wordpress_provided_from_email
     *
     * @return string
     */
    
public function configure_outgoing_test_from_email$wordpress_provided_from_email ): string {
        return 
$this->test_from_email ?? $wordpress_provided_from_email;
    }
}