Frontend.php
1 year ago
FrontendServiceProvider.php
11 months ago
LoginAfterRestore.php
1 year ago
LoginForm.php
1 year ago
LoginNotice.php
5 years ago
Frontend.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Frontend; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Rest\Rest; |
| 7 | use WPStaging\Framework\SiteInfo; |
| 8 | |
| 9 | use function WPStaging\functions\debug_log; |
| 10 | |
| 11 | /** |
| 12 | * Class Frontend |
| 13 | * @package WPStaging\Frontend |
| 14 | */ |
| 15 | class Frontend |
| 16 | { |
| 17 | /** |
| 18 | * @var object |
| 19 | */ |
| 20 | protected $settings; |
| 21 | |
| 22 | /** |
| 23 | * @var bool |
| 24 | */ |
| 25 | protected $accessDenied = false; |
| 26 | |
| 27 | /** |
| 28 | * @var LoginForm |
| 29 | */ |
| 30 | protected $loginForm; |
| 31 | |
| 32 | public function __construct() |
| 33 | { |
| 34 | $this->defineHooks(); |
| 35 | |
| 36 | $this->settings = json_decode(json_encode(get_option("wpstg_settings", []))); |
| 37 | |
| 38 | $this->loginForm = WPStaging::make(LoginForm::class); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check permissions for the page to decide whether to disable the page |
| 43 | * @return void |
| 44 | */ |
| 45 | public function checkPermissions() |
| 46 | { |
| 47 | $this->resetPermaLinks(); |
| 48 | |
| 49 | if ($this->showLoginForm()) { |
| 50 | if ($this->accessDenied) { |
| 51 | wp_logout(); |
| 52 | $this->loginForm->setError(__('Access Denied', 'wp-staging')); |
| 53 | } |
| 54 | |
| 55 | $overrides = [ |
| 56 | 'label_username' => __('Username or Email Address', 'wp-staging'), |
| 57 | ]; |
| 58 | $this->loginForm->renderForm($this->loginForm->getDefaultArguments($overrides)); |
| 59 | die(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Define Hooks |
| 65 | * @return void |
| 66 | */ |
| 67 | private function defineHooks() |
| 68 | { |
| 69 | static $isRegistered = false; |
| 70 | if ($isRegistered) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | add_action("init", [$this, "checkPermissions"]); |
| 75 | add_action("init", [$this, "resavePermalinks"]); |
| 76 | |
| 77 | $isRegistered = true; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Show a login form if user is not authorized |
| 82 | * @return bool |
| 83 | */ |
| 84 | protected function showLoginForm(): bool |
| 85 | { |
| 86 | $this->accessDenied = false; |
| 87 | |
| 88 | // Don't show login form if it is a cron job |
| 89 | if (defined('DOING_CRON') && DOING_CRON) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // Don't show login form if from wp-cli |
| 94 | if ('cli' === PHP_SAPI && defined('WP_CLI')) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // Don't show login form if showLoginForm filter is set to false. Used by Real Cookie Banner plugin |
| 99 | if (apply_filters('wpstg.frontend.showLoginForm', false)) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Don't show login form for rest requests |
| 104 | |
| 105 | /** @var Rest $rest */ |
| 106 | $rest = WPStaging::make(Rest::class); |
| 107 | if ($rest->isRestUrl()) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if ($this->isLoginPage() || is_admin()) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (!$this->isStagingSite()) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // Allow access for administrator |
| 120 | if (current_user_can('manage_options')) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | return (!isset($this->settings->disableAdminLogin) || $this->settings->disableAdminLogin !== '1'); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Check if it is a staging site |
| 129 | * @return bool |
| 130 | */ |
| 131 | protected function isStagingSite(): bool |
| 132 | { |
| 133 | return (new SiteInfo())->isStagingSite(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check if it is the login page |
| 138 | * @return bool |
| 139 | */ |
| 140 | protected function isLoginPage(): bool |
| 141 | { |
| 142 | return ($GLOBALS["pagenow"] === "wp-login.php"); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Reset permalink structure of the clone to default; index.php?p=123 |
| 147 | */ |
| 148 | protected function resetPermaLinks() |
| 149 | { |
| 150 | if (!$this->isStagingSite() || get_option("wpstg_rmpermalinks_executed") === "true") { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // $wp_rewrite is not available before the init hook. So we need to use the global variable |
| 155 | global $wp_rewrite; |
| 156 | |
| 157 | // @see https://developer.wordpress.org/reference/classes/wp_rewrite/set_permalink_structure/ |
| 158 | $wp_rewrite->set_permalink_structure(''); |
| 159 | |
| 160 | flush_rewrite_rules(); |
| 161 | |
| 162 | update_option("wpstg_rmpermalinks_executed", "true"); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return void |
| 167 | */ |
| 168 | public function resavePermalinks() |
| 169 | { |
| 170 | if (!$this->isStagingSite() || get_option("wpstg_resave_permalinks_executed") === "true") { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | try { |
| 175 | include_once(ABSPATH . 'wp-admin/includes/misc.php'); // Include `misc.php` to ensure `save_mod_rewrite_rules` is available when `flush_rules` is executed. |
| 176 | global $wp_rewrite; |
| 177 | $wp_rewrite->init(); |
| 178 | $wp_rewrite->flush_rules(true); |
| 179 | update_option("wpstg_resave_permalinks_executed", "true"); |
| 180 | } catch (\Throwable $e) { |
| 181 | debug_log('File wp-admin/includes/misc.php does not exist. Error: ' . $e->getMessage()); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 |