Skip to content

Commit

Permalink
Automatically add OpeningHoursSpecifications to Identity and Creator …
Browse files Browse the repository at this point in the history
…JSON-LD

Signed-off-by: Andrew Welch <andrew@nystudio107.com>
  • Loading branch information
khalwat committed Apr 1, 2018
1 parent 8312068 commit a483088
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 29 deletions.
32 changes: 3 additions & 29 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use nystudio107\seomatic\helpers\Field as FieldHelper;
use nystudio107\seomatic\helpers\PullField as PullFieldHelper;
use nystudio107\seomatic\helpers\ArrayHelper;
use nystudio107\seomatic\helpers\DynamicMeta as DynamicMetaHelper;
use nystudio107\seomatic\helpers\ImageTransform as ImageTransformHelper;
use nystudio107\seomatic\models\MetaBundle;
use nystudio107\seomatic\models\MetaScriptContainer;
Expand All @@ -23,7 +24,6 @@
use craft\elements\Asset;
use craft\elements\Category;
use craft\elements\Entry;
use craft\helpers\DateTimeHelper;
use craft\helpers\UrlHelper;
use craft\models\Site;
use craft\web\Controller;
Expand Down Expand Up @@ -801,32 +801,6 @@ public function actionSaveTracking()
// Protected Methods
// =========================================================================

/**
* @inheritdoc
*/
public function normalizeTimes(&$value)
{
if (is_string($value)) {
$value = Json::decode($value);
}
$normalized = [];
$times = ['open', 'close'];
for ($day = 0; $day <= 6; $day++) {
foreach ($times as $time) {
if (
isset($value[$day][$time]) &&
($date = DateTimeHelper::toDateTime($value[$day][$time])) !== false
) {
$normalized[$day][$time] = $date;
} else {
$normalized[$day][$time] = null;
}
}
}

$value = $normalized;
}

/**
* @param array $variables
*/
Expand Down Expand Up @@ -1031,11 +1005,12 @@ protected function getSiteIdFromHandle($siteHandle)

/**
* Prep the entity settings for saving to the db
*
* @param array &$settings
*/
protected function prepEntitySettings(&$settings)
{
$this->normalizeTimes($settings['localBusinessOpeningHours']);
DynamicMetaHelper::normalizeTimes($settings['localBusinessOpeningHours']);
$settings['computedType'] = $this->getSpecificEntityType($settings);
if (!empty($settings['genericImageIds'])) {
$asset = Craft::$app->getAssets()->getAssetById($settings['genericImageIds'][0]);
Expand All @@ -1046,5 +1021,4 @@ protected function prepEntitySettings(&$settings)
}
}
}

}
83 changes: 83 additions & 0 deletions src/helpers/DynamicMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace nystudio107\seomatic\helpers;

use craft\helpers\Json;
use nystudio107\seomatic\models\Entity;
use nystudio107\seomatic\models\jsonld\LocalBusiness;
use nystudio107\seomatic\Seomatic;
use nystudio107\seomatic\models\jsonld\BreadcrumbList;
use nystudio107\seomatic\models\jsonld\Thing;
Expand All @@ -19,6 +22,7 @@
use Craft;
use craft\base\Element;
use craft\errors\SiteNotFoundException;
use craft\helpers\DateTimeHelper;
use craft\helpers\UrlHelper;

use yii\base\Exception;
Expand Down Expand Up @@ -98,6 +102,57 @@ public static function addDynamicMetaToContainers(string $uri = '', int $siteId
self::addMetaJsonLdBreadCrumbs($siteId);
self::addMetaLinkHrefLang();
self::addSameAsMeta();
$metaSiteVars = Seomatic::$plugin->metaContainers->metaSiteVars;
$jsonLd = Seomatic::$plugin->jsonLd->get('identity');
self::addOpeningHours($jsonLd, $metaSiteVars->identity);
$jsonLd = Seomatic::$plugin->jsonLd->get('creator');
self::addOpeningHours($jsonLd, $metaSiteVars->creator);
}
}

/**
* Add the OpeningHoursSpecific to the $jsonLd based on the Entity settings
*
* @param MetaJsonLd $jsonLd
* @param Entity $entity
*/
public static function addOpeningHours(MetaJsonLd $jsonLd, Entity $entity)
{
if ($jsonLd instanceof LocalBusiness && !empty($entity)) {
/** @var LocalBusiness $jsonLd */
$openingHours = [];
$days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
$times = $entity->localBusinessOpeningHours;
$index = 0;
foreach ($times as $hours) {
$openTime = "";
$closeTime = "";
if (!empty($hours['open'])) {
/** @var \DateTime $dateTime */
$dateTime = DateTimeHelper::toDateTime($hours['open']['date'], false, false);
if ($dateTime !== false) {
$openTime = $dateTime->format('H:i:s');
}
}
if (!empty($hours['close'])) {
/** @var \DateTime $dateTime */
$dateTime = DateTimeHelper::toDateTime($hours['close']['date'], false, false);
if ($dateTime !== false) {
$closeTime = $dateTime->format('H:i:s');
}
}
if ($openTime && $closeTime) {
$spec = [
"type" => "OpeningHoursSpecification",
"opens" => $openTime,
"closes" => $closeTime,
"dayOfWeek" => [$days[$index]],
];
$openingHours[] = $spec;
}
$index++;
}
$jsonLd->openingHoursSpecification = $openingHours;
}
}

Expand Down Expand Up @@ -348,4 +403,32 @@ public static function getLocalizedUrls(string $uri = null, int $siteId = null)

return $localizedUrls;
}

/**
* Normalize the array of opening hours passed in
*
* @param $value
*/
public static function normalizeTimes(&$value)
{
if (is_string($value)) {
$value = Json::decode($value);
}
$normalized = [];
$times = ['open', 'close'];
for ($day = 0; $day <= 6; $day++) {
foreach ($times as $time) {
if (isset($value[$day][$time])
&& ($date = DateTimeHelper::toDateTime($value[$day][$time])) !== false
) {
$normalized[$day][$time] = $date;
} else {
$normalized[$day][$time] = null;
}
}
}

$value = $normalized;
}

}

0 comments on commit a483088

Please sign in to comment.