Permalink
Cannot retrieve contributors at this time
113 lines (101 sloc)
3.56 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
craft-seomatic/src/models/MetaLinkContainer.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 | |
/** | |
* SEOmatic plugin for Craft CMS 3.x | |
* | |
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, | |
* and flexible | |
* | |
* @link https://nystudio107.com | |
* @copyright Copyright (c) 2017 nystudio107 | |
*/ | |
namespace nystudio107\seomatic\models; | |
use nystudio107\seomatic\Seomatic; | |
use nystudio107\seomatic\base\MetaContainer; | |
use nystudio107\seomatic\helpers\ImageTransform as ImageTransformHelper; | |
use Craft; | |
use yii\caching\TagDependency; | |
/** | |
* @author nystudio107 | |
* @package Seomatic | |
* @since 3.0.0 | |
*/ | |
class MetaLinkContainer extends MetaContainer | |
{ | |
// Constants | |
// ========================================================================= | |
const CONTAINER_TYPE = 'MetaLinkContainer'; | |
// Public Properties | |
// ========================================================================= | |
/** | |
* The data in this container | |
* | |
* @var MetaLink[] $data | |
*/ | |
public $data = []; | |
// Public Methods | |
// ========================================================================= | |
/** | |
* @inheritdoc | |
*/ | |
public function includeMetaData($dependency) | |
{ | |
Craft::beginProfile('MetaLinkContainer::includeMetaData', __METHOD__); | |
$uniqueKey = $this->handle.$dependency->tags[3]; | |
$cache = Craft::$app->getCache(); | |
if ($this->clearCache) { | |
TagDependency::invalidate($cache, $dependency->tags[3]); | |
} | |
$tagData = $cache->getOrSet( | |
$this::CONTAINER_TYPE.$uniqueKey, | |
function () use ($uniqueKey) { | |
Craft::info( | |
$this::CONTAINER_TYPE.' cache miss: '.$uniqueKey, | |
__METHOD__ | |
); | |
$tagData = []; | |
if ($this->prepForInclusion()) { | |
/** @var MetaLink $metaLinkModel */ | |
foreach ($this->data as $metaLinkModel) { | |
if ($metaLinkModel->include) { | |
$configs = $metaLinkModel->tagAttributesArray(); | |
foreach ($configs as $config) { | |
if ($metaLinkModel->prepForRender($config)) { | |
$tagData[] = $config; | |
// If `devMode` is enabled, validate the Meta Link and output any model errors | |
if (Seomatic::$devMode) { | |
$metaLinkModel->debugMetaItem( | |
'Link attribute: ' | |
); | |
} | |
} | |
} | |
} | |
} | |
} | |
return $tagData; | |
}, | |
Seomatic::$cacheDuration, | |
$dependency | |
); | |
// Invalidate the cache we just created if there were pending image transforms in it | |
if (ImageTransformHelper::$pendingImageTransforms) { | |
TagDependency::invalidate($cache, $dependency->tags[3]); | |
} | |
// Register the tags | |
foreach ($tagData as $config) { | |
Seomatic::$view->registerLinkTag($config); | |
} | |
Craft::endProfile('MetaLinkContainer::includeMetaData', __METHOD__); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function normalizeContainerData() | |
{ | |
parent::normalizeContainerData(); | |
foreach ($this->data as $key => $config) { | |
$config['key'] = $key; | |
$this->data[$key] = MetaLink::create($key, $config); | |
} | |
} | |
} |