Skip to content

feat:[LAR-85] add articles Approuved in sitemap #261

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

Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ composer.phar
/public/hot
/public/storage
/public/media
/public/sitemap.xml
/public/**/*.xml
/storage/*.key
/storage/framework/cache
.env
Expand Down
21 changes: 21 additions & 0 deletions app/Actions/Article/ApprovedArticleAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Actions\Article;

use App\Gamify\Points\ArticlePublished;
use App\Models\Article;

final class ApprovedArticleAction
{
public function execute(Article $article): Article
{
$article->approved_at = now();
$article->save();

givePoint(new ArticlePublished($article));

return $article;
}
}
63 changes: 30 additions & 33 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,44 @@
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Psr\Http\Message\UriInterface;
use Spatie\Sitemap\SitemapGenerator;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\SitemapIndex;
use Spatie\Sitemap\Tags\Url;

final class GenerateSitemap extends Command
{
protected $signature = 'sitemap:generate';

protected $description = 'Crawl the site to generate a sitemap.xml file';

/**
* @var array|string[]
*/
private array $noIndexPaths = [
'',
'/forum/*',
'/user/*',
];
protected $description = 'Generate the sitemap';

public function handle(): void
{
SitemapGenerator::create(config('app.url'))
->shouldCrawl(fn (UriInterface $url) => $this->shouldIndex($url->getPath()))
->hasCrawled(function (Url $url) {
if ($this->shouldNotIndex($url->path())) {
return;
}

return $url;
})
->writeToFile(public_path('sitemap.xml'));
}

private function shouldNotIndex(string $path): bool
{
return Str::is($this->noIndexPaths, $path);
}

private function shouldIndex(string $path): bool
{
return ! $this->shouldNotIndex($path);
Sitemap::create()
->add(
Url::create(route('home'))
->setLastModificationDate(now()->subMinutes(10))
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
->setPriority(0.5)
)
->add(
Url::create(route('sponsors'))
->setLastModificationDate(now()->subMinutes(10))
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
->setPriority(0.5)
)
->add(
Url::create(route('about'))
->setLastModificationDate(now()->subMinutes(10))
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
->setPriority(0.5)
)
->writeToFile(public_path('sitemaps/base_sitemap.xml'));

$sitemap = SitemapIndex::create()
->add('/sitemaps/base_sitemap.xml')
->add('/sitemaps/discussion_sitemap.xml')
->add('/sitemaps/blog_sitemap.xml');

$sitemap->writeToFile(public_path('sitemap.xml'));
}
}
27 changes: 27 additions & 0 deletions app/Console/Commands/Sitemap/GenerateArticlesSitemapCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands\Sitemap;

use App\Models\Article;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;

final class GenerateArticlesSitemapCommand extends Command
{
protected $signature = 'sitemap:blog-generate';

protected $description = 'Generate the articles sitemaps.';

public function handle(): void
{
$sitemap = Sitemap::create();

Article::query()->whereNotNull('approved_at')->each(function ($article) use ($sitemap): void {
$sitemap->add($article); // @phpstan-ignore-line
});

$sitemap->writeToFile(public_path('sitemaps/blog_sitemap.xml'));
}
}
27 changes: 27 additions & 0 deletions app/Console/Commands/Sitemap/GenerateDiscussionsSitemapCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands\Sitemap;

use App\Models\Discussion;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;

final class GenerateDiscussionsSitemapCommand extends Command
{
protected $signature = 'sitemap:discussion-generate';

protected $description = 'Generate the discussions sitemaps.';

public function handle(): void
{
$sitemap = Sitemap::create();

Discussion::query()->whereHas('replies')->each(function ($discussion) use ($sitemap): void {
$sitemap->add($discussion); // @phpstan-ignore-line
});

$sitemap->writeToFile(public_path('sitemaps/discussion_sitemap.xml'));
}
}
7 changes: 2 additions & 5 deletions app/Filament/Resources/ArticleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Filament\Resources;

use App\Actions\Article\ApprovedArticleAction;
use App\Filament\Resources\ArticleResource\Pages;
use App\Gamify\Points\ArticlePublished;
use App\Models\Article;
use Filament\Resources\Resource;
use Filament\Support\Enums\MaxWidth;
Expand Down Expand Up @@ -89,10 +89,7 @@ public static function table(Table $table): Table
->action(function ($record): void {
Gate::authorize('approve', $record);

$record->approved_at = now();
$record->save();

givePoint(new ArticlePublished($record));
app(ApprovedArticleAction::class)->execute($record);
}),
Action::make('declined')
->visible(fn (Article $record) => $record->isAwaitingApproval())
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Components/Discussion/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function comments(): Collection
{
$replies = collect();

// @phpstan-ignore-next-line
foreach ($this->discussion->replies->load(['allChildReplies', 'user']) as $reply) {
/** @var Reply $reply */
if ($reply->allChildReplies->isNotEmpty()) {
Expand Down
29 changes: 20 additions & 9 deletions app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
use App\Traits\HasTags;
use App\Traits\Reactable;
use App\Traits\RecordsActivity;
use Carbon\Carbon;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;

/**
* @property-read int $id
Expand All @@ -33,17 +36,17 @@
* @property int $user_id
* @property string | null $locale
* @property-read User $user
* @property \Carbon\Carbon | null $published_at
* @property \Carbon\Carbon | null $submitted_at
* @property \Carbon\Carbon | null $approved_at
* @property \Carbon\Carbon | null $shared_at
* @property \Carbon\Carbon | null $declined_at
* @property \Carbon\Carbon | null $sponsored_at
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property Carbon | null $published_at
* @property Carbon | null $submitted_at
* @property Carbon | null $approved_at
* @property Carbon | null $shared_at
* @property Carbon | null $declined_at
* @property Carbon | null $sponsored_at
* @property Carbon $created_at
* @property Carbon $updated_at
* @property \Illuminate\Database\Eloquent\Collection | Tag[] $tags
*/
final class Article extends Model implements HasMedia, ReactableInterface, Viewable
final class Article extends Model implements HasMedia, ReactableInterface, Sitemapable, Viewable
{
use HasAuthor;
use HasFactory;
Expand Down Expand Up @@ -96,6 +99,14 @@ public function newEloquentBuilder($query): ArticleQueryBuilder
return new ArticleQueryBuilder($query);
}

public function toSitemapTag(): Url
{
return Url::create(route('articles.show', $this))
->setLastModificationDate(Carbon::create($this->updated_at)) // @phpstan-ignore-line
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.5);
}

public function excerpt(int $limit = 110): string
{
return Str::limit(strip_tags((string) md_to_html($this->body)), $limit);
Expand Down
13 changes: 12 additions & 1 deletion app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;

/**
* @property-read int $id
Expand All @@ -41,8 +43,9 @@
* @property Carbon $updated_at
* @property User $user
* @property Collection | SpamReport[] $spamReports
* @property Collection | Reply[] $replies
*/
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
final class Discussion extends Model implements ReactableInterface, ReplyInterface, Sitemapable, SpamReportableContract, SubscribeInterface, Viewable
{
use HasAuthor;
use HasFactory;
Expand Down Expand Up @@ -107,6 +110,14 @@ public function excerpt(int $limit = 110): string
return Str::limit(strip_tags((string) md_to_html($this->body)), $limit);
}

public function toSitemapTag(): Url
{
return Url::create(route('discussions.show', $this))
->setLastModificationDate(Carbon::create($this->updated_at)) // @phpstan-ignore-line
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.5);
}

public function isPinned(): bool
{
return $this->is_pinned;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"spatie/laravel-feed": "^4.2.1",
"spatie/laravel-google-fonts": "^1.2.3",
"spatie/laravel-permission": "^6.10.0",
"spatie/laravel-sitemap": "^7.2.1",
"spatie/laravel-sitemap": "^7.3",
"stevebauman/location": "^7.4.0",
"symfony/http-client": "^7.1.8",
"symfony/mailgun-mailer": "^7.1",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added public/sitemaps/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;

Artisan::command('inspire', function (): void {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();

Schedule::command('sitemap:blog-generate')->dailyAt('01:00');
Schedule::command('sitemap:discussion-generate')->dailyAt('01:10');
Schedule::command('sitemap:generate')->dailyAt('02:00');
Loading