File: /home/slyfwmm/laretediclo/wp-content/plugins/itempress/inc/sync-tasks.php
<?php
if (!defined('ABSPATH')) {
exit;
}
function itempress_sync_normalize_type(string $type): string
{
$type = sanitize_key($type);
return in_array($type, ['home', 'tag', 'category'], true) ? $type : 'home';
}
function itempress_sync_normalize_key(string $type, string $key): string
{
$type = itempress_sync_normalize_type($type);
$key = sanitize_title($key);
if ($type === 'home' || $key === '') {
return 'default';
}
return $key;
}
function itempress_sync_task_id(string $type, string $key): string
{
$type = itempress_sync_normalize_type($type);
$key = itempress_sync_normalize_key($type, $key);
return md5($type . '|' . $key);
}
function itempress_sync_state_option(string $task_id): string
{
return 'itempress_sync_state_' . $task_id;
}
function itempress_sync_get_state(string $type, string $key): array
{
$task_id = itempress_sync_task_id($type, $key);
$state = get_option(itempress_sync_state_option($task_id), []);
return is_array($state) ? $state : [];
}
function itempress_sync_save_state(string $task_id, array $state): void
{
$state['updated_at'] = current_time('mysql');
update_option(itempress_sync_state_option($task_id), $state, false);
}
function itempress_sync_get_queue(): array
{
$queue = get_option('itempress_sync_queue', []);
if (!is_array($queue)) {
return [];
}
return array_values(array_unique(array_filter(array_map('sanitize_text_field', $queue))));
}
function itempress_sync_save_queue(array $queue): void
{
$queue = array_values(array_unique(array_filter(array_map('sanitize_text_field', $queue))));
update_option('itempress_sync_queue', $queue, false);
}
function itempress_get_next_tag()
{
global $wpdb;
$tag_name = $wpdb->get_var($wpdb->prepare(
"SELECT t.name
FROM {$wpdb->terms} t
INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
LEFT JOIN {$wpdb->termmeta} tm ON t.term_id = tm.term_id AND tm.meta_key = 'itempress_tag_done'
WHERE tt.taxonomy = 'itempress_tag'
AND tt.count > 0
AND (tm.meta_value IS NULL OR tm.meta_value = '')
ORDER BY t.term_id ASC
LIMIT 1"
));
if ($tag_name !== null && $tag_name !== false) {
return $tag_name;
}
return false;
}
function itempress_mark_tag_done(string $tag_name): void
{
$term = get_term_by('name', $tag_name, 'itempress_tag');
if ($term && !is_wp_error($term)) {
update_term_meta($term->term_id, 'itempress_tag_done', 1);
}
}