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 7.6.8 7.6.9 7.7.0 7.8.0 7.8.1 7.8.10 7.8.11 7.8.12 7.8.13 7.8.14 7.8.15 7.8.16 7.8.17 7.8.18 7.8.2 7.8.3 7.8.4 7.8.5 7.8.5.1 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1 7.9.10 7.9.11 7.9.2 7.9.3 7.9.4 7.9.5 7.9.6 7.9.7 7.9.8 7.9.9 8.0.0 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.1.0 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 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.2.0 3.3.0 3.4.0 3.5.0 3.6.1 3.7.0 3.8.0 3.9.0 3.9.1 3.9.2 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0
admin-site-enhancements / classes / class-utilities.php
admin-site-enhancements / classes Last commit date
class-activation.php 3 years ago class-admin-interface.php 3 years ago class-common-methods.php 3 years ago class-content-management.php 3 years ago class-custom-code.php 3 years ago class-deactivation.php 3 years ago class-disable-components.php 3 years ago class-login-logout.php 3 years ago class-optimizations.php 3 years ago class-security.php 3 years ago class-settings-fields-render.php 3 years ago class-settings-sanitization.php 3 years ago class-settings-sections-fields.php 3 years ago class-utilities.php 3 years ago
class-utilities.php
259 lines
1 <?php
2
3 namespace ASENHA\Classes;
4 use WP_Error;
5
6 /**
7 * Class related to Utilities features
8 *
9 * @since 1.5.0
10 */
11 class Utilities {
12
13 /**
14 * Show admin bar status icon
15 *
16 * @since 4.1.0
17 */
18 public function show_admin_bar_icon() {
19 add_action( 'wp_before_admin_bar_render', [ $this, 'add_wp_admin_bar_item' ] );
20 add_action( 'admin_head', [ $this, 'add_wp_admin_bar_item_styles' ] );
21 add_action( 'wp_head', [ $this, 'add_wp_admin_bar_item_styles' ] );
22 }
23
24 /**
25 * Add WP Admin Bar item
26 *
27 * @since 4.1.0
28 */
29 public function add_wp_admin_bar_item() {
30 global $wp_admin_bar;
31
32 if ( is_user_logged_in() ) {
33 if ( current_user_can( 'manage_options' ) ) {
34 $wp_admin_bar->add_menu( array(
35 'id' => 'password_protection',
36 'title' => '',
37 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
38 'meta' => array(
39 'title' => 'Password protection is enabled for this site.',
40 ),
41 ) );
42 }
43 }
44
45 }
46
47 /**
48 * Add icon and CSS for admin bar item
49 *
50 * @since 4.1.0
51 */
52 public function add_wp_admin_bar_item_styles() {
53
54 if ( is_user_logged_in() ) {
55 if ( current_user_can( 'manage_options' ) ) {
56
57 ?>
58 <style>
59 #wp-admin-bar-password_protection {
60 background-color: green !important;
61 transition: .25s;
62 }
63 #wp-admin-bar-password_protection > .ab-item {
64 color: #fff !important;
65 }
66 #wp-admin-bar-password_protection > .ab-item:before {
67 content: "\f160";
68 top: 2px;
69 color: #fff !important;
70 margin-right: 0px;
71 }
72 #wp-admin-bar-password_protection:hover > .ab-item {
73 background-color: #006600 !important;
74 color: #fff;
75 }
76 </style>
77 <?php
78
79 }
80 }
81
82 }
83
84 /**
85 * Disable page caching
86 *
87 * @since 4.1.0
88 */
89 public function maybe_disable_page_caching() {
90
91 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
92 define( 'DONOTCACHEPAGE', true );
93 }
94
95 }
96
97 /**
98 * Maybe show login form
99 *
100 * @since 4.1.0
101 */
102 public function maybe_show_login_form() {
103
104 // When user is logged-in as in an administrator
105 if ( is_user_logged_in() ) {
106 if ( current_user_can( 'manage_options' ) ) {
107 return; // Do not load login form or perform redirection to the login form
108 }
109 }
110
111 // When site visitor has entered correct password
112 $auth_cookie = $_COOKIE['asenha_password_protection'];
113
114 if ( 'authenticated' == $auth_cookie ) {
115 return; // Do not load login form or perform redirection to the login form
116 }
117
118 if ( isset( $_REQUEST['protected-page'] ) && 'view' == $_REQUEST['protected-page'] ) {// Show login form
119
120 $password_protected_login_page_template = ASENHA_PATH . 'includes/password-protected-login.php';
121
122 load_template( $password_protected_login_page_template );
123 exit();
124
125 } else { // Redirect to login form
126
127 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] );
128
129 $args = array(
130 'protected-page' => 'view',
131 'source' => urlencode( $current_url ),
132 );
133
134 $pwd_protect_login_url = add_query_arg( $args, home_url() );
135
136 nocache_headers();
137 wp_safe_redirect( $pwd_protect_login_url );
138 exit();
139
140 }
141
142 }
143
144 /**
145 * Maybe process login to access protected page content
146 *
147 * @since 4.1.0
148 */
149 public function maybe_process_login() {
150
151 global $password_protected_errors;
152 $password_protected_errors = new WP_Error();
153
154 if ( isset( $_REQUEST['protected_page_pwd'] ) ) {
155
156 $password_input = $_REQUEST['protected_page_pwd'];
157
158 $options = get_option( ASENHA_SLUG_U, array() );
159 $stored_password = $options['password_protection_password'];
160
161 if ( ! empty( $password_input ) ) {
162
163 if ( $password_input == base64_decode( $stored_password ) ) { // Password is correct
164
165 // Set auth cookie
166 // $expiration = time() + DAY_IN_SECONDS; // in 24 hours
167 $expiration = 0; // by the end of browsing session
168 setcookie( 'asenha_password_protection', 'authenticated', $expiration, COOKIEPATH, COOKIE_DOMAIN, true, true );
169
170 // Redirect
171 $redirect_to_url = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : '';
172 wp_safe_redirect( $redirect_to_url );
173 exit();
174
175 } else { // Password is incorrect
176
177 // Add error message
178 $password_protected_errors->add( 'incorrect_password', 'Incorrect password.' );
179
180 }
181
182 } else { // Password input is empty
183
184 // Add error message
185 $password_protected_errors->add( 'empty_password', 'Password can not be empty.' );
186
187 }
188
189 }
190 }
191
192 /**
193 * Add custom login error messages
194 *
195 * @since 4.1.0
196 */
197 public function add_login_error_messages() {
198
199 global $password_protected_errors;
200
201 if ( $password_protected_errors->get_error_code() ) {
202
203 $messages = '';
204 $errors = '';
205
206 // Extract the error message
207
208 foreach( $password_protected_errors->get_error_codes() as $code ) {
209
210 $severity = $password_protected_errors->get_error_data( $code );
211
212 foreach( $password_protected_errors->get_error_messages( $code ) as $error ) {
213 if ( 'message' == $severity ) {
214 $messages .= $error . '<br />';
215 } else {
216 $errors .= $error . '<br />';
217 }
218 }
219
220 }
221
222 // Output the error message
223
224 if ( ! empty( $messages ) ) {
225 echo '<p class="message">' . $messages . '</p>';
226 }
227
228 if ( ! empty( $errors ) ) {
229 echo '<div id="login_error">' . $errors . '</div>';
230 }
231
232 }
233
234 }
235
236 /**
237 * Redirect 404 to homepage
238 *
239 * @since 1.7.0
240 */
241 public function redirect_404_to_homepage() {
242
243 if ( ! is_404() || is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) {
244
245 return;
246
247 } else {
248
249 // wp_safe_redirect( home_url(), 301 );
250
251 header( 'HTTP/1.1 301 Moved Permanently');
252 header( 'Location: ' . home_url() );
253 exit();
254
255 }
256
257 }
258
259 }