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 | } |