bpModFrontend

Handle links generation and placing in the activity loop.

Defined (1)

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

/classes/bpModFrontend.php  
  1. class bpModFrontend extends bpModeration 
  2.  
  3. var $already_flagged = array(); 
  4.  
  5. function __construct() 
  6. parent::__construct(); 
  7.  
  8. wp_enqueue_style('bp-moderation', $this->plugin_url . '/css/bp-moderation.css', false, $this->plugin_ver, 'screen'); 
  9. wp_enqueue_script('bp-moderation', $this->plugin_url . '/js/bp-moderation.js', array('jquery'), $this->plugin_ver, !'in footer'); 
  10.  
  11. add_action('bp_activity_entry_meta', array(&$this, 'activity_loop_link'), 20); 
  12.  
  13. $this->already_flagged = $this->already_flagged_by_current_user(); 
  14.  
  15. /** 
  16. * query which contents are already been flagged by the current user 
  17. * it doesn't return an array, it join everything in a comma separated string 
  18. * with each content represented by $type_$id_$id2, so when checking if a particular 
  19. * content is already flagged is not necessary to iterate them but only checking 
  20. * if a key is present 
  21. * @return <string> concatenated list of item_type, item_id, item_id2 
  22. */ 
  23. function already_flagged_by_current_user() 
  24. global $wpdb, $bp; 
  25.  
  26. if (!$bp->loggedin_user->id) { 
  27. return array(); 
  28.  
  29. //TODO: cache this 
  30.  
  31. $uid = (int)$bp->loggedin_user->id; 
  32.  
  33. $sql = "SELECT CONCAT_WS( '-', c.item_type, CAST( c.item_id AS CHAR ) , CAST( c.item_id2 AS CHAR ) ) as cont_key 
  34. FROM {$this->contents_table} c NATURAL JOIN {$this->flags_table} f WHERE f.reporter_id = {$uid}"; 
  35.  
  36. return $wpdb->get_col($sql); 
  37.  
  38.  
  39. function activity_loop_link() 
  40.  
  41. $act_type = bp_get_activity_type(); 
  42.  
  43. if (empty ($this->types_map[$act_type])) { 
  44. return; 
  45.  
  46. $args = array( 
  47. 'type' => $this->types_map[$act_type],  
  48. 'id' => bp_get_activity_item_id(),  
  49. 'id2' => bp_get_activity_secondary_item_id(),  
  50. 'author_id' => bp_get_activity_user_id(),  
  51. 'is_main_content' => bp_is_single_activity(),  
  52. 'context' => 'activity-loop',  
  53. 'custom_class' => 'button',  
  54. ); 
  55.  
  56. $args = apply_filters("bp_moderation_activity_loop_link_args_$act_type", $args); 
  57.  
  58. if ($args) { 
  59. echo $this->generate_link($args); 
  60.  
  61. /** 
  62. * generte an <a href ...> tag that point to an url that trigger the flag 
  63. * for the content identified by given parameters 
  64. * @param <array|strings> $args see defaults 
  65. * @return <string> the flag/unflag link 
  66. */ 
  67. function generate_link($args = '') 
  68.  
  69. $defaults = array( 
  70. 'type' => '', // content type slug as defined in register_content_type() (mandatory) 
  71. 'id' => 0, // primary id of the content (mandatory) 
  72. 'id2' => 0, // secondary id of the content 
  73. 'author_id' => 0, // id of the author, used only to check identify current user as the author 
  74. 'is_author' => false, // alternative to author_id, if current user is an author of the content 
  75. 'unflagged_text' => null, // text to be displayed when is not flagged, null = default text, false = no text 
  76. 'flagged_text' => null, // to be displayed when is already flagged, null = default text, false = no text 
  77. 'can_moderate' => is_super_admin(), // if current user can moderate the content, used in future features 
  78. 'is_main_content' => false, // if this content is the main content of the current page, used in future features 
  79. 'custom_class' => '', // custom css class of the link, use the class bpm-no-images if you do not want flag icon 
  80. 'context' => 'called', // internal arg, place a css class depending where and how the link is called 
  81. ); 
  82.  
  83. $params = wp_parse_args($args, $defaults); 
  84. extract($params, EXTR_SKIP); 
  85.  
  86. //check mandatory params 
  87. if (!$type || !$id) { 
  88. return false; 
  89.  
  90. $id = (int)$id; 
  91. $id2 = (int)$id2; 
  92.  
  93. //TODO: anonymous flagging?cookies? 
  94. if (!bp_loggedin_user_id()) { 
  95. return null; 
  96.  
  97. $is_author = $is_author || $author_id == bp_loggedin_user_id(); 
  98.  
  99. // role relative to the content: 'moderator', 'author' or 'normal' 
  100. if ($can_moderate) { 
  101. $role = 'moderator'; 
  102. elseif ($is_author) 
  103. $role = 'author'; 
  104. else 
  105. $role = 'normal'; 
  106.  
  107. switch ($role) { 
  108. case 'author': 
  109. return null; 
  110. case 'moderator': 
  111. case 'normal': 
  112. //has current user already flagged this? 
  113. $flagged = in_array("$type-$id-$id2", $this->already_flagged); 
  114.  
  115. $action = $flagged ? 'unflag' : 'flag'; 
  116.  
  117. $nonce = $this->create_nonce($action, $type, $id, $id2); 
  118.  
  119. $get_data = array( 
  120. 'bpmod-action' => $action,  
  121. 'type' => $type,  
  122. 'id' => $id,  
  123. 'id2' => $id2,  
  124. '_wpnonce' => $nonce 
  125. ); 
  126.  
  127. $url = bp_core_get_root_domain() . '/?' . http_build_query($get_data); 
  128.  
  129. $text = $flagged ? $flagged_text : $unflagged_text; 
  130. if (null === $text) { 
  131. $text = $flagged ? $this->options['flagged_text'] 
  132. : $this->options['unflagged_text']; 
  133.  
  134. $text = apply_filters('bp_moderation_link_text', $text, $flagged, $type, $id, $id2); 
  135.  
  136. $title_text = $flagged ? __('Unflag this content', 'bp-moderation') : __('Flag as inappropriate', 'bp-moderation'); 
  137.  
  138. $link = "<a href='$url' title='" . $title_text; 
  139. $link .= "' class='bpm-report-link bpm-type-{$type} "; 
  140. $link .= $flagged ? 'bpm-flagged ' : 'bpm-unflagged '; 
  141. $link .= (empty($text) ? 'bpm-no-text ' : ''); 
  142. $link .= "bpm-context-$context $custom_class' >"; 
  143. $link .= "<span class='bpm-inner-text' >" . ($text ? $text : ' ') . "</span>"; 
  144. $link .= "</a>"; 
  145.  
  146. return apply_filters('bp_moderation_get_link', $link, $type, $is_author, $id, $id2); 
  147. default: 
  148. return null; 
  149.  
  150.  
  151. /** 
  152. * print flag/unflag link 
  153. * static method for printing a reporting link 
  154. * @see bpModFrontend::generate_link for args details 
  155. */ 
  156. function link($args = '') 
  157. echo bpModFrontend::get_link($args); 
  158.  
  159. /** 
  160. * Get flag/unflag link 
  161. * static method for getting a reporting link 
  162. * @see bpModFrontend::generate_link for args details 
  163. */ 
  164. function get_link($args = '') 
  165. $_this =& bpModeration::get_istance(); 
  166. return $_this->generate_link($args); 
  167.