AmazonSES
5 days ago
ElasticEmail
5 days ago
Gmail
5 days ago
Mail
5 days ago
MailerSend
5 days ago
Mailgun
5 days ago
Mailjet
5 days ago
Mandrill
5 days ago
Outlook
5 days ago
Pepipost
5 days ago
PepipostAPI
5 days ago
Postmark
5 days ago
Resend
5 days ago
SMTP
5 days ago
SMTP2GO
5 days ago
SMTPcom
5 days ago
Sendgrid
5 days ago
Sendinblue
5 days ago
Sendlayer
5 days ago
SparkPost
5 days ago
Zoho
5 days ago
AuthAbstract.php
5 days ago
AuthInterface.php
5 days ago
Loader.php
5 days ago
MailerAbstract.php
5 days ago
MailerInterface.php
5 days ago
OptionsAbstract.php
5 days ago
OptionsInterface.php
5 days ago
AuthAbstract.php
246 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Providers; |
| 4 | |
| 5 | use WPMailSMTP\ConnectionInterface; |
| 6 | use WPMailSMTP\Options; |
| 7 | |
| 8 | /** |
| 9 | * Class AuthAbstract. |
| 10 | * |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | abstract class AuthAbstract implements AuthInterface { |
| 14 | |
| 15 | /** |
| 16 | * The Connection object. |
| 17 | * |
| 18 | * @since 3.7.0 |
| 19 | * |
| 20 | * @var ConnectionInterface |
| 21 | */ |
| 22 | protected $connection; |
| 23 | |
| 24 | /** |
| 25 | * The connection options object. |
| 26 | * |
| 27 | * @since 3.7.0 |
| 28 | * |
| 29 | * @var Options |
| 30 | */ |
| 31 | protected $connection_options; |
| 32 | |
| 33 | /** |
| 34 | * Mailer DB options. |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | protected $options = []; |
| 41 | |
| 42 | /** |
| 43 | * @since 1.0.0 |
| 44 | * |
| 45 | * @var mixed |
| 46 | */ |
| 47 | protected $client; |
| 48 | |
| 49 | /** |
| 50 | * Mailer slug. |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | protected $mailer_slug = ''; |
| 57 | |
| 58 | /** |
| 59 | * Key for a stored unique state value. |
| 60 | * |
| 61 | * @since 1.5.0 |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
| 65 | public $state_key = 'wp_mail_smtp_provider_client_state'; |
| 66 | |
| 67 | /** |
| 68 | * Auth constructor. |
| 69 | * |
| 70 | * @since 1.0.0 |
| 71 | * |
| 72 | * @param ConnectionInterface $connection The Connection object. |
| 73 | */ |
| 74 | public function __construct( $connection = null ) { |
| 75 | |
| 76 | if ( ! is_null( $connection ) ) { |
| 77 | $this->connection = $connection; |
| 78 | } else { |
| 79 | $this->connection = wp_mail_smtp()->get_connections_manager()->get_primary_connection(); |
| 80 | } |
| 81 | |
| 82 | $this->connection_options = $this->connection->get_options(); |
| 83 | $this->mailer_slug = $this->connection->get_mailer_slug(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Use the composer autoloader to include the auth library and all dependencies. |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | protected function include_vendor_lib() { |
| 92 | |
| 93 | require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php'; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the url, that users will be redirected back to finish the OAuth process. |
| 98 | * |
| 99 | * @since 1.0.0 |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public static function get_plugin_auth_url() { |
| 104 | |
| 105 | return add_query_arg( 'tab', 'auth', wp_mail_smtp()->get_admin()->get_admin_page_url() ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Update auth code in our DB. |
| 110 | * |
| 111 | * @since 1.0.0 |
| 112 | * |
| 113 | * @param string $code |
| 114 | */ |
| 115 | protected function update_auth_code( $code ) { |
| 116 | |
| 117 | $all = $this->connection_options->get_all(); |
| 118 | |
| 119 | // To save in DB. |
| 120 | $all[ $this->mailer_slug ]['auth_code'] = $code; |
| 121 | |
| 122 | // To save in currently retrieved options array. |
| 123 | $this->options['auth_code'] = $code; |
| 124 | |
| 125 | // NOTE: These options need to be saved by overwriting all options, because WP automatic updates can cause an issue: GH #575! |
| 126 | $this->connection_options->set( $all, false, true ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Update Setup Wizard flag in our DB. |
| 131 | * |
| 132 | * @since 2.6.0 |
| 133 | * |
| 134 | * @param boolean $state A state (true/false) to set the is_setup_wizard_auth mailer setting. |
| 135 | */ |
| 136 | public function update_is_setup_wizard_auth( $state ) { |
| 137 | |
| 138 | $all = $this->connection_options->get_all(); |
| 139 | |
| 140 | // To save in DB. |
| 141 | $all[ $this->mailer_slug ]['is_setup_wizard_auth'] = (bool) $state; |
| 142 | |
| 143 | // To save in currently retrieved options array. |
| 144 | $this->options['is_setup_wizard_auth'] = (bool) $state; |
| 145 | |
| 146 | // NOTE: These options need to be saved by overwriting all options, because WP automatic updates can cause an issue: GH #575! |
| 147 | $this->connection_options->set( $all, false, true ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Update access token in our DB. |
| 152 | * |
| 153 | * @since 1.0.0 |
| 154 | * |
| 155 | * @param mixed $token |
| 156 | */ |
| 157 | protected function update_access_token( $token ) { |
| 158 | |
| 159 | $all = $this->connection_options->get_all(); |
| 160 | |
| 161 | // To save in DB. |
| 162 | $all[ $this->mailer_slug ]['access_token'] = $token; |
| 163 | |
| 164 | // To save in currently retrieved options array. |
| 165 | $this->options['access_token'] = $token; |
| 166 | |
| 167 | // NOTE: These options need to be saved by overwriting all options, because WP automatic updates can cause an issue: GH #575! |
| 168 | $this->connection_options->set( $all, false, true ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Update refresh token in our DB. |
| 173 | * |
| 174 | * @since 1.0.0 |
| 175 | * |
| 176 | * @param mixed $token |
| 177 | */ |
| 178 | protected function update_refresh_token( $token ) { |
| 179 | |
| 180 | $all = $this->connection_options->get_all(); |
| 181 | |
| 182 | // To save in DB. |
| 183 | $all[ $this->mailer_slug ]['refresh_token'] = $token; |
| 184 | |
| 185 | // To save in currently retrieved options array. |
| 186 | $this->options['refresh_token'] = $token; |
| 187 | |
| 188 | // NOTE: These options need to be saved by overwriting all options, because WP automatic updates can cause an issue: GH #575! |
| 189 | $this->connection_options->set( $all, false, true ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Update access token scopes in our DB. |
| 194 | * |
| 195 | * @since 3.4.0 |
| 196 | * |
| 197 | * @param array $scopes Scopes array. |
| 198 | */ |
| 199 | protected function update_scopes( $scopes ) { |
| 200 | |
| 201 | $all = $this->connection_options->get_all(); |
| 202 | |
| 203 | // To save in DB. |
| 204 | $all[ $this->mailer_slug ]['scopes'] = $scopes; |
| 205 | |
| 206 | // To save in currently retrieved options array. |
| 207 | $this->options['scopes'] = $scopes; |
| 208 | |
| 209 | // NOTE: These options need to be saved by overwriting all options, because WP automatic updates can cause an issue: GH #575! |
| 210 | $this->connection_options->set( $all, false, true ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get state value that should be used for `state` parameter in OAuth authorization request. |
| 215 | * |
| 216 | * @since 3.7.0 |
| 217 | * |
| 218 | * @return string |
| 219 | */ |
| 220 | protected function get_state() { |
| 221 | |
| 222 | $state = [ |
| 223 | wp_create_nonce( $this->state_key ), |
| 224 | $this->connection->get_id(), |
| 225 | ]; |
| 226 | |
| 227 | return implode( '-', $state ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @inheritdoc |
| 232 | */ |
| 233 | public function is_clients_saved() { |
| 234 | |
| 235 | return ! empty( $this->options['client_id'] ) && ! empty( $this->options['client_secret'] ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @inheritdoc |
| 240 | */ |
| 241 | public function is_auth_required() { |
| 242 | |
| 243 | return empty( $this->options['access_token'] ) || empty( $this->options['refresh_token'] ); |
| 244 | } |
| 245 | } |
| 246 |