Skip to content

Commit b05fea8

Browse files
committed
#5422 Support for schema. News schema added and FAQs schema template simplified.
1 parent ec61237 commit b05fea8

File tree

11 files changed

+599
-211
lines changed

11 files changed

+599
-211
lines changed

e107_admin/prefs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112

113113
if(isset($_POST['contact_info']) && is_array($_POST['contact_info']))
114114
{
115-
$core_pref->set('contact_info', []); // reset to type array if string had been used in the past.
115+
$core_pref->set('contact_info', []); // reset to type array if string had been used in the past.
116116
}
117117

118118
$smtp_opts = array();

e107_core/shortcodes/batch/news_shortcodes.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ function sc_news_body($parm=null)
9999

100100
e107::getBB()->clearClass();
101101

102+
if($parm == 'raw')
103+
{
104+
$news_body = strip_tags($news_body);
105+
}
106+
102107
return $news_body;
103108
}
104109

@@ -507,7 +512,12 @@ public function sc_news_image($parm=null)
507512
{
508513
$imgParms['loading'] = $parm['loading'];
509514
}
510-
515+
516+
if(!empty($parm['return']))
517+
{
518+
$imgParms['return'] = $parm['return'];
519+
$parm['type'] = 'meta';
520+
}
511521

512522
$imgTag = $tp->toImage($srcPath,$imgParms);
513523

@@ -522,6 +532,10 @@ public function sc_news_image($parm=null)
522532
return empty($src) ? e_IMAGE_ABS."generic/nomedia.png" : $src;
523533
break;
524534

535+
case 'meta':
536+
return $tp->replaceConstants($imgTag, 'full');
537+
break;
538+
525539
case 'url':
526540
return "<a href='".e107::getUrl()->create('news/view/item', $this->news_item)."'>".$imgTag."</a>";
527541
break;
@@ -622,6 +636,10 @@ private function formatDate($date, $parm)
622636
$ret = $con->convert_date($date, 'forum');
623637
break;
624638

639+
case 'atom':
640+
$ret = date(DATE_ATOM, $date);
641+
break;
642+
625643
default :
626644
$ret = $tp->toDate($date, $parm);
627645
break;
@@ -640,6 +658,10 @@ function sc_news_date($parm=null)
640658

641659
function sc_news_modified($parm=null)
642660
{
661+
if(empty($this->news_item['news_modified']))
662+
{
663+
return null;
664+
}
643665
return $this->formatDate($this->news_item['news_modified'], $parm);
644666
}
645667

@@ -1097,7 +1119,9 @@ function newsTitleLink($parm = null)
10971119

10981120
function sc_news_url($parm=null)
10991121
{
1100-
return e107::getUrl()->create('news/view/item', $this->news_item);
1122+
$options = (!empty($parm) && is_array($parm)) ? $parm : array();
1123+
1124+
return e107::getUrl()->create('news/view/item', $this->news_item, $options);
11011125
}
11021126

11031127

e107_handlers/e_parse_class.php

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -854,27 +854,118 @@ public function parseTemplate($text, $parseSCFiles = true, $extraCodes = null, $
854854
return e107::getScParser()->parseCodes($text, $parseSCFiles, $extraCodes, $eVars);
855855
}
856856

857+
857858
/**
858-
* @experimental
859-
* @param string $text
860-
* @param bool $parseSCFiles
861-
* @param object|array $extraCodes
862-
* @param object $eVars
863-
* @return string
859+
* Parses a JSON schema template, processes placeholders, and reconstructs the JSON with optional main entity and extra codes.
860+
*
861+
* @param string $text The JSON schema template to be parsed.
862+
* @param bool $parseSCFiles Whether to enable the parsing of shortcode files. Defaults to true.
863+
* @param object|null $extraCodes Optional extra codes object for placeholder parsing.
864+
* @param array|null $mainEntity Optional data array to replace the 'mainEntity' structure in the schema.
865+
* @return string|false The processed JSON schema string on success, or false if the input JSON is invalid.
864866
*/
865-
public function parseSchemaTemplate($text, $parseSCFiles = true, $extraCodes = null, $eVars = null)
867+
public function parseSchemaTemplate($text, $parseSCFiles = true, $extraCodes = null, $mainEntity = null)
866868
{
869+
870+
// Initialize the parser
867871
$parse = e107::getScParser();
868-
$parse->setMode('schema');
869-
$text = e107::getScParser()->parseCodes($text, $parseSCFiles, $extraCodes, $eVars);
870-
$text = str_replace('<!-- >', '', $text); // cleanup
871-
$parse->setMode('default');
872+
$parse->setMode('schema'); // Set parsing mode for schema
872873

873-
return $text;
874+
// Step 1: Decode the JSON input into an array
875+
$jsonArray = json_decode($text, true);
876+
877+
// Step 2: Validate JSON decoding
878+
if(json_last_error() !== JSON_ERROR_NONE)
879+
{
880+
error_log('Invalid JSON: ' . json_last_error_msg());
881+
return false;
882+
883+
}
884+
885+
// Step 3: Recursive function to process the JSON structure
886+
$processItems = function (&$item) use (&$processItems, $parse, $parseSCFiles, $extraCodes, $mainEntity)
887+
{
888+
889+
if(is_array($item))
890+
{
891+
// Check if the current item contains 'mainEntity', the target of our processing
892+
if(isset($item['mainEntity']) && is_array($mainEntity))
893+
{
894+
// Get the first template item from the 'mainEntity' array to use as the structure
895+
$schemaTemplate = $item['mainEntity'][0];
896+
$item['mainEntity'] = []; // Reset the 'mainEntity' array to prevent duplication
897+
898+
foreach($mainEntity as $dataRow)
899+
{
900+
901+
// Create a fresh copy of the schema template for this specific dataRow
902+
$duplicatedItem = json_decode(json_encode($schemaTemplate), true);
903+
904+
// Update the extraCodes for the current data row
905+
if(method_exists($extraCodes, 'setVars'))
906+
{
907+
$extraCodes->setVars($dataRow); // Inject new placeholders from this row
908+
}
909+
910+
// Process placeholders in the duplicated item
911+
foreach($duplicatedItem as &$value)
912+
{
913+
if(is_string($value) && strpos($value, '{') !== false)
914+
{
915+
// Parse placeholders for current dataRow
916+
$value = $parse->parseCodes($value, $parseSCFiles, $extraCodes);
917+
$value = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
918+
$value = strip_tags($value);
919+
}
920+
elseif(is_array($value))
921+
{
922+
// Recursively process arrays (e.g., nested structures)
923+
$processItems($value);
924+
}
925+
}
874926

927+
// Append the processed item to the 'mainEntity' array
928+
$item['mainEntity'][] = $duplicatedItem;
929+
}
930+
}
931+
else
932+
{
933+
// Recursively process other parts of the JSON structure
934+
foreach($item as &$value)
935+
{
936+
$processItems($value);
937+
}
938+
}
939+
}
940+
elseif(is_string($item))
941+
{
942+
// Parse string placeholders, if any
943+
if(strpos($item, '{') !== false)
944+
{
945+
946+
$item = $parse->parseCodes($item, $parseSCFiles, $extraCodes);
947+
$item = str_replace('&amp;', '&', $item);
948+
$item = html_entity_decode($item, ENT_QUOTES | ENT_HTML5, 'UTF-8');
949+
$item = strip_tags($item);
950+
951+
952+
}
953+
}
954+
};
955+
956+
// Step 4: Initiate processing for the entire JSON structure
957+
$processItems($jsonArray);
958+
959+
// Reset the parse mode after processing
960+
$parse->setMode('default');
961+
962+
// Step 5: Encode the final result back into JSON
963+
return json_encode($jsonArray, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
875964
}
876965

877966

967+
968+
878969
/**
879970
* Simple parser
880971
*
@@ -2174,9 +2265,10 @@ public function toText($text)
21742265

21752266
$search = array('&amp;#039;', '&amp;#036;', '&#039;', '&#036;', '&#092;', '&amp;#092;');
21762267
$replace = array("'", '$', "'", '$', "\\", "\\");
2177-
$text = str_replace($search, $replace, $text);
21782268

2179-
return $text;
2269+
return str_replace($search, $replace, $text);
2270+
2271+
21802272
}
21812273

21822274

@@ -4729,6 +4821,12 @@ public function toImage($file, $parm = array())
47294821
return null;
47304822
}
47314823

4824+
if(varset($parm['return']) === 'url')
4825+
{
4826+
$path = $tp->createConstants($path, 'mix');
4827+
return $tp->replaceConstants($path, 'full');
4828+
}
4829+
47324830
$html .= "<img {$id}class=\"{$class}\" src=\"" . $path . '" alt="' . $alt . '" ' . $srcset . $width . $height . $style . $loading . $title . ' />';
47334831

47344832
// $html .= ($this->convertToWebP) ? "\n</picture>" : '';

e107_plugins/faqs/faqs.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,6 @@ function view_all_query($srch='')
385385
$sc->tag = htmlspecialchars(varset($tag), ENT_QUOTES, 'utf-8');
386386
$sc->category = varset($category);
387387

388-
if(!empty($schemaTemplate['start']))
389-
{
390-
$schema = $tp->parseSchemaTemplate($schemaTemplate['start'],false,$sc);
391-
}
392-
393388
if(!empty($_GET['id'])) // expand one specific FAQ.
394389
{
395390
$sc->item =intval($_GET['id']);
@@ -407,7 +402,7 @@ function view_all_query($srch='')
407402
// $text = $tp->parseTemplate($FAQ_START, true, $sc);
408403

409404
// $text = "";
410-
405+
$start = false;
411406

412407

413408
if($this->pref['list_type'] == 'ol')
@@ -419,17 +414,14 @@ function view_all_query($srch='')
419414
$FAQ_LISTALL['end'] = str_replace($tsrch,$trepl, $FAQ_LISTALL['end']);
420415
}
421416

422-
$schemaItems = [];
417+
423418
foreach ($data as $rw)
424419
{
425420
$rw['faq_sef'] = eHelper::title2sef($tp->toText($rw['faq_question']),'dashl');
426421

427422
$sc->setVars($rw);
428423

429-
if(!empty($schemaTemplate['item']))
430-
{
431-
$schemaItems[] = $tp->parseSchemaTemplate($schemaTemplate['item'],false,$sc);
432-
}
424+
433425

434426
if($sc->item == $rw['faq_id'])
435427
{
@@ -445,32 +437,35 @@ function view_all_query($srch='')
445437
}
446438
$text .= "\n\n<!-- FAQ Start ".$rw['faq_info_order']."-->\n\n";
447439
$text .= $tp->parseTemplate($FAQ_LISTALL['start'], true, $sc);
448-
$start = TRUE;
440+
$start = true;
449441
}
450442

451443
$text .= $tp->parseTemplate($FAQ_LISTALL['item'], true, $sc);
452444
$prevcat = $rw['faq_info_order'];
453445
$sc->counter++;
454446
}
455447

456-
if(!empty($schemaItems))
457-
{
458-
$schema .= implode(",", $schemaItems);
459-
}
460448

461-
$text .= ($start) ? $tp->parseTemplate($FAQ_LISTALL['end'], true, $sc) : "";
462449

463-
if(!empty($schemaTemplate['end']))
464-
{
465-
$schema .= $tp->parseSchemaTemplate($schemaTemplate['end'],false,$sc);
466-
}
450+
$text .= ($start) ? $tp->parseTemplate($FAQ_LISTALL['end'], true, $sc) : "";
467451

468-
if(!empty($schema))
452+
if(!empty($schemaTemplate))
469453
{
454+
if(isset($schemaTemplate['end']) && isset($schemaTemplate['item']) && isset($schemaTemplate['start']))
455+
{
456+
$schemaTpl = $schemaTemplate['start']."\n".$schemaTemplate['item']."\n".$schemaTemplate['end'];
457+
$schema = $tp->parseSchemaTemplate($schemaTpl, true, $sc, $data);
458+
}
459+
elseif(is_string($schemaTemplate))
460+
{
461+
$schema = $tp->parseSchemaTemplate($schemaTemplate, true, $sc, $data);
462+
}
470463

471-
e107::schema($schema);
464+
if(!empty($schema))
465+
{
466+
e107::schema($schema);
467+
}
472468
}
473-
// $text .= $tp->parseTemplate($FAQ_END, true, $sc);
474469

475470
return $text;
476471

e107_plugins/faqs/faqs_shortcodes.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function sc_faq_hide($parm=null)
8080
}
8181

8282

83-
function sc_faq_question($parm='')
83+
function sc_faq_question($parm=[])
8484
{
8585
$tp = e107::getParser();
8686
$parm = eHelper::scDualParams($parm);
@@ -93,7 +93,7 @@ function sc_faq_question($parm='')
9393

9494
$faqNew = ($this->var['faq_datestamp'] > $newDate) ? " faq-new" : "";
9595

96-
if($param == 'expand' && !empty($this->var['faq_answer']))
96+
if($param === 'expand' && !empty($this->var['faq_answer']))
9797
{
9898

9999
$id = "faq_".$this->var['faq_id'];
@@ -130,6 +130,11 @@ function sc_faq_question($parm='')
130130
}
131131
else
132132
{
133+
if(isset($parm['html']) && empty($parm['html']))
134+
{
135+
return e107::getParser()->toText($this->var['faq_question']);
136+
}
137+
133138
$text = $tp->toHTML($this->var['faq_question'],true, 'TITLE');
134139
}
135140
return $text;
@@ -154,8 +159,13 @@ function sc_faq_question_link($parm='')
154159
return "<a class='faq-question' href='". e107::url('faqs', 'item', $this->var)."' >".$tp -> toHTML($this->var['faq_question'],true,'TITLE')."</a>";
155160
}
156161

157-
function sc_faq_answer()
162+
function sc_faq_answer($parm=[])
158163
{
164+
if(isset($parm['html']) && empty($parm['html']))
165+
{
166+
return e107::getParser()->toText($this->var['faq_answer']);
167+
}
168+
159169
return e107::getParser()->toHTML($this->var['faq_answer'],true,'BODY');
160170
}
161171

0 commit comments

Comments
 (0)