functions.php 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. <?php
  2. /**
  3. * iro functions and definitions.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package iro
  8. */
  9. define('IRO_VERSION', wp_get_theme()->get('Version'));
  10. define('INT_VERSION', '18.0.0');
  11. define('BUILD_VERSION', '2');
  12. //Option-Framework
  13. require get_template_directory() . '/opt/option-framework.php';
  14. /**
  15. * composer autoload
  16. */
  17. require_once 'vendor/autoload.php';
  18. if (!function_exists('iro_opt')) {
  19. $GLOBALS['iro_options'] = get_option('iro_options');
  20. function iro_opt($option = '', $default = null)
  21. {
  22. return $GLOBALS['iro_options'][$option] ?? $default;
  23. }
  24. }
  25. if (!function_exists('iro_opt_update')) {
  26. function iro_opt_update($option = '', $value = null)
  27. {
  28. $options = get_option('iro_options');
  29. $options[$option] = $value;
  30. update_option('iro_options', $options);
  31. }
  32. }
  33. $shared_lib_basepath = iro_opt('shared_library_basepath') ? get_template_directory_uri() : (iro_opt('lib_cdn_path','https://fastly.jsdelivr.net/gh/mirai-mamori/Sakurairo@'). IRO_VERSION);
  34. $core_lib_basepath = iro_opt('core_library_basepath') ? get_template_directory_uri() : (iro_opt('lib_cdn_path','https://fastly.jsdelivr.net/gh/mirai-mamori/Sakurairo@'). IRO_VERSION);
  35. //Update-Checker
  36. require 'update-checker/update-checker.php';
  37. use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
  38. function UpdateCheck($url,$flag = 'Sakurairo'){
  39. return PucFactory::buildUpdateChecker(
  40. $url,
  41. __FILE__,
  42. $flag
  43. );
  44. }
  45. switch(iro_opt('iro_update_source')){
  46. case 'github':
  47. $iroThemeUpdateChecker = UpdateCheck('https://github.com/mirai-mamori/Sakurairo','Sakurairo');
  48. break;
  49. case 'upyun':
  50. $iroThemeUpdateChecker = UpdateCheck('https://update.maho.cc/jsdelivr.json');
  51. break;
  52. case 'official_building':
  53. $iroThemeUpdateChecker = UpdateCheck('https://update.maho.cc/'.iro_opt('iro_update_channel').'/check.json');
  54. }
  55. //ini_set('display_errors', true);
  56. //error_reporting(E_ALL);
  57. error_reporting(E_ALL & ~E_NOTICE);
  58. if (!function_exists('akina_setup'))
  59. {
  60. function akina_setup()
  61. {
  62. /*
  63. * Make theme available for translation.
  64. * Translations can be filed in the /languages/ directory.
  65. * If you're building a theme based on Akina, use a find and replace
  66. * to change 'akina' to the name of your theme in all the template files.
  67. */
  68. load_theme_textdomain('sakurairo', get_template_directory() . '/languages');
  69. /*
  70. * Enable support for Post Thumbnails on posts and pages.
  71. *
  72. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  73. */
  74. add_theme_support('post-thumbnails');
  75. set_post_thumbnail_size(150, 150, true);
  76. // This theme uses wp_nav_menu() in one location.
  77. register_nav_menus(array(
  78. 'primary' => __('Nav Menus', 'sakurairo'), //导航菜单
  79. ));
  80. /*
  81. * Switch default core markup for search form, comment form, and comments
  82. * to output valid HTML5.
  83. */
  84. add_theme_support('html5', array(
  85. 'search-form',
  86. 'comment-form',
  87. 'comment-list',
  88. 'gallery',
  89. 'caption',
  90. ));
  91. /*
  92. * Enable support for Post Formats.
  93. * See https://developer.wordpress.org/themes/functionality/post-formats/
  94. */
  95. add_theme_support('post-formats', array(
  96. 'aside',
  97. 'image',
  98. 'status',
  99. ));
  100. // Set up the WordPress core custom background feature.
  101. add_theme_support('custom-background', apply_filters('akina_custom_background_args', array(
  102. 'default-color' => 'ffffff',
  103. 'default-image' => '',
  104. )));
  105. /**
  106. * 废弃过时的wp_title
  107. * @seealso https://make.wordpress.org/core/2015/10/20/document-title-in-4-4/
  108. */
  109. add_theme_support( 'title-tag' );
  110. add_filter('pre_option_link_manager_enabled', '__return_true');
  111. // 优化代码
  112. //去除头部冗余代码
  113. remove_action('wp_head', 'feed_links_extra', 3);
  114. remove_action('wp_head', 'rsd_link');
  115. remove_action('wp_head', 'wlwmanifest_link');
  116. remove_action('wp_head', 'index_rel_link');
  117. remove_action('wp_head', 'start_post_rel_link', 10, 0);
  118. remove_action('wp_head', 'wp_generator');
  119. remove_action('wp_head', 'wp_generator'); //隐藏wordpress版本
  120. remove_filter('the_content', 'wptexturize'); //取消标点符号转义
  121. //remove_action('rest_api_init', 'wp_oembed_register_route');
  122. //remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
  123. //remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
  124. //remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4);
  125. //remove_action('wp_head', 'wp_oembed_add_discovery_links');
  126. //remove_action('wp_head', 'wp_oembed_add_host_js');
  127. remove_action('template_redirect', 'rest_output_link_header', 11, 0);
  128. function coolwp_remove_open_sans_from_wp_core()
  129. {
  130. wp_deregister_style('open-sans');
  131. wp_register_style('open-sans', false);
  132. wp_enqueue_style('open-sans', '');
  133. }
  134. add_action('init', 'coolwp_remove_open_sans_from_wp_core');
  135. if (!function_exists('disable_emojis')) {
  136. /**
  137. * Disable the emoji's
  138. * @see https://wordpress.org/plugins/disable-emojis/
  139. */
  140. function disable_emojis()
  141. {
  142. remove_action('wp_head', 'print_emoji_detection_script', 7);
  143. remove_action('admin_print_scripts', 'print_emoji_detection_script');
  144. remove_action('wp_print_styles', 'print_emoji_styles');
  145. remove_action('admin_print_styles', 'print_emoji_styles');
  146. remove_filter('the_content_feed', 'wp_staticize_emoji');
  147. remove_filter('comment_text_rss', 'wp_staticize_emoji');
  148. remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
  149. add_filter('tiny_mce_plugins', 'disable_emojis_tinymce');
  150. }
  151. add_action('init', 'disable_emojis');
  152. }
  153. if (!function_exists('disable_emojis_tinymce'))
  154. {
  155. /**
  156. * Filter function used to remove the tinymce emoji plugin.
  157. *
  158. * @param array $plugins
  159. * @return array Difference betwen the two arrays
  160. */
  161. function disable_emojis_tinymce($plugins)
  162. {
  163. if (is_array($plugins)) {
  164. return array_diff($plugins, array('wpemoji'));
  165. } else {
  166. return array();
  167. }
  168. }
  169. }
  170. // 移除菜单冗余代码
  171. add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
  172. add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
  173. add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
  174. function my_css_attributes_filter($var)
  175. {
  176. return is_array($var) ? array_intersect($var, array('current-menu-item', 'current-post-ancestor', 'current-menu-ancestor', 'current-menu-parent')) : '';
  177. }
  178. }
  179. };
  180. add_action('after_setup_theme', 'akina_setup');
  181. //说说页面
  182. function shuoshuo_custom_init()
  183. {
  184. $labels = array(
  185. 'name' => __("Ideas", "sakurairo"),
  186. 'singular_name' => __("Idea", "sakurairo"),
  187. 'add_new' => __("Publish New Idea", "sakurairo"),
  188. 'add_new_item' => __("Publish New Idea", "sakurairo"),
  189. 'edit_item' => __("Edit Idea", "sakurairo"),
  190. 'new_item' => __("New Idea", "sakurairo"),
  191. 'view_item' => __("View Idea", "sakurairo"),
  192. 'search_items' => __("Search Idea", "sakurairo"),
  193. 'not_found' => __("Not Found Idea", "sakurairo"),
  194. 'not_found_in_trash' => __("No Idea in the Trash", "sakurairo"),
  195. 'parent_item_colon' => '',
  196. 'menu_name' => __("Ideas", "sakurairo")
  197. );
  198. $args = array(
  199. 'labels' => $labels,
  200. 'public' => true,
  201. 'publicly_queryable' => true,
  202. 'show_ui' => true,
  203. 'show_in_menu' => true,
  204. 'query_var' => true,
  205. 'rewrite' => true,
  206. 'capability_type' => 'post',
  207. 'has_archive' => true,
  208. 'hierarchical' => false,
  209. 'menu_position' => null,
  210. 'supports' => array(
  211. 'title',
  212. 'editor',
  213. 'author'
  214. )
  215. );
  216. register_post_type('shuoshuo', $args);
  217. }
  218. add_action('init', 'shuoshuo_custom_init');
  219. function admin_lettering()
  220. {
  221. echo '<style>body{font-family: Microsoft YaHei;}</style>';
  222. }
  223. add_action('admin_head', 'admin_lettering');
  224. /**
  225. * Set the content width in pixels, based on the theme's design and stylesheet.
  226. *
  227. * Priority 0 to make it available to lower priority callbacks.
  228. *
  229. * @global int $content_width
  230. */
  231. function akina_content_width()
  232. {
  233. $GLOBALS['content_width'] = apply_filters('akina_content_width', 640);
  234. }
  235. add_action('after_setup_theme', 'akina_content_width', 0);
  236. /**
  237. * Enqueue scripts and styles.
  238. */
  239. function sakura_scripts()
  240. {
  241. global $core_lib_basepath;
  242. global $shared_lib_basepath;
  243. if (iro_opt('smoothscroll_option')) {
  244. wp_enqueue_script('SmoothScroll', $shared_lib_basepath. '/js/smoothscroll.js', array(), IRO_VERSION . iro_opt('cookie_version', ''), true);
  245. }
  246. wp_enqueue_style('saukra-css', $core_lib_basepath.'/style.css', array(), IRO_VERSION);
  247. if (!is_home()){
  248. //非主页的资源
  249. wp_enqueue_style('entry-content',
  250. $core_lib_basepath.'/css/theme/'.(iro_opt('entry_content_style') == 'sakurairo' ?'sakura' : 'github').'.css',
  251. array(), IRO_VERSION);
  252. wp_enqueue_script('app-page', $core_lib_basepath . '/js/page.js', array('app','polyfills'), IRO_VERSION, true);
  253. }
  254. wp_enqueue_script('app', $core_lib_basepath . '/js/app.js', array('polyfills'), IRO_VERSION, true);
  255. wp_enqueue_script('polyfills', $core_lib_basepath . '/js/polyfill.js', array(), IRO_VERSION, true);
  256. if (is_singular() && comments_open() && get_option('thread_comments')) {
  257. wp_enqueue_script('comment-reply');
  258. }
  259. //前端脚本本地化
  260. if (get_locale() != 'zh_CN') {
  261. wp_localize_script('app', '_sakurairoi18n', array(
  262. '复制成功!' => __("Copied!", 'sakurairo'),
  263. '拷贝代码' => __("Copy Code", 'sakurairo'),
  264. '你的封面API好像不支持跨域调用,这种情况下缓存是不会生效的哦' => __("Your cover API seems to not support Cross Origin Access. In this case, Cover Cache won't take effect.", 'sakurairo'),
  265. '提交中....' => __('Commiting....', 'sakurairo'),
  266. '提交成功' => __('Succeed', 'sakurairo'),
  267. '每次上传上限为10张' => __('10 files max per request', 'sakurairo'),
  268. "图片上传大小限制为5 MB\n\n「{0}」\n\n这张图太大啦~请重新上传噢!" => __("5 MB max per file.\n\n「{0}」\n\nThis image is too large~Please reupload!", 'sakurairo'),
  269. '上传中...' => __('Uploading...', 'sakurairo'),
  270. '图片上传成功~' => __('Uploaded successfully~', 'sakurairo'),
  271. "上传失败!\n文件名=> {0}\ncode=> {1}\n{2}" => __("Upload failed!\nFile Name=> {0}\ncode=> {1}\n{2}", 'sakurairo'),
  272. '上传失败,请重试.' => __('Upload failed, please retry.', 'sakurairo'),
  273. '页面加载出错了 HTTP {0}' => __("Page Load failed. HTTP {0}", 'sakurairo'),
  274. '很高兴你翻到这里,但是真的没有了...' => __("Glad you come, but we've got nothing left.", 'sakurairo'),
  275. "文章" => __("Post", 'sakurairo'),
  276. "标签" => __("Tag", 'sakurairo'),
  277. "分类" => __("Category", 'sakurairo'),
  278. "页面" => __("Page", 'sakurairo'),
  279. "评论" => __("Comment", 'sakurairo'),
  280. "已暂停..." => __("Paused...", 'sakurairo'),
  281. "正在载入视频 ..." => __("Loading Video...", 'sakurairo'),
  282. "将从网络加载字体,流量请注意" => __("Downloading fonts, be aware of your data usage.", 'sakurairo'),
  283. "您真的要设为私密吗?" => __("Are you sure you want set it private?", 'sakurairo'),
  284. "您之前已设过私密评论" => __("You had set private comment before", 'sakurairo')
  285. ));
  286. }
  287. }
  288. add_action('wp_enqueue_scripts', 'sakura_scripts');
  289. /**
  290. * load .php.
  291. */
  292. require get_template_directory() . '/inc/decorate.php';
  293. require get_template_directory() . '/inc/swicher.php';
  294. require get_template_directory() . '/inc/api.php';
  295. /**
  296. * Custom template tags for this theme.
  297. */
  298. require get_template_directory() . '/inc/template-tags.php';
  299. /**
  300. * Customizer additions.
  301. */
  302. require get_template_directory() . '/inc/customizer.php';
  303. /**
  304. * function update
  305. */
  306. require get_template_directory() . '/inc/theme_plus.php';
  307. require get_template_directory() . '/inc/categories-images.php';
  308. //Comment Location Start
  309. function convertip($ip)
  310. {
  311. if (empty($ip)) $ip = get_comment_author_IP();
  312. $ch = curl_init();
  313. $url = 'https://api.nmxc.ltd/ip/' . $ip;
  314. $timeout = 5;
  315. curl_setopt($ch, CURLOPT_URL, $url);
  316. // curl_setopt ($ch, CURLOPT_URL, 'http://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip='.$ip);
  317. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  318. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  319. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  320. $file_contents = curl_exec($ch);
  321. curl_close($ch);
  322. $result = null;
  323. $result = json_decode($file_contents, true);
  324. if ($result && $result['code'] != 0) {
  325. return "未知";
  326. }
  327. if ($result['data']['country'] != '中国') {
  328. return $result['data']['country'];
  329. }
  330. return $result['data']['country'] . '&nbsp;·&nbsp;' . $result['data']['region'] . '&nbsp;·&nbsp;' . $result['data']['city'];
  331. }
  332. //Comment Location End
  333. if (!function_exists('akina_comment_format')) {
  334. function akina_comment_format($comment, $args, $depth)
  335. {
  336. $GLOBALS['comment'] = $comment;
  337. ?>
  338. <li <?php comment_class(); ?> id="comment-<?php echo esc_attr(comment_ID()); ?>">
  339. <div class="contents">
  340. <div class="comment-arrow">
  341. <div class="main shadow">
  342. <div class="profile">
  343. <a href="<?php comment_author_url(); ?>" target="_blank" rel="nofollow"><?php echo str_replace('src=', 'src="' . iro_opt('load_in_svg') . '" onerror="imgError(this,1)" data-src=', get_avatar($comment->comment_author_email, '80', '', get_comment_author(), array('class' => array('lazyload')))); ?></a>
  344. </div>
  345. <div class="commentinfo">
  346. <section class="commeta">
  347. <div class="left">
  348. <h4 class="author"><a href="<?php comment_author_url(); ?>" target="_blank" rel="nofollow"><?php echo get_avatar($comment->comment_author_email, '24', '', get_comment_author()); ?><span class="bb-comment isauthor" title="<?php _e('Author', 'sakurairo'); ?>"><?php _e('Blogger', 'sakurairo'); /*博主*/ ?></span> <?php comment_author(); ?> <?php echo get_author_class($comment->comment_author_email, $comment->user_id); ?></a></h4>
  349. </div>
  350. <?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
  351. <div class="right">
  352. <div class="info"><time datetime="<?php comment_date('Y-m-d'); ?>"><?php echo poi_time_since(strtotime($comment->comment_date), true); //comment_date(get_option('date_format'));
  353. ?></time><?= siren_get_useragent($comment->comment_agent); ?><?php echo mobile_get_useragent_icon($comment->comment_agent); ?>&nbsp;<?php if (iro_opt('comment_location')) {
  354. _e('Location', 'sakurairo'); /*来自*/ ?>: <?php echo convertip(get_comment_author_ip());
  355. } ?>
  356. <?php if (current_user_can('manage_options') and (wp_is_mobile() == false)) {
  357. $comment_ID = $comment->comment_ID;
  358. $i_private = get_comment_meta($comment_ID, '_private', true);
  359. $flag = null;
  360. $flag .= ' <i class="fa-regular fa-snowflake"></i> <a href="javascript:;" data-actionp="set_private" data-idp="' . get_comment_id() . '" id="sp" class="sm">' . __("Private", "sakurairo") . ': <span class="has_set_private">';
  361. if (!empty($i_private)) {
  362. $flag .= __("Yes", "sakurairo") . ' <i class="fa-solid fa-lock"></i>';
  363. } else {
  364. $flag .= __("No", "sakurairo") . ' <i class="fa-solid fa-lock-open"></i>';
  365. }
  366. $flag .= '</span></a>';
  367. $flag .= edit_comment_link('<i class="fa-solid fa-pen-to-square"></i> ' . __("Edit", "mashiro"), ' <span style="color:rgba(0,0,0,.35)">', '</span>');
  368. echo $flag;
  369. } ?></div>
  370. </div>
  371. </section>
  372. </div>
  373. <div class="body">
  374. <?php comment_text(); ?>
  375. </div>
  376. </div>
  377. <div class="arrow-left"></div>
  378. </div>
  379. </div>
  380. <hr>
  381. <?php
  382. }
  383. }
  384. /**
  385. * 获取访客VIP样式
  386. */
  387. function get_author_class($comment_author_email, $user_id)
  388. {
  389. global $wpdb;
  390. $author_count = count($wpdb->get_results(
  391. "SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' "
  392. ));
  393. # 等级梯度
  394. $lv_array = [0, 5, 10, 20, 40, 80, 160];
  395. $Lv = 0;
  396. foreach ($lv_array as $key => $value) {
  397. if ($value >= $author_count) break;
  398. $Lv = $key;
  399. }
  400. // $Lv = $author_count < 5 ? 0 : ($author_count < 10 ? 1 : ($author_count < 20 ? 2 : ($author_count < 40 ? 3 : ($author_count < 80 ? 4 : ($author_count < 160 ? 5 : 6)))));
  401. echo "<span class=\"showGrade{$Lv}\" title=\"Lv{$Lv}\"><img src=\"".iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/')."comment_level/level_{$Lv}.svg\" style=\"height: 1.5em; max-height: 1.5em; display: inline-block;\"></span>";
  402. }
  403. /**
  404. * post views
  405. */
  406. function restyle_text($number)
  407. {
  408. switch (iro_opt('statistics_format')) {
  409. case "type_2": //23,333 次访问
  410. return number_format($number);
  411. case "type_3": //23 333 次访问
  412. return number_format($number, 0, '.', ' ');
  413. case "type_4": //23k 次访问
  414. if ($number >= 1000) {
  415. return round($number / 1000, 2) . 'k';
  416. }
  417. return $number;
  418. default:
  419. return $number;
  420. }
  421. }
  422. function set_post_views()
  423. {
  424. if (!is_singular()) return;
  425. global $post;
  426. $post_id = intval($post->ID);
  427. if (!$post_id) return;
  428. $views = (int) get_post_meta($post_id, 'views', true);
  429. if (!update_post_meta($post_id, 'views', ($views + 1))) {
  430. add_post_meta($post_id, 'views', 1, true);
  431. }
  432. }
  433. add_action('get_header', 'set_post_views');
  434. function get_post_views($post_id) {
  435. // 检查传入的参数是否有效
  436. if (empty($post_id) || !is_numeric($post_id)) {
  437. return 'Error: Invalid post ID.';
  438. }
  439. // 检查 WP-Statistics 插件是否安装
  440. if ((function_exists('wp_statistics_pages')) && (iro_opt('statistics_api') == 'wp_statistics')){
  441. // 使用 WP-Statistics 插件获取浏览量
  442. $views = wp_statistics_pages('total', 'uri', $post_id);
  443. return empty($views) ? 0 : $views;
  444. } else {
  445. // 使用文章自定义字段获取浏览量
  446. $views = get_post_meta($post_id, 'views', true);
  447. return empty($views) ? 0 : $views;
  448. }
  449. }
  450. function is_webp(): bool
  451. {
  452. return (isset($_COOKIE['su_webp']) || (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')));
  453. }
  454. /*
  455. * 友情链接
  456. */
  457. function get_the_link_items($id = null)
  458. {
  459. $bookmarks = get_bookmarks('orderby=date&category=' . $id);
  460. $output = '';
  461. if (!empty($bookmarks)) {
  462. $output .= '<ul class="link-items fontSmooth">';
  463. foreach ($bookmarks as $bookmark) {
  464. if (empty($bookmark->link_description)) {
  465. $bookmark->link_description = __('This guy is so lazy ╮(╯▽╰)╭', 'sakurairo');
  466. }
  467. if (empty($bookmark->link_image)) {
  468. $bookmark->link_image = 'https://s.nmxc.ltd/sakurairo_vision/@2.6/basic/friendlink.jpg';
  469. }
  470. $output .= '<li class="link-item"><a class="link-item-inner effect-apollo" href="' . $bookmark->link_url . '" title="' . $bookmark->link_description . '" target="_blank" rel="friend"><img class="lazyload" onerror="imgError(this,1)" data-src="' . $bookmark->link_image . '" src="' . iro_opt('load_in_svg') . '"></br><span class="sitename" style="'. $bookmark->link_notes .'">' . $bookmark->link_name . '</span><div class="linkdes">' . $bookmark->link_description . '</div></a></li>';
  471. }
  472. $output .= '</ul>';
  473. }
  474. return $output;
  475. }
  476. function get_link_items()
  477. {
  478. $linkcats = get_terms('link_category');
  479. $result = null;
  480. if (empty($linkcats)) return get_the_link_items();
  481. $link_category_need_display = get_post_meta(get_queried_object_id(), 'link_category_need_display', false);
  482. foreach ($linkcats as $linkcat) {
  483. if (!empty($link_category_need_display) && !in_array($linkcat->name, $link_category_need_display, true)) {
  484. continue;
  485. }
  486. $result .= '<h3 class="link-title"><span class="link-fix">' . $linkcat->name . '</span></h3>';
  487. if ($linkcat->description) {
  488. $result .= '<div class="link-description">' . $linkcat->description . '</div>';
  489. }
  490. $result .= get_the_link_items($linkcat->term_id);
  491. }
  492. return $result;
  493. }
  494. /*
  495. * Gravatar头像使用中国服务器
  496. */
  497. function gravatar_cn(string $url):string
  498. {
  499. $gravatar_url = array('0.gravatar.com/avatar', '1.gravatar.com/avatar', '2.gravatar.com/avatar', 'secure.gravatar.com/avatar');
  500. return str_replace($gravatar_url, iro_opt('gravatar_proxy'), $url);
  501. }
  502. if (iro_opt('gravatar_proxy')) {
  503. add_filter('get_avatar_url', 'gravatar_cn', 4);
  504. }
  505. /*
  506. * 检查主题版本号,并在更新主题后执行设置选项值的更新
  507. */
  508. function visual_resource_updates($specified_version, $option_name, $new_value) {
  509. $theme = wp_get_theme();
  510. $current_version = $theme->get('Version');
  511. if (version_compare($current_version, $specified_version, '>')) {
  512. $option_value = iro_opt($option_name);
  513. if (strpos($option_value, '@') === false || substr($option_value, strpos($option_value, '@') + 1) !== $new_value) {
  514. $option_value = preg_replace('/@.*/', '@' . $new_value, $option_value);
  515. }
  516. iro_opt_update($option_name, $option_value);
  517. }
  518. }
  519. visual_resource_updates('2.5.6', 'vision_resource_basepath', '2.6/');
  520. /*
  521. * 阻止站内文章互相Pingback
  522. */
  523. function theme_noself_ping(&$links)
  524. {
  525. $home = get_option('home');
  526. foreach ($links as $l => $link) {
  527. if (0 === strpos($link, $home)) {
  528. unset($links[$l]);
  529. }
  530. }
  531. }
  532. add_action('pre_ping', 'theme_noself_ping');
  533. /*
  534. * 订制body类
  535. */
  536. function akina_body_classes($classes)
  537. {
  538. // Adds a class of group-blog to blogs with more than 1 published author.
  539. if (is_multi_author()) {
  540. $classes[] = 'group-blog';
  541. }
  542. // Adds a class of hfeed to non-singular pages.
  543. if (!is_singular()) {
  544. $classes[] = 'hfeed';
  545. }
  546. // 定制中文字体class
  547. $classes[] = 'chinese-font';
  548. /*if(!wp_is_mobile()) {
  549. $classes[] = 'serif';
  550. }*/
  551. if (isset($_COOKIE['dark' . iro_opt('cookie_version', '')])) {
  552. $classes[] = $_COOKIE['dark' . iro_opt('cookie_version', '')] == '1' ? 'dark' : ' ';
  553. } else {
  554. $classes[] = ' ';
  555. }
  556. return $classes;
  557. }
  558. add_filter('body_class', 'akina_body_classes');
  559. /*
  560. * 图片CDN
  561. */
  562. add_filter('upload_dir', 'wpjam_custom_upload_dir');
  563. function wpjam_custom_upload_dir($uploads)
  564. {
  565. /* $upload_path = '';
  566. */ $upload_url_path = iro_opt('image_cdn');
  567. /* if (empty($upload_path) || 'wp-content/uploads' == $upload_path) {
  568. $uploads['basedir'] = WP_CONTENT_DIR . '/uploads';
  569. } elseif (0 !== strpos($upload_path, ABSPATH)) {
  570. $uploads['basedir'] = path_join(ABSPATH, $upload_path);
  571. } else {
  572. $uploads['basedir'] = $upload_path;
  573. } */
  574. $uploads['path'] = $uploads['basedir'] . $uploads['subdir'];
  575. if ($upload_url_path) {
  576. $uploads['baseurl'] = $upload_url_path;
  577. $uploads['url'] = $uploads['baseurl'] . $uploads['subdir'];
  578. }
  579. return $uploads;
  580. }
  581. /*
  582. * 删除自带小工具
  583. */
  584. function unregister_default_widgets()
  585. {
  586. unregister_widget('WP_Widget_Pages');
  587. unregister_widget('WP_Widget_Calendar');
  588. unregister_widget('WP_Widget_Archives');
  589. unregister_widget('WP_Widget_Links');
  590. unregister_widget('WP_Widget_Meta');
  591. unregister_widget('WP_Widget_Search');
  592. //unregister_widget('WP_Widget_Text');
  593. unregister_widget('WP_Widget_Categories');
  594. unregister_widget('WP_Widget_Recent_Posts');
  595. //unregister_widget('WP_Widget_Recent_Comments');
  596. //unregister_widget('WP_Widget_RSS');
  597. //unregister_widget('WP_Widget_Tag_Cloud');
  598. unregister_widget('WP_Nav_Menu_Widget');
  599. }
  600. add_action("widgets_init", "unregister_default_widgets", 11);
  601. /**
  602. * Jetpack setup function.
  603. *
  604. * See: https://jetpack.com/support/infinite-scroll/
  605. * See: https://jetpack.com/support/responsive-videos/
  606. */
  607. function akina_jetpack_setup()
  608. {
  609. // Add theme support for Infinite Scroll.
  610. add_theme_support('infinite-scroll', array(
  611. 'container' => 'main',
  612. 'render' => 'akina_infinite_scroll_render',
  613. 'footer' => 'page',
  614. ));
  615. // Add theme support for Responsive Videos.
  616. add_theme_support('jetpack-responsive-videos');
  617. }
  618. add_action('after_setup_theme', 'akina_jetpack_setup');
  619. /**
  620. * Custom render function for Infinite Scroll.
  621. */
  622. function akina_infinite_scroll_render()
  623. {
  624. while (have_posts()) {
  625. the_post();
  626. get_template_part('tpl/content', is_search() ? 'search' : get_post_format());
  627. }
  628. }
  629. /*
  630. * 编辑器增强
  631. */
  632. function enable_more_buttons($buttons)
  633. {
  634. $buttons[] = 'hr';
  635. $buttons[] = 'del';
  636. $buttons[] = 'sub';
  637. $buttons[] = 'sup';
  638. $buttons[] = 'fontselect';
  639. $buttons[] = 'fontsizeselect';
  640. $buttons[] = 'cleanup';
  641. $buttons[] = 'styleselect';
  642. $buttons[] = 'wp_page';
  643. $buttons[] = 'anchor';
  644. $buttons[] = 'backcolor';
  645. return $buttons;
  646. }
  647. add_filter("mce_buttons_3", "enable_more_buttons");
  648. // 下载按钮
  649. function download($atts, $content = null)
  650. {
  651. return '<a class="download" href="' . $content . '" rel="external"
  652. target="_blank" title="'.__("Download Link","sakurairo").'">
  653. <span><i class="fa-solid fa-download"></i>Download</span></a>';
  654. }
  655. add_shortcode("download", "download");
  656. add_action('after_wp_tiny_mce', 'bolo_after_wp_tiny_mce');
  657. function bolo_after_wp_tiny_mce($mce_settings)
  658. {
  659. ?>
  660. <script type="text/javascript">
  661. QTags.addButton('download', '下载按钮', "[download]下载地址[/download]");
  662. function bolo_QTnextpage_arg1() {}
  663. </script>
  664. <?php }
  665. /*
  666. * 后台登录页
  667. * @M.J
  668. */
  669. //Login Page style
  670. function custom_login()
  671. {
  672. require get_template_directory() . '/inc/login_addcss.php';
  673. //echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('template_directory') . '/inc/login.css" />'."\n";
  674. echo '<link rel="stylesheet" type="text/css" href="' . get_template_directory_uri() . '/inc/login.css?' . IRO_VERSION . '" />' . "\n";
  675. //echo '<script type="text/javascript" src="'.get_bloginfo('template_directory').'/js/jquery.min.js"></script>'."\n";
  676. }
  677. add_action('login_head', 'custom_login');
  678. //Login Page Title
  679. function custom_headertitle($title)
  680. {
  681. return get_bloginfo('name');
  682. }
  683. add_filter('login_headertext', 'custom_headertitle');
  684. //Login Page Link
  685. function custom_loginlogo_url($url)
  686. {
  687. return esc_url(home_url('/'));
  688. }
  689. add_filter('login_headerurl', 'custom_loginlogo_url');
  690. //Login Page Footer
  691. function custom_html()
  692. {
  693. $loginbg = iro_opt('login_background') ?: iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'series/login_background.webp'; ?>
  694. <script type="text/javascript">
  695. document.body.insertAdjacentHTML("afterbegin", "<div class=\"loading\"><img src=\"<?=iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/')?>basic/login_loading.gif\" width=\"58\" height=\"10\"></div>");
  696. document.head.insertAdjacentHTML("afterbegin", "<style>.show{opacity:1;}.hide{opacity:0;transition: opacity 400ms;}</style>");
  697. const loading = document.querySelector(".loading"),
  698. src = "<?= $loginbg ?>",
  699. afterLoaded = () => {
  700. document.body.style.backgroundImage = `url(${src})`
  701. loading.classList.add("hide");
  702. loading.classList.remove("show");
  703. loading.addEventListener("transitionend", () => {
  704. loading.remove()
  705. });
  706. },
  707. img = document.createElement('img')
  708. img.src = src
  709. img.addEventListener('load',afterLoaded,{once:true})
  710. <?php //3秒钟内加载不到图片也移除加载中提示
  711. ?>
  712. setTimeout(afterLoaded, 3000)
  713. document.addEventListener("DOMContentLoaded", ()=>{
  714. document.querySelector("h1 a").style.backgroundImage = "url('<?= iro_opt('login_logo_img')?>')";
  715. forgetmenot = document.querySelector(".forgetmenot");
  716. if (forgetmenot){
  717. forgetmenot.outerHTML = '<p class="forgetmenot"><?=__("Remember me","sakurairo")?><input name="rememberme" id="rememberme" value="forever" type="checkbox"><label for="rememberme" style="float: right;margin-top: 5px;transform: scale(2);margin-right: -10px;"></label></p>';
  718. }
  719. const captchaimg = document.getElementById("captchaimg");
  720. captchaimg && captchaimg.addEventListener("click",(e)=>{
  721. fetch("<?= rest_url('sakura/v1/captcha/create')?>")
  722. .then(resp=>resp.json())
  723. .then(json=>{
  724. e.target.src = json["data"];
  725. document.querySelector("input[name=\'timestamp\']").value = json["time"];
  726. document.querySelector("input[name=\'id\']").value = json["id"];
  727. });
  728. })
  729. }, false);
  730. </script>
  731. <?php
  732. }
  733. add_action('login_footer', 'custom_html');
  734. //Login message
  735. //* Add custom message to WordPress login page
  736. function smallenvelop_login_message($message)
  737. {
  738. return empty($message) ? '<p class="message"><strong>You may try 3 times for every 5 minutes!</strong></p>' : $message;
  739. }
  740. //add_filter( 'login_message', 'smallenvelop_login_message' );
  741. //Fix password reset bug </>
  742. function resetpassword_message_fix($message)
  743. {
  744. return str_replace(['>','<'], '', $message);
  745. // $message = str_replace('<', '', $message);
  746. // $message = str_replace('>', '', $message);
  747. // return $message;
  748. }
  749. add_filter('retrieve_password_message', 'resetpassword_message_fix');
  750. //Fix register email bug </>
  751. function new_user_message_fix($message)
  752. {
  753. $show_register_ip = '注册IP | Registration IP: ' . get_the_user_ip() . ' (' . convertip(get_the_user_ip()) . ")\r\n\r\n如非本人操作请忽略此邮件 | Please ignore this email if this was not your operation.\r\n\r\n";
  754. $message = str_replace('To set your password, visit the following address:', $show_register_ip . '在此设置密码 | To set your password, visit the following address:', $message);
  755. $message = str_replace('<', '', $message);
  756. $message = str_replace('>', "\r\n\r\n设置密码后在此登录 | Login here after setting password: ", $message);
  757. return $message;
  758. }
  759. add_filter('wp_new_user_notification_email', 'new_user_message_fix');
  760. /*
  761. * 评论邮件回复
  762. */
  763. function comment_mail_notify($comment_id)
  764. {
  765. $mail_user_name = iro_opt('mail_user_name') ? iro_opt('mail_user_name') : 'poi';
  766. $comment = get_comment($comment_id);
  767. $parent_id = $comment->comment_parent ?: '';
  768. $spam_confirmed = $comment->comment_approved;
  769. $mail_notify = iro_opt('mail_notify') ? get_comment_meta($parent_id, 'mail_notify', false) : false;
  770. $admin_notify = iro_opt('admin_notify') ? '1' : ((isset(get_comment($parent_id)->comment_author_email) && get_comment($parent_id)->comment_author_email) != get_bloginfo('admin_email') ? '1' : '0');
  771. if (($parent_id != '') && ($spam_confirmed != 'spam') && ($admin_notify != '0') && (!$mail_notify)) {
  772. $wp_email = $mail_user_name . '@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  773. $to = trim(get_comment($parent_id)->comment_author_email);
  774. $subject = '你在 [' . get_option("blogname") . '] 的留言有了回应';
  775. $message = '
  776. <div style="background: white;
  777. width: 95%;
  778. max-width: 800px;
  779. margin: auto auto;
  780. border-radius: 5px;
  781. border: ' . iro_opt('theme_skin') . ' 1px solid;
  782. overflow: hidden;
  783. -webkit-box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.12);
  784. box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.18);">
  785. <header style="overflow: hidden;">
  786. <img style="width:100%;z-index: 666;" src="' . iro_opt('mail_img') . '">
  787. </header>
  788. <div style="padding: 5px 20px;">
  789. <p style="position: relative;
  790. color: white;
  791. float: left;
  792. z-index: 999;
  793. background: ' . iro_opt('theme_skin') . ';
  794. padding: 5px 30px;
  795. margin: -25px auto 0 ;
  796. box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.30)">Dear&nbsp;' . trim(get_comment($parent_id)->comment_author) . '</p>
  797. <br>
  798. <h3>您有一条来自<a style="text-decoration: none;color: ' . iro_opt('theme_skin') . ' " target="_blank" href="' . home_url() . '/">' . get_option("blogname") . '</a>的回复</h3>
  799. <br>
  800. <p style="font-size: 14px;">您在文章《' . get_the_title($comment->comment_post_ID) . '》上发表的评论:</p>
  801. <div style="border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">'
  802. . trim(get_comment($parent_id)->comment_content) . '</div>
  803. <p style="font-size: 14px;">' . trim($comment->comment_author) . ' 给您的回复如下:</p>
  804. <div style="border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">'
  805. . trim($comment->comment_content) . '</div>
  806. <div style="text-align: center;">
  807. <img src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'basic/comment-mail.png" alt="hr" style="width:100%;
  808. margin:5px auto 5px auto;
  809. display: block;">
  810. <a style="text-transform: uppercase;
  811. text-decoration: none;
  812. font-size: 14px;
  813. border: 2px solid #6c7575;
  814. color: #2f3333;
  815. padding: 10px;
  816. display: inline-block;
  817. margin: 10px auto 0; " target="_blank" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">点击查看回复的完整內容</a>
  818. </div>
  819. <p style="font-size: 12px;text-align: center;color: #999;">本邮件为系统自动发出,请勿直接回复<br>
  820. &copy; ' . date('Y') . ' ' . get_option("blogname") . '</p>
  821. </div>
  822. </div>
  823. ';
  824. $message = convert_smilies($message);
  825. $message = str_replace('{{', '<img src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'/smilies/bilipng/emoji_', $message);
  826. $message = str_replace('}}', '.png" alt="emoji" style="height: 2em; max-height: 2em;">', $message);
  827. $message = str_replace('{UPLOAD}', 'https://i.loli.net/', $message);
  828. $message = str_replace('[/img][img]', '[/img^img]', $message);
  829. $message = str_replace('[img]', '<img src="', $message);
  830. $message = str_replace('[/img]', '" style="width:80%;display: block;margin-left: auto;margin-right: auto;">', $message);
  831. $message = str_replace('[/img^img]', '" style="width:80%;display: block;margin-left: auto;margin-right: auto;"><img src="', $message);
  832. $from = 'From: "' . get_option('blogname') . "\" <$wp_email>";
  833. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  834. wp_mail($to, $subject, $message, $headers);
  835. }
  836. }
  837. add_action('comment_post', 'comment_mail_notify');
  838. /*
  839. * 链接新窗口打开
  840. */
  841. function rt_add_link_target($content)
  842. {
  843. $content = str_replace('<a', '<a rel="nofollow"', $content);
  844. // use the <a> tag to split into segments
  845. $bits = explode('<a ', $content);
  846. // loop though the segments
  847. foreach ($bits as $key => $bit) {
  848. // fix the target="_blank" bug after the link
  849. if (strpos($bit, 'href') === false) {
  850. continue;
  851. }
  852. // fix the target="_blank" bug in the codeblock
  853. if (strpos(preg_replace('/code([\s\S]*?)\/code[\s]*/m', 'temp', $content), $bit) === false) {
  854. continue;
  855. }
  856. // find the end of each link
  857. $pos = strpos($bit, '>');
  858. // check if there is an end (only fails with malformed markup)
  859. if ($pos !== false) {
  860. // get a string with just the link's attibutes
  861. $part = substr($bit, 0, $pos);
  862. // for comparison, get the current site/network url
  863. $siteurl = network_site_url();
  864. // if the site url is in the attributes, assume it's in the href and skip, also if a target is present
  865. if (strpos($part, $siteurl) === false && strpos($part, 'target=') === false) {
  866. // add the target attribute
  867. $bits[$key] = 'target="_blank" ' . $bits[$key];
  868. }
  869. }
  870. }
  871. // re-assemble the content, and return it
  872. return implode('<a ', $bits);
  873. }
  874. add_filter('comment_text', 'rt_add_link_target');
  875. // 评论通过BBCode插入图片
  876. function comment_picture_support($content)
  877. {
  878. $content = str_replace('http://', 'https://', $content); // 干掉任何可能的 http
  879. $content = str_replace('{UPLOAD}', 'https://i.loli.net/', $content);
  880. $content = str_replace('[/img][img]', '[/img^img]', $content);
  881. $content = str_replace('[img]', '<br><img src="' . iro_opt('load_in_svg') . '" data-src="', $content);
  882. $content = str_replace('[/img]', '" class="lazyload comment_inline_img" onerror="imgError(this)"><br>', $content);
  883. $content = str_replace('[/img^img]', '" class="lazyload comment_inline_img" onerror="imgError(this)"><img src="' . iro_opt('load_in_svg') . '" data-src="', $content);
  884. return $content;
  885. }
  886. add_filter('comment_text', 'comment_picture_support');
  887. /*
  888. * 修改评论表情调用路径
  889. */
  890. // 简单遍历系统表情库,今后应考虑标识表情包名——使用增加的扩展名,同时保留原有拓展名
  891. // 还有一个思路是根据表情调用路径来判定<-- 此法最好!
  892. // 贴吧
  893. $wpsmiliestrans = array();
  894. function push_tieba_smilies()
  895. {
  896. global $wpsmiliestrans;
  897. // don't bother setting up smilies if they are disabled
  898. if (!get_option('use_smilies'))
  899. return;
  900. $tiebaname = array('good', 'han', 'spray', 'Grievance', 'shui', 'reluctantly', 'anger', 'tongue', 'se', 'haha', 'rmb', 'doubt', 'tear', 'surprised2', 'Happy', 'ku', 'surprised', 'theblackline', 'smilingeyes', 'spit', 'huaji', 'bbd', 'hu', 'shame', 'naive', 'rbq', 'britan', 'aa', 'niconiconi', 'niconiconi_t', 'niconiconit', 'awesome');
  901. $return_smiles = '';
  902. $type = is_webp() ? 'webp' : 'png';
  903. $tiebaimgdir = 'tieba' . $type . '/';
  904. $smiliesgs = '.' . $type;
  905. foreach ($tiebaname as $tieba_Name) {
  906. // 选择面版
  907. $return_smiles = $return_smiles . '<span title="' . $tieba_Name . '" onclick="grin(' . "'" . $tieba_Name . "'" . ',type = \'tieba\')"><img loading="lazy" src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'smilies/' . $tiebaimgdir . 'icon_' . $tieba_Name . $smiliesgs . '" /></span>';
  908. // 正文转换
  909. $wpsmiliestrans['::' . $tieba_Name . '::'] = '<span title="' . $tieba_Name . '" onclick="grin(' . "'" . $tieba_Name . "'" . ',type = \'tieba\')"><img loading="lazy" src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'smilies/' . $tiebaimgdir . 'icon_' . $tieba_Name . $smiliesgs . '" /></span>';
  910. }
  911. return $return_smiles;
  912. }
  913. push_tieba_smilies();
  914. function tieba_smile_filter($content)
  915. {
  916. global $wpsmiliestrans;
  917. $content = str_replace(array_keys($wpsmiliestrans), $wpsmiliestrans, $content);
  918. return $content;
  919. }
  920. add_filter('the_content', 'tieba_smile_filter'); //替换文章关键词
  921. add_filter('comment_text', 'tieba_smile_filter'); //替换评论关键词
  922. function push_emoji_panel()
  923. {
  924. return '
  925. <a class="emoji-item">(⌒▽⌒)</a>
  926. <a class="emoji-item">( ̄▽ ̄)</a>
  927. <a class="emoji-item">(=・ω・=)</a>
  928. <a class="emoji-item">(`・ω・´)</a>
  929. <a class="emoji-item">(〜 ̄△ ̄)〜</a>
  930. <a class="emoji-item">(・∀・)</a>
  931. <a class="emoji-item">(°∀°)ノ</a>
  932. <a class="emoji-item">( ̄3 ̄)</a>
  933. <a class="emoji-item">╮( ̄▽ ̄)╭</a>
  934. <a class="emoji-item">(´_ゝ`)</a>
  935. <a class="emoji-item">←_←</a>
  936. <a class="emoji-item">→_→</a>
  937. <a class="emoji-item">(&lt;_&lt;)</a>
  938. <a class="emoji-item">(&gt;_&gt;)</a>
  939. <a class="emoji-item">(;¬_¬)</a>
  940. <a class="emoji-item">("▔□▔)/</a>
  941. <a class="emoji-item">(゚Д゚≡゚д゚)!?</a>
  942. <a class="emoji-item">Σ(゚д゚;)</a>
  943. <a class="emoji-item">Σ( ̄□ ̄||)</a>
  944. <a class="emoji-item">(’;ω;‘)</a>
  945. <a class="emoji-item">(/TДT)/</a>
  946. <a class="emoji-item">(^・ω・^ )</a>
  947. <a class="emoji-item">(。・ω・。)</a>
  948. <a class="emoji-item">(● ̄(エ) ̄●)</a>
  949. <a class="emoji-item">ε=ε=(ノ≧∇≦)ノ</a>
  950. <a class="emoji-item">(’・_・‘)</a>
  951. <a class="emoji-item">(-_-#)</a>
  952. <a class="emoji-item">( ̄へ ̄)</a>
  953. <a class="emoji-item">( ̄ε(# ̄)Σ</a>
  954. <a class="emoji-item">ヽ(‘Д’)ノ</a>
  955. <a class="emoji-item">(#-_-)┯━┯</a>
  956. <a class="emoji-item">(╯°口°)╯(┴—┴</a>
  957. <a class="emoji-item">←◡←</a>
  958. <a class="emoji-item">( ♥д♥)</a>
  959. <a class="emoji-item">_(:3」∠)_</a>
  960. <a class="emoji-item">Σ&gt;―(〃°ω°〃)♡→</a>
  961. <a class="emoji-item">⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄</a>
  962. <a class="emoji-item">(╬゚д゚)▄︻┻┳═一</a>
  963. <a class="emoji-item">・*・:≡( ε:)</a>
  964. <a class="emoji-item">(笑)</a>
  965. <a class="emoji-item">(汗)</a>
  966. <a class="emoji-item">(泣)</a>
  967. <a class="emoji-item">(苦笑)</a>
  968. ';
  969. }
  970. // bilibili smiles
  971. $bilismiliestrans = array();
  972. function push_bili_smilies()
  973. {
  974. global $bilismiliestrans;
  975. $name = array('baiyan', 'bishi', 'bizui', 'chan', 'dai', 'daku', 'dalao', 'dalian', 'dianzan', 'doge', 'facai', 'fanu', 'ganga', 'guilian', 'guzhang', 'haixiu', 'heirenwenhao', 'huaixiao', 'jingxia', 'keai', 'koubizi', 'kun', 'lengmo', 'liubixue', 'liuhan', 'liulei', 'miantian', 'mudengkoudai', 'nanguo', 'outu', 'qinqin', 'se', 'shengbing', 'shengqi', 'shuizhao', 'sikao', 'tiaokan', 'tiaopi', 'touxiao', 'tuxue', 'weiqu', 'weixiao', 'wunai', 'xiaoku', 'xieyanxiao', 'yiwen', 'yun', 'zaijian', 'zhoumei', 'zhuakuang');
  976. $return_smiles = '';
  977. $type = is_webp() ? 'webp' : 'png';
  978. $biliimgdir = 'bili' . $type . '/';
  979. $smiliesgs = '.' . $type;
  980. foreach ($name as $smilies_Name) {
  981. // 选择面版
  982. $return_smiles = $return_smiles . '<span title="' . $smilies_Name . '" onclick="grin(' . "'" . $smilies_Name . "'" . ',type = \'Math\')"><img loading="lazy" src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'smilies/' . $biliimgdir . 'emoji_' . $smilies_Name . $smiliesgs . '" /></span>';
  983. // 正文转换
  984. $bilismiliestrans['{{' . $smilies_Name . '}}'] = '<span title="' . $smilies_Name . '" onclick="grin(' . "'" . $smilies_Name . "'" . ',type = \'Math\')"><img loading="lazy" src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'smilies/' . $biliimgdir . 'emoji_' . $smilies_Name . $smiliesgs . '" /></span>';
  985. }
  986. return $return_smiles;
  987. }
  988. push_bili_smilies();
  989. function bili_smile_filter($content)
  990. {
  991. global $bilismiliestrans;
  992. $content = str_replace(array_keys($bilismiliestrans), $bilismiliestrans, $content);
  993. return $content;
  994. }
  995. add_filter('the_content', 'bili_smile_filter'); //替换文章关键词
  996. add_filter('comment_text', 'bili_smile_filter'); //替换评论关键词
  997. function featuredtoRSS($content)
  998. {
  999. global $post;
  1000. if (has_post_thumbnail($post->ID)) {
  1001. $content = '<div>' . get_the_post_thumbnail($post->ID, 'medium', array('style' => 'margin-bottom: 15px;')) . '</div>' . $content;
  1002. }
  1003. return $content;
  1004. }
  1005. add_filter('the_excerpt_rss', 'featuredtoRSS');
  1006. add_filter('the_content_feed', 'featuredtoRSS');
  1007. //
  1008. function bili_smile_filter_rss($content)
  1009. {
  1010. $type = is_webp() ? 'webp' : 'png';
  1011. $biliimgdir = 'bili' . $type . '/';
  1012. $smiliesgs = '.' . $type;
  1013. $content = str_replace('{{', '<img src="'.iro_opt('vision_resource_basepath','https://s.nmxc.ltd/sakurairo_vision/@2.6/').'smilies/' . $biliimgdir, $content);
  1014. $content = str_replace('}}', $smilesgs . '" alt="emoji" style="height: 2em; max-height: 2em;">', $content);
  1015. $content = str_replace('[img]', '<img src="', $content);
  1016. $content = str_replace('[/img]', '" style="display: block;margin-left: auto;margin-right: auto;">', $content);
  1017. return $content;
  1018. }
  1019. add_filter('comment_text_rss', 'bili_smile_filter_rss'); //替换评论rss关键词
  1020. function toc_support($content)
  1021. {
  1022. $content = str_replace('[toc]', '<div class="has-toc have-toc"></div>', $content); // TOC 支持
  1023. $content = str_replace('[begin]', '<span class="begin">', $content); // 首字格式支持
  1024. $content = str_replace('[/begin]', '</span>', $content); // 首字格式支持
  1025. return $content;
  1026. }
  1027. add_filter('the_content', 'toc_support');
  1028. add_filter('the_excerpt_rss', 'toc_support');
  1029. add_filter('the_content_feed', 'toc_support');
  1030. function check_title_tags($content) {
  1031. $dom = new DOMDocument();
  1032. @$dom->loadHTML($content);
  1033. $headings = $dom->getElementsByTagName('h1');
  1034. for ($i = 1; $i <= 6; $i++) {
  1035. $headings = $dom->getElementsByTagName('h' . $i);
  1036. foreach ($headings as $heading) {
  1037. if (trim($heading->nodeValue) != '') {
  1038. return true;
  1039. }
  1040. }
  1041. }
  1042. return false;
  1043. }
  1044. // 显示访客当前 IP
  1045. function get_the_user_ip()
  1046. {
  1047. // if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  1048. // //check ip from share internet
  1049. // $ip = $_SERVER['HTTP_CLIENT_IP'];
  1050. // } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  1051. // //to check ip is pass from proxy
  1052. // $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  1053. // } else {
  1054. // $ip = $_SERVER['REMOTE_ADDR'];
  1055. // }
  1056. // 简略版
  1057. // $ip = $_SERVER['HTTP_CLIENT_IP'] ?: ($_SERVER['HTTP_X_FORWARDED_FOR'] ?: $_SERVER['REMOTE_ADDR']);
  1058. $ip = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
  1059. return apply_filters('wpb_get_ip', $ip);
  1060. }
  1061. add_shortcode('show_ip', 'get_the_user_ip');
  1062. /*歌词*/
  1063. function hero_get_lyric()
  1064. {
  1065. /** These are the lyrics to Hero */
  1066. $lyrics = "";
  1067. // Here we split it into lines
  1068. $lyrics = explode("\n", $lyrics);
  1069. // And then randomly choose a line
  1070. return wptexturize($lyrics[mt_rand(0, count($lyrics) - 1)]);
  1071. }
  1072. // This just echoes the chosen line, we'll position it later
  1073. function hello_hero()
  1074. {
  1075. $chosen = hero_get_lyric();
  1076. echo $chosen;
  1077. }
  1078. /*私密评论*/
  1079. add_action('wp_ajax_nopriv_siren_private', 'siren_private');
  1080. add_action('wp_ajax_siren_private', 'siren_private');
  1081. function siren_private()
  1082. {
  1083. $comment_id = $_POST["p_id"];
  1084. $action = $_POST["p_action"];
  1085. if ($action == 'set_private') {
  1086. update_comment_meta($comment_id, '_private', 'true');
  1087. $i_private = get_comment_meta($comment_ID, '_private', true);
  1088. echo empty($i_private) ? '是' : '否';
  1089. }
  1090. die;
  1091. }
  1092. //时间序列
  1093. function memory_archives_list() {
  1094. // 尝试从缓存中获取结果
  1095. $cache_key = 'memory_archives_list_' . get_locale();
  1096. $output = get_transient($cache_key);
  1097. if ($output !== false) {
  1098. echo $output;
  1099. return;
  1100. }
  1101. $output = '<div id="archives"><p style="text-align:right;">[<span id="al_expand_collapse">' . __("All expand/collapse", "sakurairo") /*全部展开/收缩*/ . '</span>]<!-- (注: 点击月份可以展开)--></p>';
  1102. $posts = get_posts(array(
  1103. 'posts_per_page' => -1,
  1104. 'ignore_sticky_posts' => true,
  1105. 'post_type' => 'post'
  1106. ));
  1107. $posts_sorted_by_time = [];
  1108. foreach ($posts as $post) {
  1109. $post_id = $post->ID;
  1110. $post_time = get_post_time('U', false, $post_id);
  1111. $posts_sorted_by_time[$post_time] = $post;
  1112. }
  1113. krsort($posts_sorted_by_time); // 按时间倒序排列
  1114. $year = 0;
  1115. $mon = 0;
  1116. foreach ($posts_sorted_by_time as $post) {
  1117. setup_postdata($post);
  1118. $year_tmp = get_post_time('Y', false, $post);
  1119. $mon_tmp = get_post_time('m', false, $post);
  1120. $post_id = $post->ID;
  1121. $post_views = get_post_views($post_id);
  1122. if ($mon != $mon_tmp && $mon > 0) {
  1123. $output .= '</ul></li>';
  1124. }
  1125. if ($year != $year_tmp && $year > 0) {
  1126. $output .= '</ul>';
  1127. }
  1128. if ($year != $year_tmp) {
  1129. $year = $year_tmp;
  1130. $output .= '<h3 class="al_year">' . $year . __(" ", "year", "sakurairo") . /*年*/ ' </h3><ul class="al_mon_list">'; //输出年份
  1131. }
  1132. if ($mon != $mon_tmp) {
  1133. $mon = $mon_tmp;
  1134. $output .= '<li class="al_li"><span class="al_mon"><span style="color:' . iro_opt('theme_skin') . ';">' . get_post_time('M', false, $post) . '</span> (<span id="post-num"></span>' . __(" post(s)", "sakurairo") /*篇文章*/ . ')</span><ul class="al_post_list">'; //输出月份
  1135. }
  1136. $output .= '<li>' . '<a href="' . get_permalink($post) . '"><span style="color:' . iro_opt('theme_skin') . ';">' /*get_post_time('d'.__(" ","sakurairo"), false, $post) 日*/ . '</span>' . get_the_title($post) . ' <span>(' . $post_views . ' <span class="fa-regular fa-gem" aria-hidden="true"></span> / ' . get_comments_number($post) . ' <span class="fa-regular fa-comment-dots" aria-hidden="true"></span>)</span></a></li>';
  1137. }
  1138. wp_reset_postdata();
  1139. $output .= '</ul></li></ul> <!--<ul class="al_mon_list"><li><ul class="al_post_list" style="display: block;"><li>博客已经萌萌哒运行了<span id="monitorday"></span>天</li></ul></li></ul>--></div>';
  1140. set_transient($cache_key, $output, 3600);
  1141. echo $output;
  1142. }
  1143. // 在保存文章后清空缓存
  1144. function clear_memory_archives_list_cache($post_id) {
  1145. delete_transient('memory_archives_list_' . get_locale());
  1146. }
  1147. add_action('save_post', 'clear_memory_archives_list_cache');
  1148. /*
  1149. * 隐藏 Dashboard
  1150. */
  1151. /* Remove the "Dashboard" from the admin menu for non-admin users */
  1152. function remove_dashboard()
  1153. {
  1154. global $current_user, $menu, $submenu;
  1155. wp_get_current_user();
  1156. if (!in_array('administrator', $current_user->roles)) {
  1157. reset($menu);
  1158. $page = key($menu);
  1159. while ((__('Dashboard') != $menu[$page][0]) && next($menu)) {
  1160. $page = key($menu);
  1161. }
  1162. if (__('Dashboard') == $menu[$page][0]) {
  1163. unset($menu[$page]);
  1164. }
  1165. reset($menu);
  1166. $page = key($menu);
  1167. while (!$current_user->has_cap($menu[$page][1]) && next($menu)) {
  1168. $page = key($menu);
  1169. }
  1170. if (
  1171. preg_match('#wp-admin/?(index.php)?$#', $_SERVER['REQUEST_URI']) &&
  1172. ('index.php' != $menu[$page][2])
  1173. ) {
  1174. wp_redirect(get_option('siteurl') . '/wp-admin/profile.php');
  1175. }
  1176. }
  1177. }
  1178. add_action('admin_menu', 'remove_dashboard');
  1179. /**
  1180. * Filter the except length to 20 words. 限制摘要长度
  1181. *
  1182. * @param int $length Excerpt length.
  1183. * @return int (Maybe) modified excerpt length.
  1184. */
  1185. function GBsubstr($string, $start, $length)
  1186. {
  1187. if (strlen($string) > $length) {
  1188. $str = null;
  1189. $len = 0;
  1190. $i = $start;
  1191. while ($len < $length) {
  1192. if (ord(substr($string, $i, 1)) > 0xc0) {
  1193. $str .= substr($string, $i, 3);
  1194. $i += 3;
  1195. } elseif (ord(substr($string, $i, 1)) > 0xa0) {
  1196. $str .= substr($string, $i, 2);
  1197. $i += 2;
  1198. } else {
  1199. $str .= substr($string, $i, 1);
  1200. $i++;
  1201. }
  1202. $len++;
  1203. }
  1204. return $str;
  1205. } else {
  1206. return $string;
  1207. }
  1208. }
  1209. /**
  1210. * chatgpt excerpt
  1211. */
  1212. require_once __DIR__.'/inc/chatgpt/hooks.php';
  1213. IROChatGPT\apply_chatgpt_hook();
  1214. function excerpt_length($exp)
  1215. {
  1216. if (!function_exists('mb_substr')) {
  1217. $exp = GBsubstr($exp, 0, 80);
  1218. } else {
  1219. /*
  1220. * To use mb_substr() function, you should uncomment "extension=php_mbstring.dll" in php.ini
  1221. */
  1222. $exp = mb_substr($exp, 0, 80);
  1223. }
  1224. return $exp;
  1225. }
  1226. add_filter('the_excerpt', 'excerpt_length',11);
  1227. /*
  1228. * 后台路径
  1229. */
  1230. /*
  1231. add_filter('site_url', 'wpadmin_filter', 10, 3);
  1232. function wpadmin_filter( $url, $path, $orig_scheme ) {
  1233. $old = array( "/(wp-admin)/");
  1234. $admin_dir = WP_ADMIN_DIR;
  1235. $new = array($admin_dir);
  1236. return preg_replace( $old, $new, $url, 1);
  1237. }
  1238. */
  1239. function admin_ini()
  1240. {
  1241. wp_enqueue_style('admin-styles-fix-icon', get_site_url() . '/wp-includes/css/dashicons.css');
  1242. wp_enqueue_style('cus-styles-fit', get_template_directory_uri() . '/css/dashboard-fix.css');
  1243. }
  1244. add_action('admin_enqueue_scripts', 'admin_ini');
  1245. /*
  1246. * 后台通知
  1247. */
  1248. /**
  1249. * 在提供权限的情况下,为管理员用户显示通知并更新 meta 值
  1250. */
  1251. function theme_admin_notice_callback() {
  1252. // 判断当前用户是否为管理员
  1253. if ( !current_user_can( 'manage_options' ) ) {
  1254. return;
  1255. }
  1256. // 读取 meta 值
  1257. $meta_value = get_user_meta( get_current_user_id(), 'theme_admin_notice', true );
  1258. // 判断 meta 值是否存在
  1259. if ( $meta_value ) {
  1260. return; // 如果存在,退出函数,避免重复加载通知
  1261. }
  1262. // 显示通知
  1263. $theme_name = 'Sakurairo';
  1264. switch ( get_locale() ) {
  1265. case 'zh_CN':
  1266. $thankyou = '感谢你使用 '.$theme_name.' 主题!这里有一些需要你的许可的东西(*/ω\*)';
  1267. $dislike = '不,谢谢';
  1268. $allow_send = '允许发送你的主题版本数据以便官方统计';
  1269. break;
  1270. case 'zh_TW':
  1271. $thankyou = '感謝你使用 '.$theme_name.' 主題!以下是一些需要你許可的內容。';
  1272. $dislike = '謝謝,不用了';
  1273. $allow_send = '允許出於統計目的發送主題版本数据';
  1274. break;
  1275. case 'ja':
  1276. $thankyou = 'ご使用いただきありがとうございます!以下は、あなたの許可が必要なコンテンツです。';
  1277. $dislike = 'いいえ、結構です';
  1278. $allow_send = '統計目的のためにあなたのテーマバージョンを送信することを許可する';
  1279. break;
  1280. default:
  1281. $thankyou = 'Thank you for using the '.$theme_name.' theme! There is something that needs your approval.';
  1282. $dislike = 'No, thanks';
  1283. $allow_send = 'Allow sending your theme version for statistical purposes';
  1284. break;
  1285. }
  1286. ?>
  1287. <div class="notice notice-success" id="send-ver-tip">
  1288. <p><?php echo $thankyou; ?></p>
  1289. <button class="button" onclick="dismiss_notice()"><?php echo $dislike; ?></button>
  1290. <button class="button" onclick="update_option()"><?php echo $allow_send; ?></button>
  1291. </div>
  1292. <script>
  1293. function dismiss_notice() {
  1294. // 隐藏通知
  1295. document.getElementById( "send-ver-tip" ).style.display = "none";
  1296. // 写入 1 到 meta
  1297. var data = new FormData();
  1298. data.append( 'action', 'update_theme_admin_notice_meta' );
  1299. data.append( 'user_id', '<?php echo get_current_user_id(); ?>' );
  1300. data.append( 'meta_key', 'theme_admin_notice' );
  1301. data.append( 'meta_value', '1' );
  1302. fetch( '<?php echo admin_url( 'admin-ajax.php' ); ?>', {
  1303. method: 'POST',
  1304. body: data
  1305. } );
  1306. }
  1307. function update_option() {
  1308. // 隐藏通知
  1309. document.getElementById( "send-ver-tip" ).style.display = "none";
  1310. // 发送 AJAX 请求
  1311. var xhr = new XMLHttpRequest();
  1312. xhr.open( "POST", "<?php echo admin_url( 'admin-ajax.php' ); ?>", true );
  1313. xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
  1314. xhr.send( "action=update_theme_option&option=send_theme_version&value=true" );
  1315. // 写入 1 到 meta
  1316. var data = new FormData();
  1317. data.append( 'action', 'update_theme_admin_notice_meta' );
  1318. data.append( 'user_id', '<?php echo get_current_user_id(); ?>' );
  1319. data.append( 'meta_key', 'theme_admin_notice' );
  1320. data.append( 'meta_value', '1' );
  1321. fetch( '<?php echo admin_url( 'admin-ajax.php' ); ?>', {
  1322. method: 'POST',
  1323. body: data
  1324. } );
  1325. }
  1326. </script>
  1327. <?php
  1328. }
  1329. add_action('admin_notices', 'theme_admin_notice_callback');
  1330. // AJAX 处理函数 - 更新主题选项
  1331. add_action( 'wp_ajax_update_theme_option', 'update_theme_option' );
  1332. function update_theme_option() {
  1333. $option = $_POST['option'];
  1334. $value = sanitize_text_field( $_POST['value'] );
  1335. iro_opt_update( $option, $value );
  1336. wp_die();
  1337. }
  1338. // AJAX 处理函数 - 写入 theme_admin_notice 元值
  1339. add_action( 'wp_ajax_update_theme_admin_notice_meta', 'update_theme_admin_notice_meta' );
  1340. function update_theme_admin_notice_meta() {
  1341. $user_id = $_POST['user_id'];
  1342. $meta_key = $_POST['meta_key'];
  1343. $meta_value = sanitize_text_field( $_POST['meta_value'] );
  1344. update_user_meta( $user_id, $meta_key, $meta_value );
  1345. wp_die();
  1346. }
  1347. //dashboard scheme
  1348. function dash_scheme($key, $name, $col1, $col2, $col3, $col4, $base, $focus, $current, $rules = "")
  1349. {
  1350. $hash = "color_1=" . str_replace("#", "", $col1) .
  1351. "&color_2=" . str_replace("#", "", $col2) .
  1352. "&color_3=" . str_replace("#", "", $col3) .
  1353. "&color_4=" . str_replace("#", "", $col4) .
  1354. "&rules=" . urlencode($rules);
  1355. wp_admin_css_color(
  1356. $key,
  1357. $name,
  1358. get_template_directory_uri() . "/inc/dash-scheme.php?" . $hash,
  1359. array($col1, $col2, $col3, $col4),
  1360. array('base' => $base, 'focus' => $focus, 'current' => $current)
  1361. );
  1362. }
  1363. //Sakurairo
  1364. dash_scheme(
  1365. $key = "sakurairo",
  1366. $name = "Sakurairo🌸",
  1367. $col1 = iro_opt('admin_second_class_color'),
  1368. $col2 = iro_opt('admin_first_class_color'),
  1369. $col3 = iro_opt('admin_emphasize_color'),
  1370. $col4 = iro_opt('admin_emphasize_color'),
  1371. $base = "#FFF",
  1372. $focus = "#FFF",
  1373. $current = "#FFF",
  1374. $rules = '#adminmenu .wp-has-current-submenu .wp-submenu a,#adminmenu .wp-has-current-submenu.opensub .wp-submenu a,#adminmenu .wp-submenu a,#adminmenu a.wp-has-current-submenu:focus+.wp-submenu a,#wpadminbar .ab-submenu .ab-item,#wpadminbar .quicklinks .menupop ul li a,#wpadminbar .quicklinks .menupop.hover ul li a,#wpadminbar.nojs .quicklinks .menupop:hover ul li a,.folded #adminmenu .wp-has-current-submenu .wp-submenu a{color:' . iro_opt('admin_text_color') . '}body{background-image:url(' . iro_opt('admin_background') . ');background-attachment:fixed;background-size:cover;}#wpcontent{background:rgba(255,255,255,.0)}.wp-core-ui .button-primary{background:' . iro_opt('admin_button_color') . '!important;border-color:' . iro_opt('admin_button_color') . '!important;color:' . iro_opt('admin_text_color') . '!important;box-shadow:0 1px 0 ' . iro_opt('admin_button_color') . '!important;text-shadow:0 -1px 1px ' . iro_opt('admin_button_color') . ',1px 0 1px ' . iro_opt('admin_button_color') . ',0 1px 1px ' . iro_opt('admin_button_color') . ',-1px 0 1px ' . iro_opt('admin_button_color') . '!important}'
  1375. );
  1376. //Set Default Admin Color Scheme for New Users
  1377. function set_default_admin_color($user_id)
  1378. {
  1379. $args = array(
  1380. 'ID' => $user_id,
  1381. 'admin_color' => 'sunrise',
  1382. );
  1383. wp_update_user($args);
  1384. }
  1385. //add_action('user_register', 'set_default_admin_color');
  1386. //Stop Users From Switching Admin Color Schemes
  1387. //if ( !current_user_can('manage_options') ) remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
  1388. // WordPress Custom style @ Admin
  1389. function custom_admin_open_sans_style()
  1390. {
  1391. require get_template_directory() . '/inc/admin_addcss.php';
  1392. }
  1393. add_action('admin_head', 'custom_admin_open_sans_style');
  1394. // WordPress Custom Font @ Admin
  1395. function custom_admin_open_sans_font()
  1396. {
  1397. echo '<link href="https://' . iro_opt('gfonts_api','fonts.loli.net') . '/css?family=Noto+Serif+SC&display=swap" rel="stylesheet">' . PHP_EOL;
  1398. echo '<style>body, #wpadminbar *:not([class="ab-icon"]), .wp-core-ui, .media-menu, .media-frame *, .media-modal *{font-family: "Noto Serif SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;}</style>' . PHP_EOL;
  1399. }
  1400. add_action('admin_head', 'custom_admin_open_sans_font');
  1401. // WordPress Custom Font @ Admin Frontend Toolbar
  1402. function custom_admin_open_sans_font_frontend_toolbar()
  1403. {
  1404. if (current_user_can('administrator') && is_admin_bar_showing()) {
  1405. echo '<link href="https://' . iro_opt('gfonts_api','fonts.loli.net') . '/css?family=Noto+Serif+SC&display=swap" rel="stylesheet">' . PHP_EOL;
  1406. echo '<style>#wpadminbar *:not([class="ab-icon"]){font-family: "Noto Serif SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;}</style>' . PHP_EOL;
  1407. }
  1408. }
  1409. add_action('wp_head', 'custom_admin_open_sans_font_frontend_toolbar');
  1410. // WordPress Custom Font @ Admin Login
  1411. function custom_admin_open_sans_font_login_page()
  1412. {
  1413. if (stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/')) !== false) {
  1414. echo '<link href="https://' . iro_opt('gfonts_api','fonts.loli.net') . '/css?family=Noto+Serif+SC&display=swap" rel="stylesheet">' . PHP_EOL;
  1415. echo '<style>body{font-family: "Noto Serif SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;}</style>' . PHP_EOL;
  1416. }
  1417. }
  1418. add_action('login_head', 'custom_admin_open_sans_font_login_page');
  1419. // 阻止垃圾注册
  1420. add_action('register_post', 'codecheese_register_post', 10, 3);
  1421. function codecheese_register_post($sanitized_user_login, $user_email, $errors)
  1422. {
  1423. // Blocked domains
  1424. $domains = array(
  1425. 'net.buzzcluby.com',
  1426. 'buzzcluby.com',
  1427. 'mail.ru',
  1428. 'h.captchaeu.info',
  1429. 'edge.codyting.com'
  1430. );
  1431. // Get visitor email domain
  1432. $email = explode('@', $user_email);
  1433. // Check and display error message for the registration form if exists
  1434. if (in_array($email[1], $domains)) {
  1435. $errors->add('invalid_email', __('<b>ERROR</b>: This email domain (<b>@' . $email[1] . '</b>) has been blocked. Please use another email.'));
  1436. }
  1437. }
  1438. function array_html_props(array $props)
  1439. {
  1440. $props_string = '';
  1441. foreach ($props as $key => $value) {
  1442. $props_string .= ' ' . $key . '="' . $value . '"';
  1443. }
  1444. return $props_string;
  1445. }
  1446. /**
  1447. * 渲染一个懒加载的<img>
  1448. * @author KotoriK
  1449. */
  1450. function lazyload_img(string $src, string $class = '', array $otherParam = array())
  1451. {
  1452. $noscriptParam = $otherParam;
  1453. if ($class) $noscriptParam['class'] = $class;
  1454. $noscriptParam['src'] = $src;
  1455. $otherParam['class'] = 'lazyload' . ($class ? ' ' . $class : '');
  1456. $otherParam['data-src'] = $src;
  1457. $otherParam['onerror'] = 'imgError(this)';
  1458. $otherParam['src'] = iro_opt('page_lazyload_spinner');
  1459. $noscriptProps = '';
  1460. $props = array_html_props($otherParam);
  1461. $noscriptProps = array_html_props($noscriptParam);
  1462. return "<img$props/><noscript><img$noscriptProps/></noscript>";
  1463. }
  1464. // html 标签处理器
  1465. function html_tag_parser($content)
  1466. {
  1467. if (!is_feed()) {
  1468. //图片懒加载标签替换
  1469. if (iro_opt('page_lazyload') && iro_opt('page_lazyload_spinner')) {
  1470. $img_elements = array();
  1471. $is_matched = preg_match_all('/<img[^<]*>/i', $content, $img_elements);
  1472. if ($is_matched) {
  1473. array_walk($img_elements[0], function ($img) use (&$content) {
  1474. $class_found = 0;
  1475. $new_img = preg_replace('/class=[\'"]([^\'"]+)[\'"]/i', 'class="$1 lazyload"', $img, -1, $class_found);
  1476. if ($class_found == 0) {
  1477. $new_img = str_replace('<img ', '<img class="lazyload"', $new_img);
  1478. }
  1479. $new_img = preg_replace('/srcset=[\'"]([^\'"]+)[\'"]/i', 'data-srcset="$1"', $new_img);
  1480. $new_img = preg_replace('/src=[\'"]([^\'"]+)[\'"]/i', 'data-src="$1" src="' . iro_opt('page_lazyload_spinner') . '" onerror="imgError(this)"', $new_img);
  1481. $content = str_replace($img, $new_img . '<noscript>' . $img . '</noscript>', $content);
  1482. });
  1483. }
  1484. }
  1485. //Fancybox
  1486. /* Markdown Regex Pattern for Matching URLs:
  1487. * https://daringfireball.net/2010/07/improved_regex_for_matching_urls
  1488. */
  1489. $url_regex = '((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
  1490. //With Thumbnail: !{alt}(url)[th_url]
  1491. if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content, $matches)) {
  1492. foreach($matches as $result){
  1493. $content = str_replace(
  1494. $result,
  1495. preg_replace(
  1496. '/!\{([^\{\}]+)*\}\(' . $url_regex . '\)\[' . $url_regex . '\]/i',
  1497. '<a data-fancybox="gallery"
  1498. data-caption="$1"
  1499. class="fancybox"
  1500. href="$2"
  1501. alt="$1"
  1502. title="$1"><img src="$7" target="_blank" rel="nofollow" class="fancybox"></a>',
  1503. $result
  1504. ),
  1505. $content
  1506. );
  1507. }
  1508. }
  1509. //Without Thumbnail :!{alt}(url)
  1510. $content = preg_replace(
  1511. '/!\{([^\{\}]+)*\}\(' . $url_regex . '\)/i',
  1512. '<a data-fancybox="gallery"
  1513. data-caption="$1"
  1514. class="fancybox"
  1515. href="$2"
  1516. alt="$1"
  1517. title="$1"><img src="$2" target="_blank" rel="nofollow" class="fancybox"></a>',
  1518. $content
  1519. );
  1520. }
  1521. //html tag parser for rss
  1522. if (is_feed()) {
  1523. //Fancybox
  1524. $url_regex = '((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
  1525. if (preg_match_all('/\!\{.*?\)\[.*?\]/i', $content, $matches)) {
  1526. foreach ($matches as $result){
  1527. $content = str_replace(
  1528. $result,
  1529. preg_replace('/!\{([^\{\}]+)*\}\(' . $url_regex . '\)\[' . $url_regex . '\]/i', '<a href="$2"><img src="$7" alt="$1" title="$1"></a>', $result),
  1530. $content
  1531. );
  1532. }
  1533. }
  1534. $content = preg_replace('/!\{([^\{\}]+)*\}\(' . $url_regex . '\)/i', '<a href="$2"><img src="$2" alt="$1" title="$1"></a>', $content);
  1535. }
  1536. return $content;
  1537. }
  1538. add_filter('the_content', 'html_tag_parser'); //替换文章关键词
  1539. //add_filter( 'comment_text', 'html_tag_parser' );//替换评论关键词
  1540. /*
  1541. * QQ 评论
  1542. */
  1543. // 数据库插入评论表单的qq字段
  1544. add_action('wp_insert_comment', 'sql_insert_qq_field', 10, 2);
  1545. function sql_insert_qq_field($comment_ID, $commmentdata)
  1546. {
  1547. $qq = isset($_POST['new_field_qq']) ? $_POST['new_field_qq'] : false;
  1548. update_comment_meta($comment_ID, 'new_field_qq', $qq); // new_field_qq 是表单name值,也是存储在数据库里的字段名字
  1549. }
  1550. // 后台评论中显示qq字段
  1551. add_filter('manage_edit-comments_columns', 'add_comments_columns');
  1552. add_action('manage_comments_custom_column', 'output_comments_qq_columns', 10, 2);
  1553. function add_comments_columns($columns)
  1554. {
  1555. $columns['new_field_qq'] = __('QQ'); // 新增列名称
  1556. return $columns;
  1557. }
  1558. function output_comments_qq_columns($column_name, $comment_id)
  1559. {
  1560. switch ($column_name) {
  1561. case "new_field_qq":
  1562. // 这是输出值,可以拿来在前端输出,这里已经在钩子manage_comments_custom_column上输出了
  1563. echo get_comment_meta($comment_id, 'new_field_qq', true);
  1564. break;
  1565. }
  1566. }
  1567. /**
  1568. * 头像调用路径
  1569. */
  1570. add_filter('get_avatar', 'change_avatar', 10, 3);
  1571. function change_avatar($avatar)
  1572. {
  1573. global $comment, $sakura_privkey;
  1574. if ($comment && get_comment_meta($comment->comment_ID, 'new_field_qq', true)) {
  1575. $qq_number = get_comment_meta($comment->comment_ID, 'new_field_qq', true);
  1576. if (iro_opt('qq_avatar_link') == 'off') {
  1577. return '<img src="https://q2.qlogo.cn/headimg_dl?dst_uin=' . $qq_number . '&spec=100" class="lazyload avatar avatar-24 photo" alt="😀" width="24" height="24" onerror="imgError(this,1)">';
  1578. }
  1579. if (iro_opt('qq_avatar_link') == 'type_3') {
  1580. $qqavatar = file_get_contents('http://ptlogin2.qq.com/getface?appid=1006102&imgtype=3&uin=' . $qq_number);
  1581. preg_match('/:\"([^\"]*)\"/i', $qqavatar, $matches);
  1582. return '<img src="' . $matches[1] . '" class="lazyload avatar avatar-24 photo" alt="😀" width="24" height="24" onerror="imgError(this,1)">';
  1583. }
  1584. $iv = str_repeat($sakura_privkey, 2);
  1585. $encrypted = openssl_encrypt($qq_number, 'aes-128-cbc', $sakura_privkey, 0, $iv);
  1586. $encrypted = urlencode(base64_encode($encrypted));
  1587. return '<img src="' . rest_url("sakura/v1/qqinfo/avatar") . '?qq=' . $encrypted . '" class="lazyload avatar avatar-24 photo" alt="😀" width="24" height="24" onerror="imgError(this,1)">';
  1588. }
  1589. return $avatar;
  1590. }
  1591. function get_random_url(string $url):string
  1592. {
  1593. $array = parse_url($url);
  1594. if (!isset($array['query'])) {
  1595. // 无参数
  1596. $url .= '?';
  1597. } else {
  1598. // 有参数
  1599. $url .= '&';
  1600. }
  1601. return $url.random_int(1,100);
  1602. }
  1603. // default feature image
  1604. function DEFAULT_FEATURE_IMAGE(string $size='source'):string
  1605. {
  1606. if (iro_opt('post_cover_options') == 'type_2') {
  1607. return get_random_url(iro_opt('post_cover'));
  1608. }
  1609. if (iro_opt('random_graphs_options') == 'external_api'){
  1610. return get_random_url(iro_opt('random_graphs_link'));
  1611. }
  1612. $_api_url = rest_url('sakura/v1/image/feature');
  1613. $rand = rand(1, 100);
  1614. # 拼接符
  1615. $splice = strpos($_api_url, 'index.php?') !== false ? '&' : '?';
  1616. $_api_url = "{$_api_url}{$splice}size={$size}&$rand";
  1617. return $_api_url;
  1618. }
  1619. //评论回复
  1620. function sakura_comment_notify($comment_id)
  1621. {
  1622. if (!isset($_POST['mail-notify'])) {
  1623. update_comment_meta($comment_id, 'mail_notify', 'false');
  1624. }
  1625. }
  1626. add_action('comment_post', 'sakura_comment_notify');
  1627. //侧栏小工具
  1628. if (iro_opt('sakura_widget')) {
  1629. if (function_exists('register_sidebar')) {
  1630. register_sidebar(array(
  1631. 'name' => __('Sidebar'), //侧栏
  1632. 'id' => 'sakura_widget',
  1633. 'before_widget' => '<div class="widget %2$s">',
  1634. 'after_widget' => '</div>',
  1635. 'before_title' => '<div class="title"><h2>',
  1636. 'after_title' => '</h2></div>',
  1637. ));
  1638. }
  1639. }
  1640. // 评论Markdown解析
  1641. function markdown_parser($incoming_comment)
  1642. {
  1643. global $wpdb, $comment_markdown_content;
  1644. $re = '/```([\s\S]*?)```[\s]*|`{1,2}[^`](.*?)`{1,2}|\[.*?\]\([\s\S]*?\)/m';
  1645. if (preg_replace($re, 'temp', $incoming_comment['comment_content']) != strip_tags(preg_replace($re, 'temp', $incoming_comment['comment_content']))) {
  1646. siren_ajax_comment_err('评论只支持Markdown啦,见谅╮( ̄▽ ̄)╭<br>Markdown Supported while <i class="fa-solid fa-code"></i> Forbidden');
  1647. return ($incoming_comment);
  1648. }
  1649. $column_names = $wpdb->get_row("SELECT * FROM information_schema.columns where
  1650. table_name='wp_comments' and column_name = 'comment_markdown' LIMIT 1");
  1651. //Add column if not present.
  1652. if (!isset($column_names)) {
  1653. $wpdb->query("ALTER TABLE wp_comments ADD comment_markdown text");
  1654. }
  1655. $comment_markdown_content = $incoming_comment['comment_content'];
  1656. include 'inc/Parsedown.php';
  1657. $Parsedown = new Parsedown();
  1658. $incoming_comment['comment_content'] = $Parsedown->setUrlsLinked(false)->text($incoming_comment['comment_content']);
  1659. return $incoming_comment;
  1660. }
  1661. add_filter('preprocess_comment', 'markdown_parser');
  1662. remove_filter('comment_text', 'make_clickable', 9);
  1663. //保存Markdown评论
  1664. function save_markdown_comment($comment_ID, $comment_approved)
  1665. {
  1666. global $wpdb, $comment_markdown_content;
  1667. $comment = get_comment($comment_ID);
  1668. $comment_content = $comment_markdown_content;
  1669. //store markdow content
  1670. $wpdb->query("UPDATE wp_comments SET comment_markdown='" . $comment_content . "' WHERE comment_ID='" . $comment_ID . "';");
  1671. }
  1672. add_action('comment_post', 'save_markdown_comment', 10, 2);
  1673. //打开评论HTML标签限制
  1674. function allow_more_tag_in_comment()
  1675. {
  1676. global $allowedtags;
  1677. $allowedtags['pre'] = array('class' => array());
  1678. $allowedtags['code'] = array('class' => array());
  1679. $allowedtags['h1'] = array('class' => array());
  1680. $allowedtags['h2'] = array('class' => array());
  1681. $allowedtags['h3'] = array('class' => array());
  1682. $allowedtags['h4'] = array('class' => array());
  1683. $allowedtags['h5'] = array('class' => array());
  1684. $allowedtags['ul'] = array('class' => array());
  1685. $allowedtags['ol'] = array('class' => array());
  1686. $allowedtags['li'] = array('class' => array());
  1687. $allowedtags['td'] = array('class' => array());
  1688. $allowedtags['th'] = array('class' => array());
  1689. $allowedtags['tr'] = array('class' => array());
  1690. $allowedtags['table'] = array('class' => array());
  1691. $allowedtags['thead'] = array('class' => array());
  1692. $allowedtags['tbody'] = array('class' => array());
  1693. $allowedtags['span'] = array('class' => array());
  1694. }
  1695. add_action('pre_comment_on_post', 'allow_more_tag_in_comment');
  1696. /*
  1697. * 随机图
  1698. */
  1699. function create_sakura_table()
  1700. {
  1701. if (iro_opt('random_graphs_mts')) {
  1702. global $wpdb, $sakura_image_array, $sakura_mobile_image_array, $sakura_privkey;
  1703. } else {
  1704. global $wpdb, $sakura_image_array, $sakura_privkey;
  1705. }
  1706. $sakura_table_name = $wpdb->base_prefix . 'sakurairo';
  1707. require_once ABSPATH . "wp-admin/includes/upgrade.php";
  1708. dbDelta("CREATE TABLE IF NOT EXISTS `" . $sakura_table_name . "` (
  1709. `mate_key` varchar(50) COLLATE utf8_bin NOT NULL,
  1710. `mate_value` text COLLATE utf8_bin NOT NULL,
  1711. PRIMARY KEY (`mate_key`)
  1712. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;");
  1713. //default data
  1714. if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'manifest_json'")) {
  1715. $manifest = array(
  1716. "mate_key" => "manifest_json",
  1717. "mate_value" => file_get_contents(get_template_directory() . "/manifest/manifest.json"),
  1718. );
  1719. $wpdb->insert($sakura_table_name, $manifest);
  1720. }
  1721. if (iro_opt('random_graphs_mts') && !$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'mobile_manifest_json'")) {
  1722. $mobile_manifest = array(
  1723. "mate_key" => "mobile_manifest_json",
  1724. "mate_value" => file_get_contents(get_template_directory() . "/manifest/manifest_mobile.json"),
  1725. );
  1726. $wpdb->insert($sakura_table_name, $mobile_manifest);
  1727. }
  1728. if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'json_time'")) {
  1729. $time = array(
  1730. "mate_key" => "json_time",
  1731. "mate_value" => date("Y-m-d H:i:s", time()),
  1732. );
  1733. $wpdb->insert($sakura_table_name, $time);
  1734. }
  1735. if (!$wpdb->get_var("SELECT COUNT(*) FROM $sakura_table_name WHERE mate_key = 'privkey'")) {
  1736. $privkey = array(
  1737. "mate_key" => "privkey",
  1738. "mate_value" => wp_generate_password(8),
  1739. );
  1740. $wpdb->insert($sakura_table_name, $privkey);
  1741. }
  1742. //reduce sql query
  1743. $sakura_image_array = $wpdb->get_var("SELECT `mate_value` FROM $sakura_table_name WHERE `mate_key`='manifest_json'");
  1744. if (iro_opt('random_graphs_mts')) {
  1745. $sakura_mobile_image_array = $wpdb->get_var("SELECT `mate_value` FROM $sakura_table_name WHERE `mate_key`='mobile_manifest_json'");
  1746. }
  1747. $sakura_privkey = $wpdb->get_var("SELECT `mate_value` FROM $sakura_table_name WHERE `mate_key`='privkey'");
  1748. }
  1749. add_action('after_setup_theme', 'create_sakura_table');
  1750. //rest api支持
  1751. function permalink_tip()
  1752. {
  1753. if (!get_option('permalink_structure')) {
  1754. $msg = __('<b> For a better experience, please do not set <a href="/wp-admin/options-permalink.php"> permalink </a> as plain. To do this, you may need to configure <a href="https://www.wpdaxue.com/wordpress-rewriterule.html" target="_blank"> pseudo-static </a>. </ b>', 'sakurairo'); /*<b>为了更好的使用体验,请不要将<a href="/wp-admin/options-permalink.php">固定链接</a>设置为朴素。为此,您可能需要配置<a href="https://www.wpdaxue.com/wordpress-rewriterule.html" target="_blank">伪静态</a>。</b>*/
  1755. echo '<div class="notice notice-success is-dismissible" id="scheme-tip"><p><b>' . $msg . '</b></p></div>';
  1756. }
  1757. }
  1758. add_action('admin_notices', 'permalink_tip');
  1759. //code end
  1760. //发送主题版本号
  1761. function send_theme_version() {
  1762. $theme = wp_get_theme();
  1763. $version = $theme->get('Version');
  1764. $data = array(
  1765. 'date' => date('Y-m-d H:i:s'),
  1766. 'version' => $version
  1767. );
  1768. $args = array(
  1769. 'body' => $data,
  1770. 'timeout' => '5',
  1771. 'redirection' => '5',
  1772. 'httpversion' => '1.0',
  1773. 'blocking' => true,
  1774. 'headers' => array(),
  1775. 'cookies' => array()
  1776. );
  1777. wp_remote_post('https://api.maho.cc/ver-stat/index.php', $args);
  1778. }
  1779. if (iro_opt('send_theme_version') == '1') {
  1780. if (!wp_next_scheduled('daily_event')) {
  1781. wp_schedule_event(time(), 'daily', 'daily_event');
  1782. }
  1783. add_action('daily_event', 'send_theme_version');
  1784. }
  1785. //解析短代码
  1786. add_shortcode('task', 'task_shortcode');
  1787. function task_shortcode($attr, $content = '')
  1788. {
  1789. $out = '<div class="task shortcodestyle"><i class="fa-solid fa-bars"></i>' . $content . '</div>';
  1790. return $out;
  1791. }
  1792. add_shortcode('warning', 'warning_shortcode');
  1793. function warning_shortcode($attr, $content = '')
  1794. {
  1795. $out = '<div class="warning shortcodestyle"><i class="fa-solid fa-triangle-exclamation"></i>' . $content . '</div>';
  1796. return $out;
  1797. }
  1798. add_shortcode('noway', 'noway_shortcode');
  1799. function noway_shortcode($attr, $content = '')
  1800. {
  1801. $out = '<div class="noway shortcodestyle"><i class="fa-solid fa-rectangle-xmark"></i>' . $content . '</div>';
  1802. return $out;
  1803. }
  1804. add_shortcode('buy', 'buy_shortcode');
  1805. function buy_shortcode($attr, $content = '')
  1806. {
  1807. $out = '<div class="buy shortcodestyle"><i class="fa-solid fa-check-to-slot"></i>' . $content . '</div>';
  1808. return $out;
  1809. }
  1810. add_shortcode('ghcard', 'gh_card');
  1811. function gh_card($attr, $content = '')
  1812. {
  1813. extract(shortcode_atts(array("path" => ""), $attr));
  1814. return '<div class="ghcard"><a href="https://github.com/'. $path .'"><img src="https://github-readme-stats.maho.cc/api'. $content .'" alt="Github-Card"></a></div>';
  1815. }
  1816. add_shortcode('showcard', 'show_card');
  1817. function show_card($attr, $content = '')
  1818. {
  1819. extract(shortcode_atts(array("icon" => "", "title" => "", "img" => "", "color" => ""), $attr));
  1820. return '<section class="showcard">
  1821. <div class="img" alt="Show-Card" style="background:url('. $img .');background-size:cover;background-position: center;">
  1822. <a href="'. $content .'"><button class="showcard-button" style="color:'. $color .' !important;"><i class="fa-solid fa-angle-right"></i></button> </a>
  1823. </div>
  1824. <br>
  1825. <div class="icon">
  1826. <i class="'. $icon .'"></i>
  1827. </div>
  1828. <div class="title">
  1829. '. $title .'
  1830. </div>
  1831. </section>';
  1832. }
  1833. add_shortcode('conversations', 'conversations');
  1834. function conversations($attr, $content = '')
  1835. {
  1836. extract(shortcode_atts(array("avatar" => "", "direction" => ""), $attr));
  1837. $output = '<div class="conversations-code" style="display: flex; flex-direction: ' . $direction . '; padding: 10px;">';
  1838. $output .= '<img src="' . $avatar . '" style="width: 40px; height: 40px; border-radius: 50%;">';
  1839. $output .= '<div class="conversations-code-text">' . $content . '</div>';
  1840. $output .= '</div>';
  1841. return $output;
  1842. }
  1843. //code end
  1844. //WEBP支持
  1845. function mimvp_filter_mime_types($array)
  1846. {
  1847. $array['webp'] = 'image/webp';
  1848. return $array;
  1849. }
  1850. add_filter('mime_types', 'mimvp_filter_mime_types', 10, 1);
  1851. function mimvp_file_is_displayable_image($result, $path)
  1852. {
  1853. $info = @getimagesize($path);
  1854. // if ($info['mime'] == 'image/webp') {
  1855. // $result = true;
  1856. // }
  1857. // return $result;
  1858. return (bool)($info); // 根据文档这里需要返回一个bool
  1859. }
  1860. add_filter('file_is_displayable_image', 'mimvp_file_is_displayable_image', 10, 2);
  1861. //code end
  1862. //展开收缩功能
  1863. function xcollapse($atts, $content = null)
  1864. {
  1865. extract(shortcode_atts(array("title" => ""), $atts));
  1866. return '<div style="margin: 0.5em 0;">
  1867. <div class="xControl">
  1868. <i class="fa-solid fa-angle-down" style="color: #16AF90;"></i> &nbsp;&nbsp;
  1869. <span class="xTitle">' . $title . '</span>&nbsp;&nbsp;==>&nbsp;&nbsp;<a href="javascript:void(0)" class="collapseButton xButton"><span class="xbtn02">展开 / 收缩</span></a>
  1870. <div style="clear: both;"></div>
  1871. </div>
  1872. <div class="xContent" style="display: none;">' . $content . '</div>
  1873. </div>';
  1874. }
  1875. add_shortcode('collapse', 'xcollapse');
  1876. //code end
  1877. // add_action("wp_ajax_nopriv_getPhoto", "get_photo");
  1878. // add_action("wp_ajax_getPhoto", "get_photo");
  1879. // /**
  1880. // * 相册模板
  1881. // * @author siroi <mrgaopw@hotmail.com>
  1882. // * @return Json
  1883. // */
  1884. // function get_photo()
  1885. // {
  1886. // $postId = $_GET['post'];
  1887. // $page = get_post($postId);
  1888. // if ($page->post_type != "page") {
  1889. // $back['code'] = 201;
  1890. // } else {
  1891. // $back['code'] = 200;
  1892. // $back['imgs'] = array();
  1893. // $dom = new DOMDocument('1.0', 'utf-8');
  1894. // $meta = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
  1895. // $dom->loadHTML($meta . $page->post_content);
  1896. // $imgS = $dom->getElementsByTagName('img');
  1897. // //<img src="..." data-header="标题" data-info="信息" vertical=false>
  1898. // foreach ($imgS as $key => $value) {
  1899. // $attr = $value->attributes;
  1900. // $header = $attr->getNamedItem('header');
  1901. // $info = $attr->getNamedItem('data-info');
  1902. // $vertical = $attr->getNamedItem('vertical');
  1903. // //图片资源地址
  1904. // $temp['img'] = $value->attributes->getNamedItem('src')->nodeValue;
  1905. // //图片上的标题
  1906. // $temp['header'] = $header->nodeValue ?? null;
  1907. // //图片上的信息
  1908. // $temp['info'] = $info->nodeValue ?? null;
  1909. // //是否竖向展示 默认false
  1910. // $temp['vertical'] = $vertical->nodeValue ?? null;
  1911. // array_push($back['imgs'], $temp);
  1912. // }
  1913. // }
  1914. // header('Content-Type:application/json;charset=utf-8');
  1915. // echo json_encode($back);
  1916. // exit();
  1917. // }
  1918. if (!iro_opt('login_language_opt') == '1') {
  1919. add_filter( 'login_display_language_dropdown', '__return_false' );
  1920. }
  1921. if (iro_opt('captcha_select') === 'iro_captcha') {
  1922. function login_CAPTCHA()
  1923. {
  1924. include_once('inc/classes/Captcha.php');
  1925. $img = new Sakura\API\Captcha;
  1926. $test = $img->create_captcha_img();
  1927. echo '<p><label for="captcha" class="captcha">验证码<br><img id="captchaimg" width="120" height="40" src="', $test['data'], '"><input type="text" name="yzm" id="yzm" class="input" value="" size="20" tabindex="4" placeholder="请输入验证码"><input type="hidden" name="timestamp" value="', $test['time'], '"><input type="hidden" name="id" value="', $test['id'], '">'
  1928. . "</label></p>";
  1929. }
  1930. add_action('login_form', 'login_CAPTCHA');
  1931. add_action('register_form', 'login_CAPTCHA');
  1932. add_action('lostpassword_form', 'login_CAPTCHA');
  1933. /**
  1934. * 登录界面验证码验证
  1935. */
  1936. function CAPTCHA_CHECK($user, $username, $password)
  1937. {
  1938. if (empty($_POST)) {
  1939. return new WP_Error();
  1940. }
  1941. if (!(isset($_POST['yzm']) && !empty(trim($_POST['yzm'])))) {
  1942. return new WP_Error('prooffail', '<strong>错误</strong>:验证码为空!');
  1943. }
  1944. if (!isset($_POST['timestamp']) || !isset($_POST['id']) || !preg_match('/^[\w$.\/]+$/', $_POST['id']) || !ctype_digit($_POST['timestamp'])) {
  1945. return new WP_Error('prooffail', '<strong>错误</strong>:非法数据');
  1946. }
  1947. include_once('inc/classes/Captcha.php');
  1948. $img = new Sakura\API\Captcha;
  1949. $check = $img->check_captcha($_POST['yzm'], $_POST['timestamp'], $_POST['id']);
  1950. if ($check['code'] == 5) {
  1951. return $user;
  1952. }
  1953. return new WP_Error('prooffail', '<strong>错误</strong>:' . $check['msg']);
  1954. //return home_url('/wp-admin/');
  1955. }
  1956. add_filter('authenticate', 'CAPTCHA_CHECK', 20, 3);
  1957. /**
  1958. * 忘记密码界面验证码验证
  1959. */
  1960. function lostpassword_CHECK($errors)
  1961. {
  1962. if (empty($_POST)) {
  1963. return false;
  1964. }
  1965. if (isset($_POST['yzm']) && !empty(trim($_POST['yzm']))) {
  1966. if (!isset($_POST['timestamp']) || !isset($_POST['id']) || !preg_match('/^[\w$.\/]+$/', $_POST['id']) || !ctype_digit($_POST['timestamp'])) {
  1967. return new WP_Error('prooffail', '<strong>错误</strong>:非法数据');
  1968. }
  1969. include_once('inc/classes/Captcha.php');
  1970. $img = new Sakura\API\Captcha;
  1971. $check = $img->check_captcha($_POST['yzm'], $_POST['timestamp'], $_POST['id']);
  1972. if ($check['code'] != 5) {
  1973. return $errors->add('invalid_department ', '<strong>错误</strong>:' . $check['msg']);
  1974. }
  1975. }
  1976. return $errors->add('invalid_department', '<strong>错误</strong>:验证码为空!');
  1977. }
  1978. add_action('lostpassword_post', 'lostpassword_CHECK');
  1979. /**
  1980. * 注册界面验证码验证
  1981. */
  1982. function registration_CAPTCHA_CHECK($errors, $sanitized_user_login, $user_email)
  1983. {
  1984. if (empty($_POST)) {
  1985. return new WP_Error();
  1986. }
  1987. if (!(isset($_POST['yzm']) && !empty(trim($_POST['yzm'])))) {
  1988. return new WP_Error('prooffail', '<strong>错误</strong>:验证码为空!');
  1989. }
  1990. if (!isset($_POST['timestamp']) || !isset($_POST['id']) || !preg_match('/^[\w$.\/]+$/', $_POST['id']) || !ctype_digit($_POST['timestamp'])) {
  1991. return new WP_Error('prooffail', '<strong>错误</strong>:非法数据');
  1992. }
  1993. include_once('inc/classes/Captcha.php');
  1994. $img = new Sakura\API\Captcha;
  1995. $check = $img->check_captcha($_POST['yzm'], $_POST['timestamp'], $_POST['id']);
  1996. if ($check['code'] == 5) return $errors;
  1997. return new WP_Error('prooffail', '<strong>错误</strong>:' . $check['msg']);
  1998. }
  1999. add_filter('registration_errors', 'registration_CAPTCHA_CHECK', 2, 3);
  2000. } elseif (iro_opt('captcha_select') === 'vaptcha') {
  2001. function vaptchaInit()
  2002. {
  2003. include_once('inc/classes/Vaptcha.php');
  2004. $vaptcha = new Sakura\API\Vaptcha;
  2005. echo $vaptcha->html();
  2006. echo $vaptcha->script();
  2007. }
  2008. add_action('login_form', 'vaptchaInit');
  2009. function checkVaptchaAction($user)
  2010. {
  2011. if (empty($_POST)) {
  2012. return new WP_Error();
  2013. }
  2014. if (!(isset($_POST['vaptcha_server']) && isset($_POST['vaptcha_token']))) {
  2015. return new WP_Error('prooffail', '<strong>错误</strong>:请先进行人机验证');
  2016. }
  2017. if (!preg_match('/^https:\/\/([\w-]+\.)+[\w-]*([^<>=?\"\'])*$/', $_POST['vaptcha_server']) || !preg_match('/^[\w\-\$]+$/', $_POST['vaptcha_token'])) {
  2018. return new WP_Error('prooffail', '<strong>错误</strong>:非法数据');
  2019. }
  2020. include_once('inc/classes/Vaptcha.php');
  2021. $url = $_POST['vaptcha_server'];
  2022. $token = $_POST['vaptcha_token'];
  2023. $ip = get_the_user_ip();
  2024. $vaptcha = new Sakura\API\Vaptcha;
  2025. $response = $vaptcha->checkVaptcha($url, $token, $ip);
  2026. if ($response->msg && $response->success && $response->score) {
  2027. if ($response->success === 1 && $response->score >= 70) {
  2028. return $user;
  2029. }
  2030. if ($response->success === 0) {
  2031. $errorcode = $response->msg;
  2032. return new WP_Error('prooffail', '<strong>错误</strong>:' . $errorcode);
  2033. }
  2034. return new WP_Error('prooffail', '<strong>错误</strong>:人机验证失败');
  2035. } else if (is_string($response)) {
  2036. return new WP_Error('prooffail', '<strong>错误</strong>:' . $response);
  2037. }
  2038. return new WP_Error('prooffail', '<strong>错误</strong>:未知错误');
  2039. }
  2040. add_filter('authenticate', 'checkVaptchaAction', 20, 3);
  2041. }
  2042. /**
  2043. * 返回是否应当显示文章标题。
  2044. *
  2045. */
  2046. function should_show_title():bool{
  2047. $id = get_the_ID();
  2048. $use_as_thumb = get_post_meta($id, 'use_as_thumb', true); //'true','only',(default)
  2049. return !iro_opt('patternimg')
  2050. || !get_post_thumbnail_id($id)
  2051. && $use_as_thumb != 'true' && !get_post_meta($id, 'video_cover', true);
  2052. }