From d672131d866129fd1f658cf45e632186fe699a15 Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 3 Jun 2022 09:12:12 -0700 Subject: [PATCH] Fixes #4785 - og:image should be only rendered once. Moved all og meta code out of header_default.php and into social plugin. --- e107_core/templates/header_default.php | 21 +------ e107_handlers/application.php | 2 +- e107_plugins/news/news.php | 18 ++---- e107_plugins/social/e_event.php | 76 +++++++++++++++++++++----- 4 files changed, 69 insertions(+), 48 deletions(-) diff --git a/e107_core/templates/header_default.php b/e107_core/templates/header_default.php index 14afe3d04..0f016f421 100644 --- a/e107_core/templates/header_default.php +++ b/e107_core/templates/header_default.php @@ -114,8 +114,6 @@ function render_title() if(!defined('e_PAGETITLE') && ($_PAGE_TITLE = e107::getSingleton('eResponse')->getMetaTitle())) // use e107::title() to set. { define('e_PAGETITLE', $_PAGE_TITLE); - e107::meta('og:title', $_PAGE_TITLE); // will only populate if not already defined. - e107::meta('twitter:title', $_PAGE_TITLE); } $arr = []; @@ -131,12 +129,6 @@ function render_title() $arr[] = PAGE_NAME; } } - else // Frontpage - { - e107::meta('og:type', 'website'); - e107::meta('og:title', SITENAME); - e107::meta('twitter:title', SITENAME); - } $arr[] = SITENAME; @@ -167,18 +159,7 @@ echo " \n"; if(!empty($pref['meta_copyright'][e_LANGUAGE])) e107::meta('dcterms.rights',$pref['meta_copyright'][e_LANGUAGE]); if(!empty($pref['meta_author'][e_LANGUAGE])) e107::meta('author',$pref['meta_author'][e_LANGUAGE]); -if(!empty($pref['sitebutton'])) -{ - $siteButton = (strpos($pref['sitebutton'],'{e_MEDIA') !== false) ? e107::getParser()->thumbUrl($pref['sitebutton'],'w=800',false, true) : e107::getParser()->replaceConstants($pref['sitebutton'],'full'); - e107::meta('og:image',$siteButton); - unset($siteButton); -} -elseif(!empty($pref['sitelogo'])) // fallback to sitelogo -{ - $siteLogo = (strpos($pref['sitelogo'],'{e_MEDIA') !== false) ? e107::getParser()->thumbUrl($pref['sitelogo'],'w=800',false, true) : e107::getParser()->replaceConstants($pref['sitelogo'],'full'); - e107::meta('og:image',$siteLogo); - unset($siteLogo); -} + if(defined("VIEWPORT")) e107::meta('viewport',VIEWPORT); //BC ONLY diff --git a/e107_handlers/application.php b/e107_handlers/application.php index 59ef6eb56..4f102bc86 100644 --- a/e107_handlers/application.php +++ b/e107_handlers/application.php @@ -4106,7 +4106,7 @@ class eResponse 'article:section', 'article:tag', 'article:published_time', 'article:modified_time', 'og:description', 'og:image', 'og:title', 'og:updated_time','og:url', 'og:type' ); - protected $_meta_multiple = array('og:image', 'og:image:width','twitter:image'); + protected $_meta_multiple = array(); protected $_meta = array(); protected $_meta_robot_types = array('noindex'=>'NoIndex', 'nofollow'=>'NoFollow','noarchive'=>'NoArchive','noimageindex'=>'NoImageIndex' ); protected $_title_separator = ' » '; diff --git a/e107_plugins/news/news.php b/e107_plugins/news/news.php index 36d7f9286..58d3fb166 100644 --- a/e107_plugins/news/news.php +++ b/e107_plugins/news/news.php @@ -674,29 +674,19 @@ class news_front } // include news-thumbnail/image in meta. - always put this one first. - $twitterImage = false; if(!empty($news['news_thumbnail'])) { $iurl = (substr($news['news_thumbnail'],0,3)=="{e_") ? $news['news_thumbnail'] : SITEURL.e_IMAGE."newspost_images/".$news['news_thumbnail']; $tmp = explode(",", $iurl); - foreach($tmp as $mimg) + + if(!empty($tmp[0]) && substr($tmp[0],-8) !== '.youtube') { - if(substr($mimg,-8) == '.youtube' || empty($mimg)) - { - continue; - } - + $mimg = $tmp[0]; $metaImg = $tp->thumbUrl($mimg,'w=1200',false,true) ; e107::meta('og:image',$metaImg); e107::meta('og:image:width', 1200); - if(!$twitterImage) - { - e107::meta('twitter:image', $metaImg); - $twitterImage = true; - } - + e107::meta('twitter:image', $metaImg); e107::meta('twitter:card', 'summary_large_image'); - } } diff --git a/e107_plugins/social/e_event.php b/e107_plugins/social/e_event.php index 9b30068d4..1b92d31b8 100644 --- a/e107_plugins/social/e_event.php +++ b/e107_plugins/social/e_event.php @@ -30,7 +30,7 @@ class social_event $event[] = array( 'name' => "system_meta_pre", - 'function' => "og_image_add", + 'function' => "addFallbackMeta", ); return $event; @@ -42,31 +42,81 @@ class social_event /** * Callback function to add og:image if there is no any */ - function og_image_add($meta) + function addFallbackMeta($meta) { - $ogImage = e107::pref('social', 'og_image', false); + if(e_ADMIN_AREA === true) + { + return null; + } + + /** @note TITLE */ + if($title = e107::getSingleton('eResponse')->getMetaTitle()) + { + e107::meta('og:title', $title); // will only populate if not already defined. + e107::meta('twitter:title', $title); + } + elseif(deftrue('e_FRONTPAGE')) + { + e107::meta('og:title', SITENAME); + e107::meta('twitter:title', SITENAME); + } + + /** @note TYPE */ if(empty($meta['og:type'])) { e107::meta('og:type', 'website'); } - if(empty($ogImage) || empty($meta) || e_ADMIN_AREA === true) + /** @note DESCRIPTION */ + if(empty($meta['og:description'])) { - return null; - } + $description = e107::getSingleton('eResponse')->getMetaDescription(); - // check if we have og:image defined - - foreach($meta as $m) - { - if(varset($m['name']) === 'og:image') + if(empty($description) && deftrue('META_DESCRIPTION')) { - return null; + $description = META_DESCRIPTION; + } + else + { + $description = e107::pref('core', 'meta_description'); + } + + if(!empty($description)) + { + e107::meta('og:description', $description); + e107::meta('twitter:description', $description); + } } - e107::meta('og:image', e107::getParser()->thumbUrl($ogImage, 'w=500', false, true)); + /** @note IMAGE */ + if(!empty($meta['og:image'])) + { + // e107::getDebug()->log("Skipping Social plugin og:image fallback"); + return null; + } + + $pref = e107::getConfig()->getPref(); + + if($ogImage = e107::pref('social', 'og_image', false)) + { + $metaImg = e107::getParser()->thumbUrl($ogImage, 'w=800', false, true); + e107::meta('og:image', $metaImg); + e107::meta('twitter:image', $metaImg); + } + elseif(!empty($pref['sitebutton'])) + { + $siteButton = (strpos($pref['sitebutton'],'{e_MEDIA') !== false) ? e107::getParser()->thumbUrl($pref['sitebutton'],'w=800',false, true) : e107::getParser()->replaceConstants($pref['sitebutton'],'full'); + e107::meta('og:image',$siteButton); + e107::meta('twitter:image', $siteButton); + } + elseif(!empty($pref['sitelogo'])) // fallback to sitelogo + { + $siteLogo = (strpos($pref['sitelogo'],'{e_MEDIA') !== false) ? e107::getParser()->thumbUrl($pref['sitelogo'],'w=800',false, true) : e107::getParser()->replaceConstants($pref['sitelogo'],'full'); + e107::meta('og:image',$siteLogo); + e107::meta('twitter:image', $siteLogo); + } }