WordPress.org

Make WordPress Core

Ticket #21120: map_meta_cap.patch

File map_meta_cap.patch, 1.3 KB (added by kurtpayne, 6 years ago)

Memoizing map_meta_cap

  • wp-includes/capabilities.php

     
    933933 *
    934934 * @param string $cap Capability name.
    935935 * @param int $user_id User ID.
     936 * @param bool $cache Cache the results
    936937 * @return array Actual capabilities for meta capability.
    937938 */
    938 function map_meta_cap( $cap, $user_id ) {
     939function map_meta_cap( $cap, $user_id, $cache = true ) {
     940        static $_cache = array();
     941        if ( $cache && isset( $_cache[ $cap.$user_id ] ) )
     942                return $_cache[ $cap.$user_id ];
     943
    939944        $args = array_slice( func_get_args(), 2 );
    940945        $caps = array();
    941946
     
    11941199                $post_type_meta_caps = _post_type_meta_capabilities();
    11951200                if ( isset( $post_type_meta_caps[ $cap ] ) ) {
    11961201                        $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
    1197                         return call_user_func_array( 'map_meta_cap', $args );
     1202                        $_cache[ $cap.$user_id ] = call_user_func_array( 'map_meta_cap', $args );
     1203                        return $_cache[ $cap.$user_id ];
    11981204                }
    11991205
    12001206                // If no meta caps match, return the original cap.
    12011207                $caps[] = $cap;
    12021208        }
    12031209
    1204         return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
     1210        $_cache[ $cap.$user_id ] = apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
     1211        return $_cache[ $cap.$user_id ];
    12051212}
    12061213
    12071214/**