Last active
February 11, 2019 21:22
-
-
Save eonarik/3cc20c5b1ebcf78f9daeeb3e66de2241 to your computer and use it in GitHub Desktop.
modx snippet addMeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// v.1.1.3 | |
$expires = $modx->getOption('expires', $scriptProperties, 3600); | |
$noindex = $modx->getOption('noindex', $scriptProperties, false); | |
$keywordsField = $modx->getOption('keywordsField', $scriptProperties, 'keywords'); | |
$imageField = $modx->getOption('imageField', $scriptProperties, 'image'); | |
$viewport = $modx->getOption('viewport', $scriptProperties, 'width=device-width, initial-scale=1'); | |
$hasSchema = $modx->getOption('hasSchema', $scriptProperties, true); | |
$hasOpenGraph = $modx->getOption('hasOpenGraph', $scriptProperties, true); | |
$hasTwitterCard = $modx->getOption('hasTwitterCard', $scriptProperties, true); | |
$twitterCardType = $modx->getOption('twitterCardType', $scriptProperties, 'summary'); | |
$productTemplates = $modx->getOption('productTemplates', $scriptProperties, null); | |
$extMetaProperties = $modx->getOption('extMetaProperties', $scriptProperties, array()); | |
$out = array( | |
'<meta http-equiv="Content-Type" content="text/html; charset=' . $modx->getOption('modx_charset', null, 'utf-8') . '" />', | |
'<meta http-equiv="X-UA-Compatible" content="IE=Edge" />', | |
); | |
// Viewport | |
if (!empty($viewport) && $viewport) | |
{ | |
$out[] = '<meta name="viewport" content="' . $viewport . '" />'; | |
} | |
// Noindex | |
if ($noindex OR !$modx->resource->searchable) | |
{ | |
$out[] = '<meta name="Robots" content="NOINDEX, NOFOLLOW" />'; | |
} | |
// Control cache | |
if ($modx->resource->editedon) | |
{ | |
$out[] = '<meta http-equiv="Last-Modified" content="' . date('D, d M Y H:i:s T', $modx->resource->editedon) . '" />'; | |
} | |
$out[] = '<meta http-equiv="Expires" content="' . date('D, d M Y H:i:s T', time() + $expires) . '" />'; | |
// Canonical | |
if ($modx->resource->id == $modx->getOption('site_start')) | |
{ | |
$out[] = '<link rel="canonical" href="' . $modx->getOption('site_url') . '" />'; | |
} | |
else | |
{ | |
$out[] = '<link rel="canonical" href="' . $modx->getOption('site_url') . $modx->makeURL($modx->resource->id) . '" />'; | |
} | |
// Meta Seo | |
if ( | |
$descr = $modx->resource->description | |
AND is_string($descr) | |
) | |
{ | |
$out[] = '<meta name="description" content="' . htmlspecialchars($descr) . '" />'; | |
} | |
// keywords | |
if (!function_exists('getKW')) | |
{ | |
function getKW ($object, $keywordsField) | |
{ | |
global $modx; | |
$out = array(); | |
if ( | |
( | |
$kw = $object->getTVValue($keywordsField) | |
OR $kw = $object->$keywordsField | |
) | |
AND !empty($kw) | |
AND is_string($kw) | |
) | |
{ | |
$out[] = $kw; | |
} | |
else | |
{ | |
if ($parentObject = $modx->getObject('modResource', $object->parent)) | |
{ | |
$out[] = getKW($parentObject, $keywordsField); | |
} | |
} | |
return implode(',', $out); | |
} | |
} | |
if ( | |
$keywords = getKW($modx->resource, $keywordsField) | |
AND !empty($keywords) | |
) | |
{ | |
$out[] = '<meta name="keywords" content="' . htmlspecialchars($keywords) . '" />'; | |
} | |
if ( | |
( | |
$image = $modx->resource->getTVValue($imageField) | |
OR $image = $modx->resource->get($imageField) | |
) | |
AND is_string($image) | |
) | |
{ | |
$image = $modx->getOption('site_url') . ltrim($image, '/'); | |
$out[] = '<meta name="image" content="' . $image . '" />'; | |
} | |
else | |
{ | |
$image = ''; | |
} | |
if (!is_array($extMetaProperties)) | |
{ | |
$extMetaProperties = json_decode($extMetaProperties, true); | |
} | |
// schema.org | |
if ($hasSchema) | |
{ | |
if ($pagetitle = $modx->resource->get('pagetitle')) | |
{ | |
$out[] = '<meta itemprop="name" content="' . htmlspecialchars($pagetitle) . '" />'; | |
} | |
if (!empty($descr)) | |
{ | |
$out[] = '<meta itemprop="description" content="' . htmlspecialchars($descr) . '" />'; | |
} | |
if (!empty($image)) | |
{ | |
$out[] = '<meta itemprop="image" content="' . $image . '" />'; | |
} | |
} | |
// open graph | |
if ($hasOpenGraph) | |
{ | |
if ($pagetitle = $modx->resource->get('pagetitle')) | |
{ | |
$out[] = '<meta name="og:title" content="' . htmlspecialchars($pagetitle) . '" />'; | |
} | |
if (!empty($descr)) | |
{ | |
$out[] = '<meta name="og:description" content="' . htmlspecialchars($descr) . '" />'; | |
} | |
if (!empty($image)) | |
{ | |
$out[] = '<meta name="og:image" content="' . $image . '" />'; | |
} | |
$out[] = '<meta name="og:url" content="' . $modx->getOption('site_url') . '" />'; | |
$out[] = '<meta name="og:site" content="' . $modx->getOption('site_name') . '" />'; | |
if ($locale = $modx->getOption('locale')) | |
{ | |
$out[] = '<meta name="og:locale" content="' . $locale . '" />'; | |
} | |
$out[] = '<meta name="og:type" content="website" />'; | |
} | |
// twitter card | |
if ($hasTwitterCard) | |
{ | |
if ($twitterCardType) { | |
// summary – обычная карточка с картинкой, кратким описанием и заголовком | |
// summary_large_image – карточка с большой картинкой, описанием и заголовком | |
// photo – карточка с фото | |
// gallery – карточка с четырьмя фотографиями | |
// player – Видеоплеер с описанием | |
// product – карточка с описанием какого-либо продукта | |
if ($productTemplates && in_array($modx->resource->template, array_map('intval', explode(',', $productTemplates)))) { | |
$twitterCardType = 'product'; | |
// $out[] = '<meta name="twitter:label1" content="PRICE" />'; | |
// $out[] = '<meta name="twitter:data1" content="' . intval($modx->resource->get('price')) . '" />'; | |
} | |
$out[] = '<meta name="twitter:card" content="' . $twitterCardType . '" />'; | |
} | |
$out[] = '<meta name="twitter:site" content="' . $modx->getOption('site_url') . '" />'; | |
if ($pagetitle = $modx->resource->get('pagetitle')) | |
{ | |
$out[] = '<meta name="twitter:title" content="' . htmlspecialchars($pagetitle) . '" />'; | |
} | |
if (!empty($descr)) | |
{ | |
$out[] = '<meta name="twitter:description" content="' . htmlspecialchars($descr) . '" />'; | |
} | |
if (!empty($image)) | |
{ | |
$out[] = '<meta name="twitter:image" content="' . $image . '" />'; | |
} | |
} | |
// extended properties | |
if (!empty($extMetaProperties)) | |
{ | |
foreach ($extMetaProperties as $key => $value) { | |
$value = htmlspecialchars($value); | |
$out[] = "<meta property=\"{$key}\" content=\"{$value}\" />"; | |
} | |
} | |
return implode("\n", $out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment