From 550975fffa0876b182c14302d4b5cc03bcfa5205 Mon Sep 17 00:00:00 2001 From: Tom Homer Date: Fri, 8 Feb 2019 10:42:31 -0500 Subject: [PATCH] Added inLanguage and Keywords support for Structured Data For feature #902 --- plugins/staticpages/functions.inc | 33 +++++++++++++++++++------ public_html/article.php | 1 + system/classes/structureddata.class.php | 24 +++++++++++++++--- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/plugins/staticpages/functions.inc b/plugins/staticpages/functions.inc index df4e20c75..b5c22cd87 100755 --- a/plugins/staticpages/functions.inc +++ b/plugins/staticpages/functions.inc @@ -574,14 +574,17 @@ function SP_displayPage($page, $A, $comment_order = 'ASC', $comment_mode = 'nest $breadcrumbs = TOPIC_breadcrumbs('staticpages', $page); } - $parameters['headline'] = $A['sp_title']; - $parameters['url'] = $sp_url; - $parameters['datePublished'] = $A['created']; - $parameters['dateModified'] = $A['modified']; - $parameters['description'] = $A['meta_description']; - $parameters['commentCount'] = CMT_commentCount($page, 'staticpages'); - $_STRUCT_DATA->add_type('staticpages', $page, $A['structured_data_type'], $parameters); + + $properties['headline'] = $A['sp_title']; + $properties['url'] = $sp_url; + $properties['datePublished'] = $A['created']; + $properties['dateModified'] = $A['modified']; + $properties['description'] = $A['meta_description']; + $properties['keywords'] = $A['meta_keywords']; + $properties['commentCount'] = CMT_commentCount($page, 'staticpages'); + $_STRUCT_DATA->add_type('staticpages', $page, $A['structured_data_type'], $properties); $_STRUCT_DATA->set_author_item('staticpages', $page, $author_name); + $retval = COM_createHTMLDocument($retval, array('what' => $what, 'pagetitle' => $page_title, 'breadcrumbs' => $breadcrumbs, 'headercode' => $headerCode, 'rightblock' => $rightblock)); } @@ -1999,7 +2002,21 @@ function SP_render_content($sp_id, $sp_title, $sp_content, $sp_php, $cache_time } $sp_content = stripslashes($sp_content); - + + /* + // Since staticpage can be cached and autotags within content may insert structured data properties need to insert it now before cache file is written. + $properties['headline'] = $A['sp_title']; + $properties['url'] = $sp_url; + $properties['datePublished'] = $A['created']; + $properties['dateModified'] = $A['modified']; + $properties['description'] = $A['meta_description']; + $properties['keywords'] = $A['meta_keywords']; + $properties['commentCount'] = CMT_commentCount($page, 'staticpages'); + $_STRUCT_DATA->add_type('staticpages', $page, $A['structured_data_type'], $properties); + $_STRUCT_DATA->set_author_item('staticpages', $page, $author_name); + $sp_content = $_STRUCT_DATA->toScript('staticpages', $page) . $sp_content; + */ + if (($cache_time > 0 || $cache_time == -1) && $sp_php == 0) { CACHE_create_instance($cacheInstance, $sp_content); } diff --git a/public_html/article.php b/public_html/article.php index d485c74d4..5dac1311c 100644 --- a/public_html/article.php +++ b/public_html/article.php @@ -525,6 +525,7 @@ function extractExternalLinks($text) { $properties['dateModified'] = $article->displayElements('modified'); } $properties['description'] = $article->DisplayElements('meta_description'); + $properties['keywords'] = $article->DisplayElements('meta_keywords'); $properties['commentCount'] = CMT_commentCount($article->getSid(), 'article'); $_STRUCT_DATA->add_type('article', $article->getSid(), $article->displayElements('structured_data_type'), $properties); $_STRUCT_DATA->set_author_item('article', $article->getSid(), $article->DisplayElements('username')); diff --git a/system/classes/structureddata.class.php b/system/classes/structureddata.class.php index addbd3793..cca925cdb 100644 --- a/system/classes/structureddata.class.php +++ b/system/classes/structureddata.class.php @@ -93,6 +93,12 @@ public function add_type($type, $id, $sd_type, $properties = array()) // Create structured data name $sd_name = $this->create_name($type, $id); + // Remove any empty properties + foreach($properties as $key => $value) { + if(empty($value)) + unset($properties[$key]); + } + // 0 = None if ($sd_type > 0 AND $sd_type < 5) { $this->items[$sd_name]['@context'] = "https://schema.org"; @@ -120,8 +126,20 @@ public function add_type($type, $id, $sd_type, $properties = array()) $this->items[$sd_name]['commentCount'] = $properties['commentCount']; } - // inLanguage - // keywords // Meta Keywords or Topic List + $lang_id = ''; + if (COM_isMultiLanguageEnabled()) { + $lang_id = COM_getLanguageIdForObject($id); + } + if (empty($lang_id)) { + // Assume default language of site + $lang_id = COM_getLanguageId($_CONF['language_site_default']); + } + $this->items[$sd_name]['inLanguage'] = $lang_id; + + // keywords // Meta Keywords or Topic List (needs to be a comma delimited list) + if (isset($properties['keywords'])) { + $this->items[$sd_name]['keywords'] = $properties['keywords']; + } // image // thumbnailUrl @@ -129,7 +147,7 @@ public function add_type($type, $id, $sd_type, $properties = array()) // video // Can be set by autotag which can be executed first so do not overwrite if set - if (!isset($this->items[$sd_name]['description'])) { + if (isset($properties['description']) && !isset($this->items[$sd_name]['description'])) { $this->items[$sd_name]['description'] = $properties['description']; }