Plugin Directory

Changeset 2238136


Ignore:
Timestamp:
02/04/2020 08:13:17 AM (6 years ago)
Author:
kmvan
Message:

optimize get admin id method

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inn-maintenance-mode-inn/trunk/inn-maintenance-mode.php

    r2129676 r2238136  
    77// Description: The site maintenance-mode plugin | 开启站点维护模式插件,内置两种自定义功能,请参见官网说明。
    88// Author: Km.Van
    9 // Version: 3.2.1
     9// Version: 4.0.0
    1010// Author URI: https://inn-studio.com
    1111// PHP Required: 7.3
     
    1717class MaintenanceMode
    1818{
     19    const TOKEN_KEY = 'innMaintenanceModeToken';
     20
    1921    const RETRY_MINUTES = 5;
    2022
     
    6668        }
    6769
    68         if ($this->isWpRest()) {
     70        if ($this->isWpRestful()) {
    6971            return;
    7072        }
     
    8486    {
    8587        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()}";
    8891            $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>
    9093<script>
    9194;(function(){
     
    100103            document.execCommand('copy');
    101104            document.body.removeChild(input);
    102             alert('{$this->_('URL copied.')}');
     105            alert('{$this->gettext('URL copied.')}');
    103106        } catch (e){
    104             alert('{$this->_('Please copy URL manually.')}');
     107            alert('{$this->gettext('Please copy URL manually.')}');
    105108        }
    106109    })
     
    119122    }
    120123
    121     private function isWpRest(): bool
     124    private function isWpRestful(): bool
    122125    {
    123126        return false !== \strpos($this->getCurrentUrl(), 'wp-json');
     
    131134    }
    132135
    133     private function _(string $text): string
     136    private function gettext(string $text): string
    134137    {
    135138        static $lang = null;
     
    151154    }
    152155
     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
    153181    private function loginWithAdmin(): void
    154182    {
    155         $token = (string) \filter_input(\INPUT_GET, 'innMaintenanceModeToken', \FILTER_SANITIZE_STRING);
     183        $token = (string) \filter_input(\INPUT_GET, self::TOKEN_KEY, \FILTER_SANITIZE_STRING);
    156184
    157185        if ( ! $token || $token !== $this->genToken()) {
     
    161189        global $wpdb;
    162190
    163         $metaValue = 'a:1:{s:13:"administrator";b:1;}';
    164         $sql       = <<<SQL
     191        $metaValue = \serialize([$this->getAdminRoleId() => true]);
     192
     193        $sql = <<<SQL
    165194SELECT `user_id` FROM `{$wpdb->prefix}usermeta`
    166195WHERE `meta_key` = 'wp_capabilities'
    167196AND `meta_value` = %s
     197LIMIT 0, 1
    168198SQL;
    169199        $meta = $wpdb->get_row($wpdb->prepare(
     
    176206        }
    177207
    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>
     215HTML;
     216
     217        die;
    182218    }
    183219
     
    215251        \wp_die(
    216252            \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>'),
    218254                "<a href=\"{$url}\">{$name}</a>",
    219255                self::RETRY_MINUTES
    220256            ) . $this->getRetryJs(),
    221             $this->_('Maintaining...'),
     257            $this->gettext('Maintaining...'),
    222258            [
    223259                'response' => 503,
Note: See TracChangeset for help on using the changeset viewer.