Make WordPress Core

Ticket #14365: 14365.api.diff

File 14365.api.diff, 4.5 KB (added by westi, 14 years ago)

An alternative way using api additions

  • public/wp-admin/includes/plugin.php

     
    15931593 *      Default whitelisted option key names include "general," "discussion," and "reading," among others.
    15941594 * @param string $option_name The name of an option to sanitize and save.
    15951595 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
     1596 * @param string $capability The capability required to edit this option.
    15961597 * @return unknown
    15971598 */
    1598 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
    1599         global $new_whitelist_options;
     1599function register_setting( $option_group, $option_name, $sanitize_callback = '', $capability = '' ) {
     1600        global $new_whitelist_options, $option_group_caps;
    16001601
    16011602        if ( 'misc' == $option_group ) {
    16021603                _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
     
    16061607        $new_whitelist_options[ $option_group ][] = $option_name;
    16071608        if ( $sanitize_callback != '' )
    16081609                add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
     1610
     1611        // Set the capability on the group for now - in the long run this could support per option caps
     1612        if ( $capability != '' )
     1613                $option_group_caps[ $option_group ] = $capability;
    16091614}
    16101615
    16111616/**
  • public/wp-admin/options.php

     
    2424
    2525wp_reset_vars(array('action', 'option_page'));
    2626
    27 $capability = 'manage_options';
    28 
    2927if ( empty($option_page) ) // This is for back compat and will eventually be removed.
    3028        $option_page = 'options';
    31 else
    32         $capability = apply_filters( "option_page_capability_{$option_page}", $capability );
     29
     30$capability = 'manage_options';
     31if ( isset( $option_group_caps[ $option_page ] ) )
     32        $capability = $option_group_caps[ $option_page ];
    3333
    3434if ( !current_user_can( $capability ) )
    3535        wp_die(__('Cheatin’ uh?'));
  • public/wp-content/themes/twentyeleven/inc/theme-options.php

     
    4747                add_option( 'twentyeleven_theme_options', twentyeleven_get_default_theme_options() );
    4848
    4949        register_setting(
    50                 'twentyeleven_options',       // Options group, see settings_fields() call in theme_options_render_page()
    51                 'twentyeleven_theme_options', // Database option, see twentyeleven_get_theme_options()
    52                 'twentyeleven_theme_options_validate' // The sanitization callback, see twentyeleven_theme_options_validate()
     50                'twentyeleven_options',                // Options group, see settings_fields() call in theme_options_render_page()
     51                'twentyeleven_theme_options',          // Database option, see twentyeleven_get_theme_options()
     52                'twentyeleven_theme_options_validate', // The sanitization callback, see twentyeleven_theme_options_validate()
     53                'edit_theme_options'                        // The capability required to modify this option
    5354        );
    5455}
    5556add_action( 'admin_init', 'twentyeleven_theme_options_init' );
    5657
    5758/**
    58  * Change the capability required to save the 'twentyeleven_options' options group.
    59  *
    60  * @see twentyeleven_theme_options_init() First parameter to register_setting() is the name of the options group.
    61  * @see twentyeleven_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
    62  *
    63  * By default, the options groups for all registered settings require the manage_options capability.
    64  * This filter is required to change our theme options page to edit_theme_options instead.
    65  * By default, only administrators have either of these capabilities, but the desire here is
    66  * to allow for finer-grained control for roles and users.
    67  *
    68  * @param string $capability The capability used for the page, which is manage_options by default.
    69  * @return string The capability to actually use.
    70  */
    71 function twentyeleven_option_page_capability( $capability ) {
    72         return 'edit_theme_options';
    73 }
    74 add_filter( 'option_page_capability_twentyeleven_options', 'twentyeleven_option_page_capability' );
    75 
    76 /**
    7759 * Add our theme options page to the admin menu.
    7860 *
    7961 * This function is attached to the admin_menu action hook.