*/ class Ace_User_Management_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $pluginName The ID of this plugin. */ private $pluginName; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $pluginName The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $pluginName, $version ) { $this->pluginName = $pluginName; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function aceEnqueueStyles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Ace_User_Management_Loader as all of the hooks are defined * in that particular class. * * The Ace_User_Management_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( 'bootstrap-css', plugin_dir_url( __FILE__ ) . 'css/bootstrap.min.css',array(), $this->version, 'all' ); wp_enqueue_style( $this->pluginName, plugin_dir_url( __FILE__ ) . 'css/ace-user-management-public.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function aceEnqueueScripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Ace_User_Management_Loader as all of the hooks are defined * in that particular class. * * The Ace_User_Management_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->pluginName, plugin_dir_url( __FILE__ ) .'js/ace-user-management-public.js', array( 'jquery' ), $this->version, false ); wp_localize_script( 'ajax-script', 'my_ajax_object',array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); wp_enqueue_script( $this->pluginName.'api', plugin_dir_url( __FILE__ ) . 'js/ace-api.js', array(), $this->version, 'all' ); // wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js', array(), null, true); wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'bootstrap-js', plugin_dir_url( __FILE__ ) .'js/bootstrap.bundle.min.js', array( 'jquery' ), $this->version, false ); } public function getpage_data($title){ $page = get_posts( array( 'post_type' => 'page', 'title' => $title, 'posts_per_page' => 1, ) ); if ( ! empty( $page ) ) { $page_id = $page[0]->ID; } return $page_id; } public function aceShowAdminBarStatus(){ if ( ! current_user_can( 'manage_options' ) ) { show_admin_bar( false ); } } // user logout public function aceCustomLogoutPage(){ $id= $this->getpage_data('login'); wp_redirect( site_url('/').$id->post_name ); exit; } function aceRegisterUrl($link){ $id= $this->getpage_data('registration'); return str_replace(site_url('wp-login.php?action=register', 'login'),site_url().$id->ID,$link); } // login error public function aceMyLoginRedirect($redirect_to, $requested_redirect_to, $user) { global $pagePermalink; $login = $this->getpage_data('login'); $profile = $this->getpage_data('profile'); if (is_wp_error($user)) { if (isset($_POST['wp-submit'])) { wp_redirect( get_permalink($login)."?&login=failed" ); }else{ wp_redirect( get_permalink($login) ); } exit; }else{ if( $user->roles[0] == 'administrator' ) { wp_redirect( site_url('/wp-admin')); }else { wp_redirect( site_url($profile) ); } } } public function aceCatchEmptyUser( $username, $pwd ) { $loginPermalink = $this->getpage_data('login'); $recaptchaKeys = get_option('custom_reCapatcha_value'); if( $recaptchaKeys['c_reCaptcha_value'] == 1 ){ $recEmpty = empty($_POST['g-recaptcha-response']); } } // user custom profile page public function aceSubscriberLogin(){ global $current_user, $pagePermalink; wp_get_current_user(); if ( ! user_can( $current_user, "administrator" ) ) { //wp_redirect( site_url(). $pagePermalink['profile_permalink']->ID ); } } public function aceLoginoutMenuLink( $items, $args ) { $profile = $this->getpage_data('profile'); $login = $this->getpage_data('login'); $registration = $this->getpage_data('registration'); if ($args->theme_location) { if (is_user_logged_in()) { $items .= '
  • '.esc_html("Profile") .'
  • '; $items .= '
  • '. esc_html("Log Out") .'
  • '; } else { $items .= '
  • '. esc_html("Log In") .'
  • '; $items .= '
  • '. esc_html("Sign Up") .'
  • '; } } return $items; } // login and profile access public function aceRedirectToSpecificPage() { global $current_user; $login = $this->getpage_data('login'); $profile = $this->getpage_data('profile'); $registration = $this->getpage_data('registration'); $Forget_password = $this->getpage_data('Forget password'); wp_get_current_user(); if ( ! user_can( $current_user, "administrator" ) ) { if ( is_user_logged_in() ) { if ( is_page($login)) { wp_redirect( get_permalink( $profile ) ); exit; } if ( is_page($registration)) { wp_redirect( site_url() ); exit; } }else{ if( is_page($profile)) { wp_redirect( get_permalink($login) ) ; exit; } } } } public function ace_user_profile_template(){ require_once ( __DIR__ ). '/partials/ace-profile.php'; } function ace_subscriber_profile(){ $login = $this->getpage_data('login'); if(current_user_can( 'manage_options' )) return ''; if( strpos( site_url(), 'wp_admin/?action=reg.php' ) ){ wp_redirect( get_permalink($login ) ); exit; } } public function ace_login_form(){ ob_start(); $forget_password = $this->getpage_data('Forget password'); $css_by_option = get_option('ace_custom_css_plugin'); $forget_url = get_permalink($forget_password ); $recaptchaKeys = get_option('custom_reCapatcha_value'); if ( isset( $_POST['g-recaptcha-response'])) { $captcha = sanitize_text_field(wp_unslash($_POST['g-recaptcha-response'])); $secretKey = ''; $remoteip = isset($_SERVER['REMOTE_ADDR'])? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])):''; $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $captcha . "&remoteip=" . $remoteip); $responseKeys = json_decode($response, true); if (intval($responseKeys["success"]) !== 1) { return "failed"; } else { return "success"; } } if (is_user_logged_in()) { echo ""; } $recaptchaKeys = get_option('custom_reCapatcha_value'); if (isset($_GET['login']) && $_GET['login'] === 'failed') { echo '
    ⚠ Username and password incorrect
    '; } if (isset($_GET['login']) && $_GET['login'] === 'empty') { echo '
    ⚠ Please fill all fields
    '; } $url = ( __DIR__ ); require_once ( __DIR__ ). '/partials/ace-login.php'; return ob_get_clean(); } public function ace_forget_password(){ if ( is_user_logged_in() ) { wp_redirect(site_url()); } ob_start(); global $wpdb; if (isset($_POST['reset-submit'])) { if ( ! isset($_POST['ace_reset_password_nonce']) || ! wp_verify_nonce($_POST['ace_reset_password_nonce'], 'ace_reset_password_action') ) { wp_die('Security check failed'); } $resetEmail = isset($_POST['reset_password']) ? sanitize_text_field(wp_unslash($_POST['reset_password'])) : ''; $error = array(); $Confirm_Password = $this->getpage_data('Confirm Password'); if (!empty($resetEmail)) { if (is_email($resetEmail)) { if (email_exists($resetEmail)) { $user = get_user_by('email', $resetEmail); $userId = $user->ID; $randomCode = bin2hex(random_bytes(16)); $table_name = $wpdb->prefix . "ace_reset_password"; $cache_key = 'reset_code_' . $userId; $exsist_user = wp_cache_get($cache_key, 'ace_reset_password_cache'); if ($exsist_user === false) { $exsist_user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE user_id = %d", $userId)); wp_cache_set($cache_key, $exsist_user, 'ace_reset_password_cache', 3600); // Cache for 1 hour } if (!empty($exsist_user)) { $update_data = array( 'randomCode' => $randomCode, 'currentTime' => current_time('mysql') ); $where = array('user_id' => $userId); $wpdb->update($table_name, $update_data, $where); } else { $wpdb->insert($table_name, array( "user_id" => $userId, "email" => $resetEmail, "randomCode" => $randomCode )); } $to = $resetEmail; $subject = __('Lost Password', 'ace-user-management'); $message = get_permalink($Confirm_Password). $randomCode; $headers = array('Content-Type: text/html; charset=UTF-8', 'From: ' . get_option('admin_email')); if (wp_mail($to, $subject, $message, $headers)) { echo '
    ' . esc_html('Please check your email for the reset password link.', 'ace-user-management') . '

    '; exit; } else { echo '
    ' . esc_html('! Link not sent. Please try again.', 'ace-user-management') . '

    '; } } else { $error['email_exists'] = __('! Email does not exist.', 'ace-user-management'); } } else { $error['email_validate'] = __('! Please enter a valid email address.', 'ace-user-management'); } } else { $error['empty_email'] = __('! Please enter an email address.', 'ace-user-management'); } foreach ($error as $emailError) { echo '
    ' . esc_html($emailError) . '

    '; } } require_once plugin_dir_path(__FILE__) . 'partials/ace-forget-password.php'; $wp_content = ob_get_clean(); return $wp_content; } function custom_rewrite_rule() { add_rewrite_rule( '^ace-confirm-password/([a-f0-9]{32,64})/?$', // This captures the reset code (hash) 'index.php?pagename=ace-confirm-password&randNum=$matches[1]', // Map the reset code to a query var 'top' ); } function add_randNum_to_query_vars($vars) { $vars[] = 'randNum'; return $vars; } function redirect_old_randNum_url() { if (isset($_GET['randNum']) && !empty($_GET['randNum'])) { $randNum = sanitize_text_field(wp_unslash($_GET['randNum'])); // Sanitize input wp_redirect(home_url("/confirm-password/{$randNum}")); exit(); } } public function ace_confirm_code_page() { ob_start(); // Start output buffering global $wpdb; $login = $this->getpage_data('login'); $getRandcode = get_query_var('randNum'); $tablename = $wpdb->prefix . "ace_reset_password"; $cache_key = 'reset_code_' . $getRandcode; $sql = wp_cache_get($cache_key, 'ace_reset_password_cache'); if ($sql === false) { $sql = $wpdb->get_row("SELECT * FROM $tablename WHERE randomCode = '$getRandcode'"); wp_cache_set($cache_key, $sql, 'ace_reset_password_cache', 43200); } if (!empty($sql->randomCode)) { $createdAt = strtotime($sql->currentTime); $current_time = gmdate('Y-m-d H:i:s'); $current_time_unix = strtotime($current_time); $diffInMinutes = ($current_time_unix - $createdAt) / 60; if ($diffInMinutes > 10) { echo '
    Token expired. Please request a new reset link. Click here
    '; } else { $userId = $sql->user_id; $error = array(); if (isset($_POST['submit_password'])) { $password = isset($_POST['password']) ? sanitize_text_field(wp_unslash($_POST['password'])) : ''; $conPassword = isset($_POST['confirm_password']) ? sanitize_text_field(wp_unslash($_POST['confirm_password'])) : ''; if (!empty($password)) { if (strlen($password) > 6) { if (preg_match('/[!@#$%^&*(),.?":{}|<>]/', $password)) { if (!empty($conPassword)) { if (strcmp($password, $conPassword) == 0) { $user = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0; $password = wp_hash_password($password); $userTable = $wpdb->prefix . 'users'; $update_pass = $wpdb->query("UPDATE $userTable SET user_pass = '$password' WHERE ID = $user"); echo '
    ' . esc_html('Password successfully updated', 'ace-user-management') . '
    '; echo ''; } else { $error['confirm_password'] = __('! Confirm password does not match', 'ace-user-management'); } } else { $error['empty_password'] = __('! Please enter confirm password', 'ace-user-management'); } } else { $error['password_symbol'] = __('! Password must contain at least one special symbol', 'ace-user-management'); } } else { $error['password_length'] = __('! Password must be at least 6 characters long', 'ace-user-management'); } } else { $error['empty_password'] = __('! Please enter a password', 'ace-user-management'); } if (!empty($error)) { foreach ($error as $error_key => $errorValue) { echo '
    ' . esc_html($errorValue) . '
    '; } } } require_once plugin_dir_path(__FILE__) . 'partials/ace-confirm-randnum.php'; } } else { echo ''; } $wp_content = ob_get_clean(); return $wp_content; } public function ace_reg_form(){ ob_start(); $login = $this->getpage_data('login'); global $wpdb, $userID; $cache_key = 'ace_register_fields_data'; $cached_results = wp_cache_get($cache_key, 'ace_register_fields'); if ($cached_results === false) { $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}ace_register_fields ORDER BY sortby ASC", OBJECT); wp_cache_set($cache_key, $results, 'ace_register_fields', 43200); } else { $results = $cached_results; } $firstname = ''; $lastname = ''; $username = ''; $email = ''; if (isset($_POST['submit']) && sanitize_text_field(wp_unslash($_POST['submit'])) != '') { $error = array(); $username = isset($_REQUEST['username']) ? sanitize_text_field(wp_unslash($_REQUEST['username'])) : ''; if (empty($username)) { $error['username_empty'] = __('Enter Username', 'ace-user-management'); } else { if (strlen($username) < 7) { $error['username_length'] = __('Username must be more than 6 characters.', 'ace-user-management'); } if (!preg_match('/\d/', $username)) { $error['username_number'] = __('Username must contain at least one number.', 'ace-user-management'); } if (username_exists($username)) { $error['username_exists'] = __('Username already exists', 'ace-user-management'); } } // Email validation $email = isset($_REQUEST['email']) ? sanitize_text_field(wp_unslash($_REQUEST['email'])) : ''; $blocked_domains = [ 'tempmail.com', 'mailinator.com', '10minutemail.com', 'yopmail.com', 'guerrillamail.com', 'example.com' ]; $email_domain = strtolower(substr(strrchr($email, "@"), 1)); if (in_array($email_domain, $blocked_domains)) { $error['disposable_email_error'] = __('Temporary or disposable email addresses are not allowed. Please use a valid email.', 'ace-user-management'); } if (empty($email)) { $error['email_empty'] = __('Enter email', 'ace-user-management'); } elseif (!is_email($email)) { $error['email_valid'] = __('Enter valid email', 'ace-user-management'); } elseif (email_exists($email)) { $error['email_exists'] = __('This email already exists', 'ace-user-management'); } // Password and confirm password validation $password = isset($_REQUEST['password'])? sanitize_text_field(wp_unslash($_REQUEST['password'])) :''; $c_password = isset($_POST['c_password'])? sanitize_text_field(wp_unslash($_POST['c_password'])) :''; if (empty($password)) { $error['password_empty'] = __('Enter Password', 'ace-user-management'); } elseif (empty($c_password)) { $error['confirm_empty'] = __('Enter Confirm Password', 'ace-user-management'); } elseif ($password !== $c_password) { $error['con_password'] = __('Passwords do not match', 'ace-user-management'); } else { // Strong password validation (min 8 chars, 1 uppercase, 1 lowercase, 1 number, 1 special char) $password_pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,}$/'; if (!preg_match($password_pattern, $password)) { $error['password_strength'] = __('Password must be at least 8 characters long, contain one uppercase letter, one lowercase letter, one number, and one special character.', 'ace-user-management'); } } // reCaptcha validation $recaptcha_v = get_option('custom_reCapatcha_value'); if (is_array($recaptcha_v) && isset($recaptcha_v['c_reCaptcha_value']) && $recaptcha_v['c_reCaptcha_value'] == 1) { $recaptcha_response = isset($_POST['g-recaptcha-response']) ? sanitize_text_field(wp_unslash($_POST['g-recaptcha-response'])) : ''; if (empty($recaptcha_response)) { $error['reCap'] = __('Please verify that you are not a robot.', 'ace-user-management'); } else { // reCaptcha API validation $secret_key = $recaptcha_v['author_reCap_secrectkey']; $response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', [ 'body' => [ 'secret' => $secret_key, 'response' => $recaptcha_response, 'remoteip' => isset($_SERVER['REMOTE_ADDR'])? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])):'', ] ]); $body = wp_remote_retrieve_body($response); $result = json_decode($body); if (isset($result->success) && !$result->success) { $error['reCap'] = __('reCaptcha verification failed. Please try again.', 'ace-user-management'); } } } // If no errors, proceed to create the user if (empty($error)) { $userId = wp_create_user($username, $password, $email); unset($_POST['username']); unset($_POST['email']); unset($_POST['password']); unset($_POST['c_password']); unset($_POST['submit']); $userExtrafields = Ace_User_Management_Function::sanitize($_POST); update_user_meta($userId, 'user_extrafields', $userExtrafields); $user_data = get_user_meta($userId, 'user_extrafields', true); foreach ($user_data as $userkey => $userValue) { update_user_meta($userId, $userkey, $userValue); } // Redirect user after successful registration echo ''; echo ''; } } require_once plugin_dir_path(__FILE__) . 'partials/ace-registration.php'; $wp_content = ob_get_clean(); return $wp_content; } public function ace_current_user_profile(){ ob_start(); require_once plugin_dir_path( __FILE__ ). 'partials/ace-profile.php'; $wp_content = ob_get_clean(); return $wp_content; } }