-
Notifications
You must be signed in to change notification settings - Fork 1
/
archive-remote-images.php
745 lines (543 loc) · 23.4 KB
/
archive-remote-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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<?php
/*
* Plugin Name: Archive Remote Images
* Plugin URI: https://wordpress.org/plugins/archive-remote-images
* Description: Archive Remote Images allows you to scan a post to fetch remote images; then updates its content automatically.
* Author: Kason Zhao, G.Breant, kraoc
* Version: 1.0.8
* Author URI: https://profiles.wordpress.org/grosbouff/
* License: GPL2+
* Text Domain: ari
* Domain Path: /languages/
*/
class ArchiveRemoteImages{
/** Version ***************************************************************/
/**
* @public string plugin version
*/
public $version = '1.0.8';
/** Paths *****************************************************************/
public $file = '';
/**
* @public string Basename of the plugin directory
*/
public $basename = '';
/**
* @public string Prefix for the plugin
*/
public $prefix = '';
/**
* @public string Absolute path to the plugin directory
*/
public $plugin_dir = '';
/**
* @public string Absolute path to the plugin directory
*/
public $plugin_url = '';
/**
* class relative to options and options page.
*/
var $options_class;
/**
* @var The one true Instance
*/
private static $instance;
/**
* Main Instance
*
* Insures that only one instance of the plugin exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @staticvar array $instance
* @uses ::setup_globals() Setup the globals needed
* @uses ::includes() Include the required files
* @uses ::setup_actions() Setup the hooks and actions
* @return The instance
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new ArchiveRemoteImages;
self::$instance->includes();
self::$instance->setup_globals();
self::$instance->setup_actions();
}
return self::$instance;
}
/**
* A dummy constructor to prevent the plugin from being loaded more than once.
*/
private function __construct() { /* Do nothing here */ }
function includes(){
require( $this->plugin_dir . 'ari-settings.php');
}
function setup_globals(){
global $wpdb;
/** Paths *************************************************************/
$this->file = __FILE__;
$this->basename = plugin_basename( $this->file );
$this->prefix = 'ari';
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url ( $this->file );
$this->options_class = new AriSettings();
}
function setup_actions(){
//localization
add_action('init', array($this, 'load_plugin_textdomain'));
//scripts & styles
add_action( 'admin_enqueue_scripts', array( $this, 'scripts_styles' ) );
//metabox
add_action( 'add_meta_boxes', array( $this, 'metabox_init' ) );
//post processing
add_action( 'save_post', array( $this, 'save_archiving_status' ) );
add_action( 'save_post', array( $this, 'save_post_images' ),10, 2);
add_filter('ari_get_remote_image_url', array( $this, 'get_url_from_special_domain' ));
add_filter('ari_get_image_attributes',array(&$this,'image_attributes_class_id'),10,2);
}
public function load_plugin_textdomain(){
load_plugin_textdomain($this->basename, FALSE, $this->plugin_dir.'/languages/');
}
public function get_setting($slug){
$options = self::get_settings();
if (array_key_exists($slug, $options)) return $options[$slug];
}
public function get_settings(){
return $this->options_class->options;
}
////
public function scripts_styles() {
//wp_enqueue_style( 'ari-admin', $this->plugin_url .'_inc/css/ari-admin.css', array(), $this->version );
}
/**
* This function registers a metabox with the callback archive_remote_images_meta_box_callback.
* For reference: add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
*
*/
function metabox_init(){
//capabilities
if (!current_user_can('edit_post', get_the_ID())) return false;
if (!current_user_can('upload_files', get_the_ID())) return false;
$post_types = $this->options_class->get_allowed_post_types();
$ignored = self::get_setting('ignored_post_type');
foreach ($post_types as $post_type){
if (($ignored) && (in_array($post_type,$ignored))) continue;
add_meta_box('ari', __('Archive Remote Images','ari'), array(&$this,'metabox_content'), $post_type, 'side', 'high');
}
}
function metabox_content($post){
$checked = self::get_setting('default_checked');
if (self::get_setting('remember_status')){
if ($meta_value = get_post_meta($post->ID, 'ari_enabled', TRUE)){
$checked = ($meta_value == 'yes');
}
}
?>
<div id="post-img-select">
<?php
if ($count = self::count_archived_attachments(get_the_ID())){
?>
<small>
<?php
printf(
_n(
'<strong>1</strong> media has been downloaded for this post using Archive Remote Images !',
'Already <strong>%s</strong> medias have been downloaded for this post using Archive Remote Images !',
$count,
'ari' ),
$count
);
?>
</small>
<hr/>
<?php
}
?>
<input type="checkbox"value="on" <?php checked((bool)$checked); ?> id="ari-metabox-check" name="do_remote_archive"> <label for="ari-metabox-check"><?php _e('Download images locally','ari');?></label>
<?php wp_nonce_field($this->basename,'ari_form',false);?>
</p>
</div>
<?php
}
function save_archiving_status($post_id){
if (!self::get_setting('remember_status')) return $post_id;
//check save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = false;
if ( isset( $_POST[ 'ari_form' ]) && wp_verify_nonce( $_POST['ari_form'], $this->basename)) $is_valid_nonce=true;
if ($is_autosave || $is_revision || !$is_valid_nonce) return $post_id;
//capabilities
if (!current_user_can('edit_post', $post_id)) return $post_id;
if (!current_user_can('upload_files', $post_id)) return $post_id;
// OK, we're authenticated: we need to find and save the data
$checked = isset($_POST['do_remote_archive']) ? 'yes' : 'no';
ArchiveRemoteImages::debug_log(array('post_id'=>$post_id,'checked'=>$checked),"save post archiving status");
return update_post_meta($post_id, 'ari_enabled', $checked);
}
// retrieve images from content
function fetch_remote_images($doc){
$save_atts = array('src','alt','title');
$images = array();
$out = simplexml_import_dom($doc);
$img_el_all = $out -> xpath('//img');
foreach ($img_el_all as $img_el) {
$image = array();
foreach($img_el->attributes() as $att => $value) {
if (!in_array($att, $save_atts)) continue;
$value = (string)$value;
if (!$value) continue;
$image[$att] = $value;
}
$image = array_filter($image);
if (!array_key_exists('src', $image)) continue;
if (!self::is_absolute_url($image['src'])) continue; //is relative URL
if (self::is_local_image($image['src'])) continue; //is local image
$images[] = $image;
}
$images = array_filter($images);
return $images;
}
/*
* Archive images on while saving
*/
function save_post_images($post_id, $post){
global $wpdb;
//check save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
if ($is_revision) return $post_id;
if ($is_autosave) return $post_id;
//capabilities
if (!current_user_can('edit_post', $post_id)) return $post_id;
if (!current_user_can('upload_files', $post_id)) return $post_id;
//checkbox not checked
if (!isset($_POST['do_remote_archive'])) return $post_id;
//script time limit
if ($time_limit = self::get_setting('time_limit')){
set_time_limit($time_limit);
}
$new_post = clone $post;
//DOMDocument
libxml_use_internal_errors(true); //avoid errors like duplicate IDs
$doc = new DOMDocument( '1.0', get_bloginfo('charset') );
//try to fix bad HTML
$doc->recover = true;
//$doc->strictErrorChecking = false;
$html = mb_convert_encoding( $post->post_content, 'HTML-ENTITIES', 'UTF-8'); //https://stackoverflow.com/a/8218649
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_NOXMLDECL | LIBXML_NSCLEAN);
$doc->normalizeDocument();
//get images urls in post content
$images = self::fetch_remote_images($doc);
if (empty($images)) return $post_id;
ArchiveRemoteImages::debug_log(array('post_id'=>$post_id,'count'=>count($images)),"found images");
//hooks START (avoid infinite loops, disable revisions)
remove_action( 'save_post', array( $this, 'save_archiving_status' ) );
remove_action( 'save_post', array( $this, 'save_post_images' ),10, 2);
add_filter( 'wp_revisions_to_keep', array( $this, 'disable_post_revisions' ));
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'image_attributes_hook' ),10, 2);
foreach ((array)$images as $image){
$replaced = self::replace_single_image($doc,$image);
if ( !$replaced || is_wp_error($replaced) ) continue;
//update post
//is inside FOREACH so if the script breaks,
//successfully grabbed images still are replaced in the post content.
$new_post->post_content = $doc->saveHTML();
if ( $new_post->post_content === $post->post_content ) continue;
$success = wp_update_post( $new_post );
ArchiveRemoteImages::debug_log($post_id,"saved post");
}
//hooks STOP
add_action('save_post', array( $this, 'save_archiving_status' ));
add_action( 'save_post', array( $this, 'save_post_images' ),10, 2);
add_action('pre_post_update', 'wp_save_post_revision');// enable revisions again
remove_filter( 'wp_revisions_to_keep', array( $this, 'disable_post_revisions' ));
remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'image_attributes_hook' ),10, 2);
return $post_id;
}
function is_local_image($url){
$is_local = strpos($url, home_url());
return (bool)($is_local !== false);
if ($is_local !== false) {
return true;
}
}
/**
* Checks the URL is absolute
* @param type $url
*/
function is_absolute_url($url){
$parse = parse_url($url);
if (array_key_exists("host", $parse)) return true;
return false;
}
function is_local_server($url){
if ($_SERVER['REMOTE_ADDR']=='127.0.0.1') return true;
$parse = parse_url($url);
$host_names = explode(".", $parse['host']);
if (!isset($host_names[1])) return true; //no extension, we should be on a local server (like localhost)
return false;
}
/*
* Get domain (without subdomain like www.)
*/
function get_domain($url){
if (!self::is_absolute_url($url)) return false;
$parse = parse_url($url);
$host_names = explode(".", $parse['host']);
if (self::is_local_server($url)){
$domain = $parse['host'];
}else{
$domain = $host_names[count($host_names)-2] . "." . $host_names[count($host_names)-1];
}
return $domain;
}
/*
* Gets the allowed file extensions allowed for
* a specific mime ('image','video','audio',...)
* and an optional type ('jpeg','gif','mp4',...)
*/
function get_allowed_extensions($mime_check,$type_check=false){
$extensions = array();
$allowed = get_allowed_mime_types();
foreach ((array)$allowed as $ext_str => $mimetype_str) {
$mimetype = explode('/',$mimetype_str); //eg. 'image/jpeg'
$mime = $mimetype[0]; //'image'
$type = $mimetype[1]; //'jpeg'
if ( $mime!=$mime_check ) continue;
if (isset($type_check) && ( $type_check!=$type )) continue;
$mimetype_ext = explode('|',$ext_str);
$extensions = array_merge($mimetype_ext,$extensions);
}
return $extensions;
}
/*
* Gets the allowed file extensions allowed for images
*/
function get_allowed_image_extensions(){
return get_allowed_extensions('image');
}
/**
* TO FIX rename / give more informations on this function
* @param type $url
* @param type $image
* @return type
*/
function get_url_from_special_domain($url){
$domain = self::get_domain($url);
$check_domains = array(
'blogspot.com',
'blogger.com',
'ggpht.com',
'googleusercontent.com',
'gstatic.com'
);
if (in_array($domain,$check_domains)){
$response = wp_remote_request($url);
if (!is_wp_error($response)){
$my_body = wp_remote_retrieve_body($response);
if (strpos($my_body, 'src')) {
preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $my_body, $matches);
foreach ($matches[1] as $url):
$spisak = $url;
endforeach;
$url = $spisak;
}
}
}
return $url;
}
/*
* Used for disabling revisions temporary
*/
function disable_post_revisions($num){
return 0;
}
/*
* Runs a hook so we can filter the image attributes
*/
function image_attributes_hook($attr, $attachment){
return apply_filters('ari_get_image_attributes',$attr,$attachment);
}
/*
* Adds the class wp-image-ID
* Because it's easier to read when editing the code of the post content
*/
function image_attributes_class_id($attr, $attachment){
$attr['class'].= " wp-image-".$attachment->ID;
return $attr;
}
function get_image_title($image){
$title = '';
if (array_key_exists('title', $image)){
$title = $image['title'];
}elseif (array_key_exists('alt', $image)){
$title = $image['alt'];
}
return apply_filters('ari_get_image_title',$title);
}
private function get_media_by_remote_url($img_url){
$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => '_ari-url',
'value' => $img_url,
'compare' => '='
)
),
'posts_per_page' => 1
);
$query = new WP_Query($query_args);
if (!$query->have_posts()) return false;
$id = $query->posts[0]->ID;
return apply_filters("ari_get_media_by_remote_url",$id,$img_url);
}
private function create_local_image($image){
$image_url = $image['src'];
if ( $attachment_id = self::get_media_by_remote_url($image_url) ){ //this image url already has been uploaded
ArchiveRemoteImages::debug_log($attachment_id,"image already exists as attachment");
return $attachment_id;
}
//get image title
$img_title = self::get_image_title($image);
/*
media_sideload_image() do not returns the attachment ID.
hook and unhook a function to get over that.
it that function, we will save the source URL as post meta for the attachment.
The value will be $this->attachment_source, which is only used here.
This is kind of a hack, hope that media_sideload_image() will be able to return
The attachment ID in the future.
https:core.trac.wordpress.org/ticket/19629
*/
//START HACK
$this->attachment_source = $image_url;
add_action('add_attachment',array( $this, 'uploaded_image_save_source' ));
//filter that allows to update the file URL if needed (eg. depending of the domain)
$upload_url = apply_filters('ari_get_remote_image_url',$image_url);
$attachment_id = media_sideload_image($upload_url, $post->ID, $img_title,'id');
//STOP HACK
remove_action('add_attachment',array( $this, 'uploaded_image_save_source' )); //hook
$this->attachment_source = '';
if (is_wp_error($attachment_id)){
ArchiveRemoteImages::debug_log($attachment_id->get_error_message(),"error downloading image");
return $attachment_id;
}
ArchiveRemoteImages::debug_log($attachment_id,"created attachment");
}
/**
* Archive image from content
*/
function replace_single_image(&$doc,$image){
global $wpdb;
$attachment_id = null;
$image_url = $image['src'];
$image_size = self::get_setting('image_size');
$replacements = 0;
$attachment_id = $this->create_local_image($image);
if ( is_wp_error($attachment_id) ) return $attachment_id;
if ( !$attachment_id ){
ArchiveRemoteImages::debug_log($image_url,"missing media ID");
return new WP_Error( 'no_media_id','Missing media ID' );
}
$new_image_html = wp_get_attachment_image( $attachment_id, $image_size );
$new_image_html = apply_filters('ari_get_new_image_html',$new_image_html,$attachment_id);
if ( is_wp_error($new_image_html) || !$new_image_html ){
ArchiveRemoteImages::debug_log($new_image_html,"invalid image HTML");
return $new_image_html;
}
$imageTags = $doc->getElementsByTagName('img'); //get all images
$new_image_el = $doc->createDocumentFragment();
$new_image_el->appendXML($new_image_html);
foreach ($imageTags as $imageTag){
$imageTag_url = $imageTag->getAttribute('src');
if ($imageTag_url != $image_url) continue;
$parentNode = $imageTag->parentNode;
//replace <img> tag
if ( $parentNode->replaceChild($new_image_el, $imageTag) ){
$replacements++;
}
//if the parent tag of the image is a link to the (same) image,
//replace that link with a link to the uploaded image.
if (($parentNode->tagName == 'a') && (self::get_setting('replace_parent_link'))){
$link_src = $parentNode->getAttribute('href');
//link url and image source are the same
if ( $link_src == $image_url ){
$linked_image_url = self::get_linked_image_url($attachment_id);
$image_linked_size = self::get_setting('image_linked_size');
$new_linked_image_html = wp_get_attachment_image( $attachment_id, $image_linked_size );
$new_link_html = '<a href="'.$linked_image_url.'">'.$new_linked_image_html.'</a>';
$new_link_html = apply_filters('ari_get_new_link_html',$new_link_html,$attachment_id);
if ($new_link_html){
$new_link_el = $doc->createDocumentFragment();
$new_link_el->appendXML($new_link_html);
//replace <a> tag
if ( $parentNode->parentNode->replaceChild($new_link_el, $parentNode) ){
$replacements++;
}
}
}
}
}
if ($replacements){
ArchiveRemoteImages::debug_log(array('url'=>$image_url,'occurences'=>$replacements),"successfully replaced HTML for image");
}
return (bool)$replacements;
}
function get_linked_image_url($attachment_id){
$option = self::get_setting('image_linked_target');
switch ($option) {
case 'post': //attachment page
$url = get_attachment_link( $attachment_id );
break;
default : // media url
$url = wp_get_attachment_url( $attachment_id );
}
return apply_filters('ari_get_linked_image_url',$url, $attachment_id);
}
function uploaded_image_save_source($attachment_id){
$source = $this->attachment_source;
//add original URL to attachment, as meta
add_post_meta($attachment_id, '_ari-url',$source);
}
/**
* Count the number of medias already downloaded with Archive Remote Image
* @return int
*/
public function count_archived_attachments($post_id = false){
$query_args = array(
'post_type' => 'attachment',
'meta_key' => '_ari-url',
'posts_per_page' => -1
);
if ($post_id)
$query_args['post_parent'] = $post_id;
$meta_posts = get_posts( $query_args );
$meta_post_count = count( $meta_posts );
unset( $meta_posts);
return (int)$meta_post_count;
}
public static function debug_log($data,$title = null) {
if (WP_DEBUG_LOG !== true) return false;
$prefix = '[ari] ';
if($title) $prefix.=$title.': ';
if (is_array($data) || is_object($data)) {
$data = "\n" . json_encode($data,JSON_UNESCAPED_UNICODE);
}
error_log($prefix . $data);
}
}
/**
* The main function responsible for returning the one true Instance
* to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* @return The one true Instance
*/
function ari() {
return ArchiveRemoteImages::instance();
}
if (is_admin()){
ari();
}