Crypto.php
5 days ago
DB.php
5 days ago
Data.php
5 days ago
Helpers.php
5 days ago
PluginImportDataRetriever.php
5 days ago
UI.php
5 days ago
PluginImportDataRetriever.php
324 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Helpers; |
| 4 | |
| 5 | /** |
| 6 | * Class for preparing import data from other SMTP plugins. |
| 7 | * |
| 8 | * @since 2.6.0 |
| 9 | */ |
| 10 | class PluginImportDataRetriever { |
| 11 | |
| 12 | /** |
| 13 | * The slug of the SMTP plugin to prepare the data for. |
| 14 | * |
| 15 | * @since 2.6.0 |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | private $slug; |
| 20 | |
| 21 | /** |
| 22 | * PluginImportDataRetriever constructor. |
| 23 | * |
| 24 | * @since 2.6.0 |
| 25 | * |
| 26 | * @param string $slug The SMTP plugin slug. |
| 27 | */ |
| 28 | public function __construct( $slug ) { |
| 29 | |
| 30 | $this->slug = $slug; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the data for the current plugin slug. |
| 35 | * |
| 36 | * @since 2.6.0 |
| 37 | * |
| 38 | * @return false|array |
| 39 | */ |
| 40 | public function get() { |
| 41 | |
| 42 | $method_name = preg_replace( '/[\-]/', '_', sanitize_key( "get_$this->slug" ) ); |
| 43 | |
| 44 | if ( method_exists( $this, $method_name ) ) { |
| 45 | return $this->$method_name(); |
| 46 | } |
| 47 | |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Check if Easy WP SMTP plugin settings are present and extract them. |
| 53 | * |
| 54 | * @since 2.6.0 |
| 55 | * |
| 56 | * @return array |
| 57 | */ |
| 58 | private function get_easy_smtp() { |
| 59 | |
| 60 | $options = get_option( 'swpsmtp_options' ); |
| 61 | |
| 62 | if ( empty( $options ) ) { |
| 63 | return []; |
| 64 | } |
| 65 | |
| 66 | return [ |
| 67 | 'mail' => [ |
| 68 | 'mailer' => 'smtp', |
| 69 | 'from_email' => isset( $options['from_email_field'] ) ? $options['from_email_field'] : '', |
| 70 | 'from_name' => isset( $options['from_name_field'] ) ? $options['from_name_field'] : '', |
| 71 | 'from_name_force' => isset( $options['force_from_name_replace'] ) ? $options['force_from_name_replace'] : false, |
| 72 | ], |
| 73 | 'smtp' => [ |
| 74 | 'host' => isset( $options['smtp_settings']['host'] ) ? $options['smtp_settings']['host'] : '', |
| 75 | 'encryption' => isset( $options['smtp_settings']['type_encryption'] ) ? $options['smtp_settings']['type_encryption'] : 'none', |
| 76 | 'port' => isset( $options['smtp_settings']['port'] ) ? $options['smtp_settings']['port'] : 25, |
| 77 | 'auth' => isset( $options['smtp_settings']['autentication'] ) ? $options['smtp_settings']['autentication'] : true, |
| 78 | 'user' => isset( $options['smtp_settings']['username'] ) ? $options['smtp_settings']['username'] : '', |
| 79 | 'pass' => '', |
| 80 | 'autotls' => true, |
| 81 | ], |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Check if FluentSMTP plugin settings are present and extract them. |
| 87 | * |
| 88 | * @since 3.2.0 |
| 89 | * |
| 90 | * @return array |
| 91 | */ |
| 92 | private function get_fluent_smtp() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 93 | |
| 94 | $options = get_option( 'fluentmail-settings' ); |
| 95 | |
| 96 | if ( empty( $options ) ) { |
| 97 | return []; |
| 98 | } |
| 99 | |
| 100 | if ( empty( $options['misc']['default_connection'] ) || empty( $options['connections'][ $options['misc']['default_connection'] ]['provider_settings'] ) ) { |
| 101 | return []; |
| 102 | } |
| 103 | |
| 104 | $fluent_data = $options['connections'][ $options['misc']['default_connection'] ]['provider_settings']; |
| 105 | |
| 106 | $allowed_mailers = [ |
| 107 | 'smtp' => 'smtp', |
| 108 | 'ses' => 'amazonses', |
| 109 | 'mailgun' => 'mailgun', |
| 110 | 'sendgrid' => 'sendgrid', |
| 111 | 'sendinblue' => 'sendinblue', |
| 112 | 'sparkpost' => 'sparkpost', |
| 113 | 'postmark' => 'postmark', |
| 114 | 'outlook' => 'outlook', |
| 115 | ]; |
| 116 | |
| 117 | if ( empty( $fluent_data['provider'] ) || ! in_array( $fluent_data['provider'], array_keys( $allowed_mailers ), true ) ) { |
| 118 | return []; |
| 119 | } |
| 120 | |
| 121 | $data = [ |
| 122 | 'mail' => [ |
| 123 | 'mailer' => $allowed_mailers[ $fluent_data['provider'] ], |
| 124 | 'from_email' => isset( $fluent_data['sender_email'] ) ? $fluent_data['sender_email'] : '', |
| 125 | 'from_name' => isset( $fluent_data['sender_name'] ) ? $fluent_data['sender_name'] : '', |
| 126 | 'from_email_force' => isset( $fluent_data['force_from_email'] ) && $fluent_data['force_from_email'] === 'yes', |
| 127 | 'from_name_force' => isset( $fluent_data['force_from_name'] ) && $fluent_data['force_from_name'] === 'yes', |
| 128 | ], |
| 129 | ]; |
| 130 | |
| 131 | switch ( $data['mail']['mailer'] ) { |
| 132 | case 'smtp': |
| 133 | $data['smtp'] = [ |
| 134 | 'host' => isset( $fluent_data['host'] ) ? $fluent_data['host'] : '', |
| 135 | 'encryption' => isset( $fluent_data['encryption'] ) && in_array( $fluent_data['encryption'], [ 'none', 'ssl', 'tls' ], true ) ? $fluent_data['encryption'] : 'none', |
| 136 | 'port' => isset( $fluent_data['port'] ) ? $fluent_data['port'] : 25, |
| 137 | 'auth' => isset( $fluent_data['auth'] ) && $fluent_data['auth'] === 'yes', |
| 138 | 'user' => isset( $fluent_data['username'] ) ? $fluent_data['username'] : '', |
| 139 | 'pass' => isset( $fluent_data['password'] ) ? $fluent_data['password'] : '', |
| 140 | 'autotls' => isset( $fluent_data['auto_tls'] ) && $fluent_data['auto_tls'] === 'yes', |
| 141 | ]; |
| 142 | break; |
| 143 | |
| 144 | case 'amazonses': |
| 145 | $data['amazonses'] = [ |
| 146 | 'client_id' => isset( $fluent_data['access_key'] ) ? $fluent_data['access_key'] : '', |
| 147 | 'client_secret' => isset( $fluent_data['secret_key'] ) ? $fluent_data['secret_key'] : '', |
| 148 | 'region' => isset( $fluent_data['region'] ) ? $fluent_data['region'] : '', |
| 149 | ]; |
| 150 | break; |
| 151 | |
| 152 | case 'mailgun': |
| 153 | $data['mailgun'] = [ |
| 154 | 'api_key' => isset( $fluent_data['api_key'] ) ? $fluent_data['api_key'] : '', |
| 155 | 'domain' => isset( $fluent_data['domain_name'] ) ? $fluent_data['domain_name'] : '', |
| 156 | 'region' => isset( $fluent_data['region'] ) && in_array( $fluent_data['region'], [ 'us', 'eu' ], true ) ? strtoupper( $fluent_data['region'] ) : '', |
| 157 | ]; |
| 158 | break; |
| 159 | |
| 160 | case 'sendgrid': |
| 161 | $data['sendgrid'] = [ |
| 162 | 'api_key' => isset( $fluent_data['api_key'] ) ? $fluent_data['api_key'] : '', |
| 163 | ]; |
| 164 | break; |
| 165 | |
| 166 | case 'sendinblue': |
| 167 | $data['sendinblue'] = [ |
| 168 | 'api_key' => isset( $fluent_data['api_key'] ) ? $fluent_data['api_key'] : '', |
| 169 | ]; |
| 170 | break; |
| 171 | |
| 172 | case 'sparkpost': |
| 173 | $data['sparkpost'] = [ |
| 174 | 'api_key' => isset( $fluent_data['api_key'] ) ? $fluent_data['api_key'] : '', |
| 175 | ]; |
| 176 | break; |
| 177 | |
| 178 | case 'postmark': |
| 179 | $data['postmark'] = [ |
| 180 | 'api_key' => isset( $fluent_data['api_key'] ) ? $fluent_data['api_key'] : '', |
| 181 | 'message_stream' => isset( $fluent_data['message_stream'] ) ? $fluent_data['message_stream'] : '', |
| 182 | ]; |
| 183 | break; |
| 184 | |
| 185 | case 'outlook': |
| 186 | $data['outlook'] = [ |
| 187 | 'client_id' => isset( $fluent_data['client_id'] ) ? $fluent_data['client_id'] : '', |
| 188 | 'client_secret' => isset( $fluent_data['client_secret'] ) ? $fluent_data['client_secret'] : '', |
| 189 | ]; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | return $data; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Check if Post SMTP Mailer plugin settings are present and extract them. |
| 198 | * |
| 199 | * @since 2.6.0 |
| 200 | * |
| 201 | * @return array |
| 202 | */ |
| 203 | private function get_post_smtp_mailer() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 204 | |
| 205 | $options = get_option( 'postman_options' ); |
| 206 | |
| 207 | if ( empty( $options ) ) { |
| 208 | return []; |
| 209 | } |
| 210 | |
| 211 | $allowed_mailers = [ |
| 212 | 'smtp' => 'smtp', |
| 213 | 'gmail_api' => 'gmail', |
| 214 | 'sendgrid_api' => 'sendgrid', |
| 215 | 'mailgun_api' => 'mailgun', |
| 216 | ]; |
| 217 | |
| 218 | $data = [ |
| 219 | 'mail' => [ |
| 220 | 'mailer' => ( isset( $options['transport_type'] ) && in_array( $options['transport_type'], array_keys( $allowed_mailers ), true ) ) ? $allowed_mailers[ $options['transport_type'] ] : 'mail', |
| 221 | 'from_email' => isset( $options['sender_email'] ) ? $options['sender_email'] : '', |
| 222 | 'from_name' => isset( $options['sender_name'] ) ? $options['sender_name'] : '', |
| 223 | ], |
| 224 | 'smtp' => [ |
| 225 | 'host' => isset( $options['hostname'] ) ? $options['hostname'] : '', |
| 226 | 'encryption' => isset( $options['enc_type'] ) ? $options['enc_type'] : 'none', |
| 227 | 'port' => isset( $options['port'] ) ? $options['port'] : 25, |
| 228 | 'auth' => isset( $options['auth_type'] ) && $options['auth_type'] !== 'none', |
| 229 | 'user' => isset( $options['basic_auth_username'] ) ? $options['basic_auth_username'] : '', |
| 230 | 'pass' => ! empty( $options['basic_auth_password'] ) ? base64_decode( $options['basic_auth_password'] ) : '', // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 231 | 'autotls' => true, |
| 232 | ], |
| 233 | 'gmail' => [ |
| 234 | 'client_id' => isset( $options['oauth_client_id'] ) ? $options['oauth_client_id'] : '', |
| 235 | 'client_secret' => isset( $options['oauth_client_secret'] ) ? $options['oauth_client_secret'] : '', |
| 236 | ], |
| 237 | 'sendgrid' => [ |
| 238 | 'api_key' => ! empty( $options['sendgrid_api_key'] ) ? base64_decode( $options['sendgrid_api_key'] ) : '', // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 239 | ], |
| 240 | 'mailgun' => [ |
| 241 | 'api_key' => ! empty( $options['mailgun_api_key'] ) ? base64_decode( $options['mailgun_api_key'] ) : '', // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 242 | 'domain' => isset( $options['mailgun_domain_name'] ) ? $options['mailgun_domain_name'] : '', |
| 243 | 'region' => ( isset( $options['mailgun_region'] ) && ! empty( $options['mailgun_region'] ) ) ? 'EU' : 'US', |
| 244 | ], |
| 245 | ]; |
| 246 | |
| 247 | if ( class_exists( '\PostmanOptions' ) ) { |
| 248 | $pm_options = \PostmanOptions::getInstance(); |
| 249 | |
| 250 | $data['sendgrid']['api_key'] = $pm_options->getSendGridApiKey(); |
| 251 | $data['mailgun']['api_key'] = $pm_options->getMailgunApiKey(); |
| 252 | $data['smtp']['pass'] = $pm_options->getPassword(); |
| 253 | } |
| 254 | |
| 255 | return $data; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Check if SMTP Mailer plugin settings are present and extract them. |
| 260 | * |
| 261 | * @since 2.6.0 |
| 262 | * |
| 263 | * @return array |
| 264 | */ |
| 265 | private function get_smtp_mailer() { |
| 266 | |
| 267 | $options = get_option( 'smtp_mailer_options' ); |
| 268 | |
| 269 | if ( empty( $options ) ) { |
| 270 | return []; |
| 271 | } |
| 272 | |
| 273 | return [ |
| 274 | 'mail' => [ |
| 275 | 'mailer' => 'smtp', |
| 276 | 'from_email' => isset( $options['from_email'] ) ? $options['from_email'] : '', |
| 277 | 'from_name' => isset( $options['from_name'] ) ? $options['from_name'] : '', |
| 278 | ], |
| 279 | 'smtp' => [ |
| 280 | 'host' => isset( $options['smtp_host'] ) ? $options['smtp_host'] : '', |
| 281 | 'encryption' => isset( $options['type_of_encryption'] ) ? $options['type_of_encryption'] : 'none', |
| 282 | 'port' => isset( $options['smtp_port'] ) ? $options['smtp_port'] : 25, |
| 283 | 'auth' => isset( $options['smtp_auth'] ) && $options['smtp_auth'] === 'true', |
| 284 | 'user' => isset( $options['smtp_username'] ) ? $options['smtp_username'] : '', |
| 285 | 'pass' => ! empty( $options['smtp_password'] ) ? base64_decode( $options['smtp_password'] ) : '', // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 286 | 'autotls' => true, |
| 287 | ], |
| 288 | ]; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Check if WP SMTP plugin settings are present and extract them. |
| 293 | * |
| 294 | * @since 2.6.0 |
| 295 | * |
| 296 | * @return array |
| 297 | */ |
| 298 | private function get_wp_smtp() { |
| 299 | |
| 300 | $options = get_option( 'wp_smtp_options' ); |
| 301 | |
| 302 | if ( empty( $options ) ) { |
| 303 | return []; |
| 304 | } |
| 305 | |
| 306 | return [ |
| 307 | 'mail' => [ |
| 308 | 'mailer' => 'smtp', |
| 309 | 'from_email' => isset( $options['from'] ) ? $options['from'] : '', |
| 310 | 'from_name' => isset( $options['fromname'] ) ? $options['fromname'] : '', |
| 311 | ], |
| 312 | 'smtp' => [ |
| 313 | 'host' => isset( $options['host'] ) ? $options['host'] : '', |
| 314 | 'encryption' => ! empty( $options['smtpsecure'] ) ? $options['smtpsecure'] : 'none', |
| 315 | 'port' => isset( $options['port'] ) ? $options['port'] : 25, |
| 316 | 'auth' => isset( $options['smtpauth'] ) && $options['smtpauth'] === 'yes', |
| 317 | 'user' => isset( $options['username'] ) ? $options['username'] : '', |
| 318 | 'pass' => isset( $options['password'] ) ? $options['password'] : '', |
| 319 | 'autotls' => true, |
| 320 | ], |
| 321 | ]; |
| 322 | } |
| 323 | } |
| 324 |