*/
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.
*/
$script_path = plugin_dir_path(__FILE__) . 'js/ace-user-management-public.js';
$script_url = plugin_dir_url(__FILE__) . 'js/ace-user-management-public.js';
$script_version = file_exists($script_path) ? filemtime($script_path) : $this->version;
wp_enqueue_script($this->pluginName,$script_url,array('jquery'),$script_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');
if ( $id && isset( $id->post_name ) ) {
wp_safe_redirect( esc_url( 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 ) {
$login = $this->getpage_data( 'login' );
$profile = $this->getpage_data( 'profile' );
if ( is_wp_error( $user ) ) {
return get_permalink( $login ) . '?login=failed';
}
if ( isset( $user->roles[0] ) && 'administrator' === $user->roles[0] ) {
return admin_url();
}
return get_permalink( $profile );
}
public function aceCatchEmptyUser( $username, $pwd ) {
$loginPermalink = $this->getpage_data('login');
$recaptchaKeys = get_option('custom_reCapatcha_value');
if ( isset( $recaptchaKeys['c_reCaptcha_value'] ) && $recaptchaKeys['c_reCaptcha_value'] == 1 ) {
if ( ! isset( $_POST['ace_recaptcha_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ace_recaptcha_nonce'] ) ), 'ace_recaptcha_action' ) ) {
return;
}
$recaptcha_response = isset( $_POST['g-recaptcha-response'] )
? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) )
: '';
$recEmpty = empty( $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 .= '
';
$items .= '';
} else {
$items .= '';
$items .= '';
}
}
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_safe_redirect(esc_url( get_permalink( $profile )) );
exit;
}
if ( is_page($registration)) {
wp_safe_redirect(esc_url( site_url() ));
exit;
}
}else{
if( is_page($profile)) {
wp_safe_redirect(esc_url( 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_safe_redirect(esc_url( 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');
$login_error = '';
if ( isset($_POST['g-recaptcha-response']) ) {
if ( isset($_POST['login_nonce']) &&
wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['login_nonce'])), 'login_action') ) {
} else {
wp_die( esc_html__( 'Security check failed. Please try again.', 'ace-user-management' ) );
}
$captcha = sanitize_text_field(wp_unslash($_POST['g-recaptcha-response']));
$secretKey = $recaptchaKeys["author_reCap_secertkey"]; // ✅ remove PHP echo inside string
$remoteip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : '';
$response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $captcha . "&remoteip=" . $remoteip);
$responseKeys = json_decode(wp_remote_retrieve_body($response), true);
if (empty($responseKeys["success"])) {
return "failed";
} else {
return "success";
}
}
if (is_user_logged_in()) {
echo "";
}
$recaptchaKeys = get_option('custom_reCapatcha_value');
if (isset($_GET['login']) && $_GET['login'] === 'failed') {
$login_error = __('⚠ Username or password is incorrect.', 'ace-user-management');
}
if (isset($_GET['login']) && $_GET['login'] === 'empty') {
$login_error = __('⚠ Please fill in all required fields.', 'ace-user-management');
}
$url = ( __DIR__ );
require_once ( __DIR__ ). '/partials/ace-login.php';
return ob_get_clean();
}
public function ace_forget_password(){
if ( is_user_logged_in() ) {
wp_safe_redirect(site_url());
}
ob_start();
global $wpdb;
if (isset($_POST['reset-submit'])) {
$nonce = '';
if ( isset( $_POST['ace_reset_password_nonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_POST['ace_reset_password_nonce'] ) );
}
if ( ! wp_verify_nonce( $nonce, 'ace_reset_password_action' ) ) {
wp_die( esc_html__( 'Security check failed', 'ace-user-management' ) );
}
$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');
global $wpdb;
$table_name = $wpdb->prefix . 'ace_reset_password';
$cache_key = 'reset_code_' . $userId;
$exsist_user = wp_cache_get($cache_key, 'ace_reset_password_cache');
if ( false === $exsist_user ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$exsist_user = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ace_reset_password WHERE user_id = %d",
(int) $userId
)
);
wp_cache_set( $cache_key, $exsist_user, 'ace_reset_password_cache', 3600 );
}
if (!empty($exsist_user)) {
$update_data = array(
'randomCode' => $randomCode,
'currentTime' => current_time('mysql')
);
$where = array('user_id' => $userId);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->update($table_name, $update_data, $where);
} else {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$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;
}
/**
* Redirects old randNum URLs safely using nonce verification.
*/
function redirect_old_randNum_url() {
// Check if randNum and nonce exist in URL
if ( isset( $_GET['randNum'], $_GET['nonce'] ) && ! empty( $_GET['randNum'] ) ) {
// Verify nonce to ensure request is valid
$nonce = sanitize_text_field( wp_unslash( $_GET['nonce'] ) );
if ( wp_verify_nonce( $nonce, 'confirm_password_action' ) ) {
// Sanitize randNum
$randNum = sanitize_text_field( wp_unslash( $_GET['randNum'] ) );
// Perform safe redirect
wp_safe_redirect( home_url( "/confirm-password/{$randNum}" ) );
exit();
}
}
}
public function ace_confirm_code_page() {
ob_start();
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) {
global $wpdb;
$table_name = $wpdb->prefix . 'ace_reset_password';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$sql = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ace_reset_password WHERE randomCode = %s",
sanitize_text_field( $getRandcode )
)
);
if ( $sql === false ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$sql = $wpdb->get_row(
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}ace_reset_password WHERE randomCode = %s",
sanitize_text_field( $getRandcode )
)
);
wp_cache_set( $cache_key, $sql, 'ace_reset_password_cache', 43200 );
}
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'])) {
if ( isset( $_POST['ace_confirm_password_nonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_POST['ace_confirm_password_nonce'] ) );
} else {
$nonce = '';
}
if ( ! wp_verify_nonce( $nonce, 'ace_confirm_password_action' ) ) {
wp_die( esc_html__( 'Security check failed. Please try again.', 'ace-user-management' ) );
}
$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_id = intval($sql->user_id); // Secure: user linked to this token only
if ($user_id > 0) {
wp_set_password($password, $user_id);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->delete($tablename, array('randomCode' => $getRandcode)); // Delete token after use
echo '' . esc_html__('Password successfully updated', 'ace-user-management') . '
';
echo '';
} else {
echo '' . esc_html__('Invalid reset link', 'ace-user-management') . '
';
}
} 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) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$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'])) != '') {
if (isset($_POST['user_registration_nonce_field']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['user_registration_nonce_field'])), 'user_registration_nonce')) {
$username = isset($_REQUEST['username']) ? sanitize_text_field(wp_unslash($_REQUEST['username'])) : '';
$errors = [];
/* Username */
if (empty($username)) {
$errors['username'] = __('Enter Username', 'ace-user-management');
} else {
if (strlen($username) < 7) {
$errors['username'] = __('Username must be 6 character long.', 'ace-user-management');
} elseif (!preg_match('/\d/', $username)) {
$errors['username'] = __('Must contain one number also.', 'ace-user-management');
} elseif (username_exists($username)) {
$errors['username'] = __('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)) {
$errors['email'] = __('No Disposable email allowed.', 'ace-user-management');
}
if (empty($email)) {
$errors['email'] = __('Enter email', 'ace-user-management');
} elseif (!is_email($email)) {
$errors['email'] = __('Enter valid email', 'ace-user-management');
} elseif (email_exists($email)) {
$errors['email'] = __('This email already exists.', 'ace-user-management');
}
$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)) {
$errors['password'] = __('Enter Password', 'ace-user-management');
} elseif (empty($c_password)) {
$errors['c_password'] = __('Enter Confirm Password', 'ace-user-management');
} elseif ($password !== $c_password) {
$errors['c_password'] = __('Passwords do not match', 'ace-user-management');
} else {
// Strong password validation
$password_pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,}$/';
if (!preg_match($password_pattern, $password)) {
$errors['password'] = __('e.g. Abc@1234', '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');
}
}
}
foreach ($results as $field) {
if (empty($field->is_required)) {
continue;
}
$field_name = $field->input_name;
$field_label = ucfirst($field->input_label);
// Fetch the field value safely from POST
$value = '';
if ( isset($_POST[$field_name]) && is_string($_POST[$field_name]) ) {
$value = sanitize_text_field( wp_unslash( $_POST[$field_name] ) );
$value = trim($value);
}
if ($value === '') {
$errors[$field_name] = sprintf(
/* translators: %s: Field label */
esc_html__('The field %s is required', 'ace-user-management'),
esc_html($field_label)
);
}
}
// If no errors, proceed to create the user
if (empty($errors)) {
$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);
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);
}
$success_message = __('You have successfully registered.', 'ace-user-management');
}
}
}
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;
}
}