'array', 'sanitize_callback' => 'datoden_cache_sanitize', 'default' => datoden_cache_defaults()]); }); function datoden_cache_sanitize($in): array { $in = is_array($in) ? $in : []; $old = datoden_cache_options(); $o = $old; /* $o innehåller allt som redan är sparat – bara sidans egna värden ändras, resten ligger kvar orört */ $o['enabled'] = empty($in['enabled']) ? 0 : 1; $o['shop'] = empty($in['shop']) ? 0 : 1; $o['lifetime'] = in_array((int) ($in['lifetime'] ?? 24), [0, 1, 3, 6, 12, 24, 48, 72, 168], true) ? (int) $in['lifetime'] : 24; $lines = array_filter(array_map('trim', preg_split('/\R/', (string) ($in['exclude'] ?? '')))); $o['exclude'] = implode("\n", array_map('sanitize_text_field', $lines)); /* det som ändrar sidornas innehåll eller vem som får vilken fil → cachen töms */ foreach (['enabled', 'exclude'] as $k) { if ($o[$k] != $old[$k]) { add_action('shutdown', static function () { datoden_cache_purge_all(); }, 0); break; } } foreach ($in as $k => $v) if (substr((string) $k, -9) === '_imported') $o[$k] = empty($v) ? 0 : 1; // flyttflaggan behålls när flytten sparar return $o; } add_action('admin_enqueue_scripts', static function ($hook) { wp_register_style('datoden-cache-bar', false, [], DATODEN_CACHE_VERSION); wp_enqueue_style('datoden-cache-bar'); wp_add_inline_style('datoden-cache-bar', '#wpadminbar #wp-admin-bar-datoden-cache .ab-icon:before{content:"\f226";top:2px}'); if (substr((string) $hook, -strlen('_page_datoden-page-cache')) !== '_page_datoden-page-cache') return; wp_enqueue_style('datoden-cache-admin', datoden_core_url('dt-admin.css'), [], datoden_core_ver()); wp_enqueue_style('datoden-cache', plugins_url('assets/dt-cache.css', DATODEN_CACHE_FILE), [], DATODEN_CACHE_VERSION); wp_enqueue_script('datoden-cache', plugins_url('assets/dt-cache.js', DATODEN_CACHE_FILE), [], DATODEN_CACHE_VERSION, true); wp_localize_script('datoden-cache', 'datodenCache', [ 'ajax' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('datoden_cache'), 'i18n' => [ 'checking' => __('Checking…', 'datoden-page-cache'), 'server' => __('Fastest: the page comes straight from the server, without WordPress.', 'datoden-page-cache'), 'php' => __('Fast: the page comes from the cache via the plugin. The server does not use the .htaccess rules (for example Nginx), so WordPress starts briefly.', 'datoden-page-cache'), /* translators: %s: the reason the page did not come from the cache */ 'miss' => __('The page did not come from the cache. %s', 'datoden-page-cache'), 'failed' => __('The website could not be reached from the server. Try again.', 'datoden-page-cache'), 'wpfcAsk' => __('Take over the settings from WP Fastest Cache and turn it off? Its cache and .htaccess rules are removed.', 'datoden-page-cache'), 'emptied' => __('The cache is empty.', 'datoden-page-cache'), ], ]); wp_enqueue_script('datoden-autosave', datoden_core_url('dt-autosave.js'), [], datoden_core_ver(), true); }); /* ---------- kontrollen: kommer startsidan från cachen? ---------- */ function datoden_cache_ajax_guard(): void { check_ajax_referer('datoden_cache', 'nonce'); if (!current_user_can('manage_options')) wp_send_json_error([], 403); } add_action('wp_ajax_datoden_cache_check', static function () { datoden_cache_ajax_guard(); if (!datoden_cache_on()) wp_send_json_success(['state' => 'off', 'why' => __('The cache is turned off.', 'datoden-page-cache')]); $url = add_query_arg([], home_url('/')); $args = ['timeout' => 20, 'redirection' => 0, 'sslverify' => false, 'user-agent' => 'Mozilla/5.0 (DT-cache Check)', 'cookies' => []]; $r = null; for ($i = 0; $i < 2; $i++) { $r = wp_remote_get($url, $args); if (is_wp_error($r)) break; } // första gången sparas sidan, andra gången ska den komma från cachen if (is_wp_error($r)) wp_send_json_success(['state' => 'failed']); $hit = (string) wp_remote_retrieve_header($r, 'x-dt-cache'); $code = (int) wp_remote_retrieve_response_code($r); if (stripos($hit, 'server') !== false) wp_send_json_success(['state' => 'server']); if (stripos($hit, 'php') !== false) wp_send_json_success(['state' => 'php']); $body = (string) wp_remote_retrieve_body($r); if ($code >= 300 && $code < 400) $why = sprintf(/* translators: %s: address */ __('The front page redirects to %s.', 'datoden-page-cache'), (string) wp_remote_retrieve_header($r, 'location')); elseif ($code !== 200) $why = sprintf(/* translators: %d: HTTP status */ __('The front page answered with status %d.', 'datoden-page-cache'), $code); elseif (strpos($body, 'DT-cache:') !== false) $why = __('The page is saved, but another cache or the server sent it. Empty that cache too.', 'datoden-page-cache'); else $why = __('Something on the page stops it from being saved – for example a plugin that sets a cookie or marks the page as not cacheable (DONOTCACHEPAGE).', 'datoden-page-cache'); wp_send_json_success(['state' => 'miss', 'why' => $why]); }); add_action('wp_ajax_datoden_cache_empty', static function () { datoden_cache_ajax_guard(); datoden_cache_purge_all(); wp_send_json_success(datoden_cache_stats_view()); }); add_action('wp_ajax_datoden_cache_stats', static function () { datoden_cache_ajax_guard(); wp_send_json_success(datoden_cache_stats_view()); }); function datoden_cache_stats_view(): array { $s = datoden_cache_stats(); $hosts = []; foreach ($s['hosts'] as $h => $n) $hosts[] = $h . ': ' . $n; return [ /* translators: 1: number of pages, 2: size */ 'line' => sprintf(_n('%1$s page in the cache (%2$s).', '%1$s pages in the cache (%2$s).', $s['pages'], 'datoden-page-cache'), number_format_i18n($s['pages']), size_format($s['bytes'], 1) ?: '0 B'), 'hosts' => implode(' · ', $hosts), /* translators: %s: time ago */ 'purged' => $s['purged'] ? sprintf(__('Last emptied %s ago.', 'datoden-page-cache'), human_time_diff($s['purged'])) : '', ]; } /* ---------- flytt från WP Fastest Cache ---------- */ function datoden_cache_wpfc_settings(): array { $s = json_decode((string) get_option('WpFastestCache', ''), true); return is_array($s) ? $s : []; } add_action('admin_post_datoden_cache_wpfc', static function () { if (!current_user_can('manage_options') || !current_user_can('activate_plugins')) wp_die('', 403); check_admin_referer('datoden_cache_wpfc'); $s = datoden_cache_wpfc_settings(); $on = static function ($k) use ($s) { return !empty($s[$k]) && $s[$k] !== 'off'; }; $o = datoden_cache_options(); if ($s) { $o['enabled'] = $on('wpFastestCacheStatus') ? 1 : 0; } /* undantagen (sidor) */ $ex = json_decode((string) get_option('WpFastestCacheExclude', ''), true); $lines = array_filter(array_map('trim', preg_split('/\R/', (string) $o['exclude']))); foreach (is_array($ex) ? $ex : [] as $r) { if (($r['type'] ?? 'page') !== 'page' || empty($r['content'])) continue; $c = '/' . trim((string) $r['content'], '/') . '/'; $line = ($r['prefix'] ?? '') === 'contain' ? '*' . trim((string) $r['content'], '/') . '*' : (($r['prefix'] ?? '') === 'startwith' ? $c . '*' : $c); if (!in_array($line, $lines, true)) $lines[] = $line; } $o['exclude'] = implode("\n", $lines); $o['wpfc_imported'] = 1; update_option('datoden_cache_options', $o); $GLOBALS['datoden_cache_reload'] = true; if (!function_exists('deactivate_plugins')) require_once ABSPATH . 'wp-admin/includes/plugin.php'; if (is_plugin_active(DATODEN_CACHE_WPFC)) deactivate_plugins(DATODEN_CACHE_WPFC); // WP Fastest Cache tar själv bort sin cache och sina regler datoden_cache_rmdir(WP_CONTENT_DIR . '/cache/all'); datoden_cache_htaccess_write(); datoden_cache_purge_all(); wp_safe_redirect(admin_url('admin.php?page=datoden-page-cache&dtc=moved')); exit; }); /* ---------- sidan ---------- */ function datoden_cache_page(): void { if (!current_user_can('manage_options')) return; $o = datoden_cache_options(); if (!function_exists('is_plugin_active')) require_once ABSPATH . 'wp-admin/includes/plugin.php'; $wpfc_on = is_plugin_active(DATODEN_CACHE_WPFC); $wpfc_left = !$wpfc_on && datoden_cache_wpfc_settings() && empty($o['wpfc_imported']); $ht = datoden_cache_htaccess_path(); $ht_ok = file_exists($ht) ? wp_is_writable($ht) : wp_is_writable(dirname($ht)); $srv = datoden_cache_server(); $stats = datoden_cache_stats_view(); $lifetimes = [1 => __('1 hour', 'datoden-page-cache'), 3 => __('3 hours', 'datoden-page-cache'), 6 => __('6 hours', 'datoden-page-cache'), 12 => __('12 hours', 'datoden-page-cache'), 24 => __('24 hours (standard)', 'datoden-page-cache'), 48 => __('2 days', 'datoden-page-cache'), 72 => __('3 days', 'datoden-page-cache'), 168 => __('1 week', 'datoden-page-cache'), 0 => __('Until something changes', 'datoden-page-cache')]; $sw = static function (string $key, string $title, string $text, bool $first = false) use ($o) { ?>

__('DT-cache', 'datoden-page-cache'), 'pro' => false, 'version' => DATODEN_CACHE_VERSION, 'desc' => __('Finished pages, served straight away – the website feels instant.', 'datoden-page-cache')]); ?>

' . esc_html__('Settings', 'datoden-page-cache') . ''); return $links; });