Skip to content

Commit

Permalink
Add Notices and small bugfixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
freibergergarcia committed Jan 14, 2024
1 parent 2e159d5 commit bda8448
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 2 additions & 0 deletions config/di-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
namespace Custom_PTT;

use function DI\create;
use function DI\decorate;

return array(
'Admin_Menu' => create( 'Custom_PTT\Admin\Admin_Menu' ),
'Taxonomy' => create( 'Custom_PTT\Taxonomy\Taxonomy' ),
'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' ),
);
57 changes: 57 additions & 0 deletions includes/Admin/Notices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare( strict_types=1 );

namespace Custom_PTT\Admin;

use Exception;

/**
* Notices Class
*
* This class is responsible for adding admin notices.
*
* @package Custom_PTT\Admin
* @since 0.1.0-alpha
*/
final class Notices {

public function __construct() {
$this->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( '<div class="notice notice-%s is-dismissible"><p>%s</p></div>', esc_attr( $notice['severity'] ), esc_html( $notice['message'] ) );
}

delete_option( 'custom_ptt_notices' );
}
}
18 changes: 14 additions & 4 deletions includes/Admin/Taxonomy_Form_Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() );
}

/**
Expand Down
9 changes: 1 addition & 8 deletions includes/Admin/Taxonomy_Form_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit bda8448

Please sign in to comment.