SyncSingleFavorite.php
3.36 KB
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
125
126
127
128
129
130
131
132
133
<?php
namespace Favorites\Entities\Favorite;
use Favorites\Entities\User\UserRepository;
use Favorites\Helpers;
/**
* Sync a single favorite to a given save type
*/
class SyncSingleFavorite
{
/**
* The Post ID
*/
private $post_id;
/**
* The Site ID
*/
private $site_id;
/**
* The Group ID
*/
private $group_id;
/**
* User Repository
*/
private $user;
public function __construct($post_id, $site_id, $group_id = 1)
{
$this->user = new UserRepository;
$this->post_id = $post_id;
$this->site_id = $site_id;
$this->group_id = $group_id;
}
/**
* Sync a Session Favorite
*/
public function session()
{
if ( $this->user->isFavorite($this->post_id, $this->site_id) ) return $_SESSION['simplefavorites'] = $this->removeFavorite();
return $_SESSION['simplefavorites'] = $this->addFavorite();
}
/**
* Sync a Cookie Favorite
*/
public function cookie()
{
if ( $this->user->isFavorite($this->post_id, $this->site_id) ){
$favorites = $this->removeFavorite();
setcookie( 'simplefavorites', json_encode( $favorites ), time() + apply_filters( 'simplefavorites_cookie_expiration_interval', 31556926 ), '/' );
return;
}
$favorites = $this->addFavorite();
setcookie( 'simplefavorites', json_encode( $favorites ), time() + apply_filters( 'simplefavorites_cookie_expiration_interval', 31556926 ), '/' );
return;
}
/**
* Update User Meta (logged in only)
*/
public function updateUserMeta($favorites)
{
if ( !is_user_logged_in() ) return;
update_user_meta( intval(get_current_user_id()), 'simplefavorites', $favorites );
}
/**
* Remove a Favorite
*/
private function removeFavorite()
{
$favorites = $this->user->getAllFavorites($this->site_id);
foreach($favorites as $key => $site_favorites){
if ( $site_favorites['site_id'] !== $this->site_id ) continue;
foreach($site_favorites['posts'] as $k => $fav){
if ( $fav == $this->post_id ) unset($favorites[$key]['posts'][$k]);
}
if ( !Helpers::groupsExist($site_favorites) ) return;
foreach( $site_favorites['groups'] as $group_key => $group){
if ( $group['group_id'] !== $this->group_id ) continue;
foreach ( $group['posts'] as $k => $g_post_id ){
if ( $g_post_id == $this->post_id ) unset($favorites[$key]['groups'][$group_key]['posts'][$k]);
}
}
}
$this->updateUserMeta($favorites);
return $favorites;
}
/**
* Add a Favorite
*/
private function addFavorite()
{
$favorites = $this->user->getAllFavorites($this->site_id);
if ( !Helpers::siteExists($this->site_id, $favorites) ){
$favorites[] = [
'site_id' => $this->site_id,
'posts' => []
];
}
// Loop through each site's favorites, continue if not the correct site id
foreach($favorites as $key => $site_favorites){
if ( $site_favorites['site_id'] !== $this->site_id ) continue;
$favorites[$key]['posts'][] = $this->post_id;
// Add the default group if it doesn't exist yet
if ( !Helpers::groupsExist($site_favorites) ){
$favorites[$key]['groups'] = [
[
'group_id' => 1,
'site_id' => $this->site_id,
'group_name' => __('Default List', 'favorites'),
'posts' => array()
]
];
}
foreach( $favorites[$key]['groups'] as $group_key => $group){
if ( $group['group_id'] == $this->group_id )
$favorites[$key]['groups'][$group_key]['posts'][] = $this->post_id;
}
}
$this->updateUserMeta($favorites);
return $favorites;
}
}