Make WordPress Core

Ticket #58001: 58001-lazyload-user-capabilities.diff

File 58001-lazyload-user-capabilities.diff, 1.8 KB (added by sachinrajcp123, 12 months ago)
Line 
1Index: src/wp-includes/class-wp-user.php
2===================================================================
3--- src/wp-includes/class-wp-user.php (revision 60000)
4+++ src/wp-includes/class-wp-user.php (working copy)
5@@
6 public function for_site( $site_id = '' ) {
7- $this->caps = array();
8- $this->cap_key = '';
9-
10- $this->get_role_caps();
11+ $this->caps = array();
12+ $this->cap_key = '';
13+
14+ // Delay loading role caps until actually needed.
15+ $this->caps_lazy_loaded = false;
16+ }
17+
18+ /**
19+ * Lazily load capability data for the user.
20+ *
21+ * @since 6.9.0
22+ */
23+ protected function lazy_load_caps() {
24+ if ( ! empty( $this->caps_lazy_loaded ) ) {
25+ return;
26+ }
27+ $this->get_role_caps();
28+ $this->caps_lazy_loaded = true;
29 }
30
31 /**
32 * Sets the site to operate on. Defaults to the current site.
33 *
34 * @since 3.0.0
35 *
36 * @param int $site_id Site ID to operate on. Defaults to the current site.
37 */
38 public function set_site_id( $site_id = '' ) {
39 $this->site_id = $site_id ? absint( $site_id ) : get_current_blog_id();
40 }
41
42 /**
43 * Returns whether the current user has the specified capability.
44 *
45 * @since 2.0.0
46 *
47 * @param string $cap Capability name.
48 * @param mixed ...$args Optional further parameters, typically starting with an object ID.
49 * @return bool Whether the current user has the given capability.
50 */
51 public function has_cap( $cap, ...$args ) {
52+ // Ensure capabilities are loaded before checking.
53+ if ( empty( $this->caps_lazy_loaded ) ) {
54+ $this->lazy_load_caps();
55+ }
56 $args = array_merge( array( $cap ), $args );
57 $caps = call_user_func_array( 'map_meta_cap', array_merge( array( $cap, $this->ID ), $args ) );
58
59 // Must have ALL requested caps.
60 foreach ( $caps as $capability ) {
61 if ( empty( $this->allcaps[ $capability ] ) ) {
62 return false;
63 }
64 }
65
66 return true;
67 }