-
Notifications
You must be signed in to change notification settings - Fork 7
/
import-external-images.php
500 lines (385 loc) · 16.5 KB
/
import-external-images.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/*
Plugin Name: Import External Images
Plugin URI: http://martythornley.com
Version: 1.3
Description: Examines the text of a post and makes local copies of all the images linked though IMG tags, adding them as gallery attachments on the post itself.
Author: Marty Thornley
Author URI: http://martythornley.com
License: GPLv2 or later
*/
/*
based on Add Linked Images To Gallery v1.4 by Randy Hunt
http://www.bbqiguana.com/wordpress-plugins/add-linked-images-to-gallery/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
$external_image_count = 0;
define( 'EXTERNAL_IMAGES_MAX_POSTS_COUNT' , 50 );
define( 'EXTERNAL_IMAGES_MAX_COUNT' , 20 );
define( 'EXTERNAL_IMAGES_DIR' , plugin_dir_path( __FILE__ ) );
define( 'EXTERNAL_IMAGES_URL' , plugins_url( basename( dirname( __FILE__ ) ) ) );
define( 'EXTERNAL_IMAGES_ALLOW_BULK_MESSAGE' , false );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
include_once( plugin_dir_path( __FILE__ ) . 'ajax.php');
//register_activation_hook( __FILE__ , 'external_image_install' );
add_action( 'admin_menu', 'external_image_menu' );
add_action( 'admin_init', 'external_image_admin_init' );
add_action( 'admin_head' , 'external_images_bulk_resize_admin_javascript' );
add_action( 'admin_notices', 'external_images_bulk_resize_message' , 90 );
function external_image_admin_init () {
global $pagenow;
register_setting( 'external_image' , 'external_image_whichimgs' );
if ( $pagenow == 'post.php' ) {
add_action( 'post_submitbox_misc_actions' , 'import_external_images_per_post' );
add_action( 'save_post', 'external_image_import_images' );
}
add_filter( 'attachment_link' , 'force_attachment_links_to_link_to_image' , 9 , 3 );
}
function external_images_bulk_resize_message(){
global $pagenow;
if ( EXTERNAL_IMAGES_ALLOW_BULK_MESSAGE ) {
$message = '<h4>Please Resize Your Images</h4>';
$message .= '<p>You may want to resize large images on you previous site before importing images. It will help save bandwidth during the import and prevent the import from crashing.';
$message .= '<p>You can <a href="http://photographyblogsites.com/file-folder/import-tools/bulk-resize-media.zip">download the "Bulk Image Resizer" here.</a></p>';
if ( $pagenow == 'upload.php' && isset( $_GET['page'] ) && $_GET['page'] == 'external_image' ) {
echo '<div class="updated fade">';
echo $message;
echo '</div>';
}
}
}
function force_attachment_links_to_link_to_image( $link , $id ) {
$object = get_post( $id );
$mime_types = array(
'image/png',
'image/jpeg',
'image/jpeg',
'image/jpeg',
'image/gif',
'image/bmp'
);
// if this post does not exists on this site, return empty string
if ( ! $object )
return '';
if ( $object && in_array( $object->post_mime_type , $mime_types ) && $object->guid != '' )
$link = $object->guid;
return $link;
}
function external_image_menu() {
add_media_page( 'Import Images', 'Import Images', 'edit_theme_options', 'external_image', 'external_image_options' );
}
/*
* Meta Boxes for hiding pages from main menu
*/
function import_external_images_per_post() {
$external_images = external_image_get_img_tags( $_GET['post'] );
$html = '';
if ( is_array( $external_images ) && count( $external_images ) > 0 ) {
$html = '<div class="misc-pub-section " id="external-images" style="background-color: #FFFFE0; border-color: #E6DB55;">';
$html .= '<h4>You have ('.count( $external_images ).') images that can be imported!</h4>';
foreach ( $external_images as $external_image ) {
$html .= '<img style="margin: 3px; max-width:50px;" src="'.$external_image.'" />';
}
$html .= '<input type="hidden" name="import_external_images_nonce" id="import_external_images_nonce" value="'.wp_create_nonce( 'import_external_images_nonce' ).'" />';
$html .= '<p><input type="checkbox" name="import_external_images" id="import_external_images" value="import-'.$_GET['post'].'" /> Import External Images?</p>';
$html .= '<p class="howto">Only '.EXTERNAL_IMAGES_MAX_COUNT.' images will be imported at a time to keep things from taking too long.</p>';
$html .= '</div>';
}
echo $html;
}
function is_external_file( $file ) {
$allowed = array( '.jpg' , '.png', '.bmp' , '.gif' );
$ext = substr( $file , -4 );
if ( in_array( strtolower($ext) , $allowed ) )
return true;
return false;
}
function external_image_import_images( $post_id , $force = false ) {
global $pagenow;
if ( get_transient( 'saving_imported_images_' .$post_id ) )
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( $force == false && !wp_verify_nonce( $_REQUEST['import_external_images_nonce'] , 'import_external_images_nonce' ) )
return;
if ( $force == false && $pagenow != 'post.php' )
return;
if ( $force == false && $pagenow == 'post.php' && !isset( $_POST['import_external_images'] ) )
return;
if (wp_is_post_revision($post_id))
return;
$post = get_post($post_id);
$replaced = false;
$content = $post->post_content;
$imgs = external_image_get_img_tags($post_id);
$count = 0;
for ( $i=0; $i<EXTERNAL_IMAGES_MAX_COUNT; $i++ ) {
if (isset($imgs[$i]) && is_external_file($imgs[$i]) ) {
$new_img = external_image_sideload( $imgs[$i] , $post_id );
if ($new_img && is_external_file($new_img) ) {
$content = str_replace( $imgs[$i] , $new_img , $content);
$replaced = true;
$count++;
}
}
}
if ( $replaced ) {
set_transient( 'saving_imported_images_'.$post_id , 'true' , 20 );
$update_post = array();
$update_post['ID'] = $post_id;
$update_post['post_content'] = $content;
wp_update_post($update_post);
_fix_attachment_links( $post_id );
$response = $count;
} else {
$response = false;
}
return $response;
}
/*
* Handle importing of external image
* Most of this taken from WordPress function 'media_sideload_image'
* @param string $file The URL of the image to download
* @param int $post_id The post ID the media is to be associated with
* @param string $desc Optional. Description of the image
* @return string - just the image url on success, false on failure
*/
function external_image_sideload( $file , $post_id , $desc = '' ) {
if ( ! empty($file) && is_external_file( $file ) ) {
// Download file to temp location
$tmp = download_url( $file );
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
return false;
}
$desc = $file_array['name'];
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return false;
} else {
$src = wp_get_attachment_url( $id );
}
}
if ( !empty( $src ) && is_external_file( $src ) )
return $src;
else
return false;
}
function external_image_getext( $file ) {
if ( function_exists( 'mime_content_type' ) ) {
$mime = strtolower(mime_content_type($file));
switch($mime) {
case 'image/jpg':
case 'image/jpeg':
return '.jpg';
break;
case 'image/gif':
return '.gif';
break;
case 'image/png':
return '.png';
break;
}
return '';
} else {
return '';
}
}
function external_image_get_img_tags ( $post_id ) {
$post = get_post( $post_id );
$w = get_option( 'external_image_whichimgs' );
$s = get_option( 'siteurl' );
// need to also check for multisitesubdomain and mapped domain...
//$mapped = mapped_domain;
$excludes = get_option( 'external_image_excludes' );
$excludes = explode( ',' , $excludes );
$result = array();
preg_match_all( '/<img[^>]* src=[\'"]?([^>\'" ]+)/' , $post->post_content , $matches );
preg_match_all( '/<a[^>]* href=[\'"]?([^>\'" ]+)/' , $post->post_content , $matches2 );
$matches[0] = array_merge( $matches[0] , $matches2[0] );
$matches[1] = array_merge( $matches[1] , $matches2[1] );
for ( $i=0; $i<count($matches[0]); $i++ ) {
$uri = $matches[1][$i];
$path_parts = pathinfo($uri);
// check all excluded urls
if ( is_array( $excludes ) ) {
foreach( $excludes as $exclude ) {
$trim = trim( $exclude );
if ( $trim !='' && strpos( $uri , $trim ) != false )
$uri = '';
}
}
//only check FQDNs
if ( $uri != '' && preg_match( '/^http:\/\//' , $uri ) ) {
//make sure it's external
if ( $s != substr( $uri , 0 , strlen( $s ) ) && ( !isset( $mapped ) || $mapped != substr( $uri , 0 , strlen( $mapped ) ) ) ) {
$path_parts['extension'] = (isset($path_parts['extension'])) ? strtolower($path_parts['extension']) : false;
if ( $path_parts['extension'] == 'gif' || $path_parts['extension'] == 'jpg' || $path_parts['extension'] == 'png' )
$result[] = $uri;
}
}
}
//print_r( $matches );
$result = array_unique($result);
return $result;
}
function external_image_backcatalog () {
$posts = get_posts( array( 'numberposts'=>-1 ) );
echo '<h4>Processing Posts...</h4>';
set_time_limit(300);
$count = 0;
$before = '<form style="padding: 0 10px; margin: 20px 20px 0 0; float: left;" action="" method="post" name="external_image-backcatalog">';
$resubmit = '<input type="hidden" value="backcatalog" name="action">
<input class="button-primary" type="submit" value="Process More Posts">';
$after = '</form>';
foreach( $posts as $post ) {
try {
$imgs = external_image_get_img_tags($post->ID);
if ( is_array( $imgs ) && count( $imgs ) > 0 ) {
$count += count( $imgs );
echo '<p>Post titled: "<strong>'.$post->post_title . '</strong>" - ';
external_image_import_images( $post->ID , true );
echo count( $imgs ) . ' Images processed</p>';
}
} catch (Exception $e) {
echo '<em>an error occurred</em>.</p>';
}
}
if($done_message) {
echo $before;
echo $done_message;
echo $resubmit;
echo $after;
} else {
echo '<p>Finished processing past posts!</p>';
}
}
function external_image_get_backcatalog () {
$posts = get_posts( array( 'numberposts' => -1 ) );
$count_posts = 0;
$posts_to_import = array();
foreach( $posts as $post ) {
$count_images = 0;
try {
$imgs = external_image_get_img_tags($post->ID);
if ( is_array( $imgs ) && count( $imgs ) > 0 ) {
$count_images += count( $imgs );
$posts_to_import[] = $post->ID;
$count_posts ++;
}
} catch (Exception $e) {
echo '<em>an error occurred</em>.</p>';
}
}
return $posts_to_import;
}
function external_image_options () {
$_cats = '';
$_auths = '';
?>
<style type="text/css">
#import_posts #processing { background: url( <?php echo EXTERNAL_IMAGES_URL; ?>/images/ajax-loader.gif ) top left transparent no-repeat; padding: 0 0 0 23px; }
</style>
<div class="wrap" style="overflow:hidden;">
<div class="icon32" id="icon-upload"><br></div>
<h2>Import External Images</h2>
<?php
if ( isset( $_POST['action'] ) && $_POST['action'] == 'backcatalog' ) {
echo '<div id="message" class="updated fade" style="background-color:rgb(255,251,204); overflow: hidden; margin: 0 0 10px 0">';
external_image_backcatalog();
echo '</div>';
} elseif ( isset( $_POST['action'] ) && $_POST['action'] == 'update' ) {
update_option('external_image_whichimgs', esc_html( $_POST['external_image_whichimgs'] ) );
update_option('external_image_excludes', esc_html( $_POST['external_image_excludes'] ) );
echo '<div id="message" class="updated fade" style="background-color:rgb(255,251,204);"><p>Settings updated.</p></div>';
}
?>
<form name="external_image-options" method="post" action="" style="width:300px; padding: 0 20px; margin: 20px 20px 0 0 ; float: left; background: #f6f6f6; border: 1px solid #e5e5e5; ">
<h2 style="margin-top: 0px;">Options</h2>
<?php settings_fields('external_image'); ?>
<h3>Which external IMG links to process:</h3>
<p>By default, all external images are processed. This can be set to ignore images from certain domains.</p>
<p>
<label for="myradio1">
<input id="myradio1" type="radio" name="external_image_whichimgs" value="all" <?php echo (get_option('external_image_whichimgs')!='exclude'?'checked="checked"':''); ?> /> All images
</label>
</p>
<p>
<label for="myradio2">
<input id="myradio2" type="radio" name="external_image_whichimgs" value="exclude" <?php echo (get_option('external_image_whichimgs')=='exclude'?'checked="checked"':''); ?> /> Exclude images by domain
</label>
</p>
<p><label for="myradio2">Domains to exclude (comma separated):</label></p>
<p class="howto">Example: smugmug.com, flickr.com, picassa.com, photobucket.com, facebook.com</p>
<p><textarea style="height:90px; width: 294px;"id="external_image_excludes" name="external_image_excludes"><?php echo ( get_option('external_image_excludes') != '' ? get_option('external_image_excludes') : '' ); ?></textarea></p>
<div class="submit">
<input type="hidden" name="external_image_update" value="action" />
<input type="submit" name="submit" class="button-primary" value="Save Changes" />
</div>
</form>
<div id="import_all_images" style="float:left; margin:0px; display:inline; width:500px; ">
<h2 style="margin-top: 0px;">Process all posts</h2>
<?php
$posts = get_posts( array( 'numberposts'=>-1 ) );
$count = 0;
foreach( $posts as $this_post ) {
$images = external_image_get_img_tags ($this_post->ID);
if( !empty( $images ) ) {
$posts_to_fix[$count]['title'] = $this_post->post_title;
$posts_to_fix[$count]['images'] = $images;
$posts_to_fix[$count]['id'] = $this_post->ID;
}
$count++;
}
$import = '<div style="float:left; margin: 0 10px;">';
$import .= '<p class="submit" id="bulk-resize-examine-button">';
$import .= '<button class="button-primary" onclick="external_images_import_images();">Import Images Now</button>';
$import .= '</p>';
$import .= '<div id="import_posts" style="display:none padding:25px 10px 10px 80px;"></div>';
$import .= '<div id="import_results" style="display:none"></div>';
$import .= '</div>';
$html = '';
if ( is_array( $posts_to_fix ) ) {
$html .= '<p class="howto">Please note that this can take a long time for sites with a lot of posts. You can also edit each post and import images one post at a time.</p>';
$html .= '<p class="howto">We will process up to 50 posts at a time. You should <a class="button-secondary" href="'.admin_url('upload.php?page=external_image').'">refresh the page</a> when done to check if you have more than 50 posts.</p>';
$html .= '<p class="howto">Only '.EXTERNAL_IMAGES_MAX_COUNT.' images per post will be imported at a time to keep things from taking too long. For posts with more than that, they will get added back into the list when you refresh or come back and try again.</p>';
$html .= $import;
$html .= '<div id="posts_list" style="padding: 0 5px; margin: 0px; clear:both; ">';
$html .= '<h4>Here is a look at posts that contain external Images:</h4>';
$html .= '<ul style="padding: 0 0 0 5px;">';
foreach( $posts_to_fix as $post_to_fix ) {
$html .= '<li>"<strong>'.$post_to_fix['title'].'</strong>" - ' .count($post_to_fix['images']). ' images. <a href="'.admin_url('post.php?post='.$post_to_fix['id'].'&action=edit').'">Edit Post</a>.</li>';
}
$html .= '</ul>';
$html .= '</div>';
} else {
$html .= "<p>We didn't find any external images to import. You're all set!</p>";
}
$html .= '</div>';
echo $html;
?>
</div>
</div>
<?php
}