Selfcap: overview (totals, chart, log), IP rules and settings. */ class Selfcap_Admin { private const SLUG = 'selfcap'; /** Rows per page in the verification log. The admin's last choice is remembered. */ private const PER_PAGE_OPTIONS = [10, 25, 50]; private const PER_PAGE_DEFAULT = 10; private const PER_PAGE_META = 'selfcap_per_page'; private const RANGES = [7, 30, 90]; public static function register(): void { add_action('admin_menu', [self::class, 'menu']); add_action('admin_init', [self::class, 'settings']); add_action('admin_enqueue_scripts', [self::class, 'assets']); add_action('admin_post_selfcap_add_rule', [self::class, 'add_rule']); add_action('admin_post_selfcap_delete_rule', [self::class, 'delete_rule']); add_filter('plugin_action_links_'.plugin_basename(SELFCAP_FILE), [self::class, 'action_links']); } public static function menu(): void { add_menu_page('Selfcap', 'Selfcap', 'manage_options', self::SLUG, [self::class, 'page'], 'dashicons-shield', 81); } public static function assets(string $hook): void { if ($hook !== 'toplevel_page_'.self::SLUG) { return; } $base = plugin_dir_url(SELFCAP_FILE).'assets/'; wp_enqueue_style('selfcap-admin', $base.'admin.css', [], SELFCAP_VERSION); wp_enqueue_script('selfcap-admin', $base.'admin.js', [], SELFCAP_VERSION, ['in_footer' => true]); } public static function action_links(array $links): array { array_unshift($links, ''.esc_html__('Settings', 'selfcap').''); return $links; } public static function settings(): void { register_setting('selfcap', Selfcap_Plugin::OPTION, ['sanitize_callback' => [self::class, 'sanitize']]); } public static function sanitize($input): array { $input = is_array($input) ? $input : []; $proxies = array_filter(array_map('trim', preg_split('/[\s,]+/', (string) ($input['trusted_proxies'] ?? ''))), [Selfcap_IP::class, 'is_valid_range']); return [ 'enabled' => empty($input['enabled']) ? 0 : 1, 'forms' => array_values(array_intersect(array_keys(Selfcap_Plugin::forms()), (array) ($input['forms'] ?? []))), 'theme' => in_array($input['theme'] ?? '', ['auto', 'light', 'dark'], true) ? $input['theme'] : 'auto', 'language' => isset(Selfcap_Plugin::LANGUAGES[$input['language'] ?? '']) ? $input['language'] : 'auto', 'align' => in_array($input['align'] ?? '', ['left', 'center', 'right'], true) ? $input['align'] : 'left', 'plugin_language' => isset(Selfcap_Plugin::LANGUAGES[$input['plugin_language'] ?? '']) ? $input['plugin_language'] : 'auto', 'difficulty' => max(10, min(24, (int) ($input['difficulty'] ?? 18))), 'auto_block' => empty($input['auto_block']) ? 0 : 1, 'ip_headers' => empty($input['ip_headers']) ? 0 : 1, 'block_xmlrpc' => empty($input['block_xmlrpc']) ? 0 : 1, 'trusted_proxies' => implode("\n", $proxies), 'retention_days' => max(1, min(365, (int) ($input['retention_days'] ?? 30))), 'checkout_position' => array_key_exists((string) ($input['checkout_position'] ?? ''), Selfcap_Plugin::CHECKOUT_HOOKS) ? $input['checkout_position'] : 'button', // A CSS selector, only ever used by querySelector() in the browser. 'checkout_selector' => substr(trim(sanitize_text_field((string) ($input['checkout_selector'] ?? ''))), 0, 200), ]; } private static function url(string $tab, array $args = []): string { return add_query_arg(array_merge(['page' => self::SLUG, 'tab' => $tab], $args), admin_url('admin.php')); } // ---- Rule actions --------------------------------------------------- public static function add_rule(): void { if (! current_user_can('manage_options')) { wp_die(esc_html__('Not allowed.', 'selfcap'), '', ['response' => 403]); } check_admin_referer('selfcap_add_rule'); $range = isset($_POST['ip_range']) ? trim(sanitize_text_field(wp_unslash($_POST['ip_range']))) : ''; $type = isset($_POST['type']) && $_POST['type'] === 'allow' ? 'allow' : 'block'; $reason = isset($_POST['reason']) ? sanitize_text_field(wp_unslash($_POST['reason'])) : ''; $hours = isset($_POST['hours']) ? absint($_POST['hours']) : 0; if (! Selfcap_IP::is_valid_range($range)) { wp_safe_redirect(self::url('rules', ['selfcap_notice' => 'invalid'])); exit; } Selfcap_Store::add_rule($type, $range, $reason, 'manual', $hours > 0 ? time() + $hours * HOUR_IN_SECONDS : null); wp_safe_redirect(self::url('rules', ['selfcap_notice' => 'added'])); exit; } public static function delete_rule(): void { if (! current_user_can('manage_options')) { wp_die(esc_html__('Not allowed.', 'selfcap'), '', ['response' => 403]); } $id = isset($_GET['rule']) ? absint($_GET['rule']) : 0; check_admin_referer('selfcap_delete_rule_'.$id); Selfcap_Store::delete_rule($id); wp_safe_redirect(self::url('rules', ['selfcap_notice' => 'deleted'])); exit; } // ---- Page ----------------------------------------------------------- public static function page(): void { if (! current_user_can('manage_options')) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : 'overview'; $tabs = [ 'overview' => __('Overview', 'selfcap'), 'rules' => __('IP rules', 'selfcap'), 'settings' => __('Settings', 'selfcap'), ]; if (! isset($tabs[$tab])) { $tab = 'overview'; } $enabled = Selfcap_Plugin::is_enabled(); $forms = count(array_intersect(array_keys(Selfcap_Plugin::forms()), (array) Selfcap_Plugin::options()['forms'])); ?>
get_results($wpdb->prepare('SELECT * FROM %i WHERE day >= %s ORDER BY day', $stats, $since), OBJECT_K); $total = (int) $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE (%s = '' OR result = %s) AND (%s = '' OR ip = %s)", $log, $result, $result, $ip, $ip )); $rows = (array) $wpdb->get_results($wpdb->prepare( "SELECT * FROM %i WHERE (%s = '' OR result = %s) AND (%s = '' OR ip = %s) ORDER BY id DESC LIMIT %d OFFSET %d", $log, $result, $result, $ip, $ip, $per_page, ($paged - 1) * $per_page )); // phpcs:enable $totals = array_fill_keys(Selfcap_Store::STAT_COLUMNS, 0); $series = []; $peak = 0; for ($offset = $range - 1; $offset >= 0; $offset--) { $time = $now - $offset * DAY_IN_SECONDS; $date = wp_date('Y-m-d', $time); $day = $days[$date] ?? null; $point = ['time' => $time, 'date' => $date, 'offset' => $offset]; foreach (Selfcap_Store::STAT_COLUMNS as $column) { $point[$column] = $day ? (int) $day->$column : 0; $totals[$column] += $point[$column]; } $peak = max($peak, $point['passed'] + $point['failed'] + $point['blocked']); $series[] = $point; } $answered = $totals['passed'] + $totals['failed']; $labels = [ 'passed' => __('Approved', 'selfcap'), 'failed' => __('Refused', 'selfcap'), 'blocked' => __('Blocked', 'selfcap'), ]; ?>| created_at, $now))); ?> created_at)); ?> | ip); ?> |
score); ?> | reason))) as $reason) : ?> | ||||
%s
'.esc_html(Selfcap_Plugin::client_ip()).''), ['code' => []]); ?>