Frontend.php
1 day ago
FrontendServiceProvider.php
1 day ago
LoginAfterRestore.php
1 day ago
LoginForm.php
1 day ago
LoginNotice.php
1 day ago
Frontend.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Frontend; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Facades\Hooks; |
| 7 | use WPStaging\Framework\Rest\Rest; |
| 8 | use WPStaging\Framework\SiteInfo; |
| 9 | |
| 10 | use function WPStaging\functions\debug_log; |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | class Frontend |
| 17 | { |
| 18 | |
| 19 | const FILTER_FRONTEND_SHOW_LOGIN_FORM = 'wpstg.frontend.showLoginForm'; |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | protected $settings; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | protected $accessDenied = false; |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | protected $loginForm; |
| 35 | |
| 36 | public function __construct() |
| 37 | { |
| 38 | $this->defineHooks(); |
| 39 | |
| 40 | $this->settings = json_decode(json_encode(get_option("wpstg_settings", []))); |
| 41 | |
| 42 | $this->loginForm = WPStaging::make(LoginForm::class); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | public function checkPermissions() |
| 50 | { |
| 51 | $this->resetPermaLinks(); |
| 52 | |
| 53 | if ($this->showLoginForm()) { |
| 54 | if ($this->accessDenied) { |
| 55 | wp_logout(); |
| 56 | $this->loginForm->setError(__('Access Denied', 'wp-staging')); |
| 57 | } |
| 58 | |
| 59 | $overrides = [ |
| 60 | 'label_username' => __('Username or Email Address', 'wp-staging'), |
| 61 | ]; |
| 62 | $this->loginForm->renderForm($this->loginForm->getDefaultArguments($overrides)); |
| 63 | die(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | private function defineHooks() |
| 72 | { |
| 73 | static $isRegistered = false; |
| 74 | if ($isRegistered) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | add_action("init", [$this, "checkPermissions"]); |
| 79 | add_action("init", [$this, "resavePermalinks"]); |
| 80 | |
| 81 | $isRegistered = true; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | protected function showLoginForm(): bool |
| 89 | { |
| 90 | $this->accessDenied = false; |
| 91 | |
| 92 | |
| 93 | if (defined('DOING_CRON') && DOING_CRON) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | if ('cli' === PHP_SAPI && defined('WP_CLI')) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | if (Hooks::applyFilters(self::FILTER_FRONTEND_SHOW_LOGIN_FORM, false)) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | $rest = WPStaging::make(Rest::class); |
| 111 | if ($rest->isRestUrl()) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if ($this->isLoginPage() || is_admin()) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | if (!$this->isStagingSite()) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | if (current_user_can('manage_options')) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return (!isset($this->settings->disableAdminLogin) || $this->settings->disableAdminLogin !== '1'); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | protected function isStagingSite(): bool |
| 136 | { |
| 137 | return (new SiteInfo())->isStagingSite(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | protected function isLoginPage(): bool |
| 145 | { |
| 146 | return ($GLOBALS["pagenow"] === "wp-login.php"); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | protected function resetPermaLinks() |
| 153 | { |
| 154 | if (!$this->isStagingSite() || get_option("wpstg_rmpermalinks_executed") === "true") { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | global $wp_rewrite; |
| 160 | |
| 161 | |
| 162 | $wp_rewrite->set_permalink_structure(''); |
| 163 | |
| 164 | flush_rewrite_rules(); |
| 165 | |
| 166 | update_option("wpstg_rmpermalinks_executed", "true"); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | public function resavePermalinks() |
| 173 | { |
| 174 | if (!$this->isStagingSite() || get_option("wpstg_resave_permalinks_executed") === "true") { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | try { |
| 179 | include_once(ABSPATH . 'wp-admin/includes/misc.php'); |
| 180 | global $wp_rewrite; |
| 181 | $wp_rewrite->init(); |
| 182 | $wp_rewrite->flush_rules(true); |
| 183 | update_option("wpstg_resave_permalinks_executed", "true"); |
| 184 | } catch (\Throwable $e) { |
| 185 | debug_log('File wp-admin/includes/misc.php does not exist. Error: ' . $e->getMessage()); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 |