|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WPGraphQL\Logging\Admin; |
| 6 | + |
| 7 | +/** |
| 8 | + * The admin notice class for WPGraphQL Logging. |
| 9 | + * |
| 10 | + * @package WPGraphQL\Logging |
| 11 | + * |
| 12 | + * @since 0.0.1 |
| 13 | + */ |
| 14 | +class AdminNotice { |
| 15 | + /** |
| 16 | + * The admin page slug. |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + public const ADMIN_NOTICE_KEY = 'wpgraphql-logging-admin-notice'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The instance of the admin notice. |
| 24 | + * |
| 25 | + * @var self|null |
| 26 | + */ |
| 27 | + protected static ?self $instance = null; |
| 28 | + |
| 29 | + /** |
| 30 | + * Initializes the view logs page. |
| 31 | + */ |
| 32 | + public static function init(): ?self { |
| 33 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) { |
| 38 | + self::$instance = new self(); |
| 39 | + self::$instance->setup(); |
| 40 | + } |
| 41 | + |
| 42 | + do_action( 'wpgraphql_logging_admin_notice_init', self::$instance ); |
| 43 | + |
| 44 | + return self::$instance; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Setup the admin notice. |
| 49 | + */ |
| 50 | + public function setup(): void { |
| 51 | + $key = self::ADMIN_NOTICE_KEY; |
| 52 | + $is_dismissed = $this->is_notice_dismissed(); |
| 53 | + |
| 54 | + // Exit if the notice has been dismissed. |
| 55 | + if ( $is_dismissed ) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + add_action( 'admin_notices', [ $this, 'register_admin_notice' ], 10, 0 ); |
| 60 | + add_action( 'wp_ajax_' . $key, [ $this, 'process_ajax_request' ], 10, 0 ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Register admin notice to inform users about WPGraphQL Logging. |
| 65 | + */ |
| 66 | + public function register_admin_notice(): void { |
| 67 | + $template = __DIR__ . '/View/Templates/WPGraphQLLoggerNotice.php'; |
| 68 | + |
| 69 | + if ( ! file_exists( $template ) ) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + require $template; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Process the AJAX request. |
| 78 | + */ |
| 79 | + public function process_ajax_request(): void { |
| 80 | + $key = self::ADMIN_NOTICE_KEY; |
| 81 | + if ( ! isset( $_POST['action'] ) || esc_attr( $key ) !== $_POST['action'] ) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + check_ajax_referer( $key ); |
| 86 | + |
| 87 | + self::dismiss_admin_notice(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Check if the admin notice is dismissed. |
| 92 | + */ |
| 93 | + public function is_notice_dismissed(): bool { |
| 94 | + $key = self::ADMIN_NOTICE_KEY; |
| 95 | + return (bool) get_user_meta( get_current_user_id(), $key, true ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Dismiss the admin notice. |
| 100 | + */ |
| 101 | + public static function dismiss_admin_notice(): void { |
| 102 | + $key = self::ADMIN_NOTICE_KEY; |
| 103 | + update_user_meta( get_current_user_id(), $key, 1 ); |
| 104 | + } |
| 105 | +} |
0 commit comments