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 <git.mexon@spamgourmet.com>
Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
This commit is contained in:
Matthew Exon 2023-12-22 11:42:20 +01:00 committed by GitHub
parent 3cda64a255
commit a9c65f55d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View file

@ -18,6 +18,7 @@ class Admin {
\add_action( 'admin_init', array( self::class, 'register_settings' ) ); \add_action( 'admin_init', array( self::class, 'register_settings' ) );
\add_action( 'personal_options_update', array( self::class, 'save_user_description' ) ); \add_action( 'personal_options_update', array( self::class, 'save_user_description' ) );
\add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) ); \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() ) ) { if ( ! is_user_disabled( get_current_user_id() ) ) {
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) ); \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 ) {
?>
<div class="notice notice-<?php echo esc_attr( $level ); ?>">
<p><?php echo wp_kses( $admin_notice, 'data' ); ?></p>
</div>
<?php
}
/** /**
* Load settings page * Load settings page
*/ */

View file

@ -0,0 +1,18 @@
<?php
class Test_Admin extends WP_UnitTestCase {
public function test_no_permalink_structure_has_errors() {
\add_option( 'permalink_structure', '' );
\do_action( 'admin_notices' );
$this->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' );
}
}