1
0

theme_plus.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. /**
  3. * Custom function
  4. * @Siren
  5. */
  6. // 允许分类、标签描述添加html代码
  7. remove_filter('pre_term_description', 'wp_filter_kses');
  8. remove_filter('term_description', 'wp_kses_data');
  9. // 去除顶部工具栏
  10. show_admin_bar(false);
  11. /*
  12. * 视频
  13. */
  14. function bgvideo(){
  15. $dis=null;
  16. if(!iro_opt('cover_video'))return '';
  17. if(!iro_opt('cover_full_screen')) $dis = 'display:none;';
  18. $html = '<div id="video-container" style="'.$dis.'">';
  19. $html .= '<video id="bgvideo" class="video" preload="auto"></video>';
  20. $html .= '<div id="video-btn" class="loadvideo videolive"></div>';
  21. $html .= '<div id="video-add"></div>';
  22. $html .= '<div class="video-stu"></div>';
  23. $html .= '</div>';
  24. return $html;
  25. }
  26. /*
  27. * 使用本地图片作为头像,防止外源抽风问题
  28. */
  29. function get_avatar_profile_url():string{
  30. $avatar = iro_opt('personal_avatar') ?: get_avatar_url(get_the_author_meta( 'ID' ));
  31. return $avatar;
  32. }
  33. /*
  34. * 随机图
  35. */
  36. function get_random_bg_url():string{
  37. return rest_url('sakura/v1/image/feature').'?'.rand(1,1000);
  38. }
  39. /*
  40. * 订制时间样式
  41. * poi_time_since(strtotime($post->post_date_gmt));
  42. * poi_time_since(strtotime($comment->comment_date_gmt), true );
  43. * 如果中途修改过Linux系统时间则继续使用GMT可能出现时差问题!!
  44. * poi_time_since(strtotime($post->post_date));
  45. * poi_time_since(strtotime($comment->comment_date), true );
  46. */
  47. function poi_time_since( $older_date, $comment_date = false, $text = false ) {
  48. $chunks = array(
  49. array( 24 * 60 * 60, __( ' days ago', 'sakurairo' ) ),/*天前*/
  50. array( 60 * 60 , __( ' hours ago', 'sakurairo' ) ),/*小时前*/
  51. array( 60 , __( ' minutes ago', 'sakurairo' ) ),/*分钟前*/
  52. array( 1, __( ' seconds ago', 'sakurairo' ) )/*秒前*/
  53. );
  54. $newer_date = current_time('timestamp') - (iro_opt('time_zone_fix')*60*60);
  55. $since = abs( $newer_date - $older_date );
  56. if($text){
  57. $output = '';
  58. }else{
  59. $output = __('Posted on ','sakurairo')/*发布于*/;
  60. }
  61. if ( $since < 30 * 24 * 60 * 60 ) {
  62. foreach( $chunks as $chunk ) {
  63. $seconds = $chunk[0];
  64. $name = $chunk[1];
  65. if ( ( $count = floor( $since / $seconds ) ) != 0 ) {
  66. break;
  67. }
  68. }
  69. $output .= $count . $name;
  70. } else {
  71. $output .= $comment_date ? date( 'Y-m-d H:i', $older_date ) : date( 'Y-m-d', $older_date );
  72. }
  73. return $output;
  74. }
  75. /*
  76. * 首页不显示指定的分类文章
  77. */
  78. if(iro_opt('classify_display')){
  79. function classify_display($query){
  80. $source = iro_opt('classify_display');
  81. $cats = explode(',', $source);
  82. $cat = '';
  83. if ( $query->is_home ) {
  84. foreach($cats as $k => $v) {
  85. $cat .= '-'.$v.','; //重组字符串
  86. }
  87. $cat = trim($cat,',');
  88. $query->set( 'cat', $cat);
  89. }
  90. return $query;
  91. }
  92. add_filter( 'pre_get_posts', 'classify_display' );
  93. }
  94. /*
  95. * 评论添加@
  96. */
  97. function comment_add_at( $comment_text, $comment = '') {
  98. if( isset($comment->comment_parent) && $comment->comment_parent > 0) {
  99. if(substr($comment_text, 0, 3) === "<p>")
  100. $comment_text = str_replace(substr($comment_text, 0, 3), '<p><a href="#comment-' . $comment->comment_parent . '" class="comment-at">@'.get_comment_author( $comment->comment_parent ) . '</a>&nbsp;', $comment_text);
  101. else
  102. $comment_text = '<a href="#comment-' . $comment->comment_parent . '" class="comment-at">@'.get_comment_author( $comment->comment_parent ) . '</a>&nbsp;' . $comment_text;
  103. }
  104. return $comment_text;
  105. }
  106. add_filter( 'comment_text' , 'comment_add_at', 20, 2);
  107. /*
  108. * Ajax评论
  109. */
  110. if ( version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' ) ) { wp_die(__('Please upgrade wordpress to version 4.4+','sakurairo')); }/*请升级到4.4以上版本*/
  111. // 提示
  112. if(!function_exists('siren_ajax_comment_err')) {
  113. function siren_ajax_comment_err($t) {
  114. header('HTTP/1.0 500 Internal Server Error');
  115. header('Content-Type: text/plain;charset=UTF-8');
  116. echo $t;
  117. exit;
  118. }
  119. }
  120. // 机器评论验证
  121. function siren_robot_comment(){
  122. if ( !$_POST['no-robot'] && !is_user_logged_in()) {
  123. siren_ajax_comment_err('上车请刷卡。<br>Please comfirm you are not a robot.');
  124. }
  125. }
  126. if(iro_opt('not_robot')) add_action('pre_comment_on_post', 'siren_robot_comment');
  127. // 纯英文评论拦截
  128. function scp_comment_post( $incoming_comment ) {
  129. // 为什么要拦自己呢?
  130. global $user_ID;
  131. if( $user_ID && current_user_can('administrator') ) {
  132. return( $incoming_comment );
  133. } elseif(!preg_match('/[一-龥]/u', $incoming_comment['comment_content'])){
  134. siren_ajax_comment_err('写点汉字吧。You should add some Chinese words.');
  135. }
  136. return( $incoming_comment );
  137. }
  138. // add_filter('preprocess_comment', 'scp_comment_post');
  139. // 国际化很重要
  140. // 评论提交
  141. if(!function_exists('siren_ajax_comment_callback')) {
  142. function siren_ajax_comment_callback(){
  143. $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
  144. if( is_wp_error( $comment ) ) {
  145. $data = $comment->get_error_data();
  146. if ( !empty( $data ) ) {
  147. siren_ajax_comment_err($comment->get_error_message());
  148. } else {
  149. if(count($_POST)<=1){
  150. siren_ajax_comment_err("你好像提交了一个空表单。");
  151. }else{
  152. siren_ajax_comment_err(join(',',array_keys($comment->errors)));
  153. }
  154. }
  155. }
  156. $user = wp_get_current_user();
  157. do_action('set_comment_cookies', $comment, $user);
  158. $GLOBALS['comment'] = $comment; //根据你的评论结构自行修改,如使用默认主题则无需修改
  159. ?>
  160. <li <?php comment_class(); ?> id="comment-<?php echo esc_attr(comment_ID()); ?>">
  161. <div class="contents">
  162. <div class="comment-arrow">
  163. <div class="main shadow">
  164. <div class="profile">
  165. <a href="<?php comment_author_url(); ?>"><?php echo get_avatar( $comment->comment_author_email, 80, '', get_comment_author() ); ?></a>
  166. </div>
  167. <div class="commentinfo">
  168. <section class="commeta">
  169. <div class="left">
  170. <h4 class="author"><a href="<?php comment_author_url(); ?>"><?php echo get_avatar( $comment->comment_author_email, 80, '', get_comment_author() ); ?><?php comment_author(); ?> <span class="isauthor" title="<?php esc_attr_e('Author', 'sakurairo'); ?>"></span></a></h4>
  171. </div>
  172. <div class="right">
  173. <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')); ?></time></div>
  174. </div>
  175. </section>
  176. </div>
  177. <div class="body">
  178. <?php comment_text(); ?>
  179. </div>
  180. </div>
  181. <div class="arrow-left"></div>
  182. </div>
  183. </div>
  184. </li>
  185. <?php die();
  186. }
  187. }
  188. add_action('wp_ajax_nopriv_ajax_comment', 'siren_ajax_comment_callback');
  189. add_action('wp_ajax_ajax_comment', 'siren_ajax_comment_callback');
  190. /*
  191. * 前台登录
  192. */
  193. // 指定登录页面
  194. if(iro_opt('exlogin_url')){
  195. add_action('login_enqueue_scripts','login_protection');
  196. function login_protection(){
  197. if($_GET['word'] != 'press'){
  198. $admin_url = iro_opt('exlogin_url');
  199. wp_redirect( $admin_url );
  200. exit;
  201. }
  202. }
  203. }
  204. //attention: 目前仅有登陆模板在用这个函数捏,登陆模板是一个要deprecate的状态
  205. // 登录跳转
  206. function Exuser_center(){ ?>
  207. <script type='text/javascript'>
  208. <?php
  209. /* var URL;
  210. var TYPE; */ //不要污染全局命名空间啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
  211. ?>
  212. function gopage(url,descr) {
  213. function cb(sec) {
  214. document.getElementById('login-showtime').innerHTML = '<?= __("Login successful, ","sakurairo")/*空降成功*/?>'
  215. + sec + '<?= __("seconds later automatically transfer to","sakurairo")/*秒后自动转到*/?>' + descr;
  216. if (sec == 0) { window.location = url; } else { window.setTimeout(() => { cb(sec - 1); }, 1000); }
  217. }
  218. window.setTimeout(() => { cb(5); }, 1000); //倒计时秒数在这捏
  219. }
  220. </script>
  221. <?php if(current_user_can('administrator')){ ?>
  222. <div class="admin-login-check">
  223. <?php
  224. echo login_ok();
  225. if(iro_opt('login_urlskip')){ ?>
  226. <script>gopage("<?php bloginfo('url'); ?>/wp-admin/","<?=__('dashboard','sakurairo')/*管理后台*/?>");</script>
  227. <?php } ?>
  228. </div>
  229. <?php }else{ ?>
  230. <div class="user-login-check">
  231. <?php
  232. echo login_ok();
  233. if(iro_opt('login_urlskip')){?>
  234. <script>gopage("<?php bloginfo('url'); ?>","<?=__('home','sakurairo')/*主页*/?>");</script>
  235. <?php } ?>
  236. </div>
  237. <?php
  238. }
  239. }
  240. // 登录成功
  241. function login_ok(){
  242. global $current_user;
  243. wp_get_current_user();
  244. ?>
  245. <p class="ex-login-avatar"><a href="http://cn.gravatar.com/" title="<?php _e('Change avatar','sakurairo')/*更换头像*/?>" target="_blank" rel="nofollow"><?php echo get_avatar( $current_user->user_email, 110 ); ?></a></p>
  246. <p class="ex-login-username"><?php _e('Hello, ','sakurairo')/*你好,*/?><strong><?php echo $current_user->display_name; ?></strong></p>
  247. <?php if($current_user->user_email){echo '<p>'.$current_user->user_email.'</p>';} ?>
  248. <p id="login-showtime"></p>
  249. <p class="ex-logout">
  250. <a href="<?php bloginfo('url'); ?>" title="<?php _e('Home','sakurairo')/*首页*/?>"><?php _e('Home','sakurairo')/*首页*/?></a>
  251. <?php if(current_user_can('administrator')){ ?>
  252. <a href="<?php bloginfo('url'); ?>/wp-admin/" title="<?php _e('Manage','sakurairo')/*后台*/?>" target="_top"><?php _e('Manage','sakurairo')/*后台*/?></a>
  253. <?php } ?>
  254. <a href="<?php echo wp_logout_url(get_bloginfo('url')); ?>" title="<?php _e('Logout','sakurairo')/*登出*/?>" target="_top"><?php _e('Sign out? ','sakurairo')/*登出?*/?></a>
  255. </p>
  256. <?php
  257. }
  258. /*
  259. * 文章,页面头部背景图
  260. */
  261. function the_headPattern(){
  262. $t = ''; // 标题
  263. $full_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'full');
  264. $title_style = get_post_meta(get_the_ID(), 'title_style', true); // 获取自定义字段的值
  265. if(is_single()){
  266. require_once get_stylesheet_directory() . '/tpl/entry-census.php';
  267. $full_image_url = !empty($full_image_url) ? $full_image_url[0] : null;
  268. if (have_posts()) : while (have_posts()) : the_post();
  269. $center = 'single-center';
  270. $header = 'single-header';
  271. //$ava = iro_opt('personal_avatar', '') ? iro_opt('personal_avatar', '') : get_avatar_url(get_the_author_meta('user_email'));
  272. $t .= the_title( '<h1 class="entry-title" style="' . esc_attr($title_style) . '">', '</h1>', false);
  273. $t .= '<span class="toppic-line"></span>';
  274. $t .= get_entry_census_html(true);
  275. endwhile; endif;
  276. }elseif(is_page()){
  277. $full_image_url = !empty($full_image_url) ? $full_image_url[0] : null;
  278. $t .= the_title( '<span><h1 class="entry-title" style="' . esc_attr($title_style) . '">', '</h1></span>', false);
  279. }elseif(is_archive()){
  280. $full_image_url = z_taxonomy_image_url();
  281. $des = category_description() ? category_description() : ''; // 描述
  282. $t .= '<h1 class="cat-title">'.single_cat_title('', false).'</h1>';
  283. $t .= ' <span class="cat-des">'.$des.'</span>';
  284. }elseif(is_search()){
  285. $full_image_url = get_random_bg_url();
  286. $t .= '<h1 class="entry-title search-title"> '.sprintf( __( "Search results for \" %s \"","sakurairo" ), get_search_query()) ./*关于“ '.get_search_query().' ”的搜索结果*/'</h1>';
  287. }
  288. if(!iro_opt('patternimg')) $full_image_url = false;
  289. if(!is_home() && $full_image_url) : ?>
  290. <div class="pattern-center-blank"></div>
  291. <div class="pattern-center <?php if(is_single()){echo $center;} ?>">
  292. <div class="pattern-attachment bg lazyload" style="background-image: url(<?php echo iro_opt('load_out_svg'); ?>)" data-src="<?php echo $full_image_url; ?>"> </div>
  293. <header class="pattern-header <?php if(is_single()){echo $header;} ?>"><?php echo $t; ?></header>
  294. </div>
  295. <?php else :
  296. echo '<div class="blank"></div>';
  297. endif;
  298. }
  299. /**
  300. * 文章封面视频
  301. * @param $isHls
  302. */
  303. function the_video_headPattern(bool $isHls = false)
  304. {
  305. $t = ''; // 标题
  306. $thubm_image_urls = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'thumbnail');
  307. $video_cover = get_post_meta(get_the_ID(), 'video_cover', true);
  308. $video_cover_thumb = get_post_meta(get_the_ID(), 'video_cover_thumb', true);
  309. // 检查这个字段是否有值
  310. if (empty($video_cover_thumb)) { //如果值为空,输出默认值
  311. $video_poster_attr = "";
  312. } else {
  313. $video_poster_attr = ' poster="' . $video_cover_thumb . '" ';
  314. }
  315. $thubm_image_url = !empty($thubm_image_urls) ? $thubm_image_urls[0] : null;
  316. if (is_single()) {
  317. require_once get_stylesheet_directory() . '/tpl/entry-census.php';
  318. while (have_posts()) {
  319. the_post();
  320. $center = 'single-center';
  321. $header = 'single-header';
  322. //$ava = iro_opt('personal_avatar', '') ? iro_opt('personal_avatar', '') : get_avatar_url(get_the_author_meta('user_email'));
  323. $btn_playControl = '<button id="cv-pc" class="coverVideo-btn" onclick="coverVideo()"><i class="fa-solid fa-pause"></i></button>';
  324. // $btn_volumeControl = '<button id="cv-vc" class="coverVideo-btn" onclick="coverVideoMute()"><i class="fa-solid fa-volume-xmark"></i></button>';
  325. $t .= the_title('<h1 class="entry-title">', $btn_playControl./* $btn_volumeControl. */'</h1>', false);
  326. $t .= get_entry_census_html(true);
  327. }
  328. } elseif (is_page()) {
  329. $t .= the_title('<h1 class="entry-title">', '</h1>', false);
  330. } elseif (is_archive()) {
  331. $thubm_image_url = iro_opt('load_out_svg');
  332. $des = category_description() ? category_description() : ''; // 描述
  333. $t .= '<h1 class="cat-title">' . single_cat_title('', false) . '</h1>';
  334. $t .= ' <span class="cat-des">' . $des . '</span>';
  335. } elseif (is_search()) {
  336. $thubm_image_url = iro_opt('load_out_svg');
  337. $t .= '<h1 class="entry-title search-title"> ' . sprintf(__("Search results for \" %s \"", "sakurairo"), get_search_query()) ./*关于“ '.get_search_query().' ”的搜索结果*/ '</h1>';
  338. }
  339. $thubm_image_url = $thubm_image_url . "#lazyload-blur";
  340. $thubm_image_url = str_replace(iro_opt('image_cdn'), 'https://cdn.2heng.xin/', $thubm_image_url);
  341. if (!is_home()) { ?>
  342. <div class="pattern-center-blank"></div>
  343. <div class="pattern-center <?php if (is_single()) : echo $center;endif; ?>">
  344. <div class="pattern-attachment">
  345. <?php
  346. if ($isHls) {
  347. ?>
  348. <video loop playsinline muted id="coverVideo" class="hls" <?php echo $video_poster_attr; ?> data-src="<?php echo $video_cover; ?>"></video>
  349. <?php
  350. } else {
  351. ?>
  352. <video autoplay loop playsinline muted id="coverVideo" class="normal-cover-video" <?php echo $video_poster_attr; ?>>
  353. <source src="<?php echo $video_cover; ?>" type="video/mp4">
  354. <?php _e('Your browser does not support HTML5 video.','sakurairo')?>
  355. </video>
  356. <?php
  357. }
  358. ?>
  359. </div>
  360. <header class="pattern-header <?php if (is_single()) : echo $header;
  361. endif; ?>">
  362. <?php echo $t; ?>
  363. </header>
  364. </div>
  365. <?php } else {
  366. echo '<div class="blank"></div>';
  367. }
  368. }
  369. /*
  370. * 导航栏用户菜单
  371. */
  372. function header_user_menu()
  373. {
  374. global $current_user;
  375. wp_get_current_user();
  376. if (is_user_logged_in()) {
  377. $ava = iro_opt('personal_avatar') ? iro_opt('personal_avatar') : get_avatar_url($current_user->user_email);
  378. ?>
  379. <div class="header-user-avatar">
  380. <img alt="header_user_avatar" src="<?php echo get_avatar_url($current_user->ID, [64]);/*$ava;*/ ?>" width="30" height="30">
  381. <div class="header-user-menu">
  382. <div class="header-user-name">
  383. <?php _e("Signed in as", "sakurairo") ?>
  384. <div class="header-user-name-u"><?php echo $current_user->display_name; ?></div>
  385. </div>
  386. <div class="user-menu-option">
  387. <?php if (current_user_can('administrator')) { ?>
  388. <a href="<?php bloginfo('url'); ?>/wp-admin/" target="_blank"><?php _e('Dashboard', 'sakurairo')/*管理中心*/ ?></a>
  389. <a href="<?php bloginfo('url'); ?>/wp-admin/post-new.php" target="_blank"><?php _e('New post', 'sakurairo')/*撰写文章*/ ?></a>
  390. <?php } ?>
  391. <a href="<?php bloginfo('url'); ?>/wp-admin/profile.php" target="_blank"><?php _e('Profile', 'sakurairo')/*个人资料*/ ?></a>
  392. <a href="<?php echo wp_logout_url(get_bloginfo('url')); ?>" target="_top" data-no-pjax><?php _e('Sign out', 'sakurairo')/*退出登录*/ ?></a>
  393. </div>
  394. </div>
  395. </div>
  396. <?php
  397. } else {
  398. $ava = iro_opt('unlisted_avatar');
  399. global $wp;
  400. //https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
  401. //可以测试一下对不同的固定链接的兼容性
  402. $login_url = iro_opt('exlogin_url') ? iro_opt('exlogin_url') : wp_login_url(iro_opt('login_urlskip') ? '' : add_query_arg($wp->query_vars, home_url($wp->request)));
  403. ?>
  404. <div class="header-user-avatar">
  405. <a href="<?= $login_url ?>">
  406. <img alt="header_user_avatar" src="<?= $ava ?>" width="30" height="30">
  407. </a>
  408. <div class="header-user-menu">
  409. <div class="header-user-name no-logged">
  410. <a id="login-link" href="<?= $login_url ?>" data-no-pjax style="font-weight:bold;text-decoration:none"><?php _e('Log in', 'sakurairo')/*登录*/ ?></a>
  411. </div>
  412. </div>
  413. </div>
  414. <?php
  415. }
  416. }
  417. /**
  418. * 移动端侧边栏用户菜单
  419. *
  420. * @return void
  421. */
  422. function m_user_menu()
  423. {
  424. global $current_user;
  425. wp_get_current_user();
  426. if (is_user_logged_in()) {?>
  427. <div class="m-user-menu">
  428. <div class="m-user-name">
  429. <span><?php echo $current_user->display_name; ?></span>
  430. </div>
  431. <div class="m-user-menu-option">
  432. <?php if (current_user_can('administrator')) { ?>
  433. <a href="<?php bloginfo('url'); ?>/wp-admin/" target="_blank"><?php _e('Dashboard', 'sakurairo')/*管理中心*/ ?></a>
  434. <a href="<?php bloginfo('url'); ?>/wp-admin/post-new.php" target="_blank"><?php _e('New post', 'sakurairo')/*撰写文章*/ ?></a>
  435. <?php } ?>
  436. <a href="<?php bloginfo('url'); ?>/wp-admin/profile.php" target="_blank"><?php _e('Profile', 'sakurairo')/*个人资料*/ ?></a>
  437. <a href="<?php echo wp_logout_url(get_bloginfo('url')); ?>" target="_top" data-no-pjax><?php _e('Sign out', 'sakurairo')/*退出登录*/ ?></a>
  438. </div>
  439. </div>
  440. <?php
  441. } else {
  442. global $wp;
  443. $login_url = iro_opt('exlogin_url') ? iro_opt('exlogin_url') : wp_login_url(iro_opt('login_urlskip') ? '' : add_query_arg($wp->query_vars, home_url($wp->request)));
  444. ?>
  445. <div class="m-user-avatar">
  446. <div class="m-user-menu">
  447. <div class="m-user-name no-logged">
  448. <a id="login-link" href="<?= $login_url ?>" data-no-pjax style="font-weight:bold;text-decoration:none"><?php _e('Log in', 'sakurairo')/*登录*/ ?></a>
  449. <?php if (get_option('users_can_register')) { ?>
  450. <a style="font-weight:bold;text-decoration:none" href="<?php echo wp_registration_url() ?>"><?php _e('Register') ?></a>
  451. <?php } ?>
  452. </div>
  453. </div>
  454. </div>
  455. <?php
  456. }
  457. }
  458. /*
  459. * 获取相邻文章缩略图
  460. * 特色图 -> 文章图 -> 首页图
  461. */
  462. // 上一篇
  463. function get_prev_thumbnail_url() {
  464. if (iro_opt('classify_display')) {
  465. $classify_display_id = iro_opt('classify_display');
  466. } else {
  467. $classify_display_id = null;
  468. }
  469. $prev_post = get_previous_post($in_same_term = false, $excluded_terms = $classify_display_id, $taxonomy = 'category');
  470. if (!$prev_post) {
  471. return get_random_bg_url(); // 首页图
  472. } else if ( has_post_thumbnail($prev_post->ID) ) {
  473. $img_src = wp_get_attachment_image_src( get_post_thumbnail_id( $prev_post->ID ), 'large');
  474. return $img_src[0] ?? null; // 特色图
  475. }
  476. else {
  477. $content = $prev_post->post_content;
  478. preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
  479. $n = count($strResult[1]);
  480. if($n > 0){
  481. return $strResult[1][0]; // 文章图
  482. }else{
  483. return get_random_bg_url(); // 首页图
  484. }
  485. }
  486. }
  487. // 下一篇
  488. function get_next_thumbnail_url() {
  489. if (iro_opt('classify_display')) {
  490. $classify_display_id = iro_opt('classify_display');
  491. } else {
  492. $classify_display_id = null;
  493. }
  494. $next_post = get_next_post($in_same_term = false, $excluded_terms = $classify_display_id, $taxonomy = 'category');
  495. if( $next_post instanceof WP_Post){
  496. if ( has_post_thumbnail($next_post->ID) ) {
  497. $img_src = wp_get_attachment_image_src( get_post_thumbnail_id( $next_post->ID ), 'large');
  498. return $img_src[0] ?? null;
  499. }
  500. else {
  501. $content = $next_post->post_content;
  502. preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
  503. $n = count($strResult[1]);
  504. if($n > 0){
  505. return $strResult[1][0];
  506. }else{
  507. return get_random_bg_url();
  508. }
  509. }
  510. }
  511. }
  512. /**
  513. * 文章摘要
  514. */
  515. function changes_post_excerpt_more( $more ) {
  516. return ' ...';
  517. }
  518. function changes_post_excerpt_length( $length ) {
  519. return 65;
  520. }
  521. add_filter( 'excerpt_more', 'changes_post_excerpt_more' );
  522. add_filter( 'excerpt_length', 'changes_post_excerpt_length', 999 );
  523. /*
  524. * SEO优化
  525. */
  526. // 外部链接自动加nofollow
  527. add_filter( 'the_content', 'siren_auto_link_nofollow');
  528. function siren_auto_link_nofollow( $content ) {
  529. $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
  530. if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
  531. if( !empty($matches) ) {
  532. $srcUrl = get_option('siteurl');
  533. foreach($matches as $result){
  534. $tag = $result[0];
  535. $tag2 = $result[0];
  536. $url = $result[0];
  537. $noFollow = '';
  538. $pattern = '/target\s*=\s*"\s*_blank\s*"/';
  539. preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  540. if( count($match) < 1 )
  541. $noFollow .= ' target="_blank" ';
  542. $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
  543. preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  544. if( count($match) < 1 )
  545. $noFollow .= ' rel="nofollow" ';
  546. $pos = strpos($url,$srcUrl);
  547. if ($pos === false) {
  548. $tag = rtrim ($tag,'>');
  549. $tag .= $noFollow.'>';
  550. $content = str_replace($tag2,$tag,$content);
  551. }
  552. }
  553. }
  554. }
  555. $content = str_replace(']]>', ']]>', $content);
  556. return $content;
  557. }
  558. // 图片自动加标题
  559. add_filter('the_content', 'siren_auto_images_alt');
  560. function siren_auto_images_alt($content) {
  561. global $post;
  562. $post_title = $post ? $post->post_title : '默认标题'; // 检查 $post 是否为空
  563. // 优化正则表达式
  564. $pattern = '/<a([^>]*?)href=(["\'])([^"\']*?\.(?:bmp|gif|jpeg|jpg|png))\2([^>]*?)>/i';
  565. $replacement = '<a$1href=$2$3$2 alt="' . esc_attr($post_title) . '" title="' . esc_attr($post_title) . '"$4>';
  566. // 使用 preg_replace_callback 以提高性能
  567. $content = preg_replace_callback($pattern, function($matches) use ($post_title) {
  568. return '<a' . $matches[1] . 'href=' . $matches[2] . $matches[3] . $matches[2] . ' alt="' . esc_attr($post_title) . '" title="' . esc_attr($post_title) . '"' . $matches[4] . '>';
  569. }, $content);
  570. return $content;
  571. }
  572. // 分类页面全部添加斜杠,利于SEO
  573. function siren_nice_trailingslashit($string, $type_of_url) {
  574. if ( $type_of_url != 'single' )
  575. $string = trailingslashit($string);
  576. return $string;
  577. }
  578. add_filter('user_trailingslashit', 'siren_nice_trailingslashit', 10, 2);
  579. // 去除链接显示categroy
  580. add_action( 'load-themes.php', 'no_category_base_refresh_rules');
  581. add_action('created_category', 'no_category_base_refresh_rules');
  582. add_action('edited_category', 'no_category_base_refresh_rules');
  583. add_action('delete_category', 'no_category_base_refresh_rules');
  584. function no_category_base_refresh_rules() {
  585. global $wp_rewrite;
  586. $wp_rewrite -> flush_rules();
  587. }
  588. // Remove category base
  589. add_action('init', 'no_category_base_permastruct');
  590. function no_category_base_permastruct() {
  591. global $wp_rewrite, $wp_version;
  592. if (version_compare($wp_version, '3.4', '<')) {
  593. } else {
  594. $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
  595. }
  596. }
  597. // Add our custom category rewrite rules
  598. add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
  599. function no_category_base_rewrite_rules($category_rewrite) {
  600. //var_dump($category_rewrite); // For Debugging
  601. $category_rewrite = array();
  602. $categories = get_categories(array('hide_empty' => false));
  603. foreach ($categories as $category) {
  604. $category_nicename = $category -> slug;
  605. if ($category -> parent == $category -> cat_ID)// recursive recursion
  606. $category -> parent = 0;
  607. elseif ($category -> parent != 0)
  608. $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
  609. $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
  610. $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
  611. $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
  612. }
  613. // Redirect support from Old Category Base
  614. global $wp_rewrite;
  615. $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
  616. $old_category_base = trim($old_category_base, '/');
  617. $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
  618. //var_dump($category_rewrite); // For Debugging
  619. return $category_rewrite;
  620. }
  621. // Add 'category_redirect' query variable
  622. add_filter('query_vars', 'no_category_base_query_vars');
  623. function no_category_base_query_vars($public_query_vars) {
  624. $public_query_vars[] = 'category_redirect';
  625. return $public_query_vars;
  626. }
  627. // Redirect if 'category_redirect' is set
  628. add_filter('request', 'no_category_base_request');
  629. function no_category_base_request($query_vars) {
  630. //print_r($query_vars); // For Debugging
  631. if (isset($query_vars['category_redirect'])) {
  632. $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
  633. status_header(301);
  634. header("Location: $catlink");
  635. exit();
  636. }
  637. return $query_vars;
  638. }
  639. // 去除链接显示categroy END ~
  640. /**
  641. * 更改作者页链接为昵称显示
  642. */
  643. // Replace the user name using the nickname, query by user ID
  644. add_filter( 'request', 'siren_request' );
  645. function siren_request( $query_vars ){
  646. if ( array_key_exists( 'author_name', $query_vars ) ) {
  647. global $wpdb;
  648. $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
  649. if ( $author_id ) {
  650. $query_vars['author'] = $author_id;
  651. unset( $query_vars['author_name'] );
  652. }
  653. }
  654. return $query_vars;
  655. }
  656. /*
  657. * 私密评论
  658. * @bigfa
  659. */
  660. function siren_private_message_hook($comment_content , $comment){
  661. $comment_ID = $comment->comment_ID;
  662. $parent_ID = $comment->comment_parent;
  663. $parent_email = get_comment_author_email($parent_ID);
  664. $is_private = get_comment_meta($comment_ID,'_private',true);
  665. $email = $comment->comment_author_email;
  666. $current_commenter = wp_get_current_commenter();
  667. if ( $is_private ) $comment_content = '#私密# ' . $comment_content;
  668. if ( $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] || current_user_can('delete_user') ) return $comment_content;
  669. if ( $is_private ) return '<i class="fa-solid fa-lock"></i> '.__("The comment is private","sakurairo")/*该评论为私密评论*/;
  670. return $comment_content;
  671. }
  672. add_filter('get_comment_text','siren_private_message_hook',10,2);
  673. function siren_mark_private_message($comment_id){
  674. if ( isset($_POST['is-private']) ) {
  675. update_comment_meta($comment_id,'_private','true');
  676. }
  677. }
  678. add_action('comment_post', 'siren_mark_private_message');
  679. /*
  680. * 删除后台某些版权和链接
  681. * @wpdx
  682. */
  683. add_filter('admin_title', 'wpdx_custom_admin_title', 10, 2);
  684. function wpdx_custom_admin_title($admin_title, $title){
  685. return $title.' &lsaquo; '.get_bloginfo('name');
  686. }
  687. //去掉Wordpress LOGO
  688. function remove_logo($wp_toolbar) {
  689. $wp_toolbar->remove_node('wp-logo');
  690. }
  691. add_action('admin_bar_menu', 'remove_logo', 999);
  692. //去掉Wordpress 底部版权
  693. function change_footer_admin () {return '';}
  694. add_filter('admin_footer_text', 'change_footer_admin', 9999);
  695. function change_footer_version() {return '';}
  696. add_filter( 'update_footer', 'change_footer_version', 9999);
  697. //去掉Wordpres挂件
  698. function disable_dashboard_widgets() {
  699. //remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');//近期评论
  700. //remove_meta_box('dashboard_recent_drafts', 'dashboard', 'normal');//近期草稿
  701. remove_meta_box('dashboard_primary', 'dashboard', 'core');//wordpress博客
  702. remove_meta_box('dashboard_secondary', 'dashboard', 'core');//wordpress其它新闻
  703. remove_meta_box('dashboard_right_now', 'dashboard', 'core');//wordpress概况
  704. //remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');//wordresss链入链接
  705. //remove_meta_box('dashboard_plugins', 'dashboard', 'core');//wordpress链入插件
  706. //remove_meta_box('dashboard_quick_press', 'dashboard', 'core');//wordpress快速发布
  707. }
  708. add_action('admin_menu', 'disable_dashboard_widgets');
  709. /**
  710. * 获取用户UA信息
  711. */
  712. // 浏览器信息
  713. function siren_get_browsers(string $ua):array{
  714. $title = 'Unknow';
  715. $icon = 'unknown';
  716. if (strpos($ua, 'Chrome')){
  717. if (strpos($ua, 'Edg') && preg_match('#Edg/([0-9]+)#i', $ua, $matches)){
  718. $title = 'Edge '. $matches[1];
  719. $icon = 'edge';
  720. }elseif (strpos($ua, '360EE')) {
  721. $title = '360 Browser ';
  722. $icon = '360se';
  723. }elseif (strpos($ua, 'OPR') && preg_match('#OPR/([0-9]+)#i', $ua, $matches)) {
  724. $title = 'Opera '. $matches[1];
  725. $icon = 'opera';
  726. }elseif (preg_match('#Chrome/([0-9]+)#i', $ua, $matches)) {
  727. $title = 'Chrome '. $matches[1];
  728. $icon = 'chrome';
  729. }
  730. }elseif (strpos($ua, 'Firefox') && preg_match('#Firefox/([0-9]+)#i', $ua, $matches)){
  731. $title = 'Firefox '. $matches[1];
  732. $icon = 'firefox';
  733. }elseif (strpos($ua, 'Safari') && preg_match('#Safari/([0-9]+)#i', $ua, $matches)){
  734. $title = 'Safari '. $matches[1];
  735. $icon = 'safari';
  736. }
  737. return [
  738. 'title' => $title,
  739. 'icon' => $icon
  740. ];
  741. }
  742. // 操作系统信息
  743. function siren_get_os(string $ua):array{
  744. $title = 'Unknow';
  745. $icon = 'unknown';
  746. // UA样式决定strpos返回值不可能为0 所以不需要考虑为0的情况
  747. if (strpos($ua, 'Win')) {
  748. if (strpos($ua, 'Windows NT 10.0')){
  749. $title = "Windows 10/11";
  750. $icon = "win10-11";
  751. }elseif (strpos($ua, 'Windows NT 6.1')) {
  752. $title = "Windows 7";
  753. $icon = "win7";
  754. }elseif (strpos($ua, 'Windows NT 6.2')) {
  755. $title = "Windows 8";
  756. $icon = "win8";
  757. }elseif (strpos($ua, 'Windows NT 6.3')) {
  758. $title = "Windows 8.1";
  759. $icon = "win8";
  760. }
  761. }elseif (strpos($ua, 'iPhone OS') && preg_match('#iPhone OS ([0-9]+)#i', $ua, $matches)) {// 1.2 修改成 iphone os 来判断
  762. $title = "iOS ".$matches[1];
  763. $icon = "apple";
  764. }elseif (strpos($ua, 'Android') && preg_match('/Android.([0-9. _]+)/i', $ua, $matches)) {
  765. if(count(explode(7,$matches[1]))>1) $matches[1] = 'Lion '.$matches[1];
  766. elseif(count(explode(8,$matches[1]))>1) $matches[1] = 'Mountain Lion '.$matches[1];
  767. $title= $matches[0];
  768. $icon = "android";
  769. }elseif (strpos($ua, 'Mac OS') && preg_match('/Mac OS X.([\d. _]+)/i', $ua, $matches)) {
  770. $mac_ver = intval(explode('_',$matches[1])[1]);
  771. $mac_code_name = '';
  772. $has_x = $mac_ver <12;
  773. $mac_code_list = ['Cheetah','Puma','Jaguar','Panther','Tiger','Leopard','Snow Leopard','Lion','Mountain Lion','Mavericks','Yosemite','El Capitan','Sierra','High Sierra','Mojave','Catalina or Higher']; // 总16个,后续请在最后添加并且修改该条注释.
  774. if (isset($mac_code_list[$mac_ver])) {
  775. $mac_code_name = $mac_code_list[$mac_ver];
  776. }
  777. $matches[1] = $mac_code_name.' '.$matches[1];
  778. $title = 'macOS '.($has_x?'X':''.' ').str_replace('_','.',$matches[1]);
  779. $icon = "apple";
  780. }elseif (strpos($ua, 'Macintosh')) {
  781. $title = "macOS";
  782. $icon = "apple";
  783. }elseif (strpos($ua, 'Linux')) {
  784. $title = 'Linux';
  785. $icon = 'linux';
  786. }
  787. return [
  788. 'title' => $title,
  789. 'icon' => $icon
  790. ];
  791. }
  792. function siren_get_useragent(string $ua):string{
  793. if(iro_opt('comment_useragent')){
  794. // $imgurl = get_bloginfo('template_directory') . '/images/ua/';
  795. $imgurl = iro_opt('vision_resource_basepath').'ua/';
  796. $browser = siren_get_browsers($ua);
  797. $os = siren_get_os($ua);
  798. return '&nbsp;&nbsp;<span class="useragent-info">( <img alt="browser_icon" src="'. $imgurl.$browser['icon'] .'.svg">&nbsp;'. $browser['title'] .'&nbsp;&nbsp;<img alt="os_icon" src="'. $imgurl.$os['icon'] .'.svg">&nbsp;'. $os['title'] .' )</span>'; }
  799. return '';
  800. }
  801. // UA 显示移动定制
  802. function mobile_get_useragent_icon(string $ua):string{
  803. if(iro_opt('comment_useragent')){
  804. $imgurl = iro_opt('vision_resource_basepath').'ua/';
  805. $browser = siren_get_browsers($ua);
  806. $os = siren_get_os($ua);
  807. return '<span class="useragent-info-m">( <img alt="browser_icon" src="'. $imgurl.$browser['icon'] .'.svg">&nbsp;&nbsp;<img alt="os_icon" src="'. $imgurl.$os['icon'] .'.svg"> )</span>';
  808. }
  809. return '';
  810. }
  811. /*
  812. * 打赏
  813. */
  814. function the_reward(){
  815. $alipay = iro_opt('alipay_code');
  816. $wechat = iro_opt('wechat_code');
  817. if($alipay || $wechat){
  818. $alipay = $alipay ? '<li class="alipay-code"><img alt="alipay_img" src="'.$alipay.'"></li>' : '';
  819. $wechat = $wechat ? '<li class="wechat-code"><img alt="wechat_img" src="'.$wechat.'"></li>' : '';
  820. ?>
  821. <div class="reward-open"> <i class="fa-solid fa-piggy-bank fa-sm"></i>
  822. <div class="reward-main">
  823. <ul class="reward-row">
  824. <?php echo $alipay.$wechat; ?>
  825. </ul>
  826. </div>
  827. </div>
  828. <?php
  829. }
  830. }