WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/30/2016 03:52:01 PM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.