| @@ -38,11 +38,23 @@ | ||
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * Get the current user ID. |
| 42 | + * | |
| 43 | + * `get_current_user_id()` can still be 0 when this singleton is built — a | |
| 44 | + * wp-cli run before `wp_set_current_user()`, or a request whose auth has not | |
| 45 | + * been resolved yet. Caching that 0 for the rest of the request made every | |
| 46 | + * derived write bail in `can_write()`, so a connect could answer "Site | |
| 47 | + * connected successfully" and persist nothing. Re-read while it is still | |
| 48 | + * unknown; a resolved id is never overwritten. | |
| 49 | + * | |
| 42 | 50 | * @return int |
| 43 | 51 | */ |
| 44 | 52 | public function current_user_id(): int { |
| 53 | + if ( $this->current_user <= 0 ) { | |
| 54 | + $this->current_user = get_current_user_id(); | |
| 55 | + } | |
| 56 | + | |
| 45 | 57 | return $this->current_user; |
| 46 | 58 | } |
| 47 | 59 | |
| 48 | 60 | /** |
| @@ -63,13 +75,13 @@ | ||
| 63 | 75 | */ |
| 64 | 76 | public function who_am_i(): string { |
| 65 | 77 | $_who_am_i = 'local'; |
| 66 | 78 | |
| 67 | - if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) { | |
| 79 | + if( $this->is_global() > 0 && $this->is_global() === $this->current_user_id() ) { | |
| 68 | 80 | $_who_am_i = 'global'; |
| 69 | 81 | } |
| 70 | 82 | |
| 71 | - if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) { | |
| 83 | + if( $this->is_global() > 0 && $this->is_global() !== $this->current_user_id() ) { | |
| 72 | 84 | $_who_am_i = 'link'; |
| 73 | 85 | } |
| 74 | 86 | |
| 75 | 87 | if( $this->is_global() == 0 ) { |
| @@ -96,9 +108,9 @@ | ||
| 96 | 108 | * @return integer |
| 97 | 109 | */ |
| 98 | 110 | private function user_id(): int { |
| 99 | 111 | if ( $this->force_current_user ) { |
| 100 | - return $this->current_user; | |
| 112 | + return $this->current_user_id(); | |
| 101 | 113 | } |
| 102 | 114 | |
| 103 | 115 | $_who_am_i = $this->who_am_i(); |
| 104 | 116 | |
| @@ -104,17 +116,17 @@ | ||
| 104 | 116 | |
| 105 | 117 | if( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 106 | 118 | $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) ); |
| 107 | 119 | if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) { |
| 108 | - return $this->current_user; | |
| 120 | + return $this->current_user_id(); | |
| 109 | 121 | } |
| 110 | 122 | } |
| 111 | 123 | |
| 112 | 124 | if( $_who_am_i === 'link' && $this->has_api ) { |
| 113 | - return $this->current_user; | |
| 125 | + return $this->current_user_id(); | |
| 114 | 126 | } |
| 115 | 127 | |
| 116 | - return $_who_am_i === 'local' ? $this->current_user : $this->is_global(); | |
| 128 | + return $_who_am_i === 'local' ? $this->current_user_id() : $this->is_global(); | |
| 117 | 129 | } |
| 118 | 130 | |
| 119 | 131 | /** |
| 120 | 132 | * Globally logged in and the User ID of globally logged-in user. |
| @@ -144,9 +156,9 @@ | ||
| 144 | 156 | return $this->who_am_i() !== 'local'; |
| 145 | 157 | } |
| 146 | 158 | |
| 147 | 159 | public function signed_as_global(): bool { |
| 148 | - return $this->current_user === $this->is_global(); | |
| 160 | + return $this->current_user_id() === $this->is_global(); | |
| 149 | 161 | } |
| 150 | 162 | |
| 151 | 163 | /** |
| 152 | 164 | * Set optional user meta or option data |
| @@ -244,9 +256,9 @@ | ||
| 244 | 256 | * |
| 245 | 257 | * @return bool |
| 246 | 258 | */ |
| 247 | 259 | private function can_write(): bool { |
| 248 | - return $this->current_user > 0; | |
| 260 | + return $this->current_user_id() > 0; | |
| 249 | 261 | } |
| 250 | 262 | |
| 251 | 263 | private function _is_global() { |
| 252 | 264 | return apply_filters( 'templately_multisite_is_global', false ); |