925 lines
30 KiB
PHP
925 lines
30 KiB
PHP
<?php
|
||
/**
|
||
* پیامهای فرم تماس با ما — ذخیره در وردپرس، ایمیل و پیامک اپراتور.
|
||
*
|
||
* @package Hesabix2
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
/** @var string */
|
||
const HESABIX2_CONTACT_POST_TYPE = 'hesabix_contact_msg';
|
||
|
||
/** @var string */
|
||
const HESABIX2_CONTACT_CAPTCHA_NS = 'hesabix2/v1';
|
||
|
||
/** @var int */
|
||
const HESABIX2_CONTACT_CAPTCHA_TTL = 600;
|
||
|
||
/** @var int */
|
||
const HESABIX2_CONTACT_CAPTCHA_LENGTH = 5;
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_messages_bootstrap() {
|
||
hesabix2_contact_register_post_type();
|
||
hesabix2_contact_register_captcha_routes();
|
||
|
||
if ( is_admin() ) {
|
||
hesabix2_contact_admin_init();
|
||
}
|
||
}
|
||
add_action( 'init', 'hesabix2_contact_messages_bootstrap' );
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_register_post_type() {
|
||
register_post_type(
|
||
HESABIX2_CONTACT_POST_TYPE,
|
||
array(
|
||
'labels' => array(
|
||
'name' => __( 'پیامهای تماس', 'hesabix2' ),
|
||
'singular_name' => __( 'پیام تماس', 'hesabix2' ),
|
||
),
|
||
'public' => false,
|
||
'show_ui' => false,
|
||
'show_in_menu' => false,
|
||
'show_in_rest' => false,
|
||
'capability_type' => 'post',
|
||
'map_meta_cap' => true,
|
||
'supports' => array( 'title' ),
|
||
'delete_with_user' => false,
|
||
)
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
function hesabix2_contact_default_settings() {
|
||
return array(
|
||
'notify_email' => 'info@hesabix.ir',
|
||
'sms_enabled' => '0',
|
||
'operator_phone' => '',
|
||
'sms_template' => __( 'پیام جدید تماس با ما از {name}. موضوع: {subject}', 'hesabix2' ),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @param string $key کلید.
|
||
* @return mixed
|
||
*/
|
||
function hesabix2_contact_get_setting( $key ) {
|
||
$defaults = hesabix2_contact_default_settings();
|
||
$option = 'hesabix2_contact_' . $key;
|
||
$value = get_option( $option, isset( $defaults[ $key ] ) ? $defaults[ $key ] : '' );
|
||
return isset( $defaults[ $key ] ) && ( '' === $value || null === $value )
|
||
? $defaults[ $key ]
|
||
: $value;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data فیلدهای فرم.
|
||
* @return true|WP_Error
|
||
*/
|
||
function hesabix2_contact_handle_submission( $data ) {
|
||
if ( empty( $data['hesabix_contact_nonce'] ) ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'درخواست نامعتبر است.', 'hesabix2' ) );
|
||
}
|
||
|
||
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $data['hesabix_contact_nonce'] ) ), 'hesabix_contact' ) ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'خطای امنیتی. لطفاً دوباره تلاش کنید.', 'hesabix2' ) );
|
||
}
|
||
|
||
$captcha_id = isset( $data['captcha_id'] ) ? sanitize_text_field( wp_unslash( $data['captcha_id'] ) ) : '';
|
||
$captcha_code = isset( $data['captcha_code'] ) ? hesabix2_normalize_digits( wp_unslash( $data['captcha_code'] ) ) : '';
|
||
$captcha_ok = hesabix2_contact_verify_captcha( $captcha_id, $captcha_code );
|
||
if ( is_wp_error( $captcha_ok ) ) {
|
||
return $captcha_ok;
|
||
}
|
||
|
||
$name = isset( $data['contact_name'] ) ? sanitize_text_field( wp_unslash( $data['contact_name'] ) ) : '';
|
||
$email = isset( $data['contact_email'] ) ? sanitize_email( wp_unslash( $data['contact_email'] ) ) : '';
|
||
$phone = isset( $data['contact_phone'] ) ? sanitize_text_field( wp_unslash( $data['contact_phone'] ) ) : '';
|
||
$subject = isset( $data['contact_subject'] ) ? sanitize_text_field( wp_unslash( $data['contact_subject'] ) ) : '';
|
||
$message = isset( $data['contact_message'] ) ? sanitize_textarea_field( wp_unslash( $data['contact_message'] ) ) : '';
|
||
|
||
if ( '' === $name || '' === $email || '' === $message ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'نام، ایمیل و پیام الزامی هستند.', 'hesabix2' ) );
|
||
}
|
||
|
||
if ( ! is_email( $email ) ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'لطفاً یک ایمیل معتبر وارد کنید.', 'hesabix2' ) );
|
||
}
|
||
|
||
$post_id = hesabix2_contact_store_message(
|
||
array(
|
||
'name' => $name,
|
||
'email' => $email,
|
||
'phone' => $phone,
|
||
'subject' => $subject,
|
||
'message' => $message,
|
||
)
|
||
);
|
||
|
||
if ( is_wp_error( $post_id ) ) {
|
||
return $post_id;
|
||
}
|
||
|
||
$email_sent = hesabix2_contact_send_email(
|
||
array(
|
||
'name' => $name,
|
||
'email' => $email,
|
||
'phone' => $phone,
|
||
'subject' => $subject,
|
||
'message' => $message,
|
||
)
|
||
);
|
||
update_post_meta( $post_id, '_hesabix_contact_email_sent', $email_sent ? '1' : '0' );
|
||
|
||
$sms_result = hesabix2_contact_send_operator_sms(
|
||
array(
|
||
'name' => $name,
|
||
'email' => $email,
|
||
'phone' => $phone,
|
||
'subject' => $subject,
|
||
'message' => $message,
|
||
)
|
||
);
|
||
if ( is_wp_error( $sms_result ) ) {
|
||
update_post_meta( $post_id, '_hesabix_contact_sms_sent', '0' );
|
||
update_post_meta( $post_id, '_hesabix_contact_sms_error', $sms_result->get_error_message() );
|
||
} elseif ( true === $sms_result ) {
|
||
update_post_meta( $post_id, '_hesabix_contact_sms_sent', '1' );
|
||
delete_post_meta( $post_id, '_hesabix_contact_sms_error' );
|
||
} else {
|
||
update_post_meta( $post_id, '_hesabix_contact_sms_sent', 'skipped' );
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* تبدیل ارقام فارسی/عربی به انگلیسی (فقط رقم).
|
||
*
|
||
* @param string $value مقدار ورودی.
|
||
* @return string
|
||
*/
|
||
function hesabix2_normalize_digits( $value ) {
|
||
$value = (string) $value;
|
||
$from = array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩' );
|
||
$to = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
|
||
|
||
return preg_replace( '/\D+/', '', str_replace( $from, $to, $value ) );
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_register_captcha_routes() {
|
||
add_action( 'rest_api_init', 'hesabix2_contact_rest_register_captcha' );
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_rest_register_captcha() {
|
||
register_rest_route(
|
||
HESABIX2_CONTACT_CAPTCHA_NS,
|
||
'/contact/captcha',
|
||
array(
|
||
'methods' => WP_REST_Server::CREATABLE,
|
||
'permission_callback' => 'hesabix2_contact_captcha_permission',
|
||
'callback' => 'hesabix2_contact_rest_create_captcha',
|
||
)
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @return true|WP_Error
|
||
*/
|
||
function hesabix2_contact_captcha_permission() {
|
||
$nonce = isset( $_SERVER['HTTP_X_WP_NONCE'] )
|
||
? sanitize_text_field( wp_unslash( (string) $_SERVER['HTTP_X_WP_NONCE'] ) )
|
||
: '';
|
||
|
||
if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
|
||
return new WP_Error( 'hesabix_contact_captcha', __( 'درخواست نامعتبر است.', 'hesabix2' ), array( 'status' => 403 ) );
|
||
}
|
||
|
||
$ip = hesabix2_contact_client_ip();
|
||
$key = 'hesabix2_cc_rl_' . md5( $ip );
|
||
$n = (int) get_transient( $key );
|
||
if ( $n >= 30 ) {
|
||
return new WP_Error( 'hesabix_contact_captcha', __( 'تعداد درخواست بیش از حد است. کمی بعد دوباره تلاش کنید.', 'hesabix2' ), array( 'status' => 429 ) );
|
||
}
|
||
set_transient( $key, $n + 1, MINUTE_IN_SECONDS );
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @return string
|
||
*/
|
||
function hesabix2_contact_client_ip() {
|
||
foreach ( array( 'HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR' ) as $key ) {
|
||
if ( empty( $_SERVER[ $key ] ) ) {
|
||
continue;
|
||
}
|
||
$raw = sanitize_text_field( wp_unslash( (string) $_SERVER[ $key ] ) );
|
||
if ( 'HTTP_X_FORWARDED_FOR' === $key ) {
|
||
$raw = trim( explode( ',', $raw )[0] );
|
||
}
|
||
if ( '' !== $raw ) {
|
||
return $raw;
|
||
}
|
||
}
|
||
return 'unknown';
|
||
}
|
||
|
||
/**
|
||
* @return WP_REST_Response|WP_Error
|
||
*/
|
||
function hesabix2_contact_rest_create_captcha() {
|
||
$captcha = hesabix2_contact_captcha_create();
|
||
if ( is_wp_error( $captcha ) ) {
|
||
return $captcha;
|
||
}
|
||
|
||
return new WP_REST_Response(
|
||
array(
|
||
'success' => true,
|
||
'data' => array(
|
||
'captcha_id' => $captcha['id'],
|
||
'image_base64' => base64_encode( $captcha['image'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
|
||
),
|
||
),
|
||
200
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @return array{id: string, image: string}|WP_Error
|
||
*/
|
||
function hesabix2_contact_captcha_create() {
|
||
if ( ! function_exists( 'imagecreatetruecolor' ) ) {
|
||
return new WP_Error( 'hesabix_contact_captcha', __( 'سرویس کد امنیتی در دسترس نیست.', 'hesabix2' ) );
|
||
}
|
||
|
||
$code = '';
|
||
for ( $i = 0; $i < HESABIX2_CONTACT_CAPTCHA_LENGTH; $i++ ) {
|
||
$code .= (string) wp_rand( 0, 9 );
|
||
}
|
||
|
||
$id = 'cc_' . bin2hex( random_bytes( 12 ) );
|
||
hesabix2_contact_captcha_store( $id, $code );
|
||
|
||
$image = hesabix2_contact_captcha_render_image( $code );
|
||
if ( '' === $image ) {
|
||
return new WP_Error( 'hesabix_contact_captcha', __( 'خطا در تولید کد امنیتی.', 'hesabix2' ) );
|
||
}
|
||
|
||
return array(
|
||
'id' => $id,
|
||
'image' => $image,
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @param string $captcha_id شناسه.
|
||
* @param string $code پاسخ.
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_captcha_store( $captcha_id, $code ) {
|
||
set_transient(
|
||
'hesabix2_cc_' . $captcha_id,
|
||
hesabix2_contact_captcha_hash( $captcha_id, $code ),
|
||
HESABIX2_CONTACT_CAPTCHA_TTL
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @param string $captcha_id شناسه.
|
||
* @param string $code پاسخ.
|
||
* @return string
|
||
*/
|
||
function hesabix2_contact_captcha_hash( $captcha_id, $code ) {
|
||
return hash_hmac( 'sha256', $code, wp_salt( 'hesabix2_contact_captcha' ) . '|' . $captcha_id );
|
||
}
|
||
|
||
/**
|
||
* @param string $code کد عددی.
|
||
* @return string PNG binary.
|
||
*/
|
||
function hesabix2_contact_captcha_render_image( $code ) {
|
||
$width = 140;
|
||
$height = 48;
|
||
$img = imagecreatetruecolor( $width, $height );
|
||
if ( ! $img ) {
|
||
return '';
|
||
}
|
||
|
||
$bg = imagecolorallocate( $img, 248, 250, 252 );
|
||
imagefilledrectangle( $img, 0, 0, $width, $height, $bg );
|
||
|
||
for ( $i = 0; $i < 5; $i++ ) {
|
||
$line = imagecolorallocate( $img, wp_rand( 140, 210 ), wp_rand( 140, 210 ), wp_rand( 160, 220 ) );
|
||
imageline( $img, wp_rand( 0, $width ), wp_rand( 0, $height ), wp_rand( 0, $width ), wp_rand( 0, $height ), $line );
|
||
}
|
||
|
||
for ( $i = 0; $i < 40; $i++ ) {
|
||
$dot = imagecolorallocate( $img, wp_rand( 120, 200 ), wp_rand( 120, 200 ), wp_rand( 120, 200 ) );
|
||
imagesetpixel( $img, wp_rand( 0, $width - 1 ), wp_rand( 0, $height - 1 ), $dot );
|
||
}
|
||
|
||
$chars = str_split( $code );
|
||
$count = count( $chars );
|
||
$slot = (int) floor( $width / ( $count + 1 ) );
|
||
$font = 5;
|
||
$font_h = imagefontheight( $font );
|
||
$font_w = imagefontwidth( $font );
|
||
|
||
foreach ( $chars as $index => $char ) {
|
||
$color = imagecolorallocate( $img, wp_rand( 20, 60 ), wp_rand( 20, 60 ), wp_rand( 20, 60 ) );
|
||
$x = ( ( $index + 1 ) * $slot ) - (int) floor( $font_w / 2 );
|
||
$y = (int) floor( ( $height - $font_h ) / 2 ) + wp_rand( -3, 3 );
|
||
imagestring( $img, $font, max( 4, $x ), max( 4, $y ), $char, $color );
|
||
}
|
||
|
||
ob_start();
|
||
imagepng( $img );
|
||
$png = (string) ob_get_clean();
|
||
imagedestroy( $img );
|
||
|
||
return $png;
|
||
}
|
||
|
||
/**
|
||
* بررسی کپچای محلی فرم تماس.
|
||
*
|
||
* @param string $captcha_id شناسه کپچا.
|
||
* @param string $captcha_code کد واردشده.
|
||
* @return true|WP_Error
|
||
*/
|
||
function hesabix2_contact_verify_captcha( $captcha_id, $captcha_code ) {
|
||
$captcha_id = sanitize_text_field( (string) $captcha_id );
|
||
$captcha_code = hesabix2_normalize_digits( $captcha_code );
|
||
|
||
if ( strlen( $captcha_id ) < 8 || strlen( $captcha_code ) < HESABIX2_CONTACT_CAPTCHA_LENGTH ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'کد امنیتی را وارد کنید.', 'hesabix2' ) );
|
||
}
|
||
|
||
$key = 'hesabix2_cc_' . $captcha_id;
|
||
$stored = get_transient( $key );
|
||
delete_transient( $key );
|
||
|
||
if ( ! is_string( $stored ) || '' === $stored ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'کد امنیتی منقضی شده است. لطفاً تصویر را بروزرسانی کنید.', 'hesabix2' ) );
|
||
}
|
||
|
||
$hash = hesabix2_contact_captcha_hash( $captcha_id, $captcha_code );
|
||
if ( ! hash_equals( $stored, $hash ) ) {
|
||
return new WP_Error( 'hesabix_contact', __( 'کد امنیتی نامعتبر است. لطفاً تصویر را بروزرسانی کنید و دوباره تلاش کنید.', 'hesabix2' ) );
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* فیلدهای کپچا در فرم تماس با ما.
|
||
*
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_captcha_fields() {
|
||
?>
|
||
<div class="hesabix2-contact-captcha" data-captcha-wrap>
|
||
<label for="contact_captcha_code" class="block text-sm font-medium text-slate-700 mb-1.5">
|
||
<?php esc_html_e( 'کد امنیتی', 'hesabix2' ); ?> <span class="text-red-500">*</span>
|
||
</label>
|
||
<div class="hesabix2-contact-captcha__row flex items-center gap-2 mb-2">
|
||
<img
|
||
class="hesabix2-contact-captcha__img rounded-xl border border-slate-200 bg-slate-50 min-h-[48px]"
|
||
alt="<?php esc_attr_e( 'کد امنیتی', 'hesabix2' ); ?>"
|
||
data-captcha-img
|
||
width="140"
|
||
height="48"
|
||
/>
|
||
<button
|
||
type="button"
|
||
class="hesabix2-contact-captcha__refresh flex-shrink-0 w-10 h-10 rounded-xl border border-slate-200 bg-slate-50 hover:bg-brand-50 hover:text-brand-600 text-slate-500 transition flex items-center justify-center"
|
||
data-captcha-refresh
|
||
aria-label="<?php esc_attr_e( 'تصویر جدید', 'hesabix2' ); ?>"
|
||
>
|
||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M23 4v6h-6M1 20v-6h6"/><path d="M3.51 9a9 9 0 0114.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0020.49 15"/></svg>
|
||
</button>
|
||
</div>
|
||
<input
|
||
type="text"
|
||
name="captcha_code"
|
||
id="contact_captcha_code"
|
||
class="w-full px-4 py-3 rounded-xl border border-slate-200 focus:border-brand-500 focus:ring-2 focus:ring-brand-500/20 outline-none transition dir-ltr"
|
||
autocomplete="off"
|
||
inputmode="numeric"
|
||
pattern="[0-9]*"
|
||
maxlength="<?php echo (int) HESABIX2_CONTACT_CAPTCHA_LENGTH; ?>"
|
||
required
|
||
dir="ltr"
|
||
data-captcha-code
|
||
placeholder="<?php esc_attr_e( 'اعداد تصویر (فقط عدد)', 'hesabix2' ); ?>"
|
||
value="<?php echo isset( $_POST['captcha_code'] ) ? esc_attr( hesabix2_normalize_digits( wp_unslash( $_POST['captcha_code'] ) ) ) : ''; ?>"
|
||
/>
|
||
<input type="hidden" name="captcha_id" data-captcha-id value="" />
|
||
</div>
|
||
<?php
|
||
}
|
||
|
||
/**
|
||
* @param array<string, string> $fields فیلدها.
|
||
* @return int|WP_Error
|
||
*/
|
||
function hesabix2_contact_store_message( $fields ) {
|
||
$title = $fields['subject']
|
||
? sprintf( __( 'پیام از %1$s — %2$s', 'hesabix2' ), $fields['name'], $fields['subject'] )
|
||
: sprintf( __( 'پیام از %s', 'hesabix2' ), $fields['name'] );
|
||
|
||
$post_id = wp_insert_post(
|
||
array(
|
||
'post_type' => HESABIX2_CONTACT_POST_TYPE,
|
||
'post_status' => 'private',
|
||
'post_title' => $title,
|
||
'post_content' => $fields['message'],
|
||
),
|
||
true
|
||
);
|
||
|
||
if ( is_wp_error( $post_id ) ) {
|
||
return $post_id;
|
||
}
|
||
|
||
update_post_meta( $post_id, '_hesabix_contact_name', $fields['name'] );
|
||
update_post_meta( $post_id, '_hesabix_contact_email', $fields['email'] );
|
||
update_post_meta( $post_id, '_hesabix_contact_phone', $fields['phone'] );
|
||
update_post_meta( $post_id, '_hesabix_contact_subject', $fields['subject'] );
|
||
update_post_meta( $post_id, '_hesabix_contact_status', 'new' );
|
||
update_post_meta( $post_id, '_hesabix_contact_ip', isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '' );
|
||
|
||
return (int) $post_id;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, string> $fields فیلدها.
|
||
* @return bool
|
||
*/
|
||
function hesabix2_contact_send_email( $fields ) {
|
||
$to = (string) hesabix2_contact_get_setting( 'notify_email' );
|
||
if ( '' === $to || ! is_email( $to ) ) {
|
||
$to = 'info@hesabix.ir';
|
||
}
|
||
|
||
$body = "نام و نام خانوادگی: {$fields['name']}\n";
|
||
$body .= "ایمیل: {$fields['email']}\n";
|
||
if ( '' !== $fields['phone'] ) {
|
||
$body .= "شماره تماس: {$fields['phone']}\n";
|
||
}
|
||
$body .= "موضوع: {$fields['subject']}\n\n";
|
||
$body .= "پیام:\n{$fields['message']}";
|
||
|
||
$mail_subject = $fields['subject']
|
||
? '[تماس با ما] ' . $fields['subject']
|
||
: '[تماس با ما] پیام جدید';
|
||
|
||
$headers = array(
|
||
'Content-Type: text/plain; charset=UTF-8',
|
||
'From: ' . get_bloginfo( 'name' ) . ' <' . get_option( 'admin_email' ) . '>',
|
||
'Reply-To: ' . $fields['name'] . ' <' . $fields['email'] . '>',
|
||
);
|
||
|
||
return (bool) wp_mail( $to, $mail_subject, $body, $headers );
|
||
}
|
||
|
||
/**
|
||
* @param array<string, string> $fields فیلدها.
|
||
* @return true|false|WP_Error false = غیرفعال یا پیکربندی نشده.
|
||
*/
|
||
function hesabix2_contact_send_operator_sms( $fields ) {
|
||
if ( '1' !== (string) hesabix2_contact_get_setting( 'sms_enabled' ) ) {
|
||
return false;
|
||
}
|
||
|
||
$operator_phone = (string) hesabix2_contact_get_setting( 'operator_phone' );
|
||
if ( '' === $operator_phone ) {
|
||
return new WP_Error( 'hesabix_contact_sms', __( 'شماره اپراتور تنظیم نشده است.', 'hesabix2' ) );
|
||
}
|
||
|
||
if ( ! class_exists( 'Hpauth_Behin_SMS' ) || ! Hpauth_Behin_SMS::is_configured() ) {
|
||
return new WP_Error( 'hesabix_contact_sms', __( 'تنظیمات پیامک بهین (پورتال مشتریان) کامل نیست.', 'hesabix2' ) );
|
||
}
|
||
|
||
$template = (string) hesabix2_contact_get_setting( 'sms_template' );
|
||
$text = hesabix2_contact_render_sms_template( $template, $fields );
|
||
|
||
return Hpauth_Behin_SMS::send( $operator_phone, $text );
|
||
}
|
||
|
||
/**
|
||
* @param string $template الگو.
|
||
* @param array<string, string> $fields فیلدها.
|
||
* @return string
|
||
*/
|
||
function hesabix2_contact_render_sms_template( $template, $fields ) {
|
||
$snippet = wp_html_excerpt( $fields['message'], 80, '…' );
|
||
|
||
$replacements = array(
|
||
'{name}' => $fields['name'],
|
||
'{email}' => $fields['email'],
|
||
'{phone}' => '' !== $fields['phone'] ? $fields['phone'] : '—',
|
||
'{subject}' => '' !== $fields['subject'] ? $fields['subject'] : '—',
|
||
'{message}' => $fields['message'],
|
||
'{message_snippet}' => $snippet,
|
||
'{site}' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
|
||
);
|
||
|
||
return strtr( $template, $replacements );
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_admin_init() {
|
||
add_action( 'admin_menu', 'hesabix2_contact_admin_menu' );
|
||
add_action( 'admin_init', 'hesabix2_contact_register_settings' );
|
||
add_action( 'admin_post_hesabix2_contact_mark_read', 'hesabix2_contact_handle_mark_read' );
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_admin_menu() {
|
||
add_menu_page(
|
||
__( 'پیامهای تماس', 'hesabix2' ),
|
||
__( 'پیامهای تماس', 'hesabix2' ),
|
||
'manage_options',
|
||
'hesabix2-contact-messages',
|
||
'hesabix2_contact_render_list_page',
|
||
'dashicons-email-alt',
|
||
26
|
||
);
|
||
|
||
add_submenu_page(
|
||
'hesabix2-contact-messages',
|
||
__( 'لیست پیامها', 'hesabix2' ),
|
||
__( 'لیست پیامها', 'hesabix2' ),
|
||
'manage_options',
|
||
'hesabix2-contact-messages',
|
||
'hesabix2_contact_render_list_page'
|
||
);
|
||
|
||
add_submenu_page(
|
||
'hesabix2-contact-messages',
|
||
__( 'تنظیمات تماس', 'hesabix2' ),
|
||
__( 'تنظیمات', 'hesabix2' ),
|
||
'manage_options',
|
||
'hesabix2-contact-settings',
|
||
'hesabix2_contact_render_settings_page'
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_register_settings() {
|
||
register_setting(
|
||
'hesabix2_contact_settings',
|
||
'hesabix2_contact_notify_email',
|
||
array(
|
||
'type' => 'string',
|
||
'sanitize_callback' => 'sanitize_email',
|
||
)
|
||
);
|
||
register_setting(
|
||
'hesabix2_contact_settings',
|
||
'hesabix2_contact_sms_enabled',
|
||
array(
|
||
'type' => 'string',
|
||
'sanitize_callback' => static function ( $val ) {
|
||
return $val ? '1' : '';
|
||
},
|
||
)
|
||
);
|
||
register_setting(
|
||
'hesabix2_contact_settings',
|
||
'hesabix2_contact_operator_phone',
|
||
array(
|
||
'type' => 'string',
|
||
'sanitize_callback' => 'sanitize_text_field',
|
||
)
|
||
);
|
||
register_setting(
|
||
'hesabix2_contact_settings',
|
||
'hesabix2_contact_sms_template',
|
||
array(
|
||
'type' => 'string',
|
||
'sanitize_callback' => 'sanitize_textarea_field',
|
||
)
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_handle_mark_read() {
|
||
if ( ! current_user_can( 'manage_options' ) ) {
|
||
wp_die( esc_html__( 'دسترسی غیرمجاز.', 'hesabix2' ) );
|
||
}
|
||
|
||
check_admin_referer( 'hesabix2_contact_mark_read' );
|
||
|
||
$post_id = isset( $_GET['message_id'] ) ? absint( $_GET['message_id'] ) : 0;
|
||
if ( $post_id && HESABIX2_CONTACT_POST_TYPE === get_post_type( $post_id ) ) {
|
||
update_post_meta( $post_id, '_hesabix_contact_status', 'read' );
|
||
}
|
||
|
||
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=hesabix2-contact-messages' ) );
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_render_list_page() {
|
||
if ( ! current_user_can( 'manage_options' ) ) {
|
||
return;
|
||
}
|
||
|
||
$view_id = isset( $_GET['message_id'] ) ? absint( $_GET['message_id'] ) : 0;
|
||
if ( $view_id && HESABIX2_CONTACT_POST_TYPE === get_post_type( $view_id ) ) {
|
||
hesabix2_contact_render_detail_page( $view_id );
|
||
return;
|
||
}
|
||
|
||
$paged = max( 1, absint( $_GET['paged'] ?? 1 ) );
|
||
$status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
|
||
|
||
$meta_query = array();
|
||
if ( in_array( $status, array( 'new', 'read' ), true ) ) {
|
||
$meta_query[] = array(
|
||
'key' => '_hesabix_contact_status',
|
||
'value' => $status,
|
||
);
|
||
}
|
||
|
||
$query = new WP_Query(
|
||
array(
|
||
'post_type' => HESABIX2_CONTACT_POST_TYPE,
|
||
'post_status' => 'private',
|
||
'posts_per_page' => 20,
|
||
'paged' => $paged,
|
||
'orderby' => 'date',
|
||
'order' => 'DESC',
|
||
'meta_query' => $meta_query,
|
||
)
|
||
);
|
||
|
||
$base_url = admin_url( 'admin.php?page=hesabix2-contact-messages' );
|
||
?>
|
||
<div class="wrap">
|
||
<h1 class="wp-heading-inline"><?php esc_html_e( 'پیامهای تماس با ما', 'hesabix2' ); ?></h1>
|
||
<hr class="wp-header-end" />
|
||
|
||
<ul class="subsubsub">
|
||
<li><a href="<?php echo esc_url( $base_url ); ?>" <?php echo '' === $status ? 'class="current"' : ''; ?>><?php esc_html_e( 'همه', 'hesabix2' ); ?></a> |</li>
|
||
<li><a href="<?php echo esc_url( add_query_arg( 'status', 'new', $base_url ) ); ?>" <?php echo 'new' === $status ? 'class="current"' : ''; ?>><?php esc_html_e( 'خواندهنشده', 'hesabix2' ); ?></a> |</li>
|
||
<li><a href="<?php echo esc_url( add_query_arg( 'status', 'read', $base_url ) ); ?>" <?php echo 'read' === $status ? 'class="current"' : ''; ?>><?php esc_html_e( 'خواندهشده', 'hesabix2' ); ?></a></li>
|
||
</ul>
|
||
|
||
<table class="wp-list-table widefat fixed striped">
|
||
<thead>
|
||
<tr>
|
||
<th scope="col"><?php esc_html_e( 'تاریخ', 'hesabix2' ); ?></th>
|
||
<th scope="col"><?php esc_html_e( 'نام', 'hesabix2' ); ?></th>
|
||
<th scope="col"><?php esc_html_e( 'ایمیل', 'hesabix2' ); ?></th>
|
||
<th scope="col"><?php esc_html_e( 'موضوع', 'hesabix2' ); ?></th>
|
||
<th scope="col"><?php esc_html_e( 'وضعیت', 'hesabix2' ); ?></th>
|
||
<th scope="col"><?php esc_html_e( 'ایمیل / پیامک', 'hesabix2' ); ?></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php if ( $query->have_posts() ) : ?>
|
||
<?php
|
||
while ( $query->have_posts() ) :
|
||
$query->the_post();
|
||
$id = get_the_ID();
|
||
$msg_status = (string) get_post_meta( $id, '_hesabix_contact_status', true );
|
||
$email_ok = '1' === (string) get_post_meta( $id, '_hesabix_contact_email_sent', true );
|
||
$sms_sent = (string) get_post_meta( $id, '_hesabix_contact_sms_sent', true );
|
||
?>
|
||
<tr>
|
||
<td>
|
||
<a href="<?php echo esc_url( add_query_arg( 'message_id', $id, $base_url ) ); ?>">
|
||
<?php echo esc_html( get_the_date( 'Y/m/d H:i' ) ); ?>
|
||
</a>
|
||
</td>
|
||
<td><?php echo esc_html( (string) get_post_meta( $id, '_hesabix_contact_name', true ) ); ?></td>
|
||
<td dir="ltr"><?php echo esc_html( (string) get_post_meta( $id, '_hesabix_contact_email', true ) ); ?></td>
|
||
<td><?php echo esc_html( (string) get_post_meta( $id, '_hesabix_contact_subject', true ) ); ?></td>
|
||
<td>
|
||
<?php
|
||
echo 'read' === $msg_status
|
||
? esc_html__( 'خواندهشده', 'hesabix2' )
|
||
: esc_html__( 'جدید', 'hesabix2' );
|
||
?>
|
||
</td>
|
||
<td>
|
||
<?php
|
||
echo $email_ok ? '✉️' : '✉️❌';
|
||
if ( '1' === $sms_sent ) {
|
||
echo ' 📱';
|
||
} elseif ( '0' === $sms_sent ) {
|
||
echo ' 📱❌';
|
||
}
|
||
?>
|
||
</td>
|
||
</tr>
|
||
<?php endwhile; ?>
|
||
<?php else : ?>
|
||
<tr>
|
||
<td colspan="6"><?php esc_html_e( 'پیامی یافت نشد.', 'hesabix2' ); ?></td>
|
||
</tr>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table>
|
||
|
||
<?php
|
||
$total_pages = (int) $query->max_num_pages;
|
||
if ( $total_pages > 1 ) {
|
||
echo '<div class="tablenav"><div class="tablenav-pages">';
|
||
echo paginate_links(
|
||
array(
|
||
'base' => add_query_arg( 'paged', '%#%', $base_url ),
|
||
'format' => '',
|
||
'current' => $paged,
|
||
'total' => $total_pages,
|
||
)
|
||
);
|
||
echo '</div></div>';
|
||
}
|
||
wp_reset_postdata();
|
||
?>
|
||
</div>
|
||
<?php
|
||
}
|
||
|
||
/**
|
||
* @param int $post_id شناسه پیام.
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_render_detail_page( $post_id ) {
|
||
$post = get_post( $post_id );
|
||
if ( ! $post || HESABIX2_CONTACT_POST_TYPE !== $post->post_type ) {
|
||
return;
|
||
}
|
||
|
||
$status = (string) get_post_meta( $post_id, '_hesabix_contact_status', true );
|
||
$email_ok = '1' === (string) get_post_meta( $post_id, '_hesabix_contact_email_sent', true );
|
||
$sms_sent = (string) get_post_meta( $post_id, '_hesabix_contact_sms_sent', true );
|
||
$sms_error = (string) get_post_meta( $post_id, '_hesabix_contact_sms_error', true );
|
||
$list_url = admin_url( 'admin.php?page=hesabix2-contact-messages' );
|
||
|
||
if ( 'read' !== $status ) {
|
||
update_post_meta( $post_id, '_hesabix_contact_status', 'read' );
|
||
}
|
||
?>
|
||
<div class="wrap">
|
||
<h1>
|
||
<a href="<?php echo esc_url( $list_url ); ?>" class="page-title-action"><?php esc_html_e( 'بازگشت به لیست', 'hesabix2' ); ?></a>
|
||
<?php echo esc_html( get_the_title( $post ) ); ?>
|
||
</h1>
|
||
|
||
<table class="form-table" role="presentation">
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'تاریخ', 'hesabix2' ); ?></th>
|
||
<td><?php echo esc_html( get_the_date( 'Y/m/d H:i', $post ) ); ?></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'نام', 'hesabix2' ); ?></th>
|
||
<td><?php echo esc_html( (string) get_post_meta( $post_id, '_hesabix_contact_name', true ) ); ?></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'ایمیل', 'hesabix2' ); ?></th>
|
||
<td dir="ltr"><a href="mailto:<?php echo esc_attr( (string) get_post_meta( $post_id, '_hesabix_contact_email', true ) ); ?>"><?php echo esc_html( (string) get_post_meta( $post_id, '_hesabix_contact_email', true ) ); ?></a></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'تلفن', 'hesabix2' ); ?></th>
|
||
<td dir="ltr"><?php echo esc_html( (string) get_post_meta( $post_id, '_hesabix_contact_phone', true ) ); ?></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'موضوع', 'hesabix2' ); ?></th>
|
||
<td><?php echo esc_html( (string) get_post_meta( $post_id, '_hesabix_contact_subject', true ) ); ?></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'پیام', 'hesabix2' ); ?></th>
|
||
<td><div style="max-width:40rem;white-space:pre-wrap;"><?php echo esc_html( $post->post_content ); ?></div></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'ارسال ایمیل', 'hesabix2' ); ?></th>
|
||
<td><?php echo $email_ok ? esc_html__( 'موفق', 'hesabix2' ) : esc_html__( 'ناموفق', 'hesabix2' ); ?></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'ارسال پیامک', 'hesabix2' ); ?></th>
|
||
<td>
|
||
<?php
|
||
if ( '1' === $sms_sent ) {
|
||
esc_html_e( 'موفق', 'hesabix2' );
|
||
} elseif ( '0' === $sms_sent ) {
|
||
esc_html_e( 'ناموفق', 'hesabix2' );
|
||
if ( '' !== $sms_error ) {
|
||
echo ' — ' . esc_html( $sms_error );
|
||
}
|
||
} else {
|
||
esc_html_e( 'غیرفعال یا ارسال نشده', 'hesabix2' );
|
||
}
|
||
?>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
</div>
|
||
<?php
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
function hesabix2_contact_render_settings_page() {
|
||
if ( ! current_user_can( 'manage_options' ) ) {
|
||
return;
|
||
}
|
||
|
||
$behin_ok = class_exists( 'Hpauth_Behin_SMS' ) && Hpauth_Behin_SMS::is_configured();
|
||
$portal_settings_url = admin_url( 'options-general.php?page=hpauth-settings' );
|
||
?>
|
||
<div class="wrap">
|
||
<h1><?php esc_html_e( 'تنظیمات فرم تماس با ما', 'hesabix2' ); ?></h1>
|
||
|
||
<?php if ( ! $behin_ok ) : ?>
|
||
<div class="notice notice-warning">
|
||
<p>
|
||
<?php
|
||
printf(
|
||
/* translators: %s: settings page URL */
|
||
esc_html__( 'برای ارسال پیامک، ابتدا تنظیمات بهین SMS را در %s تکمیل کنید.', 'hesabix2' ),
|
||
'<a href="' . esc_url( $portal_settings_url ) . '">' . esc_html__( 'پورتال مشتریان', 'hesabix2' ) . '</a>'
|
||
);
|
||
?>
|
||
</p>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<form method="post" action="options.php">
|
||
<?php settings_fields( 'hesabix2_contact_settings' ); ?>
|
||
<table class="form-table" role="presentation">
|
||
<tr>
|
||
<th scope="row"><label for="hesabix2_contact_notify_email"><?php esc_html_e( 'ایمیل دریافت پیام', 'hesabix2' ); ?></label></th>
|
||
<td>
|
||
<input name="hesabix2_contact_notify_email" id="hesabix2_contact_notify_email" type="email" class="regular-text" dir="ltr"
|
||
value="<?php echo esc_attr( (string) hesabix2_contact_get_setting( 'notify_email' ) ); ?>" />
|
||
<p class="description"><?php esc_html_e( 'پس از ثبت فرم، یک ایمیل به این آدرس ارسال میشود.', 'hesabix2' ); ?></p>
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><?php esc_html_e( 'پیامک به اپراتور', 'hesabix2' ); ?></th>
|
||
<td>
|
||
<label>
|
||
<input type="checkbox" name="hesabix2_contact_sms_enabled" value="1"
|
||
<?php checked( hesabix2_contact_get_setting( 'sms_enabled' ), '1' ); ?> />
|
||
<?php esc_html_e( 'با هر پیام جدید، پیامک به اپراتور ارسال شود', 'hesabix2' ); ?>
|
||
</label>
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><label for="hesabix2_contact_operator_phone"><?php esc_html_e( 'شماره موبایل اپراتور', 'hesabix2' ); ?></label></th>
|
||
<td>
|
||
<input name="hesabix2_contact_operator_phone" id="hesabix2_contact_operator_phone" type="text" class="regular-text" dir="ltr"
|
||
value="<?php echo esc_attr( (string) hesabix2_contact_get_setting( 'operator_phone' ) ); ?>"
|
||
placeholder="09123456789" />
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row"><label for="hesabix2_contact_sms_template"><?php esc_html_e( 'متن پیامک', 'hesabix2' ); ?></label></th>
|
||
<td>
|
||
<textarea name="hesabix2_contact_sms_template" id="hesabix2_contact_sms_template" rows="4" class="large-text"><?php echo esc_textarea( (string) hesabix2_contact_get_setting( 'sms_template' ) ); ?></textarea>
|
||
<p class="description">
|
||
<?php esc_html_e( 'متغیرهای قابل استفاده:', 'hesabix2' ); ?>
|
||
<code>{name}</code>, <code>{email}</code>, <code>{phone}</code>, <code>{subject}</code>, <code>{message}</code>, <code>{message_snippet}</code>, <code>{site}</code>
|
||
</p>
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
<?php submit_button(); ?>
|
||
</form>
|
||
</div>
|
||
<?php
|
||
}
|