Jake Vanderwerf
2 days ago 235ce5716edc2f7cbe80fdccf26eac7269587839
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace JVBase\managers\Notifications;
 
use JVBase\managers\Cache;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
use WP_Query;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
/**
 * ContentNotifications
 *  Gets notifications of
 **/
class Content
{
    protected static Cache $cache;
    public function __construct()
    {
        self::$cache = Cache::for('content_notifications')->connect('post');
    }
    public static function getSinceDate(string $date):string
    {
        return match ($date) {
            'week' => '-1 week',
            'month' => '-1 month',
            default => '-1 day',
        };
    }
    public static function forUser(int $userID, ?string $sinceDate = null, bool $modified = false):array
    {
        $date = self::getSinceDate($sinceDate);
 
        $posts = new WP_Query([
            'posts_per_page'    => -1,
            'post_status'       => 'publish',
            'post_author'       => $userID,
            'date_query'        => [
                [
                    'after'     => $date,
                    'inclusive' => true,
                    'column'    => $modified ? 'post_modified' : 'post_date'
                ]
            ],
            'fields' => 'ids',
        ]);
        wp_reset_postdata();
        return $posts->posts;
    }
 
    public static function forTerm(int $termID, string $taxonomy, ?string $sinceDate = null, bool $modified = false):array
    {
        if (!in_array($taxonomy, ['category', 'tag'])){
            $taxonomy = jvbCheckBase($taxonomy);
        }
        $date = self::getSinceDate($sinceDate);
        $posts = new WP_Query([
            'posts_per_page'    => -1,
            'post_status'       => 'publish',
            'date_query'        => [
                [
                    'after'     => $date,
                    'inclusive' => true,
                    'column'    => $modified ? 'post_modified' : 'post_date'
                ]
            ],
            'tax_query'         => [
                [
                    'taxonomy'  => $taxonomy,
                    'terms'     => $termID,
                ]
            ],
            'fields'    => 'ids',
        ]);
        wp_reset_postdata();
        return $posts->posts;
    }
 
    public static function formatForNotification(array $IDs, string $type):array
    {
        $posts = [];
        foreach ($IDs as $ID) {
            $posts[] = self::$cache->remember(
                $ID,
                function() use ($ID) {
                    $meta = Meta::forPost($ID);
                    $img = $meta->get('post_thumbnail');
                    $registrar = Registrar::getInstance(get_post_type($ID));
                    return array_merge([
                        'url'       => get_the_permalink($ID),
                        'content'   => $registrar->getType(),
                        'icon'      => $registrar->getIcon(),
                        'image'     => (!empty($img) ? jvbImageData($img) : [])
                    ],
                        $meta->getAll([
                            'post_date',
                            'post_modified',
                            'post_title',
                            'post_excerpt',
                        ])
                    );
                }
            );
        }
        return $posts;
    }
}