" . __("Argon 主题不支持 Wordpress 4.4 以下版本,请更新 Wordpress", 'argon') . ""; } function theme_slug_setup() { add_theme_support('title-tag'); add_theme_support('post-thumbnails'); load_theme_textdomain('argon', get_template_directory() . '/languages'); } add_action('after_setup_theme','theme_slug_setup'); $argon_version = !(wp_get_theme() -> Template) ? wp_get_theme() -> Version : wp_get_theme(wp_get_theme() -> Template) -> Version; $GLOBALS['theme_version'] = $argon_version; // 强制使用本地资源,避免 CDN 加载问题 $GLOBALS['assets_path'] = get_bloginfo('template_url'); // ==================== 强制刷新缓存功能 ==================== /** * 检查强制刷新缓存是否启用 * 启用后 1 小时自动关闭 */ function argon_is_force_refresh_enabled() { $enabled_time = get_option('argon_force_refresh_enabled_time', 0); if ($enabled_time == 0) { return false; } // 检查是否超过 1 小时 if (time() - $enabled_time > 3600) { // 自动关闭 update_option('argon_force_refresh_enabled_time', 0); return false; } return true; } /** * 获取资源版本号 * 如果启用了强制刷新,返回当前时间戳 */ function argon_get_assets_version() { if (argon_is_force_refresh_enabled()) { return $GLOBALS['theme_version'] . '.' . time(); } return $GLOBALS['theme_version']; } /** * 启用强制刷新缓存 */ function argon_enable_force_refresh() { check_ajax_referer('argon_force_refresh', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(__('权限不足', 'argon')); } update_option('argon_force_refresh_enabled_time', time()); wp_send_json_success(array( 'message' => __('强制刷新已启用,将在 1 小时后自动关闭', 'argon'), 'expires_at' => time() + 3600 )); } add_action('wp_ajax_argon_enable_force_refresh', 'argon_enable_force_refresh'); /** * 关闭强制刷新缓存 */ function argon_disable_force_refresh() { check_ajax_referer('argon_force_refresh', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(__('权限不足', 'argon')); } update_option('argon_force_refresh_enabled_time', 0); wp_send_json_success(array( 'message' => __('强制刷新已关闭', 'argon') )); } add_action('wp_ajax_argon_disable_force_refresh', 'argon_disable_force_refresh'); /** * 获取强制刷新状态 */ function argon_get_force_refresh_status() { check_ajax_referer('argon_force_refresh', 'nonce'); $enabled_time = get_option('argon_force_refresh_enabled_time', 0); $is_enabled = argon_is_force_refresh_enabled(); $remaining = 0; if ($is_enabled && $enabled_time > 0) { $remaining = max(0, 3600 - (time() - $enabled_time)); } wp_send_json_success(array( 'enabled' => $is_enabled, 'remaining' => $remaining )); } add_action('wp_ajax_argon_get_force_refresh_status', 'argon_get_force_refresh_status'); //翻译 Hook function argon_locate_filter($locate){ if (substr($locate, 0, 2) == 'zh'){ if ($locate == 'zh_TW'){ return $locate; } return 'zh_CN'; } if (substr($locate, 0, 2) == 'en'){ return 'en_US'; } if (substr($locate, 0, 2) == 'ru'){ return 'ru_RU'; } return 'en_US'; } function argon_get_locate(){ if (function_exists("determine_locale")){ return argon_locate_filter(determine_locale()); } $determined_locale = get_locale(); if (is_admin()){ $determined_locale = get_user_locale(); } } function theme_locale_hook($locate, $domain){ if ($domain == 'argon'){ return argon_locate_filter($locate); } return $locate; } add_filter('theme_locale', 'theme_locale_hook', 10, 2); //更新主题版本后的兼容 $argon_last_version = get_option("argon_last_version"); if ($argon_last_version == ""){ $argon_last_version = "0.0"; } if (version_compare($argon_last_version, $GLOBALS['theme_version'], '<' )){ if (version_compare($argon_last_version, '0.940', '<')){ if (get_option('argon_mathjax_v2_enable') == 'true' && get_option('argon_mathjax_enable') != 'true'){ update_option("argon_math_render", 'mathjax2'); } if (get_option('argon_mathjax_enable') == 'true'){ update_option("argon_math_render", 'mathjax3'); } } if (version_compare($argon_last_version, '0.970', '<')){ if (get_option('argon_show_author') == 'true'){ update_option("argon_article_meta", 'time|views|comments|categories|author'); } } if (version_compare($argon_last_version, '1.1.0', '<')){ if (get_option('argon_enable_zoomify') != 'false'){ update_option("argon_enable_fancybox", 'true'); update_option("argon_enable_zoomify", 'false'); } } if (version_compare($argon_last_version, '1.3.4', '<')){ switch (get_option('argon_search_post_filter', 'post,page')){ case 'post,page': update_option("argon_enable_search_filters", 'true'); update_option("argon_search_filters_type", '*post,*page,shuoshuo'); break; case 'post,page,shuoshuo': update_option("argon_enable_search_filters", 'true'); update_option("argon_search_filters_type", '*post,*page,*shuoshuo'); break; case 'post,page,hide_shuoshuo': update_option("argon_enable_search_filters", 'true'); update_option("argon_search_filters_type", '*post,*page'); break; case 'off': default: update_option("argon_enable_search_filters", 'false'); break; } } update_option("argon_last_version", $GLOBALS['theme_version']); } //引入邮件模板系统 require_once(get_template_directory() . '/email-templates/base.php'); require_once(get_template_directory() . '/email-templates/comment-notify.php'); require_once(get_template_directory() . '/email-templates/reply-notify.php'); //检测更新 require_once(get_template_directory() . '/theme-update-checker/plugin-update-checker.php'); $argon_update_source = get_option('argon_update_source'); switch ($argon_update_source) { case "stop": break; case "fastgit": $argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker( 'https://api.solstice23.top/argon/info.json?source=fastgit', get_template_directory() . '/functions.php', 'argon' ); break; case "cfworker": $argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker( 'https://api.solstice23.top/argon/info.json?source=cfworker', get_template_directory() . '/functions.php', 'argon' ); break; case "solstice23top": $argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker( 'https://api.solstice23.top/argon/info.json?source=0', get_template_directory() . '/functions.php', 'argon' ); break; case "github": default: $argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker( 'https://raw.githubusercontent.com/solstice23/argon-theme/master/info.json', get_template_directory() . '/functions.php', 'argon' ); } //热更新功能 function argon_hot_reload_init() { // 检查是否启用热更新 if (get_option('argon_enable_hot_reload', 'false') != 'true') { return; } // 记录当前主题版本 $current_version = $GLOBALS['theme_version']; $last_known_version = get_option('argon_hot_reload_last_version', ''); // 如果版本发生变化,清理缓存并记录更新 if (!empty($last_known_version) && $last_known_version !== $current_version) { argon_clear_all_caches(); argon_record_hot_reload_update($last_known_version, $current_version); } // 更新记录的版本 update_option('argon_hot_reload_last_version', $current_version); } // 清理所有缓存 function argon_clear_all_caches() { // 清理 WordPress 对象缓存 if (function_exists('wp_cache_flush')) { wp_cache_flush(); } // 清理主题更新检查器缓存 delete_site_transient('update_themes'); delete_transient('argon_update_info'); // 清理可能存在的其他主题相关缓存 global $wpdb; $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_argon_%'"); $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_argon_%'"); $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_puc_%'"); $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_timeout_puc_%'"); // 触发缓存清理钩子,允许其他插件响应 do_action('argon_cache_cleared'); } // 记录热更新 function argon_record_hot_reload_update($old_version, $new_version) { $update_history = get_option('argon_hot_reload_history', array()); // 添加新的更新记录 $update_history[] = array( 'old_version' => $old_version, 'new_version' => $new_version, 'time' => current_time('timestamp'), 'dismissed' => false ); // 只保留最近 10 条记录 if (count($update_history) > 10) { $update_history = array_slice($update_history, -10); } update_option('argon_hot_reload_history', $update_history); } // 获取未读的更新通知 function argon_get_pending_update_notices() { $update_history = get_option('argon_hot_reload_history', array()); $pending = array(); foreach ($update_history as $index => $update) { if (empty($update['dismissed'])) { $pending[$index] = $update; } } return $pending; } // 标记更新通知为已读 function argon_dismiss_update_notice() { check_ajax_referer('argon_dismiss_update_notice', 'nonce'); $index = isset($_POST['index']) ? intval($_POST['index']) : -1; $update_history = get_option('argon_hot_reload_history', array()); if ($index === -1) { // 标记所有为已读 foreach ($update_history as &$update) { $update['dismissed'] = true; } } else if (isset($update_history[$index])) { $update_history[$index]['dismissed'] = true; } update_option('argon_hot_reload_history', $update_history); wp_send_json_success(); } add_action('wp_ajax_argon_dismiss_update_notice', 'argon_dismiss_update_notice'); // 手动触发缓存清理 function argon_manual_clear_cache() { check_ajax_referer('argon_clear_cache', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(__('权限不足', 'argon')); } argon_clear_all_caches(); wp_send_json_success(__('缓存已清理', 'argon')); } add_action('wp_ajax_argon_clear_cache', 'argon_manual_clear_cache'); // 在后台显示更新通知 function argon_admin_hot_reload_notice() { if (get_option('argon_enable_hot_reload', 'false') != 'true') { return; } $pending_notices = argon_get_pending_update_notices(); if (empty($pending_notices)) { return; } $latest = end($pending_notices); $latest_index = key($pending_notices); ?>

' . esc_html($latest['old_version']) . '', '' . esc_html($latest['new_version']) . '' ); ?>

×

' . esc_html($latest['old_version']) . '', '' . esc_html($latest['new_version']) . '' ); ?>

$error_message, 'source' => $error_source, 'muted_at' => current_time('timestamp'), 'muted_by' => wp_get_current_user()->display_name ); update_option('argon_muted_errors', $muted_errors); wp_send_json_success(); } add_action('wp_ajax_argon_mute_error', 'argon_mute_error'); // 取消屏蔽错误 function argon_unmute_error() { check_ajax_referer('argon_debug_console', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(__('权限不足', 'argon')); } $error_hash = sanitize_text_field($_POST['error_hash']); $muted_errors = argon_get_muted_errors(); if (isset($muted_errors[$error_hash])) { unset($muted_errors[$error_hash]); update_option('argon_muted_errors', $muted_errors); } wp_send_json_success(); } add_action('wp_ajax_argon_unmute_error', 'argon_unmute_error'); // 批量删除屏蔽的错误 function argon_clear_muted_errors() { check_ajax_referer('argon_debug_console', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(__('权限不足', 'argon')); } update_option('argon_muted_errors', array()); wp_send_json_success(); } add_action('wp_ajax_argon_clear_muted_errors', 'argon_clear_muted_errors'); // 在 footer 中输出调试按钮(放在页脚底部) function argon_debug_console_footer_button() { if (get_option('argon_enable_debug_console', 'false') != 'true') { return; } ?>
Theme: WP: PHP: Git: UA:
array( 'method'=>"GET", 'header'=>"User-Agent: ArgonTheme\r\n" ) ) ); $result = file_get_contents('http://api.solstice23.top/argon_analytics/index.php?domain=' . urlencode($_SERVER['HTTP_HOST']) . '&version='. urlencode($GLOBALS['theme_version']), false, $contexts); update_option('argon_has_inited', 'true'); return $result; }else{ update_option('argon_has_inited', 'true'); } } if (get_option('argon_has_inited') != 'true'){ post_analytics_info(); } //时区修正 if (get_option('argon_enable_timezone_fix') == 'true'){ date_default_timezone_set('UTC'); } //注册小工具 function argon_widgets_init() { register_sidebar( array( 'name' => __('左侧栏小工具', 'argon'), 'id' => 'leftbar-tools', 'description' => __( '左侧栏小工具 (如果设置会在侧栏增加一个 Tab)', 'argon'), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); register_sidebar( array( 'name' => __('右侧栏小工具', 'argon'), 'id' => 'rightbar-tools', 'description' => __( '右侧栏小工具 (在 "Argon 主题选项" 中选择 "三栏布局" 才会显示)', 'argon'), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); register_sidebar( array( 'name' => __('站点概览额外内容', 'argon'), 'id' => 'leftbar-siteinfo-extra-tools', 'description' => __( '站点概览额外内容', 'argon'), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); } add_action('widgets_init', 'argon_widgets_init'); //注册新后台主题配色方案 function argon_add_admin_color(){ wp_admin_css_color( 'argon', 'Argon', get_bloginfo('template_directory') . "/admin.css", array("#5e72e4", "#324cdc", "#e8ebfb"), array('base' => '#525f7f', 'focus' => '#5e72e4', 'current' => '#fff') ); } add_action('admin_init', 'argon_add_admin_color'); function argon_admin_themecolor_css(){ $themecolor = get_option("argon_theme_color", "#5e72e4"); $RGB = hexstr2rgb($themecolor); $HSL = rgb2hsl($RGB['R'], $RGB['G'], $RGB['B']); echo " "; if (get_option("argon_enable_immersion_color", "false") == "true"){ echo ""; } } add_filter('admin_head', 'argon_admin_themecolor_css'); function array_remove(&$arr, $item){ $pos = array_search($item, $arr); if ($pos !== false){ array_splice($arr, $pos, 1); } } //数字格式化 function format_number_in_kilos($number) { if ($number < 1000){ return $number; } if (1000 <= $number && $number < 1000000){ if (1000 <= $number && $number < 10000){ return round($number / 1000, 1) . "K"; }else{ return round($number / 1000, 0) . "K"; } } if (1000000 <= $number && $number <= 10000000){ return round($number / 1000000, 1) . "M"; }else{ return round($number / 1000000, 0) . "M"; } } //表情包 require_once(get_template_directory() . '/emotions.php'); //文章特色图片 function argon_get_first_image_of_article(){ global $post; if (post_password_required()){ return false; } $post_content_full = apply_filters('the_content', preg_replace( '', '', $post -> post_content)); preg_match('//', $post_content_full, $match); if (isset($match[3])){ return $match[3]; } return false; } function argon_has_post_thumbnail($postID = 0){ if ($postID == 0){ global $post; $postID = $post -> ID; } if (has_post_thumbnail()){ return true; } $argon_first_image_as_thumbnail = get_post_meta($postID, 'argon_first_image_as_thumbnail', true); if ($argon_first_image_as_thumbnail == ""){ $argon_first_image_as_thumbnail = "default"; } if ($argon_first_image_as_thumbnail == "true" || ($argon_first_image_as_thumbnail == "default" && get_option("argon_first_image_as_thumbnail_by_default", "false") == "true")){ if (argon_get_first_image_of_article() != false){ return true; } } return false; } function argon_get_post_thumbnail($postID = 0){ if ($postID == 0){ global $post; $postID = $post -> ID; } if (has_post_thumbnail()){ return apply_filters("argon_post_thumbnail", wp_get_attachment_image_src(get_post_thumbnail_id($postID), "full")[0]); } return apply_filters("argon_post_thumbnail", argon_get_first_image_of_article()); } //文末附加内容 function get_additional_content_after_post(){ global $post; $postID = $post -> ID; $res = get_post_meta($post -> ID, 'argon_after_post', true); if ($res == "--none--"){ return ""; } if ($res == ""){ $res = get_option("argon_additional_content_after_post"); } $res = str_replace("\n", "
", $res); $res = str_replace("%url%", get_permalink($postID), $res); $res = str_replace("%link%", '' . get_permalink($postID) . '', $res); $res = str_replace("%title%", get_the_title(), $res); $res = str_replace("%author%", get_the_author(), $res); return $res; } //输出分页页码 function get_argon_formatted_paginate_links($maxPageNumbers, $extraClasses = ''){ $args = array( 'prev_text' => '', 'next_text' => '', 'before_page_number' => '', 'after_page_number' => '', 'show_all' => True ); $res = paginate_links($args); //单引号转双引号 & 去除上一页和下一页按钮 $res = preg_replace( '/\'/', '"', $res ); $res = preg_replace( '/
  • '; } if ($current > 1){ $html .= '
  • '; } for ($i = $from; $i <= $to; $i++){ if ($current == $i){ $html .= '
  • ' . $i . '
  • '; }else{ $html .= '
  • ' . $i . '
  • '; } } if ($current < $total){ $html .= '
  • '; } if ($to < $total){ $html .= '
  • '; } return ''; } function get_argon_formatted_paginate_links_for_all_platforms(){ return get_argon_formatted_paginate_links(7) . get_argon_formatted_paginate_links(5, " pagination-mobile"); } //访问者 Token & Session function get_random_token(){ return md5(uniqid(microtime(true), true)); } function set_user_token_cookie(){ if (!isset($_COOKIE["argon_user_token"]) || strlen($_COOKIE["argon_user_token"]) != 32){ $newToken = get_random_token(); setcookie("argon_user_token", $newToken, array( 'expires' => time() + 10 * 365 * 24 * 60 * 60, 'path' => '/', 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax' )); $_COOKIE["argon_user_token"] = $newToken; } } function session_init(){ set_user_token_cookie(); if (!session_id()){ if (function_exists('session_set_cookie_params')){ session_set_cookie_params(array( 'lifetime' => 0, 'path' => '/', 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax' )); } session_start(); } } session_init(); //add_action('init', 'session_init'); //页面 Description Meta function get_seo_description(){ global $post; if (is_single() || is_page()){ if (get_the_excerpt() != ""){ return preg_replace('/ \[…]$/', '…', get_the_excerpt()); } if (!post_password_required()){ return htmlspecialchars(mb_substr(str_replace("\n", '', strip_tags($post -> post_content)), 0, 50)) . "..."; }else{ return __("这是一个加密页面,需要密码来查看", 'argon'); } }else{ return get_option('argon_seo_description'); } } //页面 Keywords function get_seo_keywords(){ if (is_single()){ global $post; $tags = get_the_tags('', ',', '', $post -> ID); if ($tags != null){ $res = ""; foreach ($tags as $tag){ if ($res != ""){ $res .= ","; } $res .= $tag -> name; } return $res; } } if (is_category()){ return single_cat_title('', false); } if (is_tag()){ return single_tag_title('', false); } if (is_author()){ return get_the_author(); } if (is_post_type_archive()){ return post_type_archive_title('', false); } if (is_tax()){ return single_term_title('', false); } return get_option('argon_seo_keywords'); } //页面分享预览图 function get_og_image(){ global $post; $postID = $post -> ID; $argon_first_image_as_thumbnail = get_post_meta($postID, 'argon_first_image_as_thumbnail', 'true'); if (has_post_thumbnail() || $argon_first_image_as_thumbnail == 'true'){ return argon_get_post_thumbnail($postID); } return ''; } //页面浏览量 function get_post_views($post_id){ $count_key = 'views'; $count = get_post_meta($post_id, $count_key, true); if ($count==''){ delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); $count = '0'; } return number_format_i18n($count); } function set_post_views(){ if (!is_single() && !is_page()) { return; } if (!isset($post_id)){ global $post; $post_id = $post -> ID; } if (post_password_required($post_id)){ return; } if (isset($_GET['preview'])){ if ($_GET['preview'] == 'true'){ if (current_user_can('publish_posts')){ return; } } } $noPostView = 'false'; if (isset($_POST['no_post_view'])){ $noPostView = $_POST['no_post_view']; } if ($noPostView == 'true'){ return; } global $post; if (!isset($post -> ID)){ return; } $post_id = $post -> ID; $count_key = 'views'; $count = get_post_meta($post_id, $count_key, true); if (is_single() || is_page()) { if ($count==''){ delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); } else { update_post_meta($post_id, $count_key, $count + 1); } } } add_action('get_header', 'set_post_views'); //字数和预计阅读时间 function get_article_words($str){ preg_match_all('/[\S\s]*?([\S\s]*?)<\/code>[\S\s]*?<\/pre>/im', $str, $codeSegments, PREG_PATTERN_ORDER); $codeSegments = $codeSegments[3]; $codeTotal = 0; foreach ($codeSegments as $codeSegment){ $codeLines = preg_split('/\r\n|\n|\r/', $codeSegment); foreach ($codeLines as $line){ if (strlen(trim($str)) > 0){ $codeTotal++; } } } $str = preg_replace( '/[\S\s]*?<\/code>/im', '', $str ); $str = preg_replace( '/[\S\s]*?<\/pre>/im', '', $str ); $str = preg_replace( '/[\S\s]*?<\/style>/im', '', $str ); $str = preg_replace( '/[\S\s]*?<\/script>/im', '', $str ); $str = preg_replace('/<[^>]+?>/', ' ', $str); $str = html_entity_decode(strip_tags($str)); preg_match_all('/[\x{4e00}-\x{9fa5}]/u' , $str , $cnRes); $cnTotal = count($cnRes[0]); $enRes = preg_replace('/[\x{4e00}-\x{9fa5}]/u', '', $str); preg_match_all('/[a-zA-Z0-9_\x{0392}-\x{03c9}\x{0400}-\x{04FF}]+|[\x{4E00}-\x{9FFF}\x{3400}-\x{4dbf}\x{f900}-\x{faff}\x{3040}-\x{309f}\x{ac00}-\x{d7af}\x{0400}-\x{04FF}]+|[\x{00E4}\x{00C4}\x{00E5}\x{00C5}\x{00F6}\x{00D6}]+|\w+/u' , $str , $enRes); $enTotal = count($enRes[0]); return array( 'cn' => $cnTotal, 'en' => $enTotal, 'code' => $codeTotal, ); } function get_article_words_total($str){ $res = get_article_words($str); return $res['cn'] + $res['en'] + $res['code']; } function get_reading_time($len){ $speedcn = get_option('argon_reading_speed', 300); $speeden = get_option('argon_reading_speed_en', 160); $speedcode = get_option('argon_reading_speed_code', 20); $reading_time = $len['cn'] / $speedcn + $len['en'] / $speeden + $len['code'] / $speedcode; if ($reading_time < 0.3){ return __("几秒读完", 'argon'); } if ($reading_time < 1){ return __("1 分钟内", 'argon'); } if ($reading_time < 60){ return ceil($reading_time) . " " . __("分钟", 'argon'); } return round($reading_time / 60 , 1) . " " . __("小时", 'argon'); } //当前文章是否可以生成目录 function have_catalog(){ if (!is_single() && !is_page()){ return false; } if (post_password_required()){ return false; } if (is_page() && is_page_template('timeline.php')){ return true; } $content = get_post(get_the_ID()) -> post_content; // 检查 HTML 标题标签 if (preg_match('//i', $content)){ return true; } // 检查 Gutenberg 标题块 if (preg_match('/