From c61784411988d36d9bbd93cd3a97e773990af342 Mon Sep 17 00:00:00 2001 From: yehudah Date: Mon, 25 Nov 2019 08:22:35 +0000 Subject: Adding a folder --- .../PostmanSendTestEmailController.php | 358 +++++++++++++++++++++ 1 file changed, 358 insertions(+) create mode 100644 Postman/Postman-Send-Test-Email/PostmanSendTestEmailController.php (limited to 'Postman/Postman-Send-Test-Email/PostmanSendTestEmailController.php') diff --git a/Postman/Postman-Send-Test-Email/PostmanSendTestEmailController.php b/Postman/Postman-Send-Test-Email/PostmanSendTestEmailController.php new file mode 100644 index 0000000..1a207c8 --- /dev/null +++ b/Postman/Postman-Send-Test-Email/PostmanSendTestEmailController.php @@ -0,0 +1,358 @@ +logger = new PostmanLogger( get_class( $this ) ); + $this->rootPluginFilenameAndPath = $rootPluginFilenameAndPath; + $this->options = PostmanOptions::getInstance(); + + PostmanUtils::registerAdminMenu( $this, 'addEmailTestSubmenu' ); + + // hook on the init event + add_action( 'init', array( + $this, + 'on_init', + ) ); + + // initialize the scripts, stylesheets and form fields + add_action( 'admin_init', array( + $this, + 'on_admin_init', + ) ); + } + + /** + * Functions to execute on the init event + * + * "Typically used by plugins to initialize. The current user is already authenticated by this time." + * ref: http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request + */ + public function on_init() { + // register Ajax handlers + new PostmanSendTestEmailAjaxController(); + } + + /** + * Fires on the admin_init method + */ + public function on_admin_init() { + $this->registerStylesAndScripts(); + } + + /** + * Get the settings option array and print one of its values + */ + public function test_email_callback() { + printf( '', self::RECIPIENT_EMAIL_FIELD_NAME, wp_get_current_user()->user_email ); + } + + /** + * Register and add settings + */ + private function registerStylesAndScripts() { + if ( $this->logger->isTrace() ) { + $this->logger->trace( 'registerStylesAndScripts()' ); + } + + $pluginData = apply_filters( 'postman_get_plugin_metadata', null ); + + // register the stylesheet resource + wp_register_style( 'postman_send_test_email', plugins_url( 'Postman/Postman-Send-Test-Email/postman_send_test_email.css', $this->rootPluginFilenameAndPath ), PostmanViewController::POSTMAN_STYLE, $pluginData ['version'] ); + + // register the javascript resource + wp_register_script( 'postman_test_email_wizard_script', plugins_url( 'Postman/Postman-Send-Test-Email/postman_send_test_email.js', $this->rootPluginFilenameAndPath ), array( + PostmanViewController::JQUERY_SCRIPT, + 'jquery_validation', + 'jquery_steps_script', + PostmanViewController::POSTMAN_SCRIPT, + ), $pluginData ['version'] ); + } + + /** + * Register the Email Test screen + */ + public function addEmailTestSubmenu() { + $page = add_submenu_page( null, sprintf( __( '%s Setup', 'post-smtp' ), __( 'Postman SMTP', 'post-smtp' ) ), __( 'Postman SMTP', 'post-smtp' ), Postman::MANAGE_POSTMAN_CAPABILITY_NAME, PostmanSendTestEmailController::EMAIL_TEST_SLUG, array( + $this, + 'outputTestEmailContent', + ) ); + // When the plugin options page is loaded, also load the stylesheet + add_action( 'admin_print_styles-' . $page, array( + $this, + 'enqueueEmailTestResources', + ) ); + } + + /** + */ + function enqueueEmailTestResources() { + wp_enqueue_style( 'jquery_steps_style' ); + wp_enqueue_style( PostmanViewController::POSTMAN_STYLE ); + wp_enqueue_style( 'postman_send_test_email' ); + wp_enqueue_script( 'postman_test_email_wizard_script' ); + wp_localize_script( PostmanViewController::POSTMAN_SCRIPT, 'postman_email_test', array( + 'recipient' => '#' . self::RECIPIENT_EMAIL_FIELD_NAME, + 'not_started' => _x( 'In Outbox', 'Email Test Status', 'post-smtp' ), + 'sending' => _x( 'Sending...', 'Email Test Status', 'post-smtp' ), + 'success' => _x( 'Success', 'Email Test Status', 'post-smtp' ), + 'failed' => _x( 'Failed', 'Email Test Status', 'post-smtp' ), + 'ajax_error' => __( 'Ajax Error', 'post-smtp' ), + ) ); + } + + /** + */ + public function outputTestEmailContent() { + print '
'; + + PostmanViewController::outputChildPageHeader( __( 'Send a Test Email', 'post-smtp' ) ); + + printf( '
', PostmanUtils::getSettingsPageUrl() ); + + wp_nonce_field('post-smtp', 'security' ); + + // Step 1 + printf( '
%s
', __( 'Specify the Recipient', 'post-smtp' ) ); + print '
'; + printf( '%s', __( 'Who is this message going to?', 'post-smtp' ) ); + printf( '

%s', __( 'This utility allows you to send an email message for testing.', 'post-smtp' ) ); + print ' '; + /* translators: where %d is an amount of time, in seconds */ + printf( '%s

', sprintf( _n( 'If there is a problem, Postman will give up after %d second.', 'If there is a problem, Postman will give up after %d seconds.', $this->options->getReadTimeout(), 'post-smtp' ), $this->options->getReadTimeout() ) ); + printf( '', _x( 'Recipient Email Address', 'Configuration Input Field', 'post-smtp' ) ); + print $this->test_email_callback(); + print '
'; + + // Step 2 + printf( '
%s
', __( 'Send The Message', 'post-smtp' ) ); + print '
'; + print ''; + print __( 'Sending the message:', 'post-smtp' ); + printf( ' %s', _x( 'In Outbox', 'Email Test Status', 'post-smtp' ) ); + print ''; + print '
'; + printf( '

', __( 'Status', 'post-smtp' ) ); + print ''; + print '
'; + print '
'; + + // Step 3 + printf( '
%s
', __( 'Session Transcript', 'post-smtp' ) ); + print '
'; + printf( '%s', __( 'Examine the Session Transcript if you need to.', 'post-smtp' ) ); + printf( '

%s

', __( 'This is the conversation between Postman and the mail server. It can be useful for diagnosing problems. DO NOT post it on-line, it may contain your account password.', 'post-smtp' ) ); + print '
'; + printf( '

', __( 'Session Transcript', 'post-smtp' ) ); + print ''; + print '
'; + print '
'; + + print '
'; + print '
'; + } +} + +/** + * + * @author jasonhendriks + */ +class PostmanSendTestEmailAjaxController extends PostmanAbstractAjaxHandler { + + /** + * Constructor + * + * @param PostmanOptions $options + * @param PostmanOAuthToken $authorizationToken + * @param PostmanConfigTextHelper $oauthScribe + */ + function __construct() { + parent::__construct(); + PostmanUtils::registerAjaxHandler( 'postman_send_test_email', $this, 'sendTestEmailViaAjax' ); + } + + /** + * Yes, this procedure is just for testing. + * + * @return boolean + */ + function test_mode() { + return true; + } + + /** + * This Ajax sends a test email + */ + function sendTestEmailViaAjax() { + + check_admin_referer('post-smtp', 'security'); + + // get the email address of the recipient from the HTTP Request + $email = $this->getRequestParameter( 'email' ); + + // get the name of the server from the HTTP Request + $serverName = PostmanUtils::postmanGetServerName(); + + /* translators: where %s is the domain name of the site */ + $subject = sprintf( _x( 'Postman SMTP Test (%s)', 'Test Email Subject', 'post-smtp' ), $serverName ); + + // Postman API: indicate to Postman this is just for testing + add_filter( 'postman_test_email', array( + $this, + 'test_mode', + ) ); + + // this header specifies that there are many parts (one text part, one html part) + $header = 'Content-Type: multipart/alternative;'; + + // createt the message content + $message = $this->createMessageContent(); + + // send the message + $success = wp_mail( $email, $subject, $message, $header ); + + // Postman API: remove the testing indicator + remove_filter( 'postman_test_email', array( + $this, + 'test_mode', + ) ); + + // Postman API: retrieve the result of sending this message from Postman + $result = apply_filters( 'postman_wp_mail_result', null ); + + // post-handling + if ( $success ) { + $this->logger->debug( 'Test Email delivered to server' ); + // the message was sent successfully, generate an appropriate message for the user + $statusMessage = sprintf( __( 'Your message was delivered (%d ms) to the SMTP server! Congratulations :)', 'post-smtp' ), $result ['time'] ); + + $this->logger->debug( 'statusmessage: ' . $statusMessage ); + + // compose the JSON response for the caller + $response = array( + 'message' => $statusMessage, + 'transcript' => $result ['transcript'], + ); + + // log the response + if ( $this->logger->isTrace() ) { + $this->logger->trace( 'Ajax Response:' ); + $this->logger->trace( $response ); + } + + // send the JSON response + wp_send_json_success( $response ); + } else { + $this->logger->error( 'Test Email NOT delivered to server - ' . $result ['exception']->getCode() ); + // the message was NOT sent successfully, generate an appropriate message for the user + $statusMessage = $result ['exception']->getMessage(); + + $this->logger->debug( 'statusmessage: ' . $statusMessage ); + + // compose the JSON response for the caller + $response = array( + 'message' => $statusMessage, + 'transcript' => $result ['transcript'], + ); + + // log the response + if ( $this->logger->isTrace() ) { + $this->logger->trace( 'Ajax Response:' ); + $this->logger->trace( $response ); + } + + // send the JSON response + wp_send_json_error( $response ); + } + } + + /** + * Create the multipart message content + * + * @return string + */ + private function createMessageContent() { + // Postman API: Get the plugin metadata + $pluginData = apply_filters( 'postman_get_plugin_metadata', null ); + + /* + translators: where %s is the Postman plugin version number (e.g. 1.4) */ + // English - Mandarin - French - Hindi - Spanish - Portuguese - Russian - Japanese + // http://www.pinyin.info/tools/converter/chars2uninumbers.html + $greeting = 'Hello! - 你好 - Bonjour! - नमस्ते - ¡Hola! - Olá - Привет! - 今日は'; + $sentBy = sprintf( _x( 'Sent by Postman %s', 'Test Email Tagline', 'post-smtp' ), $pluginData ['version'] ); + $imageSource = __( 'Image source', 'post-smtp' ); + $withPermission = __( 'Used with permission', 'post-smtp' ); + $messageArray = array( + 'Content-Type: text/plain; charset = "UTF-8"', + 'Content-Transfer-Encoding: 8bit', + '', + 'Hello!', + '', + sprintf( '%s - https://wordpress.org/plugins/post-smtp/', $sentBy ), + '', + 'Content-Type: text/html; charset=UTF-8', + 'Content-Transfer-Encoding: quoted-printable', + '', + '', + '', + '', + '', + '', + '', + '', + '
', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + '
', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + '
', + '
', + sprintf( '
%s
', $greeting ), + sprintf( '
%s', $sentBy ), + '
https://wordpress.org/plugins/post-smtp/', + '
', + '
', + '
', + sprintf( '
%s: poofytoo.com - %s', $imageSource, $withPermission ), + '
', + '', + '', + ); + return implode( PostmanMessage::EOL, $messageArray ); + } +} -- cgit v1.2.3