/wp-content/themes/hicms/functions.php
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
- <?php
- include("includes/theme_options.php");
- if (function_exists('register_sidebar'))
- {
- register_sidebar(array(
- 'name' => '小工具1',
- 'before_widget' => '<div class="r_widget">',
- 'after_widget' => '',
- 'before_title' => '<h3>',
- 'after_title' => '</h3>',
- 'after_widget' => '</div>',
- ));
- }
- {
- register_sidebar(array(
- 'name' => '小工具2',
- 'before_widget' => '<div class="r_widget">',
- 'after_widget' => '',
- 'before_title' => '<h3>',
- 'after_title' => '</h3>',
- 'after_widget' => '</div>',
- ));
- }
- {
- register_sidebar(array(
- 'name' => '小工具3',
- 'before_widget' => '<div class="r_widget">',
- 'after_widget' => '',
- 'before_title' => '<h3>',
- 'after_title' => '</h3>',
- 'after_widget' => '</div>',
- ));
- }
-
- if ( function_exists('register_nav_menus') ) {
- register_nav_menus(array(
- 'primary' => '导航菜单'
- ));
- }
- /* 访问计数 */
- function record_visitors()
-
- {
-
- if (is_singular())
-
- {
-
- global $post;
-
- $post_ID = $post->ID;
-
- if($post_ID)
-
- {
-
- $post_views = (int)get_post_meta($post_ID, 'views', true);
-
- if(!update_post_meta($post_ID, 'views', ($post_views+1)))
-
- {
-
- add_post_meta($post_ID, 'views', 1, true);
-
- }
-
- }
-
- }
-
- }
-
- add_action('wp_head', 'record_visitors');
-
-
-
-
- /// 函数名称:post_views
-
- /// 函数作用:取得文章的阅读次数
-
- function post_views($before = '(点击 ', $after = '次)', $echo = 1)
-
- {
-
- global $post;
-
- $post_ID = $post->ID;
-
- $views = (int)get_post_meta($post_ID,'views', true);
-
- if ($echo) echo $before, number_format($views), $after;
-
- else return $views;
-
- }
-
- // 获得热评文章
- function simple_get_most_viewed($posts_num=10, $days=90){
- global $wpdb;
- $sql = "SELECT ID , post_title , comment_count
- FROM $wpdb->posts
- WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days
- AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit')
- ORDER BY comment_count DESC LIMIT 0 , $posts_num ";
- $posts = $wpdb->get_results($sql);
- $output = "";
- foreach ($posts as $post){
- $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>";
- }
- echo $output;
- }
- //标题文字截断
- function cut_str($src_str,$cut_length)
- {
- $return_str='';
- $i=0;
- $n=0;
- $str_length=strlen($src_str);
- while (($n<$cut_length) && ($i<=$str_length))
- {
- $tmp_str=substr($src_str,$i,1);
- $ascnum=ord($tmp_str);
- if ($ascnum>=224)
- {
- $return_str=$return_str.substr($src_str,$i,3);
- $i=$i+3;
- $n=$n+2;
- }
- elseif ($ascnum>=192)
- {
- $return_str=$return_str.substr($src_str,$i,2);
- $i=$i+2;
- $n=$n+2;
- }
- elseif ($ascnum>=65 && $ascnum<=90)
- {
- $return_str=$return_str.substr($src_str,$i,1);
- $i=$i+1;
- $n=$n+2;
- }
- else
- {
- $return_str=$return_str.substr($src_str,$i,1);
- $i=$i+1;
- $n=$n+1;
- }
- }
- if ($i<$str_length)
- {
- $return_str = $return_str . '';
- }
- if (get_post_status() == 'private')
- {
- $return_str = $return_str . '(private)';
- }
- return $return_str;
- }
-
- //分页
- function pagination($query_string){
- global $posts_per_page, $paged;
- $my_query = new WP_Query($query_string ."&posts_per_page=-1");
- $total_posts = $my_query->post_count;
- if(empty($paged))$paged = 1;
- $prev = $paged - 1;
- $next = $paged + 1;
- $range = 5; // 修改数字,可以显示更多的分页链接
- $showitems = ($range * 2)+1;
- $pages = ceil($total_posts/$posts_per_page);
- if(1 != $pages){
- echo "<div class='pagination'>";
- echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<a href='".get_pagenum_link(1)."' class='fir_las'>最前</a>":"";
- echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."' class='page_previous'>« 上一页</a>":"";
- for ($i=1; $i <= $pages; $i++){
- if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
- echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
- }
- }
- echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."' class='page_next'>下一页 »</a>" :"";
- echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."' class='fir_las'>最后</a>":"";
- echo "</div>\n";
- }
- }
-
- //日志归档
- class hacklog_archives
- {
- function GetPosts()
- {
- global $wpdb;
- if ( $posts = wp_cache_get( 'posts', 'ihacklog-clean-archives' ) )
- return $posts;
- $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'";
- $rawposts =$wpdb->get_results( $query, OBJECT );
- foreach( $rawposts as $key => $post ) {
- $posts[ mysql2date( 'Y.m', $post->post_date ) ][] = $post;
- $rawposts[$key] = null;
- }
- $rawposts = null;
- wp_cache_set( 'posts', $posts, 'ihacklog-clean-archives' );;
- return $posts;
- }
- function PostList( $atts = array() )
- {
- global $wp_locale;
- global $hacklog_clean_archives_config;
- $atts = shortcode_atts(array(
- 'usejs' => $hacklog_clean_archives_config['usejs'],
- 'monthorder' => $hacklog_clean_archives_config['monthorder'],
- 'postorder' => $hacklog_clean_archives_config['postorder'],
- 'postcount' => '1',
- 'commentcount' => '1',
- ), $atts);
- $atts=array_merge(array('usejs'=>1,'monthorder' =>'new','postorder' =>'new'),$atts);
- $posts = $this->GetPosts();
- ( 'new' == $atts['monthorder'] ) ? krsort( $posts ) : ksort( $posts );
- foreach( $posts as $key => $month ) {
- $sorter = array();
- foreach ( $month as $post )
- $sorter[] = $post->post_date_gmt;
- $sortorder = ( 'new' == $atts['postorder'] ) ? SORT_DESC : SORT_ASC;
- array_multisort( $sorter, $sortorder, $month );
- $posts[$key] = $month;
- unset($month);
- }
- $html = '<div class="car-container';
- if ( 1 == $atts['usejs'] ) $html .= ' car-collapse';
- $html .= '">'. "\n";
- if ( 1 == $atts['usejs'] ) $html .= '<a href="#" class="car-toggler">展开所有月份'."</a>\n\n";
- $html .= '<ul class="car-list">' . "\n";
- $firstmonth = TRUE;
- foreach( $posts as $yearmonth => $posts ) {
- list( $year, $month ) = explode( '.', $yearmonth );
- $firstpost = TRUE;
- foreach( $posts as $post ) {
- if ( TRUE == $firstpost ) {
- $spchar = $firstmonth ? '<span class="car-toggle-icon car-minus">-</span>' : '<span class="car-toggle-icon car-plus">+</span>';
- $html .= ' <li><span class="car-yearmonth" style="cursor:pointer;">'.$spchar.' ' . sprintf( __('%1$s %2$d'), $wp_locale->get_month($month), $year );
- if ( '0' != $atts['postcount'] )
- {
- $html .= ' <span title="文章数量">(共' . count($posts) . '篇文章)</span>';
- }
- if ($firstmonth == FALSE) {
- $html .= "</span>\n <ul class='car-monthlisting' style='display:none;'>\n";
- } else {
- $html .= "</span>\n <ul class='car-monthlisting'>\n";
- }
- $firstpost = FALSE;
- $firstmonth = FALSE;
- }
- $html .= ' <li>' . mysql2date( 'd', $post->post_date ) . '日: <a target="_blank" href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a>';
- if ( '0' != $atts['commentcount'] && ( 0 != $post->comment_count || 'closed' != $post->comment_status ) && empty($post->post_password) )
- $html .= ' <span title="评论数量">(' . $post->comment_count . '条评论)</span>';
- $html .= "</li>\n";
- }
- $html .= " </ul>\n </li>\n";
- }
- $html .= "</ul>\n</div>\n";
- return $html;
- }
- function PostCount()
- {
- $num_posts = wp_count_posts( 'post' );
- return number_format_i18n( $num_posts->publish );
- }
- }
- if(!empty($post->post_content))
- {
- $all_config=explode(';',$post->post_content);
- foreach($all_config as $item)
- {
- $temp=explode('=',$item);
- $hacklog_clean_archives_config[trim($temp[0])]=htmlspecialchars(strip_tags(trim($temp[1])));
- }
- }
- else
- {
- $hacklog_clean_archives_config=array('usejs'=>1,'monthorder' =>'new','postorder' =>'new');
- }
- $hacklog_archives=new hacklog_archives();
-
- //==========
- function wp_icpsysterm(){
- $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;
- }
-
- //密码保护提示
- function password_hint( $c ){
- global $post, $user_ID, $user_identity;
- if ( empty($post->post_password) )
- return $c;
- if ( isset($_COOKIE['wp-postpass_'.COOKIEHASH]) && stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) == $post->post_password )
- return $c;
- if($hint = get_post_meta($post->ID, 'password_hint', true)){
- $url = get_option('siteurl').'/wp-pass.php';
- if($hint)
- $hint = '密码提示:'.$hint;
- else
- $hint = "请输入您的密码";
- if($user_ID)
- $hint .= sprintf('欢迎进入,您的密码是:', $user_identity, $post->post_password);
- $out = <<<END
- <form method="post" action="$url">
- <p>这篇文章是受保护的文章,请输入密码继续阅读:</p>
- <div>
- <label>$hint<br/>
- <input type="password" name="post_password"/></label>
- <input type="submit" value="输入密码" name="Submit"/>
- </div>
- </form>
- END;
- return $out;
- }else{
- return $c;
- }
- }
- add_filter('the_content', 'password_hint');
- //============
- function wp_protect(){
- echo'<script type="text/javascript">alert("亲,保留个版权塞!")</script>';
- }
- //支持外链缩略图
- if ( function_exists('add_theme_support') )
- add_theme_support('post-thumbnails');
- function catch_first_image() {
- global $post, $posts;
- $first_img = '';
- ob_start();
- ob_end_clean();
- $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
- $first_img = $matches [1] [0];
- if(empty($first_img)){
- $random = mt_rand(1, 20);
- echo get_bloginfo ( 'stylesheet_directory' );
- echo '/images/random/tb'.$random.'.jpg';
- }
- return $first_img;
- }
-
- //自定义头像
- add_filter( 'avatar_defaults', 'fb_addgravatar' );
- function fb_addgravatar( $avatar_defaults ) {
- $myavatar = get_bloginfo('template_directory') . '/images/gravatar.png';
- $avatar_defaults[$myavatar] = '自定义头像';
- return $avatar_defaults;
- }
-
- // 评论回复/头像缓存
- function weisay_comment($comment, $args, $depth) {
- $GLOBALS['comment'] = $comment;
- global $commentcount,$wpdb, $post;
- if(!$commentcount) { //初始化楼层计数器
- $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");
- $cnt = count($comments);//获取主评论总数量
- $page = get_query_var('cpage');//获取当前评论列表页码
- $cpp=get_option('comments_per_page');//获取每页评论显示数量
- if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
- $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数
- } else {
- $commentcount = $cpp * $page + 1;
- }
- }
- ?>
- <li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
- <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
- <?php $add_below = 'div-comment'; ?>
- <div class="comment-author vcard"><?php if (get_option('swt_type') == 'Display') { ?>
- <?php
- $p = 'avatar/';
- $f = md5(strtolower($comment->comment_author_email));
- $a = $p . $f .'.jpg';
- $e = ABSPATH . $a;
- if (!is_file($e)){ //当头像不存在就更新
- $d = get_bloginfo('wpurl'). '/avatar/default.jpg';
- $s = '40'; //头像大小 自行根据自己模板设置
- $r = get_option('avatar_rating');
- $g = 'http://www.gravatar.com/avatar/'.$f.'.jpg?s='.$s.'&d='.$d.'&r='.$r;
- $avatarContent = file_get_contents($g);
- file_put_contents($e, $avatarContent);
- if ( filesize($e) == 0 ){ copy($d, $e); }
- };
- ?>
- <img src='<?php bloginfo('wpurl'); ?>/<?php echo $a ?>' alt='' class='avatar' />
- <?php { echo ''; } ?>
- <?php } else { include(TEMPLATEPATH . '/comment_gravatar.php'); } ?>
- <div class="floor"><?php
- if(!$parent_id = $comment->comment_parent){
- switch ($commentcount){
- case 2 :echo "沙发";--$commentcount;break;
- case 3 :echo "板凳";--$commentcount;break;
- case 4 :echo "地板";--$commentcount;break;
- default:printf('%1$s楼', --$commentcount);
- }
- }
- ?>
- </div><strong><?php comment_author_link() ?></strong>:<?php edit_comment_link('编辑',' ',''); ?></div>
- <?php if ( $comment->comment_approved == '0' ) : ?>
- <span style="color:#C00; font-style:inherit">您的评论正在等待审核中...</span>
- <br />
- <?php endif; ?>
- <?php comment_text() ?>
-
- <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>
- </div>
- <?php
- }
- function weisay_end_comment() {
- echo '</li>';
- }
-
- //登陆显示头像
- function weisay_get_avatar($email, $size = 48){
- return get_avatar($email, $size);
- }
- //彩色标签云
- function colorCloud($text) {
- $text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
- return $text;
- }
- function colorCloudCallback($matches) {
- $text = $matches[1];
- for($a=0;$a<6;$a++){ //采用#ffffff方法
- $color.=dechex(rand(0,15));//累加随机的数据--dechex()将十进制改为十六进制
- }
- $pattern = '/style=(\'|\")(.*)(\'|\")/i';
- $text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
- return "<a $text>";
- unset($color);//卸载color
- }
- add_filter('wp_tag_cloud', 'colorCloud', 1);
-
- //自动生成版权时间
- function comicpress_copyright() {
- global $wpdb;
- $copyright_dates = $wpdb->get_results("
- SELECT
- YEAR(min(post_date_gmt)) AS firstdate,
- YEAR(max(post_date_gmt)) AS lastdate
- FROM
- $wpdb->posts
- WHERE
- post_status = 'publish'
- ");
- $output = '';
- if($copyright_dates) {
- $copyright = "© " . $copyright_dates[0]->firstdate;
- if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
- $copyright .= '-' . $copyright_dates[0]->lastdate;
- }
- $output = $copyright;
- }
- return $output;
- }
-
- //评论邮件通知
- function comment_mail_notify($comment_id) {
- $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
- $comment = get_comment($comment_id);
- $comment_author_email = trim($comment->comment_author_email);
- $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
- $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
- $spam_confirmed = $comment->comment_approved;
- if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
- $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
- $subject = '您在 [' . get_option("blogname") . '] 的评论有新的回复';
- $message = '
- <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;">
- <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
- <p>您曾在 [' . get_option("blogname") . '] 的文章 《' . get_the_title($comment->comment_post_ID) . '》 上发表评论:<br />'
- . nl2br(get_comment($parent_id)->comment_content) . '</p>
- <p>' . trim($comment->comment_author) . ' 给您的回复如下:<br />'
- . nl2br($comment->comment_content) . '<br /></p>
- <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复的完整內容</a></p>
- <p>欢迎再次光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
- <p>(此郵件由系統自動發出, 請勿回覆.)</p>
- </div>';
- $message = convert_smilies($message);
- $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
- $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
- wp_mail( $to, $subject, $message, $headers );
- //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
- }
- }
- add_action('comment_post', 'comment_mail_notify');
- //全部设置结束
- ?>