From 5be8af429718505c5afe50dd053c7b24424fe44a Mon Sep 17 00:00:00 2001 From: Filip Penkava Date: Fri, 21 Apr 2017 09:26:45 +0200 Subject: [PATCH] WSP-63, WSP-37, WSP-41 --- autoload.php | 26 +++++++++---- superdeskPublisher.php | 85 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 96 insertions(+), 15 deletions(-) diff --git a/autoload.php b/autoload.php index 82b0aa4..c630018 100644 --- a/autoload.php +++ b/autoload.php @@ -14,12 +14,12 @@ if ($obj['type'] == 'text') { $settings = get_option('superdesk_settings'); if ($obj['pubstatus'] == 'usable') { - $content = $obj['description_html'] . "" . $obj['body_html']; - if (!empty($obj['located'])) { - $content = substr($obj['description_html'], strpos($obj['description_html'], '>') + 1, strlen($obj['description_html'])) . "" . $obj['body_html']; - - $content = '

' . wp_strip_all_tags($obj['located']) . $settings['separator-located'] . $content; + $content = $obj['description_html'] . ""; + $content .= '

' . wp_strip_all_tags($obj['located']) . $settings['separator-located']; + $content .= mb_substr($obj['body_html'], mb_strpos($obj['body_html'], '>') + 1, mb_strlen($obj['body_html'])); + } else { + $content = $obj['description_html'] . "" . $obj['body_html']; } /* if ($settings['display-copyright'] == "on" && isset($obj['associations']['featuremedia']['copyrightnotice'])) { @@ -121,8 +121,9 @@ if ($obj['type'] == 'text') { $author_id = 0; } - if ($settings['download-images'] && $settings['download-images'] == 'on') { - $content = embed_images($content); + $image = null; + if (isset($settings['download-images']) && $settings['download-images'] === 'on') { + $content = embed_images($content, $image); } $sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'"); @@ -222,6 +223,17 @@ if ($obj['type'] == 'text') { $alt = (!empty($obj['associations']['featuremedia']['body_text'])) ? wp_strip_all_tags($obj['associations']['featuremedia']['body_text']) : ''; saveAttachment($obj['associations']['featuremedia'], $post_ID, $caption, $alt); } + } else if ($image !== null) { + $filenameQ = explode("/", $image->src); + $filename = $filenameQ[count($filenameQ) - 1]; + + $fileExist = $wpdb->get_row("SELECT meta_id, post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE '%" . wp_strip_all_tags($filename) . "'"); + + if ($fileExist) { + set_post_thumbnail($post_ID, $fileExist->post_id); + } else { + savePicture($image->src, $post_ID, $image->oldSrc, $obj['associations']); + } } } elseif ($obj['pubstatus'] == 'canceled') { /* remove article */ diff --git a/superdeskPublisher.php b/superdeskPublisher.php index ae429c7..99bfd88 100644 --- a/superdeskPublisher.php +++ b/superdeskPublisher.php @@ -109,6 +109,52 @@ function saveAttachment($picture, $post_ID, $caption, $alt) { update_post_meta($attach_id, '_wp_attachment_image_alt', wp_slash($alt)); } +function savePicture($localPath, $postId, $oldSrc, $associations) { + $filenameQ = explode("/", $localPath); + $filename = $filenameQ[count($filenameQ) - 1]; + + $name = null; + $mimeType = null; + foreach ($associations as $key => $value) { + var_dump($value); + if (isset($value['renditions'])) { + foreach ($value['renditions'] as $value2) { + if ($value2['href'] === $oldSrc) { + $name = $key; + $mimeType = $value2['mimetype']; + break 2; + } + } + } + } + if ($name == null) + return; + + $caption = generate_caption_image($associations[$name]); + $alt = (!empty($associations[$name]['body_text'])) ? wp_strip_all_tags($associations[$name]['body_text']) : ''; + + $attachment = array( + 'guid' => $localPath, + 'post_mime_type' => $mimeType, + 'post_title' => $caption, + 'post_content' => '', + 'post_excerpt' => $caption, + 'post_status' => 'inherit' + ); + + + $attach_id = wp_insert_attachment($attachment, date("Y") . "/" . date("m") . "/" . $filename, $postId); + + require_once( ABSPATH . 'wp-admin/includes/image.php' ); + + $attach_data = wp_generate_attachment_metadata($attach_id, wp_upload_dir()['path'] . "/" . $filename); + + wp_update_attachment_metadata($attach_id, $attach_data); + set_post_thumbnail($postId, $attach_id); + + update_post_meta($attach_id, '_wp_attachment_image_alt', wp_slash($alt)); +} + function custom_wpkses_post_tags($tags, $context) { if ('post' === $context) { $tags['iframe'] = array( @@ -126,23 +172,23 @@ function generate_caption_image($media) { $caption = ''; $settings = get_option('superdesk_settings'); if (!empty(trim($media['description_text']))) { - $caption.= wp_strip_all_tags($media['description_text']); + $caption .= wp_strip_all_tags($media['description_text']); } if (!empty(trim($media['byline']))) { if (!empty($caption)) { - $caption.=' '; + $caption .= ' '; } - $caption.= $settings['separator-caption-image'] . ': ' . wp_strip_all_tags($media['byline']); + $caption .= (empty($settings['separator-caption-image']) ? ':' : $settings['separator-caption-image']) . ' ' . wp_strip_all_tags($media['byline']); } if (!empty(trim($media['copyrightholder'])) && $settings['copyrightholder-image'] == 'on') { - $caption.= ' / ' . wp_strip_all_tags($media['copyrightholder']); + $caption .= ' / ' . wp_strip_all_tags($media['copyrightholder']); } if (!empty(trim($media['copyrightnotice'])) && $settings['copyrightnotice-image'] == 'on') { - $caption.= ' ' . wp_strip_all_tags($media['copyrightnotice']); + $caption .= ' ' . wp_strip_all_tags($media['copyrightnotice']); } return $caption; @@ -150,12 +196,28 @@ function generate_caption_image($media) { function embed_src($src) { $filename = sha1($src); - + saveFile($src, wp_upload_dir()['path'] . "/" . $filename); return wp_upload_dir()['url'] . "/" . $filename; } -function embed_images($html) { +class Image { + + public $src, $oldSrc; + + public function __construct(array $attrs, $oldSrc) { + $this->src = $attrs['src']; + $this->oldSrc = $oldSrc; + stripQuotes($this->src); + } + +} + +function stripQuotes(&$value) { + $value = mb_substr($value, 1, mb_strlen($value) - 2); +} + +function embed_images($html, &$image) { $result = array(); preg_match_all('/]+>/i', $html, $result); if (count($result) > 0) { @@ -172,15 +234,22 @@ function embed_images($html) { foreach ($img as $htmlTag => $src) { $attrs = array(); if (isset($src[1], $src[2])) { + $oldSrc = ''; foreach ($src[1] as $key => $attr) { $value = $src[2][$key]; if ($attr === "src") { - $value = mb_substr($value, 1, mb_strlen($value) - 2); + stripQuotes($value); + $oldSrc = $value; $value = '"' . embed_src($value) . '"'; } $attrs[$attr] = $value; } + + if ($image === null) { + $image = new Image($attrs, $oldSrc); + } + $newHtmlTag = " $attrValue) { $newHtmlTag .= " " . $attrName . "=" . $attrValue;