WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/19/2008 11:35:09 PM (13 years ago)
Author:
ryan
Message:

Move whitelist functions to plugin.php. Add settings_fields().

File:
1 edited

Legend:

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

    r9182 r9249  
    798798}
    799799
     800/* Whitelist functions */
     801
     802/**
     803 * Register a setting and its sanitization callback
     804 *
     805 * @since 2.7.0
     806 *
     807 * @param string $option_group A settings group name.  Can be anything.
     808 * @param string $option_name The name of an option to sanitize and save.
     809 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
     810 * @return unknown
     811 */
     812function register_setting($option_group, $option_name, $sanitize_callback = '') {
     813    return add_option_update_handler($option_group, $option_name, $sanitize_callback);
     814}
     815
     816/**
     817 * Unregister a setting
     818 *
     819 * @since 2.7.0
     820 *
     821 * @param unknown_type $option_group
     822 * @param unknown_type $option_name
     823 * @param unknown_type $sanitize_callback
     824 * @return unknown
     825 */
     826function unregister_setting($option_group, $option_name, $sanitize_callback = '') {
     827    return remove_option_update_handler($option_group, $option_name, $sanitize_callback);
     828}
     829
     830/**
     831 * {@internal Missing Short Description}}
     832 *
     833 * @since unknown
     834 *
     835 * @param unknown_type $option_group
     836 * @param unknown_type $option_name
     837 * @param unknown_type $sanitize_callback
     838 */
     839function add_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
     840    global $new_whitelist_options;
     841    $new_whitelist_options[ $option_group ][] = $option_name;
     842    if ( $sanitize_callback != '' )
     843        add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
     844}
     845
     846/**
     847 * {@internal Missing Short Description}}
     848 *
     849 * @since unknown
     850 *
     851 * @param unknown_type $option_group
     852 * @param unknown_type $option_name
     853 * @param unknown_type $sanitize_callback
     854 */
     855function remove_option_update_handler($option_group, $option_name, $sanitize_callback = '') {
     856    global $new_whitelist_options;
     857    $pos = array_search( $option_name, $new_whitelist_options );
     858    if ( $pos !== false )
     859        unset( $new_whitelist_options[ $option_group ][ $pos ] );
     860    if ( $sanitize_callback != '' )
     861        remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
     862}
     863
     864/**
     865 * {@internal Missing Short Description}}
     866 *
     867 * @since unknown
     868 *
     869 * @param unknown_type $options
     870 * @return unknown
     871 */
     872function option_update_filter( $options ) {
     873    global $new_whitelist_options;
     874
     875    if ( is_array( $new_whitelist_options ) )
     876        $options = add_option_whitelist( $new_whitelist_options, $options );
     877
     878    return $options;
     879}
     880add_filter( 'whitelist_options', 'option_update_filter' );
     881
     882/**
     883 * {@internal Missing Short Description}}
     884 *
     885 * @since unknown
     886 *
     887 * @param unknown_type $new_options
     888 * @param unknown_type $options
     889 * @return unknown
     890 */
     891function add_option_whitelist( $new_options, $options = '' ) {
     892    if( $options == '' ) {
     893        global $whitelist_options;
     894    } else {
     895        $whitelist_options = $options;
     896    }
     897    foreach( $new_options as $page => $keys ) {
     898        foreach( $keys as $key ) {
     899            $pos = array_search( $key, $whitelist_options[ $page ] );
     900            if( $pos === false )
     901                $whitelist_options[ $page ][] = $key;
     902        }
     903    }
     904    return $whitelist_options;
     905}
     906
     907/**
     908 * {@internal Missing Short Description}}
     909 *
     910 * @since unknown
     911 *
     912 * @param unknown_type $del_options
     913 * @param unknown_type $options
     914 * @return unknown
     915 */
     916function remove_option_whitelist( $del_options, $options = '' ) {
     917    if( $options == '' ) {
     918        global $whitelist_options;
     919    } else {
     920        $whitelist_options = $options;
     921    }
     922    foreach( $del_options as $page => $keys ) {
     923        foreach( $keys as $key ) {
     924            $pos = array_search( $key, $whitelist_options[ $page ] );
     925            if( $pos !== false )
     926                unset( $whitelist_options[ $page ][ $pos ] );
     927        }
     928    }
     929    return $whitelist_options;
     930}
     931
     932/**
     933 * Output nonce, action, and option_page fields for a settings page.
     934 *
     935 * @since 2.7.0
     936 *
     937 * @param string $option_group A settings group name.  This should match the group name used in register_setting().
     938 */
     939function settings_fields($option_group) {
     940    echo "<input type='hidden' name='option_page' value='$option_group' />";
     941    echo '<input type="hidden" name="action" value="update" />';
     942    wp_nonce_field("$option_group-options");
     943}
     944
    800945?>
Note: See TracChangeset for help on using the changeset viewer.