prefix . 'user_roles', $wpdb->get_blog_prefix() . 'user_roles', 'cron', 'site_admins', 'registration', 'add_new_users', ); } /** * Keep the stored value of a protected option. * * @param mixed $value New value. * @param string $option Option name. * @param mixed $old_value Stored value. * @return mixed */ public static function protect_option( $value, $option, $old_value ) { if ( in_array( (string) $option, self::protected_options(), true ) ) { return $old_value; } return $value; } /** * Keep the stored value of a protected network option. * * @param mixed $value New value. * @param mixed $old_value Stored value. * @param string $option Option name. * @return mixed */ public static function protect_site_option( $value, $old_value, $option = '' ) { return self::protect_option( $value, $option, $old_value ); } /** * Turn the guard on for the rest of this request. */ public static function apply(): void { if ( self::$on ) { return; } self::$on = true; add_filter( 'user_has_cap', array( __CLASS__, 'filter' ), PHP_INT_MAX, 4 ); add_filter( 'map_meta_cap', array( __CLASS__, 'map' ), PHP_INT_MAX, 2 ); add_filter( 'pre_update_option', array( __CLASS__, 'protect_option' ), PHP_INT_MAX, 3 ); if ( is_multisite() ) { foreach ( self::protected_options() as $option ) { add_filter( 'pre_update_site_option_' . $option, array( __CLASS__, 'protect_site_option' ), PHP_INT_MAX, 3 ); } } // Without unfiltered_html, content saved during this request goes through kses. if ( function_exists( 'kses_init_filters' ) ) { kses_init_filters(); } } /** * Whether the guard is on. */ public static function active(): bool { return self::$on; } /** * Remove administrative capabilities from the user. * * @param array $allcaps All capabilities of the user. * @param string[] $caps Required primitive capabilities. * @param array $args Requested capability and arguments. * @param \WP_User|null $user User. * @return array */ public static function filter( $allcaps, $caps = array(), $args = array(), $user = null ): array { foreach ( self::denied() as $cap ) { $allcaps[ $cap ] = false; } return (array) $allcaps; } /** * Refuse meta capabilities that map to administrative work, including for * super admins, whose capabilities are not read from their role. * * @param string[] $caps Primitive capabilities. * @param string $cap Requested capability. * @return string[] */ public static function map( $caps, $cap ): array { $denied = self::denied(); if ( in_array( $cap, $denied, true ) || array_intersect( (array) $caps, $denied ) ) { return array( 'do_not_allow' ); } return (array) $caps; } }