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-deactivation.php
3 years ago
class-disable-components.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
435 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | use Walker_Nav_Menu_Checklist; |
| 5 | |
| 6 | /** |
| 7 | * Class related to Utilities features |
| 8 | * |
| 9 | * @since 1.5.0 |
| 10 | */ |
| 11 | class Utilities { |
| 12 | |
| 13 | /** |
| 14 | * Add metabox to Appearance >> Menus page for the login logout menu items |
| 15 | * |
| 16 | * @since 3.4.0 |
| 17 | */ |
| 18 | public function add_login_logout_metabox() { |
| 19 | add_meta_box( |
| 20 | 'add-login-logout', |
| 21 | 'Login Logout', |
| 22 | array( $this, 'add_login_logout_menu_items' ), |
| 23 | 'nav-menus', |
| 24 | 'side', |
| 25 | 'default' |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Add menu items for the login logout metabox |
| 31 | * |
| 32 | * @since 3.4.0 |
| 33 | */ |
| 34 | public function add_login_logout_menu_items() { |
| 35 | |
| 36 | // The ID of the currently selected menu |
| 37 | global $nav_menu_selected_id; |
| 38 | |
| 39 | $menu_items = array( |
| 40 | 'asenha-login' => array( |
| 41 | 'title' => 'Log In', |
| 42 | 'url' => '#asenha-login', |
| 43 | 'classes' => array( 'asenha-login-menu-item' ), |
| 44 | ), |
| 45 | 'asenha-logout' => array( |
| 46 | 'title' => 'Log Out', |
| 47 | 'url' => '#asenha-logout', |
| 48 | 'classes' => array( 'asenha-logout-menu-item' ), |
| 49 | ), |
| 50 | 'asenha-login-logout' => array( |
| 51 | 'title' => 'Log In / Log Out', |
| 52 | 'url' => '#asenha-login-logout', |
| 53 | 'classes' => array( 'asenha-login-logout-menu-item' ), |
| 54 | ), |
| 55 | ); |
| 56 | |
| 57 | $item_details = array( |
| 58 | 'db_id' => 0, |
| 59 | 'object' => 'asenha', |
| 60 | 'object_id' => '', |
| 61 | 'menu_item_parent' => 0, |
| 62 | 'type' => 'custom', |
| 63 | 'title' => '', |
| 64 | 'url' => '', |
| 65 | 'target' => '', |
| 66 | 'attr_title' => '', |
| 67 | 'classes' => array(), |
| 68 | 'xfn' => '', |
| 69 | ); |
| 70 | |
| 71 | $menu_items_object = array(); |
| 72 | |
| 73 | foreach ( $menu_items as $item_id => $details ) { |
| 74 | $menu_items_object[ $details['title'] ] = (object) $item_details; |
| 75 | $menu_items_object[ $details['title'] ]->object_id = $item_id; |
| 76 | $menu_items_object[ $details['title'] ]->title = $details['title']; |
| 77 | $menu_items_object[ $details['title'] ]->classes = $details['classes']; |
| 78 | $menu_items_object[ $details['title'] ]->url = $details['url']; |
| 79 | } |
| 80 | |
| 81 | $walker = new Walker_Nav_Menu_Checklist( array() ); |
| 82 | |
| 83 | ?> |
| 84 | <div id="login-logout-links" class="loginlinksdiv"> |
| 85 | <div id="tabs-panel-login-logout-links-all" class="tabs-panel tabs-panel-view-all tabs-panel-active"> |
| 86 | <ul id="login-logout-links-checklist" class="list:login-logout-links categorychecklist form-no-clear"> |
| 87 | <?php echo walk_nav_menu_tree( |
| 88 | array_map( 'wp_setup_nav_menu_item', $menu_items_object ), |
| 89 | 0, |
| 90 | (object) array( 'walker' => $walker) |
| 91 | ); ?> |
| 92 | </ul> |
| 93 | </div> |
| 94 | <p class="button-controls"> |
| 95 | <span class="add-to-menu"> |
| 96 | <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php echo esc_attr( 'Add to Menu' ); ?>" name="add-login-logout-links-menu-item" id="submit-login-logout-links" /> |
| 97 | <span class="spinner"></span> |
| 98 | </span> |
| 99 | </p> |
| 100 | </div> |
| 101 | <?php |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Setup login logout URL based on login state |
| 107 | * |
| 108 | * @since 3.4.0 |
| 109 | */ |
| 110 | public function set_login_logout_menu_item_dynamic_url( $menu_item ) { |
| 111 | |
| 112 | global $pagenow; |
| 113 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 114 | |
| 115 | if ( $pagenow != 'nav-menus.php' && !defined('DOING_AJAX') && isset( $menu_item->url ) && false !== strpos( $menu_item->url, 'asenha' ) ) { |
| 116 | |
| 117 | // Define login URL based on whether |
| 118 | if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) { |
| 119 | if ( array_key_exists( 'custom_login_slug', $options ) && ! empty( $options['custom_login_slug'] ) ) { |
| 120 | $login_page_url = get_site_url() . '/' . $options['custom_login_slug']; |
| 121 | } |
| 122 | } else { |
| 123 | $login_page_url = wp_login_url(); |
| 124 | } |
| 125 | |
| 126 | $logout_redirect_url = home_url(); |
| 127 | |
| 128 | switch( $menu_item->url ) { |
| 129 | case '#asenha-login'; |
| 130 | $menu_item->url = $login_page_url; |
| 131 | break; |
| 132 | case '#asenha-logout'; |
| 133 | $menu_item->url = wp_logout_url(); |
| 134 | break; |
| 135 | case '#asenha-login-logout'; |
| 136 | $menu_item->url = ( is_user_logged_in() ) ? wp_logout_url() : $login_page_url; |
| 137 | $menu_item->title = ( is_user_logged_in() ) ? 'Log Out' : 'Log In'; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | return $menu_item; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Conditionally remove login or logout menu item based on is_user_logged_in() |
| 149 | * |
| 150 | * @since 3.4.0 |
| 151 | */ |
| 152 | public function maybe_remove_login_or_logout_menu_item( $sorted_menu_items ) { |
| 153 | |
| 154 | foreach( $sorted_menu_items as $menu => $item ) { |
| 155 | |
| 156 | $item_classes = $item->classes; |
| 157 | |
| 158 | // Maybe remove Log In menu item |
| 159 | if ( in_array( 'asenha-login-menu-item', $item_classes ) ) { |
| 160 | if ( is_user_logged_in() ) { |
| 161 | unset( $sorted_menu_items[$menu] ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Maybe remove Log Out menu item |
| 166 | if ( in_array( 'asenha-logout-menu-item', $item_classes ) ) { |
| 167 | if ( ! is_user_logged_in() ) { |
| 168 | unset( $sorted_menu_items[$menu] ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | return $sorted_menu_items; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Redirect to custom internal URL after login for user roles |
| 180 | * |
| 181 | * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL. |
| 182 | * @param string $origin_url URL the user is coming from. |
| 183 | * @param object $user logged-in user's data. |
| 184 | * @since 1.5.0 |
| 185 | */ |
| 186 | public function redirect_for_roles_after_login( $username, $user ) { |
| 187 | |
| 188 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 189 | $redirect_after_login_to_slug = $options['redirect_after_login_to_slug']; |
| 190 | $redirect_after_login_for = $options['redirect_after_login_for']; |
| 191 | |
| 192 | if ( isset( $redirect_after_login_for ) && ( count( $redirect_after_login_for ) > 0 ) ) { |
| 193 | |
| 194 | // Assemble single-dimensional array of roles for which custom URL redirection should happen |
| 195 | $roles_for_custom_redirect = array(); |
| 196 | |
| 197 | foreach( $redirect_after_login_for as $role_slug => $custom_redirect ) { |
| 198 | if ( $custom_redirect ) { |
| 199 | $roles_for_custom_redirect[] = $role_slug; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Does the user have roles data in array form? |
| 204 | if ( isset( $user->roles ) && is_array( $user->roles ) ) { |
| 205 | |
| 206 | $current_user_roles = $user->roles; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | // Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard. |
| 211 | foreach ( $current_user_roles as $role ) { |
| 212 | if ( in_array( $role, $roles_for_custom_redirect ) ) { |
| 213 | |
| 214 | wp_safe_redirect( home_url( $redirect_after_login_to_slug . '/' ) ); |
| 215 | exit(); |
| 216 | |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Redirect to custom internal URL after login for user roles |
| 226 | * |
| 227 | * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL. |
| 228 | * @param string $origin_url URL the user is coming from. |
| 229 | * @param object $user logged-in user's data. |
| 230 | * @since 1.6.0 |
| 231 | */ |
| 232 | public function redirect_after_logout( $user_id ) { |
| 233 | |
| 234 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 235 | $redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug']; |
| 236 | $redirect_after_logout_for = $options['redirect_after_logout_for']; |
| 237 | |
| 238 | $user = get_userdata( $user_id ); |
| 239 | |
| 240 | if ( isset( $redirect_after_logout_for ) && ( count( $redirect_after_logout_for ) > 0 ) ) { |
| 241 | |
| 242 | // Assemble single-dimensional array of roles for which custom URL redirection should happen |
| 243 | $roles_for_custom_redirect = array(); |
| 244 | |
| 245 | foreach( $redirect_after_logout_for as $role_slug => $custom_redirect ) { |
| 246 | if ( $custom_redirect ) { |
| 247 | $roles_for_custom_redirect[] = $role_slug; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Does the user have roles data in array form? |
| 252 | if ( isset( $user->roles ) && is_array( $user->roles ) ) { |
| 253 | |
| 254 | $current_user_roles = $user->roles; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | // Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard. |
| 259 | foreach ( $current_user_roles as $role ) { |
| 260 | if ( in_array( $role, $roles_for_custom_redirect ) ) { |
| 261 | |
| 262 | wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) ); |
| 263 | exit(); |
| 264 | |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Redirect 404 to homepage |
| 274 | * |
| 275 | * @since 1.7.0 |
| 276 | */ |
| 277 | public function redirect_404_to_homepage() { |
| 278 | |
| 279 | if ( ! is_404() || is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { |
| 280 | |
| 281 | return; |
| 282 | |
| 283 | } else { |
| 284 | |
| 285 | // wp_safe_redirect( home_url(), 301 ); |
| 286 | |
| 287 | header( 'HTTP/1.1 301 Moved Permanently'); |
| 288 | header( 'Location: ' . home_url() ); |
| 289 | exit(); |
| 290 | |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Enqueue custom admin CSS |
| 297 | * |
| 298 | * @since 2.3.0 |
| 299 | */ |
| 300 | public function custom_admin_css() { |
| 301 | |
| 302 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 303 | $custom_admin_css = $options['custom_admin_css']; |
| 304 | |
| 305 | ?> |
| 306 | <style type="text/css"> |
| 307 | <?php echo wp_kses_post( $custom_admin_css ); ?> |
| 308 | </style> |
| 309 | <?php |
| 310 | |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Enqueue custom frontend CSS |
| 315 | * |
| 316 | * @since 2.3.0 |
| 317 | */ |
| 318 | public function custom_frontend_css() { |
| 319 | |
| 320 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 321 | $custom_frontend_css = $options['custom_frontend_css']; |
| 322 | |
| 323 | ?> |
| 324 | <style type="text/css"> |
| 325 | <?php echo wp_kses_post( $custom_frontend_css ); ?> |
| 326 | </style> |
| 327 | <?php |
| 328 | |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Show content of ads.txt saved to options |
| 333 | * |
| 334 | * @since 3.2.0 |
| 335 | */ |
| 336 | public function show_ads_appads_txt_content() { |
| 337 | |
| 338 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 339 | |
| 340 | $request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false; |
| 341 | |
| 342 | if ( '/ads.txt' === $request ) { |
| 343 | |
| 344 | $ads_txt_content = array_key_exists( 'ads_txt_content', $options ) ? $options['ads_txt_content'] : ''; |
| 345 | |
| 346 | header( 'Content-Type: text/plain' ); |
| 347 | echo esc_textarea( $ads_txt_content ); |
| 348 | die(); |
| 349 | |
| 350 | } |
| 351 | |
| 352 | if ( '/app-ads.txt' === $request ) { |
| 353 | |
| 354 | $app_ads_txt_content = array_key_exists( 'app_ads_txt_content', $options ) ? $options['app_ads_txt_content'] : ''; |
| 355 | |
| 356 | header( 'Content-Type: text/plain' ); |
| 357 | echo esc_textarea( $app_ads_txt_content ); |
| 358 | die(); |
| 359 | |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Insert code before </head> tag |
| 366 | * |
| 367 | * @since 3.3.0 |
| 368 | */ |
| 369 | public function insert_head_code() { |
| 370 | |
| 371 | $this->insert_code( 'head' ); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Insert code after <body> tag |
| 377 | * |
| 378 | * @since 3.3.0 |
| 379 | */ |
| 380 | public function insert_body_code() { |
| 381 | |
| 382 | $this->insert_code( 'body' ); |
| 383 | |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Insert code in footer section before </body> tag |
| 388 | * |
| 389 | * @since 3.3.0 |
| 390 | */ |
| 391 | public function insert_footer_code() { |
| 392 | |
| 393 | $this->insert_code( 'footer' ); |
| 394 | |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Insert code |
| 399 | * |
| 400 | * @since 3.3.0 |
| 401 | */ |
| 402 | public function insert_code( $location ) { |
| 403 | |
| 404 | // Do not insert code in admin, feed, robots or trackbacks |
| 405 | if ( is_admin() || is_feed() || is_robots() || is_trackback() ) { |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // Get option that stores the code |
| 410 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 411 | |
| 412 | if ( 'head' == $location ) { |
| 413 | |
| 414 | $code = array_key_exists( 'head_code', $options ) ? $options['head_code'] : ''; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | if ( 'body' == $location ) { |
| 419 | |
| 420 | $code = array_key_exists( 'body_code', $options ) ? $options['body_code'] : ''; |
| 421 | |
| 422 | } |
| 423 | |
| 424 | if ( 'footer' == $location ) { |
| 425 | |
| 426 | $code = array_key_exists( 'footer_code', $options ) ? $options['footer_code'] : ''; |
| 427 | |
| 428 | } |
| 429 | |
| 430 | echo wp_unslash( $code ) . PHP_EOL; |
| 431 | |
| 432 | } |
| 433 | |
| 434 | |
| 435 | } |