Skip to content

Commit 0f08494

Browse files
fix: preserve HTML tags in content area
1 parent 458d958 commit 0f08494

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

css/settings.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,3 +2971,7 @@ button.feedzy-action-button {
29712971
font-size: 12px;
29722972
padding: 4px 12px;
29732973
}
2974+
.tagify__tag .feedzy-custom-tag {
2975+
cursor: auto;
2976+
line-height: 1.5;
2977+
}

includes/admin/feedzy-rss-feeds-actions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ public function action_process() {
315315
return $this->generate_image();
316316
case 'modify_links':
317317
return $this->modify_links();
318+
case 'custom_html':
319+
return $this->custom_html();
318320
default:
319321
return $this->default_content();
320322
}
@@ -619,5 +621,14 @@ private function custom_field() {
619621
}
620622
return $this->default_value;
621623
}
624+
625+
/**
626+
* Get Custom HTML tag.
627+
*
628+
* @return string
629+
*/
630+
private function custom_html() {
631+
return $this->current_job->tag;
632+
}
622633
}
623634
}

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
643643
}
644644
} else {
645645
if ( 'import_post_content' === $key ) {
646+
$val = escape_html_to_tag( $val );
646647
$val = feedzy_custom_tag_escape( $val );
647648
} elseif ( 'default_thumbnail_id' === $key && ! empty( $val ) ) {
648649
$val = explode( ',', $val );

includes/feedzy-rss-feeds-feed-tweaks.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,72 @@ function feedzy_show_review_notice() {
672672

673673
return $imported_posts->have_posts();
674674
}
675+
676+
/**
677+
* Escape the HTML tag and convert it to the tagify tag value.
678+
*
679+
* @param string $content Content.
680+
* @return string
681+
*/
682+
function escape_html_to_tag( $content ) {
683+
684+
$protected_blocks = array();
685+
$placeholder_tpl = '__TAG_%d__';
686+
687+
$content = preg_replace_callback(
688+
'/\[\[\{[\s\S]*?\}\]\]/',
689+
function ( $value ) use ( &$protected_blocks, $placeholder_tpl ) {
690+
$index = count( $protected_blocks );
691+
$protected_blocks[] = $value[0];
692+
return sprintf( $placeholder_tpl, $index );
693+
},
694+
$content
695+
);
696+
697+
$base_content = $content;
698+
$html_tags = array();
699+
$converted_value = '';
700+
701+
if ( preg_match_all( '/<[^>]+>[\s\S]*?<\/[^>]+>/', $content, $matches ) ) {
702+
foreach ( $matches[0] as $match ) {
703+
704+
$base_content = str_replace( $match, '', $base_content );
705+
$html_tags[] = array(
706+
'value' => rawurlencode(
707+
wp_json_encode(
708+
array(
709+
array(
710+
'id' => 'custom_html',
711+
'tag' => $match,
712+
),
713+
)
714+
)
715+
),
716+
);
717+
}
718+
}
719+
720+
foreach ( $protected_blocks as $i => $block ) {
721+
$base_content = str_replace(
722+
sprintf( $placeholder_tpl, $i ),
723+
$block,
724+
$base_content
725+
);
726+
}
727+
728+
foreach ( $html_tags as $tag ) {
729+
$converted_value .= wp_json_encode(
730+
array(
731+
array(
732+
$tag,
733+
),
734+
)
735+
);
736+
}
737+
738+
if ( ! empty( $converted_value ) ) {
739+
$base_content .= $converted_value;
740+
}
741+
742+
return $base_content;
743+
}

includes/views/js/import-metabox-edit.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,13 +869,23 @@
869869
typeof tagData.value === 'string' &&
870870
decodeTagData !== tagData.value;
871871
let tagLabel = tagData.value;
872+
let isCustomHTML = false;
872873
if (isEncoded) {
873874
decodeTagData = JSON.parse(decodeTagData);
874875
decodeTagData = decodeTagData[0] || {};
875876
tagLabel = decodeTagData.tag.replaceAll('_', ' ');
877+
isCustomHTML = 'custom_html' === decodeTagData.id;
876878
tagData['data-actions'] = tagData.value;
877879
tagData['data-field_id'] = 'fz-content-action-tags';
878880
}
881+
if ( isCustomHTML ) {
882+
tagLabel = escapeText(tagLabel);
883+
return `
884+
<tag spellcheck="false" class='tagify__tag'>
885+
<span id="editable" class="feedzy-custom-tag tagify__tag-text" contenteditable='true'>${tagLabel}</span>
886+
</tag>`;
887+
}
888+
879889
return `
880890
<tag title='${tagLabel}' contenteditable='false' spellcheck="false" class='tagify__tag ${isEncoded ? 'fz-content-action' : ''}'>
881891
<x title='remove tag' class='tagify__tag__removeBtn'></x>
@@ -1050,6 +1060,29 @@
10501060
url.searchParams.delete('imported');
10511061
history.replaceState(history.state, '', url.href);
10521062
}
1063+
1064+
const tagify = mixContent.focus().data('tagify');
1065+
1066+
$(document).on('input', '.feedzy-post-content span', function () {
1067+
const tagElms = $(this).find('tag');
1068+
tagElms.each((i, ele) => {
1069+
if ( $(ele).find('.feedzy-custom-tag').length ) {
1070+
const newValue = ele.innerText;
1071+
const data = tagify.getSetTagData(ele);
1072+
1073+
let decodeTagData = decodeURIComponent(data.value);
1074+
decodeTagData = JSON.parse(decodeTagData);
1075+
decodeTagData = decodeTagData[0] || {};
1076+
decodeTagData.tag = newValue;
1077+
1078+
let encodeTagData = [ decodeTagData ];
1079+
encodeTagData = JSON.stringify(encodeTagData, null, 0)
1080+
data.value = encodeURIComponent(encodeTagData);
1081+
1082+
tagify.updateValueByDOMTags();
1083+
}
1084+
});
1085+
});
10531086
}
10541087

10551088
function initSummary() {
@@ -1539,3 +1572,16 @@ function initNewPostActions() {
15391572
postTitle.select();
15401573
}
15411574
}
1575+
1576+
/**
1577+
* Escape the text.
1578+
*/
1579+
function escapeText( label ) {
1580+
return label
1581+
.trim()
1582+
.replace(/&/g, '&amp;')
1583+
.replace(/</g, '&lt;')
1584+
.replace(/>/g, '&gt;')
1585+
.replace(/"/g, '&quot;')
1586+
.replace(/'/g, '&#039;')
1587+
}

0 commit comments

Comments
 (0)