roles ) ) { $email = $userOBJ->user_email; $ID = $userOBJ->ID; $random_number = wp_rand( 0, PHP_INT_MAX ); $token_str = $email.'-'.$ID.'-'.$random_number; $access_token = wp_hash( $token_str, 'inspry-agency-toolkit' ); $expiration = 180; $create_access_token_url = site_url() . '/wp-admin/admin.php?page=inspry-agency-toolkit&rescue_verify=' . $access_token; $subject = 'Agency Toolkit Admin Access Reset'; $message = "A request was made to reset the list of admins who can access the Agency Toolkit plugin. If this was an intentional, please click here to proceed with the reset. If this was not you, please disregard. This Access Reset link will be expire in 3 minutes."; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); if ( wp_mail( $email, $subject, $message, $headers ) ) { set_transient('iat_access_token', $access_token, $expiration); inspry_agency_custom_admin_notice( 'success', 'Agency Toolkit Admin Access Reset Mail Sent. Please check your inbox.' ); } } } else { inspry_agency_custom_admin_notice( 'error', 'Agency Toolkit Admin Access Reset link has timed out Please try again.' ); } } else if ( isset($_GET['rescue_verify']) && !isset($_GET['rescue']) ) { if ( get_transient ( 'iat_access_token' ) === false ) { inspry_agency_custom_admin_notice( 'error', 'Agency Toolkit Admin Access Reset link has timed out Please try again.' ); return; } $access_token = get_transient( 'iat_access_token' ); $fetch_access_token = sanitize_text_field( wp_unslash( $_GET[ 'rescue_verify' ] ) ); if ( is_string( $access_token ) && hash_equals( $access_token, $fetch_access_token ) ) { update_option( 'inspry_toolkit_limit_admins_list', [] ); delete_transient( 'iat_access_token' ); inspry_agency_custom_admin_notice( 'success', 'Agency Toolkit Admin Access Reset Successful.' ); } else { inspry_agency_custom_admin_notice( 'success', 'Agency Toolkit Admin Access Reset link has timed out Please try again.' ); } } } add_action( 'init', 'inspry_agency_toolkit_provide_access_to_user' ); } function inspry_agency_custom_admin_notice( $class, $message ) { // A locked-out admin who starts a rescue is redirected off the toolkit page // before admin_notices runs, so the notice is stored briefly per user and // shown on the next admin page they land on. Before init the user is not // known yet, so fall back to an in-request notice. if ( did_action( 'init' ) && get_current_user_id() ) { set_transient( 'iat_notice_' . get_current_user_id(), array( $class, $message ), MINUTE_IN_SECONDS ); return; } add_action( 'admin_notices', function() use ( $class, $message ) { inspry_agency_render_admin_notice( $class, $message ); } ); } function inspry_agency_render_admin_notice( $class, $message ) { echo "

" . esc_html( $message ) . "

"; } // Show any stored rescue notice once, on whichever admin page comes next add_action( 'admin_notices', function () { $user_id = get_current_user_id(); if ( ! $user_id ) { return; } $notice = get_transient( 'iat_notice_' . $user_id ); if ( ! is_array( $notice ) || count( $notice ) !== 2 ) { return; } delete_transient( 'iat_notice_' . $user_id ); inspry_agency_render_admin_notice( $notice[0], $notice[1] ); } );