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 ();
0 commit comments