<?php
/***********编辑文章页面**********/
if ( is_user_logged_in() ) {
	global $wpdb, $userdata,$ashu_option,$wp_query,$wp_rewrite;
	$post_id = isset( $wp_query->query_vars['post_id'] ) ? $wp_query->query_vars['post_id'] : 0; //获取文章ID
	//设置选项，是否允许再次编辑
	if ( $ashu_option['user']['enable_post_edit'] != 'yes' ) {
		$errors[] = '对不起，删除文章功能未启用';
	}
	$curpost = get_post( $post_id ); //根据ID获取文章
	if ( !$curpost ) {
		$errors[] = '无效的文章';
	}
	//判断权限
	if ( !current_user_can( 'delete_others_posts' ) && ( $userdata->ID != $curpost->post_author ) ) {
		$errors[] = '对不起，你没有编辑这篇文章的权限';
	}
	
	//输出错误信息
    if ( $errors ) {
		echo user_error_msg( $errors );
	}else{
	
	//删除附件
	if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "del" ) {
		check_admin_referer( 'user_attach_del' );
		$attach_id = intval( $_REQUEST['attach_id'] );
		if ( $attach_id ) {
			wp_delete_attachment( $attach_id ); //删除附件
		}
	}
	//处理文章数据
	if ( isset( $_POST['user_post_new_submit'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'user-edit-post' ) ) {
		global $userdata,$ashu_option;
        $errors = array();
        
		$title = trim( $_POST['user_post_title'] ); //标题
        $content = trim( $_POST['user_post_content'] ); //内容

        $tags = '';
        $cat = '';
        if ( isset( $_POST['user_post_tags'] ) ) {
            $tags = user_clean_tags( $_POST['user_post_tags'] ); //处理标签
        }

        //是否有附件
        if ( !empty( $_FILES['user_post_attachments'] ) ) {
            $errors = user_check_upload(); //外部函数，检查附件是否有错误
        }

        if ( empty( $title ) ) {
            $errors[] = '标题不能为空';
        } else {
            $title = trim( strip_tags( $title ) );
        }

        if ( empty( $content ) ) {
            $errors[] = '文章内容不能为空';
        } else {
            $content = trim( $content );
        }

        if ( !empty( $tags ) ) {
            $tags = explode( ',', $tags );
        }
		
		//$attach_id = isset( $_POST['user_featured_img'] ) ? intval( $_POST['user_featured_img'] ) : 0;

        //$errors = apply_filters( 'user_edit_post_validation', $errors );

        if ( !$errors ) {

            //是否允许选择分类
            if ( user_get_option( 'allow_cats' ) == 'on' ) {
                $post_category = $_POST['category'];
            } else {
                $post_category = array(get_option( 'user_default_cat' )); //设置选项-默认分类
            }

            $post_update = array(
                'ID' => trim( $_POST['post_id'] ),
                'post_title' => $title,
                'post_content' => $content,
                'post_category' => $post_category,
                'tags_input' => $tags
            );
			//更新文章
            $post_id = wp_update_post( $post_update );

            if ( $post_id ) {
                echo '<div class="success">文章更新成功</div>';
            }
        } else {
			//输出错误
            echo user_error_msg( $errors );
        }
	}
	
	//编辑文章表单
	$post_tags = wp_get_post_tags( $curpost->ID ); //获取文章标签
        $tagsarray = array();
        foreach ($post_tags as $tag) {
            $tagsarray[] = $tag->name;
        }
        $tagslist = implode( ', ', $tagsarray );
        $categories = get_the_category( $curpost->ID ); //获取分类
        $featured_image = $ashu_option['user']['enable_featured_image'];//获取设置选项，是否启用特色图像
        ?>
        <div id="user-post-area">
            <form name="user_edit_post_form" id="user_edit_post_form" action="" enctype="multipart/form-data" method="POST">
                <?php wp_nonce_field( 'user-edit-post' ); //输出安全密匙 ?>
                <ul class="user-post-form">

                    <?php if ( $featured_image == 'yes' ) { ?>
                            <li>
                                <label for="post-thumbnail"><?php echo user_get_option( 'ft_image_label' ); ?></label>
                                <div id="user-ft-upload-container">
                                    <div id="user-ft-upload-filelist">
                                        <?php
                                        $style = '';
                                        if ( has_post_thumbnail( $curpost->ID ) ) {
                                            $style = ' style="display:none"';

                                            $post_thumbnail_id = get_post_thumbnail_id( $curpost->ID );
                                            echo user_feat_img_html( $post_thumbnail_id );//外部函数输出特色图像
                                        }
                                        ?>
                                    </div>
                                    <a id="user-ft-upload-pickfiles" class="button" href="#">上传</a>
                                </div>
                                <div class="clear"></div>
                            </li>
                    <?php } ?>

                    <li>
                        <label for="new-post-title">
                            标题 <span class="required">*</span>
                        </label>
                        <input type="text" name="user_post_title" id="new-post-title" minlength="2" value="<?php echo esc_html( $curpost->post_title ); ?>">
                        <div class="clear"></div>
                    </li>
                    <li>
                        <label for="new-post-desc">
                           内容<span class="required">*</span>
                        </label>
                            <textarea name="user_post_content" class="requiredField" id="new-post-desc" cols="60" rows="8"><?php echo esc_textarea( $curpost->post_content ); ?></textarea>
                        <div class="clear"></div>
                        <p class="description">帮助信息</p>
                    </li>
                     <?php
                        if ( $ashu_option['user']['allow_tags'] == 'on' ) { //获取设置选项-是否启用标签
                       ?>
                        <li>
                            <label for="new-post-tags">
                                标签
                            </label>
                            <input type="text" name="user_post_tags" id="new-post-tags" value="<?php echo $tagslist; ?>">
                            <p class="description">多个标签请用逗号隔开</p>
                            <div class="clear"></div>
                        </li>
                    <?php } ?>
                    <li>
                        <label>&nbsp;</label>
                        <input class="user_submit" type="submit" name="user_edit_post_submit" value="提交">
                        <input type="hidden" name="user_edit_post_submit" value="yes" />
                        <input type="hidden" name="post_id" value="<?php echo $curpost->ID; ?>">
                    </li>
                </ul>
            </form>
        </div>
        <?php
	}
} else {
	printf( "对不起，没有浏览权限，请 %s 再试", wp_loginout( '', false ) );
}<?php
/***********编辑文章页面**********/
if ( is_user_logged_in() ) {
	global $wpdb, $userdata,$ashu_option,$wp_query,$wp_rewrite;
	$post_id = isset( $wp_query->query_vars['post_id'] ) ? $wp_query->query_vars['post_id'] : 0; //获取文章ID
	//设置选项，是否允许再次编辑
	if ( $ashu_option['user']['enable_post_edit'] != 'yes' ) {
		$errors[] = '对不起，删除文章功能未启用';
	}
	$curpost = get_post( $post_id ); //根据ID获取文章
	if ( !$curpost ) {
		$errors[] = '无效的文章';
	}
	//判断权限
	if ( !current_user_can( 'delete_others_posts' ) && ( $userdata->ID != $curpost->post_author ) ) {
		$errors[] = '对不起，你没有编辑这篇文章的权限';
	}
	
	//输出错误信息
    if ( $errors ) {
		echo user_error_msg( $errors );
	}else{
	
	//删除附件
	if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "del" ) {
		check_admin_referer( 'user_attach_del' );
		$attach_id = intval( $_REQUEST['attach_id'] );
		if ( $attach_id ) {
			wp_delete_attachment( $attach_id ); //删除附件
		}
	}
	//处理文章数据
	if ( isset( $_POST['user_post_new_submit'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'user-edit-post' ) ) {
		global $userdata,$ashu_option;
        $errors = array();
        
		$title = trim( $_POST['user_post_title'] ); //标题
        $content = trim( $_POST['user_post_content'] ); //内容

        $tags = '';
        $cat = '';
        if ( isset( $_POST['user_post_tags'] ) ) {
            $tags = user_clean_tags( $_POST['user_post_tags'] ); //处理标签
        }

        //是否有附件
        if ( !empty( $_FILES['user_post_attachments'] ) ) {
            $errors = user_check_upload(); //外部函数，检查附件是否有错误
        }

        if ( empty( $title ) ) {
            $errors[] = '标题不能为空';
        } else {
            $title = trim( strip_tags( $title ) );
        }

        if ( empty( $content ) ) {
            $errors[] = '文章内容不能为空';
        } else {
            $content = trim( $content );
        }

        if ( !empty( $tags ) ) {
            $tags = explode( ',', $tags );
        }
		
		//$attach_id = isset( $_POST['user_featured_img'] ) ? intval( $_POST['user_featured_img'] ) : 0;

        //$errors = apply_filters( 'user_edit_post_validation', $errors );

        if ( !$errors ) {

            //是否允许选择分类
            if ( user_get_option( 'allow_cats' ) == 'on' ) {
                $post_category = $_POST['category'];
            } else {
                $post_category = array(get_option( 'user_default_cat' )); //设置选项-默认分类
            }

            $post_update = array(
                'ID' => trim( $_POST['post_id'] ),
                'post_title' => $title,
                'post_content' => $content,
                'post_category' => $post_category,
                'tags_input' => $tags
            );
			//更新文章
            $post_id = wp_update_post( $post_update );

            if ( $post_id ) {
                echo '<div class="success">文章更新成功</div>';
            }
        } else {
			//输出错误
            echo user_error_msg( $errors );
        }
	}
	
	//编辑文章表单
	$post_tags = wp_get_post_tags( $curpost->ID ); //获取文章标签
        $tagsarray = array();
        foreach ($post_tags as $tag) {
            $tagsarray[] = $tag->name;
        }
        $tagslist = implode( ', ', $tagsarray );
        $categories = get_the_category( $curpost->ID ); //获取分类
        $featured_image = $ashu_option['user']['enable_featured_image'];//获取设置选项，是否启用特色图像
        ?>
        <div id="user-post-area">
            <form name="user_edit_post_form" id="user_edit_post_form" action="" enctype="multipart/form-data" method="POST">
                <?php wp_nonce_field( 'user-edit-post' ); //输出安全密匙 ?>
                <ul class="user-post-form">

                    <?php if ( $featured_image == 'yes' ) { ?>
                            <li>
                                <label for="post-thumbnail"><?php echo user_get_option( 'ft_image_label' ); ?></label>
                                <div id="user-ft-upload-container">
                                    <div id="user-ft-upload-filelist">
                                        <?php
                                        $style = '';
                                        if ( has_post_thumbnail( $curpost->ID ) ) {
                                            $style = ' style="display:none"';

                                            $post_thumbnail_id = get_post_thumbnail_id( $curpost->ID );
                                            echo user_feat_img_html( $post_thumbnail_id );//外部函数输出特色图像
                                        }
                                        ?>
                                    </div>
                                    <a id="user-ft-upload-pickfiles" class="button" href="#">上传</a>
                                </div>
                                <div class="clear"></div>
                            </li>
                    <?php } ?>

                    <li>
                        <label for="new-post-title">
                            标题 <span class="required">*</span>
                        </label>
                        <input type="text" name="user_post_title" id="new-post-title" minlength="2" value="<?php echo esc_html( $curpost->post_title ); ?>">
                        <div class="clear"></div>
                    </li>
                    <li>
                        <label for="new-post-desc">
                           内容<span class="required">*</span>
                        </label>
                            <textarea name="user_post_content" class="requiredField" id="new-post-desc" cols="60" rows="8"><?php echo esc_textarea( $curpost->post_content ); ?></textarea>
                        <div class="clear"></div>
                        <p class="description">帮助信息</p>
                    </li>
                     <?php
                        if ( $ashu_option['user']['allow_tags'] == 'on' ) { //获取设置选项-是否启用标签
                       ?>
                        <li>
                            <label for="new-post-tags">
                                标签
                            </label>
                            <input type="text" name="user_post_tags" id="new-post-tags" value="<?php echo $tagslist; ?>">
                            <p class="description">多个标签请用逗号隔开</p>
                            <div class="clear"></div>
                        </li>
                    <?php } ?>
                    <li>
                        <label>&nbsp;</label>
                        <input class="user_submit" type="submit" name="user_edit_post_submit" value="提交">
                        <input type="hidden" name="user_edit_post_submit" value="yes" />
                        <input type="hidden" name="post_id" value="<?php echo $curpost->ID; ?>">
                    </li>
                </ul>
            </form>
        </div>
        <?php
	}
} else {
	printf( "对不起，没有浏览权限，请 %s 再试", wp_loginout( '', false ) );
}