bpModBackendActions

Handle backend actions.

Defined (1)

The class is defined in the following location(s).

/classes/bpModBackendActions.php  
  1. class bpModBackendActions extends bpModeration 
  2.  
  3. function __construct() 
  4.  
  5. parent::__construct(); 
  6.  
  7. add_action('admin_init', array(&$this, 'route_action')); 
  8.  
  9. function route_action() 
  10. $nonce_action = $_REQUEST['bpmod-action']; 
  11. $action = $_REQUEST['bpmod-action']; 
  12. if ('bulk_contents' == $_REQUEST['bpmod-action'] || 'bulk_users' == $_REQUEST['bpmod-action']) { 
  13. $action .= '_' . $_REQUEST['bulk-action']; 
  14.  
  15. $in_ajax = defined('DOING_AJAX'); 
  16.  
  17. if ($in_ajax) { 
  18. check_ajax_referer($nonce_action); 
  19. } else { 
  20. check_admin_referer($nonce_action); 
  21. $this->redir = remove_query_arg(array('err_ids', 'marked_spammer', 'unmarked_spammer', 'content_ignored', 'content_moderated', 'content_deleted'), wp_get_referer()); 
  22.  
  23. $handle_func = array(&$this, 'handle_' . $action); 
  24. $response_func = array(&$this, ($in_ajax ? 'ajax_' : 'action_') . $action); 
  25.  
  26. if (is_callable($handle_func)) { 
  27. $result = (array)call_user_func($handle_func); 
  28.  
  29. if ($result && is_callable($response_func)) { 
  30. call_user_func_array($response_func, $result); 
  31.  
  32. //fallback if nothing has been called 
  33. if ($in_ajax) { 
  34. die(-1); 
  35. } else { 
  36. bp_core_redirect($this->redir); 
  37.  
  38.  
  39. /**** mark/unmark spammer *****************************************************/ 
  40. function handle_mark_unmark_spammer() 
  41. if (empty($_REQUEST['user_id']) || !isset($_REQUEST['set_spam'])) { 
  42. return false; 
  43.  
  44. $this->set_spammer_status($_REQUEST['user_id'], $_REQUEST['set_spam'], array(&$this, (defined('DOING_AJAX') ? 'ajax_' : 'action_') . 'mark_unmark_spammer')); 
  45.  
  46. function handle_bulk_contents_mark_spammer() 
  47. $this->handle_bulk_contents_mark_unmark_spammer(true); 
  48.  
  49. function handle_bulk_contents_unmark_spammer() 
  50. $this->handle_bulk_contents_mark_unmark_spammer(false); 
  51.  
  52. function handle_bulk_contents_mark_unmark_spammer($set_spam) 
  53. $authors = $this->get_content_authors(@$_REQUEST['bulk_items']); 
  54.  
  55. $this->set_spammer_status($authors, $set_spam, array(&$this, 'action_mark_unmark_spammer')); 
  56.  
  57. function handle_bulk_users_mark_spammer() 
  58. $this->handle_bulk_users_mark_unmark_spammer(true); 
  59.  
  60. function handle_bulk_users_unmark_spammer() 
  61. $this->handle_bulk_users_mark_unmark_spammer(false); 
  62.  
  63. function handle_bulk_users_mark_unmark_spammer($set_spam) 
  64. $user_ids = array_filter(array_map('intval', (array)@$_REQUEST['bulk_items'])); 
  65.  
  66. $this->set_spammer_status($user_ids, $set_spam, array(&$this, 'action_mark_unmark_spammer')); 
  67.  
  68. function action_mark_unmark_spammer($successes, $errors, $is_spam) 
  69. $query_arg = $is_spam ? 'marked_spammer' : 'unmarked_spammer'; 
  70. $arg_value = count($successes); 
  71.  
  72. if (1 == $arg_value) { //with only one is nicer to display the name 
  73. $username = bp_core_get_username($successes[0]); 
  74. if (is_numeric($username)) { 
  75. $username = "username:$username"; 
  76. $arg_value = empty($successes[0]) ? 0 : $username; 
  77. $args = array($query_arg => $arg_value); 
  78. if (!empty($errors)) { 
  79. $args['err_ids'] = join(', ', $errors); 
  80.  
  81. bp_core_redirect(add_query_arg($args, $this->redir)); 
  82.  
  83. /**function ajax_mark_unmark_spammer($success, $error, $is_spam) { 
  84. if(!$uid) 
  85. die(-1); 
  86. else 
  87. die(json_encode(array())); 
  88. }*/ 
  89.  
  90. /** 
  91. * set spammeer status 
  92. * @param <int|array> $user_ids member ids 
  93. * @param <bool> $is_spam mark spammer (true) or unmark (false) 
  94. * @param <callback> $end_callback function to execute after marking/unmarking, after this bpcore will redirect back & die 
  95. */ 
  96.  
  97. function set_spammer_status($user_ids, $is_spam, $end_callback) { 
  98. global $wpdb; 
  99.  
  100. $user_ids = (array)$user_ids; 
  101. $successes = array(); 
  102.  
  103. foreach ($user_ids as $user_id) { 
  104. // Bail if user ID is super admin 
  105. if ( is_super_admin( $user_id ) ) 
  106. continue; 
  107.  
  108. // Get the blogs for the user 
  109. $blogs = get_blogs_of_user( $user_id, true ); 
  110.  
  111. foreach ( (array) $blogs as $key => $details ) { 
  112.  
  113. // Do not mark the main or current root blog as spam 
  114. if ( 1 == $details->userblog_id || bp_get_root_blog_id() == $details->userblog_id ) { 
  115. continue; 
  116.  
  117. // Update the blog status 
  118. update_blog_status( $details->userblog_id, 'spam', $is_spam ); 
  119.  
  120. // Finally, mark this user as a spammer 
  121. if ( is_multisite() ) { 
  122. update_user_status( $user_id, 'spam', $is_spam ); 
  123.  
  124. // Always set single site status 
  125. $wpdb->update( $wpdb->users, array( 'user_status' => (int)$is_spam ), array( 'ID' => $user_id ) ); 
  126.  
  127. // Hide this user's activity 
  128. if ( $is_spam && bp_is_active( 'activity' ) ) { 
  129. bp_activity_hide_user_activity( $user_id ); 
  130.  
  131. // We need a special hook for is_spam so that components can delete data at spam time 
  132. $bp_action = $is_spam ? 'bp_make_spam_user' : 'bp_make_ham_user'; 
  133. do_action( $bp_action, $user_id ); 
  134.  
  135. // Call multisite actions in single site mode for good measure 
  136. if ( !is_multisite() ) { 
  137. $wp_action = $is_spam ? 'make_spam_user' : 'make_ham_user'; 
  138. do_action( $wp_action, $user_id ); 
  139.  
  140. // Allow plugins to do neat things 
  141. do_action( 'bp_core_action_set_spammer_status', $user_id, $is_spam ); 
  142.  
  143. $successes[] = $user_id; 
  144.  
  145. $errors = array_diff($user_ids, $successes); 
  146.  
  147. call_user_func($end_callback, $successes, $errors, $is_spam); 
  148.  
  149.  
  150. /**** ignore content **********************************************************/ 
  151. function handle_ignore($cont_id = null) 
  152. return $this->change_content_status('ignored', $cont_id); 
  153.  
  154. function action_ignore($result) 
  155. bp_core_redirect(add_query_arg('content_ignored', (int)$result, $this->redir)); 
  156.  
  157. function handle_bulk_contents_ignore() 
  158. $this->bulk_loop_callback(array(&$this, 'handle_ignore'), 'content_ignored'); 
  159.  
  160.  
  161. /**** mark content moderated **************************************************/ 
  162. function handle_mark_moderated($cont_id = null) 
  163. return $this->change_content_status('moderated', $cont_id); 
  164.  
  165. function action_mark_moderated($result) 
  166. bp_core_redirect(add_query_arg('content_moderated', (int)$result, $this->redir)); 
  167.  
  168. function handle_bulk_contents_mark_moderated() 
  169. $this->bulk_loop_callback(array(&$this, 'handle_mark_moderated'), 'content_moderated'); 
  170.  
  171. /**** edit redirect ***********************************************************/ 
  172. function handle_edit() 
  173. if (empty($_REQUEST['cont_id'])) { 
  174. return false; 
  175.  
  176. bpModLoader::load_class('bpModObjContent'); 
  177. $cont = new bpModObjContent($_REQUEST['cont_id']); 
  178. if (!$cont->content_id || !is_callable($this->content_types[$cont->item_type]->callbacks['edit'])) { 
  179. return false; 
  180.  
  181. $edit_cb = $this->content_types[$cont->item_type]->callbacks['edit']; 
  182.  
  183. return call_user_func($edit_cb, $cont->item_id, $cont->item_id2); 
  184.  
  185. function action_edit($result) 
  186. if ($result) { 
  187. bp_core_redirect($result); 
  188. else 
  189. bp_core_redirect($this->redir); 
  190.  
  191. /**** delete content **********************************************************/ 
  192. function handle_delete($cont_id = null) 
  193. if (!$cont_id && !($cont_id = @$_REQUEST['cont_id'])) { 
  194. return false; 
  195.  
  196. bpModLoader::load_class('bpModObjContent'); 
  197. $cont = new bpModObjContent($cont_id); 
  198. if (!$cont->content_id || !is_callable($this->content_types[$cont->item_type]->callbacks['delete'])) { 
  199. return false; 
  200.  
  201. $delete_cb = $this->content_types[$cont->item_type]->callbacks['delete']; 
  202.  
  203. if (!call_user_func($delete_cb, $cont->item_id, $cont->item_id2)) { 
  204. return false; 
  205.  
  206. $old_status = $cont->status; 
  207.  
  208. $cont->status = 'deleted'; 
  209.  
  210. if (!$cont->save()) { 
  211. return false; 
  212.  
  213. do_action('bp_moderation_content_deleted', $cont->content_id, $cont); 
  214. do_action('bp_moderation_content_status_changed', $cont->content_id, $old_status, 'deleted', $cont->item_author, $this->get_content_reporters($cont->content_id)); 
  215.  
  216. return true; 
  217.  
  218. function action_delete($result) 
  219. bp_core_redirect(add_query_arg('content_deleted', (int)$result, $this->redir)); 
  220.  
  221. function handle_bulk_contents_delete() 
  222. $this->bulk_loop_callback(array(&$this, 'handle_delete'), 'content_deleted'); 
  223.  
  224.  
  225. /**** enable/disable hotkeys **************************************************/ 
  226. function handle_hotkeys() 
  227. update_user_option(get_current_user_id(), 'bp_moderation_hotkeys', $_REQUEST['set_hotkeys'], true); 
  228.  
  229. bp_core_redirect($this->redir); 
  230.  
  231. /**** commons/utils ***********************************************************/ 
  232. function change_content_status($new_status, $cont_id = null) 
  233. if (!$cont_id && !($cont_id = @$_REQUEST['cont_id'])) { 
  234. return false; 
  235.  
  236. bpModLoader::load_class('bpModObjContent'); 
  237. $cont = new bpModObjContent($cont_id); 
  238. if (!$cont->content_id) { 
  239. return false; 
  240.  
  241. if ($new_status == $cont->status) { 
  242. return true; 
  243.  
  244. $old_status = $cont->status; 
  245. $cont->status = $new_status; 
  246. if (!$cont->save()) { 
  247. return false; 
  248.  
  249. $reporters = $this->get_content_reporters($cont->content_id); 
  250.  
  251. do_action("bp_moderation_content_$new_status", $cont->content_id, $cont, $reporters); 
  252. do_action('bp_moderation_content_status_changed', $cont->content_id, $old_status, $new_status, $cont->item_author, $reporters, $cont); 
  253.  
  254. return true; 
  255.  
  256. function bulk_loop_callback($callback, $successes_query_arg) 
  257. $cont_ids = array_filter(array_map('intval', (array)@$_REQUEST['bulk_items'])); 
  258. $successes = 0; 
  259. $errors = array(); 
  260.  
  261. foreach ($cont_ids as $cid) { 
  262. if (call_user_func($callback, $cid)) { 
  263. $successes++; 
  264. else 
  265. $errors[] = $cid; 
  266.  
  267. $args = array($successes_query_arg => $successes); 
  268. if (!empty($errors)) { 
  269. $args['err_ids'] = join(', ', $errors); 
  270.  
  271. bp_core_redirect(add_query_arg($args, $this->redir)); 
  272.  
  273.  
  274. function get_content_reporters($cont_id) 
  275. global $wpdb; 
  276.  
  277. $cont_id = (int)$cont_id; 
  278. if (!$cont_id) { 
  279. return array(); 
  280.  
  281. $sql = "SELECT DISTINCT f.reporter_id FROM {$this->contents_table} c NATURAL JOIN {$this->flags_table} f WHERE c.content_id = $cont_id"; 
  282.  
  283. return $wpdb->get_col($sql); 
  284.  
  285. function get_content_authors($cont_ids) 
  286. global $wpdb; 
  287.  
  288. $cont_ids = implode(', ', array_filter(array_map('intval', (array)$cont_ids))); 
  289. if (!$cont_ids) { 
  290. return array(); 
  291.  
  292. $sql = "SELECT DISTINCT item_author FROM {$this->contents_table} WHERE content_id IN ($cont_ids)"; 
  293.  
  294. return $wpdb->get_col($sql);