duct_id );
return str_replace( [ '
', '
', '
', ' ' ], [ '', '', '', ' ' ], str_replace( array_keys( $tags ), array_values( $tags ), $content ) );
}
public function product_sale_price_time( $product, $type = '' ) {
if ( is_numeric( $product ) ) {
$product_id = $product;
} else {
$product_id = $this->product_ID( $product );
}
$product = wc_get_product( $product_id );
if ( ! PWSMS()->is_wc_product( $product ) ) {
return '';
}
$timestamp = '';
$method = 'get_date_on_sale_' . $type;
if ( method_exists( $product, $method ) ) {
$timestamp = $product->$method();
if ( is_object( $timestamp ) && method_exists( $timestamp, 'getOffsetTimestamp' ) ) {
$timestamp = $timestamp->getOffsetTimestamp();
}
}
if ( empty( $timestamp ) ) {
$timestamp = $product->get_meta( '_sale_price_dates_' . $type, true );
}
return $timestamp;
}
public function product_admin_mobiles( $product_ids, $status = '' ) {
$product_ids = array_unique( (array) $product_ids );
$mobiles = [];
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( ! PWSMS()->is_wc_product( $product ) ) {
return '';
}
$product_admin = (array) $product->get_meta( '_pwoosms_product_admin_data', true );
$product_admin[] = $this->user_mobile_meta( $product_id );
$product_admin[] = $this->get_post_mobile_meta( $product_id );
$product_admin = array_filter( $product_admin );
foreach ( (array) $product_admin as $data ) {
if ( ! empty( $data['mobile'] ) && ! empty( $data['statuses'] ) && $this->validate_mobile( $data['mobile'] ) ) {
$statuses = $this->prepare_admin_product_status( $data['statuses'] );
if ( empty( $status ) || in_array( $status, $statuses ) ) {
$_mobiles = array_map( 'trim', explode( ',', $data['mobile'] ) );
foreach ( $_mobiles as $_mobile ) {
$mobiles[ $_mobile ][] = $product_id;
}
}
}
}
}
return $mobiles;
}
public function user_mobile_meta( $post_id = 0 ) {
$meta = 'user';
$empty_array = [ 'meta' => $meta, 'mobile' => '', 'statuses' => '' ];
$data = $this->get_saved_mobile_data( $meta, $post_id, $empty_array );
if ( is_array( $data ) ) {
return $data;
}
$meta_key = $this->get_option( "product_admin_{$meta}_meta" );
if ( empty( $meta_key ) ) {
unset( $empty_array['meta'] );
return $empty_array;
}
$post_id = intval( $data );
$post = get_post( $post_id );
if ( empty( $post->post_author ) ) {
return $empty_array;
}
return [
'meta' => $meta,
'mobile' => get_user_meta( $post->post_author, $meta_key, true ),
'statuses' => $this->get_option( 'product_admin_meta_order_status' ),
];
}
public function get_saved_mobile_data( $meta, $post_id = 0, $empty_array = [] ) {
if ( empty( $post_id ) ) {
$screen = get_current_screen();
// Check if we are on the post editing screen
if ( empty( $screen ) || 'product' !== $screen->post_type ) {
return $empty_array;
}
$post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0;
}
if ( empty( $post_id ) ) {
return $empty_array;
}
$product = wc_get_product( $post_id );
if ( ! PWSMS()->is_wc_product( $product ) ) {
return $empty_array;
}
$data = $product->get_meta( '_pwoosms_product_admin_meta_' . $meta, true );
if ( ! empty( $data ) ) {
return (array) $data;//mobile and statuses that set via admin
}
return $post_id;
}
public function get_post_mobile_meta( $post_id = 0 ) {
$meta = 'post';
$empty_array = [ 'meta' => $meta, 'mobile' => '', 'statuses' => '' ];
$data = $this->get_saved_mobile_data( $meta, $post_id, $empty_array );
if ( is_array( $data ) ) {
return $data;
}
$meta_key = $this->get_option( "product_admin_{$meta}_meta" );
if ( empty( $meta_key ) ) {
unset( $empty_array['meta'] );
return $empty_array;
}
$post_id = intval( $data );
$product = wc_get_product( $post_id );
$mobile = $product->get_meta( $meta_key, true );
return [
'meta' => $meta,
'mobile' => $mobile,
'statuses' => $this->get_option( 'product_admin_meta_order_status' ),
];
}
public function validate_mobile( $mobile ) {
$mobile = $this->modify_mobile( $mobile );
return preg_match( '/9\d{9,}?$/', trim( $mobile ) );
}
public function modify_mobile( $mobile ) {
if ( is_array( $mobile ) ) {
return array_map( [ $this, __FUNCTION__ ], $mobile );
}
$mobile = $this->mobile_english_numbers( $mobile );
$modified = preg_replace( '/\D/is', '', (string) $mobile );
if ( substr( $mobile, 0, 1 ) == '+' ) {
return '+' . $modified;
} elseif ( substr( $modified, 0, 2 ) == '00' ) {
return '+' . substr( $modified, 2 );
} elseif ( substr( $modified, 0, 1 ) == '0' ) {
return $modified;
} elseif ( ! empty( $modified ) ) {
$modified = '0' . $modified;
}
return str_replace( '+980', '0', $modified );
}
public function prepare_admin_product_status( $statuses, $array = true ) {
$delimator = '-sv-';
if ( ! is_array( $statuses ) ) {
$statuses = explode( $delimator, $statuses );
}
$statuses = array_map( 'trim', $statuses );
$statuses = array_map( [ $this, 'sanitize_text_field' ], $statuses );
$statuses = array_unique( array_filter( $statuses ) );
//واسه مقایسه کردن لازم میشه
sort( $statuses );
if ( $array ) {
return $statuses;
}
return implode( $delimator, $statuses );
}
public function product_admin_items( $order_products, $product_ids ) {
$product_ids = array_unique( $product_ids );
$items = [];
foreach ( $product_ids as $product_id ) {
$item_datas = $order_products[ $product_id ];
foreach ( (array) $item_datas as $item_data ) {
$this->prepare_items( $items, $item_data );
}
}
$items['product_ids'] = $product_ids;
return $items;
}
public function order_ID( $order ) {
return $this->order_prop( $order, 'id' );
}
public function order_note_metabox( WC_Order $order ) {
if ( ! class_exists( 'WC_Meta_Box_Order_Notes' ) ) {
return '';
}
if ( ! method_exists( 'WC_Meta_Box_Order_Notes', 'output' ) ) {
return '';
}
ob_start();
WC_Meta_Box_Order_Notes::output( $order );
return ob_get_clean();
}
public function SendSMS( $data ) {
_doing_it_wrong( __METHOD__, 'SendSMS() is deprecated. Use send_sms() instead.', '1.0.0' );
return $this->send_sms( $data );
}
public function send_sms( $data ) {
// TODO: Set mobile string handling in better way
$message = ! empty( $data['message'] ) ? esc_textarea( $data['message'] ) : '';
$mobile = ! empty( $data['mobile'] ) ? $data['mobile'] : '';
if ( ! is_array( $mobile ) ) {
$mobile = explode( ',', $mobile );
}
$mobile = $this->modify_mobile( $mobile );
$mobile = explode( ',', implode( ',', (array) $mobile ) );
$mobile = array_map( 'trim', $mobile );
$mobile = array_unique( array_filter( $mobile ) );
$gateway_obj = $this->get_sms_gateway();
$gateway_class = get_class( $gateway_obj );
if ( empty( $mobile ) ) {
$result = 'شماره موبایل خالی است . ';
} elseif ( empty( $message ) ) {
$result = 'متن پیامک خالی است . ';
} elseif ( empty( $gateway_class ) ) {
$result = 'تنظیمات درگاه پیامک انجام نشده است . ';
} elseif ( ! class_exists( $gateway_class ) ) {
$result = 'درگاه پیامکی شما وجود ندارد.';
} else {
try {
$gateway_obj->mobile = $mobile;
$gateway_obj->message = $message;
$result = $gateway_obj->send( $data );
} catch ( Exception $e ) {
$result = $e->getMessage();
}
}
if ( $result !== true && ! is_string( $result ) ) {
ob_start();
var_dump( $result );
$result = ob_get_clean();
}
if ( ! empty( $mobile ) && ! empty( $message ) ) {
$sender = '( ' . $gateway_obj->senderNumber . ' ) ' . $gateway_obj->name();
Archive::insert_record( [
'post_id' => ! empty( $data['post_id'] ) ? $data['post_id'] : '',
'type' => ! empty( $data['type'] ) ? $data['type'] : 0,
'reciever' => implode( ',', (array) $mobile ),
'message' => $message,
'sender' => $sender,
'result' => $result === true ? '_ok_' : $result,
] );
}
return $result;
}
/**
*
* Return the current active gateway
*
* @return GatewayInterface
*/
public static function get_sms_gateway() {
$active_gateway = PWSMS()->get_option( 'sms_gateway' );
if ( ! class_exists( $active_gateway ) || ! is_subclass_of( $active_gateway, GatewayInterface::class ) ) {
$active_gateway = Logger::class;
}
return new $active_gateway();
}
public function get_sms_gateways() {
$gateways = [];
$excluded_gateways = [//'PW\PWSMS\Gateways\IppanelSms' => 'ippanelsms',
];
// Gateways are static as namespace and directory
$namespace = 'PW\PWSMS\Gateways';
$dir = PWSMS_DIR . '/src/Gateways';
// Scan the directory for PHP files
foreach ( glob( "$dir/*.php" ) as $file ) {
$class = basename( $file, '.php' );
// Create Full Qualified Class Name based on file names without .php
$fqcn = "$namespace\\$class";
$active_gateway = PWSMS()->get_option( 'sms_gateway' );
if ( empty( $active_gateway ) ) {
$active_gateway = Logger::class;
}
if ( class_exists( $active_gateway ) ) {
$active_gateway_id = $active_gateway::id();
} else {
// Rollback support on older systems where the raw id of sms gateway got stored
$active_gateway_id = $active_gateway;
}
if ( class_exists( $fqcn ) ) {
$reflectionClass = new ReflectionClass( $fqcn );
// Check if the target class implements GatewayInterface and exclude main classes
$is_normal_class = $reflectionClass->implementsInterface( GatewayInterface::class ) && ! $reflectionClass->isAbstract() && ! $reflectionClass->isTrait();
if ( $is_normal_class ) {
$id = $fqcn::id();
$name = $fqcn::name();
if ( $id == $active_gateway_id ) {
// Migrate sms_gateway to the fully qualified class name
$this->update_option( 'sms_main_settings', 'sms_gateway', $fqcn );
}
$gateways[ $fqcn ] = $name;
}
}
}
// Purify gateways
$gateways = array_diff( $gateways, $excluded_gateways );
return apply_filters( 'pwoosms_sms_gateways', $gateways );
}
public function update_option( $section, $option, $value ) {
$section_settings = get_option( $section );
if ( $section_settings && isset( $section_settings[ $option ] ) ) {
$section_settings[ $option ] = $value;
update_option( $section, $section_settings );
}
}
/**
* Check if HPOS enabled.
*/
public function is_wc_order_hpos_enabled() {
return function_exists( 'wc_get_container' ) ? wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() : false;
}
public function extract_last_ten_digits( $phone_number ) {
// Remove non-digit characters
$phone_number = preg_replace( '/\D/', '', $phone_number );
// Check the length of the cleaned phone number
if ( strlen( $phone_number ) < 10 ) {
return false; // Not enough digits
}
// Extract the last 10 digits
return substr( $phone_number, - 10 );
}
}