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; ?>
' . 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( 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; } } } } ?>
|
' . 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%s