From cf322b6dca1820a6204b70abe383b9e99fd92969 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 19 Oct 2018 11:04:30 -0400 Subject: [PATCH] Improvements to FieldtypeTextarea::importValue() to improve accuracy of linked asset URLs on pages imported from one site to another where the root URL has changed (subdirectory vs. non subdirectory), as well as better handling asset URLs where the link included a hostname --- .../Fieldtype/FieldtypeTextarea.module | 62 ++++++++++++++++++- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeTextarea.module b/wire/modules/Fieldtype/FieldtypeTextarea.module index aaed4e19..6e4de955 100644 --- a/wire/modules/Fieldtype/FieldtypeTextarea.module +++ b/wire/modules/Fieldtype/FieldtypeTextarea.module @@ -366,10 +366,66 @@ class FieldtypeTextarea extends FieldtypeText { */ public function ___importValue(Page $page, Field $field, $value, array $options = array()) { $value = parent::___importValue($page, $field, $value, $options); - $originalID = (int) $page->get('_importOriginalID'); - if($originalID && $page->id && strpos($value, "/$originalID/")) { - $value = str_replace("/assets/files/$originalID/", "/assets/files/$page->id/", $value); + + // update changed IDs represented in asset paths + if(strpos($value, '/assets/files/') !== false) { + $originalID = (int) $page->get('_importOriginalID'); + if($originalID && $page->id && strpos($value, "/$originalID/")) { + $value = str_replace("/assets/files/$originalID/", "/assets/files/$page->id/", $value); + } } + + $contentType = $field->get('contentType'); + if($contentType == self::contentTypeHTML || $contentType == self::contentTypeImageHTML) { + $value = $this->importValueHTML($value, $options); + } + + return $value; + } + + /** + * Helper to importValue function for HTML-specific content + * + * This primarily updates references to export-site URLs to the current site + * + * @param string $value + * @param array $options + * @return string + * + */ + protected function importValueHTML($value, array $options) { + // update changed root URLs in href or src attributes + $config = $this->wire('config'); + $url = $config->urls->root; + $host = $config->httpHost; + $_url = isset($options['originalRootUrl']) ? $options['originalRootUrl'] : $url; // original URL + $_host = isset($options['originalHost']) ? $options['originalHost'] : $host; // original host + + if($_url === $url && $_host === $host) return $value; + + $findReplace = array(); + $href = 'href="'; + $src = 'src="'; + + if($_host != $host) { + $schemes = array('http://', 'https://'); + foreach($schemes as $scheme) { + $findReplace[$href . $scheme . $_host . '/'] = $href . '/'; + $findReplace[$href . $scheme . $_host . '/'] = $href . '/'; + } + } + + if($_url != $url) { + $findReplace[$href . $_url] = $href . $url; + $findReplace[$src . $_url] = $src . $url; + } + + foreach($findReplace as $find => $replace) { + if($find === $replace) continue; + if(strpos($value, $find) === false) continue; + $value = preg_replace('!(\s)' . $find . '!', '$1' . $replace, $value); + } + return $value; }