PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.0.2
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.0.2
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 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
views 3 years ago Frontend.php 3 years ago FrontendServiceProvider.php 3 years ago LoginAfterRestore.php 3 years ago LoginForm.php 3 years ago LoginNotice.php 5 years ago
Frontend.php
219 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 /**
10 * Class Frontend
11 * @package WPStaging\Frontend
12 */
13 class Frontend
14 {
15
16 /**
17 * @var object
18 */
19 private $settings;
20
21 /**
22 * @var bool
23 */
24 private $accessDenied = false;
25
26 public function __construct()
27 {
28 $this->defineHooks();
29
30 $this->settings = json_decode(json_encode(get_option("wpstg_settings", [])));
31 }
32
33 /**
34 * Define Hooks
35 */
36 private function defineHooks()
37 {
38 add_action("init", [$this, "checkPermissions"]);
39 add_filter("wp_before_admin_bar_render", [$this, "changeSiteName"]);
40 }
41
42 /**
43 * Change admin_bar site_name
44 *
45 * @return void
46 * @global object $wp_admin_bar
47 */
48 public function changeSiteName()
49 {
50 global $wp_admin_bar;
51 $siteTitle = apply_filters('wpstg_staging_site_title', 'STAGING');
52 if ($this->isStagingSite()) {
53 // Main Title
54 $wp_admin_bar->add_menu(
55 [
56 'id' => 'site-name',
57 'title' => is_admin() ? ($siteTitle . ' - ' . get_bloginfo('name')) : ($siteTitle . ' - ' . get_bloginfo('name') . ' Dashboard'),
58 'href' => is_admin() ? home_url('/') : admin_url(),
59 ]
60 );
61 }
62 }
63
64 /**
65 * Check permissions for the page to decide whether or not to disable the page
66 */
67 public function checkPermissions()
68 {
69 $this->resetPermaLinks();
70
71 if ($this->showLoginForm()) {
72 $login = new LoginForm();
73 if ($this->accessDenied) {
74 wp_logout();
75 $login->setError(__('Access Denied', 'wp-staging'));
76 }
77 $overrides = [
78 'label_username' => __('Username or Email Address', 'wp-staging'),
79 ];
80 $login->renderForm($login->getDefaultArguments($overrides));
81 die();
82 }
83 }
84
85
86 /**
87 * Show a login form if user is not authorized
88 * @return bool
89 */
90 private function showLoginForm()
91 {
92 $this->accessDenied = false;
93
94 // Don't show login form if from wp-cli
95 if ('cli' === PHP_SAPI && defined('WP_CLI')) {
96 return false;
97 }
98
99 // Don't show login form if showLoginForm filter is set to false. Used by Real Cookie Banner plugin
100 if (apply_filters('wpstg.frontend.showLoginForm', false)) {
101 return false;
102 }
103
104 // Don't show login form for rest requests
105
106 /** @var Rest */
107 $rest = WPStaging::make(Rest::class);
108 if ($rest->isRestUrl()) {
109 return false;
110 }
111
112 if ($this->isLoginPage() || is_admin()) {
113 return false;
114 }
115
116 if (! $this->isStagingSite()) {
117 return false;
118 }
119
120 // Allow access for administrator
121 if (current_user_can('manage_options')) {
122 return false;
123 }
124
125 // Simple check (free version only)
126 if (!defined('WPSTGPRO_VERSION')) {
127 return (!isset($this->settings->disableAdminLogin) || $this->settings->disableAdminLogin !== '1');
128 }
129
130 // Allow access for wp staging user role "all"
131 if (!empty($this->settings->userRoles) && in_array('all', $this->settings->userRoles)) {
132 return false;
133 }
134
135 if (!is_user_logged_in()) {
136 return true;
137 }
138
139 // Allow access for administrators if no user roles are defined
140 if (!isset($this->settings->userRoles) || !is_array($this->settings->userRoles)) {
141 $this->accessDenied = true;
142 return true;
143 }
144
145 $currentUser = wp_get_current_user();
146
147 if (defined('WPSTGPRO_VERSION') && !empty($this->settings->usersWithStagingAccess)) {
148 $usersWithStagingAccess = explode(',', $this->settings->usersWithStagingAccess);
149
150 // check against usernames
151 if (in_array($currentUser->user_login, $usersWithStagingAccess, true)) {
152 return false;
153 }
154
155 // check against emails
156 if (in_array($currentUser->user_email, $usersWithStagingAccess, true)) {
157 return false;
158 }
159 }
160
161 // Require login form if user is not in specific user role
162 $activeUserRoles = $currentUser->roles;
163
164 $result = isset($this->settings->userRoles) && is_array($this->settings->userRoles) ?
165 array_intersect($activeUserRoles, $this->settings->userRoles) :
166 [];
167
168 if (empty($result) && !$this->isLoginPage() && !is_admin()) {
169 $this->accessDenied = true;
170 return true;
171 }
172
173 // Don't show login form if no other rule apply
174 return false;
175 }
176
177 /**
178 * Check if it is a staging site
179 * @return bool
180 */
181 private function isStagingSite()
182 {
183 return (new SiteInfo())->isStagingSite();
184 }
185
186 /**
187 * Check if it is the login page
188 * @return bool
189 */
190 private function isLoginPage()
191 {
192 return ($GLOBALS["pagenow"] === "wp-login.php");
193 }
194
195 /**
196 * Reset permalink structure of the clone to default; index.php?p=123
197 */
198 private function resetPermaLinks()
199 {
200 // Do nothing
201 if (!$this->isStagingSite() || get_option("wpstg_rmpermalinks_executed") === "true") {
202 return;
203 }
204
205 // Do nothing
206 if (defined('WPSTGPRO_VERSION') && isset($this->settings->keepPermalinks) && $this->settings->keepPermalinks === "1") {
207 return;
208 }
209
210 // $wp_rewrite is not available before the init hook. So we need to use the global variable
211 global $wp_rewrite;
212 $wp_rewrite->set_permalink_structure(null);
213
214 flush_rewrite_rules();
215
216 update_option("wpstg_rmpermalinks_executed", "true");
217 }
218 }
219