'Link',
'plural' => 'Links',
'ajax' => false,
]);
$this->dbManager = new AFES_DbManager();
$this->baseUrl = AFES_Utils::get_base_url();
}
public function no_items()
{
esc_html_e('No Link found.', 'affieasy');
}
function get_columns()
{
return [
'tag' => esc_html__('Short url', 'affieasy'),
'webshop' => esc_html__('Webshop', 'affieasy'),
'label' => esc_html__('Link label', 'affieasy'),
'category' => esc_html__('Category', 'affieasy'),
'url' => esc_html__('Url', 'affieasy'),
];
}
function column_tag($item)
{
$url = $item['url'];
$url = urldecode($url);
$id=$item['id'];
$webshopId = $item['webshopId'];
$label = $item['label'];
$category = $item['category'];
$parameters = $item['parameters'];
$parameters = str_replace('"', "'", $parameters);
$parameters = urldecode($parameters);
$noFollow = $item['noFollow'];
$openInNewTab = $item['openInNewTab'];
$nonce = wp_create_nonce( 'my-nonce' );
$urlDelete = 'admin.php?page=affieasy-link&actionType=deletion&idParam='.$id.'&_wpnonce='.$nonce;
$shortUrl = $this->baseUrl . '?' . AFES_Constants::SHORT_LINK_SLUG . '=' . $id;
$editResult = sprintf('' . esc_html__('Edit', 'affieasy') . '');
$deleteResult = sprintf('' . esc_html__('Delete', 'affieasy') . '');
$result = sprintf('%1$s %2$s',
'' . esc_html($shortUrl),
$this->row_actions(array('edit' => $editResult,'delete' => $deleteResult))
);
return $result;
}
function column_shortUrl($item)
{
$shortUrl = $this->baseUrl . '?' . AFES_Constants::SHORT_LINK_SLUG . '=' . $item['id'];
return '' . esc_html($shortUrl);
}
function column_url($item)
{
$url = $item['url'];
// Static cache to avoid one DB query per row in the link list (N+1 prevention)
static $webshopCache = [];
$webshopId = $item['webshopId'];
if (!array_key_exists($webshopId, $webshopCache)) {
$webshopCache[$webshopId] = $this->dbManager->get_webshop_by_id($webshopId);
}
if ($webshopCache[$webshopId]->getEncodeUrl() === '1') {
$parameters = json_decode($item['parameters'], true);
$product_url = '';
if (is_array($parameters)) {
foreach ($parameters as $key => $value) {
if ($key === 'product_url') {
$product_url = $value;
break;
}
}
}
$url = str_replace($product_url, urlencode($product_url), $url);
}
return esc_html($url);
}
public function get_sortable_columns()
{
return array(
'tag' => array('tag', false),
'webshop' => array('webshop', false),
'label' => array('label', false),
'category' => array('category', false),
'url' => array('url', false));
}
function column_default($item, $column_name)
{
return stripslashes($item[$column_name]);
}
public function prepare_items()
{
$per_page = AFES_Constants::ITEMS_PER_PAGE;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin list-table search.
$search = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : null;
$total_items = $search === null ?
$this->dbManager->get_table_count(TABLE_LINK) :
$this->dbManager->get_link_count($search);
$data = $this->dbManager->get_link_page(
$this->get_pagenum(),
$per_page,
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin list-table sorting.
isset($_GET['orderby']) ? sanitize_key($_GET['orderby']) : null,
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin list-table sorting.
isset($_GET['order']) ? sanitize_key($_GET['order']) : null,
$search
);
$this->items = $data;
$this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns());
$this->set_pagination_args(array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil($total_items / $per_page)
));
}
}