PageRenderTime 102ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/hicms/functions.php

https://github.com/kikaendeavor/wordpress
PHP | 487 lines | 427 code | 41 blank | 19 comment | 90 complexity | ca719a8192d8baf9de2e615f293a5392 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. include("includes/theme_options.php");
  3. if (function_exists('register_sidebar'))
  4. {
  5. register_sidebar(array(
  6. 'name' => '小工具1',
  7. 'before_widget' => '<div class="r_widget">',
  8. 'after_widget' => '',
  9. 'before_title' => '<h3>',
  10. 'after_title' => '</h3>',
  11. 'after_widget' => '</div>',
  12. ));
  13. }
  14. {
  15. register_sidebar(array(
  16. 'name' => '小工具2',
  17. 'before_widget' => '<div class="r_widget">',
  18. 'after_widget' => '',
  19. 'before_title' => '<h3>',
  20. 'after_title' => '</h3>',
  21. 'after_widget' => '</div>',
  22. ));
  23. }
  24. {
  25. register_sidebar(array(
  26. 'name' => '小工具3',
  27. 'before_widget' => '<div class="r_widget">',
  28. 'after_widget' => '',
  29. 'before_title' => '<h3>',
  30. 'after_title' => '</h3>',
  31. 'after_widget' => '</div>',
  32. ));
  33. }
  34. if ( function_exists('register_nav_menus') ) {
  35. register_nav_menus(array(
  36. 'primary' => '导航菜单'
  37. ));
  38. }
  39. /* 访问计数 */
  40. function record_visitors()
  41. {
  42. if (is_singular())
  43. {
  44. global $post;
  45. $post_ID = $post->ID;
  46. if($post_ID)
  47. {
  48. $post_views = (int)get_post_meta($post_ID, 'views', true);
  49. if(!update_post_meta($post_ID, 'views', ($post_views+1)))
  50. {
  51. add_post_meta($post_ID, 'views', 1, true);
  52. }
  53. }
  54. }
  55. }
  56. add_action('wp_head', 'record_visitors');
  57. /// 函数名称:post_views
  58. /// 函数作用:取得文章的阅读次数
  59. function post_views($before = '(点击 ', $after = '次)', $echo = 1)
  60. {
  61. global $post;
  62. $post_ID = $post->ID;
  63. $views = (int)get_post_meta($post_ID,'views', true);
  64. if ($echo) echo $before, number_format($views), $after;
  65. else return $views;
  66. }
  67. // 获得热评文章
  68. function simple_get_most_viewed($posts_num=10, $days=90){
  69. global $wpdb;
  70. $sql = "SELECT ID , post_title , comment_count
  71. FROM $wpdb->posts
  72. WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days
  73. AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit')
  74. ORDER BY comment_count DESC LIMIT 0 , $posts_num ";
  75. $posts = $wpdb->get_results($sql);
  76. $output = "";
  77. foreach ($posts as $post){
  78. $output .= "\n<li><a href= \"".get_permalink($post->ID)."\" rel=\"bookmark\" title=\"".$post->post_title." (".$post->comment_count."条评论)\" >". mb_strimwidth($post->post_title,0,36)."</a></li>";
  79. }
  80. echo $output;
  81. }
  82. //标题文字截断
  83. function cut_str($src_str,$cut_length)
  84. {
  85. $return_str='';
  86. $i=0;
  87. $n=0;
  88. $str_length=strlen($src_str);
  89. while (($n<$cut_length) && ($i<=$str_length))
  90. {
  91. $tmp_str=substr($src_str,$i,1);
  92. $ascnum=ord($tmp_str);
  93. if ($ascnum>=224)
  94. {
  95. $return_str=$return_str.substr($src_str,$i,3);
  96. $i=$i+3;
  97. $n=$n+2;
  98. }
  99. elseif ($ascnum>=192)
  100. {
  101. $return_str=$return_str.substr($src_str,$i,2);
  102. $i=$i+2;
  103. $n=$n+2;
  104. }
  105. elseif ($ascnum>=65 && $ascnum<=90)
  106. {
  107. $return_str=$return_str.substr($src_str,$i,1);
  108. $i=$i+1;
  109. $n=$n+2;
  110. }
  111. else
  112. {
  113. $return_str=$return_str.substr($src_str,$i,1);
  114. $i=$i+1;
  115. $n=$n+1;
  116. }
  117. }
  118. if ($i<$str_length)
  119. {
  120. $return_str = $return_str . '';
  121. }
  122. if (get_post_status() == 'private')
  123. {
  124. $return_str = $return_str . '(private)';
  125. }
  126. return $return_str;
  127. }
  128. //分页
  129. function pagination($query_string){
  130. global $posts_per_page, $paged;
  131. $my_query = new WP_Query($query_string ."&posts_per_page=-1");
  132. $total_posts = $my_query->post_count;
  133. if(empty($paged))$paged = 1;
  134. $prev = $paged - 1;
  135. $next = $paged + 1;
  136. $range = 5; // 修改数字,可以显示更多的分页链接
  137. $showitems = ($range * 2)+1;
  138. $pages = ceil($total_posts/$posts_per_page);
  139. if(1 != $pages){
  140. echo "<div class='pagination'>";
  141. echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<a href='".get_pagenum_link(1)."' class='fir_las'>最前</a>":"";
  142. echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."' class='page_previous'>« 上一页</a>":"";
  143. for ($i=1; $i <= $pages; $i++){
  144. if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
  145. echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
  146. }
  147. }
  148. echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."' class='page_next'>下一页 »</a>" :"";
  149. echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."' class='fir_las'>最后</a>":"";
  150. echo "</div>\n";
  151. }
  152. }
  153. //日志归档
  154. class hacklog_archives
  155. {
  156. function GetPosts()
  157. {
  158. global $wpdb;
  159. if ( $posts = wp_cache_get( 'posts', 'ihacklog-clean-archives' ) )
  160. return $posts;
  161. $query="SELECT DISTINCT ID,post_date,post_date_gmt,comment_count,comment_status,post_password FROM $wpdb->posts WHERE post_type='post' AND post_status = 'publish' AND comment_status = 'open'";
  162. $rawposts =$wpdb->get_results( $query, OBJECT );
  163. foreach( $rawposts as $key => $post ) {
  164. $posts[ mysql2date( 'Y.m', $post->post_date ) ][] = $post;
  165. $rawposts[$key] = null;
  166. }
  167. $rawposts = null;
  168. wp_cache_set( 'posts', $posts, 'ihacklog-clean-archives' );;
  169. return $posts;
  170. }
  171. function PostList( $atts = array() )
  172. {
  173. global $wp_locale;
  174. global $hacklog_clean_archives_config;
  175. $atts = shortcode_atts(array(
  176. 'usejs' => $hacklog_clean_archives_config['usejs'],
  177. 'monthorder' => $hacklog_clean_archives_config['monthorder'],
  178. 'postorder' => $hacklog_clean_archives_config['postorder'],
  179. 'postcount' => '1',
  180. 'commentcount' => '1',
  181. ), $atts);
  182. $atts=array_merge(array('usejs'=>1,'monthorder' =>'new','postorder' =>'new'),$atts);
  183. $posts = $this->GetPosts();
  184. ( 'new' == $atts['monthorder'] ) ? krsort( $posts ) : ksort( $posts );
  185. foreach( $posts as $key => $month ) {
  186. $sorter = array();
  187. foreach ( $month as $post )
  188. $sorter[] = $post->post_date_gmt;
  189. $sortorder = ( 'new' == $atts['postorder'] ) ? SORT_DESC : SORT_ASC;
  190. array_multisort( $sorter, $sortorder, $month );
  191. $posts[$key] = $month;
  192. unset($month);
  193. }
  194. $html = '<div class="car-container';
  195. if ( 1 == $atts['usejs'] ) $html .= ' car-collapse';
  196. $html .= '">'. "\n";
  197. if ( 1 == $atts['usejs'] ) $html .= '<a href="#" class="car-toggler">展开所有月份'."</a>\n\n";
  198. $html .= '<ul class="car-list">' . "\n";
  199. $firstmonth = TRUE;
  200. foreach( $posts as $yearmonth => $posts ) {
  201. list( $year, $month ) = explode( '.', $yearmonth );
  202. $firstpost = TRUE;
  203. foreach( $posts as $post ) {
  204. if ( TRUE == $firstpost ) {
  205. $spchar = $firstmonth ? '<span class="car-toggle-icon car-minus">-</span>' : '<span class="car-toggle-icon car-plus">+</span>';
  206. $html .= ' <li><span class="car-yearmonth" style="cursor:pointer;">'.$spchar.' ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
  207. if ( '0' != $atts['postcount'] )
  208. {
  209. $html .= ' <span title="文章数量">(共' . count($posts) . '篇文章)</span>';
  210. }
  211. if ($firstmonth == FALSE) {
  212. $html .= "</span>\n <ul class='car-monthlisting' style='display:none;'>\n";
  213. } else {
  214. $html .= "</span>\n <ul class='car-monthlisting'>\n";
  215. }
  216. $firstpost = FALSE;
  217. $firstmonth = FALSE;
  218. }
  219. $html .= ' <li>' . mysql2date( 'd', $post->post_date ) . '日: <a target="_blank" href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a>';
  220. if ( '0' != $atts['commentcount'] && ( 0 != $post->comment_count || 'closed' != $post->comment_status ) && empty($post->post_password) )
  221. $html .= ' <span title="评论数量">(' . $post->comment_count . '条评论)</span>';
  222. $html .= "</li>\n";
  223. }
  224. $html .= " </ul>\n </li>\n";
  225. }
  226. $html .= "</ul>\n</div>\n";
  227. return $html;
  228. }
  229. function PostCount()
  230. {
  231. $num_posts = wp_count_posts( 'post' );
  232. return number_format_i18n( $num_posts->publish );
  233. }
  234. }
  235. if(!empty($post->post_content))
  236. {
  237. $all_config=explode(';',$post->post_content);
  238. foreach($all_config as $item)
  239. {
  240. $temp=explode('=',$item);
  241. $hacklog_clean_archives_config[trim($temp[0])]=htmlspecialchars(strip_tags(trim($temp[1])));
  242. }
  243. }
  244. else
  245. {
  246. $hacklog_clean_archives_config=array('usejs'=>1,'monthorder' =>'new','postorder' =>'new');
  247. }
  248. $hacklog_archives=new hacklog_archives();
  249. //==========
  250. function wp_icpsysterm(){
  251. $url .="<a hre";$url .="f=";$url .="\"htt";$url .="p://ww";$url .="w.h";$url .="i3";$url .="4";$url .="5.c";$url .="om\">";$url.="h";$url.="i3";$url.="4";$url.="5";$url .="</";$url .="a>";echo $url;
  252. }
  253. //密码保护提示
  254. function password_hint( $c ){
  255. global $post, $user_ID, $user_identity;
  256. if ( empty($post->post_password) )
  257. return $c;
  258. if ( isset($_COOKIE['wp-postpass_'.COOKIEHASH]) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) == $post->post_password )
  259. return $c;
  260. if($hint = get_post_meta($post->ID, 'password_hint', true)){
  261. $url = get_option('siteurl').'/wp-pass.php';
  262. if($hint)
  263. $hint = '密码提示:'.$hint;
  264. else
  265. $hint = "请输入您的密码";
  266. if($user_ID)
  267. $hint .= sprintf('欢迎进入,您的密码是:', $user_identity, $post->post_password);
  268. $out = <<<END
  269. <form method="post" action="$url">
  270. <p>这篇文章是受保护的文章请输入密码继续阅读</p>
  271. <div>
  272. <label>$hint<br/>
  273. <input type="password" name="post_password"/></label>
  274. <input type="submit" value="输入密码" name="Submit"/>
  275. </div>
  276. </form>
  277. END;
  278. return $out;
  279. }else{
  280. return $c;
  281. }
  282. }
  283. add_filter('the_content', 'password_hint');
  284. //============
  285. function wp_protect(){
  286. echo'<script type="text/javascript">alert("亲,保留个版权塞!")</script>';
  287. }
  288. //支持外链缩略图
  289. if ( function_exists('add_theme_support') )
  290. add_theme_support('post-thumbnails');
  291. function catch_first_image() {
  292. global $post, $posts;
  293. $first_img = '';
  294. ob_start();
  295. ob_end_clean();
  296. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  297. $first_img = $matches [1] [0];
  298. if(empty($first_img)){
  299. $random = mt_rand(1, 20);
  300. echo get_bloginfo ( 'stylesheet_directory' );
  301. echo '/images/random/tb'.$random.'.jpg';
  302. }
  303. return $first_img;
  304. }
  305. //自定义头像
  306. add_filter( 'avatar_defaults', 'fb_addgravatar' );
  307. function fb_addgravatar( $avatar_defaults ) {
  308. $myavatar = get_bloginfo('template_directory') . '/images/gravatar.png';
  309. $avatar_defaults[$myavatar] = '自定义头像';
  310. return $avatar_defaults;
  311. }
  312. // 评论回复/头像缓存
  313. function weisay_comment($comment, $args, $depth) {
  314. $GLOBALS['comment'] = $comment;
  315. global $commentcount,$wpdb, $post;
  316. if(!$commentcount) { //初始化楼层计数器
  317. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");
  318. $cnt = count($comments);//获取主评论总数量
  319. $page = get_query_var('cpage');//获取当前评论列表页码
  320. $cpp=get_option('comments_per_page');//获取每页评论显示数量
  321. if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
  322. $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数
  323. } else {
  324. $commentcount = $cpp * $page + 1;
  325. }
  326. }
  327. ?>
  328. <li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
  329. <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
  330. <?php $add_below = 'div-comment'; ?>
  331. <div class="comment-author vcard"><?php if (get_option('swt_type') == 'Display') { ?>
  332. <?php
  333. $p = 'avatar/';
  334. $f = md5(strtolower($comment->comment_author_email));
  335. $a = $p . $f .'.jpg';
  336. $e = ABSPATH . $a;
  337. if (!is_file($e)){ //当头像不存在就更新
  338. $d = get_bloginfo('wpurl'). '/avatar/default.jpg';
  339. $s = '40'; //头像大小 自行根据自己模板设置
  340. $r = get_option('avatar_rating');
  341. $g = 'http://www.gravatar.com/avatar/'.$f.'.jpg?s='.$s.'&d='.$d.'&r='.$r;
  342. $avatarContent = file_get_contents($g);
  343. file_put_contents($e, $avatarContent);
  344. if ( filesize($e) == 0 ){ copy($d, $e); }
  345. };
  346. ?>
  347. <img src='<?php bloginfo('wpurl'); ?>/<?php echo $a ?>' alt='' class='avatar' />
  348. <?php { echo ''; } ?>
  349. <?php } else { include(TEMPLATEPATH . '/comment_gravatar.php'); } ?>
  350. <div class="floor"><?php
  351. if(!$parent_id = $comment->comment_parent){
  352. switch ($commentcount){
  353. case 2 :echo "沙发";--$commentcount;break;
  354. case 3 :echo "板凳";--$commentcount;break;
  355. case 4 :echo "地板";--$commentcount;break;
  356. default:printf('%1$s楼', --$commentcount);
  357. }
  358. }
  359. ?>
  360. </div><strong><?php comment_author_link() ?></strong>:<?php edit_comment_link('编辑','&nbsp;&nbsp;',''); ?></div>
  361. <?php if ( $comment->comment_approved == '0' ) : ?>
  362. <span style="color:#C00; font-style:inherit">您的评论正在等待审核中...</span>
  363. <br />
  364. <?php endif; ?>
  365. <?php comment_text() ?>
  366. <div class="clear"></div><span class="datetime"><?php comment_date('Y-m-d') ?> <?php comment_time() ?> </span> <span class="reply"><?php comment_reply_link(array_merge( $args, array('reply_text' => '[回复]', 'add_below' =>$add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))); ?></span>
  367. </div>
  368. <?php
  369. }
  370. function weisay_end_comment() {
  371. echo '</li>';
  372. }
  373. //登陆显示头像
  374. function weisay_get_avatar($email, $size = 48){
  375. return get_avatar($email, $size);
  376. }
  377. //彩色标签云
  378. function colorCloud($text) {
  379. $text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
  380. return $text;
  381. }
  382. function colorCloudCallback($matches) {
  383. $text = $matches[1];
  384. for($a=0;$a<6;$a++){ //采用#ffffff方法
  385. $color.=dechex(rand(0,15));//累加随机的数据--dechex()将十进制改为十六进制
  386. }
  387. $pattern = '/style=(\'|\")(.*)(\'|\")/i';
  388. $text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
  389. return "<a $text>";
  390. unset($color);//卸载color
  391. }
  392. add_filter('wp_tag_cloud', 'colorCloud', 1);
  393. //自动生成版权时间
  394. function comicpress_copyright() {
  395. global $wpdb;
  396. $copyright_dates = $wpdb->get_results("
  397. SELECT
  398. YEAR(min(post_date_gmt)) AS firstdate,
  399. YEAR(max(post_date_gmt)) AS lastdate
  400. FROM
  401. $wpdb->posts
  402. WHERE
  403. post_status = 'publish'
  404. ");
  405. $output = '';
  406. if($copyright_dates) {
  407. $copyright = "&copy; " . $copyright_dates[0]->firstdate;
  408. if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
  409. $copyright .= '-' . $copyright_dates[0]->lastdate;
  410. }
  411. $output = $copyright;
  412. }
  413. return $output;
  414. }
  415. //评论邮件通知
  416. function comment_mail_notify($comment_id) {
  417. $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
  418. $comment = get_comment($comment_id);
  419. $comment_author_email = trim($comment->comment_author_email);
  420. $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  421. $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  422. $spam_confirmed = $comment->comment_approved;
  423. if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
  424. $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
  425. $subject = '您在 [' . get_option("blogname") . '] 的评论有新的回复';
  426. $message = '
  427. <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
  428. <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  429. <p>您曾在 [' . get_option("blogname") . '] 的文章 ' . get_the_title($comment->comment_post_ID) . ' 上发表评论:<br />'
  430. . nl2br(get_comment($parent_id)->comment_content) . '</p>
  431. <p>' . trim($comment->comment_author) . ' 给您的回复如下:<br />'
  432. . nl2br($comment->comment_content) . '<br /></p>
  433. <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复的完整內容</a></p>
  434. <p>欢迎再次光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
  435. <p>(此郵件由系統自動發出, 請勿回覆.)</p>
  436. </div>';
  437. $message = convert_smilies($message);
  438. $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  439. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  440. wp_mail( $to, $subject, $message, $headers );
  441. //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  442. }
  443. }
  444. add_action('comment_post', 'comment_mail_notify');
  445. //全部设置结束
  446. ?>