Skip to content

Commit

Permalink
Merge branch 'master' into content-linking
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Dec 15, 2024
2 parents 87b6ea3 + d1ca00d commit e7e6ea8
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 38 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php: [8.1, 8.2, 8.3]
php: [8.1, 8.2, 8.3, 8.4]
laravel: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
os: [ubuntu-latest]
Expand All @@ -26,9 +26,15 @@ jobs:
php: 8.3
laravel: 11.*
stability: prefer-stable
- os: windows-latest
php: 8.4
laravel: 11.*
stability: prefer-stable
exclude:
- php: 8.1
laravel: 11.*
- php: 8.4
laravel: 10.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
}
},
"require": {
"statamic/cms": "^5.0.0",
"statamic/cms": "^5.41",
"donatello-za/rake-php-plus": "^1.0",
"openai-php/client": "^0.10.1",
"ext-dom": "*"
},
"require-dev": {
"orchestra/testbench": "^8.0 || ^9.0",
"phpunit/phpunit": "^10.0",
"orchestra/testbench": "^8.28 || ^9.6.1",
"phpunit/phpunit": "^10.5.35",
"laravel/pint": "^1.17"
},
"config": {
Expand Down
5 changes: 4 additions & 1 deletion src/Directives/SeoProDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function renderTag($tag, $context)
);
}

return $this->setContext($context)->$tag();
return $this
->setContext($context)
->setParameters([])
->$tag();
}

protected function isMissingContext($context)
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public function index()
$content = Cache::remember(Sitemap::CACHE_KEY.'_index', $cacheUntil, function () {
return view('seo-pro::sitemap_index', [
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
'sitemaps' => Sitemap::paginatedSitemaps(),
'sitemaps' => app(Sitemap::class)->paginatedSitemaps(),
])->render();
});
} else {
$content = Cache::remember(Sitemap::CACHE_KEY, $cacheUntil, function () {
return view('seo-pro::sitemap', [
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
'pages' => Sitemap::pages(),
'pages' => app(Sitemap::class)->pages(),
])->render();
});
}
Expand All @@ -45,7 +45,7 @@ public function show($page)
$cacheKey = Sitemap::CACHE_KEY.'_'.$page;

$content = Cache::remember($cacheKey, $cacheUntil, function () use ($page) {
abort_if(empty($pages = Sitemap::paginatedPages($page)), 404);
abort_if(empty($pages = app(Sitemap::class)->paginatedPages($page)), 404);

return view('seo-pro::sitemap', [
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
Expand Down
64 changes: 37 additions & 27 deletions src/Sitemap/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\SeoPro\Sitemap;

use Illuminate\Support\Collection as IlluminateCollection;
use Illuminate\Support\LazyCollection;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
Expand All @@ -16,36 +18,32 @@ class Sitemap

const CACHE_KEY = 'seo-pro.sitemap';

public static function pages()
public function pages(): array
{
$sitemap = new static;

return collect()
->merge($sitemap->getPages($sitemap->publishedEntries()))
->merge($sitemap->getPages($sitemap->publishedTerms()))
->merge($sitemap->getPages($sitemap->publishedCollectionTerms()))
->sortBy(function ($page) {
return substr_count(rtrim($page->path(), '/'), '/');
})
->merge($this->publishedEntries())
->merge($this->publishedTerms())
->merge($this->publishedCollectionTerms())
->pipe(fn ($pages) => $this->getPages($pages))
->sortBy(fn ($page) => substr_count(rtrim($page->path(), '/'), '/'))
->values()
->map
->toArray();
->toArray()
->all();
}

public static function paginatedPages(int $page)
public function paginatedPages(int $page): array
{
$sitemap = new static;

$perPage = config('statamic.seo-pro.sitemap.pagination.limit', 100);
$offset = ($page - 1) * $perPage;
$remaining = $perPage;

$pages = collect([]);
$pages = collect();

$entryCount = $sitemap->publishedEntriesCount() - 1;
$entryCount = $this->publishedEntriesCount() - 1;

if ($offset < $entryCount) {
$entries = $sitemap->publishedEntriesQuery()->offset($offset)->limit($perPage)->get();
$entries = $this->publishedEntriesForPage($page, $perPage);

if ($entries->count() < $remaining) {
$remaining -= $entries->count();
Expand All @@ -58,8 +56,9 @@ public static function paginatedPages(int $page)
$offset = max($offset - $entryCount, 0);

$pages = $pages->merge(
collect($sitemap->publishedTerms())
->merge($sitemap->publishedCollectionTerms())
collect()
->merge($this->publishedTerms())
->merge($this->publishedCollectionTerms())
->skip($offset)
->take($remaining)
);
Expand All @@ -69,18 +68,18 @@ public static function paginatedPages(int $page)
return [];
}

return $sitemap->getPages($pages)
return $this
->getPages($pages)
->values()
->map
->toArray();
->toArray()
->all();
}

public static function paginatedSitemaps()
public function paginatedSitemaps(): array
{
$sitemap = new static;

// would be nice to make terms a count query rather than getting the count from the terms collection
$count = $sitemap->publishedEntriesCount() + $sitemap->publishedTerms()->count() + $sitemap->publishedCollectionTerms()->count();
$count = $this->publishedEntriesCount() + $this->publishedTerms()->count() + $this->publishedCollectionTerms()->count();

$sitemapCount = ceil($count / config('statamic.seo-pro.sitemap.pagination.limit', 100));

Expand Down Expand Up @@ -112,7 +111,7 @@ protected function getPages($items)
->filter();
}

private function publishedEntriesQuery()
protected function publishedEntriesQuery()
{
$collections = Collection::all()
->map(function ($collection) {
Expand All @@ -131,12 +130,23 @@ private function publishedEntriesQuery()
->orderBy('uri');
}

protected function publishedEntries()
protected function publishedEntries(): LazyCollection
{
return $this->publishedEntriesQuery()->lazy();
}

protected function publishedEntriesCount()
protected function publishedEntriesForPage(int $page, int $perPage): IlluminateCollection
{
$offset = ($page - 1) * $perPage;

return $this
->publishedEntriesQuery()
->offset($offset)
->limit($perPage)
->get();
}

protected function publishedEntriesCount(): int
{
return $this->publishedEntriesQuery()->count();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/SeoProTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function metaData()
$metaData['is_twitter_glide_enabled'] = $this->isGlidePresetEnabled('seo_pro_twitter');
$metaData['is_og_glide_enabled'] = $this->isGlidePresetEnabled('seo_pro_og');

return $metaData;
return $this->aliasedResult($metaData);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixtures/site/content/collections/pages/about.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
title: About
subtitle: The Best About Page
updated_by: 96300192-873c-4615-b992-157508a8d7c5
updated_at: 1579284102
template: page
id: 62136fa2-9e5c-4c38-a894-a2753f02f5ff
---
I'm just a kid living in the 90's, writing articles in his secret public journal wonder if someday, somewhere in the future, they will be read by someone.
I'm just a kid living in the 90's, writing articles in his secret public journal wonder if someday, somewhere in the future, they will be read by someone.
48 changes: 48 additions & 0 deletions tests/MetaTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,54 @@ public function it_hydrates_cascade_on_custom_routes_using_blade_directive($view
$this->assertStringContainsStringIgnoringLineEndings('<title>Custom Route Entry Title | Site Name</title>', $content);
}

/** @test */
public function it_can_loop_over_meta_data()
{
$this->withoutExceptionHandling();
$this->prepareViews('antlers');
$this->files->put(resource_path('views-seo-pro/layout.antlers.html'), <<<'EOT'
{{ seo_pro:meta_data }}
<h1>{{ title }}</h1>
<h2>{{ description }}</h2>
<h3>{{ canonical_url }}</h3>
{{ /seo_pro:meta_data }}
EOT);

$content = $this->get('/the-view')->content();
$this->assertStringContainsStringIgnoringLineEndings('<h1>The View</h1>', $content);
$this->assertStringContainsStringIgnoringLineEndings('<h2>A wonderful view!</h2>', $content);
$this->assertStringContainsStringIgnoringLineEndings('<h3>http://cool-runnings.com/the-view</h3>', $content);
}

/** @test */
public function it_can_loop_over_aliased_meta_data()
{
$this->withoutExceptionHandling();

$this
->prepareViews('antlers')
->setSeoOnCollection(Collection::find('pages'), [
'title' => '@seo:subtitle',
]);

$this->files->put(resource_path('views-seo-pro/layout.antlers.html'), <<<'EOT'
<h1 class="title_outside">{{ title }}</h1>
{{ seo_pro:meta_data as="foo" }}
<h1 class="title_inside">{{ title }}</h1>
<h1>{{ foo:title }}</h1>
<h3>{{ foo:canonical_url }}</h3>
<h3 class="canonical_url_without_scoping">{{ canonical_url }}</h3>
{{ /seo_pro:meta_data }}
EOT);

$content = $this->get('/about')->content();
$this->assertStringContainsStringIgnoringLineEndings('<h1 class="title_outside">About</h1>', $content);
$this->assertStringContainsStringIgnoringLineEndings('<h1 class="title_inside">About</h1>', $content);
$this->assertStringContainsStringIgnoringLineEndings('<h1>The Best About Page</h1>', $content);
$this->assertStringContainsStringIgnoringLineEndings('<h3>http://cool-runnings.com/about</h3>', $content);
$this->assertStringContainsStringIgnoringLineEndings('<h3 class="canonical_url_without_scoping"></h3>', $content);
}

protected function setCustomGlidePresetDimensions($app)
{
$app->config->set('statamic.seo-pro.assets', [
Expand Down
Loading

0 comments on commit e7e6ea8

Please sign in to comment.