UserFavoriteCount.php
1.24 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
<?php
namespace Favorites\API\Shortcodes;
class UserFavoriteCount
{
/**
* Shortcode Options
* @var array
*/
private $options;
/**
* List Filters
* @var array
*/
private $filters;
public function __construct()
{
add_shortcode('user_favorite_count', [$this, 'renderView']);
}
/**
* Shortcode Options
*/
private function setOptions($options)
{
$this->options = shortcode_atts([
'user_id' => '',
'site_id' => '',
'post_types' => ''
], $options);
}
/**
* Parse Post Types
*/
private function parsePostTypes()
{
if ( $this->options['post_types'] == "" ) return;
$post_types = explode(',', $this->options['post_types']);
$this->filters = ['post_type' => sanitize_text_field($post_types)];
}
/**
* Render the HTML list
* @param $options, array of shortcode options
*/
public function renderView($options)
{
$this->setOptions($options);
$this->parsePostTypes();
$this->options['user_id'] = ( $this->options['user_id'] == "" ) ? null : intval($this->options['user_id']);
$this->options['site_id'] = ( $this->options['site_id'] == "" ) ? get_current_blog_id() : intval($this->options['site_id']);
return get_user_favorites_count($this->options['user_id'], $this->options['site_id'], $this->filters, true);
}
}