Version Description
Download this release
Release Info
Developer | qqworld |
Plugin | QQWorld Auto Save Images |
Version | 1.0 |
Comparing to | |
See all releases |
Version 1.0
- qqworld-auto-save-images.php +74 -0
qqworld-auto-save-images.php
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: QQWorld Auto Save Images
|
4 |
+
Plugin URI: http://wordpress.org/plugins/qqworld-auto-save-images/
|
5 |
+
Description: Automatically keep the all remote picture to the local, and automatically set featured image. 自动保存远程图片到本地,自动设置特色图片,并且支持机器人采集软件从外部提交。
|
6 |
+
Version: 1.0
|
7 |
+
Author: Michael Wang
|
8 |
+
Author URI: http://project.qqworld.org
|
9 |
+
*/
|
10 |
+
|
11 |
+
class QQWorld_auto_save_images {
|
12 |
+
function __construct() {
|
13 |
+
add_action('publish_post', array($this, 'fetch_images') );
|
14 |
+
}
|
15 |
+
function fetch_images($post_ID) {
|
16 |
+
//Check to make sure function is not executed more than once on save
|
17 |
+
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
|
18 |
+
return;
|
19 |
+
|
20 |
+
if ( !current_user_can('edit_post', $post_ID) )
|
21 |
+
return;
|
22 |
+
|
23 |
+
remove_action('publish_post', array($this, 'fetch_images') );
|
24 |
+
|
25 |
+
$post=get_post($post_ID);
|
26 |
+
$content=$post->post_content;
|
27 |
+
$preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($content),$matches);
|
28 |
+
if($preg){
|
29 |
+
$i = 1;
|
30 |
+
foreach($matches[1] as $image_url){
|
31 |
+
if(empty($image_url)) continue;
|
32 |
+
$pos=strpos($image_url,get_bloginfo('url'));
|
33 |
+
if($pos===false){
|
34 |
+
$res=$this->save_images($image_url,$post_id,$i);
|
35 |
+
$replace=$res['url'];
|
36 |
+
$content=str_replace($image_url,$replace,$content);
|
37 |
+
}
|
38 |
+
$i++;
|
39 |
+
}
|
40 |
+
}
|
41 |
+
//Replace the image in the post
|
42 |
+
wp_update_post(array('ID' => $post_ID, 'post_content' => $content));
|
43 |
+
add_action('publish_post', array($this, 'fetch_images') );
|
44 |
+
}
|
45 |
+
//save exterior images
|
46 |
+
function save_images($image_url,$post_id,$i){
|
47 |
+
$file=file_get_contents($image_url);
|
48 |
+
$filename=basename($image_url);
|
49 |
+
preg_match( '/(.*?)(\.\w+)$/', $filename, $match );
|
50 |
+
$im_name = md5($match[1]).$match[2];
|
51 |
+
$res=wp_upload_bits($im_name,'',$file);
|
52 |
+
$attach_id = $this->insert_attachment($res['file'],$post_id);
|
53 |
+
if( $i==1 ) set_post_thumbnail( $post_id, $attach_id );
|
54 |
+
return $res;
|
55 |
+
}
|
56 |
+
|
57 |
+
//insert attachment
|
58 |
+
function insert_attachment($file,$id){
|
59 |
+
$dirs=wp_upload_dir();
|
60 |
+
$filetype=wp_check_filetype($file);
|
61 |
+
$attachment=array(
|
62 |
+
'guid'=>$dirs['baseurl'].'/'._wp_relative_upload_path($file),
|
63 |
+
'post_mime_type'=>$filetype['type'],
|
64 |
+
'post_title'=>preg_replace('/\.[^.]+$/','',basename($file)),
|
65 |
+
'post_content'=>'',
|
66 |
+
'post_status'=>'inherit'
|
67 |
+
);
|
68 |
+
$attach_id=wp_insert_attachment($attachment,$file,$id);
|
69 |
+
$attach_data=wp_generate_attachment_metadata($attach_id,$file);
|
70 |
+
wp_update_attachment_metadata($attach_id,$attach_data);
|
71 |
+
return $attach_id;
|
72 |
+
}
|
73 |
+
}
|
74 |
+
new QQWorld_auto_save_images;
|