From 367e7440be935fc95eb4b31a0b534b36b0b791df Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Fri, 5 Mar 2021 13:56:35 +0900 Subject: [PATCH] Don't overwrite OpenGraph tags already set by user --- classes/context/Context.class.php | 1 + classes/display/HTMLDisplayHandler.php | 62 ++++++++++++++++---------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/classes/context/Context.class.php b/classes/context/Context.class.php index 4a9730a55..f2aced118 100644 --- a/classes/context/Context.class.php +++ b/classes/context/Context.class.php @@ -2764,6 +2764,7 @@ class Context public static function setCanonicalURL($url) { self::$_instance->canonical_url = escape($url, false); + self::addOpenGraphData('og:url', self::$_instance->canonical_url); } /** diff --git a/classes/display/HTMLDisplayHandler.php b/classes/display/HTMLDisplayHandler.php index ff831d1d1..df5b9f261 100644 --- a/classes/display/HTMLDisplayHandler.php +++ b/classes/display/HTMLDisplayHandler.php @@ -457,40 +457,56 @@ class HTMLDisplayHandler } } + // Get existing metadata. + $og_data = array(); + foreach (Context::getOpenGraphData() as $val) + { + $og_data[$val['property']] = $val['content']; + } + // Add basic metadata. Context::addOpenGraphData('og:title', $permitted ? Context::getBrowserTitle() : lang('msg_not_permitted')); Context::addOpenGraphData('og:site_name', Context::getSiteTitle()); - if ($page_type === 'article' && $permitted && config('seo.og_extract_description')) + if (!isset($og_data['og:description']) || !Context::getMetaTag('description')) { - $description = trim(utf8_normalize_spaces($oDocument->getContentText(200))); + if ($page_type === 'article' && $permitted && config('seo.og_extract_description')) + { + $description = trim(utf8_normalize_spaces($oDocument->getContentText(200))); + } + else + { + $description = Context::getMetaTag('description'); + } + Context::addOpenGraphData('og:description', $description); + Context::addMetaTag('description', $description); } - else - { - $description = Context::getMetaTag('description'); - } - Context::addOpenGraphData('og:description', $description); - Context::addMetaTag('description', $description); // Add metadata about this page. - Context::addOpenGraphData('og:type', $page_type); - if ($page_type === 'article') + if (!isset($og_data['og:type'])) { - $canonical_url = getFullUrl('', 'mid', $current_module_info->mid, 'document_srl', $document_srl); + Context::addOpenGraphData('og:type', $page_type); } - elseif (($page = Context::get('page')) > 1) + if (!isset($og_data['og:url']) || !Context::getCanonicalURL()) { - $canonical_url = getFullUrl('', 'mid', $current_module_info->mid, 'page', $page); + if ($page_type === 'article') + { + $canonical_url = getFullUrl('', 'mid', $current_module_info->mid, 'document_srl', $document_srl); + } + elseif (($page = Context::get('page')) > 1) + { + $canonical_url = getFullUrl('', 'mid', $current_module_info->mid, 'page', $page); + } + elseif ($current_module_info->module_srl == $site_module_info->module_srl) + { + $canonical_url = getFullUrl(''); + } + else + { + $canonical_url = getFullUrl('', 'mid', $current_module_info->mid); + } + Context::addOpenGraphData('og:url', $canonical_url); + Context::setCanonicalURL($canonical_url); } - elseif ($current_module_info->module_srl == $site_module_info->module_srl) - { - $canonical_url = getFullUrl(''); - } - else - { - $canonical_url = getFullUrl('', 'mid', $current_module_info->mid); - } - Context::addOpenGraphData('og:url', $canonical_url); - Context::setCanonicalURL($canonical_url); // Add metadata about the locale. $lang_type = Context::getLangType();