Skip to content

Commit

Permalink
Merge branch 'release/3.4.5' into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Aug 30, 2021
2 parents 9618fea + 8bf31b8 commit 01dae8b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# SEOmatic Changelog

## 3.4.5 - 2021.08.29
### Changed
* Further optimized the CP requests by eliminating redundant calls to `loadMetaContainers()` and also only calling `addDynamicMetaToContainers()` on headless requests
* Added more fine-grained profiling to the `DynamicHelper` class

### Fixed
* Fix a bug where SEO image settings could be inherited incorrectly ([#962](https://github.com/nystudio107/craft-seomatic/issues/962))
* Fixed an issue where elements with newly added SEO Settings field wouldn't appear in the sitemap ([#964](https://github.com/nystudio107/craft-seomatic/issues/964))

## 3.4.4 - 2021.08.26
### Fixed
* Ensure metacontainers are loaded for new elements that have an SEO Settings field in their layout ([#960](https://github.com/nystudio107/craft-seomatic/issues/960))
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-seomatic",
"description": "SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.",
"type": "craft-plugin",
"version": "3.4.4",
"version": "3.4.5",
"keywords": [
"craft",
"cms",
Expand Down
3 changes: 1 addition & 2 deletions src/fields/SeoSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ public function getInputHtml($value, ElementInterface $element = null): string
Seomatic::$plugin->metaContainers->previewMetaContainers($element->uri, $element->siteId, true);
}

$source = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element);
$contentMeta = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId($source[1], $source[0], $element->siteId, $source[4]);
$contentMeta = Seomatic::$plugin->metaBundles->getContentMetaBundleForElement($element);
$globalMeta = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($element->siteId);

$variables['parentBundles'] = [$contentMeta, $globalMeta];
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/DynamicMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public static function addDynamicMetaToContainers(string $uri = null, int $siteI
*/
public static function addOpeningHours(MetaJsonLd $jsonLd, Entity $entity)
{
Craft::beginProfile('DynamicMeta::addOpeningHours', __METHOD__);
if ($jsonLd instanceof LocalBusiness && $entity !== null) {
/** @var LocalBusiness $jsonLd */
$openingHours = [];
Expand Down Expand Up @@ -374,6 +375,7 @@ public static function addOpeningHours(MetaJsonLd $jsonLd, Entity $entity)
}
$jsonLd->openingHoursSpecification = $openingHours;
}
Craft::endProfile('DynamicMeta::addOpeningHours', __METHOD__);
}

/**
Expand All @@ -384,6 +386,7 @@ public static function addOpeningHours(MetaJsonLd $jsonLd, Entity $entity)
*/
public static function addContactPoints(MetaJsonLd $jsonLd, Entity $entity)
{
Craft::beginProfile('DynamicMeta::addContactPoints', __METHOD__);
if ($jsonLd instanceof Organization && $entity !== null) {
/** @var Organization $jsonLd */
$contactPoints = [];
Expand All @@ -400,6 +403,7 @@ public static function addContactPoints(MetaJsonLd $jsonLd, Entity $entity)
}
$jsonLd->contactPoint = $contactPoints;
}
Craft::endProfile('DynamicMeta::addContactPoints', __METHOD__);
}

/**
Expand All @@ -409,6 +413,7 @@ public static function addContactPoints(MetaJsonLd $jsonLd, Entity $entity)
*/
public static function addMetaJsonLdBreadCrumbs(int $siteId = null)
{
Craft::beginProfile('DynamicMeta::addMetaJsonLdBreadCrumbs', __METHOD__);
$position = 0;
if ($siteId === null) {
$siteId = Craft::$app->getSites()->currentSite->id
Expand Down Expand Up @@ -510,6 +515,7 @@ public static function addMetaJsonLdBreadCrumbs(int $siteId = null)
if (!empty($crumbs->itemListElement)) {
Seomatic::$plugin->jsonLd->add($crumbs);
}
Craft::endProfile('DynamicMeta::addMetaJsonLdBreadCrumbs', __METHOD__);
}

/**
Expand All @@ -520,6 +526,7 @@ public static function addMetaJsonLdBreadCrumbs(int $siteId = null)
*/
public static function addMetaLinkHrefLang(string $uri = null, int $siteId = null)
{
Craft::beginProfile('DynamicMeta::addMetaLinkHrefLang', __METHOD__);
$siteLocalizedUrls = self::getLocalizedUrls($uri, $siteId);

if (!empty($siteLocalizedUrls)) {
Expand Down Expand Up @@ -556,13 +563,15 @@ public static function addMetaLinkHrefLang(string $uri = null, int $siteId = nul
$ogLocaleAlternate->content = $ogContentArray;
}
}
Craft::endProfile('DynamicMeta::addMetaLinkHrefLang', __METHOD__);
}

/**
* Add the Same As meta tags and JSON-LD
*/
public static function addSameAsMeta()
{
Craft::beginProfile('DynamicMeta::addSameAsMeta', __METHOD__);
$metaContainers = Seomatic::$plugin->metaContainers;
$sameAsUrls = [];
if (!empty($metaContainers->metaSiteVars->sameAsLinks)) {
Expand All @@ -580,6 +589,7 @@ public static function addSameAsMeta()
if ($identity !== null && property_exists($identity, 'sameAs')) {
$identity->sameAs = $sameAsUrls;
}
Craft::endProfile('DynamicMeta::addSameAsMeta', __METHOD__);
}

/**
Expand All @@ -596,6 +606,7 @@ public static function addSameAsMeta()
*/
public static function getLocalizedUrls(string $uri = null, int $siteId = null): array
{
Craft::beginProfile('DynamicMeta::getLocalizedUrls', __METHOD__);
$localizedUrls = [];
// No pagination params for URLs
$urlParams = null;
Expand Down Expand Up @@ -746,6 +757,7 @@ public static function getLocalizedUrls(string $uri = null, int $siteId = null):
];
}
}
Craft::endProfile('DynamicMeta::getLocalizedUrls', __METHOD__);

return $localizedUrls;
}
Expand Down
16 changes: 12 additions & 4 deletions src/models/MetaSitemapVars.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,18 @@ public static function create(array $config = [])
public function init()
{
// Enforce types
$this->sitemapUrls = (bool)$this->sitemapUrls;
$this->sitemapAssets = (bool)$this->sitemapAssets;
$this->sitemapFiles = (bool)$this->sitemapFiles;
$this->sitemapAltLinks = (bool)$this->sitemapAltLinks;
if ($this->sitemapUrls !== null) {
$this->sitemapUrls = (bool)$this->sitemapUrls;
}
if ($this->sitemapAssets !== null) {
$this->sitemapAssets = (bool)$this->sitemapAssets;
}
if ($this->sitemapFiles !== null) {
$this->sitemapFiles = (bool)$this->sitemapFiles;
}
if ($this->sitemapAltLinks !== null) {
$this->sitemapAltLinks = (bool)$this->sitemapAltLinks;
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/services/MetaBundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class MetaBundles extends Component
*/
protected $globalMetaBundles = [];

/**
* @var array parent meta bundles for elements
*/
protected $elementContentMetaBundles = [];

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -605,6 +610,24 @@ public function getContentMetaBundlesForSiteId($sourceSiteId, $filter = ''): arr
return $metaBundles;
}

/**
* Get the parent content meta bundle for a given element.
*
* @param Element $element
* @return mixed|MetaBundle|null
*/
public function getContentMetaBundleForElement(Element $element)
{
$source = $this->getMetaSourceFromElement($element);
$key = implode(".", $source) . '.' . $element->siteId;

if (empty($this->elementContentMetaBundles[$key])) {
$this->elementContentMetaBundles[$key] = $this->getMetaBundleBySourceId($source[1], $source[0], $element->siteId, $source[4]);
}

return $this->elementContentMetaBundles[$key];
}

/**
* Set fields the user is unable to edit to an empty string, so they are
* filtered out when meta containers are combined
Expand Down
36 changes: 35 additions & 1 deletion src/services/MetaContainers.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class MetaContainers extends Component
const GLOBALS_CACHE_KEY = 'parsed_globals_';
const SCRIPTS_CACHE_KEY = 'body_scripts_';

/** @var array Rules for replacement values on arbitrary empty values */
const COMPOSITE_SETTING_LOOKUP = [
'ogImage' => [
'metaBundleSettings.ogImageSource' => 'sameAsSeo.seoImage',
],
'twitterImage' => [
'metaBundleSettings.twitterImageSource' => 'sameAsSeo.seoImage',
],
];

/**
* @event InvalidateContainerCachesEvent The event that is triggered when SEOmatic
* is about to clear its meta container caches
Expand Down Expand Up @@ -214,7 +224,10 @@ public function loadMetaContainers(string $uri = '', int $siteId = null)
$this->loadGlobalMetaContainers($siteId);
$this->loadContentMetaContainers();
$this->loadFieldMetaContainers();
DynamicMetaHelper::addDynamicMetaToContainers($uri, $siteId);
// We only need the dynamic data for headless requests
if (Seomatic::$headlessRequest) {
DynamicMetaHelper::addDynamicMetaToContainers($uri, $siteId);
}
} else {
$cache = Craft::$app->getCache();
list($this->metaGlobalVars, $this->metaSiteVars, $this->metaSitemapVars, $this->metaContainers) = $cache->getOrSet(
Expand Down Expand Up @@ -366,6 +379,10 @@ public function previewMetaContainers(
bool $parseVariables = false,
bool $includeElement = true
) {
// If we've already previewed the containers for this request, there's no need to do it again
if (Seomatic::$previewingMetaContainers && !Seomatic::$headlessRequest) {
return;
}
// It's possible this won't exist at this point
if (!Seomatic::$seomaticVariable) {
// Create our variable and stash it in the plugin for global access
Expand Down Expand Up @@ -859,6 +876,23 @@ protected function loadFieldMetaContainers()
/** @var MetaBundle $metaBundle */
$metaBundle = $element->$fieldHandle;
Seomatic::$plugin->metaBundles->pruneFieldMetaBundleSettings($metaBundle, $fieldHandle);

// See which properties have to be overridden, because the parent bundle says so.
foreach (self::COMPOSITE_SETTING_LOOKUP as $settingName => $rules) {
if (empty($metaBundle->metaGlobalVars->{$settingName})) {
$parentBundle = Seomatic::$plugin->metaBundles->getContentMetaBundleForElement($element);

foreach ($rules as $settingPath => $action) {
list ($container, $property) = explode('.', $settingPath);
list ($testValue, $sourceSetting) = explode('.', $action);

if ($parentBundle->{$container}->{$property} == $testValue) {
$metaBundle->metaGlobalVars->{$settingName} = $metaBundle->metaGlobalVars->{$sourceSetting};
}
}
}
}

// Handle re-creating the `mainEntityOfPage` so that the model injected into the
// templates has the appropriate attributes
$generalContainerKey = MetaJsonLdContainer::CONTAINER_TYPE.JsonLdService::GENERAL_HANDLE;
Expand Down

0 comments on commit 01dae8b

Please sign in to comment.