Skip to content

Commit c9b2de5

Browse files
authored
Merge pull request #219 from bcc-code/copilot/retrieve-groups-and-flush-cache
Add flush groups cache button to admin settings
2 parents 5fb1a34 + 5806769 commit c9b2de5

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

plugins/bcc-login/includes/class-bcc-login-settings.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class BCC_Login_Settings_Provider {
6666
private BCC_Login_Settings $_settings;
6767
private BCC_Coreapi_Client $_coreapi;
6868
private BCC_Storage $_storage;
69+
private bool $_use_groups_settings = false;
6970

7071
protected $option_name = 'bcc_login_settings';
7172
protected $options_page = 'bcc_login';
@@ -289,6 +290,7 @@ function register_settings() {
289290
$this->_settings->client_secret,
290291
$this->_settings->coreapi_audience
291292
);
293+
$this->_use_groups_settings = $use_groups_settings;
292294
$use_notification_settings = $use_groups_settings; // Tie notification settings to groupsettings for now
293295

294296
add_settings_section( 'general', '', null, $this->options_page );
@@ -586,12 +588,60 @@ function render_options_page() { ?>
586588
<?php do_settings_sections( $this->options_page ); ?>
587589
<?php submit_button(); ?>
588590
</form>
591+
<?php $this->render_flush_groups_cache_button( ); ?>
592+
<?php settings_errors('groups_cache_flushed'); ?>
589593
<?php $this->render_delete_subscribers_button( ); ?>
590594
<?php settings_errors('subscribers_deleted'); ?>
591595
</div>
592596
<?php
593597
}
594598

599+
/**
600+
* Renders a button for flushing groups cache
601+
*/
602+
function render_flush_groups_cache_button() {
603+
if ( ! current_user_can('manage_options') || ! $this->_use_groups_settings )
604+
return;
605+
?>
606+
<hr>
607+
<h2><?php _e( 'Groups Cache', 'bcc-login' ); ?></h2>
608+
<p><?php _e( 'Flush the cached groups data. This will force groups to be re-fetched from the API on the next page load.', 'bcc-login') ?></p>
609+
<form method="post">
610+
<?php wp_nonce_field( 'flush_groups_cache', 'flush_groups_cache_nonce' ); ?>
611+
<input type="hidden" name="flush_groups_cache" value="true">
612+
<?php submit_button( __( 'Flush Groups Cache', 'bcc-login' ), 'secondary', 'flush_groups_cache_btn', false ); ?>
613+
</form>
614+
<?php
615+
$this->flush_groups_cache_handler();
616+
}
617+
618+
/**
619+
* Handles flushing groups cache
620+
*/
621+
function flush_groups_cache_handler() {
622+
if ( ! isset( $_POST['flush_groups_cache'] ) || $_POST['flush_groups_cache'] !== 'true' ) {
623+
return;
624+
}
625+
626+
if ( ! isset( $_POST['flush_groups_cache_nonce'] ) || ! wp_verify_nonce( $_POST['flush_groups_cache_nonce'], 'flush_groups_cache' ) ) {
627+
return;
628+
}
629+
630+
if ( ! current_user_can('manage_options') ) {
631+
return;
632+
}
633+
634+
delete_transient('coreapi_groups');
635+
delete_transient('coreapi_all_groups');
636+
637+
add_settings_error(
638+
'groups_cache_flushed',
639+
'groups_cache_flushed',
640+
__( 'Groups cache has been flushed successfully.', 'bcc-login' ),
641+
'success'
642+
);
643+
}
644+
595645
/**
596646
* Renders a button for deleting subscribers
597647
*/

0 commit comments

Comments
 (0)