diff --git a/src/Messages/AdminMessage.php b/src/Messages/AdminMessage.php index 58b3bc3aa..857c0805c 100644 --- a/src/Messages/AdminMessage.php +++ b/src/Messages/AdminMessage.php @@ -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 */ diff --git a/src/Plugin.php b/src/Plugin.php index 9055b64a0..676caf81b 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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 = [ @@ -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( + '

%2$s

', + esc_attr( $class ), + commonsbooking_sanitizeHTML( $message ) + ); + delete_transient( $info_type ); + } + } } /** diff --git a/src/Wordpress/Options/OptionsTab.php b/src/Wordpress/Options/OptionsTab.php index a9159f4a1..4b4e7b225 100644 --- a/src/Wordpress/Options/OptionsTab.php +++ b/src/Wordpress/Options/OptionsTab.php @@ -2,6 +2,7 @@ namespace CommonsBooking\Wordpress\Options; +use CommonsBooking\Messages\AdminMessage; use CommonsBooking\Plugin; use CommonsBooking\View\TimeframeExport; use Exception; @@ -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 */ @@ -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'] !== "" ) { @@ -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 ); + } } }