File: /home/salamatk/takarzan.ir/wp-content/plugins/luman-plus/Includes/Core/Helper.php
<?php
namespace lumanPlus\Core;
class Helper
{
public static function asset (string $path) :string
{
return LPL_ASSETS_URL . $path;
}
public static function view (string $file,...$variables): void
{
$path = str_replace('.','/',$file) . '.php';
$path = LPL_FRONT_DIR . $path;
if ( file_exists($path) ) {
if (!empty($variables)) {
extract($variables[0], EXTR_SKIP);
}
include_once $path;
}
}
public static function dd (...$data)
{
echo '<pre>';
var_dump(...$data);
echo '</pre>';
wp_die();
}
public static function httpRequest ($url , $method , $data = [] , array $headers = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($data)) {
$data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public static function getOption($mainKey, $default = null)
{
global $wpdb;
$row = $wpdb->get_row(
$wpdb->prepare("SELECT meta_value FROM wp_luman_plus_login_settings WHERE meta_key = %s", $mainKey),
ARRAY_A
);
if (!$row) {
return $default;
}
return $row['meta_value'] ?? $default;
}
public static function setOption($key, $params)
{
global $wpdb;
$table = 'wp_luman_plus_login_settings';
$exists = $wpdb->get_var(
$wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE meta_key = %s", $key)
);
if (is_array($params) || is_object($params)) {
$params = wp_json_encode($params);
}
if ($exists) {
// update
$result = $wpdb->update(
$table,
['meta_value' => $params],
['meta_key' => $key],
['%s'],
['%s']
);
} else {
$result = $wpdb->insert(
$table,
[
'meta_key' => $key,
'meta_value' => $params
],
['%s', '%s']
);
}
return $result !== false;
}
public static function persianNumToEng($string)
{
return strtr($string, [
'۰' => '0', '١' => '1', '۱' => '1',
'۲' => '2', '٢' => '2',
'۳' => '3', '٣' => '3',
'۴' => '4', '٤' => '4',
'۵' => '5', '٥' => '5',
'۶' => '6', '٦' => '6',
'۷' => '7', '٧' => '7',
'۸' => '8', '٨' => '8',
'۹' => '9', '٩' => '9'
]);
}
public static function normalizeMobile($mobile)
{
$mobile = trim($mobile);
$mobile = self::persianNumToEng($mobile);
$mobile = str_replace([' ', '-', '(', ')'], '', $mobile);
if (strpos($mobile, '+98') === 0) {
$mobile = '0' . substr($mobile, 3);
}
if (strpos($mobile, '0098') === 0) {
$mobile = '0' . substr($mobile, 4);
}
if (strpos($mobile, '98') === 0 && strlen($mobile) === 12) {
$mobile = '0' . substr($mobile, 2);
}
if (strlen($mobile) === 10 && preg_match('/^9\d{9}$/', $mobile)) {
$mobile = '0' . $mobile;
}
if (preg_match('/^09\d{9}$/', $mobile)) {
return $mobile;
}
return false;
}
public static function generateCode (int $length)
{
$min = (int) str_pad('1', $length, '0');
$max = (int) str_pad('', $length, '9');
return rand($min, $max);
}
/**
* Generate a strong random password
*
* @param int $length Password length (default: 16)
* @return string Generated password
*/
public static function generateStrongPassword(int $length = 16): string
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
$password = '';
// Ensure at least one of each type
$password .= substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 1); // lowercase
$password .= substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 1); // uppercase
$password .= substr(str_shuffle('0123456789'), 0, 1); // number
$password .= substr(str_shuffle('!@#$%^&*'), 0, 1); // special
// Fill the rest randomly
for ($i = strlen($password); $i < $length; $i++) {
$password .= $chars[random_int(0, strlen($chars) - 1)];
}
// Shuffle to avoid predictable pattern
return str_shuffle($password);
}
/**
* Generate a unique dummy email for users without email
* This creates an email that cannot be replicated by others
*
* @param string $mobile Mobile number (optional, for uniqueness)
* @return string Unique dummy email
*/
public static function generateUniqueDummyEmail(string $mobile = ''): string
{
// استفاده از timestamp + random hash برای ایجاد ایمیل منحصر به فرد
$timestamp = time();
$random = wp_generate_password(16, false);
$unique_string = $timestamp . '_' . $random;
// اگر موبایل وجود دارد، برای اضافی بودن unique استفاده میکنیم اما آن را در ایمیل نشان نمیدهیم
if (!empty($mobile)) {
// استفاده از hash کوچک از موبایل + random برای اطمینان از uniqueness
$mobile_hash = substr(md5($mobile . $timestamp . $random), 0, 8);
$unique_string = $timestamp . '_' . $mobile_hash . '_' . substr($random, 0, 8);
}
// استفاده از .local که یک TLD غیرقابل ثبت است
return 'user_' . $unique_string . '@noemail.local';
}
public static function config (string $config, string $key , $default = null)
{
}
/**
* Get register form field settings
*
* @return array
*/
public static function getRegisterFormSettings(): array
{
return [
'email_field' => Helper::getOption('register_email_field', 'required'),
'password_field' => Helper::getOption('register_password_field', 'show'),
];
}
/**
* Check if email field should be shown in register form
*
* @return bool
*/
public static function shouldShowEmailField(): bool
{
$setting = Helper::getOption('register_email_field', 'required');
return $setting !== 'hidden';
}
/**
* Check if email field is required in register form
*
* @return bool
*/
public static function isEmailFieldRequired(): bool
{
$setting = Helper::getOption('register_email_field', 'required');
return $setting === 'required';
}
/**
* Check if password field should be shown in register form
*
* @return bool
*/
public static function shouldShowPasswordField(): bool
{
$setting = Helper::getOption('register_password_field', 'show');
return $setting === 'show';
}
}