Make WordPress Core

Changeset 38910


Ignore:
Timestamp:
10/25/2016 05:07:20 PM (7 years ago)
Author:
joehoyle
Message:

Options: Add 'default' to register_setting

Add a default argument to register_setting that will be used an the default option value viet get_option() in the event of no other option being specified. This means (if chosen) developers can define their default once via register_option and not have to duplicate the value every time they make a call to get_option().

Props rmccue, jorbin, jtsternberg.
Fixes #38176.

Location:
trunk
Files:
2 edited

Legend:

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

    r38832 r38910  
    5656    if ( defined( 'WP_SETUP_CONFIG' ) )
    5757        return false;
     58
     59    // Distinguish between `false` as a default, and not passing one.
     60    $passed_default = func_num_args() > 1;
    5861
    5962    if ( ! wp_installing() ) {
     
    6871             * @since 3.4.0
    6972             * @since 4.4.0 The `$option` parameter was added.
     73             * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
    7074             *
    7175             * @param mixed  $default The default value to return if the option does not exist
    7276             *                        in the database.
    7377             * @param string $option  Option name.
     78             * @param bool   $passed_default Was `get_option()` passed a default value?
    7479             */
    75             return apply_filters( "default_option_{$option}", $default, $option );
     80            return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
    7681        }
    7782
     
    98103
    99104                    /** This filter is documented in wp-includes/option.php */
    100                     return apply_filters( 'default_option_' . $option, $default, $option );
     105                    return apply_filters( 'default_option_' . $option, $default, $option, $passed_default );
    101106                }
    102107            }
     
    110115        } else {
    111116            /** This filter is documented in wp-includes/option.php */
    112             return apply_filters( 'default_option_' . $option, $default, $option );
     117            return apply_filters( 'default_option_' . $option, $default, $option, $passed_default );
    113118        }
    114119    }
     
    18361841 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    18371842 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
     1843 *     @type mixed    $default           Default value when calling `get_option()`.
    18381844 * }
    18391845 */
     
    18871893        add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
    18881894    }
     1895    if ( array_key_exists( 'default', $args ) ) {
     1896        add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
     1897    }
    18891898
    18901899    $wp_registered_settings[ $option_name ] = $args;
     
    19511960    return $wp_registered_settings;
    19521961}
     1962
     1963/**
     1964 * Filter the default value for the option.
     1965 *
     1966 * For settings which register a default setting in `register_setting()`, this
     1967 * function is added as a filter to `default_option_{$option}`.
     1968 *
     1969 * @since 4.7.0
     1970 *
     1971 * @param mixed $default Existing default value to return.
     1972 * @param string $option Option name.
     1973 * @param bool $passed_default Was `get_option()` passed a default value?
     1974 * @return mixed Filtered default value.
     1975 */
     1976function filter_default_option( $default, $option, $passed_default ) {
     1977    if ( $passed_default ) {
     1978        return $default;
     1979    }
     1980
     1981    $registered = get_registered_settings();
     1982    if ( empty( $registered[ $option ] ) ) {
     1983        return $default;
     1984    }
     1985
     1986    return $registered[ $option ]['default'];
     1987}
  • trunk/tests/phpunit/tests/option/registration.php

    r38690 r38910  
    4141
    4242    /**
     43     * @ticket 38176
     44     */
     45    public function test_register_with_default() {
     46        register_setting( 'test_group', 'test_default', array(
     47            'default' => 'Fuck Cancer'
     48        ));
     49
     50        $this->assertEquals( 'Fuck Cancer', get_option( 'test_default' ) );
     51    }
     52
     53    /**
     54     * @ticket 38176
     55     */
     56    public function test_register_with_default_override() {
     57        register_setting( 'test_group', 'test_default', array(
     58            'default' => 'Fuck Cancer'
     59        ));
     60
     61        $this->assertEquals( 'Fuck Leukemia', get_option( 'test_default', 'Fuck Leukemia' ) );
     62    }
     63
     64    /**
    4365     * @expectedDeprecated register_setting
    4466     */
Note: See TracChangeset for help on using the changeset viewer.