From bda8448e64edf12182e3733c86a96f0f7b797dd4 Mon Sep 17 00:00:00 2001 From: Bruno FG Date: Sun, 14 Jan 2024 13:45:11 +1100 Subject: [PATCH] Add Notices and small bugfixes and improvements --- README.md | 2 +- config/di-config.php | 2 + includes/Admin/Notices.php | 57 ++++++++++++++++++++++++ includes/Admin/Taxonomy_Form_Handler.php | 18 ++++++-- includes/Admin/Taxonomy_Form_Page.php | 9 +--- 5 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 includes/Admin/Notices.php diff --git a/README.md b/README.md index 9290bf5..2e04b71 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Once the plugin is activate, there is no need to generate any code for you to pu ## Installation -Composer install is suficient +Composer install is sufficient ``` composer install ``` diff --git a/config/di-config.php b/config/di-config.php index c3b73ed..0a23ce9 100644 --- a/config/di-config.php +++ b/config/di-config.php @@ -17,6 +17,7 @@ namespace Custom_PTT; use function DI\create; +use function DI\decorate; return array( 'Admin_Menu' => create( 'Custom_PTT\Admin\Admin_Menu' ), @@ -24,4 +25,5 @@ 'Post_Type' => create( 'Custom_PTT\Post_Type\Post_Type' ), 'Taxonomy_Form_Page' => create( 'Custom_PTT\Admin\Taxonomy_Form_Page' ), 'Taxonomy_Form_Handler' => create( 'Custom_PTT\Admin\Taxonomy_Form_Handler' ), + 'Notices' => create( 'Custom_PTT\Admin\Notices' ), ); diff --git a/includes/Admin/Notices.php b/includes/Admin/Notices.php new file mode 100644 index 0000000..ab6b1c0 --- /dev/null +++ b/includes/Admin/Notices.php @@ -0,0 +1,57 @@ +init(); + } + + public function init(): void { + add_action( 'admin_notices', array( Notices::class, 'display_admin_notices' ) ); + } + + /** + * Add admin Notices + * + * @param string $severity + * @param string $message + * + * @return void + * @throws Exception + */ + public static function add_admin_notice( string $severity, string $message ): void { + $notices = get_option( 'custom_ptt_notices', array() ); + $notices[] = array( + 'severity' => $severity, + 'message' => $message, + ); + update_option( 'custom_ptt_notices', $notices ); + } + + /** + * @throws Exception + */ + public static function display_admin_notices(): void { + $notices = get_option( 'custom_ptt_notices', array() ); + + foreach ( $notices as $notice ) { + printf( '

%s

', esc_attr( $notice['severity'] ), esc_html( $notice['message'] ) ); + } + + delete_option( 'custom_ptt_notices' ); + } +} diff --git a/includes/Admin/Taxonomy_Form_Handler.php b/includes/Admin/Taxonomy_Form_Handler.php index c255b6a..07370a9 100644 --- a/includes/Admin/Taxonomy_Form_Handler.php +++ b/includes/Admin/Taxonomy_Form_Handler.php @@ -67,13 +67,21 @@ public function handle_form_submission(): void { } $store_taxonomy = $this->store_taxonomy( $data ); - if ( false === $store_taxonomy ) { + if ( ! array_key_exists( $data['taxonomy_slug'], $store_taxonomy ) ) { throw new Exception( esc_html__( 'An error occurred while saving the taxonomy.', 'custom-post-types-taxonomies' ) ); } + Notices::add_admin_notice( + 'success', + esc_html__( 'Taxonomy updated successfully.', 'custom-post-types-taxonomies' ) + ); wp_redirect( admin_url( 'admin.php?page=custom-post-types-taxonomies-taxonomies&status=success' ) ); } catch ( Exception $e ) { + Notices::add_admin_notice( + 'error', + $e->getMessage() + ); wp_redirect( admin_url( 'admin.php?page=custom-post-types-taxonomies-taxonomies&status=error' ) ); } exit; @@ -110,14 +118,16 @@ private function register_taxonomy( array $data ): WP_Error|WP_Taxonomy { * * @param array $data The data to use when storing the taxonomy. * + * @return array Return the registered taxonomies. * @since 0.1.0-alpha - * @return bool True if the option was updated successfully, false otherwise. */ - private function store_taxonomy( array $data ): bool { + private function store_taxonomy( array $data ): array { $taxonomies = get_option( CUSTOM_PTT_TAXONOMY_OPTION_NAME, array() ); $taxonomies[ $data['taxonomy_slug'] ] = $data; - return update_option( CUSTOM_PTT_TAXONOMY_OPTION_NAME, $taxonomies ); + $update_taxonomy = update_option( CUSTOM_PTT_TAXONOMY_OPTION_NAME, $taxonomies ); + + return get_option( CUSTOM_PTT_TAXONOMY_OPTION_NAME, array() ); } /** diff --git a/includes/Admin/Taxonomy_Form_Page.php b/includes/Admin/Taxonomy_Form_Page.php index 9c25c8f..b67c629 100644 --- a/includes/Admin/Taxonomy_Form_Page.php +++ b/includes/Admin/Taxonomy_Form_Page.php @@ -5,7 +5,6 @@ namespace Custom_PTT\Admin; use Exception; -use Custom_PTT\Config\Config_Loader; use Custom_PTT\Infrastructure\Registerable; use Custom_PTT\Utilities; use WP_Taxonomy; @@ -69,13 +68,7 @@ public function add_form_page(): void { * @throws Exception */ public function render_form(): void { - $taxonomy_name = ''; - if ( - isset( $_REQUEST['custom_ptt_taxonomy_nonce'] ) && - wp_verify_nonce( $_REQUEST['custom_ptt_taxonomy_nonce'], 'custom_ptt_save_taxonomy' ) - ) { - $taxonomy_name = isset( $_GET['taxonomy'] ) ? sanitize_text_field( $_GET['taxonomy'] ) : ''; - } + $taxonomy_name = isset( $_GET['taxonomy'] ) ? sanitize_text_field( $_GET['taxonomy'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $taxonomy_data = null; $taxonomy = get_taxonomy( $taxonomy_name );