Skip to content

Commit 9884b87

Browse files
committed
Add Project
0 parents  commit 9884b87

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

banner-772x250.jpg

18.1 KB
Loading

banner-772x250.psd

155 KB
Binary file not shown.

index.php

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
/*
3+
Plugin Name: Auto Upload Images
4+
Plugin URI: http://p30design.net/1391/08/wp-auto-upload-images.html
5+
Description: Automatically upload external images of a post to wordpress upload directory
6+
Version: 1.4.1
7+
Author: Ali Irani
8+
Author URI: http://p30design.net
9+
License: GPLv2 or later
10+
*/
11+
12+
class wp_auto_upload {
13+
14+
function __construct() {
15+
add_action('save_post', array($this, 'auto_upload'));
16+
}
17+
18+
/**
19+
* Automatically upload external images of a post to wordpress upload directory
20+
*
21+
* @param $post_id
22+
*/
23+
public function auto_upload( $post_id ) {
24+
25+
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
26+
return;
27+
28+
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
29+
return;
30+
31+
if ( false !== wp_is_post_revision($post_id) )
32+
return;
33+
34+
global $wpdb;
35+
36+
$content = $wpdb->get_var( "SELECT post_content FROM wp_posts WHERE ID='$post_id' LIMIT 1" );
37+
38+
$images_url = $this->wp_get_images_url($content);
39+
40+
if($images_url) {
41+
foreach ($images_url as $image_url) {
42+
if(!$this->wp_is_myurl($image_url) && $new_image_url = $this->wp_save_image($image_url, $post_id)) {
43+
$new_images_url[] = $new_image_url;
44+
unset($new_image_url);
45+
} else {
46+
$new_images_url[] = $image_url;
47+
}
48+
}
49+
50+
$total = count($new_images_url);
51+
52+
for ($i = 0; $i <= $total-1; $i++) {
53+
$new_images_url[$i] = parse_url($new_images_url[$i]);
54+
$content = preg_replace('/'. preg_quote($images_url[$i], '/') .'/', $new_images_url[$i]['path'], $content);
55+
}
56+
57+
remove_action( 'save_post', array($this, 'auto_upload') );
58+
wp_update_post( array('ID' => $post_id, 'post_content' => $content) );
59+
add_action( 'save_post', array($this, 'auto_upload') );
60+
}
61+
}
62+
63+
/**
64+
* Detect url of images which exists in content
65+
*
66+
* @param $content
67+
* @return array of urls or false
68+
*/
69+
public function wp_get_images_url( $content ) {
70+
preg_match_all('/<img[^>]*src=("|\')([^(\?|#|"|\')]*)(\?|#)?[^("|\')]*("|\')[^>]*\/>/', $content, $urls, PREG_SET_ORDER);
71+
72+
if(is_array($urls)) {
73+
foreach ($urls as $url)
74+
$images_url[] = $url[2];
75+
}
76+
77+
if (is_array($images_url)) {
78+
$images_url = array_unique($images_url);
79+
rsort($images_url);
80+
}
81+
82+
return isset($images_url) ? $images_url : false;
83+
}
84+
85+
/**
86+
* Check url is internal or external
87+
*
88+
* @param $url
89+
* @return true or false
90+
*/
91+
public function wp_is_myurl( $url ) {
92+
$url = $this->wp_get_base_url($url);
93+
$myurl = $this->wp_get_base_url(get_bloginfo('url'));
94+
95+
switch ($url) {
96+
case NULL:
97+
case $myurl:
98+
return true;
99+
break;
100+
101+
default:
102+
return false;
103+
break;
104+
}
105+
}
106+
107+
/**
108+
* Give a $url and return Base of a $url
109+
*
110+
* @param $url
111+
* @return base of $url without wwww
112+
*/
113+
public function wp_get_base_url( $url ) {
114+
$url = parse_url($url, PHP_URL_HOST); // Give base URL
115+
$temp = preg_split('/^(www(2|3)?\.)/i', $url, -1, PREG_SPLIT_NO_EMPTY); // Delete www from URL
116+
117+
return $temp[0];
118+
}
119+
120+
/**
121+
* Save image on wp_upload_dir
122+
* Add image to Media Library and attach to post
123+
*
124+
* @param $url
125+
* @param $post_id
126+
* @return new $url or false
127+
*/
128+
public function wp_save_image($url, $post_id = 0) {
129+
$image_name = basename($url);
130+
131+
$upload_dir = wp_upload_dir(date('Y/m'));
132+
$path = $upload_dir['path'] . '/' . $image_name;
133+
$new_image_url = $upload_dir['url'] . '/' . rawurlencode($image_name);
134+
$file_exists = true;
135+
$i = 0;
136+
137+
while ( $file_exists ) {
138+
if ( file_exists($path) ) {
139+
if ( $this->wp_get_exfilesize($url) == filesize($path) ) {
140+
return $new_image_url;
141+
} else {
142+
$i++;
143+
$path = $upload_dir['path'] . '/' . $i . '_' . $image_name;
144+
$new_image_url = $upload_dir['url'] . '/' . $i . '_' . $image_name;
145+
}
146+
} else {
147+
$file_exists = false;
148+
}
149+
}
150+
151+
if(function_exists('curl_init')) {
152+
$ch = curl_init($url);
153+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
154+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
155+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
156+
$data = curl_exec($ch);
157+
curl_close($ch);
158+
file_put_contents($path, $data);
159+
160+
$wp_filetype = wp_check_filetype($new_image_url);
161+
$attachment = array(
162+
'guid' => $new_image_url,
163+
'post_mime_type' => $wp_filetype['type'],
164+
'post_title' => preg_replace('/\.[^.]+$/', '', basename($new_image_url)),
165+
'post_content' => '',
166+
'post_status' => 'inherit'
167+
);
168+
wp_insert_attachment($attachment, $path, $post_id);
169+
170+
return $new_image_url;
171+
} else {
172+
return false;
173+
}
174+
}
175+
176+
/**
177+
* return size of external file
178+
*
179+
* @param $file
180+
* @return $size
181+
*/
182+
public function wp_get_exfilesize( $file ) {
183+
$ch = curl_init($file);
184+
curl_setopt($ch, CURLOPT_NOBODY, true);
185+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
186+
curl_setopt($ch, CURLOPT_HEADER, true);
187+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
188+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
189+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
190+
$data = curl_exec($ch);
191+
curl_close($ch);
192+
193+
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
194+
return $contentLength = (int)$matches[1];
195+
else
196+
return false;
197+
}
198+
199+
}
200+
201+
$wp_auto_upload = new wp_auto_upload();

readme.txt

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
=== Auto Upload Images ===
2+
Contributors: airani
3+
Donate link: http://p30design.net/about/contact
4+
Tags: upload, auto, image, automaticlly, images, admin, post
5+
Requires at least: 2.7
6+
Tested up to: 3.5.1
7+
Stable tag: trunk
8+
License: GPLv2 or later
9+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
10+
11+
== Description ==
12+
13+
When you want to save post, this plugin search for images url which exists in post and automatically upload external images to wordpress upload directory and replace external link with your link.
14+
Automatically add image to Media Library and attach to post.
15+
16+
<a href="http://p30design.net/1391/08/wp-auto-upload-images.html" title="plugin page" target="_blank">Official Plugin Page</a>
17+
18+
== Installation ==
19+
20+
Upload the "Auto Upload Images" plugin to your blog, Activate it.
21+
22+
1, 2, 3: You're done!
23+
24+
== Frequently Asked Questions ==
25+
26+
= Are there special settings for the plugin? =
27+
28+
No, Just Activate it :)
29+
30+
== Changelog ==
31+
32+
= 1.4.1 =
33+
34+
* Fixed Bug: Fixed tiny bug ;) Thanks to Ali for reporting bug
35+
36+
= 1.4 =
37+
38+
* New Feature: Work With Multi Address Sites
39+
* Fixed Bug: Work with Persian & Arabic URLs
40+
* Fixed Bug: Replace URL for images already been uploaded
41+
* Implementation with object-oriented
42+
43+
= 1.3 =
44+
45+
* Fixed some bugs
46+
47+
= 1.2 =
48+
49+
* Fixed Bug: Save one revision post
50+
* Fixed Bug: Fix pattern of urls
51+
* Fixed Bug: Save file with same name
52+
* Fixed Bug: More images with same urls in post
53+
* Fixed Bug: Work with ssl urls
54+
55+
= 1.1 =
56+
57+
* Add image to Media Library and attach to post
58+
* Fix a bug
59+
60+
= 1.0 =
61+
62+
* It's first version.

0 commit comments

Comments
 (0)