| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\NotificationSubscription; |
| 7 |
use FluentCommunity\App\Services\Helper; |
| 8 |
use FluentCommunity\Framework\Support\Arr; |
| 9 |
|
| 10 |
class Scheduler |
| 11 |
{ |
| 12 |
|
| 13 |
public function register() |
| 14 |
{ |
| 15 |
add_action('fluent_community_scheduled_hour_jobs', function () { |
| 16 |
$this->checkDailyDigestSchedule(); |
| 17 |
do_action('fluent_community/maybe_delete_draft_medias'); |
| 18 |
}); |
| 19 |
|
| 20 |
add_action('fluent_community_send_daily_digest_init', function () { |
| 21 |
do_action('fluent_community_send_daily_digest'); |
| 22 |
}); |
| 23 |
|
| 24 |
add_action('fluent_community_daily_jobs', function () { |
| 25 |
// let's fire the old email notifications hook |
| 26 |
do_action('fluent_community/remove_old_notifications'); |
| 27 |
|
| 28 |
$this->maybeRemoveOldScheuledActionLogs(); |
| 29 |
}); |
| 30 |
|
| 31 |
add_action('init', function () { |
| 32 |
$this->maybeRemoveOldScheuledActionLogs(); |
| 33 |
}); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public function checkDailyDigestSchedule($willReset = false) |
| 38 |
{ |
| 39 |
$notificationSettings = Utility::getEmailNotificationSettings(); |
| 40 |
$globalStatus = Arr::get($notificationSettings, 'digest_email_status', 'no'); |
| 41 |
|
| 42 |
if ($globalStatus != 'yes') { |
| 43 |
// Global Status is false |
| 44 |
// Check if any user enabled that or not |
| 45 |
$isEnabled = NotificationSubscription::where('notification_type', 'digest_mail') |
| 46 |
->where('is_read', 1) |
| 47 |
->exist(); |
| 48 |
|
| 49 |
if (!$isEnabled) { |
| 50 |
// unset the scheduled action |
| 51 |
if (as_next_scheduled_action('fluent_community_send_daily_digest_init', null, 'fluent-community')) { |
| 52 |
as_unschedule_all_actions('fluent_community_send_daily_digest_init', null, 'fluent-community'); |
| 53 |
} |
| 54 |
return; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
$notificationDay = Arr::get($notificationSettings, 'digest_mail_day'); |
| 59 |
$digestTime = Arr::get($notificationSettings, 'daily_digest_time', '09:00'); |
| 60 |
// Let's check if we have the daily digest action scheduled or not |
| 61 |
if (!as_next_scheduled_action('fluent_community_send_daily_digest_init', null, 'fluent-community')) { |
| 62 |
$notificationDay = 'next ' . $notificationDay . 'day ' . $digestTime; |
| 63 |
$diff = time() - current_time('timestamp'); |
| 64 |
$timeStamp = strtotime($notificationDay) + $diff; |
| 65 |
as_schedule_single_action($timeStamp, 'fluent_community_send_daily_digest_init', null, 'fluent-community', true); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
private function maybeRemoveOldScheuledActionLogs($group_slug = 'fluent-community', $days_old = 7) |
| 70 |
{ |
| 71 |
global $wpdb; |
| 72 |
|
| 73 |
// Get the timestamp for 7 days ago |
| 74 |
$cutoff_date = date('Y-m-d H:i:s', strtotime("-{$days_old} days")); |
| 75 |
|
| 76 |
// Get the group ID |
| 77 |
$group_id = $wpdb->get_var($wpdb->prepare( |
| 78 |
"SELECT group_id FROM {$wpdb->prefix}actionscheduler_groups WHERE slug = %s", |
| 79 |
$group_slug |
| 80 |
)); |
| 81 |
|
| 82 |
if (!$group_id) { |
| 83 |
return false; // Group not found |
| 84 |
} |
| 85 |
|
| 86 |
// Delete old actions and their associated logs |
| 87 |
$deleted = $wpdb->query($wpdb->prepare(" |
| 88 |
DELETE a, l |
| 89 |
FROM {$wpdb->prefix}actionscheduler_actions a |
| 90 |
LEFT JOIN {$wpdb->prefix}actionscheduler_logs l ON a.action_id = l.action_id |
| 91 |
WHERE a.group_id = %d |
| 92 |
AND a.status IN ('complete', 'failed') |
| 93 |
AND a.scheduled_date_gmt < %s", $group_id, $cutoff_date)); |
| 94 |
|
| 95 |
// Clean up orphaned claims |
| 96 |
$wpdb->query(" |
| 97 |
DELETE c |
| 98 |
FROM {$wpdb->prefix}actionscheduler_claims c |
| 99 |
LEFT JOIN {$wpdb->prefix}actionscheduler_actions a ON c.claim_id = a.claim_id |
| 100 |
WHERE a.action_id IS NULL"); |
| 101 |
|
| 102 |
return $deleted; |
| 103 |
} |
| 104 |
} |
| 105 |
|