| 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\Framework\Support\Arr; |
| 8 |
use FluentCommunity\Framework\Support\DateTime; |
| 9 |
use FluentCommunity\App\Services\Helper; |
| 10 |
|
| 11 |
class Scheduler |
| 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 |
}, 10); |
| 19 |
|
| 20 |
add_action('fluent_community_send_daily_digest_init', function () { |
| 21 |
do_action('fluent_community_send_daily_digest'); |
| 22 |
}, 10); |
| 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 |
$this->maybeRemoveOldScheuledActionLogs(); |
| 28 |
}, 10); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
public function checkDailyDigestSchedule($willReset = false) |
| 33 |
{ |
| 34 |
$notificationSettings = Utility::getEmailNotificationSettings(); |
| 35 |
$globalStatus = Arr::get($notificationSettings, 'digest_email_status', 'no'); |
| 36 |
|
| 37 |
if ($globalStatus != 'yes') { |
| 38 |
// Global Status is false |
| 39 |
// Check if any user enabled that or not |
| 40 |
$isEnabled = NotificationSubscription::query()->where('notification_type', 'digest_mail') |
| 41 |
->where('is_read', 1) |
| 42 |
->exists(); |
| 43 |
|
| 44 |
if (!$isEnabled) { |
| 45 |
// unset the scheduled action |
| 46 |
if (\as_next_scheduled_action('fluent_community_send_daily_digest_init')) { |
| 47 |
\as_unschedule_all_actions('fluent_community_send_daily_digest_init', [], 'fluent-community'); |
| 48 |
} |
| 49 |
return; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
$notificationDay = Arr::get($notificationSettings, 'digest_mail_day'); |
| 54 |
$digestTime = Arr::get($notificationSettings, 'daily_digest_time', '09:00'); |
| 55 |
|
| 56 |
// Let's check if we have the daily digest action scheduled or not |
| 57 |
if (!\as_next_scheduled_action('fluent_community_send_daily_digest_init')) { |
| 58 |
$timestamp = $this->getNextOccurrenceTimestamp(Helper::getFullDayName($notificationDay), $digestTime); |
| 59 |
if ($timestamp) { |
| 60 |
\as_schedule_single_action($timestamp, 'fluent_community_send_daily_digest_init', [], 'fluent-community', true); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
private function maybeRemoveOldScheuledActionLogs($group_slug = 'fluent-community', $days_old = 7) |
| 66 |
{ |
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
// Get the timestamp for 7 days ago |
| 70 |
$cutoff_date = gmdate('Y-m-d H:i:s', strtotime("-{$days_old} days")); |
| 71 |
|
| 72 |
// Get the group ID |
| 73 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 74 |
$group_id = $wpdb->get_var($wpdb->prepare( |
| 75 |
"SELECT group_id FROM {$wpdb->prefix}actionscheduler_groups WHERE slug = %s", |
| 76 |
$group_slug |
| 77 |
)); |
| 78 |
|
| 79 |
if (!$group_id) { |
| 80 |
return false; // Group not found |
| 81 |
} |
| 82 |
|
| 83 |
// Delete old actions and their associated logs |
| 84 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 85 |
$deleted = $wpdb->query($wpdb->prepare(" |
| 86 |
DELETE a, l |
| 87 |
FROM {$wpdb->prefix}actionscheduler_actions a |
| 88 |
LEFT JOIN {$wpdb->prefix}actionscheduler_logs l ON a.action_id = l.action_id |
| 89 |
WHERE a.group_id = %d |
| 90 |
AND a.status IN ('complete', 'failed') |
| 91 |
AND a.scheduled_date_gmt < %s", $group_id, $cutoff_date)); |
| 92 |
|
| 93 |
// Clean up orphaned claims |
| 94 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 95 |
$wpdb->query(" |
| 96 |
DELETE c |
| 97 |
FROM {$wpdb->prefix}actionscheduler_claims c |
| 98 |
LEFT JOIN {$wpdb->prefix}actionscheduler_actions a ON c.claim_id = a.claim_id |
| 99 |
WHERE a.action_id IS NULL"); |
| 100 |
|
| 101 |
return $deleted; |
| 102 |
} |
| 103 |
|
| 104 |
private function getNextOccurrenceTimestamp($dayname, $time) |
| 105 |
{ |
| 106 |
// Ensure dayname is lowercase and valid |
| 107 |
$dayname = strtolower($dayname); |
| 108 |
$valid_days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; |
| 109 |
if (!in_array($dayname, $valid_days)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
// Get current time in WordPress timezone |
| 114 |
$current = current_datetime(); |
| 115 |
|
| 116 |
// Create target DateTime with "next" modifier in WordPress timezone |
| 117 |
$target = new \DateTime('next ' . $dayname . ' ' . $time, wp_timezone()); |
| 118 |
|
| 119 |
$targetDate = $target->format('Ymd'); |
| 120 |
$currentDate = $current->format('Ymd'); |
| 121 |
|
| 122 |
// If it's the same day and time has passed, move to next week |
| 123 |
if ($targetDate === $currentDate || $currentDate > $targetDate) { |
| 124 |
$target->modify('+7 days'); |
| 125 |
} |
| 126 |
|
| 127 |
// Switch to UTC timezone and get timestamp |
| 128 |
$target->setTimezone(new \DateTimeZone('UTC')); |
| 129 |
return $target->getTimestamp(); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|