'save', // Singular name of the listed records.
'plural' => 'saves', // Plural name of the listed records.
'ajax' => true, // Does this table support ajax?
)
);
}
/**
* Prepare the items for the table to process
*/
public function prepare_items() {
$savetype = ( isset( $_REQUEST['savetype'] ) ? sanitize_text_field( $_REQUEST['savetype'] ) : 'all' );
$this->process_bulk_action();
$this->search_box( __( 'Search', 'affiliate-ai-lite' ), 'search' );
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$perPage = 25;
$currentPage = $this->get_pagenum();
$offset = ( $currentPage - 1 ) * $perPage;
// var_dump($this);
$orderbycol = 'id'; // just a default
$allowed = array(
'post' => 1,
);
$sortorder = array(
'asc' => 'ASC',
'desc' => 'DESC',
);
$orderby = ( ! empty( $_REQUEST['orderby'] ) && isset( $allowed[ $_REQUEST['orderby'] ] ) ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id'; // If no sort, default to title
if ( $orderby == 'post' ) {
$orderby = 'post_id';}
$order = ( ! empty( $_REQUEST['order'] ) && isset( $sortorder[ $_REQUEST['order'] ] ) ) ? sanitize_text_field( $_REQUEST['order'] ) : 'desc';
// var_dump($orderby);
// var_dump($order);
$data = $this->table_data( $savetype, $orderby . ' ' . $order, $offset, $perPage );
// usort( $data, array( &$this, 'sort_data' ) );
// var_dump($data);
if ( ! empty( $data['count'] ) ) {
$totalItems = $data['count'];
} else {
$totalItems = 0; }
unset( $data['count'] );
$this->set_pagination_args(
array(
'total_items' => $totalItems,
'per_page' => $perPage,
'total_pages' => ceil( $totalItems / $perPage ),
)
);
$this->_column_headers = array( $columns, $hidden, $sortable );
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns() {
$columns = array(
'cb' => '',
'post' => __( 'Post', 'affiliate-ai-lite' ),
'type' => __( 'Search type', 'affiliate-ai-lite' ),
'count' => __( 'Product count', 'affiliate-ai-lite' ),
'search' => __( 'Search', 'affiliate-ai-lite' ),
'product' => __( 'Product(s)', 'affiliate-ai-lite' ),
'update' => __( 'Price updated', 'affiliate-ai-lite' ),
);
return $columns;
}
protected function get_bulk_actions() {
$actions = array(
'delete' => _x( 'Delete', 'List table bulk action', 'affiliate-ai-lite' ),
);
return $actions;
}
protected function process_bulk_action() {
if ( ! current_user_can( get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_user_right', 'manage_options' ) ) ) { return false; }
// Detect when a bulk action is being triggered.
if ( 'delete' === $this->current_action() && ( isset( $_REQUEST['cache'] ) || isset( $_POST['save'] ) ) ) {
// security
if ( ! empty( $_REQUEST['_wpnonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) );
if ( ! empty( $_REQUEST['cache'] ) ) {
$action = 'deletesave_' . sanitize_text_field( $_REQUEST['cache'] );
// var_dump( $action );
} elseif ( ! empty( $_POST['save'] ) ) {
$action = 'bulk-' . $this->_args['plural'];
}
// var_dump( $action );
if ( ! wp_verify_nonce( $nonce, $action ) ) {
return false;
}
} else {
return false;
}
if ( ! empty( $_POST['save'] ) && is_array( $_POST['save'] ) ) {
$cache = implode( '|', array_map( 'sanitize_text_field', $_POST['save'] ) );
}
elseif ( ! empty( $_REQUEST['cache'] ) ) {
$cache = sanitize_text_field( $_REQUEST['cache'] );
}
else {
$cache = '';
}
global $wpdb;
if ( ! empty( $wpdb ) && $wpdb instanceof wpdb ) {
$table_name = $wpdb->prefix . 'afx_cache';
$flag = $wpdb->suppress_errors;
$wpdb->suppress_errors( true );
$wpdb->query( $wpdb->prepare( "DELETE FROM $table_name WHERE name REGEXP %s", $cache ) );
$wpdb->suppress_errors( $flag );
return true;
}
return false;
}
}
public function search_box( $text, $input_id ) { ?>
'search-submit' ) ); ?>
',
'save',
esc_attr( $item['id'] )
);
}
protected function column_product( $item ) {
$products = $item['product'];
$arr_prod = array();
foreach ( $products as $product ) {
$arr_prod[] = '' . esc_html( $product ) . '';
}
echo wp_kses_post( implode( '
', $arr_prod ) );
}
protected function column_post( $item ) {
$page = ! empty( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : '';
// Build edit row action.
$edit_query_args = array(
'page' => $page,
'action' => 'edit',
'save' => $item['id'],
);
$edit_post = ! empty( $item['post'] ) ? get_edit_post_link( $item['post'] ) : admin_url() . 'widgets.php';
$actions['edit'] = sprintf(
'%2$s',
esc_url( $edit_post ),
// esc_url( wp_nonce_url( get_edit_post_link($item['post']) ) ),
_x( 'Edit post', 'List table row action', 'affiliate-ai-lite' )
);
// Build delete row action.
$delete_query_args = array(
'page' => $page,
'action' => 'delete',
'cache' => $item['id'],
);
$actions['delete'] = sprintf(
'%2$s',
esc_url( wp_nonce_url( add_query_arg( $delete_query_args, 'admin.php' ), 'deletesave_' . $item['id'] ) ),
_x( 'Delete cache', 'List table row action', 'affiliate-ai-lite' )
);
if ( empty( $item['post'] ) ) {
$title = 'Widget'; } else {
$title = ! empty( get_the_title( $item['post'] ) ) ? get_the_title( $item['post'] ) : __( '(no title)', 'affiliate-ai-lite' );
// Build See row action.
$view_query_args = array(
'page' => $page,
'action' => 'view',
'save' => $item['id'],
);
$actions['view'] = sprintf(
'%2$s',
esc_url( get_post_permalink( $item['post'] ) ),
_x( 'View post', 'List table row action', 'affiliate-ai-lite' )
);
}
// Print the title contents.
printf(
'%1$s
(%2$s)%3$s',
esc_html( $title ),
esc_html( $item['id'] ),
wp_kses_post( $this->row_actions( $actions ) )
);
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns() {
return array();
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns() {
return array( 'post' => array( 'post', false ) );
}
public function get_views() {
$views = array();
$current = ( ! empty( $_REQUEST['savetype'] ) ? sanitize_text_field( $_REQUEST['savetype'] ) : 'all' );
// All link
$class = ( $current == 'all' ? ' class="current"' : '' );
$all_url = remove_query_arg( 'savetype' );
$all_url = remove_query_arg( 'paged', $all_url );
$views['all'] = "" . esc_html__( 'All', 'affiliate-ai-lite' ) . '';
// Old Update
$old_url = add_query_arg( 'savetype', 'old' );
$old_url = remove_query_arg( 'paged', $old_url );
$class = ( $current == 'old' ? ' class="current"' : '' );
$views['old'] = "" . esc_html__( 'Old update', 'affiliate-ai-lite' ) . '';
// missing product
$miss_url = add_query_arg( 'savetype', 'miss' );
$miss_url = remove_query_arg( 'paged', $miss_url );
$class = ( $current == 'miss' ? ' class="current"' : '' );
$views['miss'] = "" . esc_html__( 'Missing product', 'affiliate-ai-lite' ) . '';
return $views;
}
/**
* Get the table data
*
* @return Array
*/
private function table_data( $savetype, $order, $offset, $perPage ) {
$data = array();
global $wpdb;
$neg = 0;
if ( ! empty( $wpdb ) && $wpdb instanceof wpdb ) {
$table_name = $wpdb->prefix . 'afx_cache';
$flag = $wpdb->suppress_errors;
$wpdb->suppress_errors( true );
// SQL identifiers/direction are allowlisted; %s would sort by a literal string.
$sort = preg_split( '/\s+/', trim( $order ) );
$sort_column = in_array( $sort[0] ?? '', array( 'id', 'post_id' ), true ) ? $sort[0] : 'id';
$sort_direction = 'ASC' === strtoupper( $sort[1] ?? '' ) ? 'ASC' : 'DESC';
$sql = $wpdb->prepare(
"SELECT * FROM $table_name WHERE auto = 0 ORDER BY $sort_column $sort_direction LIMIT %d OFFSET %d",
$perPage,
$offset
);
$data = $wpdb->get_results( $sql, ARRAY_A );
if ( empty( $data ) ) {
return;}
foreach ( $data as $k => $v ) {
$details = '';
$nd = '';
$asin = '';
$ean = '';
$pid = '';
$diff = '';
$search = '';
$details = json_decode( $v['value'], true );
if ( ! is_array( $details ) ) {
$neg++;
continue; }
$result[ $k ]['id'] = $v['name'];
$result[ $k ]['post'] = $v['post_id'];
// if (empty($result[$k]['post'])) { $neg++; unset($result[$k]); continue; }
$result[ $k ]['type'] = $details['type'];
$nd = count( explode( ',', $details['search'] ) );
$result[ $k ]['update'] = implode(
'
',
array_map(
function( $d ) {
if ( ! empty( $d ) ) {
// Si > 15 jour alors passer en orange
// if ( $d + ( 60 * 60 * 24 * 15 ) > round( current_time( 'timestamp' ) ) ) {
if ( $d + ( 60 * 60 * 24 * 15 ) > round( time() ) ) {
return '' . gmdate( 'Y-m-d', $d ) . '';
} else {
return '' . gmdate( 'Y-m-d', $d ) . '';
}
} else {
return '';
}
},
array_column( $details, 'date' )
)
);
$result[ $k ]['product'] = array_column( $details, 'title' );
if ( $result[ $k ]['type'] == 'asin' ) {
$search_asin = explode( ',', $details['search'] );
$search_asin = array_map( 'sanitize_key', $search_asin );
$asin = array_column( $details, 'sku' );
$asin = array_map( 'sanitize_key', $asin );
$diff = array_diff( $search_asin, $asin );
$diff = array_map(
function( $d ) {
return '' . esc_html( $d ) . '';
},
$diff
);
$search = array_merge( $asin, $diff );
$result[ $k ]['search'] = strtoupper( implode( '
', $search ) );
// var_dump($asin);
} elseif ( $result[ $k ]['type'] == AFFIAI_PLUGIN_PROD_ID ) {
$search_ean = explode( ',', $details['search'] );
unset( $details['type'] );
unset( $details['search'] );
$ean = array_map(
function( $d ) {
if (!empty($d) && $d !== 'date') {
return str_replace( 'E_', '', $d );
}
},
array_keys( array_filter( $details, 'is_array' ) )
);
$ean = array_filter( $ean );
$diff = array_diff( $search_ean, $ean );
$diff = array_map(
function( $d ) {
return '' . esc_html( $d ) . '';
},
$diff
);
//in pid array unset key where value = date
$search = array_merge( $ean, $diff );
$result[ $k ]['search'] = implode( '
', $search );
} elseif ( $result[ $k ]['type'] == 'custom' ) {
$search_cust = array_filter( explode( '|', $details['search'] ) );
foreach ( $search_cust as $sc ) {
$result[ $k ]['search'][] = '' . $sc . '';
}
if ( !empty($result[ $k ]['search']) ) {
$result[ $k ]['search'] = implode( '
', $result[ $k ]['search'] );
}
} else {
$result[ $k ]['search'] = str_replace( ',', '
', $details['search'] );
}
unset( $details['search'] );
unset( $details['type'] );
unset( $details['cache'] );
$result[ $k ]['count'] = count( array_filter( $details, 'is_array' ) );
}
$result['count'] = $wpdb->get_var(
"SELECT COUNT(*) FROM $table_name WHERE auto = 0"
);
$result['count'] = $result['count'] - $neg;
$wpdb->suppress_errors( $flag );
return $result;
}
return false;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'id':
case 'post':
case 'type':
case 'count':
case 'product':
case 'update':
case 'quota':
echo isset($item[ $column_name ]) ? wp_kses_post( $item[ $column_name ] ) : '';
break;
case 'search':
echo isset($item[ $column_name ]) ? wp_kses_post( $item[ $column_name ] ) : '';
break;
default:
echo esc_html__( 'Unknown', 'affiliate-ai-lite' );
break;
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @param String $a Data
* @param String $b Data
*
* @return Mixed
*/
private function sort_data( $a, $b ) {
// Set defaults
$orderby = 'post';
$order = 'asc';
// If orderby is set, use this as the sort column
if ( ! empty( $_GET['orderby'] ) ) {
$orderby = sanitize_text_field( $_GET['orderby'] );
}
// If order is set use this as the order
if ( ! empty( $_GET['order'] ) ) {
$order = sanitize_text_field( $_GET['order'] );
}
$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
if ( $order === 'asc' ) {
return $result;
}
return -$result;
}
}
?>