/var/www/html_us/wp-content/plugins/wp-smtp/src/Mail/AbstractController.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
<?php

namespace SolidWP\Mail;

/**
 * Interface InterfaceController
 *
 * This interface defines the contract that controllers in the SolidWP\Mail namespace should adhere to.
 * Controllers implementing this interface should provide a method to register hooks.
 *
 * @package Solid_SMTP
 */
abstract class AbstractController {

    
/**
     * Nonce name for each controller, default is a generic
     *
     * @var string
     */
    
protected string $nonce_name 'solid-wp-nonce';

    
/**
     * Option name for version
     */
    
const OPTION_VERSION_NAME 'solid_smtp_version';

    
/**
     * Register hooks.
     *
     * Implementing classes should use this method to register hooks with WordPress or other systems.
     *
     * @return void
     */
    
abstract public function register_hooks();

    
/**
     * Checks if the current user has the required capability and verifies nonce for a specific action.
     *
     * @param string $action The nonce action.
     *
     * @return bool True if the user has the capability and nonce is valid, false otherwise.
     */
    
protected function able_to_performstring $action '' ): bool {
        if ( ! 
current_user_can'manage_options' ) ) {
            return 
false;
        }

        
$nonce sanitize_text_fieldwp_unslash$_REQUEST$this->nonce_name ] ?? '' ) );

        if ( ! 
wp_verify_nonce$nonce$action ) ) {
            return 
false;
        }

        return 
true;
    }

    
/**
     * Retrieves input data, sanitizes it, and processes it by setting properties if possible.
     *
     * @param string $key The key of the input data.
     * @param string $default_value
     *
     * @return string
     */
    
protected function get_and_sanitize_inputstring $keystring $default_value '' ): string {
        
//phpcs:ignore
        
return sanitize_text_fieldwp_unslash$_REQUEST$key ] ?? $default_value ) );
    }

    
/**
     * Sanitize value from textarea.
     *
     * @param string $key
     *
     * @return string
     */
    
protected function get_and_sanitize_textareastring $key ): string {
        
//phpcs:ignore
        
return sanitize_textarea_fieldwp_unslash$_REQUEST$key ] ?? '' ) );
    }

    
/**
     * Converts a WP_Error object to an associative array.
     *
     * This method takes a WP_Error object and converts its error codes and messages
     * into an associative array where the keys are the error codes and the values
     * are the corresponding error messages.
     *
     * @param \WP_Error $error The WP_Error object to convert.
     *
     * @return array The associative array of error codes and messages.
     */
    
protected function wp_error_to_array\WP_Error $error ): array {
        
$errors = [];
        foreach ( 
$error->get_error_codes() as $code ) {
            
$errors$code ] = $error->get_error_message$code );
        }

        return 
$errors;
    }

    
/**
     * Just return generic error on ajax handling.
     *
     * @param string $error_message
     *
     * @return void
     */
    
protected function bail_out_generic_errorstring $error_message ): void {
        
wp_send_json_error(
            [
                
'message' => $error_message,
            ]
        );
    }
}