Make WordPress Core

Ticket #43210: 43210.diff

File 43210.diff, 3.1 KB (added by flixos90, 7 years ago)
  • src/wp-includes/capabilities.php

     
    248248
    249249                        $caps[] = $post_type->cap->publish_posts;
    250250                        break;
     251                case 'manage_option':
     252                        $caps[] = 'manage_options';
     253                        $option = $args[0];
     254
     255                        if ( has_filter( "auth_option_{$option}" ) ) {
     256                                /**
     257                                 * Filters whether the user is allowed to manage an option.
     258                                 *
     259                                 * @since 5.0.0
     260                                 *
     261                                 * @param bool   $allowed Whether the user can manage the option. Default false.
     262                                 * @param string $option  The option name.
     263                                 * @param int    $user_id User ID.
     264                                 */
     265                                $allowed = apply_filters( "auth_option_{$option}", false, $option, $user_id );
     266
     267                                if ( ! $allowed ) {
     268                                        $caps[] = $cap;
     269                                }
     270                        }
     271                        break;
    251272                case 'edit_post_meta':
    252273                case 'delete_post_meta':
    253274                case 'add_post_meta':
  • src/wp-includes/option.php

     
    20322032 *
    20332033 * @since 2.7.0
    20342034 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
     2035 * @since 5.0.0 Introduced the `$auth_callback` argument.
    20352036 *
    20362037 * @global array $new_whitelist_options
    20372038 * @global array $wp_registered_settings
     
    20462047 *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
    20472048 *     @type string   $description       A description of the data attached to this setting.
    20482049 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
     2050 *     @type callable $auth_callback     A callback function to use when performing a manage_option check.
    20492051 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    20502052 *     @type mixed    $default           Default value when calling `get_option()`.
    20512053 * }
     
    20582060                'group'             => $option_group,
    20592061                'description'       => '',
    20602062                'sanitize_callback' => null,
     2063                'auth_callback'     => null,
    20612064                'show_in_rest'      => false,
    20622065        );
    20632066
     
    21132116        if ( ! empty( $args['sanitize_callback'] ) ) {
    21142117                add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
    21152118        }
     2119        if ( ! empty( $args['auth_callback'] ) ) {
     2120                add_filter( "auth_option_{$option_name}", $args['auth_callback'], 10, 3 );
     2121        }
    21162122        if ( array_key_exists( 'default', $args ) ) {
    21172123                add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
    21182124        }
     
    21822188                        remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
    21832189                }
    21842190
     2191                // Remove the auth callback if one was set during registration.
     2192                if ( ! empty( $wp_registered_settings[ $option_name ]['auth_callback'] ) ) {
     2193                        remove_filter( "auth_option_{$option_name}", $wp_registered_settings[ $option_name ]['auth_callback'], 10 );
     2194                }
     2195
    21852196                unset( $wp_registered_settings[ $option_name ] );
    21862197        }
    21872198}