/bp-forums/bbpress/bb-plugins/bozo.php

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