diff --git a/css/nav.css b/css/nav.css index 7f0a793..e7575ab 100644 --- a/css/nav.css +++ b/css/nav.css @@ -282,9 +282,12 @@ input[type="button"],input[type="submit"]{background-color:#222;border:0;-webkit /* --- tougao --- */ .show_ico{float:left;width:64px;height:64px;line-height:60px;padding:2px;cursor:pointer} +.show_ico .remove{position:absolute;cursor:pointer;top:1px;right:1px;font-size:20px;z-index:1;display:block;width:24px;height:24px;line-height:24px;background:rgba(34,34,34,.5);border-radius:50%} +.wechat .show_ico .remove{font-size:25px;width:29px;height:29px;line-height:29px} +.show_ico .remove:hover{color:#f1404b} .upload_img{position:relative;display:table;color:white;background-color:rgba(136,136,136,.1);overflow:hidden;cursor:pointer;text-align:center;vertical-align:center;border-radius:5px;transition:.3s} .wechat .show_ico{width:132px;height:132px;line-height:128px} -.upload_img:hover{background:#222324} +.upload_img:not(.disabled):hover{background:#222324} .upload_img input{position:absolute;cursor:pointer;opacity:0;left:0;right:0;top:0;bottom:0} .postform{margin:5px} .form-control{border-color:#bbb;padding:5px 10px;border-radius:3px} diff --git a/inc/ajax.php b/inc/ajax.php new file mode 100644 index 0000000..ff54273 --- /dev/null +++ b/inc/ajax.php @@ -0,0 +1,127 @@ + $wp_upload_dir['url'] . '/' . $dataname, // 外部链接的 url + 'post_mime_type' => $file['type'], // 文件 mime 类型 + 'post_title' => preg_replace( '/\.[^.]+$/', '', $basename ), // 附件标题,采用去除扩展名之后的文件名 + 'post_content' => '', // 文章内容,留空 + 'post_status' => 'inherit' + ); + $attach_id = wp_insert_attachment( $attachment, $filename ); // 插入附件信息 + if($attach_id != 0){ + require_once( ABSPATH . 'wp-admin/includes/image.php' ); // 确保包含此文件,因为wp_generate_attachment_metadata()依赖于此文件。 + $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); + wp_update_attachment_metadata( $attach_id, $attach_data ); // 生成附件的元数据,并更新数据库记录。 + print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>$basename)))); + exit(); + }else{ + echo '{"status":4,"msg":"图片上传失败!"}'; + exit(); + } + } +} + +//删除图片 +add_action('wp_ajax_nopriv_img_remove', 'io_img_remove'); +add_action('wp_ajax_img_remove', 'io_img_remove'); +function io_img_remove(){ + $attach_id = $_POST["id"]; + if( empty($attach_id) ){ + echo '{"status":3,"msg":"没有上传图像!"}'; + exit; + } + if ( false === wp_delete_attachment( $attach_id ) ) + echo '{"status":4,"msg":"图片 '.$attach_id.' 删除失败!"}'; + else + echo '{"status":1,"msg":"删除成功!"}'; + exit; +} + +//提交文章 +add_action('wp_ajax_nopriv_contribute_post', 'io_contribute'); +add_action('wp_ajax_contribute_post', 'io_contribute'); +function io_contribute(){ + $delay = 40; + if( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < $delay ){ + error('{"status":2,"msg":"您投稿也太勤快了吧,'. ($delay - ( time() - $_COOKIE["tougao"] )).'秒后再试!"}'); + } + + //表单变量初始化 + $sites_link = isset( $_POST['tougao_sites_link'] ) ? trim(htmlspecialchars($_POST['tougao_sites_link'], ENT_QUOTES)) : ''; + $sites_sescribe = isset( $_POST['tougao_sites_sescribe'] ) ? trim(htmlspecialchars($_POST['tougao_sites_sescribe'], ENT_QUOTES)) : ''; + $title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : ''; + $category = isset( $_POST['tougao_cat'] ) ? $_POST['tougao_cat'] : '0'; + $sites_ico = isset( $_POST['tougao_sites_ico'] ) ? trim(htmlspecialchars($_POST['tougao_sites_ico'], ENT_QUOTES)) : ''; + $wechat_qr = isset( $_POST['tougao_wechat_qr'] ) ? trim(htmlspecialchars($_POST['tougao_wechat_qr'], ENT_QUOTES)) : ''; + $content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : ''; + + // 表单项数据验证 + if ( $category == "0" ){ + error('{"status":4,"msg":"请选择分类。"}'); + } + if ( !empty(get_term_children($category, 'favorites'))){ + error('{"status":4,"msg":"不能选用父级分类目录。"}'); + } + if ( empty($sites_sescribe) || mb_strlen($sites_sescribe) > 50 ) { + error('{"status":4,"msg":"网站描叙必须填写,且长度不得超过50字。"}'); + } + if ( empty($sites_link) && empty($wechat_qr) ){ + error('{"status":3,"msg":"网站链接和公众号二维码至少填一项。"}'); + } + elseif ( !empty($sites_link) && !preg_match('/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $sites_link)) { + error('{"status":4,"msg":"网站链接必须符合URL格式。"}'); + } + if ( empty($title) || mb_strlen($title) > 30 ) { + error('{"status":4,"msg":"网站名称必须填写,且长度不得超过30字。"}'); + } + //if ( empty($content) || mb_strlen($content) > 10000 || mb_strlen($content) < 6) { + // error('{"status":4,"msg":"内容必须填写,且长度不得超过10000字,不得少于6字。"}'); + //} + + $tougao = array( + 'comment_status' => 'closed', + 'ping_status' => 'closed', + //'post_author' => 1,//用于投稿的用户ID + 'post_title' => $title, + 'post_content' => $content, + 'post_status' => 'pending', + 'post_type' => 'sites', + //'tax_input' => array( 'favorites' => array($category) ) //游客不可用 + ); + + // 将文章插入数据库 + $status = wp_insert_post( $tougao ); + if ($status != 0){ + global $wpdb; + add_post_meta($status, '_sites_sescribe', $sites_sescribe); + add_post_meta($status, '_sites_link', $sites_link); + add_post_meta($status, '_sites_order', '0'); + if( !empty($sites_ico)) + add_post_meta($status, '_thumbnail', $sites_ico); + if( !empty($wechat_qr)) + add_post_meta($status, '_wechat_qr', $wechat_qr); + wp_set_post_terms( $status, array($category), 'favorites'); //设置文章分类 + setcookie("tougao", time(), time()+$delay+10); + error('{"status":1,"msg":"投稿成功!"}'); + }else{ + error('{"status":4,"msg":"投稿失败!"}'); + } +} +function error($ErrMsg) { + echo $ErrMsg; + exit; +} diff --git a/inc/contribute-ajax.php b/inc/contribute-ajax.php index a6045cc..8d617c3 100644 --- a/inc/contribute-ajax.php +++ b/inc/contribute-ajax.php @@ -1,5 +1,7 @@ 1,'msg'=>'图片添加成功','data'=>array('src'=>wp_get_attachment_url( $attach_id ),'title'=>time())))); + print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>time())))); exit(); }else{ echo '{"status":4,"msg":"图片上传失败!"}'; exit(); } -} \ No newline at end of file +} diff --git a/inc/inc.php b/inc/inc.php index f38f029..43d704c 100644 --- a/inc/inc.php +++ b/inc/inc.php @@ -13,6 +13,7 @@ require_once get_theme_file_path() .'/inc/post-type.php'; require_once get_theme_file_path() .'/inc/fav-content.php'; require_once get_theme_file_path() .'/inc/meta-boxes.php'; +require_once get_theme_file_path() .'/inc/ajax.php'; // 禁用版本修订 diff --git a/inc/register.php b/inc/register.php index 6f96ed6..6f25ace 100644 --- a/inc/register.php +++ b/inc/register.php @@ -32,8 +32,9 @@ function theme_load_scripts() { wp_enqueue_script( 'comment-reply' ); } } - wp_localize_script('app', 'Theme' , array( + wp_localize_script('appjs', 'theme' , array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), + 'addico' => get_template_directory_uri() . '/images/add.png', )); } add_action('wp_enqueue_scripts', 'theme_load_scripts'); diff --git a/style.css b/style.css index 420e8a4..9ada080 100644 --- a/style.css +++ b/style.css @@ -2,7 +2,7 @@ Theme Name:WebStack Theme URI:https://www.iowen.cn Description:by 一为!官方网站:一为忆 -Version:1.1225 +Version:1.1227 Author:iowen Author URI: https://www.iowen.cn/ */ diff --git a/contribute.php b/template-contribute.php similarity index 80% rename from contribute.php rename to template-contribute.php index b89b2cd..4c8b339 100644 --- a/contribute.php +++ b/template-contribute.php @@ -41,8 +41,9 @@