Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions assets/js/ctct-plugin-admin/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ window.CTCTAJAX = {};

// Trigger any field modifications we need to do.
that.handleReviewAJAX();
that.handleAPITest();
};

// Handle saving the decision regarding the review prompt admin notice.
Expand Down Expand Up @@ -50,5 +51,40 @@ window.CTCTAJAX = {};
}
};

that.handleAPITest = () => {
const apitestlink = document.querySelector('#ctct-test-api');
if (apitestlink) {
apitestlink.addEventListener('click', (e) => {
e.preventDefault();

const data = new FormData();
data.append('action', 'constant_contact_test_api_ajax_handler');

const params = new URLSearchParams(e.target.href);

if (params.get('ctct-test-connection')) {
data.append('ctct-test-connection-nonce', params.get('ctct-test-connection'));
}

fetch(window.ajaxurl, options = {
method: 'POST', body: data,
})
.then((response) => response.json())
.then((response) => {
const successDOM = document.querySelector('#ctct-test-api-result');
successDOM.innerHTML = response.data.is_connected;
successDOM.setAttribute('class', '');
if (response.success) {
successDOM.classList.add('success');
} else {
successDOM.classList.add('no-success');
}
}).catch((error) => {
console.log(error);
});
});
}
};

that.init();
}(window, window.CTCTAJAX));
19 changes: 13 additions & 6 deletions assets/sass/_admin-connect.scss
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@
}

// Connection Details
.ctct-connected-wrap{

.ctct-connected-wrap {
.ctct-connection-details{
display: flex;
text-align: left;
Expand All @@ -343,15 +342,23 @@
}

p{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;

&.ctct-label{
justify-content: flex-end;
}
}
}

#ctct-test-api-result {
font-weight: bold;
margin-left: 10px;
&.success {
color: variables.$color-success-dark;
}

&.no-success {
color: variables.$color-error-dark;
}
}
}

// Getting Started/ Next Steps
Expand Down
2 changes: 2 additions & 0 deletions assets/sass/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ $color-black-translucent: rgba(0,0,0, 0.5);

// Theming
$color-error: $color-red;
$color-error-dark: #a90b00;
$color-success: $color-green;
$color-success-dark: #1c7727;

// constant contact colors
$color-sky-blue: #88c5e2;
Expand Down
41 changes: 41 additions & 0 deletions includes/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ public function get_updated_lists_ids( $old_ids_string ) {
return $this->get( "contact_lists/list_id_xrefs?sequence_ids={$old_ids_string}", $this->base_args );
}

public function test_connection() {
return $this->test( $this->base_args );
}

/**
* GET method for API requests.
*
Expand Down Expand Up @@ -434,4 +438,41 @@ private function delete( string $endpoint, array $args = [] ) : array {

return json_decode( $response['body'], true );
}

/**
* Method to do a basic API request test, to check on current credentials.
*
* @since 2.22.0
*
* @param array $args
* @return bool
*/
private function test( array $args = [] ): bool {

$url = $this->base_url . '/account/user/privileges';

$options = [
/**
* Sets the HTTP timeout, in seconds, for the request.
*
* @since 2.22.0
*
* @param int $value The timeout limit, in seconds. Defaults to 30.
* @param string $request_url The request URL.
*
* @return int
*/
'timeout' => apply_filters( 'http_request_timeout', 30, $url ),
'headers' => $args,
];

$response = wp_safe_remote_get( $url, $options );
if ( is_wp_error( $response ) ) {
return false;
}

$code = wp_remote_retrieve_response_code( $response );

return 200 === $code;
}
}
12 changes: 10 additions & 2 deletions includes/class-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function admin_page_display() {
if ( constant_contact_get_needs_manual_reconnect() ) {
$heading = esc_html__( 'Manual reconnection required', 'constant-contact-forms' );
$description = esc_html__( 'Issues with reauthentication for tokens occurred and a manual disconnect and reconnect is needed. Use the status button to start the re-authentication process.', 'constant-contact-forms' );
$btn_value = esc_attr__( 'Disconnected', 'constant-contact-forms' );
$btn_value = esc_attr__( 'Force disconnect', 'constant-contact-forms' );
}
$times = constant_contact_get_issued_expired_access_token_times();
?>
Expand Down Expand Up @@ -228,7 +228,15 @@ public function admin_page_display() {
<?php endif; ?>
<div class="ctct-connection-details">
<p class="ctct-label">
<strong><?php esc_html_e( 'Status:', 'constant-contact-forms' ); ?></strong>
<strong><?php esc_html_e( 'Test status:', 'constant-contact-forms' ); ?></strong>
</p>
<p><a id="ctct-test-api" href="<?php echo esc_url( wp_nonce_url(admin_url('edit.php?post_type=ctct_forms&page=ctct_options_connect'), 'ctct-test-connection', 'ctct-test-connection' ) ); ?>"><?php esc_html_e('Test current API key', 'constant-contact-forms' ); ?></a>
<span id="ctct-test-api-result"></span>
</p>
</div>
<div class="ctct-connection-details">
<p class="ctct-label">
<strong><?php esc_html_e( 'Action:', 'constant-contact-forms' ); ?></strong>
</p>
<form method="post" action="<?php echo esc_url( $this->redirect_url ); ?>">
<?php wp_nonce_field( 'ctct-admin-disconnect', 'ctct-admin-disconnect' ); ?>
Expand Down
30 changes: 29 additions & 1 deletion includes/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ function constant_contact_get_date_field_order( $format = '' ) {
/**
* Return an array of timestamps for issued, current, and expected expiration time for current access token.
*
* @since NEXT
* @since 2.21.0
*
* @return array|null
* @throws Exception
Expand All @@ -861,3 +861,31 @@ function constant_contact_get_issued_expired_access_token_times() {
'expires' => $obj->format( 'Y-m-d, H:i' ),
];
};

/**
* Make a test API request to a general endpoint.
*
* Meant to just verify access tokens are still valid.
*
* @since 2.22.0
*
* @return bool
*/
function constant_contact_test_api_ajax_handler(): bool {

if ( ! current_user_can( 'manage_options' ) ) {
exit();
}

if ( ! wp_verify_nonce( $_REQUEST['ctct-test-connection-nonce'], 'ctct-test-connection' ) ) {
wp_send_json_error( [ 'nonce-result' => 'failed' ] );
exit();
}

$is_connected = constant_contact()->get_api()->cc()->test_connection();
$is_connected ?
wp_send_json_success( [ 'is_connected' => 'did connect' ], 200 ) :
wp_send_json_error( [ 'is_connected' => 'did not connect' ], 200 );
exit();
}
add_action( 'wp_ajax_constant_contact_test_api_ajax_handler', 'constant_contact_test_api_ajax_handler' );