From ab7f0e4bf5553f3f313aa961d99b1ce0042b7126 Mon Sep 17 00:00:00 2001 From: Paul Dwight Date: Wed, 25 Jan 2023 10:58:03 +0000 Subject: [PATCH] formatting --- composer.json | 5 +- src/Models/Behaviours/HasMetadata.php | 59 +++++++++++-------- src/Models/Metadata.php | 12 ++-- .../Behaviours/HandleMetadata.php | 43 ++++++++------ src/Repositories/MetadataRepository.php | 6 +- src/Traits/SetsMetadata.php | 31 +++++----- src/TwillMetadataServiceProvider.php | 5 +- src/config/metadata.php | 22 +++---- src/config/seotools.php | 44 +++++++------- ...020_05_14_195821_create_metadata_table.php | 2 +- ...101423_update_metadata_for_translation.php | 16 ++--- ...756_update_metadata_description_fields.php | 6 +- ...n_translated_columns_on_metadata_table.php | 10 ++-- src/resources/lang/en/form.php | 6 +- src/resources/lang/en/settings.php | 2 +- 15 files changed, 143 insertions(+), 126 deletions(-) diff --git a/composer.json b/composer.json index 8bebcb6..76af399 100644 --- a/composer.json +++ b/composer.json @@ -33,5 +33,8 @@ "artesaos/seotools": "^1.0" }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "require-dev": { + "laravel/pint": "^1.4" + } } diff --git a/src/Models/Behaviours/HasMetadata.php b/src/Models/Behaviours/HasMetadata.php index 22bfcfc..be48483 100644 --- a/src/Models/Behaviours/HasMetadata.php +++ b/src/Models/Behaviours/HasMetadata.php @@ -1,65 +1,71 @@ 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(); } @@ -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; } } diff --git a/src/Models/Metadata.php b/src/Models/Metadata.php index 546bab7..df7b507 100644 --- a/src/Models/Metadata.php +++ b/src/Models/Metadata.php @@ -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 { @@ -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); @@ -72,6 +70,7 @@ 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']; } @@ -79,6 +78,7 @@ 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']; } @@ -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; } @@ -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 : ''); } @@ -125,5 +126,4 @@ private function getTableColumns() { return Schema::getColumnListing($this->getTable()); } - } diff --git a/src/Repositories/Behaviours/HandleMetadata.php b/src/Repositories/Behaviours/HandleMetadata.php index fad3c90..b9b630d 100644 --- a/src/Repositories/Behaviours/HandleMetadata.php +++ b/src/Repositories/Behaviours/HandleMetadata.php @@ -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'; @@ -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) { @@ -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(); @@ -67,35 +68,39 @@ 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; } @@ -103,8 +108,8 @@ protected function setFieldDefaults( Model $object, $fields) { * @param $key * @return bool */ - protected function isMetadataField($key) { + protected function isMetadataField($key) + { return substr($key, 0, strlen($this->metadataFieldPrefix)) === $this->metadataFieldPrefix; } - } diff --git a/src/Repositories/MetadataRepository.php b/src/Repositories/MetadataRepository.php index ff26b8b..6a27a35 100644 --- a/src/Repositories/MetadataRepository.php +++ b/src/Repositories/MetadataRepository.php @@ -1,13 +1,11 @@ model = $model; } -} \ No newline at end of file +} diff --git a/src/Traits/SetsMetadata.php b/src/Traits/SetsMetadata.php index 51867ef..0fd9c5c 100644 --- a/src/Traits/SetsMetadata.php +++ b/src/Traits/SetsMetadata.php @@ -1,55 +1,58 @@ 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'); } } } } - } diff --git a/src/TwillMetadataServiceProvider.php b/src/TwillMetadataServiceProvider.php index 7f172e5..1ed80a2 100644 --- a/src/TwillMetadataServiceProvider.php +++ b/src/TwillMetadataServiceProvider.php @@ -37,7 +37,7 @@ public function boot() ], 'config'); $this->publishes([ - __DIR__.'/resources/views/' => resource_path('views/vendor/twill-metadata') + __DIR__.'/resources/views/' => resource_path('views/vendor/twill-metadata'), ], 'views'); $this->publishes([ @@ -45,7 +45,8 @@ public function boot() ], 'lang'); } - private function extendBlade() { + private function extendBlade() + { Blade::include('twill-metadata::includes.metadata-fields', 'metadataFields'); Blade::include('twill-metadata::includes.metadata-settings', 'metadataSettings'); } diff --git a/src/config/metadata.php b/src/config/metadata.php index 31ef166..6c6fcaa 100644 --- a/src/config/metadata.php +++ b/src/config/metadata.php @@ -10,17 +10,17 @@ ], 'opengraph_type_options' => [ - ['value' => 'website', 'label' => 'Website',], - ['value' => 'article','label' => 'Article',], - ['value' => 'book', 'label' => 'Book',], + ['value' => 'website', 'label' => 'Website'], + ['value' => 'article', 'label' => 'Article'], + ['value' => 'book', 'label' => 'Book'], ['value' => 'profile', 'label' => 'Profile'], ], 'card_type_options' => [ - [ 'value' => 'summary', 'label' => 'Summary',], - [ 'value' => 'summary_large_image','label' => 'Summary with Large Image',], - [ 'value' => 'app', 'label' => 'App',], - [ 'value' => 'player', 'label' => 'Player'], + ['value' => 'summary', 'label' => 'Summary'], + ['value' => 'summary_large_image', 'label' => 'Summary with Large Image'], + ['value' => 'app', 'label' => 'App'], + ['value' => 'player', 'label' => 'Player'], ], 'mediasParams' => [ @@ -32,10 +32,10 @@ 'minValues' => [ 'width' => 1200, 'height' => 627, - ] - ] + ], + ], ], - ] - ] + ], + ], ]; diff --git a/src/config/seotools.php b/src/config/seotools.php index 579a024..4d400f0 100644 --- a/src/config/seotools.php +++ b/src/config/seotools.php @@ -8,25 +8,25 @@ /* * The default configurations to be used by the meta generator. */ - 'defaults' => [ - 'title' => false, // set false to total remove - 'titleBefore' => false, // Put defaults.title before page title, like 'It's Over 9000! - Dashboard' - 'description' => false, // set false to total remove - 'separator' => ' - ', - 'keywords' => [], - 'canonical' => null, // Set null for using Url::current(), set false to total remove - 'robots' => false, // Set to 'all', 'none' or any combination of index/noindex and follow/nofollow + 'defaults' => [ + 'title' => false, // set false to total remove + 'titleBefore' => false, // Put defaults.title before page title, like 'It's Over 9000! - Dashboard' + 'description' => false, // set false to total remove + 'separator' => ' - ', + 'keywords' => [], + 'canonical' => null, // Set null for using Url::current(), set false to total remove + 'robots' => false, // Set to 'all', 'none' or any combination of index/noindex and follow/nofollow ], /* * Webmaster tags are always added. */ 'webmaster_tags' => [ - 'google' => null, - 'bing' => null, - 'alexa' => null, + 'google' => null, + 'bing' => null, + 'alexa' => null, 'pinterest' => null, - 'yandex' => null, - 'norton' => null, + 'yandex' => null, + 'norton' => null, ], 'add_notranslate_class' => false, @@ -36,12 +36,12 @@ * The default configurations to be used by the opengraph generator. */ 'defaults' => [ - 'title' => false, // set false to total remove + 'title' => false, // set false to total remove 'description' => false, // set false to total remove - 'url' => false, // Set null for using Url::current(), set false to total remove - 'type' => false, - 'site_name' => false, - 'images' => [], + 'url' => false, // Set null for using Url::current(), set false to total remove + 'type' => false, + 'site_name' => false, + 'images' => [], ], ], 'twitter' => [ @@ -58,11 +58,11 @@ * The default configurations to be used by the json-ld generator. */ 'defaults' => [ - 'title' => false, // set false to total remove + 'title' => false, // set false to total remove 'description' => false, // set false to total remove - 'url' => false, // Set null for using Url::current(), set false to total remove - 'type' => 'WebPage', - 'images' => [], + 'url' => false, // Set null for using Url::current(), set false to total remove + 'type' => 'WebPage', + 'images' => [], ], ], ]; diff --git a/src/migrations/2020_05_14_195821_create_metadata_table.php b/src/migrations/2020_05_14_195821_create_metadata_table.php index c8e69cb..24fcdef 100644 --- a/src/migrations/2020_05_14_195821_create_metadata_table.php +++ b/src/migrations/2020_05_14_195821_create_metadata_table.php @@ -29,7 +29,7 @@ public function up() $table->string('meta_describable_type'); $table->timestamps(); - $table->index(['meta_describable_id','meta_describable_type']); + $table->index(['meta_describable_id', 'meta_describable_type']); }); } diff --git a/src/migrations/2021_09_20_101423_update_metadata_for_translation.php b/src/migrations/2021_09_20_101423_update_metadata_for_translation.php index 098fbc5..c51f61e 100644 --- a/src/migrations/2021_09_20_101423_update_metadata_for_translation.php +++ b/src/migrations/2021_09_20_101423_update_metadata_for_translation.php @@ -1,12 +1,14 @@ softDeletes(); }); @@ -21,14 +23,12 @@ public function up() { }); } - public function down() { - Schema::table('metadata', function(Blueprint $table) { + public function down() + { + Schema::table('metadata', function (Blueprint $table) { $table->dropSoftDeletes(); }); Schema::dropIfExists('metadata_translations'); } - - - -} \ No newline at end of file +} diff --git a/src/migrations/2021_09_21_114756_update_metadata_description_fields.php b/src/migrations/2021_09_21_114756_update_metadata_description_fields.php index 23ea6b3..bda87c1 100644 --- a/src/migrations/2021_09_21_114756_update_metadata_description_fields.php +++ b/src/migrations/2021_09_21_114756_update_metadata_description_fields.php @@ -6,7 +6,6 @@ class UpdateMetadataDescriptionFields extends Migration { - /** * Run the migrations. * @@ -14,7 +13,7 @@ class UpdateMetadataDescriptionFields extends Migration */ public function up() { - Schema::table('metadata', function(Blueprint $table) { + Schema::table('metadata', function (Blueprint $table) { $table->text('description')->change(); $table->text('og_description')->change(); }); @@ -27,7 +26,7 @@ public function up() public function down() { - Schema::table('metadata', function(Blueprint $table) { + Schema::table('metadata', function (Blueprint $table) { $table->string('description')->change(); $table->string('og_description')->change(); }); @@ -37,5 +36,4 @@ public function down() $table->string('og_description')->change(); }); } - } diff --git a/src/migrations/2021_09_22_105532_remove_non_translated_columns_on_metadata_table.php b/src/migrations/2021_09_22_105532_remove_non_translated_columns_on_metadata_table.php index 475e529..cbeab13 100644 --- a/src/migrations/2021_09_22_105532_remove_non_translated_columns_on_metadata_table.php +++ b/src/migrations/2021_09_22_105532_remove_non_translated_columns_on_metadata_table.php @@ -1,11 +1,11 @@ dropColumn(['title', 'description', 'og_title', 'og_description', 'canonical_url']); }); } public function down() { - Schema::table('metadata', function(Blueprint $table){ + Schema::table('metadata', function (Blueprint $table) { $table->string('title')->nullable(); $table->text('description')->nullable(); $table->string('og_title')->nullable(); @@ -28,6 +28,4 @@ public function down() $table->string('canonical_url')->nullable(); }); } - - -} \ No newline at end of file +} diff --git a/src/resources/lang/en/form.php b/src/resources/lang/en/form.php index 2704887..878536d 100644 --- a/src/resources/lang/en/form.php +++ b/src/resources/lang/en/form.php @@ -12,7 +12,7 @@ 'title' => [ 'label' => 'Meta Title', 'note' => 'Recommended length 40 - 60 characters', - ], + ], 'description' => [ 'label' => 'Meta Description', 'note' => 'Recommended length 80 - 120 characters', @@ -21,7 +21,7 @@ 'label' => 'Tell search engines not to index this page (noindex tag).', ], 'nofollow' => [ - 'label' => 'Tell search engines not to follow links on this page (nofollow tag).' + 'label' => 'Tell search engines not to follow links on this page (nofollow tag).', ], 'canonical_url' => [ 'label' => 'Canonical URL', @@ -48,4 +48,4 @@ 'placeholder' => 'Select a type', ], ], -]; \ No newline at end of file +]; diff --git a/src/resources/lang/en/settings.php b/src/resources/lang/en/settings.php index b04032a..466beb7 100644 --- a/src/resources/lang/en/settings.php +++ b/src/resources/lang/en/settings.php @@ -14,4 +14,4 @@ 'note' => 'If the organization has a twitter account, add it here. (include the @symbol. e.g @twitter)', ], ], -]; \ No newline at end of file +];