From b5fd728e37aa2a9c7a5f37e8dead0a95117d541b Mon Sep 17 00:00:00 2001 From: yehudah Date: Mon, 25 Nov 2019 09:25:43 +0000 Subject: =?UTF-8?q?phpmailer=20delivery=20improvments=20bug=20fixes=20add?= =?UTF-8?q?=20option=20to=20disable=20notifications=20fix=20Invalid=20?= =?UTF-8?q?=E2=80=9CReply-To=E2=80=9D=20e-mail=20address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Postman/Extensions/Core/Notifications/INotify.php | 7 + .../Core/Notifications/PostmanMailNotify.php | 14 ++ .../Core/Notifications/PostmanNotify.php | 241 +++++++++++++++++++++ .../Core/Notifications/PostmanNotifyOptions.php | 57 +++++ .../Core/Notifications/PostmanPushoverNotify.php | 34 +++ .../Core/Notifications/PostmanSlackNotify.php | 39 ++++ 6 files changed, 392 insertions(+) create mode 100644 Postman/Extensions/Core/Notifications/INotify.php create mode 100644 Postman/Extensions/Core/Notifications/PostmanMailNotify.php create mode 100644 Postman/Extensions/Core/Notifications/PostmanNotify.php create mode 100644 Postman/Extensions/Core/Notifications/PostmanNotifyOptions.php create mode 100644 Postman/Extensions/Core/Notifications/PostmanPushoverNotify.php create mode 100644 Postman/Extensions/Core/Notifications/PostmanSlackNotify.php (limited to 'Postman/Extensions/Core') diff --git a/Postman/Extensions/Core/Notifications/INotify.php b/Postman/Extensions/Core/Notifications/INotify.php new file mode 100644 index 0000000..f40548d --- /dev/null +++ b/Postman/Extensions/Core/Notifications/INotify.php @@ -0,0 +1,7 @@ +options = new PostmanNotifyOptions(); + + add_filter( 'post_smtp_admin_tabs', array( $this, 'tabs' ) ); + add_action( 'post_smtp_settings_menu', array( $this, 'menu' ) ); + add_action( 'post_smtp_settings_fields', array( $this, 'settings' ) ); + add_action( 'post_smtp_on_failed', array( $this, 'notify' ), 10, 5 ); + add_filter( 'post_smtp_sanitize', array( $this, 'sanitize' ), 10, 3 ); + } + + public function menu() { + print '
'; + do_settings_sections( self::NOTIFICATIONS_OPTIONS ); + + $currentKey = $this->options->getNotificationService(); + $pushover = $currentKey == 'pushover' ? 'block' : 'none'; + $slack = $currentKey == 'slack' ? 'block' : 'none'; + + echo '
'; + do_settings_sections( self::NOTIFICATIONS_PUSHOVER_CRED ); + echo '
'; + + echo '
'; + do_settings_sections( self::NOTIFICATIONS_SLACK_CRED ); + echo '
'; + + do_action( 'post_smtp_notification_settings' ); + + print '
'; + } + + public function sanitize($new_input, $input, $sanitizer) { + // Notifications + $sanitizer->sanitizeString( 'Pushover Service', PostmanNotifyOptions::NOTIFICATION_SERVICE, $input, $new_input, $this->options->getNotificationService() ); + $sanitizer->sanitizePassword( 'Pushover Username', PostmanNotifyOptions::PUSHOVER_USER, $input, $new_input, $this->options->getPushoverUser() ); + $sanitizer->sanitizePassword( 'Pushover Token', PostmanNotifyOptions::PUSHOVER_TOKEN, $input, $new_input, $this->options->getPushoverToken() ); + $sanitizer->sanitizePassword( 'Slack Token', PostmanNotifyOptions::SLACK_TOKEN, $input, $new_input, $this->options->getSlackToken() ); + + // Chrome extension + $sanitizer->sanitizeString( 'Push Chrome Extension', PostmanNotifyOptions::NOTIFICATION_USE_CHROME, $input, $new_input ); + $sanitizer->sanitizePassword( 'Push Chrome Extension UID', PostmanNotifyOptions::NOTIFICATION_CHROME_UID, $input, $new_input, $this->options->getNotificationChromeUid() ); + + return $new_input; + } + + public function tabs($tabs) { + $tabs['notifications'] = __( 'Notifications', 'post-smtp' ); + + return $tabs; + } + + public function settings() { + // Notifications + add_settings_section( self::NOTIFICATIONS_SECTION, _x( 'Notifications Settings', 'Configuration Section Title', 'post-smtp' ), array( + $this, + 'printNotificationsSectionInfo', + ), self::NOTIFICATIONS_OPTIONS ); + + add_settings_field( PostmanNotifyOptions::NOTIFICATION_SERVICE, _x( 'Notification Service', 'Configuration Input Field', 'post-smtp' ), array( + $this, + 'notification_service_callback', + ), self::NOTIFICATIONS_OPTIONS, self::NOTIFICATIONS_SECTION ); + + // Pushover + add_settings_section( 'pushover_credentials', _x( 'Pushover Credentials', 'Configuration Section Title', 'post-smtp' ), array( + $this, + 'printNotificationsSectionInfo', + ), self::NOTIFICATIONS_PUSHOVER_CRED ); + + add_settings_field( PostmanNotifyOptions::PUSHOVER_USER, _x( 'Pushover User Key', 'Configuration Input Field', 'post-smtp' ), array( + $this, + 'pushover_user_callback', + ), self::NOTIFICATIONS_PUSHOVER_CRED, 'pushover_credentials' ); + + add_settings_field( PostmanNotifyOptions::PUSHOVER_TOKEN, _x( 'Pushover App Token', 'Configuration Input Field', 'post-smtp' ), array( + $this, + 'pushover_token_callback', + ), self::NOTIFICATIONS_PUSHOVER_CRED, 'pushover_credentials' ); + + // Slack + add_settings_section( 'slack_credentials', _x( 'Slack Credentials', 'Configuration Section Title', 'post-smtp' ), array( + $this, + 'printNotificationsSectionInfo', + ), self::NOTIFICATIONS_SLACK_CRED ); + + add_settings_field( PostmanNotifyOptions::SLACK_TOKEN, _x( 'Slack Webhook', 'Configuration Input Field', 'post-smtp' ), array( + $this, + 'slack_token_callback', + ), self::NOTIFICATIONS_SLACK_CRED, 'slack_credentials' ); + + add_settings_field( PostmanNotifyOptions::NOTIFICATION_USE_CHROME, _x( 'Push to chrome extension', 'Configuration Input Field', 'post-smtp' ), array( + $this, + 'notification_use_chrome_callback', + ), self::NOTIFICATIONS_OPTIONS, self::NOTIFICATIONS_SECTION ); + + add_settings_field( 'notification_chrome_uid', _x( 'Chrome Extension UID', 'Configuration Input Field', 'post-smtp' ), array( + $this, + 'notification_chrome_uid_callback', + ), self::NOTIFICATIONS_OPTIONS, self::NOTIFICATIONS_SECTION ); + } + + /** + * Print the Section text + */ + public function printNotificationsSectionInfo() { + } + + public function notification_service_callback() { + $inputDescription = __( 'Select the notification service you want to recieve alerts about failed emails.' ); + + $options = apply_filters('post_smtp_notification_service', array( + 'none' => __( 'None', 'post-smtp' ), + 'default' => __( 'WP Admin Email', 'post-smtp' ), + 'pushover' => __( 'Pushover', 'post-smtp' ), + 'slack' => __( 'Slack', 'post-smtp' ), + )); + + printf( '
%s', $inputDescription ); + } + + public function notification_use_chrome_callback() { + $value = $this->options->useChromeExtension(); + printf( '', 'postman_options', PostmanNotifyOptions::NOTIFICATION_USE_CHROME, $value ? 'checked="checked"' : '' ); + } + + public function notification_chrome_uid_callback() { + printf( '', 'postman_options', 'notification_chrome_uid', PostmanUtils::obfuscatePassword( $this->options->getNotificationChromeUid() ) ); + } + + public function pushover_user_callback() { + printf( '', 'postman_options', PostmanNotifyOptions::PUSHOVER_USER, $this->options->getPushoverUser() ); + } + + public function pushover_token_callback() { + printf( '', 'postman_options', PostmanNotifyOptions::PUSHOVER_TOKEN, $this->options->getPushoverToken() ); + } + + public function slack_token_callback() { + printf( '', 'postman_options', PostmanNotifyOptions::SLACK_TOKEN, $this->options->getSlackToken() ); + echo '' . __( 'Get your webhook URL here', 'post-smtp' ) . ''; + + } + + /** + * @param PostmanEmailLog $log + * @param PostmanMessage $message + * @param string $transcript + * @param PostmanTransport $transport + * @param string $errorMessage + */ + public function notify ($log, $postmanMessage, $transcript, $transport, $errorMessage ) { + $message = __( 'You getting this message because an error detected while delivered your email.', 'post-smtp' ); + $message .= "\r\n" . sprintf( __( 'For the domain: %1$s','post-smtp' ), get_bloginfo('url') ); + $message .= "\r\n" . __( 'The log to paste when you open a support issue:', 'post-smtp' ) . "\r\n"; + + if ( $errorMessage && ! empty( $errorMessage ) ) { + + $message = $message . $errorMessage; + + $notification_service = PostmanNotifyOptions::getInstance()->getNotificationService(); + switch ($notification_service) { + case 'none': + $notifyer = false; + break; + case 'default': + $notifyer = new PostmanMailNotify; + break; + case 'pushover': + $notifyer = new PostmanPushoverNotify; + break; + case 'slack': + $notifyer = new PostmanSlackNotify; + break; + default: + $notifyer = new PostmanMailNotify; + } + + $notifyer = apply_filters('post_smtp_notifier', $notifyer, $notification_service); + + // Notifications + if ( $notifyer ) { + $notifyer->send_message($message, $log); + } + + $this->push_to_chrome($errorMessage); + } + } + + public function push_to_chrome($message) { + $push_chrome = PostmanNotifyOptions::getInstance()->useChromeExtension(); + + if ( $push_chrome ) { + $uid = PostmanNotifyOptions::getInstance()->getNotificationChromeUid(); + + if ( empty( $uid ) ) { + return; + } + + $url = 'https://postmansmtp.com/chrome/' . $uid; + + $args = array( + 'body' => array( + 'message' => $message + ) + ); + + $response = wp_remote_post( $url , $args ); + } + } + + private function printSelectOption( $label, $optionKey, $currentKey ) { + $optionPattern = ''; + printf( $optionPattern, $optionKey, $optionKey == $currentKey ? 'selected="selected"' : '', $label ); + } +} +new PostmanNotify(); \ No newline at end of file diff --git a/Postman/Extensions/Core/Notifications/PostmanNotifyOptions.php b/Postman/Extensions/Core/Notifications/PostmanNotifyOptions.php new file mode 100644 index 0000000..08c27db --- /dev/null +++ b/Postman/Extensions/Core/Notifications/PostmanNotifyOptions.php @@ -0,0 +1,57 @@ +options = get_option( 'postman_options' ); + } + + public function getNotificationService() { + if ( isset( $this->options [ self::NOTIFICATION_SERVICE ] ) ) { + return $this->options [ self::NOTIFICATION_SERVICE ]; + } else { + return self::DEFAULT_NOTIFICATION_SERVICE; + } + } + + public function getPushoverUser() { + if ( isset( $this->options [ self::PUSHOVER_USER ] ) ) { + return base64_decode( $this->options [ self::PUSHOVER_USER ] ); + } + } + + public function getPushoverToken() { + if ( isset( $this->options [ self::PUSHOVER_TOKEN ] ) ) { + return base64_decode( $this->options [ self::PUSHOVER_TOKEN ] ); + } + } + + public function getSlackToken() { + if ( isset( $this->options [ self::SLACK_TOKEN ] ) ) { + return base64_decode( $this->options [ self::SLACK_TOKEN ] ); + } + } + + public function useChromeExtension() { + if ( isset( $this->options [ self::NOTIFICATION_USE_CHROME ] ) ) { + return $this->options [ self::NOTIFICATION_USE_CHROME ]; + } + } + + public function getNotificationChromeUid() { + if ( isset( $this->options [ self::NOTIFICATION_CHROME_UID ] ) ) { + return base64_decode( $this->options [ self::NOTIFICATION_CHROME_UID ] ); + } + } +} \ No newline at end of file diff --git a/Postman/Extensions/Core/Notifications/PostmanPushoverNotify.php b/Postman/Extensions/Core/Notifications/PostmanPushoverNotify.php new file mode 100644 index 0000000..14ef7d2 --- /dev/null +++ b/Postman/Extensions/Core/Notifications/PostmanPushoverNotify.php @@ -0,0 +1,34 @@ +getPushoverToken(); + $user_key = $options->getPushoverUser(); + + $args = array( + 'body' => array( + "token" => $app_token, + "user" => $user_key, + "message" => $message, + ) + ); + + $result = wp_remote_post( $api_url, $args ); + + if ( is_wp_error($result) ) { + error_log( __CLASS__ . ': ' . $result->get_error_message() ); + } + + $body = json_decode( wp_remote_retrieve_body( $result ), true ); + if ( $body['status'] == 0 ) { + error_log( __CLASS__ . ': ' . print_r( $body, true ) ); + } + } +} \ No newline at end of file diff --git a/Postman/Extensions/Core/Notifications/PostmanSlackNotify.php b/Postman/Extensions/Core/Notifications/PostmanSlackNotify.php new file mode 100644 index 0000000..5b6fae3 --- /dev/null +++ b/Postman/Extensions/Core/Notifications/PostmanSlackNotify.php @@ -0,0 +1,39 @@ +getSlackToken(); + + $headers = array( + 'content-type' => 'application/json' + ); + + $body = array( + 'text' => $message + ); + + $args = array( + 'headers' => $headers, + 'body' => json_encode($body) + ); + + $result = wp_remote_post( $api_url, $args ); + + if ( is_wp_error($result) ) { + error_log( __CLASS__ . ': ' . $result->get_error_message() ); + } + + $code = wp_remote_retrieve_response_code( $result ); + $message = wp_remote_retrieve_response_message( $result ); + + if ( $code != 200 && $message !== 'OK' ) { + error_log( __CLASS__ . ': ' . $message ); + } + } +} \ No newline at end of file -- cgit v1.2.3