Files
futurewalker/app/Jobs/Tasks/PublishIndexPostTask.php
2023-11-21 19:18:11 +08:00

45 lines
1.1 KiB
PHP

<?php
namespace App\Jobs\Tasks;
use App\Models\Post;
use App\Notifications\PostWasPublished;
use Exception;
use Illuminate\Support\Facades\Notification;
use LaravelFreelancerNL\LaravelIndexNow\Facades\IndexNow;
use LaravelGoogleIndexing;
class PublishIndexPostTask
{
public static function handle(int $post_id)
{
$post = Post::find($post_id);
if (is_null($post)) {
return;
}
$post->status = 'publish';
if ($post->save()) {
if ((app()->environment() == 'production') && (config('platform.global.indexing'))) {
$post_url = route('front.post', ['slug' => $post->slug, 'category_slug' => $post->category->slug]);
try {
IndexNow::submit($post_url);
} catch (Exception) {
}
try {
LaravelGoogleIndexing::create()->update($post_url);
} catch (Exception) {
}
}
Notification::route('facebook', 'default')->notify(new PostWasPublished($post));
}
}
}