esc_html( 'آرشیو پیام های واتساپ ووکامرس' ),
'plural' => esc_html( 'آرشیو پیامک های واتساپ ووکامرس' ),
'ajax' => false,
] );
} else {
parent::__construct( [
'singular' => esc_html( 'WooCommerce 360Messenger Message Archive' ),
'plural' => esc_html( 'Archive of 360Messenger WooCommerce messages' ),
'ajax' => false,
] );
}
}
public function no_items() {
if ( get_locale() == 'fa_IR' ) {
echo esc_html( 'موردی یافت نشد.' );
} else {
echo esc_html( 'Item not found.' );
}
}
public function column_default( $item, $column_name ): string {
$align = is_rtl() ? esc_html( 'right' ) : esc_html( 'left' );
switch ( $column_name ) {
case 'sender':
case 'reciever':
return '
' . esc_html( $item[ $column_name ] ) . '
';
default:
if ( is_string( $item[ $column_name ] ) ) {
return nl2br( $item[ $column_name ] );
}
return print_r( $item[ $column_name ], true );
}
}
public function column_cb( $item ): string {
return sprintf(
'', esc_html( $item['id'] )
);
}
public function column_post_id( $item ) {
if ( empty( $item['post_id'] ) ) {
return '-';
}
$post_id = absint( sanitize_text_field( $item['post_id'] ) );
$post_type = get_post_type( $post_id );
$value = [];
switch ( $post_type ) {
case 'shop_order':
if ( get_locale() == 'fa_IR' ) {
$edit_title = esc_html( 'مدیریت سفارش' );
$filter_title = esc_html( 'مشاهده آرشیو پیام های واتساپ این سفارش' );
$value[] = esc_html( 'سفارش #' ) . sanitize_text_field( $post_id );
} else {
$edit_title = esc_html( 'Order Management' );
$filter_title = esc_html( 'View the archive of 360Messenger messages of this order' );
$value[] = esc_html( 'Order #' ) . sanitize_text_field( $post_id );
}
break;
case 'product':
if ( get_locale() == 'fa_IR' ) {
$edit_title = esc_html( 'مدیریت محصول' );
$filter_title = esc_html( 'مشاهده آرشیو پیام های واتساپ این محصول' );
$value[] = esc_html( 'محصول' );
} else {
$edit_title = esc_html( 'Product Management' );
$filter_title = esc_html( 'View the archive of 360Messenger messages of this product' );
$value[] = esc_html( 'product' );
}
$value[] = get_the_title( sanitize_text_field( $post_id ) );
break;
default:
return '-';
}
$actions = [
'delete' => sprintf( '%s', esc_html( sanitize_text_field( $post_id ) ), esc_html( $edit_title ) ),
];
$post_id = '' . implode( ' :: ', esc_html( $value ) ) . '';
return sprintf( '%1$s %2$s', sanitize_text_field( $post_id ), $this->row_actions( $actions ) );
}
public function column_result( $item ) {
$result = ! empty( $item['result'] ) ? sanitize_text_field($item['result']) : '';
if ( trim( $result ) == '_ok_' ) {
if ( get_locale() == 'fa_IR' ) {
$result = esc_html('پیام با موفقیت ارسال شد.');
} else {
$result = esc_html('The message was sent successfully.');
}
}
return $result;
}
public function column_type( $item ) {
if ( empty( $item['type'] ) ) {
return '-';
}
switch ( $item['type'] ) {
case '1':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('ارسال دسته جمعی');
} else {
$value = esc_html('Bulk Send');
}
break;
/*customer*/
case '2':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مشتری - خودکار - سفارش');
} else {
$value = esc_html('customer - auto - order');
}
break;
case '3':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مشتری - دستی - متاباکس سفارش');
} else {
$value = esc_html('customer - manual - order metabox');
}
break;
/*general manager*/
case '4':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مدیر کل - خودکار - سفارش');
} else {
$value = esc_html('General Manager - Auto - Order');
}
break;
/* product manager*/
case '5':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مدیر محصول - خودکار - سفارش');
} else {
$value = esc_html('Product Manager - Auto - Order');
}
break;
case '6':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مدیر محصول - دستی - متاباکس محصول');
} else {
$value = esc_html('Product Manager - Manual - Product Metabox');
}
break;
/*joint general manager and product manager*/
case '7':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مدیران - خودکار - ناموجود شدن');
} else {
$value = esc_html('administrators - auto - disappear');
}
break;
case '8':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('مدیران - خودکار - کم بودن موجودی');
} else {
$value = esc_html('Managers - Auto - Low Stock');
}
break;
/*اطلاع رسانی*/
case '9':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('اطلاع رسانی - حراج شدن - اتوماتیک');
} else {
$value = esc_html('notification - auction - automatic');
}
break;
case '10':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('خبرنامه - حراج شدن - دستی');
} else {
$value = esc_html('Newsletter - Auction - Manual');
}
break;
/*--*/
case '11':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('اطلاع رسانی - موجود شدن - اتوماتیک');
} else {
$value = esc_html('notification - availability - automatic');
}
break;
case '12':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('اطلاع رسانی - موجود شدن - دستی');
} else {
$value = esc_html('notification - availability - manual');
}
break;
/*--*/
case '13':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('اطلاع رسانی - کم بودن موجودی - اتوماتیک');
} else {
$value = esc_html('Information - low stock - automatic');
}
break;
case '14':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('اطلاع رسانی - کم بودن موجودی - دستی');
} else {
$value = esc_html('Information - low stock - manual');
}
break;
/*--*/
case '15':
if ( get_locale() == 'fa_IR' ) {
$value = esc_html('اطلاع رسانی - گزینه های دلخواه - دستی');
} else {
$value = esc_html('Notification - Preferences - Manual');
}
break;
default:
$value = '';
}
return $value;
}
public function column_date( $item ): string {
$delete_nonce = wp_create_nonce( 'WooNotify_delete_archive' );
$url = add_query_arg( [
'action' => 'delete',
'item' => absint(sanitize_text_field( $item['id']) ),
'_wpnonce' => $delete_nonce,
] );
$actions = [
'delete' => sprintf( ''.esc_html('حذف').'', $url ),// @todo escape url
];
$date = date_i18n( 'Y-m-d H:i:s', strtotime( $item['date'] ) );
$date = WooNotify()->mayBeJalaliDate( $date );
return sprintf( '%1$s %2$s', $date, $this->row_actions( $actions ) );
}
public function prepare_items() {
$this->_column_headers = [ $this->get_columns(), [], $this->get_sortable_columns() ];
$this->process_bulk_action();
$per_page = 20;
$this->set_pagination_args( [
'total_items' => $this->record_count(),
'per_page' => $per_page,
] );
$this->items = $this->get_items( $per_page, $this->get_pagenum() );
}
public function get_columns(): array {
if ( get_locale() == 'fa_IR' ) {
return [
'cb' => '',
'date' => esc_html('زمان'),
'post_id' => esc_html('سفارش / محصول'),
'type' => esc_html('نوع پیام'),
'message' => esc_html('متن پیام'),
'reciever' => esc_html('گیرندگان'),
'sender' => esc_html('وبسرویس'),
'result' => esc_html('نتیجه وبسرویس'),
];
} else {
return [
'cb' => '',
'date' => esc_html('time'),
'post_id' => esc_html('Order / Product'),
'type' => esc_html('message type'),
'message' => esc_html('message text'),
'reciever' => esc_html('recipients'),
'sender' => esc_html('webservice'),
'result' => esc_html('webservice result'),
];
}
}
public function get_sortable_columns(): array {
return [
'post_id' => [ esc_html('post_id'), false ],
'type' => [ esc_html('type'), false ],
'sender' => [ esc_html('sender'), false ],
'reciever' => [ esc_html('reciever'), false ],
'date' => [ esc_html('date'), false ],
];
}
public function process_bulk_action() {
$action = $this->current_action();
if ( 'delete' === $action ) {
//if ( ! wp_verify_nonce( sanitize_text_field($_REQUEST['_wpnonce']) ?? null, 'WooNotify_delete_archive' ) ) {
//new
if ( ! wp_verify_nonce( sanitize_text_field( $_REQUEST['_wpnonce'] ) ?? null, 'WooNotify_delete_archive' ) ) {
if ( get_locale() == 'fa_IR' ) {
wp_die( esc_html('خطایی رخ داده است. بعدا تلاش کنید.') );
} else {
wp_die( esc_html('An error occurred. Try again later.') );
}
}
$this->delete_item( absint( sanitize_text_field( [ 'item' ] ?? 0 ) ) );
if ( get_locale() == 'fa_IR' ) {
echo ''.esc_html('آیتم حذف شد.').'
';
} else {
echo ''.esc_html('The item has been removed.').'
';
}
} else if ( $action == 'bulk_delete' ) {
if ( ! wp_verify_nonce( sanitize_text_field( $_REQUEST['_wpnonce'] ) ?? null, 'bulk-' . $this->_args['plural'] ) ) {
//if ( ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'bulk-' . $this->_args['plural'] ) ) {
// if ( ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'bulk-' . $this->_args['plural'] ) ) {
// if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-' . $this->_args['plural'] ) ) {
if ( get_locale() == 'fa_IR' ) {
wp_die( esc_html('خطایی رخ داده است. بعدا تلاش کنید.') );
} else {
wp_die( esc_html('An error occurred. Try again later.') );
}
}
$delete_ids = array_map( 'absint', $_REQUEST['item'] ?? [] );
foreach ( (array) $delete_ids as $id ) {
$this->delete_item( $id );
}
if ( get_locale() == 'fa_IR' ) {
echo ''.esc_html('آیتم ها حذف شدند.').'
';
} else {
echo ''.esc_html('Items have been removed.').'
';
}
}
}
public function delete_item( int $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(): int {
global $wpdb;
if ( ! $this->table_exists() ) {
return 0;
}
//old
//return $wpdb->get_var( $this->get_query( true ) );
global $wpdb;
$query = $this->get_query( true );
$result = $wpdb->get_var( $query );
return $result;
//new
//return $wpdb->get_var( $wpdb->prepare( $this->get_query( true ) ) );
}
private function table_exists() {
global $wpdb;
$wild = '%';
$like = $wild . $wpdb->esc_like( self::table() ) . $wild;
$result = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $like ) );
return ! is_null( $result ) ? 1 : 0;
}
private function get_query( $count = false ) {
global $wpdb;
$select = $count ? 'count(*)' : '*';
$sql = sprintf( "SELECT %s FROM %s", $select, self::table() );
if ( isset( $_REQUEST['s'] ) ) {
$s = ltrim( sanitize_text_field( $_REQUEST['s'] ), '0' );
$sql .= $wpdb->prepare( " WHERE (`message` LIKE %s OR `reciever` LIKE %s OR `sender` LIKE %s)", '%' . $wpdb->esc_like( $s ) . '%', '%' . $wpdb->esc_like( $s ) . '%', '%' . $wpdb->esc_like( $s ) . '%' );
}
if ( ! empty( $_REQUEST['id'] ) ) {
//old code
$post_id = array_map( 'absint', is_array( sanitize_text_field( $_REQUEST['id'] ) ) ? sanitize_text_field( $_REQUEST['id'] ) : explode( ',', (string) sanitize_text_field( $_REQUEST['id'] ) ) );
//new code
//$post_id = isset( $_REQUEST['id'] ) ? wp_parse_id_list( $_REQUEST['id'] ) : array();
$sql .= ( isset( $s ) ? ' AND' : ' WHERE' ) . ' (`post_id` IN (' . implode( ',', sanitize_text_field( $post_id ) ) . '))';
}
if ( ! empty( $_REQUEST['orderby'] ) ) {
$sql .= $wpdb->prepare( ' ORDER BY %s', sanitize_text_field( $_REQUEST['orderby'] ) );
$sql .= sanitize_text_field( sanitize_text_field( $_REQUEST['order'] ) ) == 'DESC' ? ' DESC' : ' ASC';
} else {
$sql .= ' ORDER BY id DESC';
}
return $sql;
}
public function get_items( int $per_page = 20, int $page_number = 1 ) {
global $wpdb;
if ( ! $this->table_exists() ) {
return [];
}
$query = $this->get_query();
//$query .= $wpdb->prepare("LIMIT %d" , $per_page);
//$query .= $wpdb->prepare(" OFFSET ( %d - 1 ) * %d" , $page_number , $per_page);
//old
//return $wpdb->get_results( $query, 'ARRAY_A' );
global $wpdb;
$query = $this->get_query();
$results = $wpdb->get_results( $query, ARRAY_A );
return $results;
//new
//return $wpdb->get_results( $wpdb->prepare( $query ), 'ARRAY_A' );
}
public function get_bulk_actions(): array {
if ( get_locale() == 'fa_IR' ) {
return [
'bulk_delete' => esc_html('حذف'),
];
} else {
return [
'bulk_delete' => esc_html('حذف'),
];
}
}
}
class WooNotify_360Messenger_Archive {
public function __construct() {
add_action( 'WooNotify_settings_form_bottom_360Messenger_archive', [ $this, 'archiveTable' ] );
add_action( 'init', [ $this, 'createTable' ] );
}
public static function insertRecord( $data ) {
global $wpdb;
$time = time();
if ( function_exists( 'wc_timezone_offset' ) ) {
$time += wc_timezone_offset();
}
$wpdb->insert( WooNotify_360Messenger_Archive_List_Table::table(), [
'post_id' => ! empty( $data['post_id'] ) ? sanitize_text_field($data['post_id']) : 0,
'type' => ! empty( $data['type'] ) ? sanitize_text_field($data['type']) : 0,
'reciever' => ! empty( $data['reciever'] ) ? sanitize_text_field($data['reciever'] ): '',
'message' => ! empty( $data['message'] ) ? sanitize_text_field($data['message']) : '',
'sender' => ! empty( $data['sender'] ) ? sanitize_text_field($data['sender'] ): '',
'result' => ! empty( $data['result'] ) ? sanitize_text_field($data['result']) : '',
'date' => gmdate( 'Y-m-d H:i:s', sanitize_text_field($time) ),
], [ '%d', '%d', '%s', '%s', '%s', '%s', '%s' ] );
}
public function createTable() {
global $wpdb;
if ( get_option( 'WooNotify_table_archive' ) ) {
return;
}
if ( ! function_exists( 'dbDelta' ) ) {
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
}
$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_Archive_List_Table::table();
dbDelta( "CREATE TABLE IF NOT EXISTS $table (
id mediumint(8) unsigned NOT NULL auto_increment,
post_id mediumint(8) unsigned,
type tinyint(2),
reciever TEXT NOT NULL,
message TEXT NOT NULL,
sender VARCHAR(100),
result TEXT,
date DATETIME,
PRIMARY KEY (id)
) $charset_collate;" );
update_option( 'WooNotify_table_archive', '1' );
}
public function archiveTable() {
$list = new WooNotify_360Messenger_Archive_List_Table();
$list->prepare_items();
?>