Make WordPress Core

Changeset 63943


Ignore:
Timestamp:
09/26/2026 12:37:37 PM (6 weeks ago)
Author:
afercia
Message:

Improve the Settings admin notices implementation.

The amdin notices rendered in the Settings pages use their own function to echo the notices, which is a little legacy. Today, Core provides a standardized way to render the admin notices by the means of wp_admin_notice().

  • Refactors settings_errors() to use wp_admin_notice() internally.
  • Expands the related tests.

Added bonus: The Settings pages notices will now fire the some hooks and filters fired by wp_admin_notice(), e.g. wp_admin_notice and wp_admin_notice_markup, which is a necessary preparation for #50486.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13669

Props afercia, westonruter, joedolson.
See #50486.
Fixes #66161.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template.php

    r63648 r63943  
    19751975 * @since 5.3.0 Legacy `error` and `updated` CSS classes are mapped to
    19761976 *              `notice-error` and `notice-success`.
     1977 * @since 7.2.0 Uses `wp_admin_notice()` to echo the notices so that the CSS
     1978 *              classes, filters, and hooks are handled consistently.
    19771979 *
    19781980 * @param string $setting        Optional slug title of a specific setting whose errors you want.
    … …  
    19931995        }
    19941996
    1995         $output = '';
    1996 
    19971997        foreach ( $settings_errors as $key => $details ) {
    1998                 if ( 'updated' === $details['type'] ) {
    1999                         $details['type'] = 'success';
    2000                 }
    2001 
    2002                 if ( in_array( $details['type'], array( 'error', 'success', 'warning', 'info' ), true ) ) {
    2003                         $details['type'] = 'notice-' . $details['type'];
    2004                 }
    2005 
    2006                 $css_id    = sprintf(
     1998                $type = trim( $details['type'] );
     1999
     2000                if ( 'updated' === $type ) {
     2001                        $type = 'success';
     2002                }
     2003
     2004                $additional_classes = array( 'settings-error' );
     2005
     2006                /*
     2007                 * Backward compatibility: `wp_admin_notice()` generates the admin notice
     2008                 * CSS classes based on the passed message type. For example, 'error',
     2009                 * 'success', 'warning', 'info'. Custom types will be treated as
     2010                 * additional classes and appended to the end of the CSS classes.
     2011                 */
     2012                if ( ! in_array( $type, array( 'error', 'success', 'warning', 'info' ), true ) ) {
     2013                        $additional_classes = array_merge( $additional_classes, explode( ' ', $type ) );
     2014                        $type               = '';
     2015                }
     2016
     2017                /*
     2018                 * Note that the setting error code contains underscores, for example:
     2019                 * `settings_updated`. This will build a CSS ID selector that contains
     2020                 * underscores, which are currently not allowed by the WordPress CSS
     2021                 * Coding Standards. Kept for backwards compatibility.
     2022                 */
     2023                $css_id = sprintf(
    20072024                        'setting-error-%s',
    2008                         esc_attr( $details['code'] )
     2025                        $details['code'],
    20092026                );
    2010                 $css_class = sprintf(
    2011                         'notice %s settings-error is-dismissible',
    2012                         esc_attr( $details['type'] )
     2027
     2028                wp_admin_notice(
     2029                        "<strong>{$details['message']}</strong>",
     2030                        array(
     2031                                'type'               => $type,
     2032                                'code'               => $details['code'],
     2033                                'id'                 => $css_id,
     2034                                'paragraph_wrap'     => true,
     2035                                'dismissible'        => true,
     2036                                'additional_classes' => $additional_classes,
     2037                        )
    20132038                );
    2014 
    2015                 $output .= "<div id='$css_id' class='$css_class'> \n";
    2016                 $output .= "<p><strong>{$details['message']}</strong></p>";
    2017                 $output .= "</div> \n";
    2018         }
    2019 
    2020         echo $output;
     2039        }
    20212040}
    20222041
  • trunk/tests/phpunit/tests/admin/includesTemplate.php

    r62756 r63943  
    443443                $wp_settings_errors = null;
    444444
    445                 $expected = sprintf( 'notice %s settings-error is-dismissible', $expected );
    446 
    447445                $this->assertStringContainsString( $expected, $output );
    448446                $this->assertStringNotContainsString( 'notice-notice-', $output );
    … …  
    451449        public function data_settings_errors_css_classes() {
    452450                return array(
    453                         array( 'error', 'notice-error' ),
    454                         array( 'success', 'notice-success' ),
    455                         array( 'warning', 'notice-warning' ),
    456                         array( 'info', 'notice-info' ),
    457                         array( 'updated', 'notice-success' ),
    458                         array( 'notice-error', 'notice-error' ),
    459                         array( 'error my-own-css-class hello world', 'error my-own-css-class hello world' ),
     451                        // The admin notices default types will generate CSS classes in this order.
     452                        array( 'error', 'notice notice-error is-dismissible settings-error' ),
     453                        array( 'success', 'notice notice-success is-dismissible settings-error' ),
     454                        array( 'warning', 'notice notice-warning is-dismissible settings-error' ),
     455                        array( 'info', 'notice notice-info is-dismissible settings-error' ),
     456                        array( 'updated', 'notice notice-success is-dismissible settings-error' ),
     457                        /*
     458                         * Backward compatibility: custom types and anything else (e.g.
     459                         * strings with spaces) are now passed as additional_classes. As
     460                         * such, the classes will be appended to the end of the generated
     461                         * class attribute.
     462                         */
     463                        array( 'notice-error', 'notice is-dismissible settings-error notice-error' ),
     464                        array( 'custom', 'notice is-dismissible settings-error custom' ),
     465                        array( 'error my-own-css-class hello world', 'notice is-dismissible settings-error error my-own-css-class hello world' ),
     466                        array( 'error my-notice-is-awesome hello world', 'notice is-dismissible settings-error error my-notice-is-awesome hello world' ),
    460467                );
    461468        }
Note: See TracChangeset for help on using the changeset viewer.