@@ -9,71 +9,18 @@ class Fields
99{
1010 public static function transform (string $ html , string $ type = 'fields ' , string $ name = '' ): string
1111 {
12- $ html = self ::repeaters ($ html );
12+ $ html = Repeater::wrapDynamicContainers ($ html , $ name );
13+ $ html = Repeater::explicitRepeaters ($ html , $ name );
14+
1315 $ html = self ::replacePostMeta ($ html , $ type );
1416 $ html = self ::replaceGenerics ($ html , $ type );
1517 $ html = self ::replaceNodeValues ($ html , $ type , $ name );
1618 $ html = self ::wrapGradientOverlay ($ html );
1719 $ html = self ::formReplace ($ html );
1820 return $ html ;
1921 }
20- // Repeaters
21- private static function repeaters (string $ html ): string
22- {
23- $ dom = Utils::loadDom ($ html );
24- $ xpath = new \DOMXPath ($ dom );
25- // $nodes = $xpath->query('//*[@data-pattern-repeater-child]');
26- $ nodes = $ xpath ->query ('//*[@data-pattern-repeater-child and not(@data-processed)] ' );
27-
28- if ($ nodes ->length === 0 ) {
29- return $ html ;
30- }
31-
32- $ chunk_html = '' ;
33- foreach ($ nodes as $ node ) {
34- $ node ->setAttribute ('data-processed ' , '1 ' );
35-
36- $ tmp_dom = Utils::newDom ();
37- $ tmp_dom ->appendChild ($ tmp_dom ->importNode ($ node , true ));
38-
39- $ repeated = trim ($ tmp_dom ->saveHTML ());
40-
41- $ repeated = Links::transform ($ repeated , 'item ' );
42- $ repeated = Images::transform ($ repeated , 'item ' );
43- $ repeated = Videos::transform ($ repeated , 'item ' );
44-
45- $ repeated = self ::replacePostMeta ($ repeated , 'item ' );
46- $ repeated = self ::replaceGenerics ($ repeated , 'item ' );
47- $ repeated = self ::replaceNodeValues ($ repeated , 'item ' , $ name ?? '' );
48-
49- $ repeated = Clean::fixCloseTags ($ repeated );
50- $ repeated = Clean::fixHtml ($ repeated );
51-
52- $ chunk_html .= $ repeated ;
53- }
54-
55-
56- $ parent = $ xpath ->query ('//*[@data-pattern-repeater-parent] ' )[0 ];
57- $ parent ->nodeValue = '' ; // clear
58-
59- $ loop_start = $ dom ->createDocumentFragment ();
60- $ loop_start ->appendXML ("{% if fields.items %} \n{% for item in fields.items %} " );
61- $ parent ->appendChild ($ loop_start );
62-
63- $ chunk = $ dom ->createDocumentFragment ();
64- $ chunk ->appendXML ('<![CDATA[ ' . $ chunk_html . ']]> ' );
65- $ parent ->appendChild ($ chunk );
66-
67- $ loop_end = $ dom ->createDocumentFragment ();
68- $ loop_end ->appendXML ("{% endfor %} \n{% endif %} " );
69- $ parent ->appendChild ($ loop_end );
70-
71- $ html = $ dom ->saveHTML ();
72-
73- return $ html ;
74- }
7522 // Generic fields
76- private static function replaceGenerics (string $ html , string $ type ): string
23+ public static function replaceGenerics (string $ html , string $ type ): string
7724 {
7825 preg_match_all ('/%%(.*?)%%/ ' , $ html , $ matches );
7926
@@ -90,7 +37,7 @@ private static function replaceGenerics(string $html, string $type): string
9037 return $ html ;
9138 }
9239 // Post meta
93- private static function replacePostMeta (string $ html , string $ type ): string
40+ public static function replacePostMeta (string $ html , string $ type ): string
9441 {
9542 preg_match_all ('/%%(.*?)%%/ ' , $ html , $ matches );
9643
@@ -108,37 +55,36 @@ private static function replacePostMeta(string $html, string $type): string
10855 return $ html ;
10956 }
11057 // Post info
111- private static function replaceNodeValues (string $ html , string $ type , string $ name ): string
58+ public static function replaceNodeValues (string $ html , string $ type , string $ name ): string
11259 {
11360 $ dom = Utils::loadDom ($ html );
11461 $ xpath = new \DOMXPath ($ dom );
11562 $ nodes = $ xpath ->query ('//*[@data-pattern-post-info] ' );
63+ if ($ nodes ->length === 0 ) return $ html ;
11664
117- if ($ nodes ->length === 0 ) {
118- return $ html ;
119- }
65+ $ sectionTypeNode = $ xpath ->query ('//section[@data-post-info-type][1] ' )->item (0 );
66+ $ postInfoKind = $ sectionTypeNode ? $ sectionTypeNode ->getAttribute ('data-post-info-type ' ) : 'post-info ' ;
67+ $ isTaxonomy = ($ postInfoKind === 'post-taxonomy ' );
68+ $ getter = $ isTaxonomy ? 'get_term ' : 'get_post ' ;
12069
12170 $ postInfo = 'postInfoItem ' ;
122- $ postInfoType = 'item.post ' ;
71+ $ postInfoType = ($ type === 'fields ' )
72+ ? ($ name === 'current-post-info ' ? 'current_post ' : 'fields.post ' )
73+ : 'item.post ' ;
12374
124- if ($ type === 'fields ' ) {
125- if ($ name === 'current-post-info ' ) {
126- $ postInfoType = 'current_post ' ;
127- } else {
128- $ postInfoType = 'fields.post ' ;
129- }
130- }
13175 $ imageSelectNode = $ xpath ->query ('//*[@data-image-select] ' )->item (0 );
13276 $ imageSelect = $ imageSelectNode ? $ imageSelectNode ->getAttribute ('data-image-select ' ) : 'image ' ;
13377 $ imageKey = "post_ " . $ imageSelect ;
13478
135- foreach ($ nodes as $ key => $ node ) {
79+ foreach ($ nodes as $ i => $ node ) {
80+ if ($ node ->getAttribute ('data-post-info-processed ' ) === '1 ' ) continue ;
81+ $ node ->setAttribute ('data-post-info-processed ' , '1 ' );
82+
13683 $ elem = $ node ->getAttribute ('data-pattern-post-info ' );
13784
138- if ($ key === 0 ) {
85+ if ($ i === 0 ) {
13986 $ frag = $ dom ->createDocumentFragment ();
140- $ frag ->appendXML ("<inserttwig>{% set {$ postInfo } = get_post( {$ postInfoType }) %}</inserttwig> " );
141-
87+ $ frag ->appendXML ("<inserttwig>{% set {$ postInfo } = {$ getter }( {$ postInfoType }) %}</inserttwig> " );
14288 $ section = $ xpath ->query ('//section[1] ' )->item (0 );
14389 if ($ section && $ section ->parentNode ) {
14490 $ section ->parentNode ->insertBefore ($ frag , $ section );
@@ -157,7 +103,6 @@ private static function replaceNodeValues(string $html, string $type, string $na
157103 {% set isSVG = check_file_type(image.id) == 'image/svg+xml' %}
158104 {% set mainImageSrc = gt_image_mainsrc(image) %}
159105 {% set srcset = isSVG ? '' : gt_image_srcset(image) %}</inserttwig> " );
160-
161106 if ($ node ->nodeName === 'img ' && $ node ->hasAttribute ('srcset ' )) {
162107 $ img = $ node ;
163108 $ img ->parentNode ->insertBefore ($ frag , $ img );
@@ -166,8 +111,40 @@ private static function replaceNodeValues(string $html, string $type, string $na
166111 $ img ->setAttribute ('title ' , '{{ image.title }} ' );
167112 $ img ->setAttribute ('alt ' , '{{ image.alt }} ' );
168113 }
114+ } elseif ($ elem === 'post_video ' ) {
115+ $ videoSelect = $ node ->getAttribute ('data-video-select ' );
116+ $ frag = $ dom ->createDocumentFragment ();
117+ $ frag ->appendXML ("<inserttwig>{% set videoSelect = ' $ videoSelect' %}
118+ {% set videoDesktop = attribute( {$ postInfo }, videoSelect) %}
119+ {% set mainVideoSrc = gt_video_mainsrc(videoDesktop['url']) %}</inserttwig> " );
120+ $ videos = $ node ->getElementsByTagName ('video ' );
121+ if ($ videos ->length > 0 ) {
122+ $ video = $ videos ->item (0 );
123+ $ video ->parentNode ->insertBefore ($ frag , $ video );
124+ $ sources = $ video ->getElementsByTagName ('source ' );
125+ foreach ($ sources as $ source ) {
126+ $ source ->setAttribute ('src ' , '{{ mainVideoSrc }} ' );
127+ }
128+ }
169129 } else {
170130 $ node ->nodeValue = "{{ {$ postInfo }. {$ elem } }} " ;
131+ $ parent = $ node ->parentNode ;
132+ if ($ parent && $ parent ->nodeType === XML_ELEMENT_NODE ) {
133+ $ parentClass = ' ' . ($ parent ->getAttribute ('class ' ) ?? '' ) . ' ' ;
134+ if (strpos ($ parentClass , ' post-info-v3__content-container ' ) !== false ) {
135+ $ toRemove = [];
136+ for ($ child = $ parent ->firstChild ; $ child !== null ; $ child = $ child ->nextSibling ) {
137+ if ($ child !== $ node ) {
138+ $ toRemove [] = $ child ;
139+ }
140+ }
141+ foreach ($ toRemove as $ rm ) {
142+ if ($ rm ->parentNode ) {
143+ $ rm ->parentNode ->removeChild ($ rm );
144+ }
145+ }
146+ }
147+ }
171148 }
172149 }
173150 return Utils::saveDom ($ dom );
@@ -231,7 +208,7 @@ private static function formReplace(string $html): string
231208 }
232209
233210 if ($ endNode ->parentNode ) {
234- if ( $ renderDynamic === '1 ' ) {
211+ if ($ renderDynamic === '1 ' ) {
235212 $ shortcode = $ dom ->createTextNode ("{{ function('do_shortcode', '[cdbform id=' ~ fields.form ~ ']') }} " );
236213 } else {
237214 $ shortcode = $ dom ->createTextNode ("{{ function('do_shortcode', '[cdbform id= " . $ formId . "]') }} " );
0 commit comments