init_components(); $this->setup_hooks(); } /** @ignore */ private function __clone() {} /** @ignore */ public function __wakeup() {} // ------------------------------------------------------------------------- // Initialisation // ------------------------------------------------------------------------- /** * Instantiate all plugin sub-components. * * @since 1.0.0 */ private function init_components(): void { $this->settings_manager = AAMO_Notification_Groups_Settings_Manager::get_instance(); $this->notice_collector = AAMO_Notification_Groups_Notice_Collector::get_instance(); } /** * Register all WordPress hooks. * * Note: the activation hook is registered in the main plugin file * (aamo-notification-groups.php) rather than here, because it must be * called at file-load time — before plugins_loaded fires — for correct * behaviour during a first activation. * * @since 1.0.0 */ private function setup_hooks(): void { add_action( 'admin_menu', array( $this, 'register_admin_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'admin_notices', array( $this, 'maybe_render_demo_notices' ) ); add_action( 'admin_notices', array( $this, 'maybe_render_settings_saved_notice' ) ); add_action( 'admin_post_' . AAMO_Notification_Groups_Constants::SAVE_SETTINGS_ACTION, array( $this, 'handle_save_settings' ) ); } // ------------------------------------------------------------------------- // Admin page // ------------------------------------------------------------------------- /** * Register the plugin's admin page as a submenu under Settings. * * @since 1.0.0 */ public function register_admin_menu(): void { $capability = AAMO_Notification_Groups_Constants::CAP_MANAGE_NOTIFICATIONS; $this->main_hook = add_submenu_page( 'options-general.php', __( 'Notification Groups', 'aamo-notification-groups' ), __( 'Notification Groups', 'aamo-notification-groups' ), $capability, AAMO_Notification_Groups_Constants::PAGE_MAIN, array( $this, 'render_main_page' ) ); } /** * Render the main admin page. * * @since 1.0.0 */ public function render_main_page(): void { if ( ! current_user_can( AAMO_Notification_Groups_Constants::CAP_MANAGE_NOTIFICATIONS ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'aamo-notification-groups' ) ); } include AAMO_NOTIFICATION_GROUPS_DIR . 'templates/admin-page.php'; } // ------------------------------------------------------------------------- // Demo notices // ------------------------------------------------------------------------- /** * Output a one-off batch of fake notices covering every severity tab, * but only when this plugin's own "Generate Demo Notifications" link * was just clicked — verified via nonce, and scoped to this plugin's * own admin page. Nothing is persisted; a normal visit to this page * never shows these. * * @since 1.0.0 */ public function maybe_render_demo_notices(): void { if ( empty( $_GET[ AAMO_Notification_Groups_Constants::DEMO_QUERY_ARG ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- presence check only; verified below. return; } if ( ! $this->main_hook || ! get_current_screen() || get_current_screen()->id !== $this->main_hook ) { return; } if ( ! current_user_can( AAMO_Notification_Groups_Constants::CAP_MANAGE_NOTIFICATIONS ) ) { return; } check_admin_referer( AAMO_Notification_Groups_Constants::DEMO_NONCE_ACTION ); $this->render_demo_notices(); } /** * Echo the demo notice battery: two each of error/warning/success/info * (one success notice deliberately uses the legacy `.updated` class * instead of `notice-success`), plus one bare `.notice` with no * severity class so the Misc tab has something to show too. * * @since 1.0.0 */ private function render_demo_notices(): void { $demo_notices = array( array( 'class' => 'notice notice-error is-dismissible', 'message' => __( 'Demo: payment gateway failed to connect.', 'aamo-notification-groups' ), ), array( 'class' => 'notice notice-error', 'message' => __( 'Demo: a required field is missing on the last saved record.', 'aamo-notification-groups' ), ), array( 'class' => 'notice notice-warning', 'message' => __( 'Demo: a scheduled task did not run on time.', 'aamo-notification-groups' ), ), array( 'class' => 'notice notice-warning', 'message' => __( 'Demo: storage usage is approaching its limit.', 'aamo-notification-groups' ), ), array( 'class' => 'notice notice-success is-dismissible', 'message' => __( 'Demo: settings saved successfully.', 'aamo-notification-groups' ), ), array( 'class' => 'updated', 'message' => __( 'Demo: import completed (legacy .updated notice style).', 'aamo-notification-groups' ), ), array( 'class' => 'notice notice-info', 'message' => __( 'Demo: a new version of a dependency is available.', 'aamo-notification-groups' ), ), array( 'class' => 'notice notice-info', 'message' => __( 'Demo: this feature is in beta.', 'aamo-notification-groups' ), ), array( 'class' => 'notice', 'message' => __( 'Demo: uncategorized notice with no severity class — lands in Misc.', 'aamo-notification-groups' ), ), ); foreach ( $demo_notices as $notice ) { printf( '
%2$s
%s