PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Frontend / Frontend.php
wp-staging / Frontend Last commit date
Frontend.php 5 months ago FrontendServiceProvider.php 11 months ago LoginAfterRestore.php 8 months ago LoginForm.php 1 year ago LoginNotice.php 5 years 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 * Class Frontend
14 * @package WPStaging\Frontend
15 */
16 class Frontend
17 {
18 /** @var string */
19 const FILTER_FRONTEND_SHOW_LOGIN_FORM = 'wpstg.frontend.showLoginForm';
20
21 /**
22 * @var object
23 */
24 protected $settings;
25
26 /**
27 * @var bool
28 */
29 protected $accessDenied = false;
30
31 /**
32 * @var LoginForm
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 * Check permissions for the page to decide whether to disable the page
47 * @return void
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 * Define Hooks
69 * @return void
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 * Show a login form if user is not authorized
86 * @return bool
87 */
88 protected function showLoginForm(): bool
89 {
90 $this->accessDenied = false;
91
92 // Don't show login form if it is a cron job
93 if (defined('DOING_CRON') && DOING_CRON) {
94 return false;
95 }
96
97 // Don't show login form if from wp-cli
98 if ('cli' === PHP_SAPI && defined('WP_CLI')) {
99 return false;
100 }
101
102 // Don't show login form if showLoginForm filter is set to false. Used by Real Cookie Banner plugin
103 if (Hooks::applyFilters(self::FILTER_FRONTEND_SHOW_LOGIN_FORM, false)) {
104 return false;
105 }
106
107 // Don't show login form for rest requests
108
109 /** @var Rest $rest */
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 // Allow access for administrator
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 * Check if it is a staging site
133 * @return bool
134 */
135 protected function isStagingSite(): bool
136 {
137 return (new SiteInfo())->isStagingSite();
138 }
139
140 /**
141 * Check if it is the login page
142 * @return bool
143 */
144 protected function isLoginPage(): bool
145 {
146 return ($GLOBALS["pagenow"] === "wp-login.php");
147 }
148
149 /**
150 * Reset permalink structure of the clone to default; index.php?p=123
151 */
152 protected function resetPermaLinks()
153 {
154 if (!$this->isStagingSite() || get_option("wpstg_rmpermalinks_executed") === "true") {
155 return;
156 }
157
158 // $wp_rewrite is not available before the init hook. So we need to use the global variable
159 global $wp_rewrite;
160
161 // @see https://developer.wordpress.org/reference/classes/wp_rewrite/set_permalink_structure/
162 $wp_rewrite->set_permalink_structure('');
163
164 flush_rewrite_rules();
165
166 update_option("wpstg_rmpermalinks_executed", "true");
167 }
168
169 /**
170 * @return void
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'); // Include `misc.php` to ensure `save_mod_rewrite_rules` is available when `flush_rules` is executed.
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