" . __("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'); //翻译 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 post_analytics_info(){ if(function_exists('file_get_contents')){ $contexts = stream_context_create( array( 'http' => 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('/