positions = self::get_positions();
}
public static function get_positions()
{
return array(
'start' => __('At the beginning', 'all-images-ai'),
'first_h2' => __('After the first <h2> title', 'all-images-ai'),
'last_h2' => __('After the last <h2> title', 'all-images-ai'),
'all_h2' => __('After every <h2> title', 'all-images-ai'),
'random_h2' => __('After a random <h2> title', 'all-images-ai'),
);
}
public function get_columns()
{
$columns = array(
'id' => '',
'cb' => '',
'time' => __('Date', 'all-images-ai'),
'post' => __('Post', 'all-images-ai'),
'type' => __('Type', 'all-images-ai'),
'position' => __('Position', 'all-images-ai'),
'status' => __('Status', 'all-images-ai'),
'images' => __('Images', 'all-images-ai'),
'actions' => '',
);
return $columns;
}
protected function get_sortable_columns()
{
$sortable_columns = array(
'time' => array('time', false),
'type' => array('type', false),
'status' => array('status', false),
);
return $sortable_columns;
}
protected function get_bulk_actions() {
return array(
'delete' => __('Delete', 'all-images-ai'),
);
}
public function prepare_items()
{
$this->table_data = $this->get_table_data();
$columns = $this->get_columns();
$hidden = array('id');
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable, 'id');
usort($this->table_data, array(&$this, 'usort_reorder'));
$per_page = get_user_meta( get_current_user_id(), 'all_images_page_all_images_ai_generations_per_page', true );
/* pagination */
$per_page = ( !empty($per_page) ? $per_page : 20 );
$current_page = $this->get_pagenum();
$total_items = count($this->table_data);
$this->table_data = array_slice($this->table_data, (($current_page - 1) * $per_page), $per_page);
$this->set_pagination_args(array(
'total_items' => $total_items, // total number of items
'per_page' => $per_page, // items to show on a page
'total_pages' => ceil( $total_items / $per_page ) // use ceil to round up
));
$this->items = $this->table_data;
}
private function usort_reorder($a, $b)
{
$orderby = (!empty($_GET['orderby'])) ? sanitize_text_field($_GET['orderby']) : 'time';
$order = (!empty($_GET['order'])) ? sanitize_text_field($_GET['order']) : 'desc';
if (is_callable(array($this, 'column_'.$orderby))) {
$valA = $this->{'column_'.$orderby}($a);
$valB = $this->{'column_'.$orderby}($b);
} else {
$valA = $a[$orderby];
$valB = $b[$orderby];
}
$result = strcmp($a[$orderby], $b[$orderby]);
return ($order === 'asc') ? $result : -$result;
}
private function get_table_data()
{
global $wpdb;
$table = $wpdb->prefix.'allimages_generations';
return $wpdb->get_results(
"SELECT * FROM {$table} ORDER BY `time` ASC",
ARRAY_A
);
}
public function single_row( $item ) {
echo '
';
$this->single_row_columns( $item );
echo '
';
}
public function column_cb($item)
{
return sprintf(
'',
$item['id']
);
}
public function column_id($item)
{
return '';
}
public function column_time($item)
{
return date_i18n(esc_html__('Y-m-d H:i:s', 'all-images-ai'), strtotime($item['time']));
}
public function column_post($item)
{
$post_id = $item['post_id'];
if (current_user_can('edit_post', $post_id)) {
$post_link = "";
$post_link .= esc_html(get_the_title($post_id)).'';
} else {
$post_link = esc_html(get_the_title($post_id));
}
return $post_link;
}
public function column_type($item)
{
if ($item['type'] == 'featured') {
return esc_html__('Featured image', 'all-images-ai');
} else {
return esc_html__('Content image', 'all-images-ai');
}
}
public function column_position($item)
{
if ($item['position'] !== null && strpos($item['position'], 'h2_') > -1) {
$arr = explode('_', $item['position']);
return esc_html('H2 #'.$arr[1] + 1);
} else {
return ($item['type'] == 'content' && isset($this->positions[$item['position']])) ? esc_html(
$this->positions[$item['position']]
) : esc_html__('Featured image', 'all-images-ai');
}
}
public function column_status($item)
{
switch ($item['status']) {
case 'initializing':
return ''.esc_html__('Initializing...', 'all-images-ai').'';
case 'created':
case 'print.created':
return ''.esc_html__('Created, waiting...', 'all-images-ai').'';
case 'print.active':
case 'print.progress':
return ''.esc_html__('Generating...', 'all-images-ai').'';
case 'print.failed':
return ''.esc_html__('Failed', 'all-images-ai').'';
case 'no_credits':
return ''.esc_html__(
'Failed, no credits left',
'all-images-ai'
).'';
case 'print.completed':
return ''.esc_html__('Complete', 'all-images-ai').'';
}
}
public function column_images($item)
{
return (!empty($item['images']) ? self::get_gen_table_images_html(
$item['images'],
$item['chosen'],
($item['status'] === 'print.progress')
) : '');
}
public static function get_gen_table_images_html($json, $chosen, $in_progress = false)
{
$images_html = '';
$images = json_decode($json);
foreach ($images as $image) {
$class = ($image->id == $chosen) ? "pinned-overlay chosen" : "";
$class .= ($in_progress) ? "in-progress" : "";
$image->url = str_replace('.aiv1/', '.ai/v1/', $image->url);
$im_title = (!empty($image->title)) ? esc_attr($image->title) : '';
$free = (isset($image->free) && $image->free) ? 1 : 0;
$images_html .= '';
}
return $images_html;
}
public function column_actions($item)
{
$pagenum = $this->get_pagenum();
return ''.esc_html__('Delete', 'all-images-ai').'';
}
protected function get_table_classes() {
$classes = parent::get_table_classes();
if (($key = array_search('fixed', $classes)) !== false) {
unset($classes[$key]);
}
return $classes;
}
}