';
}
}
public function delete_item( $id ) {
global $wpdb;
$wpdb->delete( self::table(), [ 'id' => $id ] );
}
public static function table(): string {
global $wpdb;
return $wpdb->prefix . self::$table;
}
public function record_count() {
global $wpdb;
return $wpdb->get_var( $this->get_query( true ) );
}
private function get_query( $count = false ) {
global $wpdb;
$table = self::table();
$where = [];
if ( isset( $_REQUEST['s'] ) ) {
$s = esc_attr(sanitize_text_field( $_REQUEST['s'] ));
$s = self::prepareMobile( $s );
$where[] = '(mobile LIKE "%' . $wpdb->prepare( "%s",$s ). '%")';
}
if ( ! empty( $_REQUEST['product_id'] ) ) {
$where[] = '(`product_id` IN (' . self::request_product_id() . '))';
}
$where = ! empty( $where ) ? '(' . implode( ' AND ', $where ) . ')' : '';
$order_by = ! empty(esc_attr(sanitize_text_field( $_REQUEST['orderby'])) ) ? esc_attr(sanitize_text_field( trim( $_REQUEST['orderby'] )) ) : '';
$select = $count ? 'count(*)' : '*';
if ( $order_by == 'groups' ) {
$sql = $wpdb->prepare( "SELECT %s, SUBSTRING_INDEX(SUBSTRING_INDEX(t.groups, ',', n.n), ',', -1) groups
FROM %s t CROSS JOIN (SELECT a.N + b.N * 10 + 1 n FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a,
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n) n WHERE (n.n <= 1 + (LENGTH(t.groups) - LENGTH(REPLACE(t.groups, ',', ''))))", $select, $table );
if ( ! empty( $where ) ) {
$sql .= " AND {$where}";
}
} else {
$sql = sprintf( "SELECT %s FROM %s", $select, $table );
if ( ! empty( $where ) ) {
$sql .= " WHERE {$where}";
}
}
if ( ! empty( $order_by ) ) {
$sql .= esc_attr(sanitize_text_field($_REQUEST['order'])) == 'DESC' ? ' DESC' : ' ASC';
$sql .= $wpdb->prepare( " ORDER BY %s %s", $order_by, $order );
if ( $order_by != 'product_id' ) {
$sql .= $wpdb->prepare( ", product_id %s", $order );
}
} else {
$sql .= ' ORDER BY id DESC';
}
return $sql;
}
public function get_items( int $per_page = 20, int $page_number = 1 ) {
global $wpdb;
$sql = $this->get_query();
//$sql .= $wpdb->prepare("LIMIT %d" , $per_page);
//$sql .= $wpdb->prepare(" OFFSET ( %d - 1 ) * %d" , $page_number , $per_page);
$results = $wpdb->get_results( $sql, 'ARRAY_A' );
$this->set_users_mobile( $results );
return $results;
}
public function set_users_mobile( $result ) {
$mobiles = array_unique( wp_list_pluck( $result, 'mobile' ) );
$meta_key = WooNotify()->buyerMobileMeta();
$user_meta = [ 'relation' => 'OR' ];
foreach ( $mobiles as $mobile ) {
$user_meta[] = [
'key' => $meta_key,
'value' => self::prepareMobile( $mobile ),
'compare' => 'LIKE',
];
}
$users = ( new WP_User_Query( [ 'meta_query' => $user_meta ] ) )->get_results();
$_users = [];
foreach ( $users as $user ) {
if ( ! empty( $user->ID ) ) {
$_mobile = get_user_meta( $user->ID, $meta_key, true );
$_mobile = self::prepareMobile( $_mobile );
foreach ( $mobiles as $mobile ) {
$mobile = self::prepareMobile( $mobile );
if ( stripos( $_mobile, $mobile ) !== false ) {
$_mobile = $mobile;
break;
}
}
$_users[ $_mobile ] = $user;
}
}
self::$users = $_users;
return $_users;
}
public function get_bulk_actions() {
if (get_locale() == 'fa_IR'){
return [
'bulk_delete' => 'حذف',
];
}
else
{
return [
'bulk_delete' => 'delete',
];
}
}
}
class WooNotify_360Messenger_Contacts {
public function __construct() {
add_action( 'WooNotify_settings_form_bottom_360Messenger_contacts', [ $this, 'contactsTable' ] );
add_action( 'init', [ $this, 'createTable' ] );
add_action( 'init', [ $this, 'moveOldContants__3_8' ] );
add_action( 'wp_ajax_change_360Messenger_text', [ $this, 'change360MessengerTextCallback' ] );
if(isset($_GET['test'])){
self::getContactsMobiles(361 , '_in');
}
}
public static function groupName( $group_id, $product_id, $cond = true ) {
$groups = self::getGroups( $product_id, false, $cond );
return isset( $groups[ $group_id ] ) ? $groups[ $group_id ] : '';
}
public static function getGroups( $product_id, $check = true, $cond = true ) {
$groups = [];
if ( ! $check || ! WooNotify()->ProductHasProp( $product_id, 'is_on_sale' ) ) {
if ( ! $cond || WooNotify()->hasNotifCond( 'enable_onsale', $product_id ) ) {
$groups['_onsale'] = WooNotify()->getValue( 'notif_onsale_text', $product_id );
}
}
if ( ! $check || ! WooNotify()->ProductHasProp( $product_id, 'is_in_stock' ) ) {
if ( ! $cond || WooNotify()->hasNotifCond( 'enable_notif_no_stock', $product_id ) ) {
$groups['_in'] = WooNotify()->getValue( 'notif_no_stock_text', $product_id );
}
}
if ( ! $check || WooNotify()->ProductHasProp( $product_id, 'is_not_low_stock' ) ) {
if ( ! $cond || WooNotify()->hasNotifCond( 'enable_notif_low_stock', $product_id ) ) {
$groups['_low'] = WooNotify()->getValue( 'notif_low_stock_text', $product_id );
}
}
foreach ( explode( PHP_EOL, (string) WooNotify()->getValue( 'notif_options', $product_id ) ) as $option ) {
$options = explode( ":", $option, 2 );
if ( count( $options ) == 2 ) {
$groups["$options[0]"] = $options[1];
}
}
return array_filter( $groups );
}
/*------------------------------------------------------------------------------*/
public static function getContactByMobile( $product_id, $mobile ) {
$table = WooNotify_360Messenger_Contacts_List_Table::table();
$product_id = absint( $product_id );
$mobile = WooNotify_360Messenger_Contacts_List_Table::prepareMobile( $mobile );
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE product_id=%d AND mobile LIKE '%s'", $product_id, "%$mobile%" ), ARRAY_A );
}
public static function getContactsMobiles( int $product_id, string $group ) {
global $wpdb;
$table = WooNotify_360Messenger_Contacts_List_Table::table();
$query = "SELECT `mobile` FROM `{$table}` WHERE product_id=%d";
$query .= ' AND ( `groups` LIKE %s )';
$group_name = '%' . $wpdb->esc_like( $group ) . '%';
$mobiles = $wpdb->get_col( $wpdb->prepare( $query, $product_id, $group_name) );
$mobiles = array_unique( array_filter( $mobiles ) );
return $mobiles;
}
public function createTable() {
if ( get_option( 'WooNotify_table_contacts_created' ) ) {
return;
}
if ( ! function_exists( 'dbDelta' ) ) {
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
}
global $wpdb;
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) {
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE $wpdb->collate";
}
$table = WooNotify_360Messenger_Contacts_List_Table::table();
dbDelta( "CREATE TABLE IF NOT EXISTS {$table} (
`id` mediumint(8) unsigned NOT NULL auto_increment,
`product_id` mediumint(8) unsigned,
`mobile` VARCHAR(250),
`groups` VARCHAR(250),
PRIMARY KEY (id)
) $charset_collate;" );
update_option( 'WooNotify_table_contacts_created', '1' );
}
/*-------------------------------------------------------------------------------*/
public function moveOldContants__3_8() {
if ( get_option( 'WooNotify_table_contacts_updated' ) ) {
return;
}
global $wpdb;
if ( ! $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", WooNotify_360Messenger_Contacts_List_Table::table() ) ) ) {
return;
}
// transfare 50 member
$sql = $wpdb->prepare("SELECT * FROM %s WHERE `meta_key`='_hannanstd_360Messenger_notification' LIMIT 50" , $wpdb->postmeta);
$results = $wpdb->get_results( $sql, 'ARRAY_A' );
if ( empty( $results ) ) {
update_option( 'WooNotify_table_contacts_updated', '1' );
}
foreach ( $results as $result ) {
$contacts = [];
foreach ( explode( '***', (string) $result['meta_value'] ) as $contact ) {
[ $contact ] = explode( '_vsh_', $contact, 1 );
if ( strlen( $contact ) < 2 ) {
continue;
}
[ $mobile, $groups ] = explode( '|', $contact, 2 );
if ( WooNotify()->validateMobile( $mobile ) ) {
$groups = explode( ',', $groups );
$_groups = ! empty( $contacts[ $mobile ] ) ? $contacts[ $mobile ] : [];
$contacts[ $mobile ] = array_unique( array_merge( $groups, $_groups ) );
}
}
$insert = true;
$meta_id = $result['meta_id'];
$product_id = $result['post_id'];
foreach ( $contacts as $mobile => $groups ) {
$insert = self::insertContact( [
'product_id' => $product_id,
'mobile' => $mobile,
'groups' => $groups,
] ) && $insert;
}
if ( $insert ) {
$wpdb->update( $wpdb->postmeta, [
'meta_key' => '_WooNotify_newsletter_contacts__moved',
], [
'meta_id' => absint( $meta_id ),
] );
}
}
}
public static function insertContact( $data ) {
if ( empty( $data['product_id'] ) || empty( $data['mobile'] ) || empty( $data['groups'] ) ) {
return false;
}
global $wpdb;
return $wpdb->insert( WooNotify_360Messenger_Contacts_List_Table::table(), [
'product_id' => absint( $data['product_id'] ),
'mobile' => WooNotify()->modifyMobile( $data['mobile'] ),
'groups' => self::prepareGroups( $data['groups'] ),
], [ '%d', '%s', '%s' ] );
}
private static function prepareGroups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = explode( ',', (string) $groups );
}
$groups = array_map( 'sanitize_text_field', $groups );
$groups = array_map( 'trim', $groups );
$groups = array_unique( array_filter( $groups ) );
$groups = implode( ',', $groups );
return $groups;
}
public function contactsTable() {
$updated = get_option( 'WooNotify_table_contacts_updated' );
if ( ! $updated ) { ?>
در حال انتقال دیتابیس مشترکین اطلاه رسانی سایت شما از جدول post_meta به یک جدول مستقل هستیم.
این عمل با توجه به حجم مشترکین شما ممکن است کمی زمانبر باشد.
لطفا لحظات دیگری پس از انتقال کامل مشترکین مراجعه نمایید.
';
}
else
{
echo '
We are moving the database of your sites notification subscribers from the post_meta table to an independent table.
This process may take some time depending on the volume of your subscribers.
Please come back a few moments after the complete transfer of subscribers.
';
}
?>
انتقال دیتابیس مشترکین اطلاع رسانی سایت شما از جدول post_meta به یک جدول مستقل با موفقیت انجام شد.
';
}
else
{
echo '
The transfer of the database of your sites notification subscribers from the post_meta table to an independent table has been done successfully.
';
}
?>