QQWorld Auto Save Images - Version 1.3

Version Description

Download this release

Release Info

Developer qqworld
Plugin Icon 128x128 QQWorld Auto Save Images
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

Files changed (1) hide show
  1. qqworld-auto-save-images.php +17 -16
qqworld-auto-save-images.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: QQWorld Auto Save Images
4
  Plugin URI: https://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.2
7
  Author: Michael Wang
8
  Author URI: http://www.qqworld.org
9
  */
@@ -91,18 +91,17 @@ class QQWorld_auto_save_images {
91
 
92
  $post=get_post($post_ID);
93
  $content=$post->post_content;
94
- $preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($content),$matches);
95
  if($preg){
96
- $i = 1;
97
  foreach($matches[1] as $image_url){
98
  if(empty($image_url)) continue;
99
  $pos=strpos($image_url,get_bloginfo('url'));
100
  if($pos===false){
101
- $res=$this->save_images($image_url,$post_id,$i);
102
- $replace=$res['url'];
103
- $content=str_replace($image_url,$replace,$content);
 
104
  }
105
- $i++;
106
  }
107
  }
108
  //Replace the image in the post
@@ -111,15 +110,17 @@ class QQWorld_auto_save_images {
111
  }
112
 
113
  //save exterior images
114
- function save_images($image_url,$post_id,$i){
115
- $file=file_get_contents($image_url);
116
- $filename=basename($image_url);
117
- preg_match( '/(.*?)(\.\w+)$/', $filename, $match );
118
- $im_name = $match[1].$match[2];
119
- $res=wp_upload_bits($im_name,'',$file);
120
- $attach_id = $this->insert_attachment($res['file'],$post_id);
121
- if( $i==1 ) set_post_thumbnail( $post_id, $attach_id );
122
- return $res;
 
 
123
  }
124
 
125
  //insert attachment
3
  Plugin Name: QQWorld Auto Save Images
4
  Plugin URI: https://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.3
7
  Author: Michael Wang
8
  Author URI: http://www.qqworld.org
9
  */
91
 
92
  $post=get_post($post_ID);
93
  $content=$post->post_content;
94
+ $preg=preg_match_all('/<img.*?src="(.*?)(\?.*?)?"/',stripslashes($content),$matches);
95
  if($preg){
 
96
  foreach($matches[1] as $image_url){
97
  if(empty($image_url)) continue;
98
  $pos=strpos($image_url,get_bloginfo('url'));
99
  if($pos===false){
100
+ if ($res=$this->save_images($image_url,$post_id)) {
101
+ $replace=$res['url'];
102
+ $content=str_replace($image_url,$replace,$content);
103
+ }
104
  }
 
105
  }
106
  }
107
  //Replace the image in the post
110
  }
111
 
112
  //save exterior images
113
+ function save_images($image_url, $post_id){
114
+ if ( $file=@file_get_contents($image_url) ) {
115
+ $filename=basename($image_url);
116
+ preg_match( '/(.*?)(\.\w+)$/', $filename, $match );
117
+ $im_name = $match[1].$match[2];
118
+ $res=wp_upload_bits($im_name,'',$file);
119
+ $attach_id = $this->insert_attachment($res['file'],$post_id);
120
+ if( !has_post_thumbnail($post_id) ) set_post_thumbnail( $post_id, $attach_id );
121
+ return $res;
122
+ }
123
+ return false;
124
  }
125
 
126
  //insert attachment