From a9c65f55d64d38c622a9fec43b9e66661e00fd00 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Fri, 22 Dec 2023 11:42:20 +0100 Subject: [PATCH] show admin notice when permalink structure is plain (#615) * show admin notice when permalink structure is plain fix #609 * fix phpcs issues * remove calculation of static array of issues * small changes * removed unused attribute --------- Co-authored-by: Matthew Exon Co-authored-by: Matthias Pfefferle --- includes/class-admin.php | 32 ++++++++++++++++++++++++++++++++ tests/test-class-admin.php | 18 ++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/test-class-admin.php diff --git a/includes/class-admin.php b/includes/class-admin.php index 7acfff9..dca3aee 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -18,6 +18,7 @@ class Admin { \add_action( 'admin_init', array( self::class, 'register_settings' ) ); \add_action( 'personal_options_update', array( self::class, 'save_user_description' ) ); \add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); + \add_action( 'admin_notices', array( self::class, 'admin_notices' ) ); if ( ! is_user_disabled( get_current_user_id() ) ) { \add_action( 'show_user_profile', array( self::class, 'add_profile' ) ); @@ -46,6 +47,37 @@ class Admin { } } + /** + * Display admin menu notices about configuration problems or conflicts. + * + * @return void + */ + public static function admin_notices() { + $permalink_structure = \get_option( 'permalink_structure' ); + if ( empty( $permalink_structure ) ) { + $admin_notice = \__( 'You are using the ActivityPub plugin without setting a permalink structure. This will prevent ActivityPub from working. Please set a permalink structure.', 'activitypub' ); + self::show_admin_notice( $admin_notice, 'error' ); + } + } + + /** + * Display one admin menu notice about configuration problems or conflicts. + * + * @param string $admin_notice The notice to display. + * @param string $level The level of the notice (error, warning, success, info). + * + * @return void + */ + private static function show_admin_notice( $admin_notice, $level ) { + ?> + +
+

+
+ + expectOutputRegex( "/notice-error/" ); + + \delete_option( 'permalink_structure' ); + } + + public function test_has_permalink_structure_no_errors() { + \add_option( 'permalink_structure', '/archives/%post_id%' ); + \do_action( 'admin_notices' ); + $this->expectOutputRegex( "/^((?!notice-error).)*$/s" ); + + \delete_option( 'permalink_structure' ); + } +}