Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit page title to 60-70 chars for SEO #677

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions app/Support/helpers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Merge data together and ensure duplicate keys can't exist by throwing an error.
*
Expand All @@ -15,27 +16,72 @@ function merge()

foreach ((array) $params as $key => $value) {
// Require only arrays
if (! is_array($value)) {
throw new Exception('Merged variables must be type array, but a '.gettype($value).' was given. '."\n\n".'Value: '.$value);
if (!is_array($value)) {
throw new Exception('Merged variables must be type array, but a ' . gettype($value) . ' was given. ' . "\n\n" . 'Value: ' . $value);
}

// Make sure every key has a string name
collect($value)->each(function ($value, $key) {
if (! is_string($key)) {
throw new Exception('Merged variables must have key as a string, but a '.gettype($key).' was given. '."\n\n Key: ".$key."\n Values: ".(is_array($value) ? json_encode(array_keys($value)) : $value));
if (!is_string($key)) {
throw new Exception('Merged variables must have key as a string, but a ' . gettype($key) . ' was given. ' . "\n\n Key: " . $key . "\n Values: " . (is_array($value) ? json_encode(array_keys($value)) : $value));
}
});

// Make sure there aren't any similiar keys
if (collect($value)->diffKeys($merged)->count() != count($value)) {
throw new Exception('Array key conflict. Update your array to not conflict with other keys: '."\n\n Values:".json_encode(array_keys($value)));
throw new Exception('Array key conflict. Update your array to not conflict with other keys: ' . "\n\n Values:" . json_encode(array_keys($value)));
}

// Merge the arrays
$merged = $merged->merge($value);
}

return $merged->toArray();
$merged = $merged->toArray();

// Add computed title tag
if (!empty($merged['base']) && !array_key_exists('title', $merged['base']['meta'])) {
$merged['base']['meta']['title'] = stringify_page_title($merged['base']);
}

return $merged;
}

/**
* Calculate the title tag based in priority information about the page
*
* @param array $data
* @return string
*/
function stringify_page_title($data)
{
$titles = [];
$char_length = 0;

// Page title (except homepages)
if (!empty($data['page']['title'])
&& !empty($data['server']['path'])
&& $data['server']['path'] != '/'
&& $data['server']['path'] != rtrim($data['site']['subsite-folder'], '/')
) {
$titles[] = $data['page']['title'];
$char_length += strlen($data['page']['title']);
}

// Site title (if there is enough room)
if (!empty($data['site']['title'])
&& ($char_length + 22) < 60
) {
$titles[] = $data['site']['title'];
$char_length += strlen($data['site']['title']);
}

// University name (if there is room)
if (($char_length + 22) <= 70) {
$titles[] = 'Wayne State University';
}

// Use dash separators
return implode(' - ', $titles);
}

/**
Expand Down
1 change: 0 additions & 1 deletion resources/views/components/head-title.blade.php

This file was deleted.

2 changes: 1 addition & 1 deletion resources/views/layouts/main.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
@include('components.meta')

<title>@include('components.head-title')</title>
<title>{{ $base['meta']['title'] ?? 'Wayne State University' }}</title>

<link rel="icon" type="image/x-icon" href="https://wayne.edu/favicon.ico">
<link rel="stylesheet" href="{{ mix('_resources/css/main.css') }}">
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Support/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\Support;

use PHPUnit\Framework\Attributes\Test;
use Illuminate\Support\Str;
use Tests\TestCase;

final class HelpersTest extends TestCase
Expand Down Expand Up @@ -42,4 +43,59 @@ public function merge_should_merge_arrays(): void

$this->assertEquals(['test1' => 'Test1', 'test2' => 'Test2'], $merge);
}

#[Test]
public function merge_should_stringify_page_title(): void
{
// Default keys
$pre_merge['base']['site']['title'] = '';
$pre_merge['base']['site']['subsite-folder'] = '';
$pre_merge['base']['server']['path'] = '';

// If there is already a page title, do nothing
$pre_merge['base']['meta']['title'] = $this->faker->sentence();
$post_merge = merge($pre_merge);
$this->assertEquals($pre_merge, $post_merge);
unset($pre_merge['base']['meta']['title']);

// If on homepage, do not include page title
$pre_merge['base']['page']['title'] = $this->faker->word();
$pre_merge['base']['server']['path'] = '';
$post_merge = merge($pre_merge);
$this->assertEquals('Wayne State University', $post_merge['base']['meta']['title']);

// If on a regaular page
$pre_merge['base']['page']['title'] = $this->faker->word();
$pre_merge['base']['server']['path'] = '/path';
$post_merge = merge($pre_merge);
$this->assertEquals(
$pre_merge['base']['page']['title'] . ' - Wayne State University',
$post_merge['base']['meta']['title']
);

// If there is a site title
$pre_merge['base']['page']['title'] = $this->faker->word();
$pre_merge['base']['site']['title'] = $this->faker->word();
$post_merge = merge($pre_merge);
$this->assertEquals(
$pre_merge['base']['page']['title'] . ' - ' . $pre_merge['base']['site']['title'] . ' - Wayne State University',
$post_merge['base']['meta']['title']
);

// Page title longer than 38 chars, drop the site name
$pre_merge['base']['page']['title'] = Str::random(40);
$post_merge = merge($pre_merge);
$this->assertEquals(
$pre_merge['base']['page']['title'] . ' - Wayne State University',
$post_merge['base']['meta']['title']
);

// Page title longer than 48 chars, drop Wayne State University
$pre_merge['base']['page']['title'] = Str::random(50);
$post_merge = merge($pre_merge);
$this->assertEquals(
$pre_merge['base']['page']['title'],
$post_merge['base']['meta']['title']
);
}
}