WordPress.org

Make WordPress Core

Changeset 38687


Ignore:
Timestamp:
09/30/2016 03:52:01 PM (4 years ago)
Author:
rmccue
Message:

Options: Move register_setting() from wp-admin to wp-includes.

With [38635], register_setting is now a more generic setting registration function and is usable outside of the admin.

See #37885.

Location:
trunk/src
Files:
2 edited

Legend:

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

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

    r38636 r38687  
    16871687    return $result;
    16881688}
     1689
     1690/**
     1691 * Register a setting and its data.
     1692 *
     1693 * @since 2.7.0
     1694 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
     1695 *
     1696 * @global array $new_whitelist_options
     1697 * @global array $wp_registered_settings
     1698 *
     1699 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
     1700 *  Default whitelisted option key names include "general," "discussion," and "reading," among others.
     1701 * @param string $option_name The name of an option to sanitize and save.
     1702 * @param array  $args {
     1703 *     Data used to describe the setting when registered.
     1704 *
     1705 *     @type string   $type              The type of data associated with this setting.
     1706 *     @type string   $description       A description of the data attached to this setting.
     1707 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
     1708 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
     1709 * }
     1710 */
     1711function register_setting( $option_group, $option_name, $args = array() ) {
     1712    global $new_whitelist_options, $wp_registered_settings;
     1713
     1714    $defaults = array(
     1715        'type'              => 'string',
     1716        'group'             => $option_group,
     1717        'description'       => '',
     1718        'sanitize_callback' => null,
     1719        'show_in_rest'      => false,
     1720    );
     1721
     1722    // Back-compat: old sanitize callback is added.
     1723    if ( is_callable( $args ) ) {
     1724        $args = array(
     1725            'sanitize_callback' => $args,
     1726        );
     1727    }
     1728
     1729    /**
     1730     * Filters the registration arguments when registering a setting.
     1731     *
     1732     * @since 4.7.0
     1733     *
     1734     * @param array  $args         Array of setting registration arguments.
     1735     * @param array  $defaults     Array of default arguments.
     1736     * @param string $option_group Setting group.
     1737     * @param string $option_name  Setting name.
     1738     */
     1739    $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
     1740    $args = wp_parse_args( $args, $defaults );
     1741
     1742    if ( ! is_array( $wp_registered_settings ) ) {
     1743        $wp_registered_settings = array();
     1744    }
     1745
     1746    if ( 'misc' == $option_group ) {
     1747        _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
     1748        $option_group = 'general';
     1749    }
     1750
     1751    if ( 'privacy' == $option_group ) {
     1752        _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
     1753        $option_group = 'reading';
     1754    }
     1755
     1756    $new_whitelist_options[ $option_group ][] = $option_name;
     1757    if ( ! empty( $args['sanitize_callback'] ) ) {
     1758        add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
     1759    }
     1760
     1761    $wp_registered_settings[ $option_name ] = $args;
     1762}
     1763
     1764/**
     1765 * Unregister a setting.
     1766 *
     1767 * @since 2.7.0
     1768 * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
     1769 *
     1770 * @global array $new_whitelist_options
     1771 *
     1772 * @param string   $option_group      The settings group name used during registration.
     1773 * @param string   $option_name       The name of the option to unregister.
     1774 * @param callable $deprecated        Deprecated.
     1775 */
     1776function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
     1777    global $new_whitelist_options, $wp_registered_settings;
     1778
     1779    if ( 'misc' == $option_group ) {
     1780        _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
     1781        $option_group = 'general';
     1782    }
     1783
     1784    if ( 'privacy' == $option_group ) {
     1785        _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
     1786        $option_group = 'reading';
     1787    }
     1788
     1789    $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
     1790    if ( $pos !== false ) {
     1791        unset( $new_whitelist_options[ $option_group ][ $pos ] );
     1792    }
     1793    if ( '' !== $deprecated ) {
     1794        _deprecated_argument( __FUNCTION__, '4.7.0', __( '$sanitize_callback is deprecated. The callback from register_setting() is used instead.' ) );
     1795        remove_filter( "sanitize_option_{$option_name}", $deprecated );
     1796    }
     1797
     1798    if ( isset( $wp_registered_settings[ $option_name ] ) ) {
     1799        // Remove the sanitize callback if one was set during registration.
     1800        if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
     1801            remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
     1802        }
     1803
     1804        unset( $wp_registered_settings[ $option_name ] );
     1805    }
     1806}
     1807
     1808/**
     1809 * Retrieves an array of registered settings.
     1810 *
     1811 * @since 4.7.0
     1812 *
     1813 * @return array List of registered settings, keyed by option name.
     1814 */
     1815function get_registered_settings() {
     1816    global $wp_registered_settings;
     1817
     1818    if ( ! is_array( $wp_registered_settings ) ) {
     1819        return array();
     1820    }
     1821
     1822    return $wp_registered_settings;
     1823}
Note: See TracChangeset for help on using the changeset viewer.