File: /mnt/data/dreamssalon-wp-market/wp-content/themes/dreamsalon/inc/function-utilities.php
<?php
/**
* dreamsalon Utility functions
*
* @package dreamsalon
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Gets the ID of the current page.
*
* @since 1.0.0
*/
add_action('wp_ajax_nopriv_send_otp', 'send_otp_ajax');
add_action('wp_ajax_send_otp', 'send_otp_ajax');
function send_otp() {
global $dreamsalon_theme_options;
check_ajax_referer('send_otp_nonce', 'security');
$email = sanitize_email($_POST['email']);
$otp = wp_rand(100000, 999999);
$email_encrupt = wp_hash( strtolower( trim( $email ) ) );
// Set OTP in transient for 3 minutes
set_transient('otp_' . $email_encrupt($email), $otp, 180);
// Verify options are set correctly
if (empty($dreamsalon_theme_options)) {
error_log("dreamsalon_theme_options is empty or missing.");
}
$otp_emails_name = $dreamsalon_theme_options['otp_emails_name'] ?? 'Default Name';
$otp_emails_from_email = $dreamsalon_theme_options['otp_emails_from_email'] ?? '[email protected]';
$otp_subject_from_email = $dreamsalon_theme_options['otp_subject_from_email'] ?? 'Your Confirmation Code';
$otp_content_from_email = $dreamsalon_theme_options['otp_content_from_email'] ?? 'Your OTP code is {otp}.';
$otp_email_logo = $dreamsalon_theme_options['otp_email_logo'];
$otp_email_logo_url = is_array($otp_email_logo) && isset($otp_email_logo['url']) ? $otp_email_logo['url'] : '';
$subject = $otp_subject_from_email;
$email_content = str_replace(
[ '{user_email}', '{otp}'],
[ $email, $otp],
$otp_content_from_email
);
$message = '<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body { font-family: Arial, sans-serif; color: #333; line-height: 1.6; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f9f9f9; border: 1px solid #ddd; }
.header { text-align: center; margin-bottom: 30px; }
.header img { max-width: 150px; }
.content { background: #fff; padding: 20px; border-radius: 5px; }
.content h2 { color: #333; }
.content p { margin-bottom: 20px; }
.content ul { padding-left:0px; }
.button { text-align: center; margin-top: 20px; }
.button a { background-color: #0073e6; color: #fff; padding: 12px 20px; border-radius: 5px; text-decoration: none; font-weight: bold; }
.footer { margin-top: 30px; text-align: center; color: #999; font-size: 12px; }
</style>
</head>
<body>
<div class="container">
<div class="header">';
if (!empty($otp_email_logo_url)) {
$message .= '<img src="' . esc_url($otp_email_logo_url) . '" alt="Logo">';
}
$message .= '</div>
<div class="content">' . $email_content . '</div>
<div class="footer">
© ' . date("Y") . ' ' . get_bloginfo('name') . '. All rights reserved.
</div>
</div>
</body>
</html>';
$headers = [
'Content-Type: text/html; charset=UTF-8',
'From: ' . $otp_emails_name . ' <' . $otp_emails_from_email . '>'
];
if ($email) {
if ( function_exists( 'custom_send_email' ) ) {
custom_send_email( $email, $subject, $message, $headers );
}
wp_send_json_success();
} else {
error_log("Failed to send OTP to $email with subject: $subject.");
wp_send_json_error(array('message' => 'Failed to send OTP.'));
}
wp_die();
}
// Fix SVG thumbnail display in Media Library
function fix_svg_display_admin() {
echo '<style>
.attachment-266x266, .thumbnail img {
width: 100% !important;
height: auto !important;
}
</style>';
}
add_action( 'admin_head', 'fix_svg_display_admin' );
add_filter('manage_users_columns', function($columns) {
$columns['ds_email_verified'] = 'verification';
return $columns;
});
add_filter('manage_users_custom_column', function($value, $column_name, $user_id) {
if ($column_name === 'ds_email_verified') {
$status = get_user_meta($user_id, 'ds_email_verified', true);
if ($status === 'yes') {
return '<span style="color:green;font-weight:bold;">Verified</span>';
} elseif ($status === 'no') {
return '<span style="color:red;font-weight:bold;">Not Verified</span>';
} else {
return '<span style="color:#888;">—</span>';
}
}
return $value;
}, 10, 3);
// /**
// * Direct database authentication functions
// */
function dreamsalon_handle_custom_login() {
// Run only on POST
if ( ! isset( $_POST['dreamsalon_login_nonce'] ) ) {
return;
}
// Verify nonce
if (
!isset($_POST['dreamsalon_login_nonce']) ||
!wp_verify_nonce($_POST['dreamsalon_login_nonce'], 'dreamsalon_login_action')
) {
return;
}
$username = sanitize_text_field($_POST['username'] ?? '');
$password = sanitize_text_field($_POST['password'] ?? '');
$remember = isset($_POST['remember']);
// Validation
if (empty($username) || empty($password)) {
wc_add_notice(__('Both fields are required.', 'dreamsalon'), 'error');
return;
}
// Use WordPress built-in authentication
$user = wp_authenticate($username, $password);
if (is_wp_error($user)) {
wc_add_notice($user->get_error_message(), 'error');
return;
}
// Check if account is active
if (!empty($user->user_status) && $user->user_status != 0) {
wc_add_notice(__('Account is not active.', 'dreamsalon'), 'error');
return;
}
// Login user
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID, $remember);
update_user_meta($user->ID, 'last_login', current_time('mysql'));
// Handle redirect
$redirect_to = isset($_POST['redirect_to']) ? esc_url_raw(wp_unslash($_POST['redirect_to'])) : '';
// Fallback redirect if none provided
if (empty($redirect_to)) {
if (in_array('administrator', (array)$user->roles)) {
$redirect_to = admin_url();
} else {
$redirect_to = home_url('/');
}
}
wp_safe_redirect($redirect_to);
exit;
}
add_action('template_redirect', 'dreamsalon_handle_custom_login');
// ... (keep existing code at the top of the file until line 270)
/**
* Filter get_avatar_url to use custom profile image
*/
function dreamsalon_custom_avatar_url($url, $id_or_email, $args) {
$user_id = 0;
// Get user ID from different input types
if (is_numeric($id_or_email)) {
$user_id = absint($id_or_email);
} elseif (is_object($id_or_email)) {
if (!empty($id_or_email->user_id)) {
$user_id = absint($id_or_email->user_id);
} elseif (!empty($id_or_email->ID)) {
$user_id = absint($id_or_email->ID);
} elseif (!empty($id_or_email->comment_author_email)) {
$user = get_user_by('email', $id_or_email->comment_author_email);
$user_id = $user ? $user->ID : 0;
}
} elseif (is_string($id_or_email) && is_email($id_or_email)) {
$user = get_user_by('email', $id_or_email);
$user_id = $user ? $user->ID : 0;
}
// If we have a valid user ID, check for custom avatar
if ($user_id > 0) {
// Try custom_profile_image first (your primary meta key)
$avatar_id = get_user_meta($user_id, 'custom_profile_image', true);
// If not found, try other backup meta keys
if (!$avatar_id) {
$avatar_id = get_user_meta($user_id, 'custom_avatar_id', true);
}
if (!$avatar_id) {
$avatar_id = get_user_meta($user_id, 'wp_user_avatar', true);
}
if (!$avatar_id) {
$avatar_id = get_user_meta($user_id, 'wp_user_profile_picture', true);
}
if (!$avatar_id) {
$avatar_id = get_user_meta($user_id, 'profile_picture', true);
}
// If we have an avatar ID, get the image URL
if ($avatar_id) {
$size = isset($args['size']) ? $args['size'] : 96;
$avatar_url = wp_get_attachment_image_url($avatar_id, array($size, $size));
if ($avatar_url) {
return $avatar_url;
}
}
// Handle simple_local_avatar array format
$simple_avatar = get_user_meta($user_id, 'simple_local_avatar', true);
if (is_array($simple_avatar) && isset($simple_avatar['full'])) {
return $simple_avatar['full'];
}
}
return $url;
}
add_filter('get_avatar_url', 'dreamsalon_custom_avatar_url', 10, 3);
/**
* Filter get_avatar to use custom profile image
*/
function dreamsalon_custom_avatar($avatar, $id_or_email, $size, $default, $alt) {
$user_id = 0;
// Get user ID from different input types
if (is_numeric($id_or_email)) {
$user_id = absint($id_or_email);
} elseif (is_object($id_or_email)) {
if (!empty($id_or_email->user_id)) {
$user_id = absint($id_or_email->user_id);
} elseif (!empty($id_or_email->ID)) {
$user_id = absint($id_or_email->ID);
} elseif (!empty($id_or_email->comment_author_email)) {
$user = get_user_by('email', $id_or_email->comment_author_email);
$user_id = $user ? $user->ID : 0;
}
} elseif (is_string($id_or_email) && is_email($id_or_email)) {
$user = get_user_by('email', $id_or_email);
$user_id = $user ? $user->ID : 0;
}
// If we have a valid user ID and custom avatar, replace the avatar
if ($user_id > 0) {
$avatar_id = get_user_meta($user_id, 'custom_profile_image', true);
// If not found, try other backup meta keys
if (!$avatar_id) {
$avatar_id = get_user_meta($user_id, 'custom_avatar_id', true);
}
if (!$avatar_id) {
$avatar_id = get_user_meta($user_id, 'wp_user_avatar', true);
}
if ($avatar_id) {
$avatar_url = wp_get_attachment_image_url($avatar_id, array($size, $size));
if ($avatar_url) {
$avatar = "<img alt='" . esc_attr($alt) . "' src='" . esc_url($avatar_url) . "' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
}
}
return $avatar;
}
add_filter('get_avatar', 'dreamsalon_custom_avatar', 10, 5);
function format_images_into_grid($content) {
// Pattern to find all img tags
$pattern = '/<img[^>]+>/';
// Find all images in the content
preg_match_all($pattern, $content, $matches);
if (empty($matches[0])) {
return $content;
}
$images = $matches[0];
$image_count = count($images);
// Group images into rows of 3
$image_rows = array_chunk($images, 3);
// Replace each group of images with grid layout
foreach ($image_rows as $row_index => $row_images) {
if (count($row_images) === 3) {
// For exactly 3 images in a row
$replacement = '<!-- start row --><div class="row align-items-center justify-content-center row-gap-3 mb-4">';
foreach ($row_images as $img_index => $img) {
$img_with_style = add_border_radius_to_image($img);
$replacement .= '<div class="col-md-4">' . $img_with_style . '</div><!-- end col -->';
}
$replacement .= '</div><!-- end row -->';
} else {
// For less than 3 images (last row)
$replacement = '<!-- start row --><div class="row align-items-center justify-content-center row-gap-3 mb-4">';
$col_class = 'col-md-' . (12 / count($row_images));
foreach ($row_images as $img_index => $img) {
$img_with_style = add_border_radius_to_image($img);
$replacement .= '<div class="' . $col_class . '">' . $img_with_style . '</div><!-- end col -->';
}
$replacement .= '</div><!-- end row -->';
}
// Create pattern to find this specific group of images
$search_pattern = '/' . preg_quote(implode('', $row_images), '/') . '/';
$content = preg_replace($search_pattern, $replacement, $content, 1);
}
return $content;
}
if ( ! function_exists( 'salon_placeholder_img_src' ) ) {
function salon_placeholder_img_src() {
return get_stylesheet_directory_uri() . '/assets/images/placeholder.webp';
}
}
if (!function_exists('dreamsalon_add_dynamic_styles')) {
function dreamsalon_add_dynamic_styles() {
global $dreamsalon_theme_options;
ob_start();
?>
<?php
$theme_primary_color = !empty( $dreamsalon_theme_options['primary_color'] ) ? $dreamsalon_theme_options['primary_color'] : '';
?>
<?php
if (!empty($theme_primary_color)) {
?>
:root {--dreamsalon_primary_color:<?php echo esc_html( $theme_primary_color );?>;}
<?php }
return ob_get_clean();
}
}
//get options from wp_options for redux
if( !function_exists('dreamsalon_fl_framework_getoptions') )
{
function dreamsalon_fl_framework_getoptions($get_text)
{
global $dreamsalon_theme_options;
if(isset($dreamsalon_theme_options[$get_text]) && $dreamsalon_theme_options[$get_text] !=""):
return $dreamsalon_theme_options[$get_text];
else:
return false;
endif;
}
}
?>