Skip to content

Commit

Permalink
Fix cache cleared notice type (#1659)
Browse files Browse the repository at this point in the history
OptionsTab: Uses success type on cache-cleared submit action

* Also adds an try-catch around plugin clear action

* Documents the use of notice_type
  • Loading branch information
datengraben authored Dec 1, 2024
1 parent 7b12c3f commit 662f80a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/Messages/AdminMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
*/
class AdminMessage {


private string $message;

private string $notice_type;
private string $message;

private string $notice_type;


/**
* __construct
*
* @param mixed $message message text
* @param mixed $notice_type admin_notice type (can be: info, warning, success, error)
* @param mixed $notice_type admin_notice type (can be: info, warning, success, error) and corresponds to the
* css class `notice-*` in the rendered element.
*
* @return void
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ public static function registerLocationTaxonomy() {

/**
* Renders error for backend_notice.
* TODO refactor this using the AdminMessage type
*/
public static function renderError() {
$errorTypes = [
Expand All @@ -501,6 +502,22 @@ public static function renderError() {
delete_transient( $errorType );
}
}

$infoTypes = [
OptionsTab::SUCCESS_TYPE,
];

foreach ( $infoTypes as $info_type ) {
if ( $message = get_transient( $info_type ) ) {
$class = 'notice notice-success is-dismissible';
printf(
'<div class="%1$s"><p>%2$s</p></div>',
esc_attr( $class ),
commonsbooking_sanitizeHTML( $message )
);
delete_transient( $info_type );
}
}
}

/**
Expand Down
32 changes: 25 additions & 7 deletions src/Wordpress/Options/OptionsTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CommonsBooking\Wordpress\Options;

use CommonsBooking\Messages\AdminMessage;
use CommonsBooking\Plugin;
use CommonsBooking\View\TimeframeExport;
use Exception;
Expand All @@ -16,6 +17,8 @@ class OptionsTab {

// Error type for backend error output
public const ERROR_TYPE = "commonsbooking-options-error";

public const SUCCESS_TYPE = "commonsbooking-options-success";
/**
* @var \CMB2
*/
Expand Down Expand Up @@ -118,12 +121,18 @@ public static function prependTitle( array $metabox_group ): array {
}

/**
* actions to be fired after the options page was saved
* Actions to be fired after the options page was saved.
*
* @return void
* @throws Exception
*/
public static function savePostOptions() {

// Dev note: At the moment this uses transients, which then are hooked into `admin_notices` action in Plugin.php
// Why? Transients are used here instead of AdminMessage class, because the hook of savePostOptions
// (CMB2-Hook 'cmb2_save_options-page_field') is too late for rendering of the admin page.
// So if you want to refactor this, savePostOptions needs to be hooked into an action which fires earlier.

if ( array_key_exists( 'action', $_REQUEST ) && $_REQUEST['action'] == "commonsbooking_options_export" ) {
// Check for export action
if ( array_key_exists( 'export-filepath', $_REQUEST ) && $_REQUEST['export-filepath'] !== "" ) {
Expand All @@ -146,12 +155,21 @@ public static function savePostOptions() {
} elseif ( array_key_exists( 'action', $_REQUEST ) && $_REQUEST['action'] == "commonsbooking_options_advanced-options" ) {
//Check for request to clear cache
if ( array_key_exists( 'submit-cmb', $_REQUEST ) && $_REQUEST['submit-cmb'] == "clear-cache" ) {
Plugin::clearCache();
set_transient(
self::ERROR_TYPE,
commonsbooking_sanitizeHTML( __( "Cache cleared.", 'commonsbooking' ) ),
45
);
try {
Plugin::clearCache();
set_transient(
self::SUCCESS_TYPE,
commonsbooking_sanitizeHTML( __( "Cache cleared.", 'commonsbooking' ) ),
45 );
} catch ( Exception $e ) {
if ( WP_DEBUG ) {
error_log( $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
set_transient(
self::ERROR_TYPE,
commonsbooking_sanitizeHTML( __( "Error while clearing the cache.", 'commonsbooking' ) ),
45 );
}
}
}

Expand Down

0 comments on commit 662f80a

Please sign in to comment.