diff --git a/e107-importer.php b/e107-importer.php index 4b853fe..0f33bdb 100644 --- a/e107-importer.php +++ b/e107-importer.php @@ -157,6 +157,13 @@ function link_attachment_to_post($attachment_id, $post_id) { } + function get_attachment_id_from_url($url) { + global $wpdb; + $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$url'"; + return $wpdb->get_var($query); + } + + // Generic code to initialize the e107 context function inite107Context() { /* Some part of the code below is copy of (and/or inspired by) code from the e107 project, licensed @@ -1267,7 +1274,11 @@ function importImages($html_content, $content_id, $content_type, $local_only = F else $post_id = $content_id; - foreach ($this->extract_html_tags($html_content, 'img', array('http', 'https')) as $tag) { + // Extract image tags from the HTML + $img_tag_list = $this->extract_html_tags($html_content, 'img', array('http', 'https')); + + // Upload images and update the mapping + foreach ($img_tag_list as $tag) { $img_tag = $tag['tag_string']; $img_attrs = $tag['attribute_string']; $img_url = $tag['attributes']['src']; @@ -1280,67 +1291,90 @@ function importImages($html_content, $content_id, $content_type, $local_only = F if (!preg_match('/^https?:\/\//i', $img_url)) $img_url = SITEURL.$img_url; - // If the image was already uploaded, use the previous upload, else remotely upload it - if (array_key_exists($img_url, $this->image_mapping)) { - $attachment_id = $this->image_mapping[$img_url]; - - // Update the link between attachment and the post - // This ends with an uploaded image attached to the post the image was initially used in when it was both used in comments and posts - if ($content_type != 'comment') - link_attachment_to_post($attachment_id, $post_id); + // If the image was already uploaded, skip it + if (array_key_exists($img_url, $this->image_mapping)) + continue; - } else { - // Only import files from authorized domains - $domain_ok = True; - // TODO: use parse_url() for better comparison - if ($allowed_domains && is_array($allowed_domains) && sizeof($allowed_domains) > 0) { - $domain_ok = False; - foreach ($allowed_domains as $domain) { - $domain = $this->deleteTrailingChar($domain, '/'); - if (substr($img_url, 0, strlen($domain)) == $domain) { - $domain_ok = True; - break; - } - } - } - if (!$domain_ok) - continue; - - // Get image description from the alt or title attribute - $img_desc = ''; - foreach (array('alt', 'title') as $desc_tag) - if (array_key_exists($desc_tag, $tag['attributes']) and !empty($tag['attributes'][$desc_tag])) { - $img_desc = $tag['attributes'][$desc_tag]; + // Only import files from authorized domains + $domain_ok = True; + // TODO: use parse_url() for better comparison + if ($allowed_domains && is_array($allowed_domains) && sizeof($allowed_domains) > 0) { + $domain_ok = False; + foreach ($allowed_domains as $domain) { + $domain = $this->deleteTrailingChar($domain, '/'); + if (substr($img_url, 0, strlen($domain)) == $domain) { + $domain_ok = True; break; } + } + } + if (!$domain_ok) + continue; - // URLs with spaces are not considered valid by WordPress (see: http://core.trac.wordpress.org/ticket/16330#comment:5 ) - // Replace spaces by their percent-encoding equivalent - //$img_url = "http://home.nordnet.fr/francois.jankowski/pochette avant thumb.jpg"; - $img_fixed_url = str_replace(' ', '%20', html_entity_decode($img_url)); - // Download remote file and attach it to the post - $new_tag = media_sideload_image($img_fixed_url, $post_id, $img_desc); - if (is_wp_error($new_tag)) { - ?> -
%s
. Here is the error message:', 'e107-importer'), $img_fixed_url, htmlspecialchars($img_tag)); ?>%s
. Here is the error message:', 'e107-importer'), $img_fixed_url, htmlspecialchars($img_tag)); ?>