-
Notifications
You must be signed in to change notification settings - Fork 1
/
go_ranks.php
228 lines (200 loc) · 6.55 KB
/
go_ranks.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
function go_ranks() {
global $wpdb;
$dir = plugin_dir_url(__FILE__);
add_submenu_page( 'game-on-options.php', 'Ranks', 'Ranks', 'manage_options', 'go_ranks', 'go_ranks_menu');
}
function go_ranks_menu() {
global $wpdb;
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
else{
?>
<form method="post" action="">
<input name="go_fix_ranks" type="submit" value="Fix Ranks"/>
</form>
<?php
if(isset($_POST['go_fix_ranks'])){
$table_name_user_meta = $wpdb->prefix . "usermeta";
$table_name_go_totals = $wpdb->prefix . "go_totals";
global $default_role;
$role = get_option('go_role',$default_role);
$uid = $wpdb->get_results("SELECT user_id
FROM ".$table_name_user_meta."
WHERE meta_key = 'wp_capabilities'
AND (meta_value LIKE '%".$role."%' or meta_value like '%administrator%')");
foreach($uid as $id){foreach($id as $uids){
$ranks = get_option('go_ranks', false);
$current_points = go_return_points($uids);
while($current_points >= current($ranks)){
next($ranks);
}
$next_rank_points = current($ranks);
$next_rank = array_search($next_rank_points, $ranks);
$rank_points = prev($ranks);
$new_rank = array_search($rank_points, $ranks);
$new_rank_array= array(array($new_rank, $rank_points),array($next_rank, $next_rank_points));
update_user_meta($uids,'go_rank', $new_rank_array );
}
}}
$ranks = get_option('go_ranks',false);
?>
<div> <table style="width:250px;" id="ranks_table" class="widefat">
<th style="width:125px;">Rank</th>
<th style="width:125px;"><?php echo get_option('go_points_name'); ?></th><tbody id="table_rows">
<?php
foreach($ranks as $level => $points){
echo '<tr><td>'.$level.'</td><td>'.$points.'</td><td><button onclick="go_remove_ranks(\''.$level.'\');">Remove</button></td></tr>';
}
?>
</tbody>
</table>
</div>
<div class="widefat">
Ranks: <textarea id="ranks"></textarea>
<?php echo get_option('go_points_name');?>: <textarea id="points"></textarea>
Separator: <textarea id="separator"></textarea><button onclick="go_add_ranks();" >+</button>
</div>
<script language="javascript">
function go_add_ranks(){
ajaxurl = '<?= get_site_url() ?>/wp-admin/admin-ajax.php';
jQuery.ajax({
type: "post",url: ajaxurl,data: { action: 'go_add_ranks', ranks: jQuery('#ranks').val(), points: jQuery('#points').val(), separator: jQuery('#separator').val()},
success: function(html){
jQuery('#table_rows').html(html);;
}
}); }
function go_remove_ranks(rank_key){
ajaxurl = '<?= get_site_url() ?>/wp-admin/admin-ajax.php';
jQuery.ajax({
type: "post",url: ajaxurl,data: { action: 'go_remove_ranks', ranks: rank_key},
success: function(html){
jQuery('#table_rows').html(html);;
}
}); }
</script>
<?php
}
}
function go_add_ranks(){
global $wpdb;
$new_ranks = $_POST['ranks'];
$points = $_POST['points'];
$separator = $_POST['separator'];
$ranks = get_option('go_ranks',false);
if(!$ranks){
$ranks = array('Level 1'=> 0);
}
if($separator == ''){
if(is_numeric($points)){
$ranks[$new_ranks] = $points;
}}
else{
$new_ranks = explode($separator,$new_ranks);
$points = explode($separator,$points);
foreach($new_ranks as $index => $new_ranks){
$ranks[$new_ranks] = $points[$index];
}
}
asort($ranks);
update_option( 'go_ranks', $ranks );
foreach($ranks as $ranks => $points){
echo '<tr><td>'.$ranks.'</td><td>'.$points.'</td><td><button onclick="go_remove_ranks(\''.$ranks.'\');">Remove</button></td></tr>';
}
die();
}
function go_remove_ranks(){
global $wpdb;
$new_ranks = $_POST['ranks'];
$ranks = get_option('go_ranks',false);
unset($ranks[$new_ranks]);
update_option( 'go_ranks', $ranks );
foreach($ranks as $ranks => $points){
echo '<tr><td>'.$ranks.'</td><td>'.$points.'</td><td><button onclick="go_remove_ranks(\''.$ranks.'\');">Remove</button></td></tr>';
}
die();
}
function go_update_ranks($user_id, $total_points){
global $wpdb;
global $current_rank;
go_get_rank($user_id);
global $current_rank_points;
global $next_rank;
global $next_rank_points;
global $current_points;
if($next_rank != ''){
if($total_points >= $next_rank_points){
$ranks = get_option('go_ranks');
$ranks_keys = array_keys($ranks);
$new_rank_key = array_search($next_rank, $ranks_keys);
$new_next_rank = $ranks_keys[($new_rank_key+1)];
$new_rank = array(array($next_rank, $next_rank_points), array($new_next_rank, $ranks[$new_next_rank]));
update_user_meta($user_id, 'go_rank', $new_rank);
$update = true;}}
else {
$ranks = get_option('go_ranks', false);
$current_points = go_return_points($uids);
while($current_points >= current($ranks)){
next($ranks);
}
$next_rank_points = current($ranks);
$next_rank = array_search($next_rank_points, $ranks);
$rank_points = prev($ranks);
$new_rank = array_search($rank_points, $ranks);
$new_rank_array= array(array($new_rank, $rank_points),array($next_rank, $next_rank_points));
update_user_meta($uids,'go_rank', $new_rank_array );
$update = true;}
if($update){
go_get_rank($user_id);
global $current_rank;
global $current_rank_points;
global $next_rank;
global $next_rank_points;
global $counter;
$counter++;
$space = $counter*85;
echo '<div id="go_notification" class="go_notification" style="top: '.$space.'px; color: #FFD700;"> '.$current_rank.'!</div><script type="text/javascript" language="javascript">go_notification();
jQuery("#go_admin_bar_rank").html("'.$current_rank.'");
</script>';
}
}
function go_get_rank($user_id) {
global $wpdb;
$rank = get_user_meta($user_id, 'go_rank');
global $current_rank;
global $current_rank_points;
global $next_rank;
global $next_rank_points;
$current_rank = $rank[0][0][0];
$current_rank_points = $rank[0][0][1];
$next_rank = $rank[0][1][0];
$next_rank_points = $rank[0][1][1];
}
function go_get_all_ranks() {
$all_ranks = get_option('go_ranks');
$all_ranks_sorted = array();
foreach($all_ranks as $level => $points) {
$all_ranks_sorted[] = array('name' => $level , 'value' => $points);
}
return $all_ranks_sorted;
}
function go_clean_ranks() {
$all_ranks = get_option('go_ranks');
$all_ranks_sorted = array();
foreach($all_ranks as $level => $points) {
echo '<option value="'.$points.'">'.$level.'</option>';
}
}
function go_get_rank_key($points) {
global $wpdb;
$ranks = get_option('go_ranks',false);
foreach ($ranks as $key => $rank ) {
switch($rank) {
case $points:
return $key;
break;
}
}
}
?>