'آرشیو پیام های واتساپ ووکامرس',
'plural' => 'آرشیو پیامک های واتساپ ووکامرس',
'ajax' => false,
] );
}
else
{
parent::__construct( [
'singular' => 'WooCommerce 360Messenger Message Archive',
'plural' => 'Archive of 360Messenger WooCommerce messages',
'ajax' => false,
] );
}
}
public function no_items() {
if (get_locale() == 'fa_IR'){
echo 'موردی یافت نشد.';
}
else
{
echo 'Item not found.';
}
}
public function column_default( $item, $column_name ): string {
$align = is_rtl() ? 'right' : 'left';
switch ( $column_name ) {
case 'sender':
case 'reciever':
return '
' . $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(
'', $item['id']
);
}
public function column_post_id( $item ) {
if ( empty( $item['post_id'] ) ) {
return '-';
}
$post_id = absint( $item['post_id'] );
$post_type = get_post_type( $post_id );
$value = [];
switch ( $post_type ) {
case 'shop_order':
if (get_locale() == 'fa_IR'){
$edit_title = 'مدیریت سفارش';
$filter_title = 'مشاهده آرشیو پیام های واتساپ این سفارش';
$value[] = 'سفارش #' . $post_id;
}
else
{
$edit_title = 'Order Management';
$filter_title = 'View the archive of 360Messenger messages of this order';
$value[] = 'Order #' . $post_id;
}
break;
case 'product':
if (get_locale() == 'fa_IR'){
$edit_title = 'مدیریت محصول';
$filter_title = 'مشاهده آرشیو پیام های واتساپ این محصول';
$value[] = 'محصول';
}
else
{
$edit_title = 'Product Management';
$filter_title = 'View the archive of 360Messenger messages of this product';
$value[] = 'product';
}
$value[] = get_the_title( $post_id );
break;
default:
return '-';
}
$actions = [
'delete' => sprintf( '%s', get_edit_post_link( $post_id ), $edit_title ),
];
$post_id = '' . implode( ' :: ', $value ) . '';
return sprintf( '%1$s %2$s', $post_id, $this->row_actions( $actions ) );
}
public function column_result( $item ) {
$result = ! empty( $item['result'] ) ? $item['result'] : '';
if ( trim( $result ) == '_ok_' ) {
if (get_locale() == 'fa_IR')
$result = 'پیام با موفقیت ارسال شد.';
else
$result = '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 = 'ارسال دسته جمعی';
else
$value = 'Bulk Send';
break;
/*customer*/
case '2':
if (get_locale() == 'fa_IR')
$value = 'مشتری - خودکار - سفارش';
else
$value = 'customer - auto - order';
break;
case '3':
if (get_locale() == 'fa_IR')
$value = 'مشتری - دستی - متاباکس سفارش';
else
$value = 'customer - manual - order metabox';
break;
/*general manager*/
case '4':
if (get_locale() == 'fa_IR')
$value = 'مدیر کل - خودکار - سفارش';
else
$value = 'General Manager - Auto - Order';
break;
/* product manager*/
case '5':
if (get_locale() == 'fa_IR')
$value = 'مدیر محصول - خودکار - سفارش';
else
$value = 'Product Manager - Auto - Order';
break;
case '6':
if (get_locale() == 'fa_IR')
$value = 'مدیر محصول - دستی - متاباکس محصول';
else
$value = 'Product Manager - Manual - Product Metabox';
break;
/*joint general manager and product manager*/
case '7':
if (get_locale() == 'fa_IR')
$value = 'مدیران - خودکار - ناموجود شدن';
else
$value = 'administrators - auto - disappear';
break;
case '8':
if (get_locale() == 'fa_IR')
$value = 'مدیران - خودکار - کم بودن موجودی';
else
$value = 'Managers - Auto - Low Stock';
break;
/*اطلاع رسانی*/
case '9':
if (get_locale() == 'fa_IR')
$value = 'اطلاع رسانی - حراج شدن - اتوماتیک';
else
$value = 'notification - auction - automatic';
break;
case '10':
if (get_locale() == 'fa_IR')
$value = 'خبرنامه - حراج شدن - دستی';
else
$value = 'Newsletter - Auction - Manual';
break;
/*--*/
case '11':
if (get_locale() == 'fa_IR')
$value = 'اطلاع رسانی - موجود شدن - اتوماتیک';
else
$value = 'notification - availability - automatic';
break;
case '12':
if (get_locale() == 'fa_IR')
$value = 'اطلاع رسانی - موجود شدن - دستی';
else
$value = 'notification - availability - manual';
break;
/*--*/
case '13':
if (get_locale() == 'fa_IR')
$value = 'اطلاع رسانی - کم بودن موجودی - اتوماتیک';
else
$value = 'Information - low stock - automatic';
break;
case '14':
if (get_locale() == 'fa_IR')
$value = 'اطلاع رسانی - کم بودن موجودی - دستی';
else
$value = 'Information - low stock - manual';
break;
/*--*/
case '15':
if (get_locale() == 'fa_IR')
$value = 'اطلاع رسانی - گزینه های دلخواه - دستی';
else
$value = '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( $item['id'] ),
'_wpnonce' => $delete_nonce,
] );
$actions = [
'delete' => sprintf( 'حذف', $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' => 'زمان',
'post_id' => 'سفارش / محصول',
'type' => 'نوع پیام',
'message' => 'متن پیام',
'reciever' => 'گیرندگان',
'sender' => 'وبسرویس',
'result' => 'نتیجه وبسرویس',
];
}
else
{
return [
'cb' => '',
'date' => 'time',
'post_id' => 'Order / Product',
'type' => 'message type',
'message' => 'message text',
'reciever' => 'recipients',
'sender' => 'webservice',
'result' => 'webservice result',
];
}
}
public function get_sortable_columns(): array {
return [
'post_id' => [ 'post_id', false ],
'type' => [ 'type', false ],
'sender' => [ 'sender', false ],
'reciever' => [ 'reciever', false ],
'date' => [ 'date', false ],
];
}
public function process_bulk_action() {
$action = $this->current_action();
if ( 'delete' === $action ) {
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'] ?? null, 'WooNotify_delete_archive' ) ) {
if (get_locale() == 'fa_IR')
wp_die( 'خطایی رخ داده است. بعدا تلاش کنید.' );
else
wp_die( 'An error occurred. Try again later.' );
}
$this->delete_item( absint(sanitize_text_field( ['item'] ?? 0 )) );
if (get_locale() == 'fa_IR')
echo '';
else
echo 'The item has been removed.
';
} else if ( $action == 'bulk_delete' ) {
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'] ?? null, 'bulk-' . $this->_args['plural'] ) ) {
if (get_locale() == 'fa_IR')
wp_die( 'خطایی رخ داده است. بعدا تلاش کنید.' );
else
wp_die( '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 '';
else
echo '';
}
}
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' => 'حذف',
];
}
else
{
return [
'bulk_delete' => 'حذف',
];
}
}
}
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'] ) ? $data['post_id'] : 0,
'type' => ! empty( $data['type'] ) ? $data['type'] : 0,
'reciever' => ! empty( $data['reciever'] ) ? $data['reciever'] : '',
'message' => ! empty( $data['message'] ) ? $data['message'] : '',
'sender' => ! empty( $data['sender'] ) ? $data['sender'] : '',
'result' => ! empty( $data['result'] ) ? $data['result'] : '',
'date' => gmdate( 'Y-m-d H:i:s', $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();
?>