Changeset 2238136
- Timestamp:
- 02/04/2020 08:13:17 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inn-maintenance-mode-inn/trunk/inn-maintenance-mode.php
r2129676 r2238136 7 7 // Description: The site maintenance-mode plugin | 开启站点维护模式插件,内置两种自定义功能,请参见官网说明。 8 8 // Author: Km.Van 9 // Version: 3.2.19 // Version: 4.0.0 10 10 // Author URI: https://inn-studio.com 11 11 // PHP Required: 7.3 … … 17 17 class MaintenanceMode 18 18 { 19 const TOKEN_KEY = 'innMaintenanceModeToken'; 20 19 21 const RETRY_MINUTES = 5; 20 22 … … 66 68 } 67 69 68 if ($this->isWpRest ()) {70 if ($this->isWpRestful()) { 69 71 return; 70 72 } … … 84 86 { 85 87 if (false !== \stripos($pluginFile, \basename(__DIR__))) { 86 $adminUrl = get_admin_url(); 87 $url = "{$adminUrl}?token={$this->genToken()}"; 88 $adminUrl = \get_admin_url(); 89 $tokenKey = self::TOKEN_KEY; 90 $url = "{$adminUrl}?{$tokenKey}={$this->genToken()}"; 88 91 $opts = <<<HTML 89 <a id="inn-maintenance__copy" href="{$url}" class="button button-primary" style="line-height: 1.5; height: auto;">{$this-> _('Administrator token URL')}</a>92 <a id="inn-maintenance__copy" href="{$url}" class="button button-primary" style="line-height: 1.5; height: auto;">{$this->gettext('Administrator token URL')}</a> 90 93 <script> 91 94 ;(function(){ … … 100 103 document.execCommand('copy'); 101 104 document.body.removeChild(input); 102 alert('{$this-> _('URL copied.')}');105 alert('{$this->gettext('URL copied.')}'); 103 106 } catch (e){ 104 alert('{$this-> _('Please copy URL manually.')}');107 alert('{$this->gettext('Please copy URL manually.')}'); 105 108 } 106 109 }) … … 119 122 } 120 123 121 private function isWpRest (): bool124 private function isWpRestful(): bool 122 125 { 123 126 return false !== \strpos($this->getCurrentUrl(), 'wp-json'); … … 131 134 } 132 135 133 private function _(string $text): string136 private function gettext(string $text): string 134 137 { 135 138 static $lang = null; … … 151 154 } 152 155 156 private function getAdminRoleId(): string 157 { 158 global $wpdb; 159 160 $roles = \get_option("{$wpdb->prefix}user_roles") ?: []; 161 162 if ( ! $roles) { 163 return ''; 164 } 165 166 foreach ($roles as $roleId => $role) { 167 $caps = $role['capabilities'] ?? []; 168 169 if ( ! $caps) { 170 continue; 171 } 172 173 if ((bool) ($caps['manage_options'] ?? false)) { 174 return $roleId; 175 } 176 } 177 178 return ''; 179 } 180 153 181 private function loginWithAdmin(): void 154 182 { 155 $token = (string) \filter_input(\INPUT_GET, 'innMaintenanceModeToken', \FILTER_SANITIZE_STRING);183 $token = (string) \filter_input(\INPUT_GET, self::TOKEN_KEY, \FILTER_SANITIZE_STRING); 156 184 157 185 if ( ! $token || $token !== $this->genToken()) { … … 161 189 global $wpdb; 162 190 163 $metaValue = 'a:1:{s:13:"administrator";b:1;}'; 164 $sql = <<<SQL 191 $metaValue = \serialize([$this->getAdminRoleId() => true]); 192 193 $sql = <<<SQL 165 194 SELECT `user_id` FROM `{$wpdb->prefix}usermeta` 166 195 WHERE `meta_key` = 'wp_capabilities' 167 196 AND `meta_value` = %s 197 LIMIT 0, 1 168 198 SQL; 169 199 $meta = $wpdb->get_row($wpdb->prepare( … … 176 206 } 177 207 178 \wp_set_current_user($meta->user_id); 179 \wp_set_auth_cookie($meta->user_id, true); 180 181 die($this->_('Logged as administrator.')); 208 \wp_set_current_user((int) $meta->user_id); 209 \wp_set_auth_cookie((int) $meta->user_id, true); 210 211 $adminUrl = \get_admin_url(); 212 213 echo <<<HTML 214 <a href="{$adminUrl}">✔️ {$this->gettext('Logged as administrator.')}</a> 215 HTML; 216 217 die; 182 218 } 183 219 … … 215 251 \wp_die( 216 252 \sprintf( 217 $this-> _('%1$s in maintenance, we will come back soon! <small>(Auto-refresh in %2$d minutes)</small>'),253 $this->gettext('%1$s in maintenance, we will come back soon! <small>(Auto-refresh in %2$d minutes)</small>'), 218 254 "<a href=\"{$url}\">{$name}</a>", 219 255 self::RETRY_MINUTES 220 256 ) . $this->getRetryJs(), 221 $this-> _('Maintaining...'),257 $this->gettext('Maintaining...'), 222 258 [ 223 259 'response' => 503,
Note: See TracChangeset
for help on using the changeset viewer.