@@ -823,40 +809,4 @@ public static function ajax_reset_throttle(): void {
]
);
}
-
- /**
- * AJAX: salt-fix helper ("Fix WordPress salts"). Two modes:
- * - preview: return a ready-to-paste block of freshly generated salts.
- * - recheck: re-evaluate the salts and clear the notice once they are strong
- * (used after the admin pastes the salts into wp-config.php).
- * MagicAuth never writes wp-config.php itself, and never auto-enables the
- * branded login replacement.
- */
- public static function ajax_fix_salts(): void {
- check_ajax_referer( 'magicauth-admin-recovery' );
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_send_json_error( [ 'message' => __( 'You are not allowed to do this.', 'magicauth' ) ], 403 );
- }
-
- $mode = 'preview';
- if ( isset( $_POST['mode'] ) && is_string( $_POST['mode'] ) ) {
- $mode = sanitize_key( wp_unslash( $_POST['mode'] ) );
- }
-
- if ( 'recheck' === $mode ) {
- $result = Installer::recheck_salts_from_file();
- if ( ! $result['ok'] ) {
- wp_send_json_error( [ 'message' => $result['message'] ] );
- }
- wp_send_json_success( [ 'message' => $result['message'] ] );
- }
-
- // Default: preview. MagicAuth never edits wp-config.php; it generates a
- // fresh block of salts for the admin to paste in themselves.
- wp_send_json_success(
- [
- 'block' => Installer::generate_salt_block(),
- ]
- );
- }
}
diff --git a/includes/Installer.php b/includes/Installer.php
index 8d3796c..acfc748 100644
--- a/includes/Installer.php
+++ b/includes/Installer.php
@@ -13,6 +13,17 @@ final class Installer {
private const SALT_NOTICE_KEY = 'magicauth_salt_notice';
+ /**
+ * Site option set when an admin dismisses the weak-salt notice. Site-wide
+ * (a single option, not per-user meta), so one admin dismissing hides the
+ * notice for every admin. check_salts() clears it if salts ever return to
+ * strong, so the notice re-arms on a future regression.
+ */
+ private const SALT_NOTICE_DISMISSED_OPTION = 'magicauth_salt_notice_dismissed';
+
+ /** Nonce action backing the AJAX dismissal of the weak-salt notice. */
+ private const SALT_NOTICE_DISMISS_NONCE = 'magicauth_dismiss_salt_notice';
+
/** The eight key/salt constants WordPress derives wp_salt() from. */
private const SALT_CONSTANTS = [
'AUTH_KEY',
@@ -169,8 +180,10 @@ private static function runtime_salts_weak(): bool {
/**
* Keep the weak-salt admin-notice transient in sync with runtime reality.
* Runs on activation and on every admin_init, so a site whose salts are
- * provided by the environment, or fixed outside the wizard, clears the notice
- * on its own without a false "still weak" nag. Writes only on a state change.
+ * provided by the environment, or fixed by hand, clears the notice on its
+ * own without a false "still weak" nag. Writes only on a state change. When
+ * salts test strong again it also drops the site-wide dismissal so a later
+ * regression re-arms the notice.
*/
public static function check_salts(): void {
$weak = self::runtime_salts_weak();
@@ -180,6 +193,12 @@ public static function check_salts(): void {
} elseif ( ! $weak && $flagged ) {
delete_transient( self::SALT_NOTICE_KEY );
}
+
+ // Re-arm the dismissible notice if salts return to strong. Guarded so we
+ // only write on an actual state change.
+ if ( ! $weak && false !== get_option( self::SALT_NOTICE_DISMISSED_OPTION, false ) ) {
+ delete_option( self::SALT_NOTICE_DISMISSED_OPTION );
+ }
}
/** Whether the weak-salt notice is raised. Drives the notice and the toggle warning. */
@@ -215,187 +234,74 @@ public static function render_fpm_notice(): void {
'magicauth',
- 'magicauth-fix-salts' => '1',
- ],
- admin_url( 'options-general.php' )
- );
+ $nonce = wp_create_nonce( self::SALT_NOTICE_DISMISS_NONCE );
?>
-
+
-
- step-by-step guide, or generate fresh salts at api.wordpress.org/secret-key and paste them into wp-config.php.', 'magicauth' ),
- esc_url( self::DOCS_SALTS_URL ),
- 'https://api.wordpress.org/secret-key/1.1/salt/'
- ),
- [
- 'a' => [
- 'href' => true,
- 'target' => true,
- 'rel' => true,
- ],
- 'code' => [],
- ]
- );
- ?>
+
+
- */
- private static function salt_values(): array {
- $values = [];
- foreach ( self::SALT_CONSTANTS as $name ) {
- $values[ $name ] = self::generate_salt_value();
- }
- return $values;
- }
-
/**
- * Build a ready-to-paste block of all eight define() lines with fresh salts.
- * Used for the manual copy-and-paste path when wp-config.php is not writable.
+ * AJAX: persist a site-wide dismissal of the weak-salt notice. One admin
+ * dismissing hides it for every admin (a single site option, not per-user
+ * meta). check_salts() clears the option if salts ever return to strong, so
+ * the notice re-arms on a future regression.
*/
- public static function generate_salt_block(): string {
- $lines = [];
- foreach ( self::salt_values() as $name => $value ) {
- $lines[] = sprintf( "define( '%s', '%s' );", $name, $value );
+ public static function ajax_dismiss_salt_notice(): void {
+ check_ajax_referer( self::SALT_NOTICE_DISMISS_NONCE );
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( [ 'message' => __( 'You are not allowed to do this.', 'magicauth' ) ], 403 );
}
- return implode( "\n", $lines );
- }
-
- /**
- * Regex capturing a single define() with its quote style and value (group 3).
- * Anchored to a line start (multiline) so commented-out lines such as
- * `// define( 'AUTH_KEY', ... );` are skipped — PHP honours the first active
- * define, and matching a comment instead would corrupt detection/rewrite.
- */
- private static function define_pattern( string $name ): string {
- return '/^[ \t]*define\(\s*([\'"])' . preg_quote( $name, '/' ) . '\1\s*,\s*([\'"])(.*?)\2\s*\)\s*;/m';
- }
-
- /**
- * File-based weak-salt detection (operates on wp-config text, not runtime
- * constants). A salt is weak if missing, empty, or placeholder.
- */
- public static function config_has_weak_salts( string $contents ): bool {
- foreach ( self::SALT_CONSTANTS as $name ) {
- if ( ! preg_match( self::define_pattern( $name ), $contents, $matches ) ) {
- return true;
- }
- $value = $matches[3];
- if ( '' === trim( $value ) ) {
- return true;
- }
- foreach ( self::PLACEHOLDER_MARKERS as $marker ) {
- if ( false !== stripos( $value, $marker ) ) {
- return true;
- }
- }
- }
- return false;
- }
-
- /** Resolve wp-config.php the same way wp-load.php does (ABSPATH or one level up). */
- public static function locate_wp_config(): ?string {
- $candidate = ABSPATH . 'wp-config.php';
- if ( file_exists( $candidate ) ) {
- return $candidate;
- }
- $parent = dirname( ABSPATH ) . '/wp-config.php';
- if ( file_exists( $parent ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {
- return $parent;
- }
- return null;
- }
-
- /** WP_Filesystem in the credential-free 'direct' mode, or null if unavailable. */
- private static function filesystem(): ?\WP_Filesystem_Base {
- if ( ! function_exists( 'WP_Filesystem' ) ) {
- require_once ABSPATH . 'wp-admin/includes/file.php';
- }
- if ( 'direct' !== get_filesystem_method() ) {
- return null;
- }
- if ( ! WP_Filesystem() ) {
- return null;
- }
- global $wp_filesystem;
- return $wp_filesystem instanceof \WP_Filesystem_Base ? $wp_filesystem : null;
- }
-
- /**
- * Verify salts after a manual edit and clear the notice if they are now strong.
- * The running AJAX request has already reloaded wp-config.php, so the runtime
- * constants are authoritative and cover environment- or include-provided salts
- * that never appear as a literal define(). The located wp-config.php text is a
- * secondary signal only.
- *
- * @return array{ok:bool,message:string}
- */
- public static function recheck_salts_from_file(): array {
- $ok_message = __( 'Salts look good now. You can enable the branded login replacement whenever you like.', 'magicauth' );
-
- // Authoritative: runtime constants reflect the current file plus any
- // environment/included salts. If they are strong, the salts are fixed.
- if ( ! self::runtime_salts_weak() ) {
- delete_transient( self::SALT_NOTICE_KEY );
- return [
- 'ok' => true,
- 'message' => $ok_message,
- ];
- }
-
- // Secondary: scan the located wp-config.php directly, in case this request
- // did not pick up the edit.
- $path = self::locate_wp_config();
- if ( null !== $path ) {
- $filesystem = self::filesystem();
- $contents = null !== $filesystem ? $filesystem->get_contents( $path ) : null;
- if ( is_string( $contents ) && '' !== $contents && ! self::config_has_weak_salts( $contents ) ) {
- delete_transient( self::SALT_NOTICE_KEY );
- return [
- 'ok' => true,
- 'message' => $ok_message,
- ];
- }
- }
-
- return [
- 'ok' => false,
- 'message' => __( 'Still detecting placeholder or empty salts. Make sure you replaced all eight lines and saved wp-config.php.', 'magicauth' ),
- ];
+ update_option( self::SALT_NOTICE_DISMISSED_OPTION, 1 );
+ wp_send_json_success();
}
}
diff --git a/includes/Plugin.php b/includes/Plugin.php
index 23d6134..92581a3 100644
--- a/includes/Plugin.php
+++ b/includes/Plugin.php
@@ -45,8 +45,13 @@ public function boot(): void {
add_action( 'admin_notices', [ Installer::class, 'render_salt_notice' ] );
add_action( 'admin_notices', [ Installer::class, 'render_fpm_notice' ] );
+ // Site-wide dismissal of the weak-salt notice. Registered here (not inside
+ // the is_admin() block below) so it wires for admin-ajax requests, which the
+ // notice's self-contained dismiss script POSTs to from any admin screen.
+ add_action( 'wp_ajax_magicauth_dismiss_salt_notice', [ Installer::class, 'ajax_dismiss_salt_notice' ] );
+
// Re-evaluate salt strength on each admin load so the notice tracks reality
- // (e.g. salts provided by the environment, or fixed by hand outside the wizard).
+ // (e.g. salts provided by the environment, or fixed by hand).
add_action( 'admin_init', [ Installer::class, 'check_salts' ] );
Auth\Controller::setup();
diff --git a/tests/phpunit/Model/SaltFixTest.php b/tests/phpunit/Model/SaltFixTest.php
deleted file mode 100644
index a508fd5..0000000
--- a/tests/phpunit/Model/SaltFixTest.php
+++ /dev/null
@@ -1,110 +0,0 @@
-assertSame( 64, strlen( $value ) );
- // Must be embeddable inside a single-quoted PHP literal.
- $this->assertStringNotContainsString( "'", $value );
- $this->assertStringNotContainsString( '\\', $value );
- $this->assertNotSame( $value, Installer::generate_salt_value(), 'Values must be random per call.' );
- }
-
- public function test_salt_block_defines_all_eight_constants(): void {
- $block = Installer::generate_salt_block();
- foreach ( self::CONSTANTS as $name ) {
- $this->assertStringContainsString( "define( '" . $name . "',", $block );
- }
- $this->assertSame( 8, substr_count( $block, 'define(' ) );
- $this->assertFalse( Installer::config_has_weak_salts( $block ), 'A freshly generated block is not weak.' );
- }
-
- public function test_generated_block_is_locally_random(): void {
- // Local generation only: two blocks must differ (no fixed or remote source).
- $this->assertNotSame( Installer::generate_salt_block(), Installer::generate_salt_block() );
- }
-
- public function test_placeholder_config_reads_as_weak(): void {
- $this->assertTrue( Installer::config_has_weak_salts( $this->placeholder_config() ) );
- }
-
- public function test_empty_value_reads_as_weak(): void {
- $config = "assertTrue( Installer::config_has_weak_salts( $config ) );
- }
-
- public function test_missing_define_reads_as_weak(): void {
- // AUTH_KEY present and strong, the other seven absent.
- $config = "assertTrue( Installer::config_has_weak_salts( $config ) );
- }
-
- public function test_strong_config_reads_as_clean(): void {
- $lines = [ 'assertFalse( Installer::config_has_weak_salts( implode( "\n", $lines ) ) );
- }
-
- public function test_detection_ignores_commented_define_lines(): void {
- // A commented, strong-looking define sits above the active placeholder
- // defines. Detection must read the active defines (weak), not the comment.
- $lines = [ 'assertTrue( Installer::config_has_weak_salts( implode( "\n", $lines ) ) );
- }
-
- public function test_detection_handles_double_quoted_defines(): void {
- $weak = [ 'assertTrue( Installer::config_has_weak_salts( implode( "\n", $weak ) ) );
- $this->assertFalse( Installer::config_has_weak_salts( implode( "\n", $strong ) ) );
- }
-}