-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathPostFavorites.php
124 lines (108 loc) · 2.9 KB
/
PostFavorites.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Favorites\Entities\Post;
use Favorites\Entities\User\UserRepository;
use Favorites\Entities\Post\FavoriteCount;
/**
* Get the users who have favorited a specific post
*/
class PostFavorites
{
/**
* Post ID
*/
private $post_id;
/**
* Site ID
*/
private $site_id;
/**
* User Role
*/
private $user_role;
/**
* User Repository
* @var Favorites\Entities\User\UserRepository;
*/
private $user_repo;
/**
* Favorite Count
* @var Favorites\Entities\Post\FavoriteCount
*/
private $favorite_count;
public function __construct($post_id, $site_id, $user_role)
{
$this->post_id = ( $post_id ) ? $post_id : get_the_id();
$this->site_id = ( $site_id ) ? $site_id : get_current_blog_id();
$this->user_role = ( $user_role ) ? $user_role : '';
$this->user_repo = new UserRepository;
$this->favorite_count = new FavoriteCount;
}
/**
* Get an array of users who favorited the post
* @return array of user objects
*/
public function getUsers()
{
$users = $this->getAllUsers();
foreach($users as $key => $user){
if ( !$this->user_repo->isFavorite($this->post_id, $this->site_id, $user->ID) ){
unset($users[$key]);
}
}
return $users;
}
/**
* Get all Users
*/
private function getAllUsers()
{
$user_query = new \WP_User_Query([
'blog_id' => ( $this->site_id ) ? $this->site_id : get_current_blog_id(),
'role' => $this->user_role
]);
$users = $user_query->get_results();
return $users;
}
/**
* Get the number of Anonymous Users who have favorited the post
*/
public function anonymousCount()
{
$total_count = $this->favorite_count->getCount($this->post_id, $this->site_id);
$registered_count = count($this->getUsers());
return $total_count - $registered_count;
}
/**
* Get an HTML formatted list of users who have favorited the post
* @param string $separator (list, or string to separate each item)
* @param boolean $include_anonymous
* @param string $anonymous_label
* @param string $anonymous_label_single
*/
public function userList($separator, $include_anonymous, $anonymous_label, $anonymous_label_single)
{
$users = $this->getUsers();
$total = ( $include_anonymous ) ? count($users) + 1 : count($users);
$anonymous_count = $this->anonymousCount();
$out = ( $separator == 'list' ) ? '<ul>' : '';
foreach($users as $key => $user){
if ( $separator == 'list' ) $out .= '<li>';
$out .= $user->display_name;
if ( $separator == 'list' ) {
$out .= '</li>';
} else {
if ( $key + 1 < $total ) $out .= $separator;
}
}
if ( $include_anonymous ){
$label = ( $anonymous_count == 1 ) ? $anonymous_label_single : $anonymous_label;
if ( $separator == 'list' ){
$out .= '<li>' . $anonymous_count . ' ' . $label . '</li>';
} else {
$out .= $anonymous_count . ' ' . $label;
}
}
if ( $separator == 'list' ) $out .= '</ul>';
return apply_filters('simplefavorites_user_list', $out, $users, $anonymous_count);
}
}