SyncFavoriteCount.php
1.04 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
<?php
namespace Favorites\Entities\Post;
use Favorites\Entities\Post\FavoriteCount;
use Favorites\Entities\User\UserRepository;
/**
* Updates the favorite count for a given post
*/
class SyncFavoriteCount
{
/**
* Post ID
* @var int
*/
private $post_id;
/**
* Site ID
* @var int
*/
private $site_id;
/**
* Status
* @var string
*/
private $status;
/**
* Favorite Count
* @var object
*/
private $favorite_count;
/**
* User Repository
*/
private $user;
public function __construct($post_id, $status, $site_id)
{
$this->post_id = $post_id;
$this->status = $status;
$this->site_id = $site_id;
$this->favorite_count = new FavoriteCount;
$this->user = new UserRepository;
}
/**
* Sync the Post Total Favorites
*/
public function sync()
{
if ( !$this->user->countsInTotal() ) return false;
$count = $this->favorite_count->getCount($this->post_id, $this->site_id);
$count = ( $this->status == 'active' ) ? $count + 1 : max(0, $count - 1);
return update_post_meta($this->post_id, 'simplefavorites_count', $count);
}
}