Co-authored-by: Martin Slachta <martin.slachta@outlook.com> Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,56 +1,53 @@
|
||||
<?php
|
||||
|
||||
|
||||
function rsv_google_calendar_settings_page(): void {
|
||||
if (!current_user_can(RsvCapabilities::MANAGE)) {
|
||||
return;
|
||||
}
|
||||
class RsvGoogleCalendarSettingsPage extends RsvAdminPage {
|
||||
|
||||
$service = new RsvGoogleCalendarService();
|
||||
$notice = null;
|
||||
protected function render_content(): void {
|
||||
$service = new RsvGoogleCalendarService();
|
||||
$notice = null;
|
||||
|
||||
if (isset($_GET['connected'])) {
|
||||
$notice = ['type' => 'success', 'message' => 'Google Calendar connected successfully.'];
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rsv_google_settings_nonce'])) {
|
||||
if (!wp_verify_nonce($_POST['rsv_google_settings_nonce'], 'rsv_google_settings')) {
|
||||
wp_die('Security check failed.');
|
||||
if (isset($_GET['connected'])) {
|
||||
$notice = ['type' => 'success', 'message' => 'Google Calendar connected successfully.'];
|
||||
}
|
||||
|
||||
if (isset($_POST['rsv_disconnect'])) {
|
||||
$service->disconnect();
|
||||
$notice = ['type' => 'success', 'message' => 'Disconnected from Google Calendar.'];
|
||||
} elseif (isset($_POST['rsv_register_webhook'])) {
|
||||
$result = $service->register_webhook();
|
||||
$notice = isset($result['id'])
|
||||
? ['type' => 'success', 'message' => 'Webhook registered.']
|
||||
: ['type' => 'error', 'message' => 'Webhook registration failed: ' . ($result['error'] ?? json_encode($result))];
|
||||
} elseif (isset($_POST['rsv_stop_webhook'])) {
|
||||
$service->stop_webhook();
|
||||
$notice = ['type' => 'success', 'message' => 'Webhook stopped.'];
|
||||
} else {
|
||||
update_option('rsv_google_client_id', sanitize_text_field($_POST['rsv_google_client_id'] ?? ''));
|
||||
update_option('rsv_google_calendar_id', sanitize_text_field($_POST['rsv_google_calendar_id'] ?? 'primary'));
|
||||
// Only overwrite the (encrypted) client secret when a new value is
|
||||
// supplied — the field renders blank, so otherwise saving any other
|
||||
// setting would wipe the stored secret.
|
||||
$client_secret = sanitize_text_field($_POST['rsv_google_client_secret'] ?? '');
|
||||
if ($client_secret !== '') {
|
||||
$service->set_client_secret($client_secret);
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rsv_google_settings_nonce'])) {
|
||||
if (!wp_verify_nonce($_POST['rsv_google_settings_nonce'], 'rsv_google_settings')) {
|
||||
wp_die('Security check failed.');
|
||||
}
|
||||
$notice = ['type' => 'success', 'message' => 'Settings saved.'];
|
||||
}
|
||||
}
|
||||
|
||||
$connected = $service->is_google_connected();
|
||||
$webhook_registered = $service->is_webhook_registered();
|
||||
$webhook_expiry = (int) get_option('rsv_google_webhook_expiration', 0);
|
||||
$client_id = esc_attr(get_option('rsv_google_client_id', ''));
|
||||
$cal_id = esc_attr(get_option('rsv_google_calendar_id', 'primary'));
|
||||
$oauth_url = esc_url($service->get_oauth_url());
|
||||
?>
|
||||
<div class="wrap">
|
||||
if (isset($_POST['rsv_disconnect'])) {
|
||||
$service->disconnect();
|
||||
$notice = ['type' => 'success', 'message' => 'Disconnected from Google Calendar.'];
|
||||
} elseif (isset($_POST['rsv_register_webhook'])) {
|
||||
$result = $service->register_webhook();
|
||||
$notice = isset($result['id'])
|
||||
? ['type' => 'success', 'message' => 'Webhook registered.']
|
||||
: ['type' => 'error', 'message' => 'Webhook registration failed: ' . ($result['error'] ?? json_encode($result))];
|
||||
} elseif (isset($_POST['rsv_stop_webhook'])) {
|
||||
$service->stop_webhook();
|
||||
$notice = ['type' => 'success', 'message' => 'Webhook stopped.'];
|
||||
} else {
|
||||
update_option('rsv_google_client_id', sanitize_text_field($_POST['rsv_google_client_id'] ?? ''));
|
||||
update_option('rsv_google_calendar_id', sanitize_text_field($_POST['rsv_google_calendar_id'] ?? 'primary'));
|
||||
// Only overwrite the (encrypted) client secret when a new value is
|
||||
// supplied — the field renders blank, so otherwise saving any other
|
||||
// setting would wipe the stored secret.
|
||||
$client_secret = sanitize_text_field($_POST['rsv_google_client_secret'] ?? '');
|
||||
if ($client_secret !== '') {
|
||||
$service->set_client_secret($client_secret);
|
||||
}
|
||||
$notice = ['type' => 'success', 'message' => 'Settings saved.'];
|
||||
}
|
||||
}
|
||||
|
||||
$connected = $service->is_google_connected();
|
||||
$webhook_registered = $service->is_webhook_registered();
|
||||
$webhook_expiry = (int) get_option('rsv_google_webhook_expiration', 0);
|
||||
$client_id = esc_attr(get_option('rsv_google_client_id', ''));
|
||||
$cal_id = esc_attr(get_option('rsv_google_calendar_id', 'primary'));
|
||||
$oauth_url = esc_url($service->get_oauth_url());
|
||||
?>
|
||||
<h1>Google Calendar</h1>
|
||||
|
||||
<?php if ($notice): ?>
|
||||
@@ -127,6 +124,6 @@ function rsv_google_calendar_settings_page(): void {
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user