File: /var/www/vhosts/ensaco.es/httpdocs/wp-content/plugins/itempress/inc/app.php
<?php
if (!defined('ABSPATH')) {
exit;
}
function itempress_remote_api_base(): string
{
return rtrim((string) get_option('worker_primary_api', ''), '/');
}
function itempress_remote_api_key(): string
{
return (string) get_option('worker_api_key', '');
}
function itempress_log(string $message, string $file_name = 'runtime.log'): void
{
$upload = wp_upload_dir();
if (empty($upload['basedir'])) {
return;
}
$dir = trailingslashit($upload['basedir']) . 'itempress';
if (!is_dir($dir)) {
wp_mkdir_p($dir);
}
@file_put_contents(
$dir . '/' . sanitize_file_name($file_name),
'[' . current_time('mysql') . '] ' . $message . PHP_EOL,
FILE_APPEND
);
}
function itempress_get_page_number(): int
{
$page = (int) get_query_var('paged');
if ($page <= 0 && isset($_GET['page'])) {
$page = (int) $_GET['page'];
}
return max(1, $page);
}
function itempress_value_to_array($value): array
{
if (is_array($value)) {
return $value;
}
if (is_string($value)) {
$value = trim($value);
if ($value === '') {
return [];
}
$decoded = json_decode($value, true);
if (is_array($decoded)) {
return $decoded;
}
if (strpos($value, ',') !== false) {
return array_map('trim', explode(',', $value));
}
return [$value];
}
return [];
}
function itempress_pick_image_from_value($value): array
{
$url = '';
$id = '';
if (is_string($value)) {
$url = trim($value);
} elseif (is_array($value)) {
$url = (string) (
$value['imageUrl'] ??
$value['image_url'] ??
$value['url'] ??
$value['src'] ??
$value['href'] ??
$value['image'] ??
$value['large'] ??
$value['medium'] ??
$value['thumbnail'] ??
''
);
if (is_array($value['image'] ?? null)) {
$nested = itempress_pick_image_from_value($value['image']);
if ($url === '') {
$url = $nested['url'];
}
if ($id === '') {
$id = $nested['id'];
}
}
$id = (string) (
$value['imageId'] ??
$value['image_id'] ??
$value['id'] ??
$value['ebay_image_id'] ??
$id
);
}
if ($id === '' && $url !== '' && preg_match('#/images/g/([^/]+)/#i', $url, $m)) {
$id = $m[1];
}
return [
'url' => esc_url_raw($url),
'id' => sanitize_text_field($id),
];
}
function itempress_collect_gallery_images(array $item): array
{
$gallery = [];
$keys = [
'gallery_images', 'gallery_images_local', 'galleryImages', 'galleryImagesLocal',
'gallery', 'additionalImages', 'additional_images', 'images',
'image_urls', 'imageUrls', 'pictures', 'pictureUrls',
'pictureURL', 'PictureURL', 'product_images', 'productImages',
'extraImages', 'all_images', 'allImages',
];
foreach ($keys as $key) {
if (!array_key_exists($key, $item)) {
continue;
}
foreach (itempress_value_to_array($item[$key]) as $img) {
$picked = itempress_pick_image_from_value($img);
if ($picked['url'] !== '') {
$gallery[] = $picked;
}
}
}
$main_keys = ['image', 'image_url', 'imageUrl', 'thumbnail', 'thumbnail_url', 'thumbnailUrl', 'main_image', 'mainImage'];
foreach ($main_keys as $key) {
if (!array_key_exists($key, $item)) {
continue;
}
$picked = itempress_pick_image_from_value($item[$key]);
if ($picked['url'] !== '') {
array_unshift($gallery, $picked);
break;
}
}
$seen = [];
$out = [];
foreach ($gallery as $img) {
$url = $img['url'];
if ($url === '') {
continue;
}
$dedupe = strtolower($url);
if (isset($seen[$dedupe])) {
continue;
}
$seen[$dedupe] = true;
$out[] = $img;
}
return $out;
}
function itempress_clean_remote_item(array $item): array
{
$title = sanitize_text_field((string) ($item['title'] ?? $item['name'] ?? ''));
$sku = sanitize_text_field((string) ($item['sku'] ?? $item['item_id'] ?? $item['itemId'] ?? $item['legacyItemId'] ?? $item['id'] ?? ''));
$slug = sanitize_title((string) ($item['slug'] ?? $title));
if ($slug === '' && $sku !== '') {
$slug = sanitize_title($sku);
}
$main = ['url' => '', 'id' => ''];
foreach (['image', 'image_url', 'imageUrl', 'thumbnail', 'thumbnail_url', 'thumbnailUrl', 'main_image', 'mainImage'] as $key) {
if (array_key_exists($key, $item)) {
$main = itempress_pick_image_from_value($item[$key]);
if ($main['url'] !== '') {
break;
}
}
}
$gallery_objects = itempress_collect_gallery_images($item);
$gallery = [];
foreach ($gallery_objects as $img) {
$url = $img['url'];
if ($url === '') {
continue;
}
$gallery[] = $url;
}
if ($main['url'] === '' && !empty($gallery_objects[0]['url'])) {
$main = $gallery_objects[0];
}
$aspects = [];
foreach (['aspects', 'localizedAspects', 'itemSpecifics', 'specs', 'attributes'] as $key) {
if (!empty($item[$key])) {
$arr = itempress_value_to_array($item[$key]);
if (!empty($arr)) {
$aspects = $arr;
break;
}
}
}
$description = (string) ($item['description'] ?? $item['full_description'] ?? $item['fullDescription'] ?? $item['html_description'] ?? $item['htmlDescription'] ?? '');
$short = (string) ($item['short_description'] ?? $item['shortDescription'] ?? $item['subtitle'] ?? $item['summary'] ?? '');
return [
'sku' => $sku,
'title' => $title,
'slug' => $slug,
'description' => wp_kses_post($description),
'short_description' => wp_kses_post($short),
'price' => sanitize_text_field((string) ($item['price'] ?? $item['value'] ?? ($item['current_price'] ?? ''))),
'currency' => sanitize_text_field((string) ($item['currency'] ?? $item['currencyCode'] ?? $item['priceCurrency'] ?? 'USD')),
'image' => esc_url_raw($main['url']),
'image_url' => esc_url_raw((string) ($item['image_url'] ?? $item['imageUrl'] ?? $main['url'])),
'image_id' => sanitize_text_field((string) ($item['image_id'] ?? $item['imageId'] ?? $main['id'])),
'item_url' => esc_url_raw((string) ($item['item_url'] ?? $item['itemUrl'] ?? $item['itemWebUrl'] ?? $item['url'] ?? '')),
'condition' => sanitize_text_field((string) ($item['condition'] ?? $item['conditionDisplayName'] ?? '')),
'brand' => sanitize_text_field((string) ($item['brand'] ?? ($aspects['Brand'][0] ?? $aspects['Brand'] ?? ''))),
'mpn' => sanitize_text_field((string) ($item['mpn'] ?? ($aspects['MPN'][0] ?? $aspects['MPN'] ?? ''))),
'gtin' => sanitize_text_field((string) ($item['gtin'] ?? $item['ean'] ?? $item['upc'] ?? '')),
'category_id' => sanitize_text_field((string) ($item['category_id'] ?? $item['categoryId'] ?? '')),
'category_name' => sanitize_text_field((string) ($item['category_name'] ?? $item['categoryName'] ?? '')),
'tags' => !empty($item['tags']) && is_array($item['tags']) ? array_map('sanitize_text_field', $item['tags']) : [],
'gallery_images' => array_values(array_unique(array_filter($gallery))),
'gallery_images_local' => [],
'aspects' => is_array($aspects) ? $aspects : [],
'raw' => $item,
];
}
function itempress_proxy_image_url(string $image_url, string $image_id = '', string $slug = 'image', int $size = 800): string
{
$image_url = trim($image_url);
$slug = trim($slug);
$slug = sanitize_title($slug ?: 'image');
$slug = $slug ?: 'image';
$slug = preg_replace('/\.webp$/', '', $slug);
$slug = rtrim($slug, '/-');
$size = max(100, min(1600, $size));
if ($image_id === '') {
if (preg_match('#^https?://#i', $image_url) && preg_match('#/images/g/([^/]+)/#i', $image_url, $m)) {
$id = preg_replace('/[^a-zA-Z0-9\-~]/', '', $m[1]);
if ($id !== '') {
$image_id = $id;
}
}
if ($image_id === '' && preg_match('#/images/([a-zA-Z0-9\-~]+)/#', $image_url, $m)) {
$id = preg_replace('/[^a-zA-Z0-9\-~]/', '', $m[1]);
if ($id !== '') {
$image_id = $id;
}
}
if ($image_id === '' && preg_match('#[?&]worker_img_id=([a-zA-Z0-9\-~]+)#', $image_url, $m)) {
$id = preg_replace('/[^a-zA-Z0-9\-~]/', '', $m[1]);
if ($id !== '') {
$image_id = $id;
}
}
}
if ($image_id === '') {
return $image_url;
}
static $use_query = null;
if ($use_query === null) {
$use_query = (int) get_option('itempress_image_use_query', 0);
}
if ($use_query) {
return home_url('/?worker_img_id=' . rawurlencode($image_id) . '&size=' . $size);
}
return home_url('/images/' . rawurlencode($image_id) . '/' . $slug . '-' . $size . '.webp');
}
add_action('init', function () {
if (get_option('itempress_image_proxy_checked', false)) {
return;
}
itempress_verify_image_proxy();
update_option('itempress_image_proxy_checked', 1, false);
});
function itempress_verify_image_proxy(): void
{
$test_image_id = 'J1UAAOSwc9RnaHvU';
$webp_url = home_url('/images/' . $test_image_id . '/verify-200.webp');
$response = wp_remote_get($webp_url, [
'timeout' => 10,
'blocking' => true,
'sslverify' => false,
]);
$code = (int) wp_remote_retrieve_response_code($response);
if (is_wp_error($response) || $code !== 200) {
update_option('itempress_image_use_query', 1, false);
$query_url = home_url('/?worker_img_id=' . $test_image_id . '&size=200');
$q_response = wp_remote_get($query_url, [
'timeout' => 10,
'blocking' => true,
'sslverify' => false,
]);
if ((int) wp_remote_retrieve_response_code($q_response) !== 200) {
update_option('itempress_image_proxy_failed', 1, false);
}
} else {
update_option('itempress_image_use_query', 0, false);
delete_option('itempress_image_proxy_failed');
}
}
function itempress_check_for_update(): void
{
$base = itempress_remote_api_base();
if ($base === '') {
return;
}
$current_ver = ITEMPRESS_VERSION;
$check_url = $base . '/api/check-update?version=' . urlencode($current_ver);
$response = wp_remote_get($check_url, [
'timeout' => 15,
'sslverify' => false,
]);
if (is_wp_error($response)) {
return;
}
$code = (int) wp_remote_retrieve_response_code($response);
$body = json_decode(wp_remote_retrieve_body($response), true);
if ($code !== 200 || !is_array($body) || empty($body['update_available'])) {
return;
}
$download_url = $body['download_url'] ?? '';
if ($download_url === '') {
return;
}
itempress_log('Update available, downloading from: ' . $download_url, 'update.log');
itempress_download_and_install_update($download_url);
}
function itempress_download_and_install_update(string $download_url): void
{
$tmp_file = download_url($download_url);
if (is_wp_error($tmp_file)) {
itempress_log('Download failed: ' . $tmp_file->get_error_message(), 'update.log');
return;
}
global $wp_filesystem;
if (!function_exists('WP_Filesystem')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$creds = request_filesystem_credentials('', '', false, false, null);
if (!WP_Filesystem($creds)) {
@unlink($tmp_file);
return;
}
$plugin_dir = WP_PLUGIN_DIR . '/' . basename(ITEMPRESS_PATH);
$backup_dir = WP_CONTENT_DIR . '/itempress-backup-' . ITEMPRESS_VERSION;
if (is_dir($plugin_dir)) {
$wp_filesystem->move($plugin_dir, $backup_dir, true);
}
$unzip_result = unzip_file($tmp_file, WP_PLUGIN_DIR);
@unlink($tmp_file);
if (is_wp_error($unzip_result)) {
itempress_log('Unzip failed: ' . $unzip_result->get_error_message() . ', restoring backup', 'update.log');
$wp_filesystem->move($backup_dir, $plugin_dir, true);
return;
}
$wp_filesystem->delete($backup_dir, true);
itempress_log('Update installed successfully', 'update.log');
if (function_exists('flush_rewrite_rules')) {
flush_rewrite_rules(false);
}
}