Skip to content

Commit

Permalink
Add more control over site name position (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Aug 5, 2022
1 parent a9c2e2c commit ebdbc79
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 21 deletions.
1 change: 1 addition & 0 deletions content/content.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
seo_site_name_position: end
seo_generate_social_images: false
seo_twitter_card: summary
seo_canonical_type: current
Expand Down
1 change: 0 additions & 1 deletion content/general.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
title_separator: '|'
title_position: before
site_json_ld_type: none
use_breadcrumbs: true
5 changes: 5 additions & 0 deletions resources/lang/en/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
'default_instructions' => 'Configure the default title and description of your :type.',
],

'seo_site_name_position' => [
'instructions' => 'Set the site name position for the meta title of this :type.',
'default_instructions' => 'Set the default site name position for the meta title of your :type.',
],

'seo_title' => [
'instructions' => 'Set the meta title of this :type.',
'default_instructions' => 'Set the default meta title of your :type.',
Expand Down
16 changes: 16 additions & 0 deletions src/Fields/ContentDefaultsFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public function titleAndDescription(): array
'character_limit' => 160,
],
],
[
'handle' => 'seo_site_name_position',
'field' => [
'type' => 'button_group',
'display' => 'Site Name Position',
'instructions' => $this->trans('seo_site_name_position', 'default_instructions'),
'options' => [
'end' => 'End',
'start' => 'Start',
'disabled' => 'Disabled',
],
'default' => Defaults::data('collections')->get('seo_site_name_position'),
'localizable' => true,
'listable' => false,
],
],
];
}

Expand Down
19 changes: 2 additions & 17 deletions src/Fields/GeneralFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function general(): array
'listable' => 'hidden',
'display' => 'Site Name',
'instructions' => 'The site name is added to your meta titles.',
'width' => 50,
],
],
[
Expand All @@ -57,29 +58,13 @@ public function general(): array
'push_tags' => false,
'cast_booleans' => false,
'type' => 'select',
'instructions' => 'The separator between the site name and meta title.',
'instructions' => 'This separates the site name and page title.',
'width' => 50,
'listable' => 'hidden',
'display' => 'Title Separator',
'default' => Defaults::data('site::general')->get('title_separator'),
],
],
[
'handle' => 'title_position',
'field' => [
'options' => [
'before' => 'Before',
'after' => 'After',
],
'default' => Defaults::data('site::general')->get('title_position'),
'localizable' => true,
'type' => 'button_group',
'instructions' => 'Display the meta title before or after the site name.',
'listable' => false,
'display' => 'Title Position',
'width' => 50,
],
],
[
'handle' => 'section_knowledge_graph',
'field' => [
Expand Down
19 changes: 19 additions & 0 deletions src/Fields/OnPageSeoFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ public function titleAndDescription(): array
],
],
],
[
'handle' => 'seo_site_name_position',
'field' => [
'display' => 'Site Name Position',
'instructions' => $this->trans('seo_site_name_position', 'instructions'),
'type' => 'seo_source',
'default' => '@default',
'localizable' => true,
'classes' => 'button_group-fieldtype',
'field' => [
'type' => 'button_group',
'options' => [
'end' => 'End',
'start' => 'Start',
'disabled' => 'Disabled',
],
],
],
],
];
}

Expand Down
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ServiceProvider extends AddonServiceProvider

protected $updateScripts = [
Updates\CreateSocialImagesTheme::class,
Updates\MigrateSiteNamePosition::class,
];

protected $routes = [
Expand Down
75 changes: 75 additions & 0 deletions src/Updates/MigrateSiteNamePosition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Aerni\AdvancedSeo\Updates;

use Aerni\AdvancedSeo\Facades\Seo;
use Illuminate\Support\Arr;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Facades\Term;
use Statamic\UpdateScripts\UpdateScript;

class MigrateSiteNamePosition extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('1.3.0');
}

public function update()
{
// The mapping of old and new values.
$mapping = [
'before' => 'end',
'after' => 'start',
];

// Get all localizations and make sure they exist.
$siteDefaults = Seo::find('site', 'general')
?->ensureLocalizations(Site::all())
->localizations();

// Get the title positions from each localization.
$titlePositions = $siteDefaults
->map(fn ($data) => $data->get('title_position'))
->filter()
->map(fn ($value) => Arr::get($mapping, $value));

// Set the site name position for all collection defaults.
Seo::allOfType('collections')->each(function ($collection) use ($titlePositions) {
$titlePositions->each(function ($value, $site) use ($collection) {
$collection->in($site)
?->set('seo_site_name_position', $value)
->save();
});
});

// Set the site name position for all taxonomy defaults.
Seo::allOfType('taxonomies')->each(function ($taxonomy) use ($titlePositions) {
$titlePositions->each(function ($value, $site) use ($taxonomy) {
$taxonomy->in($site)
?->set('seo_site_name_position', $value)
->save();
});
});

// Set the site name position on entries.
Entry::query()
->whereNull('origin') // Only set on default site.
->whereNotNull('seo_title')
->get()
->each(fn ($entry) => $entry->set('seo_site_name_position', '@default')->saveQuietly());

// Set the site name position on terms.
Term::all()
->filter(fn ($term) => $term->has('seo_title'))
->each(fn ($term) => $term->set('seo_site_name_position', '@default')->save());

// Remove the old title position from the site defaults.
$siteDefaults
->filter(fn ($default) => $default->has('title_position')) // Prevent saving of defaults with no title position
->each(fn ($default) => $default->remove('title_position')->save());

$this->console()->info("Successfully migrated 'title_position' to 'site_name_position'.");
}
}
11 changes: 8 additions & 3 deletions src/View/Cascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,14 @@ protected function applyWhitelist(): self

protected function compiledTitle(): string
{
return $this->value('title_position')->value() === 'before'
? "{$this->title()} {$this->titleSeparator()} {$this->siteName()}"
: "{$this->siteName()} {$this->titleSeparator()} {$this->title()}";
$position = $this->value('site_name_position')?->value();

return match (true) {
($position === 'end') => "{$this->title()} {$this->titleSeparator()} {$this->siteName()}",
($position === 'start') => "{$this->siteName()} {$this->titleSeparator()} {$this->title()}",
($position === 'disabled') => $this->title(),
default => "{$this->title()} {$this->titleSeparator()} {$this->siteName()}",
};
}

protected function title(): string
Expand Down

0 comments on commit ebdbc79

Please sign in to comment.