Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldwight committed Jan 25, 2023
1 parent bdd47be commit ab7f0e4
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 126 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
"artesaos/seotools": "^1.0"
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"require-dev": {
"laravel/pint": "^1.4"
}
}
59 changes: 35 additions & 24 deletions src/Models/Behaviours/HasMetadata.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,71 @@
<?php


namespace CwsDigital\TwillMetadata\Models\Behaviours;

use A17\Twill\Models\Setting;

trait HasMetadata {

trait HasMetadata
{
public bool $hasMetadata = true;

public function metadata() {
public function metadata()
{
return $this->morphOne('CwsDigital\TwillMetadata\Models\Metadata', 'meta_describable');
}

public function getSocialImageAttribute() {
if ( $this->hasImage('og_image') ) {
public function getSocialImageAttribute()
{
if ($this->hasImage('og_image')) {
return $this->socialImage('og_image');
} elseif( $this->hasSpecifiedMetaFallbackImage('og_image') ) {
} elseif ($this->hasSpecifiedMetaFallbackImage('og_image')) {
return $this->getSpecifiedMetadataFallbackImage('og_image');
} elseif ( $this->hasAnyImages() ) {
} elseif ($this->hasAnyImages()) {
return $this->getDefaultMetadataFallbackImage();
} else {
$media = Setting::firstWhere('key', 'default_social_image');
if($media) {
if ($media) {
return $media->image('default_social_image', 'default');
}
}
}

public function hasSpecifiedMetaFallbackImage($key) {
if( array_key_exists($key, $this->metadataFallbacks ) ) {
return (
!empty($this->metadataFallbacks[$key]) &&
public function hasSpecifiedMetaFallbackImage($key)
{
if (array_key_exists($key, $this->metadataFallbacks)) {
return
! empty($this->metadataFallbacks[$key]) &&
is_array($this->metadataFallbacks[$key]) &&
array_key_exists('role', $this->metadataFallbacks[$key]) &&
array_key_exists('crop', $this->metadataFallbacks[$key])
);
array_key_exists('crop', $this->metadataFallbacks[$key]);
} else {
return false;
}
}

public function getSpecifiedMetadataFallbackImage($key) {
public function getSpecifiedMetadataFallbackImage($key)
{
$role = $this->metadataFallbacks[$key]['role'];
$crop = $this->metadataFallbacks[$key]['crop'];

return $this->socialImage($role, $crop, [], true);
}

public function getDefaultMetadataFallbackImage() {
if ( $this->hasAnyMedias() ) {
public function getDefaultMetadataFallbackImage()
{
if ($this->hasAnyMedias()) {
$media = $this->medias()->first();

return $this->socialImage($media->pivot->role, $media->pivot->crop, [], true);
} elseif ( $this->hasAnyBlockMedias() ) {
} elseif ($this->hasAnyBlockMedias()) {
$block = $this->blocks()->has('medias')->first();
$media = $block->medias()->first();

return $block->socialImage($media->pivot->role, $media->pivot->crop, [], true);
}
}

public function hasAnyImages() {
public function hasAnyImages()
{
return $this->hasAnyMedias() || $this->hasAnyBlockMedias();
}

Expand All @@ -70,24 +76,29 @@ protected function initializeHasMetadata()

// Add the default metadata from config into the $mediasParams array
// by default adds in an 'og_image' role with a 'default' crop
if( isset($this->mediasParams) && is_Array($this->mediasParams)) {
if (isset($this->mediasParams) && is_array($this->mediasParams)) {
$this->mediasParams = array_merge($this->mediasParams, config('metadata.mediasParams'));
} else {
$this->mediasParams = config('metadata.mediasParams');
}
}

public function usesTrait($trait) {
public function usesTrait($trait)
{
return array_key_exists($trait, class_uses($this));
}

public function hasAnyMedias() {
public function hasAnyMedias()
{
$hasMedias = $this->usesTrait('A17\Twill\Models\Behaviors\HasMedias');

return $hasMedias ? $this->medias()->count() : 0;
}

public function hasAnyBlockMedias() {
public function hasAnyBlockMedias()
{
$hasBlocks = $this->usesTrait('A17\Twill\Models\Behaviors\HasBlocks');

return $hasBlocks ? $this->blocks()->has('medias')->count() : 0;
}
}
12 changes: 6 additions & 6 deletions src/Models/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace CwsDigital\TwillMetadata\Models;

use A17\Twill\Repositories\SettingRepository;
use A17\Twill\Models\Behaviors\HasTranslation;
use A17\Twill\Services\Capsules\HasCapsules;
use A17\Twill\Models\Model;
use A17\Twill\Repositories\SettingRepository;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

class Metadata extends Model
{
Expand Down Expand Up @@ -52,7 +50,7 @@ public function field($column)
break;
}

if (!empty($this->$column)) {
if (! empty($this->$column)) {
switch ($column) {
case 'og_type':
return $this->getOgTypeContent($this->$column);
Expand All @@ -72,13 +70,15 @@ protected function getOgTypeContent($id)
{
$og_types = config('metadata.opengraph_type_options');
$key = array_search($id, array_column($og_types, 'value'));

return $og_types[$key]['label'];
}

protected function getCardTypeContent($id)
{
$og_types = config('metadata.card_type_options');
$key = array_search($id, array_column($og_types, 'value'));

return $og_types[$key]['label'];
}

Expand All @@ -87,7 +87,7 @@ protected function getCardTypeContent($id)
*/
protected function getFallbackValue($columnName)
{
if (!array_key_exists($columnName, $this->fallbacks())) {
if (! array_key_exists($columnName, $this->fallbacks())) {
return false;
}

Expand All @@ -106,6 +106,7 @@ protected function getFallbackValue($columnName)
// For title, we'll use the fallback columm and append the site title too.
if ($columnName == 'title') {
$siteTitle = app(SettingRepository::class)->byKey('site_title', 'seo');

return strip_tags($this->meta_describable->$fallbackColumnName).($siteTitle ? ' - '.$siteTitle : '');
}

Expand All @@ -125,5 +126,4 @@ private function getTableColumns()
{
return Schema::getColumnListing($this->getTable());
}

}
43 changes: 24 additions & 19 deletions src/Repositories/Behaviours/HandleMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;

trait HandleMetadata {
trait HandleMetadata
{
// Prefix for metadata fields in form
protected $metadataFieldPrefix = 'metadata';

Expand All @@ -17,8 +18,8 @@ trait HandleMetadata {
/**
* Handle saving of metadata fields from form submission
*
* @param Model $object
* @param array $fields
* @param Model $object
* @param array $fields
*/
public function afterSaveHandleMetadata(Model $object, array $fields)
{
Expand All @@ -33,17 +34,17 @@ public function afterSaveHandleMetadata(Model $object, array $fields)
$metadata = $object->metadata ?? $object->metadata()->create();

$repository->update($metadata->id, $fields);

}

/**
* Prepares the metadata fields for the admin form view
*
* @param Model $object
* @param array $fields
* @param Model $object
* @param array $fields
* @return array
*/
public function getFormFieldsHandleMetadata(Model $object, array $fields){
public function getFormFieldsHandleMetadata(Model $object, array $fields)
{
//If the metadata object doesn't exist create it. Every 'meta_describable' will need one entry.
$metadata = $object->metadata ?? $object->metadata()->create();

Expand All @@ -67,44 +68,48 @@ public function getFormFieldsHandleMetadata(Model $object, array $fields){
* Filters the full fields array down to just the metadata fields
* removes the field prefix and sets the keys correctly for persisting to store
*
* @param array $fields
* @param array $fields
* @return array
*/
protected function getMetadataFields(array $fields) {
protected function getMetadataFields(array $fields)
{
$metadataFields = [];
foreach ( $fields as $key => $value) {
if( $this->isMetadataField($key) ) {
foreach ($fields as $key => $value) {
if ($this->isMetadataField($key)) {
// transform metadata[xxxx] to xxxx
$newKey = preg_replace('/'. $this->metadataFieldPrefix .'\[([^\]]*)\]/', '$1', $key);
$newKey = preg_replace('/'.$this->metadataFieldPrefix.'\[([^\]]*)\]/', '$1', $key);
$metadataFields[$newKey] = $value;
}
}

return $metadataFields;
}

/**
* Set default values on fields that require it
*
* @param Model $object
* @param array $fields
* @param Model $object
* @param array $fields
* @return array
*/
protected function setFieldDefaults( Model $object, $fields) {
foreach( $this->withDefaultValues as $fieldName) {
if( empty($fields[$fieldName]) ) {
protected function setFieldDefaults(Model $object, $fields)
{
foreach ($this->withDefaultValues as $fieldName) {
if (empty($fields[$fieldName])) {
$property = 'metadataDefault'.Str::studly($fieldName);
$fields[$fieldName] = $object->$property;
}
}

return $fields;
}

/**
* @param $key
* @return bool
*/
protected function isMetadataField($key) {
protected function isMetadataField($key)
{
return substr($key, 0, strlen($this->metadataFieldPrefix)) === $this->metadataFieldPrefix;
}

}
6 changes: 2 additions & 4 deletions src/Repositories/MetadataRepository.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php


namespace CwsDigital\TwillMetadata\Repositories;

use A17\Twill\Repositories\ModuleRepository;
use A17\Twill\Repositories\Behaviors\HandleTranslations;
use A17\Twill\Repositories\ModuleRepository;
use CwsDigital\TwillMetadata\Models\Metadata;


class MetadataRepository extends ModuleRepository
{
use HandleTranslations;
Expand All @@ -16,4 +14,4 @@ public function __construct(Metadata $model)
{
$this->model = $model;
}
}
}
31 changes: 17 additions & 14 deletions src/Traits/SetsMetadata.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
<?php

namespace CwsDigital\TwillMetadata\Traits;

use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Database\Eloquent\Model;

trait SetsMetadata {

public function setMetadata(Model $describable) {
trait SetsMetadata
{
public function setMetadata(Model $describable)
{
$metadata = $describable->metadata;

if(!$metadata) return; // Prevent errors if model has no attached metadata
if (! $metadata) {
return;
} // Prevent errors if model has no attached metadata

SEOTools::setTitle($metadata->field('title'));

if( $metadata->field('description') ) {
if ($metadata->field('description')) {
SEOTools::setDescription($metadata->field('description'));
}

SEOTools::opengraph()->setTitle($metadata->field('og_title'));

if($metadata->field('og_description')) {
if ($metadata->field('og_description')) {
SEOTools::opengraph()->setDescription($metadata->field('og_description'));
}

SEOTools::opengraph()->addProperty('type', $metadata->field('og_type'));

if($metadata->field('og_image')) {
if ($metadata->field('og_image')) {
SEOTools::opengraph()->addImage($metadata->field('og_image'));
}

SEOTools::opengraph()->setUrl(request()->url());

if($metadata->field('canonical_url')) {
if ($metadata->field('canonical_url')) {
SEOTools::metatags()->setCanonical($metadata->field('canonical_url'));
}

$noindex = $metadata->field('noindex');
$nofollow = $metadata->field('nofollow');

if( $noindex || $nofollow ) {
if( $noindex && $nofollow ) {
SEOTools::metatags()->setRobots('noindex, nofollow');
if ($noindex || $nofollow) {
if ($noindex && $nofollow) {
SEOTools::metatags()->setRobots('noindex, nofollow');
} else {
if($noindex) {
if ($noindex) {
SEOTools::metatags()->setRobots('noindex');
}
if($nofollow) {
if ($nofollow) {
SEOTools::metatags()->setRobots('nofollow');
}
}
}
}

}
Loading

0 comments on commit ab7f0e4

Please sign in to comment.