-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-qiniu.php
290 lines (222 loc) · 7.91 KB
/
wp-qiniu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* 七牛云存储
*
* @author Yang,junlong at 2017-05-08 10:29:17 build.
* @version $Id$
*/
require_once 'qiniu-sdk-7.1.3/autoload.php';
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
// 文件上传方法
function qiniu_upload_file ($file, $key) {
if (!file_exists($file)) {
return false;
}
static $uploadMgr = null;
static $token = '';
if (!$uploadMgr) {
$qiniu_options = get_option('qiniu_options');
// 需要填写你的 Access Key 和 Secret Key
$accessKey = $qiniu_options['ak'];
$secretKey = $qiniu_options['sk'];
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 要上传的空间
$bucket = $qiniu_options['bucket'];;
// 生成上传 Token
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
}
list($ret, $err) = $uploadMgr->putFile($token, $key, $file);
if ($err !== null) {
return $err;
} else {
return $ret;
}
}
// 上传文件到七牛服务器
add_filter('wp_handle_upload', 'qiniu_handle_upload');
function qiniu_handle_upload ($data) {
$file = $data['file'];
$qiniu_options = get_option('qiniu_options');
$domain = trim($qiniu_options['domain'], '/') . '/';
$fixfile = qiniu_fixfile($file);
$key = md5($fixfile['file']) . '.' . $fixfile['exte'];
$result = qiniu_upload_file($file, $key);
$data['url'] = $domain . $key;
//$data['file'] = $key;
return $data;
}
/**
* 生成缩略图后立即上传
*
* @see https://developer.qiniu.com/dora/api/1279/basic-processing-images-imageview2
*/
add_filter('wp_generate_attachment_metadata', 'qiniu_update_attachment_meta', 999);
function qiniu_update_attachment_meta ($metadata, $upload = false) {
if(empty($metadata)) {
return $metadata;
}
$file = $metadata['file'];
$wp_upload_dir = wp_upload_dir();
$basedir = $wp_upload_dir['basedir'];
$filename = $basedir . '/'. $file;
$fixfile = qiniu_fixfile($file);
$key = md5($fixfile['file']) . '.' . $fixfile['exte'];
if($upload) {
if (qiniu_upload_file($filename, $key) == false) {
//return;
}
}
//$metadata['file'] = $key;
$metadata['file'] = $key;
$sizes = $metadata['sizes'];
//上传小尺寸文件
if (isset($sizes) && count($sizes) > 0){
foreach ($sizes as $k => $img){
if(empty($img['width'])) {
return;
}
$w = $img['width'];
$h = $img['height'];
switch ($k) {
case 'thumbnail':
$sizes[$k]['file'] = $key . '?imageView2/1/w/'.$w.'/h/'.$h.'/';
break;
case 'medium':
$sizes[$k]['file'] = $key . '?imageView2/2/w/'.$w.'/h/'.$h.'/';
case 'large':
$sizes[$k]['file'] = $key . '?imageView2/2/w/'.$w.'/h/'.$h.'/';
default:
}
$metadata['sizes'] = $sizes;
}
}
$metadata['qiniu'] = true;
return $metadata;
}
// 更新附件信息
add_filter('update_attached_file', 'qiniu_update_attachment');
function qiniu_update_attachment($file) {
$fixfile = qiniu_fixfile($file);
$key = md5($fixfile['file']) . '.' . $fixfile['exte'];
$result = qiniu_upload_file($file, $key);
return $file;
}
add_filter('wp_get_attachment_url', 'qiniu_format_attachment_url');
function qiniu_format_attachment_url($url) {
$urls = parse_url($url);
$path = trim($urls['path'], '/');
$host = str_replace($path, '', $url);
$wp_upload_dir = wp_upload_dir();
$baseurl = $wp_upload_dir['baseurl'];
$info = pathinfo($path);
$extension = $info['extension'];
$key = md5($path) . '.' . $extension;
// $qiniu_options = get_option('qiniu_options');
// $domain = trim($qiniu_options['domain'], '/');
return $baseurl . '/' . $key;
}
function qiniu_fixfile($file) {
$wp_upload_dir = wp_upload_dir();
$basedir = $wp_upload_dir['basedir'];
$filename = str_replace($basedir, '', $file);
$info = pathinfo($file);
$extension = $info['extension'];
$filename = trim($filename, '/');
return array(
'file' => $filename,
'exte' => $extension
);
}
add_action('plugins_loaded', 'qiniu_textdomain');
function qiniu_textdomain() {
load_plugin_textdomain('wp-qiniu', false, dirname(plugin_basename( __FILE__ )) . '/lang');
}
add_action('admin_menu', 'qiniu_add_setting_page');
function qiniu_add_setting_page() {
// 在 "设置" 菜单中添加一个管理子菜单,方便主题和插件的设置。
add_options_page('七牛存储选项', '七牛存储', 'manage_options', 'wp-qiniu/options.php');
}
// 表单提交提示信息
add_action('admin_notices', 'qiniu_warning');
function qiniu_warning($data) {
if ($_GET['page'] == 'wp-qiniu/options.php') {
// submit form
if(!empty($_POST['submit'])) {
// todo something
$options = array(
'ak' => trim($_POST['ak']),
'sk' => trim($_POST['sk']),
'bucket' => trim($_POST['bucket']),
'domain' => trim($_POST['domain'])
);
if (empty($options['domain'])) {
echo "<div id='qiniu-warning' class='updated fade'><p>你需要填写外链默认域名</p></div>";
return;
}
$result = update_option('qiniu_options', $options);
if ($result) {
echo '<div id="setting-error-settings_updated" class="updated settings-error"><p><strong>设置已保存。</strong></p></div>';
}
}
}
}
add_action('init', 'qiniu_sync_handle');
function qiniu_sync_handle() {
global $wpdb;
// 同步历史文件操作
if(!empty($_POST['qiniu_sync'])) {
if(!empty($_POST['post_id'])) {
$post_id = $_POST['post_id'];
$attached_file = get_post_meta($post_id, '_wp_attached_file');
$postmeta = get_post_meta($post_id, '_wp_attachment_metadata');
$postmeta = $postmeta[0];
$postmeta['file'] = $attached_file[0];
if(!empty($postmeta['file'])) {
$oldfile = $postmeta['file'];
$metadata = qiniu_update_attachment_meta($postmeta, true);
$newfile = $metadata['file'];
update_post_meta($post_id, '_wp_attachment_metadata', $metadata);
// 更新文章内容
$the_post = get_post($post_id);
$the_parent_id = $the_post->post_parent;
$the_parent_post = get_post($the_parent_id);
$the_parent_content = $the_parent_post->post_content;
$qiniu_options = get_option('qiniu_options');
$domain = trim($qiniu_options['domain'], '/');
$new_img_src = $domain . '/' . $newfile;
$oldfile = preg_replace('/\//', '\/', $oldfile);
// 替换文章中旧的图片地址
$the_new_content = preg_replace("/src=\"((http|ftp|https):\/\/sobird).+{$oldfile}/is", 'src="' . $new_img_src, $the_parent_content);
$the_new_content = preg_replace("/href=\"((http|ftp|https):\/\/sobird).+{$oldfile}/is", 'href="' . $new_img_src, $the_new_content);
wp_update_post(array(
'ID' => $the_parent_id,
'post_content' => $the_new_content
));
}
}
$posts = $wpdb->get_results("SELECT ID, post_date, post_title, post_parent FROM $wpdb->posts where post_type='attachment'");
$post_ids = array();
foreach ($posts as $post) {
$postmeta = get_post_meta($post->ID, '_wp_attachment_metadata');
$postmeta = $postmeta[0];
if(empty($postmeta['qiniu'])) {
// 新的meta data
//$metadata = qiniu_update_attachment_meta($postmeta[0]);
array_push($post_ids, $post->ID);
}
}
echo json_encode(array(
'data' => $post_ids,
'post' => $post_ids[0],
'oldfile' => $oldfile,
'newfile' => $newfile
));
die();
}
}