77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Tasks;
|
|
|
|
use App\Models\Post;
|
|
use App\Notifications\PostIncomplete;
|
|
use Notification;
|
|
|
|
class SchedulePublishTask
|
|
{
|
|
public static function handle($post_id, $post_status = 'publish')
|
|
{
|
|
sleep(2);
|
|
|
|
$post = Post::find($post_id);
|
|
|
|
if (is_null($post)) {
|
|
return;
|
|
}
|
|
|
|
if (! in_array($post->status, ['future', 'draft', 'publish'])) {
|
|
return;
|
|
}
|
|
|
|
if ((is_empty($post->title)) || (is_empty($post->slug)) || (is_empty($post->main_keyword)) || (is_empty($post->keywords)) || (is_empty($post->bites)) || (is_empty($post->featured_image)) || (is_empty($post->body)) || (is_empty($post->metadata))) {
|
|
Notification::route(get_notification_channel(), get_notification_user_id())->notify(new PostIncomplete($post));
|
|
|
|
return;
|
|
}
|
|
|
|
/*
|
|
TODO:
|
|
|
|
- to determine a published_at time, first check if there are any post with existing published_at date.
|
|
|
|
- if there are no other posts except for the current post, then the current post published_at is now().
|
|
|
|
- if there are other posts but all of them published_at is null, then the current post published_at is now().
|
|
|
|
- if there are other posts and there are non null published_at,
|
|
-- first find the latest published post (latest published_at).
|
|
-- if the latest published_at datetime is before now, then published_at is null.
|
|
-- if the latest published_at datetime is after now, then current post published_at should be 1 hour after the latest published_at
|
|
|
|
-- the idea is published_posts should be spreaded accross by an hour if found.
|
|
|
|
*/
|
|
|
|
// Check if there are any other posts with a set published_at date
|
|
$latest_published_post = Post::where('id', '!=', $post_id)->whereNotNull('published_at')->orderBy('published_at', 'DESC')->first();
|
|
|
|
//dd($latest_published_post);
|
|
|
|
if (is_null($latest_published_post)) {
|
|
$post->published_at = now();
|
|
} else {
|
|
if ($latest_published_post->published_at->lt(now())) {
|
|
|
|
$new_time = now();
|
|
|
|
} else {
|
|
|
|
$new_time = clone $latest_published_post->published_at;
|
|
|
|
}
|
|
|
|
$new_time->addMinutes(rand(40, 60));
|
|
$post->published_at = $new_time;
|
|
}
|
|
|
|
$post->published_at->subDays(1);
|
|
|
|
$post->status = $post_status; // Assuming you want to update the status to future
|
|
$post->save();
|
|
}
|
|
}
|