mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-09 11:44:10 +09:00
Initial implementation of OpenGraph metadata insertion
This commit is contained in:
parent
f84715db5b
commit
da1b69c6b7
4 changed files with 227 additions and 10 deletions
|
|
@ -92,6 +92,12 @@ class Context
|
|||
*/
|
||||
public $meta_tags = array();
|
||||
|
||||
/**
|
||||
* OpenGraph metadata
|
||||
* @var array
|
||||
*/
|
||||
public $opengraph_metadata = array();
|
||||
|
||||
/**
|
||||
* path of Xpress Engine
|
||||
* @var string
|
||||
|
|
@ -2708,34 +2714,80 @@ class Context
|
|||
|
||||
/**
|
||||
* Get meta tag
|
||||
*
|
||||
* @param string $name (optional)
|
||||
* @return array The list of meta tags
|
||||
*/
|
||||
public static function getMetaTag()
|
||||
public static function getMetaTag($name = null)
|
||||
{
|
||||
$ret = array();
|
||||
foreach(self::$_instance->meta_tags as $key => $val)
|
||||
if ($name !== null)
|
||||
{
|
||||
list($name, $is_http_equiv) = explode("\t", $key);
|
||||
$ret[] = array('name' => $name, 'is_http_equiv' => $is_http_equiv, 'content' => escape($val, false));
|
||||
return isset(self::$_instance->meta_tags[$name]) ? self::$_instance->meta_tags[$name]['content'] : null;
|
||||
}
|
||||
|
||||
$ret = array();
|
||||
foreach(self::$_instance->meta_tags as $name => $content)
|
||||
{
|
||||
$ret[] = array('name' => $name, 'is_http_equiv' => $content['is_http_equiv'], 'content' => escape($content['content'], false));
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the meta tag
|
||||
* Add meta tag
|
||||
*
|
||||
* @param string $name name of meta tag
|
||||
* @param string $content content of meta tag
|
||||
* @param mixed $is_http_equiv value of http_equiv
|
||||
* @return void
|
||||
*/
|
||||
public static function addMetaTag($name, $content, $is_http_equiv = FALSE)
|
||||
public static function addMetaTag($name, $content, $is_http_equiv = false)
|
||||
{
|
||||
getController('module')->replaceDefinedLangCode($content);
|
||||
self::$_instance->meta_tags[$name . "\t" . ($is_http_equiv ? '1' : '0')] = $content;
|
||||
self::$_instance->meta_tags[$name] = array('is_http_equiv' => (bool)$is_http_equiv, 'content' => $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get OpenGraph metadata
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getOpenGraphData()
|
||||
{
|
||||
$ret = array();
|
||||
foreach(self::$_instance->opengraph_metadata as $key => $val)
|
||||
{
|
||||
if ($val[1] === false || $val[1] === null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$ret[] = array('property' => escape($val[0], false), 'content' => escape($val[1], false));
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add OpenGraph metadata
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $content
|
||||
* @return void
|
||||
*/
|
||||
public static function addOpenGraphData($name, $content)
|
||||
{
|
||||
if (is_array($content))
|
||||
{
|
||||
foreach ($content as $key => $val)
|
||||
{
|
||||
self::addOpenGraphData("$name:$key", $val);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self::$_instance->opengraph_metadata[] = array($name, $content);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/* End of file Context.class.php */
|
||||
/* Location: ./classes/context/Context.class.php */
|
||||
|
|
|
|||
|
|
@ -213,6 +213,12 @@ class HTMLDisplayHandler
|
|||
|
||||
// Remove unnecessary information
|
||||
$output = preg_replace('/member\_\-([0-9]+)/s', 'member_0', $output);
|
||||
|
||||
// Add OpenGraph metadata
|
||||
if (Context::get('module') !== 'admin')
|
||||
{
|
||||
$this->_addOpenGraphMetadata();
|
||||
}
|
||||
|
||||
// set icon
|
||||
$oAdminModel = getAdminModel('admin');
|
||||
|
|
@ -380,6 +386,141 @@ class HTMLDisplayHandler
|
|||
}
|
||||
Context::loadFile($matches[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add OpenGraph metadata tags.
|
||||
*
|
||||
* @param string $output
|
||||
* @return void
|
||||
*/
|
||||
function _addOpenGraphMetadata()
|
||||
{
|
||||
// Get information about the current request.
|
||||
$page_type = 'website';
|
||||
$current_module_info = Context::get('current_module_info');
|
||||
$site_module_info = Context::get('site_module_info');
|
||||
$document_srl = Context::get('document_srl');
|
||||
if ($document_srl)
|
||||
{
|
||||
$oDocument = Context::get('oDocument') ?: getModel('document')->getDocument($document_srl, false, false);
|
||||
if ($oDocument instanceof documentItem && $oDocument->document_srl == $document_srl && !$oDocument->isSecret())
|
||||
{
|
||||
$page_type = 'article';
|
||||
}
|
||||
}
|
||||
|
||||
// Add basic metadata.
|
||||
Context::addOpenGraphData('og:title', Context::getBrowserTitle());
|
||||
Context::addOpenGraphData('og:site_name', Context::getSiteTitle());
|
||||
if ($page_type === 'article')
|
||||
{
|
||||
Context::addOpenGraphData('og:description', trim(utf8_normalize_spaces($oDocument->getContentText(200))));
|
||||
}
|
||||
else
|
||||
{
|
||||
Context::addOpenGraphData('og:description', Context::getMetaTag('description'));
|
||||
}
|
||||
|
||||
// Add metadata about this page.
|
||||
Context::addOpenGraphData('og:type', $page_type);
|
||||
if ($page_type === 'article')
|
||||
{
|
||||
$document_canonical_url = getFullUrl('', 'mid', $current_module_info->mid, 'document_srl', $document_srl);
|
||||
Context::addHtmlHeader(sprintf('<link rel="canonical" href="%s" />', escape($document_canonical_url)));
|
||||
Context::addOpenGraphData('og:url', $document_canonical_url);
|
||||
}
|
||||
elseif (($page = Context::get('page')) > 1)
|
||||
{
|
||||
Context::addOpenGraphData('og:url', getFullUrl('', 'mid', $current_module_info->mid, 'page', $page));
|
||||
}
|
||||
elseif ($current_module_info->module_srl == $site_module_info->module_srl)
|
||||
{
|
||||
Context::addOpenGraphData('og:url', Rhymix\Framework\URL::getCurrentDomainURL(\RX_BASEURL));
|
||||
}
|
||||
else
|
||||
{
|
||||
Context::addOpenGraphData('og:url', getFullUrl('', 'mid', $current_module_info->mid));
|
||||
}
|
||||
|
||||
// Add metadata about the locale.
|
||||
$lang_type = Context::getLangType();
|
||||
$locales = (include \RX_BASEDIR . 'common/defaults/locales.php');
|
||||
if (isset($locales[$lang_type]))
|
||||
{
|
||||
Context::addOpenGraphData('og:locale', $locales[$lang_type]);
|
||||
}
|
||||
if ($page_type === 'article' && $oDocument->getLangCode() !== $lang_type && isset($locales[$oDocument->getLangCode()]))
|
||||
{
|
||||
Context::addOpenGraphData('og:locale:alternate', $locales[$oDocument->getLangCode()]);
|
||||
}
|
||||
|
||||
// Add image.
|
||||
if ($page_type === 'article')
|
||||
{
|
||||
if (($document_images = Rhymix\Framework\Cache::get("seo:document_images:$document_srl")) === null)
|
||||
{
|
||||
$document_images = array();
|
||||
if ($oDocument->hasUploadedFiles())
|
||||
{
|
||||
foreach ($oDocument->getUploadedFiles() as $file)
|
||||
{
|
||||
if ($file->isvalid !== 'Y' || !preg_match('/\.(?:bmp|gif|jpe?g|png)$/i', $file->uploaded_filename))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
list($width, $height) = @getimagesize($file->uploaded_filename);
|
||||
if ($width < 100 && $height < 100)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$image = array('filepath' => $file->uploaded_filename, 'width' => $width, 'height' => $height);
|
||||
if ($file->cover_image === 'Y')
|
||||
{
|
||||
array_unshift($document_images, $image);
|
||||
}
|
||||
else
|
||||
{
|
||||
$document_images[] = $image;
|
||||
}
|
||||
if (count($document_images) >= 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Rhymix\Framework\Cache::set("seo:document_images:$document_srl", $document_images, 0, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$document_images = null;
|
||||
}
|
||||
|
||||
if ($document_images)
|
||||
{
|
||||
$first_image = reset($document_images);
|
||||
$first_image['filepath'] = preg_replace('/^.\\/files\\//', \RX_BASEURL . 'files/', $first_image['filepath']);
|
||||
Context::addOpenGraphData('og:image', Rhymix\Framework\URL::getCurrentDomainURL($first_image['filepath']));
|
||||
Context::addOpenGraphData('og:image:width', $first_image['width']);
|
||||
Context::addOpenGraphData('og:image:height', $first_image['height']);
|
||||
}
|
||||
elseif ($default_image = getAdminModel('admin')->getSiteDefaultImageUrl($width, $height))
|
||||
{
|
||||
Context::addOpenGraphData('og:image', Rhymix\Framework\URL::getCurrentDomainURL($default_image));
|
||||
if ($width && $height)
|
||||
{
|
||||
Context::addOpenGraphData('og:image:width', $width);
|
||||
Context::addOpenGraphData('og:image:height', $height);
|
||||
}
|
||||
}
|
||||
|
||||
// Add datetime for articles.
|
||||
if ($page_type === 'article')
|
||||
{
|
||||
Context::addOpenGraphData('article:published_time', $oDocument->getRegdate('c'));
|
||||
Context::addOpenGraphData('article:modified_time', $oDocument->getUpdate('c'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* import basic .js files.
|
||||
|
|
|
|||
21
common/defaults/locales.php
Normal file
21
common/defaults/locales.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Conversion map from languages to locales
|
||||
*
|
||||
* Copyright (c) Rhymix Developers and Contributors
|
||||
*/
|
||||
return array(
|
||||
'ko' => 'ko_KR',
|
||||
'en' => 'en_US',
|
||||
'ja' => 'ja_JP',
|
||||
'zh-CN' => 'zh_CN',
|
||||
'zh-TW' => 'zh_TW',
|
||||
'de' => 'de_DE',
|
||||
'es' => 'es_ES',
|
||||
'fr' => 'fr_FR',
|
||||
'mn' => 'mn_MN',
|
||||
'ru' => 'ru_RU',
|
||||
'tr' => 'tr_TR',
|
||||
'vi' => 'vi_VN',
|
||||
);
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" cond="$m" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<block loop="Context::getMetaTag() => $no, $val">
|
||||
<meta http-equiv="{$val['name']}"|cond="$val['is_http_equiv']" name="{$val['name']}"|cond="!$val['is_http_equiv']" content="{$val['content']}">
|
||||
<meta http-equiv="{$val['name']}"|cond="$val['is_http_equiv']" name="{$val['name']}"|cond="!$val['is_http_equiv']" content="{$val['content']}" />
|
||||
</block>
|
||||
|
||||
<!-- TITLE -->
|
||||
|
|
@ -39,6 +39,9 @@
|
|||
<link cond="$mobicon_url" rel="apple-touch-icon" href="{$mobicon_url}" />
|
||||
|
||||
<!-- OTHER HEADERS -->
|
||||
<block loop="Context::getOpenGraphData() => $og_metadata">
|
||||
<meta property="{$og_metadata['property']}" content="{$og_metadata['content']}" />
|
||||
</block>
|
||||
{Context::getHtmlHeader()}
|
||||
|
||||
<!-- COMMON JS VARIABLES -->
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue