-
Notifications
You must be signed in to change notification settings - Fork 3
/
bbpvotes-functions.php
146 lines (115 loc) · 3.75 KB
/
bbpvotes-functions.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Get a value in a multidimensional array
* http://stackoverflow.com/questions/1677099/how-to-use-a-string-as-an-array-index-path-to-retrieve-a-value
* @param type $keys
* @param type $array
* @return type
*/
function bbpvotes_get_array_value($keys = null, $array){
if (!$keys) return $array;
$keys = (array)$keys;
$first_key = $keys[0];
if(count($keys) > 1) {
if ( isset($array[$keys[0]]) ){
return bbpvotes_get_array_value(array_slice($keys, 1), $array[$keys[0]]);
}
}elseif (isset($array[$first_key])){
return $array[$first_key];
}
return false;
}
/**
* Rebuild scores for all votes
*/
function bbpvotes_rebuild_scores(){
$post_ids = array();
$default_query_args = array(
'post_type' => bbpvotes_get_enabled_post_types(),
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids'
);
$votes_up_query_args = wp_parse_args( array('meta_key' => bbpvotes()->metaname_post_vote_up), $default_query_args );
$votes_down_query_args = wp_parse_args( array('meta_key' => bbpvotes()->metaname_post_vote_down), $default_query_args );
$votes_up_query = new WP_Query( $votes_up_query_args );
$votes_down_query = new WP_Query( $votes_down_query_args );
foreach ($votes_up_query->posts as $id){
$post_ids[] = $id;
}
foreach ($votes_down_query->posts as $id){
$post_ids[] = $id;
}
$post_ids = array_unique($post_ids);
foreach ($post_ids as $id){
$post_score = 0;
$vote_count = 0;
$votes = bbpvotes_get_votes_for_post( $id );
foreach ($votes as $user_id => $vote){
$post_score += $vote;
$vote_count++;
}
update_post_meta($id, bbpvotes()->metaname_post_vote_score, $post_score);
update_post_meta($id, bbpvotes()->metaname_post_vote_count, $vote_count);
}
}
/**
* Round Numbers To K (Thousand), M (Million) or B (Billion)
* @param type $number
* @param type $min_value
* @param type $decimal
* @return type
*/
function bbpvotes_number_format( $number, $min_value = 1000, $decimal = 1 ) {
$number = (int)$number;
$output = null;
if( $number < $min_value ) {
$output = number_format_i18n( $number );
}else{
$alphabets = array(
1000000000 => _x( 'B', 'billion unit', 'bbpvotes' ),
1000000 => _x( 'M', 'million unit', 'bbpvotes' ),
1000 => _x( 'K', 'thousand unit', 'bbpvotes' ),
);
foreach( $alphabets as $key => $value ){
if( $number >= $key ) {
$output = round( $number / $key, $decimal ) . '' . $value;
break;
}
}
}
return apply_filters('bbpvotes_number_format',$output,$number);
}
function bbpvotes_get_enabled_post_types(){
$enabled = array();
$allowed = bbpvotes_get_allowed_post_types();
$ignored = bbpvotes()->get_options('ignored_post_type');
foreach((array)$allowed as $post_type){
if( in_array($post_type,$ignored) ) continue;
if ( !get_post_type_object($post_type) ) continue;
$enabled[] = $post_type;
}
return $enabled;
}
function bbpvotes_get_allowed_post_types(){
/*
$post_types = get_post_types();
$disabled = apply_filters('bbpvotes_option_post_type_disabled',array(
'attachment',
'revision',
'nav_menu_item'
)
);
$allowed = array();
foreach ((array)$post_types as $post_type){
if (in_array($post_type,$disabled)) continue;
$allowed[] = $post_type;
}
*/
$allowed = array(
bbp_get_topic_post_type(),
bbp_get_reply_post_type()
);
return $allowed;
}
?>