Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hamidrezayazdani/f9d88801bc1178fb2a7c10a969990991 to your computer and use it in GitHub Desktop.
Save hamidrezayazdani/f9d88801bc1178fb2a7c10a969990991 to your computer and use it in GitHub Desktop.
This snippet convert persian/arabic numbers to english numbers in metadata
<?php
/**
* Convert numbers to english
*
* @param $input
*
* @return array|string
*/
function ywp_convert_numbers_to_latin( $input ): array|string {
$arabic = [ '٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠' ];
$persian = [ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ];
$english = [ '9', '8', '7', '6', '5', '4', '3', '2', '1', '0' ];
$convertedPersianNums = str_replace( $persian, $english, $input );
return str_replace( $arabic, $english, $convertedPersianNums );
}
/**
* Filter hook on saving user metadata
*
* @param string $meta_value
* @param mixed $meta_key
* @param string $object_type
*
* @return mixed
*/
function ywp_convert_user_and_post_metadata_numbers_to_latin( mixed $meta_value, string $meta_key, string $object_type ): mixed {
return ywp_convert_numbers_to_latin( $meta_value );
}
$user_meta_keys = array(
'billing_phone',
'billing_postal_code',
'billing_address',
);
foreach ( $user_meta_keys as $meta_key ) {
add_filter( "sanitize_user_meta_$meta_key", 'ywp_convert_user_and_post_metadata_numbers_to_latin', 10, 3 );
}
$post_meta_keys = array(
'_billing_phone',
'_customer_mobile',
);
foreach ( $post_meta_keys as $meta_key ) {
add_filter( "sanitize_post_meta_$meta_key", 'ywp_convert_user_and_post_metadata_numbers_to_latin', 10, 3 );
}
// Note: This code is compatible with php 8
@vahid162
Copy link

vahid162 commented Aug 2, 2021

/قسمت (اول) کانورت ارور سینتکسی میداد که با این تکه کد عوضش کردم/

function ywp_convert_numbers_to_latin($input) {
$newNumbers = range(0, 9);
// 1. Persian HTML decimal
$persianDecimal = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
// 2. Arabic HTML decimal
$arabicDecimal = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
// 3. Arabic Numeric
$arabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
// 4. Persian Numeric
$persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');

$input =  str_replace($persianDecimal, $newNumbers, $input);
$input =  str_replace($arabicDecimal, $newNumbers, $input);
$input =  str_replace($arabic, $newNumbers, $input);
return str_replace($persian, $newNumbers, $input);

}

/در مورد بقیه موراد هم عمل میکرد ولی اتفاقی که می افتاد بعد ذخیره کردن پیام اخطار مهمی در این وب سایت رخ داده است ظاهر می شد مه بعد از رفرش اعمال شده بود. برای این مورد میشه چیکار کرد؟/

function ywp_convert_user_and_post_metadata_numbers_to_latin( mixed $meta_value, string $meta_key, string $object_type ): mixed {
return ywp_convert_numbers_to_latin( $meta_value );
}

$user_meta_keys = array(
'billing_phone',
'billing_postal_code',
'billing_address',
);

foreach ( $user_meta_keys as $meta_key ) {
add_filter( "sanitize_user_meta_$meta_key", 'ywp_convert_user_and_post_metadata_numbers_to_latin', 10, 3 );
}

$post_meta_keys = array(
'_billing_phone',
'_customer_mobile',
);

foreach ( $post_meta_keys as $meta_key ) {
add_filter( "sanitize_post_meta_$meta_key", 'ywp_convert_user_and_post_metadata_numbers_to_latin', 10, 3 );
}

@vahid162
Copy link

vahid162 commented Aug 4, 2021

من دارم با این تکه کد، شماره رو توی حساب کاربری میگیرم و ذخیره میکنم:

// نمایش شماره تلفن در قسمت حساب کاربری add_action( 'woocommerce_edit_account_form', 'add_billing_phone_to_edit_account_form' ); // After existing fields function add_billing_phone_to_edit_account_form() { $user = wp_get_current_user(); ?> <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> <label for="billing_phone"><?php _e( 'شماره موبایل', 'woocommerce' ); ?> <span class="required">*</span></label> <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" /> </p> <?php } // Check and validate the mobile phone add_action( 'woocommerce_save_account_details_errors','billing_phone_field_validation', 20, 1 ); function billing_phone_field_validation( $args ){ if ( isset($_POST['billing_phone']) && empty($_POST['billing_phone']) ) $args->add( 'error', __( 'Please fill in your Mobile phone', 'woocommerce' ),''); } // Save the mobile phone value to user data add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_phone', 20, 1 ); function my_account_saving_billing_phone( $user_id ) { if( isset($_POST['billing_phone']) && ! empty($_POST['billing_phone']) ) update_user_meta( $user_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']) ); }

ولی وقتی کد شما رو میزارم، توی دیباگ لاگ این خطا رو میده:

called in /home/upanteir/domains/gpante.com/public_html/wp-includes/class-wp-hook.php on line 303 and defined in /home/upanteir/domains/gpante.com/public_html/wp-content/themes/woodmart-child/functions.php:359
Stack trace:
#0 /home/upanteir/domains/gpante.com/public_html/wp-includes/class-wp-hook.php(303): ywp_convert_user_and_post_metadata_numbers_to_latin('\xDB\xB1\xDB\xB2\xDB\xB3\xDB\xB4\xDB\xB1\xDB\xB2\xDB\xB3\xDB...', 'billing_phone', 'user')
#1 /home/upanteir/domains/gpante.com/public_html/wp-includes/plugin.php(189): WP_Hook->apply_filters('\xDB\xB1\xDB\xB2\xDB\xB3\xDB\xB4\xDB\xB1\xDB\xB2\xDB\xB3\xDB...', Array)
#2 /home/upanteir/domains/gpante.com/public_html/wp-includes/meta.php(1233): apply_filters('sanitize_user_m...', '\xDB\xB1\xDB\xB2\xDB\xB3\xDB\xB4\xDB\xB1\xDB\xB2\xDB\xB3\xDB...', 'billing_phone', 'user')
#3 /home/upanteir/domains/gpante.com/public_html/wp-i in /home/upanteir/domains/gpante.com/public_html/wp-content/themes/woodmart-child/functions.php on line 359

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment