bpModActions

Handle frontend actions.

Defined (1)

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

/classes/bpModActions.php  
  1. class bpModActions extends bpModeration 
  2.  
  3. function __construct() 
  4. parent::__construct(); 
  5.  
  6. add_action('bp_moderation_init', array(&$this, 'route_action')); 
  7.  
  8. function route_action() 
  9. if (empty($_REQUEST['bpmod-action'])) return; 
  10.  
  11. $method = (defined('DOING_AJAX') && DOING_AJAX ? 'ajax_' : 'action_') . $_REQUEST['bpmod-action']; 
  12.  
  13. if (is_callable(array($this, $method))) { 
  14. call_user_func(array($this, $method)); 
  15.  
  16. function action_flag() 
  17. if ('flag' != $_REQUEST['bpmod-action']) { 
  18. return false; 
  19.  
  20. $result = $this->request_flag(); 
  21.  
  22. if ('already flagged' === $result) { 
  23. bp_core_add_message(__('You have already flagged this content', 'bp-moderation'), 'error'); 
  24. } elseif (true === $result) { 
  25. bp_core_add_message(__('Thank you for your report', 'bp-moderation'), 'success'); 
  26. } else { 
  27. bp_core_add_message(__('There was a problem flagging that content', 'bp-moderation'), 'error'); 
  28.  
  29. bp_core_redirect(wp_get_referer()); 
  30.  
  31.  
  32. function ajax_flag() 
  33. if ('flag' != $_REQUEST['bpmod-action']) { 
  34. return false; 
  35.  
  36. $result = $this->request_flag(); 
  37.  
  38. if ($result) { 
  39. $text = $this->options['flagged_text']; 
  40. $text = apply_filters('bp_moderation_link_text', $text, true, $_REQUEST['type'], $_REQUEST['id'], $_REQUEST['id2']); 
  41.  
  42. $new_nonce = $this->create_nonce('unflag', $_REQUEST['type'], $_REQUEST['id'], $_REQUEST['id2']); 
  43.  
  44. $message = array('msg' => $text, 'type' => 'success', 'new_nonce' => $new_nonce); 
  45.  
  46. if ('already flagged' === $result) { 
  47. $message['type'] = 'fade warning'; 
  48. $message['fade_msg'] = __('Already flagged', 'bp-moderation'); 
  49. } else { 
  50. $message = array('msg' => __('Error', 'bp-moderation'), 'type' => 'error'); 
  51.  
  52. die(json_encode($message)); 
  53.  
  54. function request_flag() 
  55. $type = $_REQUEST['type']; 
  56. $id = $_REQUEST['id']; 
  57. $id2 = $_REQUEST['id2']; 
  58.  
  59. if (!$this->verify_nonce('flag', $type, $id, $id2)) { 
  60. return false; 
  61.  
  62. global $bp; 
  63.  
  64. if (!$reporter = $bp->loggedin_user->id) { 
  65. return false; 
  66.  
  67. return $this->flag($type, $id, $id2, $reporter); 
  68.  
  69.  
  70. function flag($type, $id, $id2, $reporter) 
  71. list($cont_id, $flag_id) = $this->check_flag($type, $id, $id2, $reporter); 
  72. if ($cont_id && $flag_id) { 
  73. return 'already flagged'; 
  74.  
  75. $flag_count = false; 
  76.  
  77. bpModLoader::load_class('bpModObjContent'); 
  78. if ($cont_id) { 
  79. $cont = new bpModObjContent($cont_id); 
  80. } else { 
  81. $callaback_info = $this->content_types[$type]->callbacks['info']; 
  82. $info = call_user_func($callaback_info, $id, $id2); 
  83. if (!$info) { 
  84. return false; 
  85.  
  86. $cont = new bpModObjContent(); 
  87. $cont->item_type = $type; 
  88. $cont->item_id = $id; 
  89. $cont->item_id2 = $id2; 
  90. $cont->item_author = $info['author']; 
  91. $cont->item_url = $info['url']; 
  92. $cont->item_date = $info['date']; 
  93. $cont->status = 'new'; 
  94. $cont->save(); 
  95. $cont_id = $cont->content_id; 
  96. do_action('bp_moderation_first_flag', array(&$cont)); 
  97.  
  98. $flag_count = 1; 
  99.  
  100. if (!$cont_id) { 
  101. return false; 
  102.  
  103. bpModLoader::load_class('bpModObjFlag'); 
  104. $flag = new bpModObjFlag(); 
  105. $flag->content_id = $cont_id; 
  106. $flag->reporter_id = $reporter; 
  107. $flag->date = gmdate("Y-m-d H:i:s", time()); 
  108. $flag->save(); 
  109.  
  110. if ($flag->flag_id) { 
  111. if (!$flag_count) { 
  112. $flag_count = $this->count_flags($cont_id); 
  113.  
  114. //check and send warning message 
  115. $warning_threshold = $this->options['warning_threshold']; 
  116. if ('new' == $cont->status && $warning_threshold && $flag_count >= $warning_threshold) { 
  117. $this->send_warning($cont, $flag_count); 
  118.  
  119. do_action_ref_array('bp_moderation_content_flagged', array(&$cont, &$flag)); 
  120. return true; 
  121. } else 
  122. return false; 
  123.  
  124.  
  125. function send_warning(&$cont, $flag_count) 
  126. $sitename = get_blog_option(BP_ROOT_BLOG, 'blogname'); 
  127. $siteurl = get_blog_option(BP_ROOT_BLOG, 'siteurl'); 
  128. $author_name = bp_core_get_user_displayname($cont->item_author); 
  129. $author_email = bp_core_get_user_email($cont->item_author); 
  130.  
  131. $subject = "[$sitename] " . __('one of your contents has been reported as inappropriate', 'bp-moderation'); 
  132. $message = $this->options['warning_message']; 
  133. $message = str_replace('%AUTHORNAME%', $author_name, $message); 
  134. $message = str_replace('%CONTENTURL%', $cont->item_url, $message); 
  135. $message = str_replace('%SITENAME%', $sitename, $message); 
  136. $message = str_replace('%SITEURL%', $siteurl, $message); 
  137.  
  138. wp_mail($author_email, $subject, $message); 
  139.  
  140. $cont->status = 'warned'; 
  141. $cont->save(); 
  142.  
  143. do_action('bp_moderation_content_warned', $cont->content_id, $cont); 
  144.  
  145. if ($this->options['warning_forward']) { 
  146. $admin_subject = "[$sitename] " . sprintf(__('a warning for inappropriate content has been sent to %s', 'bp-moderation'), $author_name); 
  147. $admin_msg_prefix = sprintf(__( 
  148. 'Content url: %1$s 
  149. Total flags: %2$s 
  150. Author profile: %3$s 
  151.  
  152. ----- Message sent to the author ----- 
  153. ', 'bp-moderation'), $cont->item_url, $flag_count, bp_core_get_user_domain($cont->item_author)); 
  154.  
  155. wp_mail($this->options['warning_forward'], $admin_subject, $admin_msg_prefix . $message); 
  156.  
  157. function action_unflag() 
  158. if ('unflag' != $_REQUEST['bpmod-action']) { 
  159. return false; 
  160.  
  161. $result = $this->request_unflag(); 
  162.  
  163. if ('non flagged' === $result) { 
  164. bp_core_add_message(__("This content wasn't flagged", 'bp-moderation'), 'error'); 
  165. } elseif ($result) { 
  166. bp_core_add_message(__('Thank you for your report', 'bp-moderation'), 'success'); 
  167. } else { 
  168. bp_core_add_message(__('There was a problem unflagging that content', 'bp-moderation'), 'error'); 
  169.  
  170. bp_core_redirect(wp_get_referer()); 
  171.  
  172. function ajax_unflag() 
  173. if ('unflag' != $_REQUEST['bpmod-action']) { 
  174. return false; 
  175.  
  176. $result = $this->request_unflag(); 
  177.  
  178. if ($result) { 
  179. $text = $this->options['unflagged_text']; 
  180. $text = apply_filters('bp_moderation_link_text', $text, false, $_REQUEST['type'], $_REQUEST['id'], $_REQUEST['id2']); 
  181.  
  182. $new_nonce = $this->create_nonce('flag', $_REQUEST['type'], $_REQUEST['id'], $_REQUEST['id2']); 
  183.  
  184. $message = array('msg' => $text, 'type' => 'success', 'new_nonce' => $new_nonce); 
  185.  
  186. if ('non flagged' === $result) { 
  187. $message['type'] = 'fade warning'; 
  188. $message['fade_msg'] = __("It wasn't flagged", 'bp-moderation'); 
  189.  
  190. } else { 
  191. $message = array('msg' => __('Error', 'bp-moderation'), 'type' => 'error'); 
  192.  
  193. die(json_encode($message)); 
  194.  
  195. function request_unflag() 
  196. $type = $_REQUEST['type']; 
  197. $id = $_REQUEST['id']; 
  198. $id2 = $_REQUEST['id2']; 
  199.  
  200. if (!$this->verify_nonce('unflag', $type, $id, $id2)) { 
  201. return false; 
  202.  
  203. global $bp; 
  204.  
  205. if (!$reporter = $bp->loggedin_user->id) { 
  206. return false; 
  207.  
  208. return $this->unflag($type, $id, $id2, $reporter); 
  209.  
  210.  
  211. function unflag($type, $id, $id2, $reporter) 
  212.  
  213. list($cont_id, $flag_id) = $this->check_flag($type, $id, $id2, $reporter); 
  214.  
  215. if (!$cont_id || !$flag_id) { 
  216. return 'non flagged'; 
  217.  
  218. bpModLoader::load_class('bpModObjFlag'); 
  219. $flag = new bpModObjFlag($flag_id); 
  220.  
  221. if ($flag->delete()) { 
  222. do_action_ref_array('bp_moderation_content_unflagged', array($type, $id, $id2, $reporter, $cont_id, &$flag)); 
  223. return true; 
  224. } else { 
  225. return false; 
  226.  
  227. /** 
  228. * check if the content identified by $type, $id, $id2 is already in the db 
  229. * and if is already flagged by $reporter 
  230. * @param <string> $type 
  231. * @param <int> $id 
  232. * @param <int> $id2 
  233. * @param <int> $reporter 
  234. * @return <array> (content_id, flag_id) could return null, null|int, null|int, int 
  235. */ 
  236. function check_flag($type, $id, $id2, $reporter) 
  237. global $wpdb; 
  238.  
  239. $sql = <<<SQL 
  240. SELECT c.content_id, f.flag_id 
  241. FROM {$this->contents_table} c LEFT OUTER JOIN {$this->flags_table} f 
  242. ON (c.content_id = f.content_id AND f.reporter_id = %d ) 
  243. WHERE c.item_type = %s AND c.item_id = %d AND c.item_id2 = %d LIMIT 1 
  244. SQL; 
  245. $sql = $wpdb->prepare($sql, $reporter, $type, $id, $id2); 
  246.  
  247. $cont_id = $wpdb->get_var($sql, 0); 
  248. $flag_id = $wpdb->get_var(null, 1); 
  249.  
  250. return array($cont_id, $flag_id); 
  251.  
  252.  
  253. /** 
  254. * count how many times a content has been flagged 
  255. * @param int $cont_id the content id 
  256. * @return int flags count 
  257. */ 
  258. function count_flags($cont_id) 
  259. global $wpdb; 
  260.  
  261.  
  262. $sql = $wpdb->prepare("SELECT COUNT(*) FROM {$this->flags_table} f WHERE content_id = %d", $cont_id); 
  263.  
  264. $count = (int)$wpdb->get_var($sql); 
  265.  
  266. return $count; 
  267.