Add quota check button to settings

This commit is contained in:
Tetrakern 2024-07-06 00:27:36 +02:00
parent 20a7a4cffe
commit 1751d0fd08
4 changed files with 95 additions and 7 deletions

View file

@ -115,7 +115,6 @@ function fcnen_empty_trashed_subscribers() {
)
);

// Terminate
exit();
}
add_action( 'admin_post_fcnen_empty_trashed_subscribers', 'fcnen_empty_trashed_subscribers' );
@ -342,7 +341,7 @@ function fcnen_import_subscribers_csv() {
// Log
fcnen_log( 'Imported CSV.' );

// Success!
// Redirect
wp_safe_redirect(
add_query_arg(
array( 'fcnen-notice' => 'csv-imported', 'fcnen-message' => $count ),
@ -350,7 +349,6 @@ function fcnen_import_subscribers_csv() {
)
);

// Terminate
exit();
}
add_action( 'admin_post_fcnen_import_subscribers_csv', 'fcnen_import_subscribers_csv' );
@ -544,7 +542,6 @@ function fcnen_update_profile() {
) . '#fcnen'
);

// Terminate
exit();
}
add_action( 'admin_post_fcnen_update_profile', 'fcnen_update_profile' );
@ -576,7 +573,6 @@ function fcnen_clear_queue() {
)
);

// Terminate
exit();
}
add_action( 'admin_post_fcnen_clear_queue', 'fcnen_clear_queue' );
@ -660,3 +656,74 @@ function fcnen_check_mailersend_bulk_status() {
exit();
}
add_action( 'admin_post_fcnen_check_mailersend_bulk_status', 'fcnen_check_mailersend_bulk_status' );

// =======================================================================================
// QUOTA
// =======================================================================================

/**
* Check API quota
*
* @since 0.1.0
*/

function fcnen_check_mailersend_api_quota() {
// Verify
if ( ! isset( $_GET['fcnen-nonce'] ) || ! check_admin_referer( 'fcnen-mailersend-api-quota', 'fcnen-nonce' ) ) {
wp_die( __( 'Nonce verification failed. Please try again.', 'fcnen' ) );
}

if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Insufficient permissions.', 'fcnen' ) );
}

// Setup
$api_key = get_option( 'fcnen_api_key' );

// API key missing
if ( empty( $api_key ) ) {
return wp_die( __( 'API key has not been set.', 'fcnen' ) );
}

// Request
$response = wp_remote_get(
FCNEN_API['mailersend']['quota'],
array(
'headers' => array(
'Authorization' => "Bearer {$api_key}",
'Content-Type' => 'application/json'
)
)
);

// Response
if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
// Success
$json = json_decode( wp_remote_retrieve_body( $response ), true );
$message = sprintf(
__( 'Quota: %s | Remaining: %s | Reset: %s', 'fcnen' ),
$json['quota'] ?? __( 'Error', 'fcnen' ),
$json['remaining'] ?? __( 'Error', 'fcnen' ),
$json['reset'] ?? __( 'Error', 'fcnen' )
);
$notice = 'mailersend-api-quota-received';
} else {
// Error
$message = is_wp_error( $response ) ? $response->get_error_message() : __( 'API request failed.', 'fcnen' );
$notice = 'mailersend-api-quota-error';
}

// Log
fcnen_log( "MailerSend API Quota request: {$message}." );

// Redirect
wp_safe_redirect(
add_query_arg(
array( 'fcnen-notice' => $notice, 'fcnen-message' => urlencode( $message ) ),
wp_get_referer()
)
);

exit();
}
add_action( 'admin_post_fcnen_check_mailersend_api_quota', 'fcnen_check_mailersend_api_quota' );

View file

@ -742,6 +742,14 @@ function fcnen_admin_notices() {
$notice = __( 'Cleared email queue.', 'fcnen' );
$type = 'success';
break;
case 'mailersend-api-quota-received':
$notice = $message;
$type = 'success';
break;
case 'mailersend-api-quota-error':
$notice = $message;
$type = 'error';
break;
}

// Render notice
@ -1754,7 +1762,11 @@ function fcnen_settings_page() {
</tr>
</tbody>
</table>
<p class="submit"><?php submit_button( __( 'Save Changes', 'fcnen' ), 'primary', 'submit', false ); ?></p>
<p class="submit"><?php
submit_button( __( 'Save Changes', 'fcnen' ), 'primary', 'submit', false );

echo '<a href="' . wp_nonce_url( admin_url( 'admin-post.php?action=fcnen_check_mailersend_api_quota' ), 'fcnen-mailersend-api-quota', 'fcnen-nonce' ) . '" class="button" rel="noopener" ' . ( $api_key ? '' : 'disabled' ) . '>' . __( 'Check Quota', 'fcnen' ) . '</a>';
?></p>
</form>
</div>
</div>

File diff suppressed because one or more lines are too long

View file

@ -21,9 +21,18 @@
}
}

p.submit {
display: flex;
gap: 12px;
}

.hidden {
display: none !important;
}

a[disabled] {
pointer-events: none;
}
}

/* =============================================================================