'https://affgate.com/wp-content/uploads/demo-checker/feed/v1/manifest.json', 'stats_endpoint' => 'https://affgate.com/wp-json/affgate-games/v1/stats', 'installation_endpoint' => 'https://affgate.com/wp-json/affgate-games/v1/installations', 'registration_endpoint' => 'https://affgate.com/wp-json/affgate-games/v1/installations/register', 'share_usage_stats' => '0', 'weekly_sync' => '0', 'selected_providers' => [], 'selected_game_ids' => '', 'import_demand_only' => '0', 'update_existing_only' => '0', 'create_type_taxonomy' => '1', 'create_theme_taxonomy' => '1', 'public_provider_pages' => '0', 'public_type_pages' => '0', 'public_theme_pages' => '0', 'show_volatility' => '0', 'link_provider_terms' => '0', 'link_type_terms' => '0', 'link_theme_terms' => '0', 'slot_slug' => 'slots', 'provider_slug' => 'slot-provider', 'type_slug' => 'slot-type', 'theme_slug' => 'slot-theme', 'catalog_page_id' => 0, 'provider_page_id' => 0, 'type_page_id' => 0, 'theme_page_id' => 0, 'post_status' => 'publish', 'import_batch_size' => 20, 'enable_affgate_images' => '0', 'cache_images_to_media' => '0', 'show_related_provider_games' => '1', 'related_games_count' => 5, 'related_games_title' => 'More games from this provider', 'related_games_title_size' => 18, 'related_games_title_color' => '#374151', 'show_release_date' => '0', 'show_release_date_cards' => '0', 'show_popularity_index' => '1', 'show_popularity_index_cards' => '0', 'casino_block_enabled' => '0', 'casino_block_title' => '', 'casino_block_title_size' => 18, 'casino_block_title_color' => '#374151', 'casinos' => [], 'casino_block_position' => 'after_characteristics', 'casino_default_count' => 3, 'casino_visit_label' => 'Visit', 'casino_visit_label_custom' => '0', 'casino_visit_color' => '#166534', 'casino_visit_text_color' => '#ffffff', 'casino_review_label' => 'Review', 'casino_review_label_custom' => '0', 'casino_review_color' => '#4b5563', 'casino_review_text_color' => '#ffffff', 'casino_review_nofollow' => '0', 'show_interest_chart' => '0', 'interest_chart_slot' => '1', 'interest_chart_demo' => '1', 'interest_chart_freeplay' => '1', 'interest_chart_slot_color' => '#dc2626', 'interest_chart_demo_color' => '#f97316', 'interest_chart_freeplay_color' => '#16a34a', 'interest_chart_title' => '', 'interest_chart_title_size' => 18, 'interest_chart_title_color' => '#374151', 'show_country_chart' => '0', 'country_chart_type' => 'tn1', 'country_chart_view' => 'cv1', 'real_play_enabled' => '0', 'real_play_url' => '', 'real_play_label' => 'Real Play', 'free_play_label' => 'Play for Free', 'h1_template' => '[name]', 'demo_placeholder_image_id' => 0, 'primary_color' => '#166534', 'free_play_color' => '#166534', 'real_play_color' => '#166534', 'free_play_text_color' => '#ffffff', 'real_play_text_color' => '#ffffff', 'color_scheme' => 'light', 'catalog_layout' => 'grid', 'catalog_per_page' => 36, 'catalog_nav_mode' => 'pagination', 'catalog_default_sort' => 'popular', 'catalog_filter_provider' => '1', 'catalog_filter_type' => '1', 'catalog_filter_theme' => '1', 'catalog_filter_volatility' => '1', 'catalog_filter_sorting' => '1', 'catalog_filter_position' => 'top', 'catalog_filter_columns' => 5, 'catalog_filter_columns_v140' => '1', 'catalog_filter_collapse_desktop' => '0', 'catalog_filter_collapse_tablet' => '1', 'catalog_filter_collapse_mobile' => '1', 'delete_data_on_uninstall' => '0', ]; } function affgsc_settings(): array { $raw = get_option(AFFGSC_OPT, []); $raw = is_array($raw) ? $raw : []; $settings = array_replace(affgsc_default_settings(), $raw); if (!array_key_exists('enable_affgate_images', $raw) && array_key_exists('disable_affgate_images', $raw)) { $settings['enable_affgate_images'] = ((string)$raw['disable_affgate_images'] === '1') ? '0' : '1'; } if ((string)($settings['color_scheme'] ?? '') === 'default') { $settings['color_scheme'] = 'light'; } if (!array_key_exists('free_play_color', $raw)) { $settings['free_play_color'] = (string)($settings['primary_color'] ?? '#166534'); } if (!array_key_exists('real_play_color', $raw)) { $settings['real_play_color'] = (string)($settings['primary_color'] ?? '#166534'); } if (!array_key_exists('casino_visit_color', $raw)) { $settings['casino_visit_color'] = (string)($settings['primary_color'] ?? '#166534'); } if (!array_key_exists('catalog_filter_columns_v140', $raw)) { $settings['catalog_filter_columns'] = 5; } // Taxonomies are always imported in current plugin behavior. $settings['create_type_taxonomy'] = '1'; $settings['create_theme_taxonomy'] = '1'; return $settings; } function affgsc_setting(string $key, $default = null) { $settings = affgsc_settings(); return array_key_exists($key, $settings) ? $settings[$key] : $default; } function affgsc_sanitize_settings(array $raw): array { $selected = $raw['selected_providers'] ?? []; if (!is_array($selected)) $selected = []; $selected = array_values(array_filter(array_map('sanitize_title', array_map('strval', $selected)))); $enable_affgate_images = null; if (array_key_exists('enable_affgate_images', $raw)) { $enable_affgate_images = !empty($raw['enable_affgate_images']) ? '1' : '0'; } elseif (array_key_exists('disable_affgate_images', $raw)) { $enable_affgate_images = !empty($raw['disable_affgate_images']) ? '0' : '1'; } else { $enable_affgate_images = (string)affgsc_setting('enable_affgate_images', '0'); } $interest_chart_slot = !empty($raw['interest_chart_slot']) ? '1' : '0'; $interest_chart_demo = !empty($raw['interest_chart_demo']) ? '1' : '0'; $interest_chart_freeplay = !empty($raw['interest_chart_freeplay']) ? '1' : '0'; $has_interest_series = $interest_chart_slot === '1' || $interest_chart_demo === '1' || $interest_chart_freeplay === '1'; $casino_visit_label_input = trim(wp_strip_all_tags((string)($raw['casino_visit_label'] ?? ''))); $casino_visit_label = affgsc_sanitize_button_label($casino_visit_label_input, 'Visit'); $casino_visit_was_default = empty($raw['_casino_visit_label_custom_current']); $casino_visit_displayed = affgsc_sanitize_button_label((string)($raw['_casino_visit_label_displayed'] ?? ''), ''); $casino_visit_unchanged_default = $casino_visit_label_input === '' || ($casino_visit_was_default && $casino_visit_displayed !== '' && hash_equals($casino_visit_displayed, $casino_visit_label)); $casino_visit_label_custom = $casino_visit_unchanged_default ? '0' : '1'; if ($casino_visit_unchanged_default) $casino_visit_label = 'Visit'; $casino_review_label_input = trim(wp_strip_all_tags((string)($raw['casino_review_label'] ?? ''))); $casino_review_label = affgsc_sanitize_button_label($casino_review_label_input, 'Review'); $casino_review_was_default = empty($raw['_casino_review_label_custom_current']); $casino_review_displayed = affgsc_sanitize_button_label((string)($raw['_casino_review_label_displayed'] ?? ''), ''); $casino_review_unchanged_default = $casino_review_label_input === '' || ($casino_review_was_default && $casino_review_displayed !== '' && hash_equals($casino_review_displayed, $casino_review_label)); $casino_review_label_custom = $casino_review_unchanged_default ? '0' : '1'; if ($casino_review_unchanged_default) $casino_review_label = 'Review'; return [ 'manifest_url' => affgsc_sanitize_https_url((string)($raw['manifest_url'] ?? affgsc_default_settings()['manifest_url'])), 'stats_endpoint' => affgsc_sanitize_https_url((string)($raw['stats_endpoint'] ?? affgsc_default_settings()['stats_endpoint'])), 'installation_endpoint' => affgsc_sanitize_https_url((string)($raw['installation_endpoint'] ?? affgsc_default_settings()['installation_endpoint'])), 'registration_endpoint' => affgsc_sanitize_https_url((string)($raw['registration_endpoint'] ?? affgsc_default_settings()['registration_endpoint'])), 'share_usage_stats' => !empty($raw['share_usage_stats']) ? '1' : '0', 'weekly_sync' => !empty($raw['weekly_sync']) ? '1' : '0', 'selected_providers' => $selected, 'selected_game_ids' => affgsc_sanitize_id_list((string)($raw['selected_game_ids'] ?? '')), 'import_demand_only' => !empty($raw['import_demand_only']) ? '1' : '0', 'update_existing_only' => !empty($raw['update_existing_only']) ? '1' : '0', 'create_type_taxonomy' => '1', 'create_theme_taxonomy' => '1', 'public_provider_pages' => !empty($raw['public_provider_pages']) ? '1' : '0', 'public_type_pages' => !empty($raw['public_type_pages']) ? '1' : '0', 'public_theme_pages' => !empty($raw['public_theme_pages']) ? '1' : '0', 'show_volatility' => !empty($raw['show_volatility']) ? '1' : '0', 'link_provider_terms' => !empty($raw['link_provider_terms']) ? '1' : '0', 'link_type_terms' => !empty($raw['link_type_terms']) ? '1' : '0', 'link_theme_terms' => !empty($raw['link_theme_terms']) ? '1' : '0', // Keep legacy bases when the old fields are no longer present in the settings form. 'slot_slug' => affgsc_sanitize_slug_setting((string)($raw['slot_slug'] ?? affgsc_setting('slot_slug', 'slots'))), 'provider_slug' => affgsc_sanitize_slug_setting((string)($raw['provider_slug'] ?? affgsc_setting('provider_slug', 'slot-provider'))), 'type_slug' => affgsc_sanitize_slug_setting((string)($raw['type_slug'] ?? affgsc_setting('type_slug', 'slot-type'))), 'theme_slug' => affgsc_sanitize_slug_setting((string)($raw['theme_slug'] ?? affgsc_setting('theme_slug', 'slot-theme'))), 'catalog_page_id' => affgsc_sanitize_page_id($raw['catalog_page_id'] ?? 0), 'provider_page_id' => affgsc_sanitize_page_id($raw['provider_page_id'] ?? 0), 'type_page_id' => affgsc_sanitize_page_id($raw['type_page_id'] ?? 0), 'theme_page_id' => affgsc_sanitize_page_id($raw['theme_page_id'] ?? 0), 'post_status' => in_array(($raw['post_status'] ?? 'publish'), ['publish', 'draft'], true) ? (string)$raw['post_status'] : 'publish', 'import_batch_size' => min(100, max(1, (int)($raw['import_batch_size'] ?? 20))), 'enable_affgate_images' => $enable_affgate_images, 'cache_images_to_media' => $enable_affgate_images === '1' && !empty($raw['cache_images_to_media']) ? '1' : '0', 'show_related_provider_games' => !empty($raw['show_related_provider_games']) ? '1' : '0', 'related_games_count' => min(12, max(2, (int)($raw['related_games_count'] ?? 5))), 'related_games_title' => substr(sanitize_text_field((string)($raw['related_games_title'] ?? 'More games from this provider')), 0, 200), 'related_games_title_size' => affgsc_sanitize_title_font_size($raw['related_games_title_size'] ?? 18), 'related_games_title_color' => affgsc_sanitize_hex_color((string)($raw['related_games_title_color'] ?? '#374151'), '#374151'), 'show_release_date' => !empty($raw['show_release_date']) ? '1' : '0', 'show_release_date_cards' => !empty($raw['show_release_date_cards']) ? '1' : '0', 'show_popularity_index' => !empty($raw['show_popularity_index']) ? '1' : '0', 'show_popularity_index_cards' => !empty($raw['show_popularity_index_cards']) ? '1' : '0', 'casino_block_enabled' => !empty($raw['casino_block_enabled']) ? '1' : '0', 'casino_block_title' => substr(sanitize_text_field((string)($raw['casino_block_title'] ?? '')), 0, 200), 'casino_block_title_size' => affgsc_sanitize_title_font_size($raw['casino_block_title_size'] ?? 18), 'casino_block_title_color' => affgsc_sanitize_hex_color((string)($raw['casino_block_title_color'] ?? '#374151'), '#374151'), 'casinos' => affgsc_sanitize_casinos($raw['casinos'] ?? []), 'casino_block_position' => in_array(($raw['casino_block_position'] ?? 'after_characteristics'), ['after_demo', 'after_characteristics', 'before_chart', 'after_chart', 'before_related'], true) ? (string)$raw['casino_block_position'] : 'after_characteristics', 'casino_default_count' => min(50, max(1, (int)($raw['casino_default_count'] ?? 3))), 'casino_visit_label' => $casino_visit_label, 'casino_visit_label_custom' => $casino_visit_label_custom, 'casino_visit_color' => affgsc_sanitize_hex_color((string)($raw['casino_visit_color'] ?? '#166534')), 'casino_visit_text_color' => affgsc_sanitize_hex_color((string)($raw['casino_visit_text_color'] ?? '#ffffff'), '#ffffff'), 'casino_review_label' => $casino_review_label, 'casino_review_label_custom' => $casino_review_label_custom, 'casino_review_color' => affgsc_sanitize_hex_color((string)($raw['casino_review_color'] ?? '#4b5563'), '#4b5563'), 'casino_review_text_color' => affgsc_sanitize_hex_color((string)($raw['casino_review_text_color'] ?? '#ffffff'), '#ffffff'), 'casino_review_nofollow' => !empty($raw['casino_review_nofollow']) ? '1' : '0', 'show_interest_chart' => !empty($raw['show_interest_chart']) && $has_interest_series ? '1' : '0', 'interest_chart_slot' => $interest_chart_slot, 'interest_chart_demo' => $interest_chart_demo, 'interest_chart_freeplay' => $interest_chart_freeplay, 'interest_chart_slot_color' => affgsc_sanitize_hex_color((string)($raw['interest_chart_slot_color'] ?? '#dc2626')), 'interest_chart_demo_color' => affgsc_sanitize_hex_color((string)($raw['interest_chart_demo_color'] ?? '#f97316')), 'interest_chart_freeplay_color' => affgsc_sanitize_hex_color((string)($raw['interest_chart_freeplay_color'] ?? '#16a34a')), 'interest_chart_title' => substr(sanitize_text_field((string)($raw['interest_chart_title'] ?? '')), 0, 200), 'interest_chart_title_size' => affgsc_sanitize_title_font_size($raw['interest_chart_title_size'] ?? 18), 'interest_chart_title_color' => affgsc_sanitize_hex_color((string)($raw['interest_chart_title_color'] ?? '#374151'), '#374151'), 'show_country_chart' => !empty($raw['show_country_chart']) ? '1' : '0', 'country_chart_type' => in_array(($raw['country_chart_type'] ?? 'tn1'), ['tn1', 'tn2'], true) ? (string)$raw['country_chart_type'] : 'tn1', 'country_chart_view' => in_array(($raw['country_chart_view'] ?? 'cv1'), ['cv1', 'cv2', 'cv3'], true) ? (string)$raw['country_chart_view'] : 'cv1', 'real_play_enabled' => !empty($raw['real_play_enabled']) ? '1' : '0', 'real_play_url' => affgsc_sanitize_https_url((string)($raw['real_play_url'] ?? '')), 'real_play_label' => affgsc_sanitize_button_label((string)($raw['real_play_label'] ?? 'Real Play'), 'Real Play'), 'free_play_label' => affgsc_sanitize_button_label((string)($raw['free_play_label'] ?? 'Play for Free'), 'Play for Free'), 'h1_template' => affgsc_sanitize_h1_template((string)($raw['h1_template'] ?? '[name]')), 'demo_placeholder_image_id' => absint($raw['demo_placeholder_image_id'] ?? 0), 'primary_color' => affgsc_sanitize_hex_color((string)($raw['primary_color'] ?? '#166534')), 'free_play_color' => affgsc_sanitize_hex_color((string)($raw['free_play_color'] ?? '#166534')), 'real_play_color' => affgsc_sanitize_hex_color((string)($raw['real_play_color'] ?? '#166534')), 'free_play_text_color' => affgsc_sanitize_hex_color((string)($raw['free_play_text_color'] ?? '#ffffff')), 'real_play_text_color' => affgsc_sanitize_hex_color((string)($raw['real_play_text_color'] ?? '#ffffff')), 'color_scheme' => in_array(($raw['color_scheme'] ?? 'light'), ['light', 'dark'], true) ? (string)$raw['color_scheme'] : 'light', 'catalog_layout' => in_array(($raw['catalog_layout'] ?? 'grid'), ['grid', 'list'], true) ? (string)$raw['catalog_layout'] : 'grid', 'catalog_per_page' => min(120, max(1, (int)($raw['catalog_per_page'] ?? 36))), 'catalog_nav_mode' => in_array(($raw['catalog_nav_mode'] ?? 'pagination'), ['pagination', 'load_more'], true) ? (string)$raw['catalog_nav_mode'] : 'pagination', 'catalog_default_sort' => in_array(($raw['catalog_default_sort'] ?? 'popular'), ['popular', 'release_date', 'rating', 'title'], true) ? (string)$raw['catalog_default_sort'] : 'popular', 'catalog_filter_provider' => !empty($raw['catalog_filter_provider']) ? '1' : '0', 'catalog_filter_type' => !empty($raw['catalog_filter_type']) ? '1' : '0', 'catalog_filter_theme' => !empty($raw['catalog_filter_theme']) ? '1' : '0', 'catalog_filter_volatility' => !empty($raw['catalog_filter_volatility']) ? '1' : '0', 'catalog_filter_sorting' => !empty($raw['catalog_filter_sorting']) ? '1' : '0', 'catalog_filter_position' => in_array(($raw['catalog_filter_position'] ?? 'top'), ['off', 'left', 'top', 'bottom'], true) ? (string)$raw['catalog_filter_position'] : 'top', 'catalog_filter_columns' => min(6, max(1, (int)($raw['catalog_filter_columns'] ?? 5))), 'catalog_filter_columns_v140' => '1', 'catalog_filter_collapse_desktop' => !empty($raw['catalog_filter_collapse_desktop']) ? '1' : '0', 'catalog_filter_collapse_tablet' => !empty($raw['catalog_filter_collapse_tablet']) ? '1' : '0', 'catalog_filter_collapse_mobile' => !empty($raw['catalog_filter_collapse_mobile']) ? '1' : '0', 'delete_data_on_uninstall' => !empty($raw['delete_data_on_uninstall']) ? '1' : '0', ]; } function affgsc_sanitize_casinos($raw): array { if (!is_array($raw)) return []; $allowed_mimes = ['image/jpeg', 'image/png', 'image/webp', 'image/avif']; $casinos = []; foreach (array_slice(array_values($raw), 0, 50) as $item) { if (!is_array($item)) continue; $image_id = absint($item['image_id'] ?? 0); if ($image_id > 0 && (!wp_attachment_is_image($image_id) || !in_array((string)get_post_mime_type($image_id), $allowed_mimes, true))) { $image_id = 0; } $providers_raw = trim((string)($item['providers'] ?? '')); $games_raw = trim((string)($item['games'] ?? '')); $rating_raw = str_replace(',', '.', trim((string)($item['rating'] ?? ''))); $rating = $rating_raw !== '' && is_numeric($rating_raw) ? min(5, max(0, round((float)$rating_raw, 1))) : ''; $casino = [ 'id' => sanitize_key((string)($item['id'] ?? '')), 'image_id' => $image_id, 'name' => substr(sanitize_text_field((string)($item['name'] ?? '')), 0, 120), 'providers' => $providers_raw !== '' ? min(100000000, max(0, (int)$providers_raw)) : '', 'games' => $games_raw !== '' ? min(100000000, max(0, (int)$games_raw)) : '', 'bonus' => substr(sanitize_text_field((string)($item['bonus'] ?? '')), 0, 200), 'rating' => $rating, 'url' => affgsc_sanitize_https_url((string)($item['url'] ?? '')), 'review_url' => affgsc_sanitize_https_url((string)($item['review_url'] ?? '')), ]; if ($casino['id'] === '') $casino['id'] = sanitize_key(wp_generate_uuid4()); if ($casino['image_id'] <= 0 && $casino['name'] === '' && $casino['providers'] === '' && $casino['games'] === '' && $casino['bonus'] === '' && $casino['rating'] === '' && $casino['url'] === '' && $casino['review_url'] === '') continue; $casinos[] = $casino; } return $casinos; } function affgsc_sanitize_page_id($value): int { $page_id = absint($value); return $page_id > 0 && get_post_type($page_id) === 'page' && get_post_status($page_id) !== 'trash' ? $page_id : 0; } function affgsc_sanitize_slug_setting(string $value): string { $value = strtolower(trim($value)); $value = preg_replace('/[^a-z0-9_-]+/', '-', $value); $value = trim((string)$value, '-_'); return $value !== '' ? $value : 'slots'; } function affgsc_catalog_page_id(string $kind): int { $keys = [ 'catalog' => 'catalog_page_id', 'provider' => 'provider_page_id', 'type' => 'type_page_id', 'theme' => 'theme_page_id', ]; return isset($keys[$kind]) ? affgsc_sanitize_page_id(affgsc_setting($keys[$kind], 0)) : 0; } function affgsc_catalog_rewrite_base(string $kind): string { $legacy = [ 'catalog' => ['slot_slug', 'slots'], 'provider' => ['provider_slug', 'slot-provider'], 'type' => ['type_slug', 'slot-type'], 'theme' => ['theme_slug', 'slot-theme'], ]; if (!isset($legacy[$kind])) return 'slots'; $page_id = affgsc_catalog_page_id($kind); if ($page_id > 0) { $uri = trim((string)get_page_uri($page_id), '/'); if ($uri !== '') return $uri; } return affgsc_sanitize_slug_setting((string)affgsc_setting($legacy[$kind][0], $legacy[$kind][1])); } function affgsc_sanitize_button_label(string $value, string $fallback): string { $value = trim(wp_strip_all_tags($value)); return $value !== '' ? sanitize_text_field($value) : $fallback; } function affgsc_sanitize_h1_template(string $value): string { $value = trim(wp_strip_all_tags($value)); return $value !== '' ? sanitize_text_field($value) : '[name]'; } function affgsc_sanitize_hex_color(string $value, string $fallback = '#166534'): string { $value = trim($value); if (preg_match('/^#[0-9a-fA-F]{6}$/', $value)) { return strtolower($value); } if (preg_match('/^[0-9a-fA-F]{6}$/', $value)) { return '#' . strtolower($value); } return preg_match('/^#[0-9a-fA-F]{6}$/', $fallback) ? strtolower($fallback) : '#166534'; } function affgsc_sanitize_title_font_size($value): int { return min(72, max(10, (int)$value)); } function affgsc_format_number_plain($number, int $decimals = 0): string { $formatted = number_format_i18n($number, $decimals); $formatted = html_entity_decode((string)$formatted, ENT_QUOTES | ENT_HTML5, 'UTF-8'); return str_replace(["\xC2\xA0", "\xE2\x80\xAF"], ' ', $formatted); } function affgsc_sanitize_https_url(string $url): string { $url = esc_url_raw(trim($url), ['https']); if ($url === '' || strtolower((string)wp_parse_url($url, PHP_URL_SCHEME)) !== 'https') { return ''; } return $url; } function affgsc_is_public_game(int $post_id): bool { if ($post_id <= 0 || get_post_type($post_id) !== AFFGSC_POST_TYPE) { return false; } if (get_post_status($post_id) === 'publish') { return true; } return is_user_logged_in() && current_user_can('read_post', $post_id); } function affgsc_public_rate_limited(string $action, int $limit, int $window): bool { $action = sanitize_key($action); $limit = max(1, $limit); $window = max(MINUTE_IN_SECONDS, $window); $ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash((string)$_SERVER['REMOTE_ADDR'])) : ''; $ua = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash((string)$_SERVER['HTTP_USER_AGENT'])) : ''; $key = 'affgsc_rate_' . md5($action . '|' . $ip . '|' . $ua); $count = (int)get_transient($key); if ($count >= $limit) { return true; } set_transient($key, $count + 1, $window); return false; } function affgsc_sanitize_id_list(string $value): string { $ids = preg_split('/[\s,;]+/', $value); $ids = array_filter(array_map('absint', $ids ?: [])); $ids = array_values(array_unique($ids)); return implode(',', $ids); } function affgsc_parse_id_list(string $value): array { if (trim($value) === '') return []; return array_values(array_filter(array_map('absint', explode(',', affgsc_sanitize_id_list($value))))); } function affgsc_now(): string { return function_exists('wp_date') ? wp_date('c') : gmdate('c'); } function affgsc_shortcode_bool($value, bool $default = true): bool { $value = strtolower(trim((string)$value)); if ($value === '') return $default; return in_array($value, ['1', 'true', 'yes', 'on'], true); } function affgsc_query_value(string $key, string $default = ''): string { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public, read-only catalog filter parameters do not change server state. if (!isset($_GET[$key]) || !is_scalar($_GET[$key])) { return $default; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public, read-only catalog filter parameters do not change server state. $value = sanitize_text_field(wp_unslash((string)$_GET[$key])); return $value; } function affgsc_allowed_feed_url(string $url): bool { $host = strtolower((string)(wp_parse_url($url, PHP_URL_HOST) ?: '')); $allowed = (array)apply_filters('affgsc_allowed_feed_hosts', ['affgate.com']); $allowed = array_values(array_filter(array_map(static function ($value): string { return strtolower(trim((string)$value)); }, $allowed))); return $host !== '' && in_array($host, $allowed, true); } function affgsc_remote_get_json(string $url, array $expected = []): array { $url = affgsc_sanitize_https_url($url); if ($url === '' || !affgsc_allowed_feed_url($url)) { return ['error' => 'Invalid or insecure URL']; } $expected_bytes = max(0, (int)($expected['bytes'] ?? 0)); $limit = $expected_bytes > 0 ? min(25 * MB_IN_BYTES, $expected_bytes + 1) : 25 * MB_IN_BYTES; $res = wp_safe_remote_get($url, [ 'timeout' => 30, 'redirection' => 5, 'limit_response_size' => $limit, 'user-agent' => 'AffGate Slot Catalog/' . AFFGSC_VERSION, 'headers' => [ 'Accept' => 'application/json,text/plain,*/*;q=0.8', ], ]); if (is_wp_error($res)) { return ['error' => $res->get_error_message()]; } $code = (int)wp_remote_retrieve_response_code($res); if ($code < 200 || $code >= 300) { return ['error' => 'HTTP ' . $code]; } $body = (string)wp_remote_retrieve_body($res); if ($expected_bytes > 0 && strlen($body) !== $expected_bytes) { return ['error' => 'Feed file size does not match the signed manifest']; } $expected_hash = strtolower((string)($expected['sha256'] ?? '')); if ($expected_hash !== '' && (!preg_match('/^[a-f0-9]{64}$/', $expected_hash) || !hash_equals($expected_hash, hash('sha256', $body)))) { return ['error' => 'Feed file checksum does not match the signed manifest']; } $json = json_decode($body, true); if (!is_array($json)) { return ['error' => 'Invalid JSON']; } return $json; } function affgsc_weekly_slot(): array { $slot = get_option('affgsc_weekly_slot', []); if (is_array($slot)) { $hour = isset($slot['hour']) ? (int)$slot['hour'] : -1; $minute = isset($slot['minute']) ? (int)$slot['minute'] : -1; if ($hour >= 4 && $hour <= 9 && $minute >= 0 && $minute <= 59) { return ['hour' => $hour, 'minute' => $minute]; } } $seed = abs((int)crc32(affgsc_install_id())); $hour = 4 + ($seed % 6); $minute = (int)(($seed / 6) % 60); $slot = ['hour' => $hour, 'minute' => $minute]; update_option('affgsc_weekly_slot', $slot, false); return $slot; } function affgsc_next_weekly_timestamp(): int { $slot = affgsc_weekly_slot(); $server_tz_name = date_default_timezone_get(); try { $tz = new DateTimeZone($server_tz_name ?: 'UTC'); } catch (Throwable $e) { $tz = new DateTimeZone('UTC'); } $now = new DateTime('now', $tz); $next = clone $now; $next->setTime((int)$slot['hour'], (int)$slot['minute'], 0); if ($next->getTimestamp() <= $now->getTimestamp()) { $next->modify('+1 day'); } return (int)$next->getTimestamp(); } function affgsc_unschedule_hook(string $hook): void { $ts = wp_next_scheduled($hook); while ($ts) { wp_unschedule_event($ts, $hook); $ts = wp_next_scheduled($hook); } } function affgsc_schedule_events(): void { if ((string)affgsc_setting('weekly_sync', '0') !== '1') { affgsc_unschedule_hook('affgsc_weekly_sync'); return; } $version = (int)get_option('affgsc_weekly_schedule_version', 0); if ($version < 3) { affgsc_unschedule_hook('affgsc_weekly_sync'); wp_schedule_event(affgsc_next_weekly_timestamp(), 'affgsc_weekly', 'affgsc_weekly_sync', [], true); update_option('affgsc_weekly_schedule_version', 3, false); return; } if (!wp_next_scheduled('affgsc_weekly_sync')) { wp_schedule_event(affgsc_next_weekly_timestamp(), 'affgsc_weekly', 'affgsc_weekly_sync', [], true); } } function affgsc_schedule_installation_heartbeat(): void { if (affgsc_install_api_secret() === '') { affgsc_unschedule_hook('affgsc_installation_heartbeat'); return; } if (!wp_next_scheduled('affgsc_installation_heartbeat')) { wp_schedule_event(affgsc_next_weekly_timestamp(), 'affgsc_weekly', 'affgsc_installation_heartbeat', [], true); } } function affgsc_unschedule_events(): void { foreach (['affgsc_weekly_sync', 'affgsc_installation_heartbeat', 'affgsc_refresh_manifest_cache', 'affgsc_refresh_frequency_data', 'affgsc_import_tick', 'affgsc_cache_game_image', 'affgsc_image_cache_worker'] as $hook) { affgsc_unschedule_hook($hook); } } function affgsc_cron_schedules(array $schedules): array { if (!isset($schedules['affgsc_weekly'])) { $schedules['affgsc_weekly'] = [ 'interval' => WEEK_IN_SECONDS, 'display' => __('Weekly (AffGate Slot Catalog)', 'affgate-slot-catalog'), ]; } return $schedules; } function affgsc_add_privacy_policy_content(): void { if (!function_exists('wp_add_privacy_policy_content')) { return; } $content = '

' . esc_html__( 'AffGate Slot Catalog stores anonymous ratings, demo error reports, and catalog interaction counters in this WordPress database. A random cookie is used to limit repeat ratings and reports; IP addresses and browser user agents are stored only as one-way hashes.', 'affgate-slot-catalog' ) . '

'; $content .= '

' . esc_html__( 'The plugin retrieves catalog data from AffGate. When enabled, remote game images and demo iframes are loaded from third-party game provider domains, which may receive visitor connection data under their own privacy policies.', 'affgate-slot-catalog' ) . '

'; $content .= '

' . esc_html__( 'Before the first game import, or when weekly updates or interest charts are enabled, the plugin securely registers the installation with AffGate. Registration sends the site URL and hostname, a random installation ID, installation date, plugin version, active game count, and a one-time site-control proof. Once registered, weekly heartbeats and deactivation or deletion events send the corresponding installation status. These requests do not contain visitor activity, ratings, or demo reports.', 'affgate-slot-catalog' ) . '

'; $content .= '

' . esc_html__( 'Optional usage sharing is disabled by default. If a site administrator enables it, the site URL, a random installation ID, catalog counts, aggregated interactions, ratings, and submitted demo reports are sent to AffGate.', 'affgate-slot-catalog' ) . '

'; wp_add_privacy_policy_content(__('AffGate Slot Catalog', 'affgate-slot-catalog'), wp_kses_post($content)); } function affgsc_kick_import(): void { if (!wp_next_scheduled('affgsc_import_tick')) { wp_schedule_single_event(time() + 5, 'affgsc_import_tick', [], true); } if (function_exists('spawn_cron')) { spawn_cron(time()); } affgsc_kick_import_loopback(); } function affgsc_import_loopback_token(): string { return hash_hmac('sha256', home_url('/') . '|' . AFFGSC_VERSION, wp_salt('auth')); } function affgsc_kick_import_loopback(): void { if (get_transient('affgsc_import_loopback_pending')) { return; } set_transient('affgsc_import_loopback_pending', 1, 15); wp_remote_post(admin_url('admin-ajax.php'), [ 'timeout' => 0.5, 'blocking' => false, // This is the WordPress core hook used by loopback HTTP requests. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound 'sslverify' => apply_filters('https_local_ssl_verify', true), 'body' => [ 'action' => 'affgsc_import_loopback', 'token' => affgsc_import_loopback_token(), ], ]); }