theme_plus.php 33 KB

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