Make WordPress Core

Ticket #38176: 38176.diff

File 38176.diff, 12.4 KB (added by rmccue, 8 years ago)
  • src/wp-admin/includes/plugin.php

     
    17651765/* Whitelist functions */
    17661766
    17671767/**
    1768  * Register a setting and its data.
    1769  *
    1770  * @since 2.7.0
    1771  *
    1772  * @global array $new_whitelist_options
    1773  * @global array $wp_registered_settings
    1774  *
    1775  * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
    1776  *      Default whitelisted option key names include "general," "discussion," and "reading," among others.
    1777  * @param string $option_name The name of an option to sanitize and save.
    1778  * @param array  $args {
    1779  *     Data used to describe the setting when registered.
    1780  *
    1781  *     @type string   $type              The type of data associated with this setting.
    1782  *     @type string   $description       A description of the data attached to this setting.
    1783  *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    1784  *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    1785  * }
    1786  */
    1787 function register_setting( $option_group, $option_name, $args = array() ) {
    1788         global $new_whitelist_options, $wp_registered_settings;
    1789 
    1790         $defaults = array(
    1791                 'type'              => 'string',
    1792                 'group'             => $option_group,
    1793                 'description'       => '',
    1794                 'sanitize_callback' => null,
    1795                 'show_in_rest'      => false,
    1796         );
    1797 
    1798         // Back-compat: old sanitize callback is added.
    1799         if ( is_callable( $args ) ) {
    1800                 $args = array(
    1801                         'sanitize_callback' => $args,
    1802                 );
    1803         }
    1804 
    1805         /**
    1806          * Filters the registration arguments when registering a setting.
    1807          *
    1808          * @since 4.7.0
    1809          *
    1810          * @param array  $args         Array of setting registration arguments.
    1811          * @param array  $defaults     Array of default arguments.
    1812          * @param string $option_group Setting group.
    1813          * @param string $option_name  Setting name.
    1814          */
    1815         $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
    1816         $args = wp_parse_args( $args, $defaults );
    1817 
    1818         if ( ! is_array( $wp_registered_settings ) ) {
    1819                 $wp_registered_settings = array();
    1820         }
    1821 
    1822         if ( 'misc' == $option_group ) {
    1823                 _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
    1824                 $option_group = 'general';
    1825         }
    1826 
    1827         if ( 'privacy' == $option_group ) {
    1828                 _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
    1829                 $option_group = 'reading';
    1830         }
    1831 
    1832         $new_whitelist_options[ $option_group ][] = $option_name;
    1833         if ( ! empty( $args['sanitize_callback'] ) ) {
    1834                 add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
    1835         }
    1836 
    1837         $wp_registered_settings[ $option_name ] = $args;
    1838 }
    1839 
    1840 /**
    1841  * Unregister a setting.
    1842  *
    1843  * @since 2.7.0
    1844  * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
    1845  *
    1846  * @global array $new_whitelist_options
    1847  *
    1848  * @param string   $option_group      The settings group name used during registration.
    1849  * @param string   $option_name       The name of the option to unregister.
    1850  * @param callable $deprecated        Deprecated.
    1851  */
    1852 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
    1853         global $new_whitelist_options, $wp_registered_settings;
    1854 
    1855         if ( 'misc' == $option_group ) {
    1856                 _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
    1857                 $option_group = 'general';
    1858         }
    1859 
    1860         if ( 'privacy' == $option_group ) {
    1861                 _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
    1862                 $option_group = 'reading';
    1863         }
    1864 
    1865         $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
    1866         if ( $pos !== false ) {
    1867                 unset( $new_whitelist_options[ $option_group ][ $pos ] );
    1868         }
    1869         if ( '' !== $deprecated ) {
    1870                 _deprecated_argument( __FUNCTION__, '4.7.0', __( '$sanitize_callback is deprecated. The callback from register_setting() is used instead.' ) );
    1871                 remove_filter( "sanitize_option_{$option_name}", $deprecated );
    1872         }
    1873 
    1874         if ( isset( $wp_registered_settings[ $option_name ] ) ) {
    1875                 // Remove the sanitize callback if one was set during registration.
    1876                 if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
    1877                         remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
    1878                 }
    1879 
    1880                 unset( $wp_registered_settings[ $option_name ] );
    1881         }
    1882 }
    1883 
    1884 /**
    1885  * Retrieves an array of registered settings.
    1886  *
    1887  * @since 4.7.0
    1888  *
    1889  * @return array List of registered settings, keyed by option name.
    1890  */
    1891 function get_registered_settings() {
    1892         global $wp_registered_settings;
    1893 
    1894         if ( ! is_array( $wp_registered_settings ) ) {
    1895                 return array();
    1896         }
    1897 
    1898         return $wp_registered_settings;
    1899 }
    1900 
    1901 /**
    19021768 * Refreshes the value of the options whitelist available via the 'whitelist_options' hook.
    19031769 *
    19041770 * See the {@see 'whitelist_options'} filter.
  • src/wp-includes/option.php

     
    5656        if ( defined( 'WP_SETUP_CONFIG' ) )
    5757                return false;
    5858
     59        // Distinguish between `false` as a default, and not passing one.
     60        $passed_default = func_num_args() > 1;
     61
    5962        if ( ! wp_installing() ) {
    6063                // prevent non-existent options from triggering multiple queries
    6164                $notoptions = wp_cache_get( 'notoptions', 'options' );
     
    6770                         *
    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
    7883                $alloptions = wp_load_alloptions();
     
    97102                                        wp_cache_set( 'notoptions', $notoptions, 'options' );
    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                        }
    103108                }
     
    16861691        }
    16871692        return $result;
    16881693}
     1694
     1695/**
     1696 * Register a setting and its data.
     1697 *
     1698 * @since 2.7.0
     1699 *
     1700 * @global array $new_whitelist_options
     1701 * @global array $wp_registered_settings
     1702 *
     1703 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
     1704 *      Default whitelisted option key names include "general," "discussion," and "reading," among others.
     1705 * @param string $option_name The name of an option to sanitize and save.
     1706 * @param array  $args {
     1707 *     Data used to describe the setting when registered.
     1708 *
     1709 *     @type string   $type              The type of data associated with this setting.
     1710 *     @type string   $description       A description of the data attached to this setting.
     1711 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
     1712 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
     1713 *     @type mixed    $default           Default value when calling `get_option()`.
     1714 * }
     1715 */
     1716function register_setting( $option_group, $option_name, $args = array() ) {
     1717        global $new_whitelist_options, $wp_registered_settings;
     1718
     1719        $defaults = array(
     1720                'type'              => 'string',
     1721                'group'             => $option_group,
     1722                'description'       => '',
     1723                'sanitize_callback' => null,
     1724                'show_in_rest'      => false,
     1725        );
     1726
     1727        // Back-compat: old sanitize callback is added.
     1728        if ( is_callable( $args ) ) {
     1729                $args = array(
     1730                        'sanitize_callback' => $args,
     1731                );
     1732        }
     1733
     1734        /**
     1735         * Filters the registration arguments when registering a setting.
     1736         *
     1737         * @since 4.7.0
     1738         *
     1739         * @param array  $args         Array of setting registration arguments.
     1740         * @param array  $defaults     Array of default arguments.
     1741         * @param string $option_group Setting group.
     1742         * @param string $option_name  Setting name.
     1743         */
     1744        $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
     1745        $args = wp_parse_args( $args, $defaults );
     1746
     1747        if ( ! is_array( $wp_registered_settings ) ) {
     1748                $wp_registered_settings = array();
     1749        }
     1750
     1751        if ( 'misc' == $option_group ) {
     1752                _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
     1753                $option_group = 'general';
     1754        }
     1755
     1756        if ( 'privacy' == $option_group ) {
     1757                _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
     1758                $option_group = 'reading';
     1759        }
     1760
     1761        $new_whitelist_options[ $option_group ][] = $option_name;
     1762        if ( ! empty( $args['sanitize_callback'] ) ) {
     1763                add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
     1764        }
     1765        if ( array_key_exists( 'default', $args ) ) {
     1766                add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
     1767        }
     1768
     1769        $wp_registered_settings[ $option_name ] = $args;
     1770}
     1771
     1772/**
     1773 * Unregister a setting.
     1774 *
     1775 * @since 2.7.0
     1776 * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
     1777 *
     1778 * @global array $new_whitelist_options
     1779 *
     1780 * @param string   $option_group      The settings group name used during registration.
     1781 * @param string   $option_name       The name of the option to unregister.
     1782 * @param callable $deprecated        Deprecated.
     1783 */
     1784function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
     1785        global $new_whitelist_options, $wp_registered_settings;
     1786
     1787        if ( 'misc' == $option_group ) {
     1788                _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
     1789                $option_group = 'general';
     1790        }
     1791
     1792        if ( 'privacy' == $option_group ) {
     1793                _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
     1794                $option_group = 'reading';
     1795        }
     1796
     1797        $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
     1798        if ( $pos !== false ) {
     1799                unset( $new_whitelist_options[ $option_group ][ $pos ] );
     1800        }
     1801        if ( '' !== $deprecated ) {
     1802                _deprecated_argument( __FUNCTION__, '4.7.0', __( '$sanitize_callback is deprecated. The callback from register_setting() is used instead.' ) );
     1803                remove_filter( "sanitize_option_{$option_name}", $deprecated );
     1804        }
     1805
     1806        if ( isset( $wp_registered_settings[ $option_name ] ) ) {
     1807                // Remove the sanitize callback if one was set during registration.
     1808                if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
     1809                        remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
     1810                }
     1811
     1812                unset( $wp_registered_settings[ $option_name ] );
     1813        }
     1814}
     1815
     1816/**
     1817 * Retrieves an array of registered settings.
     1818 *
     1819 * @since 4.7.0
     1820 *
     1821 * @return array List of registered settings, keyed by option name.
     1822 */
     1823function get_registered_settings() {
     1824        global $wp_registered_settings;
     1825
     1826        if ( ! is_array( $wp_registered_settings ) ) {
     1827                return array();
     1828        }
     1829
     1830        return $wp_registered_settings;
     1831}
     1832
     1833/**
     1834 * Filter the default value for the option.
     1835 *
     1836 * For settings which register a default setting in `register_setting()`, this
     1837 * function is added as a filter to `default_option_{$option}`.
     1838 *
     1839 * @param mixed $default Existing default value to return.
     1840 * @param string $option Option name.
     1841 * @param bool $passed_default Was `get_option()` passed a default value?
     1842 * @return mixed Filtered default value.
     1843 */
     1844function filter_default_option( $default, $option, $passed_default ) {
     1845        if ( $passed_default ) {
     1846                return $default;
     1847        }
     1848
     1849        $registered = get_registered_settings();
     1850        if ( empty( $registered[ $option ] ) ) {
     1851                return $default;
     1852        }
     1853
     1854        return $registered[ $option ]['default'];
     1855}