Skip to content

Commit

Permalink
IMAP: If auto-connect is enabled, send an email to the site admin if …
Browse files Browse the repository at this point in the history
…connection is down.

When auto-connect is enabled, there are occasions where the IMAP connection
can be disconnected such as if GMail is acting up or if the server that
WordPress is hosted on is down.  When this happens, the IMAP connection
needs to be reinitiated from the RBE admin settings page since IMAP
connections can only be initiated from the admin area when the auto-connect
option is on.

To counteract this, this commit adds an hourly check to see if the IMAP
connection is active.  If not connected, an email is sent to the site admin
by default to prompt the person to re-enable the IMAP connection on the RBE
admin settings page.  A filter exists to change the email recipients.

See #40.
  • Loading branch information
r-a-y committed Mar 20, 2017
1 parent e6828ce commit 0e68726
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
13 changes: 13 additions & 0 deletions includes/bp-rbe-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ public function ajax_connect_notice() {
$success_msg = __( '<strong>Reply By Email</strong> is currently <span>CONNECTED</span> and checking your inbox continuously. To disconnect, deactivate the plugin.', 'bp-rbe' );

if ( bp_rbe_is_connected() ) {
// Schedule hourly check.
if ( ! wp_next_scheduled ( 'bp_rbe_schedule' ) ) {
wp_schedule_event( time() + 60 * 60, 'hourly', 'bp_rbe_schedule' );
}

wp_send_json_success( array(
'msg' => $success_msg
) );
Expand All @@ -284,6 +289,11 @@ public function ajax_connect_notice() {

// Success!
if ( bp_rbe_is_connected( array( 'clearcache' => true ) ) ) {
// Schedule hourly check.
if ( ! wp_next_scheduled ( 'bp_rbe_schedule' ) ) {
wp_schedule_event( time() + 60 * 60, 'hourly', 'bp_rbe_schedule' );
}

wp_send_json_success( array(
'msg' => $success_msg
) );
Expand Down Expand Up @@ -325,6 +335,7 @@ public function validate( $input ) {
// stop RBE if still connected via IMAP
if ( bp_rbe_is_connected() ) {
bp_rbe_stop_imap();
wp_clear_scheduled_hook( 'bp_rbe_schedule' );
}
bp_rbe_log( '- Operating mode switched to inbound -' );
}
Expand Down Expand Up @@ -409,6 +420,8 @@ public function validate( $input ) {
$output['keepaliveauto'] = 1;
} else {
$output['keepaliveauto'] = 0;

wp_clear_scheduled_hook( 'bp_rbe_schedule' );
}

// do a quick imap check if we have valid credentials to check
Expand Down
49 changes: 47 additions & 2 deletions includes/bp-rbe-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ function bp_rbe_cleanup() {
delete_site_transient( 'bp_rbe_is_connected' );
delete_site_transient( 'bp_rbe_lock' );

// we don't use WP's cron feature anymore, but we clear RBE's old scheduled
// hook just in case
// Clear RBE's cron.
wp_clear_scheduled_hook( 'bp_rbe_schedule' );
}

Expand Down Expand Up @@ -1540,6 +1539,52 @@ function bp_rbe_remove_imap_connection_marker() {
}
endif;

/**
* Run hourly WP cron check to see if IMAP connection is connected
*
* If not connected, an email will be sent to the site admin by default so
* that person can login and re-initiate the IMAP connection. This only runs
* in IMAP mode and if the auto-connect option is enabled.
*
* @since 1.0-RC5
*/
function bp_rbe_imap_down_email_notice() {
// If inbound mode or is connecting or IMAP auto-connect is off, bail.
if ( bp_rbe_is_inbound() || bp_rbe_is_connecting( array( 'clearcache' => true ) ) || ( 1 !== (int) bp_rbe_get_setting( 'keepaliveauto' ) ) ) {
return;
}

// If IMAP connection is down, send email so someone can reconnect.
if ( ! bp_rbe_is_connected() ) {
$recipients = array();
$recipients[] = get_option( 'admin_email' );

/**
* Filter of email addresses to send "IMAP connection is down" email to.
*
* @since 1.0-RC5
*
* @param array
*/
$recipients = apply_filters( 'bp_rbe_imap_down_recipients', $recipients );

if ( ! empty( $recipients ) ) {
$message = sprintf( __( 'Hi,
The IMAP connection to the email inbox - %1$s - has been disconnected.
Please manually go to:
%2$s
And click on the "Connect" button to re-establish a connection.
Otherwise, new replies by email will not be posted to the site.', 'bp-rbe' ), bp_rbe_get_setting( 'servername' ), admin_url( 'admin.php?page=bp-rbe' ) );

wp_mail( $recipients, __( 'BP Reply By Email - IMAP connection is down', 'bp-rbe' ), $message );
}
}
}

/** Modified BP functions ***********************************************/

/**
Expand Down
4 changes: 4 additions & 0 deletions includes/bp-rbe-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
add_action( 'init', 'bp_rbe_run_inbox_listener', 999 );
}

if ( bp_is_root_blog() && bp_rbe_get_setting( 'keepaliveauto' ) ) {
add_action( 'bp_rbe_schedule', 'bp_rbe_imap_down_email_notice' );
}

// email inbox parsing
/**
* In Gmail, imap_delete() moves the email to the "All Mail" folder; it doesn't mark the email for deletion.
Expand Down

0 comments on commit 0e68726

Please sign in to comment.