WSP-63, WSP-37, WSP-41

This commit is contained in:
Filip Penkava
2017-04-21 09:26:45 +02:00
parent 10b66659c7
commit 5be8af4297
2 changed files with 96 additions and 15 deletions

View File

@@ -14,12 +14,12 @@ if ($obj['type'] == 'text') {
$settings = get_option('superdesk_settings'); $settings = get_option('superdesk_settings');
if ($obj['pubstatus'] == 'usable') { if ($obj['pubstatus'] == 'usable') {
$content = $obj['description_html'] . "<!--more-->" . $obj['body_html'];
if (!empty($obj['located'])) { if (!empty($obj['located'])) {
$content = substr($obj['description_html'], strpos($obj['description_html'], '>') + 1, strlen($obj['description_html'])) . "<!--more-->" . $obj['body_html']; $content = $obj['description_html'] . "<!--more-->";
$content .= '<p>' . wp_strip_all_tags($obj['located']) . $settings['separator-located'];
$content = '<p>' . wp_strip_all_tags($obj['located']) . $settings['separator-located'] . $content; $content .= mb_substr($obj['body_html'], mb_strpos($obj['body_html'], '>') + 1, mb_strlen($obj['body_html']));
} else {
$content = $obj['description_html'] . "<!--more-->" . $obj['body_html'];
} }
/* if ($settings['display-copyright'] == "on" && isset($obj['associations']['featuremedia']['copyrightnotice'])) { /* if ($settings['display-copyright'] == "on" && isset($obj['associations']['featuremedia']['copyrightnotice'])) {
@@ -121,8 +121,9 @@ if ($obj['type'] == 'text') {
$author_id = 0; $author_id = 0;
} }
if ($settings['download-images'] && $settings['download-images'] == 'on') { $image = null;
$content = embed_images($content); 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 . "'"); $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']) : ''; $alt = (!empty($obj['associations']['featuremedia']['body_text'])) ? wp_strip_all_tags($obj['associations']['featuremedia']['body_text']) : '';
saveAttachment($obj['associations']['featuremedia'], $post_ID, $caption, $alt); 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') { } elseif ($obj['pubstatus'] == 'canceled') {
/* remove article */ /* remove article */

View File

@@ -109,6 +109,52 @@ function saveAttachment($picture, $post_ID, $caption, $alt) {
update_post_meta($attach_id, '_wp_attachment_image_alt', wp_slash($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) { function custom_wpkses_post_tags($tags, $context) {
if ('post' === $context) { if ('post' === $context) {
$tags['iframe'] = array( $tags['iframe'] = array(
@@ -126,23 +172,23 @@ function generate_caption_image($media) {
$caption = ''; $caption = '';
$settings = get_option('superdesk_settings'); $settings = get_option('superdesk_settings');
if (!empty(trim($media['description_text']))) { 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(trim($media['byline']))) {
if (!empty($caption)) { 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') { 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') { 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; return $caption;
@@ -155,7 +201,23 @@ function embed_src($src) {
return wp_upload_dir()['url'] . "/" . $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(); $result = array();
preg_match_all('/<img[^>]+>/i', $html, $result); preg_match_all('/<img[^>]+>/i', $html, $result);
if (count($result) > 0) { if (count($result) > 0) {
@@ -172,15 +234,22 @@ function embed_images($html) {
foreach ($img as $htmlTag => $src) { foreach ($img as $htmlTag => $src) {
$attrs = array(); $attrs = array();
if (isset($src[1], $src[2])) { if (isset($src[1], $src[2])) {
$oldSrc = '';
foreach ($src[1] as $key => $attr) { foreach ($src[1] as $key => $attr) {
$value = $src[2][$key]; $value = $src[2][$key];
if ($attr === "src") { if ($attr === "src") {
$value = mb_substr($value, 1, mb_strlen($value) - 2); stripQuotes($value);
$oldSrc = $value;
$value = '"' . embed_src($value) . '"'; $value = '"' . embed_src($value) . '"';
} }
$attrs[$attr] = $value; $attrs[$attr] = $value;
} }
if ($image === null) {
$image = new Image($attrs, $oldSrc);
}
$newHtmlTag = "<img"; $newHtmlTag = "<img";
foreach ($attrs as $attrName => $attrValue) { foreach ($attrs as $attrName => $attrValue) {
$newHtmlTag .= " " . $attrName . "=" . $attrValue; $newHtmlTag .= " " . $attrName . "=" . $attrValue;