<?php
add_theme_support( 'custom-aboutus' );
add_theme_support( 'custom-news' );
add_theme_support( 'custom-newsDetail' );
add_theme_support( 'custom-join' );
add_theme_support( 'custom-joinIndex' );
add_theme_support( 'custom-product' );
add_theme_support( 'custom-school' );
add_theme_support( 'custom-schoolDetail' );
add_theme_support( 'custom-contact' );
add_theme_support( 'custom-jobs' );
 
// 自定义评论留言
function submit_custom_comment() {
	if (isset($_POST['comment_data'])) {
			parse_str($_POST['comment_data'], $commentData); // 解析评论数据
			// 构建评论数据数组
			$comment_args = array(
				'comment_post_ID'      => $commentData['comment_post_ID'],
                'comment_author_IP'    => $commentData['comment_author_IP'],
				'comment_author_url'   => $commentData['comment_author_url'],
				'comment_author'       => $commentData['comment_author'],
				'comment_author_email' => $commentData['comment_author_email'],
				'comment_content'      => $commentData['comment'],
				'comment_type'         => 'comment',
				'comment_meta'         => array(
					'phone'            => $commentData['comment_author_phone'], // 将自定义字段添加到评论元数据中
					'compname'         => $commentData['comment_compname'], // 将自定义字段添加到评论元数据中
                    'address'          => $commentData['comment_provinces'].$commentData['comment_city'].$commentData['comment_area'].' '.$commentData['comment_address'], // 将自定义字段添加到评论元数据中
				)
			);
			// 插入评论
			$comment_id = wp_insert_comment($comment_args);
			if ($comment_id !== 0) {
                echo json_encode(['status' => 'success', 'comment_id' => $comment_id]);
            } else {
                echo json_encode(['status' => 'error', 'message' => 'Failed to submit comment.']);
            }
	}
	die(); // 必须终止脚本
}
add_action('wp_ajax_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理登录用户的请求
add_action('wp_ajax_nopriv_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理非登录用户的请求

// 添加后台评论处显示自定义字段代码
add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 3 );
 
function my_comments_columns( $columns ){
   $columns[ 'phone' ] = __( '电话' ); 
   $columns[ 'compname' ] = __( '主营项目' );
   $columns[ 'address' ] = __( '地址' ); 
    return $columns;
}
 
function output_my_comments_columns( $column_name, $comment_id ){
    switch( $column_name ) {
	case "phone" :
	echo get_comment_meta( $comment_id, 'phone', true );
	break;
	case "compname" :
	echo get_comment_meta( $comment_id, 'compname', true );
    break;
    case "address" :
    echo get_comment_meta( $comment_id, 'address', true );
	break;
	}
}

// 在文章被创建时设置初始浏览计数
add_action('draft_to_publish', 'set_post_views_on_publish');
add_action('save_post', 'set_post_views_on_publish');
function set_post_views_on_publish($post_ID) {
    if (!isset($_POST['post_ID'])) {
        return;
    }
    $post_ID = $_POST['post_ID'];
    if (!isset($_POST['post_status']) || 'publish' !== $_POST['post_status']) {
        return;
    }
    $count_key = 'views';
    $count = (int) get_post_meta($post_ID, $count_key, true);
    if (!$count) {
        delete_post_meta($post_ID, $count_key);
        add_post_meta($post_ID, $count_key, '0');
    }
}

// 添加封面图片选项到文章编辑页面
function add_featured_image_field_to_edit_page() {
    add_meta_box('featured_image_meta_box', '设置封面图片', 'render_featured_image_meta_box', 'post', 'normal', 'high');
}
add_action('add_meta_boxes', 'add_featured_image_field_to_edit_page');
 
// 渲染封面图片选项界面
function render_featured_image_meta_box($post) {
    // 非必要代码，用于在编辑页面显示提示信息
    echo '<p>请上传作为文章封面的图片：</p>';
    // 创建图片上传表单
    wp_nonce_field('featured_image_nonce', 'featured_image_nonce_field');
    ?>
    <input type="hidden" name="featured_image_id" value="<?php echo get_post_thumbnail_id($post->ID) ?: ''; ?>">
    <input type="file" name="featured_image">
    <?php
}
 
// 保存封面图片选项
function save_featured_image_meta($post_id) {
    if (!isset($_POST['featured_image_nonce_field']) || !wp_verify_nonce($_POST['featured_image_nonce_field'], 'featured_image_nonce')) {
        return;
    }
 
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
 
    if (isset($_FILES['featured_image']) && !empty($_FILES['featured_image']['name'])) {
        $attachment_id = media_handle_upload('featured_image', $post_id);
        if ($attachment_id) {
            update_post_meta($post_id, '_thumbnail_id', $attachment_id);
        }
    } elseif (isset($_POST['featured_image_id']) && !empty($_POST['featured_image_id'])) {
        update_post_meta($post_id, '_thumbnail_id', $_POST['featured_image_id']);
    }
}
add_action('save_post', 'save_featured_image_meta');

add_theme_support('post-thumbnails');

/**
* 数字分页函数
* 因为wordpress默认仅仅提供简单分页
* 所以要实现数字分页，需要自定义函数
* @Param int $range            数字分页的宽度
* @Return string|empty        输出分页的HTML代码        
*/
function lingfeng_pagenavi( $range = 4 ) {
    global $paged,$query;
    $max_page = 0;
    if ( !$max_page ) {
        $max_page = $query->max_num_pages;
    }
    if( $max_page >1 ) {
        echo "<div class='fenye'>"; 
        echo '<span>共'.$max_page.'页</span>';
        if( !$paged ){
            $paged = 1;
        }
        if( $paged != 1 ) {
            echo "<a href='".get_pagenum_link(1) ."' class='extend' title='跳转到首页'>首页</a>";
        }

        previous_posts_link('上一页');

        if ( $max_page >$range ) {
            if( $paged <$range ) {
                for( $i = 1; $i <= ($range +1); $i++ ) {
                    echo "<a href='".get_pagenum_link($i) ."'";
                if($i==$paged) echo " class='current'";echo ">$i</a>";
                }
            }elseif($paged >= ($max_page -ceil(($range/2)))){
                for($i = $max_page -$range;$i <= $max_page;$i++){
                    echo "<a href='".get_pagenum_link($i) ."'";
                    if($i==$paged)echo " class='current'";echo ">$i</a>";
                    }
            }elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
                    for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
                        echo "<a href='".get_pagenum_link($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a>";
                    }
                }
        }else{
                for($i = 1;$i <= $max_page;$i++){
                    echo "<a href='".get_pagenum_link($i) ."'";
                    if($i==$paged)echo " class='current'";echo ">$i</a>";
                }
            }

        next_posts_link('下一页',$max_page);

        if($paged != $max_page){
            echo "<a href='".get_pagenum_link($max_page) ."' class='extend' title='跳转到最后一页'>尾页</a>";
        }
        echo "</div>\n";  
    }
}
?>