| | |
| | | <?php |
| | | namespace JVBase\managers\Notifications; |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\CustomTable; |
| | | use JVBase\managers\Emails; |
| | | use JVBase\managers\NotificationManager; |
| | | use JVBase\managers\queue\Queue; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Registrar; |
| | | use WP_Error; |
| | | use WP_Query; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | |
| | | class EmailDigests |
| | | { |
| | | protected string $campaign; |
| | | protected Cache $terms; |
| | | protected Cache $users; |
| | | protected CustomTable $userIndex; |
| | | protected CustomTable $termIndex; |
| | | public function __construct() |
| | |
| | | protected function registerUserIndex():void |
| | | { |
| | | $table = CustomTable::for('user_notification_email_digest'); |
| | | // $types = implode(',',array_map(function($item) { return "'{$item}'"; }, Registrar::getFeatured('favouritable'))); |
| | | // $types = implode(',',array_map(function($item) { return "'{$item}'"; }, Registrar::withFeature('favouritable'))); |
| | | $table->setColumns([ |
| | | 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', |
| | | 'user_id' => "{$table->getUserIDType()} NOT NULL", |
| | |
| | | protected function registerTermIndex():void |
| | | { |
| | | $table = CustomTable::for('user_notification_email_digest'); |
| | | $types = implode(',',array_map(function($item) { return "'{$item}'"; }, Registrar::getFeatured('favouritable', 'term'))); |
| | | $types = implode(',',array_map(function($item) { return "'{$item}'"; }, Registrar::withFeature('favouritable', 'term'))); |
| | | $table->setColumns([ |
| | | 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', |
| | | 'term_id' => "{$table->getTermIDType()} NOT NULL", |
| | |
| | | return; |
| | | } |
| | | |
| | | JVB()->queue()->add( |
| | | Queue::getInstance()->add( |
| | | 'email_notification_digest', |
| | | 0, |
| | | [ |
| | |
| | | if (!in_array($frequency, ['never', 'daily', 'weekly', 'monthly'])) { |
| | | return []; |
| | | } |
| | | return JVB()->notification()->getUsersByFrequency($frequency); |
| | | return NotificationManager::getInstance()->getUsersByFrequency($frequency); |
| | | } |
| | | |
| | | public function processOperation(WP_Error|array $result, object $operation, array $data):WP_Error|array |
| | |
| | | |
| | | protected function generateUserDigest(int $userID, string $frequency):string|false |
| | | { |
| | | $subscription = JVB()->notification()->getUserSubscriptions($userID, $frequency); |
| | | $subscription = NotificationManager::getInstance()->getUserSubscriptions($userID, $frequency); |
| | | |
| | | //TODO |
| | | $notifications = JVB()->notification()->getUserNotifications($userID); |
| | | $notifications = NotificationManager::getInstance()->getUserNotifications($userID); |
| | | |
| | | if (empty($subscription) && empty($notifications)) { |
| | | return false; |
| | |
| | | $content = ''; |
| | | foreach ($subscription as $item) { |
| | | $temp = match ($item['item_type']) { |
| | | array_merge(['user'], Registrar::getFeatured('favouritable', 'user')) => $this->getUserUpdates($item['item_id'], $frequency), |
| | | Registrar::getFeatured('favouritable', 'term') => $this->getTermUpdates($item['item_id'], $item['item_type'], $frequency), |
| | | array_merge(['user'], Registrar::withFeature('favouritable', 'user')) => $this->getUserUpdates($item['item_id'], $frequency), |
| | | Registrar::withFeature('favouritable', 'term') => $this->getTermUpdates($item['item_id'], $item['item_type'], $frequency), |
| | | default => false, |
| | | }; |
| | | if ($temp) { |
| | |
| | | $image = sprintf( |
| | | '<a href="%s">%s</a>', |
| | | $url, |
| | | JVB()->email()->image($image[0], get_post_meta($img, '_wp_attachment_image_alt', true)) |
| | | Emails::getInstance()->image($image[0], get_post_meta($img, '_wp_attachment_image_alt', true)) |
| | | ); |
| | | $t = sprintf( |
| | | '<a href="%s">%s</a>', |
| | | $url, |
| | | $m->get('post_title') |
| | | ); |
| | | $output[] = JVB()->email()->card($image, $t); |
| | | $output[] = Emails::getInstance()->card($image, $t); |
| | | } |
| | | |
| | | $final = JVB()->email()->button($goTo, 'Want to See More?'); |
| | | return JVB()->email()->grid($output, 3, 'New from '.$title,'',$final); |
| | | $final = Emails::getInstance()->button($goTo, 'Want to See More?'); |
| | | return Emails::getInstance()->grid($output, 3, 'New from '.$title,'',$final); |
| | | } |
| | | |
| | | protected function getTermUpdates(int $termID, string $type, string $frequency):string|false |
| | |
| | | $image = sprintf( |
| | | '<a href="%s">%s</a>', |
| | | $url, |
| | | JVB()->email()->image($image[0], get_post_meta($img, '_wp_attachment_image_alt', true)) |
| | | Emails::getInstance()->image($image[0], get_post_meta($img, '_wp_attachment_image_alt', true)) |
| | | ); |
| | | $t = sprintf( |
| | | '<a href="%s">%s</a>', |
| | | $url, |
| | | $m->get('post_title') |
| | | ); |
| | | $output[] = JVB()->email()->card($image, $t); |
| | | $output[] = Emails::getInstance()->card($image, $t); |
| | | } |
| | | |
| | | $final = JVB()->email()->button($goTo, 'See More'); |
| | | return JVB()->email()->grid($output, 3, 'New in '.$title, '', $final); |
| | | $final = Emails::getInstance()->button($goTo, 'See More'); |
| | | return Emails::getInstance()->grid($output, 3, 'New in '.$title, '', $final); |
| | | } |
| | | } |