[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 Plugin Name: Bozo Users 4 Plugin URI: http://bbpress.org/ 5 Description: Allows moderators to mark certain users as a "bozo". Bozo users can post, but their content is only visible to themselves. 6 Author: Michael Adams 7 Version: 1.1 8 Author URI: http://blogwaffe.com/ 9 */ 10 11 function bb_bozo_posts( $where ) { 12 if ( !$id = bb_get_current_user_info( 'id' ) ) 13 return $where; 14 15 return preg_replace( 16 '/(\w+\.)?post_status = ["\']?0["\']?/', 17 "( \\1post_status = 0 OR \\1post_status > 1 AND \\1poster_id = '$id' )", 18 $where); 19 } 20 21 function bb_bozo_topics( $where ) { 22 if ( !$id = bb_get_current_user_info( 'id' ) ) 23 return $where; 24 25 return preg_replace( 26 '/(\w+\.)?topic_status = ["\']?0["\']?/', 27 "( \\1topic_status = 0 OR \\1topic_status > 1 AND \\1topic_poster = '$id' )", 28 $where); 29 } 30 31 // Gets those users with the bozo bit. Does not grab users who have been bozoed on a specific topic. 32 // NOT bbdb::prepared 33 function bb_get_bozos( $page = 1 ) { 34 global $bbdb, $bb_last_countable_query; 35 $page = (int) $page; 36 $limit = (int) bb_get_option('page_topics'); 37 if ( 1 < $page ) 38 $limit = ($limit * ($page - 1)) . ", $limit"; 39 $bb_last_countable_query = "SELECT user_id FROM $bbdb->usermeta WHERE meta_key='is_bozo' AND meta_value='1' ORDER BY umeta_id DESC LIMIT $limit"; 40 if ( $ids = (array) $bbdb->get_col( $bb_last_countable_query ) ) 41 bb_cache_users( $ids ); 42 return $ids; 43 } 44 45 function bb_current_user_is_bozo( $topic_id = false ) { 46 global $bb_current_user; 47 if ( bb_current_user_can('browse_deleted') && 'all' == @$_GET['view'] ) 48 return false; 49 $is_bozo = isset($bb_current_user->data->is_bozo) && $bb_current_user->data->is_bozo; 50 if ( !$topic_id || $is_bozo ) 51 return $is_bozo; 52 53 global $topic; 54 $topic = get_topic( $topic_id ); 55 $id = bb_get_current_user_info( 'id' ); 56 return isset($topic->bozos[$id]) && $topic->bozos[$id]; 57 } 58 59 function bb_bozo_pre_permalink() { 60 if ( bb_is_topic() ) 61 add_filter( 'get_topic_where', 'bb_bozo_topics' ); 62 } 63 64 function bb_bozo_post_permalink() { 65 if ( bb_is_topic() ) 66 remove_filter( 'get_topic_where', 'bb_bozo_topics' ); 67 } 68 69 function bb_bozo_latest_filter() { 70 global $bb_current_user; 71 if ( isset($bb_current_user->data->bozo_topics) && $bb_current_user->data->bozo_topics ) 72 add_filter( 'get_latest_topics_where', 'bb_bozo_topics' ); 73 } 74 75 function bb_bozo_topic_db_filter() { 76 global $topic, $topic_id; 77 if ( isset( $topic->topic_id ) && bb_current_user_is_bozo( $topic->topic_id ? $topic->topic_id : $topic_id ) ) { 78 add_filter( 'get_thread_where', 'bb_bozo_posts' ); 79 add_filter( 'get_thread_post_ids_where', 'bb_bozo_posts' ); 80 } 81 } 82 83 function bb_bozo_profile_db_filter() { 84 global $user; 85 if ( isset( $user->ID) && isset( $user->bozo_topics ) && bb_get_current_user_info( 'id' ) == $user->ID && @is_array($user->bozo_topics) ) 86 add_filter( 'get_recent_user_replies_where', 'bb_bozo_posts' ); 87 } 88 89 function bb_bozo_recount_topics() { 90 global $bbdb; 91 global $messages; 92 if ( isset($_POST['topic-bozo-posts']) && 1 == $_POST['topic-bozo-posts'] ): 93 $old = (array) $bbdb->get_col("SELECT object_id FROM $bbdb->meta WHERE object_type = 'bb_topic' AND meta_key = 'bozos'"); 94 $old = array_flip($old); 95 $messages[] = __('Counted the number of bozo posts in each topic'); 96 if ( $topics = (array) $bbdb->get_col("SELECT topic_id, poster_id, COUNT(post_id) FROM $bbdb->posts WHERE post_status > 1 GROUP BY topic_id, poster_id") ) : 97 $unique_topics = array_unique($topics); 98 $posters = (array) $bbdb->get_col('', 1); 99 $counts = (array) $bbdb->get_col('', 2); 100 foreach ($unique_topics as $i): 101 $bozos = array(); 102 $indices = array_keys($topics, $i); 103 foreach ( $indices as $index ) 104 $bozos[(int) $posters[$index]] = (int) $counts[$index]; 105 if ( $bozos ) : 106 bb_update_topicmeta( $i, 'bozos', $bozos ); 107 unset($indices, $index, $old[$i]); 108 endif; 109 endforeach; 110 unset($topics, $i, $counts, $posters, $bozos); 111 endif; 112 if ( $old ) : 113 $old = join(',', array_map('intval', array_flip($old))); 114 $bbdb->query("DELETE FROM $bbdb->meta WHERE object_type = 'bb_topic' AND object_id IN ($old) AND meta_key = 'bozos'"); 115 endif; 116 endif; 117 } 118 119 function bb_bozo_recount_users() { 120 global $bbdb; 121 global $messages; 122 if ( isset($_POST['topics-replied-with-bozos']) && 1 == $_POST['topics-replied-with-bozos'] ) : 123 $messages[] = __('Counted each bozo user's total posts as well as the total topics to which they have replied'); 124 if ( $users = (array) $bbdb->get_col("SELECT ID FROM $bbdb->users") ) : 125 $no_bozos = array(); 126 $bozo_mkey = $bbdb->prefix . 'bozo_topics'; 127 foreach ( $users as $user ) : 128 $topics_replied = (int) $bbdb->get_var( $bbdb->prepare( 129 "SELECT COUNT(DISTINCT topic_id) FROM $bbdb->posts WHERE post_status = 0 AND poster_id = %d", 130 $user 131 ) ); 132 bb_update_usermeta( $user, $bbdb->prefix. 'topics_replied', $topics_replied ); 133 $bozo_keys = (array) $bbdb->get_col( $bbdb->prepare( 134 "SELECT topic_id, COUNT(post_id) FROM $bbdb->posts WHERE post_status > 1 AND poster_id = %d GROUP BY topic_id", 135 $user 136 ) ); 137 $bozo_values = (array) $bbdb->get_col('', 1); 138 if ( $c = count($bozo_keys) ) : 139 for ( $i=0; $i < $c; $i++ ) 140 $bozo_topics[(int) $bozo_keys[$i]] = (int) $bozo_values[$i]; 141 bb_update_usermeta( $user, $bozo_mkey, $bozo_topics ); 142 else : 143 $no_bozos[] = (int) $user; 144 endif; 145 endforeach; 146 if ( $no_bozos ) : 147 $no_bozos = join(',', $no_bozos); 148 $bbdb->query( $bbdb->prepare( 149 "DELETE FROM $bbdb->usermeta WHERE user_id IN ($no_bozos) AND meta_key = %s", 150 $bozo_mkey 151 ) ); 152 endif; 153 unset($users, $user, $topics_replied, $bozo_keys, $bozo_values, $bozo_topics); 154 endif; 155 endif; 156 } 157 158 function bb_bozo_post_del_class( $classes, $post_id, $post ) 159 { 160 if ( isset( $post->post_status ) && 1 < $post->post_status && bb_current_user_can('browse_deleted') ) { 161 if ( $classes ) { 162 return $classes . ' bozo'; 163 } 164 return 'bozo'; 165 } 166 return $classes; 167 } 168 169 function bb_bozo_add_recount_list() { 170 global $recount_list; 171 $recount_list[21] = array('topics-replied-with-bozos', __('Count each bozo user's total posts as well as the total topics to which they have replied'), 'bb_bozo_recount_users'); 172 $recount_list[22] = array('topic-bozo-posts', __('Count the number of bozo posts in each topic'), 'bb_bozo_recount_topics'); 173 return; 174 } 175 176 function bb_bozo_topic_pages_add( $add ) { 177 global $topic; 178 if ( isset($_GET['view']) && 'all' == $_GET['view'] && bb_current_user_can('browse_deleted') ) : 179 $add += @array_sum($topic->bozos); 180 endif; 181 if ( isset( $topic->topic_id ) && bb_current_user_is_bozo( $topic->topic_id ) ) 182 $add += $topic->bozos[bb_get_current_user_info( 'id' )]; 183 return $add; 184 } 185 186 function bb_bozo_get_topic_posts( $topic_posts ) { 187 global $topic; 188 if ( isset( $topic->topic_id ) && bb_current_user_is_bozo( $topic->topic_id ) ) 189 $topic_posts += $topic->bozos[bb_get_current_user_info( 'id' )]; 190 return $topic_posts; 191 } 192 193 function bb_bozo_new_post( $post_id ) { 194 $bb_post = bb_get_post( $post_id ); 195 if ( isset( $post->post_status ) && 1 < $bb_post->post_status ) 196 bb_bozon( $bb_post->poster_id, $bb_post->topic_id ); 197 $topic = get_topic( $bb_post->topic_id, false ); 198 if ( isset( $topic->topic_posts ) && 0 == $topic->topic_posts ) 199 bb_delete_topic( $topic->topic_id, 2 ); 200 } 201 202 function bb_bozo_pre_post_status( $status, $post_id, $topic_id ) { 203 if ( !$post_id && bb_current_user_is_bozo() ) 204 $status = 2; 205 elseif ( bb_current_user_is_bozo( $topic_id ) ) 206 $status = 2; 207 return $status; 208 } 209 210 function bb_bozo_delete_post( $post_id, $new_status, $old_status ) { 211 $bb_post = bb_get_post( $post_id ); 212 if ( 1 < $new_status && 2 > $old_status ) 213 bb_bozon( $bb_post->poster_id, $bb_post->topic_id ); 214 elseif ( 2 > $new_status && 1 < $old_status ) 215 bb_fermion( $bb_post->poster_id, $bb_post->topic_id ); 216 } 217 218 function bb_bozon( $user_id, $topic_id = 0 ) { 219 global $bbdb; 220 221 $user_id = (int) $user_id; 222 $topic_id = (int) $topic_id; 223 224 if ( !$topic_id ) 225 bb_update_usermeta( $user_id, 'is_bozo', 1 ); 226 else { 227 $topic = get_topic( $topic_id ); 228 $user = bb_get_user( $user_id ); 229 230 $bozo_topics_key = $bbdb->prefix . 'bozo_topics'; 231 232 if ( isset($topic->bozos[$user_id]) ) 233 $topic->bozos[$user_id]++; 234 elseif ( isset( $topic->bozos ) && is_array( $topic->bozos ) ) 235 $topic->bozos[$user_id] = 1; 236 else 237 $topic->bozos = array($user_id => 1); 238 bb_update_topicmeta( $topic_id, 'bozos', $topic->bozos ); 239 240 if ( isset($user->{$bozo_topics_key}[$topic_id]) ) 241 $user->{$bozo_topics_key}[$topic_id]++; 242 elseif ( isset( $user->bozo_topics ) && is_array($user->bozo_topics) ) 243 $user->{$bozo_topics_key}[$topic_id] = 1; 244 else 245 $user->$bozo_topics_key = array($topic_id => 1); 246 bb_update_usermeta( $user_id, $bozo_topics_key, $user->$bozo_topics_key ); 247 } 248 } 249 250 function bb_fermion( $user_id, $topic_id = 0 ) { 251 global $bbdb; 252 253 $user_id = (int) $user_id; 254 $topic_id = (int) $topic_id; 255 256 if ( !$topic_id ) 257 bb_delete_usermeta( $user_id, 'is_bozo' ); 258 else { 259 $topic = get_topic( $topic_id ); 260 $user = bb_get_user( $user_id ); 261 262 $bozo_topics_key = $bbdb->prefix . 'bozo_topics'; 263 264 if ( --$topic->bozos[$user_id] < 1 ) 265 unset($topic->bozos[$user_id]); 266 bb_update_topicmeta( $topic_id, 'bozos', $topic->bozos ); 267 268 if ( --$user->{$bozo_topics_key}[$topic_id] < 1 ) 269 unset($user->{$bozo_topics_key}[$topic_id]); 270 bb_update_usermeta( $user_id, $bozo_topics_key, $user->$bozo_topics_key ); 271 } 272 } 273 274 function bb_bozo_profile_admin_keys( $a ) { 275 $a['is_bozo'] = array( 276 0, // Required 277 __('This user is a bozo'), // Label 278 'checkbox', // Type 279 '1', // Value 280 '' // Default when not set 281 ); 282 return $a; 283 } 284 285 286 287 function bb_bozo_get_bozo_user_ids() 288 { 289 global $bbdb; 290 $sql = "SELECT `user_id` FROM `$bbdb->usermeta` WHERE `meta_key` = 'is_bozo' AND `meta_value` = '1';"; 291 $user_ids = $bbdb->get_col( $sql, 0 ); 292 if ( is_wp_error( $user_ids ) || empty( $user_ids ) ) { 293 return false; 294 } 295 return $user_ids; 296 } 297 298 function bb_bozo_user_search_description( $description, $h2_search, $h2_role, $user_search_object ) 299 { 300 if ( isset( $user_search_object->roles ) && is_array( $user_search_object->roles ) && in_array( 'bozo', $user_search_object->roles ) ) { 301 return sprintf( '%1$s%2$s that are bozos', $h2_search, $h2_role ); 302 } 303 return $description; 304 } 305 add_filter( 'bb_user_search_description', 'bb_bozo_user_search_description', 10, 4 ); 306 307 function bb_bozo_user_search_form_add_inputs( $r, $user_search_object ) 308 { 309 $checked = ''; 310 if ( isset( $user_search_object->roles ) && is_array( $user_search_object->roles ) && in_array( 'bozo', $user_search_object->roles ) ) { 311 $checked = ' checked="checked"'; 312 } 313 $r .= "\t" . '<div>' . "\n"; 314 $r .= "\t\t" . '<label for="userbozo">' . __('Bozos only') . '</label>' . "\n"; 315 $r .= "\t\t" . '<div>' . "\n"; 316 $r .= "\t\t\t" . '<input class="checkbox-input" type="checkbox" name="userrole[]" id="userbozo" value="bozo"' . $checked . ' />' . "\n"; 317 $r .= "\t\t" . '</div>' . "\n"; 318 $r .= "\t" . '</div>' . "\n"; 319 320 return $r; 321 } 322 add_filter( 'bb_user_search_form_inputs', 'bb_bozo_user_search_form_add_inputs', 10, 2 ); 323 324 function bb_bozo_user_search_role_user_ids( $role_user_ids, $roles, $args ) 325 { 326 if ( !in_array( 'bozo', $roles ) ) { 327 return $role_user_ids; 328 } 329 330 $bozo_user_ids = bb_bozo_get_bozo_user_ids(); 331 332 if ( 1 === count( $roles ) ) { 333 return $bozo_user_ids; 334 } 335 336 global $bbdb; 337 $role_meta_key = $bbdb->escape( $bbdb->prefix . 'capabilities' ); 338 $role_sql_terms = array(); 339 foreach ( $roles as $role ) { 340 if ( 'bozo' === $role ) { 341 continue; 342 } 343 $role_sql_terms[] = "`meta_value` LIKE '%" . $bbdb->escape( like_escape( $role ) ) . "%'"; 344 } 345 $role_sql_terms = join( ' OR ', $role_sql_terms ); 346 $role_sql = "SELECT `user_id` FROM `$bbdb->usermeta` WHERE `meta_key` = '$role_meta_key' AND ($role_sql_terms);"; 347 $role_user_ids = $bbdb->get_col( $role_sql, 0 ); 348 if ( is_wp_error( $role_user_ids ) || empty( $role_user_ids ) ) { 349 return array(); 350 } 351 352 return array_intersect( (array) $bozo_user_ids, $role_user_ids ); 353 } 354 add_filter( 'bb_user_search_role_user_ids', 'bb_bozo_user_search_role_user_ids', 10, 3 ); 355 356 357 358 359 add_filter( 'pre_post_status', 'bb_bozo_pre_post_status', 5, 3 ); 360 add_action( 'bb_new_post', 'bb_bozo_new_post', 5 ); 361 add_action( 'bb_delete_post', 'bb_bozo_delete_post', 5, 3 ); 362 363 add_action( 'pre_permalink', 'bb_bozo_pre_permalink' ); 364 add_action( 'post_permalink', 'bb_bozo_post_permalink' ); 365 add_action( 'bb_index.php_pre_db', 'bb_bozo_latest_filter' ); 366 add_action( 'bb_forum.php_pre_db', 'bb_bozo_latest_filter' ); 367 add_action( 'bb_topic.php_pre_db', 'bb_bozo_topic_db_filter' ); 368 add_action( 'bb_profile.php_pre_db', 'bb_bozo_profile_db_filter' ); 369 370 add_action( 'bb_recount_list', 'bb_bozo_add_recount_list' ); 371 add_action( 'topic_pages_add', 'bb_bozo_topic_pages_add' ); 372 373 add_filter( 'post_del_class', 'bb_bozo_post_del_class', 10, 3 ); 374 add_filter( 'get_topic_posts', 'bb_bozo_get_topic_posts' ); 375 376 add_filter( 'get_profile_admin_keys', 'bb_bozo_profile_admin_keys' ); 377 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 7 01:01:35 2017 | Cross-referenced by PHPXref 0.7.1 |