SMTPfast screen. */ final class SMTPfast_Admin { const PAGE = 'smtpfast'; const CAPABILITY = 'manage_options'; const DOMAINS_CACHE = 'smtpfast_domains'; const ACCOUNT_CACHE = 'smtpfast_account'; const CACHE_SECONDS = 600; const REQUIRED_SCOPE = 'email:send'; const OPTIONAL_SCOPES = array( 'domain:read', 'email:read' ); const NOTICE_TRANSIENT = 'smtpfast_notice_'; /** * Registers the admin hooks. * * @return void */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'add_page' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); add_action( 'admin_notices', array( __CLASS__, 'failure_notice' ) ); add_action( 'admin_notices', array( __CLASS__, 'setup_notice' ) ); add_filter( 'plugin_action_links_' . plugin_basename( SMTPFAST_PLUGIN_FILE ), array( __CLASS__, 'action_links' ) ); foreach ( array( 'save_settings', 'delete_key', 'send_test', 'refresh_domains', 'clear_log', 'dismiss_failure' ) as $action ) { add_action( 'admin_post_smtpfast_' . $action, array( __CLASS__, 'handle_' . $action ) ); } } /** * Adds Settings > SMTPfast. * * @return void */ public static function add_page() { add_options_page( __( 'SMTPfast', 'smtpfast' ), __( 'SMTPfast', 'smtpfast' ), self::CAPABILITY, self::PAGE, array( __CLASS__, 'render_page' ) ); } /** * Loads the stylesheet on the settings page only. * * @param string $hook_suffix Current admin page. * @return void */ public static function enqueue( $hook_suffix ) { if ( 'settings_page_' . self::PAGE !== $hook_suffix ) { return; } wp_enqueue_style( 'smtpfast-admin', plugins_url( 'assets/admin.css', SMTPFAST_PLUGIN_FILE ), array(), SMTPFAST_VERSION ); } /** * Adds a Settings link on the Plugins screen. * * @param string[] $links Action links. * @return string[] */ public static function action_links( $links ) { array_unshift( $links, sprintf( '%s', esc_url( self::page_url() ), esc_html__( 'Settings', 'smtpfast' ) ) ); return $links; } /** * URL of a tab of the settings page. * * @param string $tab Tab. * @return string */ public static function page_url( $tab = 'general' ) { return add_query_arg( array( 'page' => self::PAGE, 'tab' => $tab, ), admin_url( 'options-general.php' ) ); } /** * Shows the last send failure on the Dashboard, the Plugins screen and the * SMTPfast settings page, until it is dismissed or the next email is sent. * * @return void */ public static function failure_notice() { if ( ! current_user_can( self::CAPABILITY ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->id, self::notice_screens(), true ) ) { return; } $failure = SMTPfast_Log::last_failure(); if ( null === $failure ) { return; } $count = isset( $failure['count'] ) ? (int) $failure['count'] : 1; ?>

id ) { return; } ?>

__( 'Settings', 'smtpfast' ), 'domains' => __( 'Domains', 'smtpfast' ), 'log' => __( 'Email log', 'smtpfast' ), ); $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only selects which tab to show. if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'general'; } ?>

'disabled' ) ); ?>
' . esc_html__( 'Not connected. WordPress sends email the default way until you add an API key.', 'smtpfast' ) . '

'; return; } $account = self::get_account(); if ( is_wp_error( $account ) ) { echo '

' . esc_html( /* translators: %s: error message. */ sprintf( __( 'The API key does not work: %s', 'smtpfast' ), $account->get_error_message() ) ) . '

'; return; } $scopes = isset( $account['scopes'] ) && is_array( $account['scopes'] ) ? $account['scopes'] : array(); $tier = isset( $account['account']['tier'] ) ? (string) $account['account']['tier'] : ''; if ( ! in_array( self::REQUIRED_SCOPE, $scopes, true ) ) { echo '

' . esc_html__( 'Connected, but this key cannot send email. Create a key with the email:send scope.', 'smtpfast' ) . '

'; return; } echo '

' . esc_html( '' !== $tier /* translators: %s: plan name. */ ? sprintf( __( 'Connected. All WordPress email goes through SMTPfast (%s plan).', 'smtpfast' ), ucfirst( $tier ) ) : __( 'Connected. All WordPress email goes through SMTPfast.', 'smtpfast' ) ) . '

'; $missing = array_diff( self::OPTIONAL_SCOPES, $scopes ); if ( ! empty( $missing ) ) { echo '

' . esc_html( /* translators: %s: list of API key scopes. */ sprintf( __( 'This key has no %s scope, so some status on this page is hidden.', 'smtpfast' ), implode( ', ', $missing ) ) ) . '

'; } } /** * The Domains tab. * * @return void */ private static function render_domains_tab() { if ( ! SMTPfast_Settings::has_api_key() ) { echo '

' . esc_html__( 'Add an API key on the Settings tab first.', 'smtpfast' ) . '

'; return; } $domains = self::get_domains(); ?>

get_error_message() ); ?>

' . esc_html__( 'Set a From email on the Settings tab so emails from plugins that use an unverified From domain can still be sent.', 'smtpfast' ) . '

'; return; } $verified = SMTPfast_Settings::is_verified_sender( $from ); if ( true === $verified ) { /* translators: %s: email address. */ echo '

' . esc_html( sprintf( __( 'Your From email %s is on a verified domain.', 'smtpfast' ), $from ) ) . '

'; } elseif ( false === $verified ) { /* translators: %s: email address. */ echo '

' . esc_html( sprintf( __( 'Your From email %s is not on a verified domain, so SMTPfast will refuse emails from it.', 'smtpfast' ), $from ) ) . '

'; } } /** * A status badge. * * @param string $status Status from the API. * @return void */ private static function render_badge( $status ) { $status = strtolower( $status ); $class = 'smtpfast-badge-wait'; if ( in_array( $status, array( 'verified', 'valid', 'pass', 'ok', 'delivered', 'sent', 'opened', 'clicked' ), true ) ) { $class = 'smtpfast-badge-ok'; } elseif ( in_array( $status, array( 'failed', 'invalid', 'missing', 'bounced', 'complained', 'canceled' ), true ) ) { $class = 'smtpfast-badge-bad'; } printf( '%s', esc_attr( $class ), esc_html( '' !== $status ? $status : __( 'unknown', 'smtpfast' ) ) ); } /** * The Email log tab: local entries, with the live status from GET /v1/emails. * * @return void */ private static function render_log_tab() { $entries = SMTPfast_Log::all(); $remote = array(); $note = ''; if ( ! empty( $entries ) && SMTPfast_Settings::has_api_key() ) { $api = new SMTPfast_API( SMTPfast_Settings::get_api_key() ); $emails = $api->list_emails( 100 ); if ( is_wp_error( $emails ) ) { /* translators: %s: error message. */ $note = sprintf( __( 'Live delivery status is not available: %s', 'smtpfast' ), $emails->get_error_message() ); } else { foreach ( $emails as $email ) { if ( isset( $email['id'] ) ) { $remote[ (string) $email['id'] ] = $email; } } } } ?>

get_error_message() ); $type = 'error'; } else { $message = __( 'Settings saved. The API key works.', 'smtpfast' ); self::get_domains(); } } } self::redirect( 'general', $type, $message ); } /** * Removes the saved API key. * * @return void */ public static function handle_delete_key() { self::check_capability(); check_admin_referer( 'smtpfast_delete_key' ); delete_option( SMTPfast_Settings::KEY_OPTION ); if ( SMTPfast_Settings::key_from_constant() ) { self::redirect( 'general', 'success', __( 'The saved API key was removed. The key from wp-config.php is still used.', 'smtpfast' ) ); } delete_option( SMTPfast_Settings::DOMAINS_OPTION ); self::clear_cache(); self::redirect( 'general', 'success', __( 'The API key was removed. WordPress sends email the default way again.', 'smtpfast' ) ); } /** * Sends a test email through wp_mail(). * * @return void */ public static function handle_send_test() { self::check_capability(); check_admin_referer( 'smtpfast_send_test' ); $to = isset( $_POST['smtpfast_test_to'] ) ? sanitize_email( wp_unslash( $_POST['smtpfast_test_to'] ) ) : ''; if ( ! is_email( $to ) ) { self::redirect( 'general', 'error', __( 'Enter a valid email address for the test.', 'smtpfast' ) ); } if ( ! SMTPfast_Settings::has_api_key() ) { self::redirect( 'general', 'error', __( 'Add an API key first.', 'smtpfast' ) ); } $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); /* translators: %s: site name. */ $subject = sprintf( __( 'SMTPfast test email from %s', 'smtpfast' ), $site ); $body = '

' . esc_html__( 'This test email was sent by the SMTPfast plugin through the SMTPfast API.', 'smtpfast' ) . '

' . '

' . esc_html( /* translators: %s: site URL. */ sprintf( __( 'Site: %s', 'smtpfast' ), home_url( '/' ) ) ) . '

'; $sent = wp_mail( $to, $subject, $body, array( 'Content-Type: text/html; charset=UTF-8' ) ); $result = SMTPfast_Mailer::last_result(); if ( $sent && is_array( $result ) && isset( $result['id'] ) ) { /* translators: 1: email address, 2: SMTPfast email id. */ self::redirect( 'general', 'success', sprintf( __( 'Test email sent to %1$s. SMTPfast email id: %2$s', 'smtpfast' ), $to, (string) $result['id'] ) ); } if ( is_wp_error( $result ) ) { $message = $sent /* translators: %s: error message. */ ? sprintf( __( 'SMTPfast failed (%s), so the fallback sent the test with the default WordPress mailer.', 'smtpfast' ), $result->get_error_message() ) /* translators: %s: error message. */ : sprintf( __( 'The test email failed: %s', 'smtpfast' ), $result->get_error_message() ); self::redirect( 'general', 'error', $message ); } self::redirect( 'general', $sent ? 'warning' : 'error', $sent ? __( 'WordPress sent the test, but not through SMTPfast. Another plugin may handle email on this site.', 'smtpfast' ) : __( 'The test email failed.', 'smtpfast' ) ); } /** * Clears the domain cache so the Domains tab checks again. * * @return void */ public static function handle_refresh_domains() { self::check_capability(); check_admin_referer( 'smtpfast_refresh_domains' ); self::clear_cache(); self::redirect( 'domains', 'success', __( 'Domain status refreshed.', 'smtpfast' ) ); } /** * Clears the local log. * * @return void */ public static function handle_clear_log() { self::check_capability(); check_admin_referer( 'smtpfast_clear_log' ); SMTPfast_Log::clear(); self::redirect( 'log', 'success', __( 'The log was cleared.', 'smtpfast' ) ); } /** * Dismisses the failure notice. * * @return void */ public static function handle_dismiss_failure() { self::check_capability(); check_admin_referer( 'smtpfast_dismiss_failure' ); SMTPfast_Log::clear_failure(); $referer = wp_get_referer(); wp_safe_redirect( $referer ? $referer : admin_url() ); exit; } /** * The account from GET /v1/me, cached for ten minutes. * * @return array|WP_Error */ private static function get_account() { $cached = get_transient( self::ACCOUNT_CACHE ); if ( is_array( $cached ) ) { return $cached; } $api = new SMTPfast_API( SMTPfast_Settings::get_api_key() ); $account = $api->get_me(); if ( ! is_wp_error( $account ) ) { set_transient( self::ACCOUNT_CACHE, $account, self::CACHE_SECONDS ); } return $account; } /** * The domains from GET /v1/domains, cached for ten minutes. Also stores the verified domain names. * * @return array>|WP_Error */ private static function get_domains() { $cached = get_transient( self::DOMAINS_CACHE ); if ( is_array( $cached ) ) { return $cached; } $api = new SMTPfast_API( SMTPfast_Settings::get_api_key() ); $domains = $api->list_domains(); if ( ! is_wp_error( $domains ) ) { SMTPfast_Settings::store_verified_domains( $domains ); set_transient( self::DOMAINS_CACHE, $domains, self::CACHE_SECONDS ); } return $domains; } /** * Clears the cached API answers. * * @return void */ private static function clear_cache() { delete_transient( self::ACCOUNT_CACHE ); delete_transient( self::DOMAINS_CACHE ); } /** * Stops unless the current user can manage options. * * @return void */ private static function check_capability() { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to change SMTPfast settings.', 'smtpfast' ), '', array( 'response' => 403 ) ); } } /** * Stores a one-time notice for the current user and goes back to the settings page. * * @param string $tab Tab to open. * @param string $type success, warning or error. * @param string $message Message. * @return void */ private static function redirect( $tab, $type, $message ) { set_transient( self::NOTICE_TRANSIENT . get_current_user_id(), array( 'type' => $type, 'message' => $message, ), 60 ); wp_safe_redirect( self::page_url( $tab ) ); exit; } /** * Shows and removes the one-time notice. * * @return void */ private static function render_flash_notice() { $key = self::NOTICE_TRANSIENT . get_current_user_id(); $notice = get_transient( $key ); if ( ! is_array( $notice ) || ! isset( $notice['type'], $notice['message'] ) ) { return; } delete_transient( $key ); $type = in_array( $notice['type'], array( 'success', 'warning', 'error' ), true ) ? $notice['type'] : 'info'; printf( '

%s

', esc_attr( $type ), esc_html( (string) $notice['message'] ) ); } }