Skip to content

Commit

Permalink
Closes #28: Add a "Send me a test email" button.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabacab committed Dec 14, 2017
1 parent d7c1501 commit 53ecb3c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 23 deletions.
10 changes: 9 additions & 1 deletion admin/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class="large-text code"
<?php print sprintf(
esc_html__( 'Paste your PGP public key here to have %1$s encrypt emails it sends you. Leave this blank if you do not want to get or know how to decrypt encrypted emails.', 'wp-pgp-encrypted-emails' ),
get_bloginfo( 'name' )
) ;?>
); ?>
</p>
</td>
</tr>
Expand Down Expand Up @@ -114,5 +114,13 @@ class="large-text code"
</td>
</tr>
<?php } // endif ?>
<tr id="wp-pgp-encrypted-emails-send-test-email">
<th>
<?php esc_html_e( 'Testing emails', 'wp-pgp-encrypted-emails' ); ?>
</th>
<td>
<?php load_template( dirname( __FILE__ ) . '/../templates/send-test-email.php' ); ?>
</td>
</tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions includes/woocommerce-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public static function renderEditAccountForm () {
<p class="description"><?php print sprintf(
esc_html__('Email encryption cannot encrypt envelope information (such as the subject) of an email, so if you want maximum privacy, make sure this option is enabled to always erase the subject line from encrypted emails you receive.', 'wp-pgp-encrypted-emails')
);?></p>

<fieldset id="wp-pgp-encrypted-emails-send-test-email">
<legend><?php esc_html_e( 'Testing emails', 'wp-pgp-encrypted-emails' ); ?></legend>
<?php load_template( dirname( __FILE__ ) . '/../templates/send-test-email.php' ); ?>
</fieldset>
</fieldset>
<?php
}
Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&amp;business=T
Tags: encryption, email, security, privacy, pgp, gpg, openpgp, smime
Requires at least: 4.4
Tested up to: 4.9
Stable tag: 0.7
Stable tag: 0.7.1
License: GPL-3.0
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -153,6 +153,9 @@ If you want to use a stronger signing keypair, you can generate one yourself (of

== Change log ==

= 0.7.1 =
* [Enhancement](https://github.com/meitar/wp-pgp-encrypted-emails/issues/28): Offer a simple "Send me a test email" button to let inexperienced users easily test their encryption setup.

= 0.7 =
* Feature: WooCommerce integration. Customers can add their own OpenPGP keys or S/MIME certificates on their "My Account" front-end supplied by WooCommerce. By default, emails sent to Customers are *not* signed with the site's PGP signing key, though individual customers can opt-in to receive either encrypted and signed or just signed emails as they wish.
* Developer: Theme authors can override the plugin's default WooCommerce integration file by adding a `woocommerce-functions.php` file to their theme. Please only do this if you know what you are doing.
Expand Down Expand Up @@ -234,6 +237,9 @@ If you want to use a stronger signing keypair, you can generate one yourself (of

== Upgrade Notice ==

= 0.7.1 =
This release adds a "Send me a test email" button to your profile page. It also includes minor code cleanup.

= 0.7 =
This release adds WooCommerce integration to help protect your business and your customer's information.

Expand Down
7 changes: 7 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Styles for WP PGP Encrypted Emails.
*/

#wp-pgp-encrypted-emails-send-test-email:target {
opacity: 0.6;
}
26 changes: 26 additions & 0 deletions templates/send-test-email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Template for the "send a test email" button.
*/

if ( ! defined( 'ABSPATH' ) ) { return; } // Disallow direct HTTP access.
?>
<p>
<a class="button"
href="<?php
$return_url = ( is_ssl() ) ? 'https://' : 'http://';
$return_url .= "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
print esc_attr(
wp_nonce_url(
admin_url( 'admin-ajax.php?action=wp_pgp_encrypted_emails_send_test_email&return_url=' . rawurlencode( $return_url ) ),
'wp_pgp_encrypted_emails_send_test_email',
'wp_pgp_encrypted_emails_send_test_email'
)
);
?>"
><?php esc_html_e( 'Send me a test email', 'wp-pgp-encrypted-emails' ); ?></a>
</p>
<p class="description"><?php print sprintf(
esc_html__( 'After you save the desired settings, return here and press the "Send me a test email" button to have %1$s send a test email to you. Make sure you can read and verify this email in your inbox to confirm that everything is working correctly.', 'wp-pgp-encrypted-emails' ),
get_bloginfo( 'name' )
);?></p>
87 changes: 66 additions & 21 deletions wp-pgp-encrypted-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* * Plugin Name: WP PGP Encrypted Emails
* * Plugin URI: https://github.com/meitar/wp-pgp-encrypted-emails
* * Description: Encrypts email sent to users who opt-in to OpenPGP- and/or S/MIME-compatible protection. <strong>Like this plugin? Please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&amp;business=TJLPJYXHSRBEE&amp;lc=US&amp;item_name=WP%20PGP%20Encrypted%20Emails&amp;item_number=wp-pgp-encrypted-emails&amp;currency_code=USD&amp;bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted" title="Send a donation to the developer of WP PGP Encrypted Emails">donate</a>. &hearts; Thank you!</strong>
* * Version: 0.7
* * Version: 0.7.1
* * Author: Maymay <[email protected]>
* * Author URI: https://maymay.net/
* * License: GPL-3.0
Expand Down Expand Up @@ -133,10 +133,13 @@ class WP_PGP_Encrypted_Emails {
public static function register () {
add_action( 'plugins_loaded', array( __CLASS__, 'registerL10n' ) );
add_action( 'init', array( __CLASS__, 'initialize' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueueStyles' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueueStyles') );

add_action( 'wp_ajax_nopriv_download_pgp_signing_public_key', array( __CLASS__, 'downloadSigningPublicKey' ) );
add_action( 'wp_ajax_download_pgp_signing_public_key', array( __CLASS__, 'downloadSigningPublicKey' ) );
add_action( 'wp_ajax_openpgp_regen_keypair', array( __CLASS__, 'regenerateKeypair' ) );
add_action( 'wp_ajax_wp_pgp_encrypted_emails_send_test_email', array( __CLASS__, 'sendTestEmail' ) );

if ( is_admin() ) {
add_action( 'admin_menu', array( __CLASS__, 'registerOptionsPage') );
Expand Down Expand Up @@ -218,6 +221,19 @@ public static function initialize () {
}
}

/**
* Enqueues the plugin's stylesheet.
*
* @see https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/
* @see https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
*/
public static function enqueueStyles () {
wp_enqueue_style(
'wp-pgp-encrypted-emails',
plugin_dir_url( __FILE__ ) . '/style.css'
);
}

/**
* Method to run when the plugin is activated by a user in the
* WordPress Dashboard admin screen.
Expand Down Expand Up @@ -1094,43 +1110,72 @@ public static function downloadSigningPublicKey () {
* @return void
*/
public static function regenerateKeypair () {
if (empty($_GET['wp_pgp_nonce']) || !wp_verify_nonce($_GET['wp_pgp_nonce'], 'wp_pgp_regen_keypair')) {
add_settings_error('general', 'settings_updated', __('Invalid keygen request.', 'wp-pgp-encrypted-emails'));
set_transient('settings_errors', get_settings_errors(), 30);
wp_safe_redirect(admin_url('options-general.php?page=wp-pgp-encrypted-emails&settings-updated=true'));
exit(1); // error exit code
if ( empty( $_GET['wp_pgp_nonce'] ) || ! wp_verify_nonce( $_GET['wp_pgp_nonce'], 'wp_pgp_regen_keypair' ) ) {
add_settings_error( 'general', 'settings_updated', __( 'Invalid keygen request.', 'wp-pgp-encrypted-emails' ) );
set_transient( 'settings_errors', get_settings_errors(), 30 );
wp_safe_redirect( admin_url( 'options-general.php?page=wp-pgp-encrypted-emails&settings-updated=true' ) );
exit( 1 ); // error exit code
}
// Make up an email address to use as the site's key identity.
// This is also what WordPress core's wp_mail() function does.
// See: https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-includes/pluggable.php#L371
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if (substr($sitename, 0, 4) == 'www.') {
$sitename = substr($sitename, 4);
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
$from_email = 'wordpress@'.$sitename;

// Key generation could take some time, so try raising the limit.
$old_time_limit = ini_get('max_execution_time');
set_time_limit(0);
$old_time_limit = ini_get( 'max_execution_time' );
set_time_limit( 0 );

// If that doesn't work, make sure we can gracefully fail.
add_action('shutdown', array(__CLASS__, 'keygenTimeoutError'));
add_action( 'shutdown', array( __CLASS__, 'keygenTimeoutError' ) );

// Now try generating a new keypair.
$keypair = WP_OpenPGP::generateKeypair("WordPress <$from_email>");
$keypair = WP_OpenPGP::generateKeypair( "WordPress <$from_email>" );

// If we're still running, restore the old settings.
set_time_limit($old_time_limit);
set_time_limit( $old_time_limit );

$ascii_keypair = array();
$ascii_keypair['privatekey'] = apply_filters('openpgp_enarmor', $keypair['privatekey'], 'PGP PRIVATE KEY BLOCK');
$ascii_keypair['publickey'] = apply_filters('openpgp_enarmor', $keypair['publickey'], 'PGP PUBLIC KEY BLOCK');
update_option(self::meta_keypair, $ascii_keypair);

add_settings_error('general', 'settings_updated', __('OpenPGP signing keypair successfully regenerated.', 'wp-pgp-encrypted-emails'), 'updated');
set_transient('settings_errors', get_settings_errors(), 30);
wp_safe_redirect(admin_url('options-general.php?page=wp-pgp-encrypted-emails&settings-updated=true'));
exit(0); // success exit code
$ascii_keypair['privatekey'] = apply_filters( 'openpgp_enarmor', $keypair['privatekey'], 'PGP PRIVATE KEY BLOCK' );
$ascii_keypair['publickey'] = apply_filters( 'openpgp_enarmor', $keypair['publickey'], 'PGP PUBLIC KEY BLOCK' );
update_option( self::meta_keypair, $ascii_keypair );

add_settings_error( 'general', 'settings_updated', __( 'OpenPGP signing keypair successfully regenerated.', 'wp-pgp-encrypted-emails' ), 'updated' );
set_transient( 'settings_errors', get_settings_errors(), 30 );
wp_safe_redirect( admin_url( 'options-general.php?page=wp-pgp-encrypted-emails&settings-updated=true' ) );
exit( 0 );
}

/**
* Sends the current user a test email so they can check their encryption settings.
*/
public static function sendTestEmail () {
if ( ! isset( $_GET['wp_pgp_encrypted_emails_send_test_email'] ) ) {
return;
} else if ( ! wp_verify_nonce( $_GET['wp_pgp_encrypted_emails_send_test_email'], 'wp_pgp_encrypted_emails_send_test_email' ) ) {
return;
}

$current_user = wp_get_current_user();
$email_body = sprintf(
esc_html__( 'This is a test message from %1$s.', 'wp-pgp-encrypted-emails' ),
get_bloginfo( 'name' )
);
wp_mail(
$current_user->user_email,
sanitize_text_field( sprintf(
__( 'Test email from %s', 'wp-pgp-encrypted-emails' ),
get_bloginfo( 'name' )
) ),
$email_body
);

$return_url = ( ! empty( $_GET['return_url'] ) ) ? $_GET['return_url'] : admin_url( 'profile.php' ) ;
wp_safe_redirect( "$return_url#wp-pgp-encrypted-emails-send-test-email" );
exit( 0 );
}

/**
Expand Down

0 comments on commit 53ecb3c

Please sign in to comment.