PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.1.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.1.1
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 1 year ago FrontendServiceProvider.php 2 years ago LoginAfterRestore.php 1 year ago LoginForm.php 1 year ago LoginNotice.php 5 years ago
Frontend.php
180 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 from wp-cli
89 if ('cli' === PHP_SAPI && defined('WP_CLI')) {
90 return false;
91 }
92
93 // Don't show login form if showLoginForm filter is set to false. Used by Real Cookie Banner plugin
94 if (apply_filters('wpstg.frontend.showLoginForm', false)) {
95 return false;
96 }
97
98 // Don't show login form for rest requests
99
100 /** @var Rest $rest */
101 $rest = WPStaging::make(Rest::class);
102 if ($rest->isRestUrl()) {
103 return false;
104 }
105
106 if ($this->isLoginPage() || is_admin()) {
107 return false;
108 }
109
110 if (!$this->isStagingSite()) {
111 return false;
112 }
113
114 // Allow access for administrator
115 if (current_user_can('manage_options')) {
116 return false;
117 }
118
119 return (!isset($this->settings->disableAdminLogin) || $this->settings->disableAdminLogin !== '1');
120 }
121
122 /**
123 * Check if it is a staging site
124 * @return bool
125 */
126 protected function isStagingSite(): bool
127 {
128 return (new SiteInfo())->isStagingSite();
129 }
130
131 /**
132 * Check if it is the login page
133 * @return bool
134 */
135 protected function isLoginPage(): bool
136 {
137 return ($GLOBALS["pagenow"] === "wp-login.php");
138 }
139
140 /**
141 * Reset permalink structure of the clone to default; index.php?p=123
142 */
143 protected function resetPermaLinks()
144 {
145 if (!$this->isStagingSite() || get_option("wpstg_rmpermalinks_executed") === "true") {
146 return;
147 }
148
149 // $wp_rewrite is not available before the init hook. So we need to use the global variable
150 global $wp_rewrite;
151
152 // @see https://developer.wordpress.org/reference/classes/wp_rewrite/set_permalink_structure/
153 $wp_rewrite->set_permalink_structure('');
154
155 flush_rewrite_rules();
156
157 update_option("wpstg_rmpermalinks_executed", "true");
158 }
159
160 /**
161 * @return void
162 */
163 public function resavePermalinks()
164 {
165 if (!$this->isStagingSite() || get_option("wpstg_resave_permalinks_executed") === "true") {
166 return;
167 }
168
169 try {
170 include_once(ABSPATH . 'wp-admin/includes/misc.php'); // Include `misc.php` to ensure `save_mod_rewrite_rules` is available when `flush_rules` is executed.
171 global $wp_rewrite;
172 $wp_rewrite->init();
173 $wp_rewrite->flush_rules(true);
174 update_option("wpstg_resave_permalinks_executed", "true");
175 } catch (\Throwable $e) {
176 debug_log('File wp-admin/includes/misc.php does not exist. Error: ' . $e->getMessage());
177 }
178 }
179 }
180