'purchase',
'plural' => 'purchases',
'ajax' => false,
)
);
$this->repository = $repository;
}
public function get_columns(): array {
return array(
'purchaser_email' => __( 'Email', 'aibender-articlevault-pro' ),
'article' => __( 'Article', 'aibender-articlevault-pro' ),
'amount' => __( 'Amount', 'aibender-articlevault-pro' ),
'status' => __( 'Status', 'aibender-articlevault-pro' ),
'created_at' => __( 'Date', 'aibender-articlevault-pro' ),
);
}
protected function get_sortable_columns(): array {
return array(
'purchaser_email' => array(
'purchaser_email',
false,
),
'amount' => array(
'amount',
false,
),
'status' => array(
'status',
false,
),
'created_at' => array(
'created_at',
true,
),
);
}
protected function column_default(
$item,
$column_name
) {
switch ( $column_name ) {
case 'purchaser_email':
$email = sanitize_email(
(string) ( $item['purchaser_email'] ?? '' )
);
return '' !== $email
? sprintf(
'%2$s',
esc_attr( $email ),
esc_html( $email )
)
: '—';
case 'article':
$article_id = absint(
$item['article_id'] ?? 0
);
$title = get_the_title( $article_id );
if ( empty( $title ) ) {
return '—';
}
$edit_url = get_edit_post_link(
$article_id
);
if ( ! $edit_url ) {
return esc_html( $title );
}
return sprintf(
'%2$s',
esc_url( $edit_url ),
esc_html( $title )
);
case 'amount':
return sprintf(
'%s %.2f',
strtoupper(
(string) ( $item['currency'] ?? 'usd' )
),
((int) ( $item['amount'] ?? 0 )) / 100
);
case 'status':
$colours = array(
'paid' => '#46b450',
'pending' => '#dba617',
'failed' => '#d63638',
'refunded' => '#646970',
);
$status = strtolower(
(string) ( $item['status'] ?? '' )
);
$colour = $colours[ $status ] ?? '#2271b1';
return sprintf(
'%2$s',
esc_attr( $colour ),
esc_html( ucfirst( $status ) )
);
case 'created_at':
$created_at = (string) (
$item['created_at'] ?? ''
);
if ( '' === $created_at ) {
return '—';
}
$timestamp = strtotime(
$created_at
);
if ( false === $timestamp ) {
return '—';
}
return esc_html(
wp_date(
'j M Y g:i a',
$timestamp
)
);
default:
return '—';
}
}
public function prepare_items(): void {
$per_page = 20;
$page = $this->get_pagenum();
$search = '';
if ( isset( $_REQUEST['s'] ) ) {
$search = sanitize_text_field(
wp_unslash(
$_REQUEST['s']
)
);
}
$orderby = isset( $_REQUEST['orderby'] )
? sanitize_key(
wp_unslash(
$_REQUEST['orderby']
)
)
: 'created_at';
$order = isset( $_REQUEST['order'] )
? strtoupper(
sanitize_key(
wp_unslash(
$_REQUEST['order']
)
)
)
: 'DESC';
$result = $this->repository->get_purchases(
$page,
$per_page,
$search,
$orderby,
$order
);
$this->items = $result['items'];
$this->_column_headers = array(
$this->get_columns(),
array(),
$this->get_sortable_columns(),
);
$this->set_pagination_args(
array(
'total_items' => $result['total'],
'per_page' => $per_page,
'total_pages' => (int) ceil(
$result['total'] / $per_page
),
)
);
}
public function no_items(): void {
esc_html_e(
'No purchases found.',
'aibender-articlevault-pro'
);
}
}