Guest User

Untitled

a guest
May 4th, 2010
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.36 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: BB Moderation Hold
  4. Plugin URI: http://www.adityanaik.com
  5. Description: Hold posts and topics for moderation
  6. Author: Aditya Naik
  7. Author URI: http://www.adityanaik.com/
  8. Version: 0.4.1
  9. */
  10. /**
  11. * Filter topics held for moderation
  12. *
  13. * @author Aditya Naik <[email protected]>
  14. * @version v 0.02 Sun Apr 08 2007 01:31:51 GMT-0400 (Eastern Daylight Time)
  15. */
  16. function bb_moderation_hold_where_moderated_topics($where){
  17. return str_replace("topic_status = '0'", "topic_status = '-1'", $where);
  18. }
  19.  
  20. /**
  21. * Filter posts held for moderation
  22. *
  23. * @author Aditya Naik <[email protected]>
  24. * @version v 0.02 Sun Apr 08 2007 01:32:35 GMT-0400 (Eastern Daylight Time)
  25. */
  26. function bb_moderation_hold_where_moderated_posts($where){
  27. return str_replace("post_status = '0'", "post_status = '-1'", $where);
  28. }
  29.  
  30. /**
  31. * Holds stuff for moderation
  32. *
  33. * Hold topics and posts for moderation depending on the options.
  34. * Also send mail options are set
  35. *
  36. * @author Aditya Naik <[email protected]>
  37. * @version v 0.01 Tue Apr 10 2007 23:29:07 GMT-0400 (Eastern Daylight Time)
  38. */
  39. function bb_moderation_hold_after_posting_do_the_magic($post_id){
  40. global $bbdb, $topic_id, $post_id, $bb_current_user;
  41. $options = bb_anonymous_default_options(bb_get_option('bb_moderation_hold'));
  42.  
  43. $hold_topics = bb_moderation_check_options('hold_topics', $options);
  44. $hold_posts = bb_moderation_check_options('hold_posts', $options);
  45.  
  46. if ($hold_topics && isset($_POST['topic']) && $forum = (int) $_POST['forum_id'] ) {
  47. $bbdb->query("UPDATE $bbdb->topics SET topic_status = '-1' WHERE topic_id = '$topic_id'");
  48. if ('Y' == $options['hold_topics_send_email']) bb_moderation_hold_mail_moderation();
  49. $post_id = false;
  50. } elseif($hold_posts) {
  51. $post_id = false;
  52. if ('Y' == $options['hold_posts_send_email']) bb_moderation_hold_mail_moderation('P');
  53. }
  54.  
  55. }
  56.  
  57. /**
  58. * Send Moderation Notification
  59. *
  60. * @author Aditya Naik <[email protected]>
  61. * @version v 0.02 Sun Apr 08 2007 01:37:30 GMT-0400 (Eastern Daylight Time)
  62. */
  63. function bb_moderation_hold_mail_moderation($obj = 'T') {
  64.  
  65. if ('T' == $obj) {
  66. $email = $options['hold_topics_email_address'];
  67. $obj = 'topic';
  68. } elseif ('P' == $obj) {
  69. $email = $options['hold_posts_email_address'];
  70. $obj = 'post';
  71. } else
  72. return;
  73.  
  74. $message = __("You have a new $s in the moderation queue.");
  75. bb_mail( $email, __('Moderation Alert'), sprintf( $message, "$obj"));
  76. }
  77.  
  78. /**
  79. * Change the status before post is created
  80. *
  81. * @author Aditya Naik <[email protected]>
  82. * @version v 0.02 Tue Apr 10 2007 23:29:38 GMT-0400 (Eastern Daylight Time)
  83. */
  84. function bb_moderation_fix_status_before_post($old_status, $post_id, $topic_id) {
  85. global $bb_current_user;
  86. $options = bb_anonymous_default_options(bb_get_option('bb_moderation_hold'));
  87. $hold_posts = bb_moderation_check_options('hold_posts', $options);
  88. if (!$post_id && $hold_posts) {
  89. if ($bb_current_user->ID == 3) {
  90. $old_status = -1;
  91. }
  92. }
  93. return $old_status;
  94. }
  95.  
  96. /**
  97. * Check Moderation options
  98. *
  99. * @author Aditya Naik <[email protected]>
  100. * @version v 0.01 Tue Apr 10 2007 23:30:05 GMT-0400 (Eastern Daylight Time)
  101. */
  102. function bb_moderation_check_options($check = 'hold_topics', $options = false) {
  103.  
  104. if (!$options)
  105. $options = bb_anonymous_default_options(bb_get_option('bb_moderation_hold'));
  106.  
  107. switch($options[$check]) {
  108. case 2:
  109. if (!$user = bb_current_user()) $ret = true;
  110. break;
  111. case 3:
  112. if (!bb_current_user_can('moderate')) $ret = true;
  113. break;
  114. default:
  115. $ret = false;
  116. }
  117.  
  118. return $ret;
  119. }
  120. add_action('bb_post.php','bb_moderation_hold_after_posting_do_the_magic');
  121. add_filter('pre_post_status','bb_moderation_fix_status_before_post',10,3);
  122. add_filter('post_delete_link','bb_moderation_fix_delete_link',10,3);
  123.  
  124. /**
  125. * Add a delete/moderate link to posts
  126. *
  127. * @author Aditya Naik <[email protected]>
  128. * @version v 0.01 Tue Apr 10 2007 23:30:05 GMT-0400 (Eastern Daylight Time)
  129. */
  130. function bb_moderation_fix_delete_link($r, $post_status, $post_id){
  131. if (-1 == $post_status)
  132. $r = "<a href='" . bb_get_option('uri') . 'bb-admin/admin-base.php?plugin=bb_moderation_hold_post_admin_page' . "' >". __('Moderate') ."</a>";
  133. return $r;
  134. }
  135.  
  136. if (!BB_IS_ADMIN) {
  137. return;
  138. }
  139.  
  140. /**
  141. * Add Admin Page
  142. *
  143. * @author Aditya Naik <[email protected]>
  144. * @version v 0.01 Sun Apr 08 2007 02:12:09 GMT-0400 (Eastern Daylight Time)
  145. */
  146. add_action( 'bb_admin_menu_generator', 'bb_moderation_hold_add_admin_page' );
  147. function bb_moderation_hold_add_admin_page() {
  148. global $bb_submenu;
  149.  
  150. bb_admin_add_submenu(__('Moderation Options'), 'moderate', 'bb_moderation_hold_admin_page', 'options-general.php');
  151. bb_admin_add_submenu(__('Topics for Moderation'), 'moderate', 'bb_moderation_hold_topic_admin_page', 'topics.php' );
  152. bb_admin_add_submenu(__('Posts for Moderation'), 'moderate', 'bb_moderation_hold_post_admin_page', 'posts.php' );
  153. }
  154.  
  155. /**
  156. * Administration Page for Moderation Options
  157. *
  158. * @author Aditya Naik <[email protected]>
  159. * @version v 0.01 Sun Apr 08 2007 02:12:05 GMT-0400 (Eastern Daylight Time)
  160. */
  161. function bb_moderation_hold_admin_page() {
  162.  
  163. global $bbdb, $topic, $bb_post, $post_id, $topic_id;
  164. $options = bb_anonymous_default_options(bb_get_option('bb_moderation_hold'));
  165. ?>
  166. <h2><?php _e('Hold for moderation') ?></h2>
  167. <form method="post">
  168. <table class="widefat">
  169. <tr<?php alt_class('options'); ?>>
  170. <td>Hold topics for moderation</td>
  171. <td>Hold posts for moderation</td>
  172. </tr>
  173. <tr>
  174. <td>
  175. <input type="radio" value="1" id="hold_topics1" name="hold_topics"<?php if (1 == $options['hold_topics']) echo ' checked' ;?> /> <label for="hold_topics1">None</label>
  176. <?php if (function_exists('bb_anonymous_posting_fix_bb_user_can')) { ?><input type="radio" value="2" id="hold_topics2" name="hold_topics"<?php if (2 == $options['hold_topics']) echo ' checked' ;?> /> <label for="hold_topics2">Anonymous Topics</label> <?php } ?>
  177. <input type="radio" value="3" id="hold_topics3" name="hold_topics"<?php if (3 == $options['hold_topics']) echo ' checked' ;?> /> <label for="hold_topics3">All Topics</label>
  178. </td>
  179. <td>
  180. <input type="radio" value="1" id="hold_posts1" name="hold_posts"<?php if (1 == $options['hold_posts']) echo ' checked' ;?> /> <label for="hold_posts1">None</label>
  181. <?php if (function_exists('bb_anonymous_posting_fix_bb_user_can')) { ?><input type="radio" value="2" id="hold_posts2" name="hold_posts"<?php if (2 == $options['hold_posts']) echo ' checked' ;?> /> <label for="hold_posts2">Anonymous Topics</label> <?php } ?>
  182. <input type="radio" value="3" id="hold_posts3" name="hold_posts"<?php if (3 == $options['hold_posts']) echo ' checked' ;?> /> <label for="hold_posts3">All Topics</label>
  183. </td>
  184. </tr>
  185. <tr<?php alt_class('options'); ?>>
  186. <td>
  187. <input type="checkbox" value="Y" id="hold_topics_send_email" name="hold_topics_send_email"<?php if ('Y' == $options['hold_topics_send_email']) echo ' checked' ;?> /> <label for="hold_topics_send_email">Send email to</label>
  188. <input type="text" value="<?php echo $options['hold_topics_email_address']; ?>" id="hold_topics_email_address" name="hold_topics_email_address" />
  189. </td>
  190. <td>
  191. <input type="checkbox" value="Y" id="hold_posts_send_email" name="hold_posts_send_email"<?php if ('Y' == $options['hold_posts_send_email']) echo ' checked' ;?> /> <label for="hold_posts_send_email">Send email to</label>
  192. <input type="text" value="<?php echo $options['hold_posts_email_address']; ?>" id="hold_posts_email_address" name="hold_posts_email_address" />
  193. </td>
  194. </tr>
  195. <tr>
  196. <td colspan="2" class="submit"><input type="submit" name="bb_moderation_hold_update_options" value="Update" /></td>
  197. </tr>
  198. </table>
  199. </form>
  200. <?php
  201. }
  202.  
  203. /**
  204. * Admin Page for Topic Moderation
  205. *
  206. * @author Aditya Naik <[email protected]>
  207. * @version v 0.01 Sun Apr 08 2007 02:32:53 GMT-0400 (Eastern Daylight Time)
  208. */
  209. function bb_moderation_hold_topic_admin_page() {
  210.  
  211. global $bbdb, $topic, $bb_post, $post_id, $topic_id;
  212. $options = bb_anonymous_default_options(bb_get_option('bb_moderation_hold'));
  213.  
  214. if ($options['hold_topics']) :
  215. if ( !bb_current_user_can('moderate') )
  216. die(__("Now how'd you get here? And what did you think you'd being doing?")); //This should never happen.
  217. add_filter( 'get_latest_topics_where', 'bb_moderation_hold_where_moderated_topics' );
  218. add_filter( 'topic_link', 'bb_make_link_view_all' );
  219. $topics = get_latest_topics( 0, $page);
  220. ?>
  221. <h2><?php _e('Topics for Moderation') ?></h2>
  222. <?php if ( $topics ) : ?>
  223. <form method="post" name="topic_moderation_form" >
  224. <table class="widefat">
  225. <tr class="thead">
  226. <th></th>
  227. <th><?php _e('Topic') ?></th>
  228. <th><?php _e('Last Poster') ?></th>
  229. <th><?php _e('Freshness') ?></th>
  230. </tr>
  231.  
  232. <?php foreach ( $topics as $topic ) : ?>
  233. <tr<?php alt_class('topic'); ?>>
  234. <td><input type="checkbox" name="topicids[]" value="<?php topic_id(); ?>" /></td>
  235. <td><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></td>
  236. <td class="num"><?php topic_last_poster(); ?></td>
  237. <td class="num"><small><?php topic_time(); ?></small></td>
  238. </tr>
  239. <?php endforeach; ?>
  240. </table>
  241. <p class="submit">
  242. <input type="submit" name="bb_moderation_hold_topic_delete" value="Delete" />
  243. <input type="submit" name="bb_moderation_hold_topic_approve" value="Approve" />
  244. </p>
  245. </form>
  246. <?php else: ?>
  247. <p>No topics for moderation</p>
  248. <?php endif;
  249. endif;
  250.  
  251. }
  252.  
  253. /**
  254. * Admin page for post moderation
  255. *
  256. * @author Aditya Naik <[email protected]>
  257. * @version v 0.01 Sun Apr 08 2007 02:30:31 GMT-0400 (Eastern Daylight Time)
  258. */
  259. function bb_moderation_hold_post_admin_page() {
  260.  
  261. global $bbdb, $topic, $bb_post, $post_id, $topic_id;
  262. $options = bb_anonymous_default_options(bb_get_option('bb_moderation_hold'));
  263.  
  264. if ($options['hold_posts']) :
  265. if ( !bb_current_user_can('moderate') )
  266. die(__("Now how'd you get here? And what did you think you'd being doing?")); //This should never happen.
  267. add_filter( 'get_latest_posts_where', 'bb_moderation_hold_where_moderated_posts' );
  268. add_filter( 'post_link', 'bb_make_link_view_all' );
  269. $posts = get_latest_posts( 50);
  270. ?>
  271. <h2><?php _e('Posts for Moderation') ?></h2>
  272. <?php if ( $posts ) : ?>
  273. <form method="post" name="moderation_form" >
  274. <table class="widefat">
  275. <tr class="thead">
  276. <th></th>
  277. <th><?php _e('Post') ?></th>
  278. <th><?php _e('Topic') ?></th>
  279. <th><?php _e('Poster') ?></th>
  280. <th><?php _e('Freshness') ?></th>
  281. </tr>
  282.  
  283. <?php foreach ( $posts as $bb_post ) :
  284. $topic = get_topic( $bb_post->topic_id);
  285. ?>
  286. <tr<?php alt_class('post'); ?>>
  287. <td><input type="checkbox" name="postids[]" value="<?php post_id(); ?>" /></td>
  288. <td><div><?php echo substr(get_post_text(),0,150); ?></div>
  289. <p><a href="<?php post_link(); ?>">Permalink</p>
  290. </td>
  291. <td><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></td>
  292. <td class="num"><?php post_author(); ?></td>
  293. <td class="num"><small><?php bb_post_time(); ?></small></td>
  294. </tr>
  295. <?php endforeach; ?>
  296. </table>
  297. <p class="submit">
  298. <input type="submit" name="bb_moderation_hold_post_delete" value="Delete" />
  299. <input type="submit" name="bb_moderation_hold_post_approve" value="Approve" />
  300. </p>
  301. </form>
  302. <?php else: ?>
  303. <p>No posts for moderation</p>
  304. <?php
  305. endif;
  306. endif;
  307. }
  308.  
  309. /**
  310. * Set Default options
  311. *
  312. * @author Aditya Naik <[email protected]>
  313. * @version v 0.01 Sun Apr 08 2007 01:27:00 GMT-0400 (Eastern Daylight Time)
  314. */
  315. function bb_anonymous_default_options($options){
  316. if (!$options) {
  317. $options = array('hold_topics' => 1, 'hold_posts' => 1);
  318. bb_update_option('bb_moderation_hold' , $options);
  319. } else {
  320. if (empty($options['hold_topics'])) $options['hold_topics'] = 1;
  321. if (empty($options['hold_posts'])) $options['hold_posts'] = 1;
  322. if (empty($options['hold_topics_send_email'])) $options['hold_topics_send_email'] = N;
  323. if (empty($options['hold_posts_send_email'])) $options['hold_posts_send_email'] = N;
  324. if (empty($options['hold_topics_email_address'])) {
  325. $options['hold_topics_send_email'] = N;
  326. }
  327. if (empty($options['hold_posts_email_address'])) {
  328. $options['hold_posts_send_email'] = N;
  329. }
  330. bb_update_option('bb_moderation_hold' , $options);
  331. }
  332. return $options;
  333. }
  334.  
  335. /**
  336. * Process Admin Page Post
  337. *
  338. * @author Aditya Naik <[email protected]>
  339. * @version v 0.01 Sun Apr 08 2007 02:11:58 GMT-0400 (Eastern Daylight Time)
  340. */
  341. add_action( 'bb_admin-header.php','bb_moderation_hold_process_post');
  342. function bb_moderation_hold_process_post() {
  343.  
  344. if(isset($_POST['bb_moderation_hold_update_options'])) {
  345. $hold_posts = $_POST['hold_posts'];
  346. $hold_topics = $_POST['hold_topics'];
  347. $hold_topics_send_email = $_POST['hold_topics_send_email'];
  348. $hold_posts_send_email = $_POST['hold_posts_send_email'];
  349. $hold_topics_email_address = $_POST['hold_topics_email_address'];
  350. $hold_posts_email_address = $_POST['hold_posts_email_address'];
  351. $options = array(
  352. 'hold_posts' => $hold_posts,
  353. 'hold_topics' => $hold_topics,
  354. 'hold_posts_send_email' => $hold_posts_send_email,
  355. 'hold_topics_send_email' => $hold_topics_send_email,
  356. 'hold_topics_email_address' => $hold_topics_email_address,
  357. 'hold_posts_email_address' => $hold_posts_email_address
  358. );
  359. bb_anonymous_default_options($options);
  360. $link = add_query_arg( array('plugin' => $_GET['plugin'], 'bb_moderation_hold_options' => 'updated'), bb_get_option( 'uri' ) . 'bb-admin/admin-base.php' );
  361. wp_redirect( $link );
  362. }
  363.  
  364. if(isset($_POST['bb_moderation_hold_topic_approve'])) {
  365. bb_moderation_hold_approve_topics($_POST['topicids']);
  366. $link = add_query_arg( array('plugin' => $_GET['plugin'], 'bb_moderation_hold_approved' => 'topics'), bb_get_option( 'uri' ) . 'bb-admin/admin-base.php' );
  367. wp_redirect( $link );
  368. }
  369.  
  370. if(isset($_POST['bb_moderation_hold_topic_delete'])) {
  371. if ($_POST['topicids']) : foreach($_POST['topicids'] as $topic_id) :
  372. bb_delete_topic( $topic_id, 1 );
  373. endforeach; endif;
  374. $link = add_query_arg( array('plugin' => $_GET['plugin'], 'bb_moderation_hold_deleted' => 'topics'), bb_get_option( 'uri' ) . 'bb-admin/admin-base.php' );
  375. wp_redirect( $link );
  376. }
  377.  
  378. if(isset($_POST['bb_moderation_hold_post_delete'])) {
  379. if ($_POST['postids']) : foreach($_POST['postids'] as $post_id) :
  380. bb_delete_post( $post_id, 1 );
  381. endforeach; endif;
  382. $link = add_query_arg( array('plugin' => $_GET['plugin'], 'bb_moderation_hold_deleted' => 'posts'), bb_get_option( 'uri' ) . 'bb-admin/admin-base.php' );
  383. wp_redirect( $link );
  384. }
  385.  
  386. if(isset($_POST['bb_moderation_hold_post_approve'])) {
  387. bb_moderation_hold_approve_posts($_POST['postids']);
  388. $link = add_query_arg( array('plugin' => $_GET['plugin'], 'bb_moderation_hold_approved' => 'posts'), bb_get_option( 'uri' ) . 'bb-admin/admin-base.php' );
  389. wp_redirect( $link );
  390. }
  391.  
  392. if ( isset($_GET['bb_moderation_hold_approved']) )
  393. $theme_notice = bb_admin_notice(ucfirst($_GET['bb_moderation_hold_approved']) . " Approved");
  394. elseif ( isset($_GET['bb_moderation_hold_deleted']) )
  395. $theme_notice = bb_admin_notice(ucfirst($_GET['bb_moderation_hold_deleted']) . " Deleted");
  396. elseif(isset($_GET['bb_moderation_hold_options']))
  397. $theme_notice = bb_admin_notice("Options Updated");
  398. }
  399.  
  400.  
  401. /**
  402. * Approve Topics
  403. *
  404. * @author Aditya Naik <[email protected]>
  405. * @version v 0.01 Sun Apr 08 2007 01:33:00 GMT-0400 (Eastern Daylight Time)
  406. */
  407. function bb_moderation_hold_approve_topics($topicids){
  408. global $bbdb;
  409. if ($topicids) : foreach($topicids as $topic_id) :
  410. $bbdb->query("UPDATE $bbdb->topics SET topic_status = '0' WHERE topic_id = '$topic_id'");
  411.  
  412. $postids = $bbdb->get_col("SELECT post_id FROM $bbdb->posts where topic_id = $topic_id");
  413. bb_moderation_hold_approve_posts($postids);
  414. endforeach; endif;
  415. }
  416.  
  417. /**
  418. * Approve Posts
  419. *
  420. * @author Aditya Naik <[email protected]>
  421. * @version v 0.01 Sun Apr 08 2007 01:34:06 GMT-0400 (Eastern Daylight Time)
  422. */
  423. function bb_moderation_hold_approve_posts($postids){
  424. global $bbdb, $thread_ids_cache;
  425. if ($postids) : foreach($postids as $post_id) :
  426. $bbdb->query("UPDATE $bbdb->posts SET post_status = '0' WHERE post_id = '$post_id'");
  427. $bb_post = bb_get_post ( $post_id );
  428. add_filter( 'get_topic_where', 'no_where' );
  429. $topic = get_topic( $bb_post->topic_id , false);
  430. $topic_id = (int) $topic->topic_id;
  431.  
  432. if (!$user = bb_get_user( $bb_post->poster_id )){
  433. $uid = 0;
  434. $uname = $bb_post->poster_name;
  435. } else {
  436. $uid = $bb_post->poster_id;
  437. }
  438.  
  439. $topic_posts = $topic->topic_posts + 1;
  440.  
  441. $bbdb->query("UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = $topic->forum_id");
  442. $bbdb->query("UPDATE $bbdb->topics SET topic_time = '" . $bb_post->post_time . "', topic_last_poster = '$uid', topic_last_poster_name = '$uname',
  443. topic_last_post_id = '$post_id', topic_posts = '$topic_posts' WHERE topic_id = '$topic_id'");
  444.  
  445. bb_update_topicmeta( $topic->topic_id, 'deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts - 1 : 0 );
  446.  
  447. if ( isset($thread_ids_cache[$topic_id]) ) {
  448. $thread_ids_cache[$topic_id]['post'][] = $post_id;
  449. $thread_ids_cache[$topic_id]['poster'][] = $uid;
  450. }
  451.  
  452. $post_ids = get_thread_post_ids( $topic_id );
  453. if ( $uid && !in_array($uid, array_slice($post_ids['poster'], 0, -1)) )
  454. bb_update_usermeta( $uid, $bb_table_prefix . 'topics_replied', $bb_current_user->data->topics_replied + 1 );
  455. endforeach; endif;
  456. }
  457.  
  458.  
  459. ?>
Advertisement
Add Comment
Please, Sign In to add comment