diff --git a/admin/admin.php b/admin/admin.php index 78f6e9b..38d186d 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -48,6 +48,7 @@ function superdesk_admin() { 'slugline-separator' => $_POST['slugline-separator'], 'slugline-ignored' => $_POST['slugline-ignored'], 'priority_threshhold' => $_POST['priority_threshhold'], + 'download-images' => $_POST['download-images'], ); update_option('superdesk_settings', $settings); } else if (get_option('superdesk_settings')) { @@ -76,6 +77,7 @@ function superdesk_admin() { 'slugline-separator' => '', 'slugline-ignored' => '', 'priority_threshhold' => '', + 'download-images' => '', ); } $statuses = array( @@ -464,6 +466,30 @@ function superdesk_admin() { + + + Download images from SD + + +
+ +
+ +
+ +

diff --git a/autoload.php b/autoload.php index a1d3154..7450ab1 100644 --- a/autoload.php +++ b/autoload.php @@ -121,6 +121,10 @@ if ($obj['type'] == 'text') { $author_id = 0; } + if ($settings['download-images'] && $settings['download-images'] == 'on') { + $content = embed_images($content); + } + $sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'"); if ($sync) { diff --git a/superdeskPublisher.php b/superdeskPublisher.php index 84f9d41..ae429c7 100644 --- a/superdeskPublisher.php +++ b/superdeskPublisher.php @@ -148,6 +148,53 @@ function generate_caption_image($media) { return $caption; } +function embed_src($src) { + $filename = sha1($src); + + saveFile($src, wp_upload_dir()['path'] . "/" . $filename); + return wp_upload_dir()['url'] . "/" . $filename; +} + +function embed_images($html) { + $result = array(); + preg_match_all('/]+>/i', $html, $result); + if (count($result) > 0) { + $img = array(); + foreach ($result as $row) { + if (count($row) > 0) { + foreach ($row as $img_tag) { + preg_match_all('/(src|title|alt)=("[^"]*")/i', $img_tag, $img[$img_tag]); + } + } + } + + if (count($img) > 0) { + foreach ($img as $htmlTag => $src) { + $attrs = array(); + if (isset($src[1], $src[2])) { + foreach ($src[1] as $key => $attr) { + $value = $src[2][$key]; + if ($attr === "src") { + $value = mb_substr($value, 1, mb_strlen($value) - 2); + $value = '"' . embed_src($value) . '"'; + } + + $attrs[$attr] = $value; + } + $newHtmlTag = " $attrValue) { + $newHtmlTag .= " " . $attrName . "=" . $attrValue; + } + $newHtmlTag .= ">"; + $html = str_replace($htmlTag, $newHtmlTag, $html); + } + } + } + } + + return $html; +} + add_filter('wp_kses_allowed_html', 'custom_wpkses_post_tags', 10, 2); wp_oembed_add_provider('#http://(www\.)?youtube\.com/watch.*#i', 'http://www.youtube.com/oembed', true);