60 lines
2.1 KiB
PHP
60 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\CmsPage;
|
|
use App\Services\IndexNowService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Throwable;
|
|
|
|
final class SubmitIndexNow extends Command
|
|
{
|
|
protected $signature = 'indexnow:submit {urls?* : Absolute URLs to submit} {--all : Submit all public indexable SonarDesk URLs}';
|
|
protected $description = 'Submit public SonarDesk URLs to IndexNow';
|
|
|
|
public function handle(IndexNowService $indexNow): int
|
|
{
|
|
if (! config('indexnow.enabled')) {
|
|
$this->warn('IndexNow is disabled. Set INDEXNOW_ENABLED=true to enable submissions.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$urls = $this->argument('urls');
|
|
if ($this->option('all') || $urls === []) {
|
|
$urls = $this->allPublicUrls();
|
|
}
|
|
|
|
try {
|
|
$response = $indexNow->submit($urls);
|
|
} catch (Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
return self::FAILURE;
|
|
}
|
|
|
|
if ($response === null || ! $response->successful()) {
|
|
$this->error('IndexNow rejected the submission'.($response ? ' (HTTP '.$response->status().').' : '.'));
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->info('Submitted '.count($urls).' URL(s) to IndexNow (HTTP '.$response->status().').');
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
/** @return array<int, string> */
|
|
private function allPublicUrls(): array
|
|
{
|
|
$routes = ['home', 'features', 'buy', 'release', 'support', 'privacy', 'terms', 'refund', 'cookies'];
|
|
$urls = collect($routes)->filter(fn (string $name) => Route::has($name))->map(fn (string $name) => route($name));
|
|
|
|
$pages = CmsPage::query()
|
|
->where('is_published', true)
|
|
->where('robots_index', true)
|
|
->where(fn ($query) => $query->whereNull('published_at')->orWhere('published_at', '<=', now()))
|
|
->pluck('slug')
|
|
->map(fn (string $slug) => route('cms.show', ['slug' => $slug]));
|
|
|
|
return $urls->merge($pages)->unique()->values()->all();
|
|
}
|
|
}
|