From e3df1300892ecbbe9a1645d7ce63d1267569bfc8 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 5 Jul 2020 15:05:02 +0000 Subject: [PATCH] Options, Meta APIs: Introduce `register_setting` and `unregister_setting` actions. Props Howdy_McGee, Pionect. Fixes #37245. git-svn-id: https://develop.svn.wordpress.org/trunk@48321 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/option.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index 459b355362..be81bd25ad 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -2212,6 +2212,7 @@ function register_setting( $option_group, $option_name, $args = array() ) { } $new_whitelist_options[ $option_group ][] = $option_name; + if ( ! empty( $args['sanitize_callback'] ) ) { add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] ); } @@ -2219,6 +2220,17 @@ function register_setting( $option_group, $option_name, $args = array() ) { add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 ); } + /** + * Fires immediately before the setting is registered but after its filters are in place. + * + * @since 5.5.0 + * + * @param string $option_group Setting group. + * @param string $option_name Setting name. + * @param array $args Array of setting registration arguments. + */ + do_action( 'register_setting', $option_group, $option_name, $args ); + $wp_registered_settings[ $option_name ] = $args; } @@ -2231,9 +2243,9 @@ function register_setting( $option_group, $option_name, $args = array() ) { * @global array $new_whitelist_options * @global array $wp_registered_settings * - * @param string $option_group The settings group name used during registration. - * @param string $option_name The name of the option to unregister. - * @param callable $deprecated Deprecated. + * @param string $option_group The settings group name used during registration. + * @param string $option_name The name of the option to unregister. + * @param callable $deprecated Deprecated. */ function unregister_setting( $option_group, $option_name, $deprecated = '' ) { global $new_whitelist_options, $wp_registered_settings; @@ -2265,9 +2277,11 @@ function unregister_setting( $option_group, $option_name, $deprecated = '' ) { } $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ], true ); + if ( false !== $pos ) { unset( $new_whitelist_options[ $option_group ][ $pos ] ); } + if ( '' !== $deprecated ) { _deprecated_argument( __FUNCTION__, @@ -2293,6 +2307,16 @@ function unregister_setting( $option_group, $option_name, $deprecated = '' ) { remove_filter( "default_option_{$option_name}", 'filter_default_option', 10 ); } + /** + * Fires immediately before the setting is unregistered and after its filters have been removed. + * + * @since 5.5.0 + * + * @param string $option_group Setting group. + * @param string $option_name Setting name. + */ + do_action( 'unregister_setting', $option_group, $option_name ); + unset( $wp_registered_settings[ $option_name ] ); } }