WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 2 years ago

#37281 new enhancement

Allow non-error notifications to be set for Customizer settings from PHP

Reported by: westonruter Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Customize Keywords: needs-patch
Focuses: Cc:

Description (last modified by westonruter)

An API adding notifications to Customizer settings was added in #34893. In JS a notification can be added of type error, warning, info, and custom. In PHP, however, only error notifications can be added to a setting, and this is done by returning WP_Error from a validation routing (or sanitization routine). It may be useful to allow other kinds of notifications to be added via PHP as well. This would likely require adding a WP_Customize_Setting::$notifications collection with a WP_Customize_Setting::add_notification() and WP_Customize_Setting::remove_notification() to provide a similar API in PHP for the general notifications API in JS. It seems clear that non-error notifications shouldn't be added by returning WP_Error instances from validation routines, but that the notifications could be added inside of such routines, for example:

<?php
class My_Setting extends WP_Customize_Setting {
    function validate( $value ) {
        $validity = parent::validate( $value );
        if ( strlen( $value ) > 20 ) {
            $this->add_notification( array(
                'type' => 'warning',
                'code' => 'long_value',
                'message' => __( 'This is a long value!' ),
            ) );
        }
    }
}

The non-error notifications could be sent back as part of full refresh and selective refresh responses in a similar way that error notifications are sent back and updated into the JS models.

Change History (3)

#1 @westonruter
5 years ago

  • Description modified (diff)

This ticket was mentioned in Slack in #core-customize by celloexpressions. View the logs.


5 years ago

#3 @celloexpressions
5 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

There are a lot of valid use cases for this; it would be nice to have in 4.7 but needs a patch and is lower priority than our main projects.

Note: See TracTickets for help on using tickets.