Skip to content

Commit

Permalink
Admin: Add "Connect" button if using IMAP mode and if auto-connect is…
Browse files Browse the repository at this point in the history
… enabled.

If IMAP mode is selected and if "Automatically reconnect?" is checked and
if we are not connected to the inbox, a "Connect" button will appear in
the "Connection Info" block.

Clicking on this button will connect to the IMAP inbox.

See #40.
  • Loading branch information
r-a-y committed Mar 8, 2017
1 parent 0e0bebb commit 2b54149
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
83 changes: 83 additions & 0 deletions includes/bp-rbe-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class BP_Reply_By_Email_Admin {
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'setup_admin' ) );

add_action( 'wp_ajax_bp_rbe_admin_connect', array( $this, 'ajax_connect' ) );
add_action( 'wp_ajax_bp_rbe_admin_connect_notice', array( $this, 'ajax_connect_notice' ) );
}

/**
Expand Down Expand Up @@ -140,6 +143,7 @@ public function head() {
p.connected span, p.not-connected span {font-weight:bold;}
p.connected span {color:green;}
p.not-connected span {color:red;}
div.imap-options .spinner {float:left; margin-top:0; margin-left:0; display:none;}
</script>
<?php
}
Expand Down Expand Up @@ -187,11 +191,79 @@ public function footer() {
$( 'tr.bp-rbe-email' ).toggle();
$( '.bp-rbe-username span' ).toggle();
});

$('button.connect').on('click', function(e) {
var btn = $(this);
btn.prop( 'disabled', true );
$('.imap-options .spinner').show();
$('.error-rbe').remove();

$.post( ajaxurl, {
action: 'bp_rbe_admin_connect',
'_wpnonce': $('#bp-rbe-ajax-connect-nonce').val()
},
function(response) {

});

// Run another AJAX to check if connected.
setTimeout( function() {
$.post( ajaxurl, {
action: 'bp_rbe_admin_connect_notice',
'_wpnonce': $('#bp-rbe-ajax-connect-nonce').val()
},
function(response) {
$('.imap-options .spinner').hide();

if ( response.success ) {
$('p.not-connected').removeClass('not-connected').addClass('connected').html( response.data.msg );
$('button.connect').hide();
} else {
btn.prop( 'disabled', false );

$( 'div.wrap form:first' ).before( '<div class="error error-rbe">' + response.data.msg + '</div>' );
}
});

}, 5000 );

});
});
</script>
<?php
}

/**
* AJAX callback to connect to the IMAP inbox.
*
* @since 1.0-RC5.
*/
public function ajax_connect() {
check_ajax_referer( 'bp_rbe_ajax_connect' );

// Run IMAP inbox check.
bp_rbe_run_inbox_listener( array( 'force' => true ) );
}

/**
* AJAX callback to check if we're connected to the IMAP inbox.
*
* @since 1.0-RC5.
*/
public function ajax_connect_notice() {
check_ajax_referer( 'bp_rbe_ajax_connect' );

if ( bp_rbe_is_connected() ) {
wp_send_json_success( array(
'msg' => __( '<strong>Reply By Email</strong> is currently <span>CONNECTED</span> and checking your inbox continuously.', 'bp-rbe' )
) );
} else {
wp_send_json_success( array(
'msg' => sprintf( __( 'Error: Unable to connect to inbox - %s', 'bp-rbe' ), join( '. ', (array) imap_errors() ) )
) );
}
}

/**
* Validate and sanitize our options before saving to the DB.
*
Expand Down Expand Up @@ -607,14 +679,25 @@ protected function schedule() {
if ( bp_rbe_is_required_completed() && ! bp_rbe_is_inbound() ) :
$is_connected = bp_rbe_is_connected();

$is_autoconnect = 1 === (int) bp_rbe_get_setting( 'keepaliveauto' );
?>
<h3><?php _e( 'Connection Info', 'bp-rbe' ); ?></h3>

<div class="spinner is-active"></div>

<p class="<?php echo $is_connected ? 'connected' : 'not-connected'; ?>">
<?php if ( $is_connected ) : ?>
<?php _e( '<strong>Reply By Email</strong> is currently <span>CONNECTED</span> and checking your inbox continuously.', 'bp-rbe' ); ?>
<?php elseif ( $is_autoconnect ) : ?>
<?php _e( '<strong>Reply By Email</strong> is currently <span>NOT CONNECTED</span>. Please click on the "Connect" button to initiate a connection.', 'bp-rbe' ); ?>

<?php wp_nonce_field( 'bp_rbe_ajax_connect', 'bp-rbe-ajax-connect-nonce' ); ?>

<p class="submit"><button type="button" class="button-primary connect"><?php esc_html_e( 'Connect', 'bp-rbe' ); ?></button></p>

<?php else : ?>
<?php _e( '<strong>Reply By Email</strong> is currently <span>NOT CONNECTED</span>. Please refresh the page to initiate a connection.', 'bp-rbe' ); ?>

<?php endif; ?>
</p>

Expand Down
3 changes: 3 additions & 0 deletions includes/classes/bp-reply-by-email-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ public function run() {
// sleep a bit before autoconnecting again
} else {
sleep( 5 );

// Do this whole thing again!
bp_rbe_run_inbox_listener( array( 'force' => true ) );
}

exit();
Expand Down

0 comments on commit 2b54149

Please sign in to comment.