Skip to content

Commit 1ec9286

Browse files
committed
添加图片删除按钮
投稿面板文件名称修改
1 parent d39ff3f commit 1ec9286

File tree

8 files changed

+194
-36
lines changed

8 files changed

+194
-36
lines changed

css/nav.css

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/ajax.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
if ( ! defined( 'ABSPATH' ) ) { exit; }
3+
4+
//图片上传
5+
add_action('wp_ajax_nopriv_img_upload', 'io_img_upload');
6+
add_action('wp_ajax_img_upload', 'io_img_upload');
7+
function io_img_upload(){
8+
$extArr = array("jpg", "png", "jpeg");
9+
$file = $_FILES['files'];
10+
if ( !empty( $file ) ) {
11+
$wp_upload_dir = wp_upload_dir(); // 获取上传目录信息
12+
$basename = $file['name'];
13+
$baseext = pathinfo($basename, PATHINFO_EXTENSION);
14+
$dataname = date("YmdHis_").substr(md5(time()), 0, 8) . '.' . $baseext;
15+
$filename = $wp_upload_dir['path'] . '/' . $dataname;
16+
rename( $file['tmp_name'], $filename ); // 将上传的图片文件移动到上传目录
17+
$attachment = array(
18+
'guid' => $wp_upload_dir['url'] . '/' . $dataname, // 外部链接的 url
19+
'post_mime_type' => $file['type'], // 文件 mime 类型
20+
'post_title' => preg_replace( '/\.[^.]+$/', '', $basename ), // 附件标题,采用去除扩展名之后的文件名
21+
'post_content' => '', // 文章内容,留空
22+
'post_status' => 'inherit'
23+
);
24+
$attach_id = wp_insert_attachment( $attachment, $filename ); // 插入附件信息
25+
if($attach_id != 0){
26+
require_once( ABSPATH . 'wp-admin/includes/image.php' ); // 确保包含此文件,因为wp_generate_attachment_metadata()依赖于此文件。
27+
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
28+
wp_update_attachment_metadata( $attach_id, $attach_data ); // 生成附件的元数据,并更新数据库记录。
29+
print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>$basename))));
30+
exit();
31+
}else{
32+
echo '{"status":4,"msg":"图片上传失败!"}';
33+
exit();
34+
}
35+
}
36+
}
37+
38+
//删除图片
39+
add_action('wp_ajax_nopriv_img_remove', 'io_img_remove');
40+
add_action('wp_ajax_img_remove', 'io_img_remove');
41+
function io_img_remove(){
42+
$attach_id = $_POST["id"];
43+
if( empty($attach_id) ){
44+
echo '{"status":3,"msg":"没有上传图像!"}';
45+
exit;
46+
}
47+
if ( false === wp_delete_attachment( $attach_id ) )
48+
echo '{"status":4,"msg":"图片 '.$attach_id.' 删除失败!"}';
49+
else
50+
echo '{"status":1,"msg":"删除成功!"}';
51+
exit;
52+
}
53+
54+
//提交文章
55+
add_action('wp_ajax_nopriv_contribute_post', 'io_contribute');
56+
add_action('wp_ajax_contribute_post', 'io_contribute');
57+
function io_contribute(){
58+
$delay = 40;
59+
if( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < $delay ){
60+
error('{"status":2,"msg":"您投稿也太勤快了吧,'. ($delay - ( time() - $_COOKIE["tougao"] )).'秒后再试!"}');
61+
}
62+
63+
//表单变量初始化
64+
$sites_link = isset( $_POST['tougao_sites_link'] ) ? trim(htmlspecialchars($_POST['tougao_sites_link'], ENT_QUOTES)) : '';
65+
$sites_sescribe = isset( $_POST['tougao_sites_sescribe'] ) ? trim(htmlspecialchars($_POST['tougao_sites_sescribe'], ENT_QUOTES)) : '';
66+
$title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
67+
$category = isset( $_POST['tougao_cat'] ) ? $_POST['tougao_cat'] : '0';
68+
$sites_ico = isset( $_POST['tougao_sites_ico'] ) ? trim(htmlspecialchars($_POST['tougao_sites_ico'], ENT_QUOTES)) : '';
69+
$wechat_qr = isset( $_POST['tougao_wechat_qr'] ) ? trim(htmlspecialchars($_POST['tougao_wechat_qr'], ENT_QUOTES)) : '';
70+
$content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : '';
71+
72+
// 表单项数据验证
73+
if ( $category == "0" ){
74+
error('{"status":4,"msg":"请选择分类。"}');
75+
}
76+
if ( !empty(get_term_children($category, 'favorites'))){
77+
error('{"status":4,"msg":"不能选用父级分类目录。"}');
78+
}
79+
if ( empty($sites_sescribe) || mb_strlen($sites_sescribe) > 50 ) {
80+
error('{"status":4,"msg":"网站描叙必须填写,且长度不得超过50字。"}');
81+
}
82+
if ( empty($sites_link) && empty($wechat_qr) ){
83+
error('{"status":3,"msg":"网站链接和公众号二维码至少填一项。"}');
84+
}
85+
elseif ( !empty($sites_link) && !preg_match('/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $sites_link)) {
86+
error('{"status":4,"msg":"网站链接必须符合URL格式。"}');
87+
}
88+
if ( empty($title) || mb_strlen($title) > 30 ) {
89+
error('{"status":4,"msg":"网站名称必须填写,且长度不得超过30字。"}');
90+
}
91+
//if ( empty($content) || mb_strlen($content) > 10000 || mb_strlen($content) < 6) {
92+
// error('{"status":4,"msg":"内容必须填写,且长度不得超过10000字,不得少于6字。"}');
93+
//}
94+
95+
$tougao = array(
96+
'comment_status' => 'closed',
97+
'ping_status' => 'closed',
98+
//'post_author' => 1,//用于投稿的用户ID
99+
'post_title' => $title,
100+
'post_content' => $content,
101+
'post_status' => 'pending',
102+
'post_type' => 'sites',
103+
//'tax_input' => array( 'favorites' => array($category) ) //游客不可用
104+
);
105+
106+
// 将文章插入数据库
107+
$status = wp_insert_post( $tougao );
108+
if ($status != 0){
109+
global $wpdb;
110+
add_post_meta($status, '_sites_sescribe', $sites_sescribe);
111+
add_post_meta($status, '_sites_link', $sites_link);
112+
add_post_meta($status, '_sites_order', '0');
113+
if( !empty($sites_ico))
114+
add_post_meta($status, '_thumbnail', $sites_ico);
115+
if( !empty($wechat_qr))
116+
add_post_meta($status, '_wechat_qr', $wechat_qr);
117+
wp_set_post_terms( $status, array($category), 'favorites'); //设置文章分类
118+
setcookie("tougao", time(), time()+$delay+10);
119+
error('{"status":1,"msg":"投稿成功!"}');
120+
}else{
121+
error('{"status":4,"msg":"投稿失败!"}');
122+
}
123+
}
124+
function error($ErrMsg) {
125+
echo $ErrMsg;
126+
exit;
127+
}

inc/contribute-ajax.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
2-
2+
/**
3+
* 弃用,已经移至ajax.php
4+
*/
35
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
46
header('Allow: POST');
57
header('HTTP/1.1 405 Method Not Allowed');
@@ -77,4 +79,4 @@
7779
function error($ErrMsg) {
7880
echo $ErrMsg;
7981
exit;
80-
}
82+
}

inc/img-upload.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
* 原文地址:https://www.iowen.cn/wordpress-visitors-upload-pictures
55
* 一为忆
66
* WebStack 导航主题
7+
*
8+
* 弃用,已经移至ajax.php
79
*/
10+
811
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
912
header('Allow: POST');
1013
header('HTTP/1.1 405 Method Not Allowed');
@@ -48,10 +51,10 @@
4851
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
4952
wp_update_attachment_metadata( $attach_id, $attach_data ); // 生成附件的元数据,并更新数据库记录。
5053
// 返回消息至前端
51-
print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('src'=>wp_get_attachment_url( $attach_id ),'title'=>time()))));
54+
print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>time()))));
5255
exit();
5356
}else{
5457
echo '{"status":4,"msg":"图片上传失败!"}';
5558
exit();
5659
}
57-
}
60+
}

inc/inc.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require_once get_theme_file_path() .'/inc/post-type.php';
1414
require_once get_theme_file_path() .'/inc/fav-content.php';
1515
require_once get_theme_file_path() .'/inc/meta-boxes.php';
16+
require_once get_theme_file_path() .'/inc/ajax.php';
1617

1718

1819
// 禁用版本修订

inc/register.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ function theme_load_scripts() {
3232
wp_enqueue_script( 'comment-reply' );
3333
}
3434
}
35-
wp_localize_script('app', 'Theme' , array(
35+
wp_localize_script('appjs', 'theme' , array(
3636
'ajaxurl' => admin_url( 'admin-ajax.php' ),
37+
'addico' => get_template_directory_uri() . '/images/add.png',
3738
));
3839
}
3940
add_action('wp_enqueue_scripts', 'theme_load_scripts');

style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Theme Name:WebStack
33
Theme URI:https://www.iowen.cn
44
Description:by 一为!官方网站:<a href="https://www.iowen.cn">一为忆</a>
5-
Version:1.1225
5+
Version:1.1227
66
Author:iowen
77
Author URI: https://www.iowen.cn/
88
*/

contribute.php renamed to template-contribute.php

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
<div class="upload_img">
4242
<div class="show_ico">
4343
<img id="show_sites_ico" src="<?php echo get_template_directory_uri() . '/images/add.png' ?>" alt="网站图标">
44+
<i id="remove_sites_ico" class="fa fa-times-circle remove" data-id="" data-type="sites_ico" style="display: none;"></i>
4445
</div>
45-
<input type="file" id="upload_ico" data-type="sites_ico" accept="image/*" onchange="uploadImg(this)" >
46+
<input type="file" id="upload_sites_ico" data-type="sites_ico" accept="image/*" onchange="uploadImg(this)" >
4647
</div>
4748
</div>
4849
<div class="row">
@@ -87,9 +88,10 @@
8788
<input type="hidden" value="" id="tougao_wechat_qr" name="tougao_wechat_qr" />
8889
<div class="upload_img wechat">
8990
<div class="show_ico">
90-
<img id="show_wechat_qr" src="<?php echo get_template_directory_uri() . '/images/add.png' ?>" alt="网站图标">
91+
<img id="show_wechat_qr" src="<?php echo get_template_directory_uri() . '/images/add.png' ?>" alt="公众号二维码">
92+
<i id="remove_wechat_qr" class="fa fa-times-circle remove" data-id="" data-type="wechat_qr" style="display: none;"></i>
9193
</div>
92-
<input type="file" id="upload_ico" data-type="wechat_qr" accept="image/*" onchange="uploadImg(this)" >
94+
<input type="file" id="upload_wechat_qr" data-type="wechat_qr" accept="image/*" onchange="uploadImg(this)" >
9395
</div>
9496
</div>
9597
<div class="col-sm-9 col-md-10 mt-2">
@@ -113,11 +115,7 @@
113115
</div>
114116
</div>
115117
</div>
116-
117-
<?php
118-
$imgUpload = get_bloginfo('template_directory') . '/inc/img-upload.php';
119-
$contribute = get_bloginfo('template_directory') . '/inc/contribute-ajax.php';
120-
?>
118+
121119
<script>
122120
var verification = Math.floor(Math.random()*(9999-1000+1)+1000);
123121
$('#verification-text').text(verification);
@@ -127,23 +125,21 @@
127125
showAlert(JSON.parse('{"status":3,"msg":"验证码错误!"}'));
128126
return false;
129127
}
130-
$.ajax( {
131-
url: '<?php echo $contribute ?>',
128+
$.ajax({
129+
url: theme.ajaxurl,
132130
type: 'POST',
133131
dataType: 'json',
134-
data: $(this).serialize(),
135-
error: function(result) {
136-
showAlert(JSON.parse('{"status":3,"msg":"网络连接错误!"}'));
137-
},
138-
success: function(result) {
139-
if(result.status == 1){
140-
verification = Math.floor(Math.random()*(9999-1000+1)+1000);
141-
$('#verification-text').text(verification);
142-
}
143-
showAlert(result);
132+
data: $(this).serialize() + "&action=contribute_post",
133+
}).done(function (result) {
134+
if(result.status == 1){
135+
verification = Math.floor(Math.random()*(9999-1000+1)+1000);
136+
$('#verification-text').text(verification);
144137
}
138+
showAlert(result);
139+
}).fail(function (result) {
140+
showAlert(JSON.parse('{"status":3,"msg":"网络连接错误!"}'));
145141
});
146-
return false;
142+
return false;
147143
});
148144
function showAlert(data) {
149145
var alert,ico;
@@ -171,10 +167,7 @@ function showAlert(data) {
171167
$('#alert_placeholder').append( $html );//prepend
172168
$html.show(100).delay(3000).hide(200, function(){ $(this).remove() });
173169
}
174-
function uploadImg(obj) {
175-
upload(obj)
176-
}
177-
function upload(file) {
170+
function uploadImg(file) {
178171
var doc_id=file.getAttribute("data-type");
179172
if (file.files != null && file.files[0] != null) {
180173
if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(file.files[0].name)) {
@@ -187,20 +180,23 @@ function upload(file) {
187180
}
188181
var formData = new FormData();
189182
formData.append('files', file.files[0]);
183+
formData.append('action','img_upload');
190184
$.ajax({
191-
url: '<?php echo $imgUpload ?>',
185+
url: theme.ajaxurl,
192186
type: 'POST',
193-
cache: false,
194187
data: formData,
195188
dataType: 'json',
189+
cache: false,
196190
processData: false,
197191
contentType: false
198192
}).done(function (result) {
199193
//console.log('--->>>'+JSON.stringify(result));
200194
showAlert(result);
201195
if(result.status == 1){
202-
document.getElementById("show_"+doc_id).src = result.data.src;
203-
document.getElementById("tougao_"+doc_id).value = result.data.src;
196+
$("#show_"+doc_id).attr("src", result.data.src);
197+
$("#tougao_"+doc_id).val(result.data.src);
198+
$("#remove_"+doc_id).data('id',result.data.id).show();
199+
$(file).attr("disabled","disabled").parent().addClass('disabled');
204200
}
205201
}).fail(function (result) {
206202
showAlert(JSON.parse('{"status":3,"msg":"网络连接错误!"}'));
@@ -210,6 +206,31 @@ function upload(file) {
210206
return false;
211207
}
212208
}
209+
$('.fa.remove').click(function() {
210+
if(!confirm('确定要删除图片吗?')){
211+
return false;
212+
}
213+
var doc_id = $(this).data('type');
214+
$.ajax( {
215+
url: theme.ajaxurl,
216+
type: 'POST',
217+
dataType: 'json',
218+
data: {
219+
action: "img_remove",
220+
id: $(this).data("id")
221+
}
222+
}).done(function (result) {
223+
showAlert(result);
224+
if(result.status == 1){
225+
$("#show_"+doc_id).attr("src", theme.addico);
226+
$("#tougao_"+doc_id).val('');
227+
$("#remove_"+doc_id).data('id','').hide();
228+
$("#upload_"+doc_id).removeAttr("disabled").val("").parent().removeClass('disabled');
229+
}
230+
}).fail(function (result) {
231+
showAlert(JSON.parse('{"status":3,"msg":"网络连接错误!"}'));
232+
});
233+
});
213234
</script>
214235

215236
<?php get_footer(); ?>

0 commit comments

Comments
 (0)