BP_Button

API to create BuddyPress buttons.

Defined (1)

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

/bp-core/classes/class-bp-button.php  
  1. class BP_Button { 
  2.  
  3. /** Button properties *****************************************************/ 
  4.  
  5. /** 
  6. * The button ID. 
  7. * @since 1.2.6 
  8. * @var string 
  9. */ 
  10. public $id = ''; 
  11.  
  12. /** 
  13. * The name of the component that the button belongs to. 
  14. * @since 1.2.6 
  15. * @var string 
  16. */ 
  17. public $component = 'core'; 
  18.  
  19. /** 
  20. * Does the user need to be logged in to see this button? 
  21. * @since 1.2.6 
  22. * @var bool 
  23. */ 
  24. public $must_be_logged_in = true; 
  25.  
  26. /** 
  27. * Whether the button should be hidden when viewing your own profile. 
  28. * @since 1.2.6 
  29. * @var bool 
  30. */ 
  31. public $block_self = true; 
  32.  
  33. /** Wrapper ***************************************************************/ 
  34.  
  35. /** 
  36. * Parent element to wrap button around. 
  37. * @since 2.7.0 
  38. * @var string Default: 'div'. 
  39. */ 
  40. public $parent_element = ''; 
  41.  
  42. /** 
  43. * Element attributes for parent element. 
  44. * @since 2.7.0 
  45. * @var array Set whatever attributes like 'id', 'class' as array key. 
  46. */ 
  47. public $parent_attr = array(); 
  48.  
  49. /** Button ****************************************************************/ 
  50.  
  51. /** 
  52. * Button element. 
  53. * @since 2.7.0 
  54. * @var string Default: 'a'. 
  55. */ 
  56. public $button_element = 'a'; 
  57.  
  58. /** 
  59. * Button attributes. 
  60. * @since 2.7.0 
  61. * @var array Set whatever attributes like 'id', 'href' as array key. 
  62. */ 
  63. public $button_attr = array(); 
  64.  
  65. /** 
  66. * The contents of the button link. 
  67. * @since 1.2.6 
  68. * @var string 
  69. */ 
  70. public $link_text = ''; 
  71.  
  72. /** 
  73. * HTML result. 
  74. * @since 1.2.6 
  75. * @var string 
  76. */ 
  77. public $contents = ''; 
  78.  
  79. /** Deprecated ***********************************************************/ 
  80.  
  81. /** 
  82. * The type of DOM element to use for a wrapper. 
  83. * @since 1.2.6 
  84. * @deprecated 2.7.0 Use $parent_element instead. 
  85. * @var string|bool 
  86. */ 
  87. public $wrapper = 'div'; 
  88.  
  89. /** 
  90. * The DOM class of the button wrapper. 
  91. * @since 1.2.6 
  92. * @deprecated 2.7.0 Set 'class' key in $parent_attr instead. 
  93. * @var string 
  94. */ 
  95. public $wrapper_class = ''; 
  96.  
  97. /** 
  98. * The DOM ID of the button wrapper. 
  99. * @since 1.2.6 
  100. * @deprecated 2.7.0 Set 'id' key in $parent_attr instead. 
  101. * @var string 
  102. */ 
  103. public $wrapper_id = ''; 
  104.  
  105. /** 
  106. * The destination link of the button. 
  107. * @since 1.2.6 
  108. * @deprecated 2.7.0 Set 'href' key in $button_attr instead. 
  109. * @var string 
  110. */ 
  111. public $link_href = ''; 
  112.  
  113. /** 
  114. * The DOM class of the button link. 
  115. * @since 1.2.6 
  116. * @deprecated 2.7.0 Set 'class' key in $button_attr instead. 
  117. * @var string 
  118. */ 
  119. public $link_class = ''; 
  120.  
  121. /** 
  122. * The DOM ID of the button link. 
  123. * @since 1.2.6 
  124. * @deprecated 2.7.0 Set 'id' key in $button_attr instead. 
  125. * @var string 
  126. */ 
  127. public $link_id = ''; 
  128.  
  129. /** 
  130. * The DOM rel value of the button link. 
  131. * @since 1.2.6 
  132. * @deprecated 2.7.0 Set 'rel' key in $button_attr instead. 
  133. * @var string 
  134. */ 
  135. public $link_rel = ''; 
  136.  
  137. /** 
  138. * Title of the button link. 
  139. * @since 1.2.6 
  140. * @deprecated 2.7.0 Set 'title' key in $button_attr instead. 
  141. * @var string 
  142. */ 
  143. public $link_title = ''; 
  144.  
  145. /** Methods ***************************************************************/ 
  146.  
  147. /** 
  148. * Builds the button based on class parameters. 
  149. * @since 1.2.6 
  150. * @param array|string $args See {@BP_Button}. 
  151. */ 
  152. public function __construct( $args = '' ) { 
  153.  
  154. $r = wp_parse_args( $args, get_class_vars( __CLASS__ ) ); 
  155.  
  156. // Backward compatibility with deprecated parameters. 
  157. $r = $this->backward_compatibility_args( $r ); 
  158.  
  159. // Deprecated. Subject to removal in a future release. 
  160. $this->wrapper = $r['wrapper']; 
  161. if ( !empty( $r['link_id'] ) ) $this->link_id = ' id="' . $r['link_id'] . '"'; 
  162. if ( !empty( $r['link_href'] ) ) $this->link_href = ' href="' . $r['link_href'] . '"'; 
  163. if ( !empty( $r['link_title'] ) ) $this->link_title = ' title="' . $r['link_title'] . '"'; 
  164. if ( !empty( $r['link_rel'] ) ) $this->link_rel = ' rel="' . $r['link_rel'] . '"'; 
  165. if ( !empty( $r['link_class'] ) ) $this->link_class = ' class="' . $r['link_class'] . '"'; 
  166. if ( !empty( $r['link_text'] ) ) $this->link_text = $r['link_text']; 
  167.  
  168. // Required button properties. 
  169. $this->id = $r['id']; 
  170. $this->component = $r['component']; 
  171. $this->must_be_logged_in = (bool) $r['must_be_logged_in']; 
  172. $this->block_self = (bool) $r['block_self']; 
  173.  
  174. // $id and $component are required and component must be active. 
  175. if ( empty( $r['id'] ) || empty( $r['component'] ) || ! bp_is_active( $this->component ) ) { 
  176. return false; 
  177.  
  178. // No button for guests if must be logged in. 
  179. if ( true == $this->must_be_logged_in && ! is_user_logged_in() ) { 
  180. return false; 
  181.  
  182. // The block_self property. 
  183. if ( true == $this->block_self ) { 
  184. /** 
  185. * No button if you are the current user in a members loop. 
  186. * This condition takes precedence, because members loops can be found on user 
  187. * profiles. 
  188. */ 
  189. if ( bp_get_member_user_id() ) { 
  190. if ( is_user_logged_in() && bp_loggedin_user_id() == bp_get_member_user_id() ) { 
  191. return false; 
  192.  
  193. // No button if viewing your own profile (and not in a members loop). 
  194. } elseif ( bp_is_my_profile() ) { 
  195. return false; 
  196.  
  197. // Should we use a parent element? 
  198. if ( ! empty( $r['parent_element'] ) ) { 
  199. if ( ! isset( $r['parent_attr']['class'] ) ) { 
  200. $r['parent_attr']['class'] = ''; 
  201.  
  202. // Always add 'generic-button' class. 
  203. if ( false === strpos( $r['parent_attr']['class'], 'generic-button' ) ) { 
  204. if ( ! empty( $r['parent_attr']['class'] ) ) { 
  205. $r['parent_attr']['class'] .= ' '; 
  206. $r['parent_attr']['class'] .= 'generic-button'; 
  207.  
  208. // Render parent element attributes. 
  209. $parent_elem = new BP_Core_HTML_Element( array( 
  210. 'element' => $r['parent_element'],  
  211. 'attr' => $r['parent_attr'] 
  212. ) ); 
  213.  
  214. // Set before and after. 
  215. $before = $parent_elem->get( 'open_tag' ); 
  216. $after = $parent_elem->get( 'close_tag' ); 
  217.  
  218. // No parent element. 
  219. } else { 
  220. $before = $after = ''; 
  221.  
  222. // Button properties. 
  223. $button = ''; 
  224. if ( ! empty( $r['button_element'] ) ) { 
  225. $button = new BP_Core_HTML_Element( array( 
  226. 'element' => $r['button_element'],  
  227. 'attr' => $r['button_attr'],  
  228. 'inner_html' => ! empty( $r['link_text'] ) ? $r['link_text'] : '' 
  229. ) ); 
  230. $button = $button->contents(); 
  231.  
  232. // Build the button. 
  233. $this->contents = $before . $button . $after; 
  234.  
  235. /** 
  236. * Filters the button based on class parameters. 
  237. * This filter is a dynamic filter based on component and component ID and 
  238. * allows button to be manipulated externally. 
  239. * @since 1.2.6 
  240. * @since 2.7.0 Added $r as a parameter. 
  241. * @param string $contents HTML being used for the button. 
  242. * @param BP_Button $this Current BP_Button instance. 
  243. * @param string $before HTML appended before the actual button. 
  244. * @param string $after HTML appended after the actual button. 
  245. * @param array $r Parsed button arguments. 
  246. */ 
  247. $this->contents = apply_filters( 'bp_button_' . $this->component . '_' . $this->id, $this->contents, $this, $before, $after, $r ); 
  248.  
  249.  
  250. /** 
  251. * Provide backward compatibility for deprecated button arguments. 
  252. * @since 2.7.0. 
  253. * @param array $r See {@link BP_Button} class for full documentation. 
  254. * @return array 
  255. */ 
  256. protected function backward_compatibility_args( $r = array() ) { 
  257. // Array of deprecated arguments. 
  258. $backpat_args = array( 
  259. 'wrapper', 'wrapper_class', 'wrapper_id',  
  260. 'link_href', 'link_class', 'link_id', 'link_rel', 'link_title' 
  261. ); 
  262.  
  263. foreach ( $backpat_args as $prop ) { 
  264. if ( empty( $r[ $prop ] ) ) { 
  265. continue; 
  266.  
  267. $parent = $child = false; 
  268. $sep = strpos( $prop, '_' ); 
  269.  
  270. // Check if this is an attribute. 
  271. if ( false !== $sep ) { 
  272. $child = true; 
  273. $parent = substr( $prop, 0, $sep ); 
  274. } else { 
  275. $parent = $prop; 
  276.  
  277. if ( 'wrapper' === $parent ) { 
  278. $parent = 'parent'; 
  279. } else { 
  280. $parent = 'button'; 
  281.  
  282. // Set element. 
  283. if ( false === $child && empty( $r[ "{$parent}_element" ] ) ) { 
  284. $r[ "{$parent}_element" ] = $r[ $prop ]; 
  285.  
  286. // Set attributes. 
  287. } elseif ( true === $child ) { 
  288. $new_prop = substr( $prop, strpos( $prop, '_' ) +1 ); 
  289. if ( empty( $r[ "{$parent}_attr" ] ) ) { 
  290. $r[ "{$parent}_attr" ] = array(); 
  291.  
  292. if ( empty( $r[ "{$parent}_attr" ][ $new_prop ] ) ) { 
  293. $r[ "{$parent}_attr" ][ $new_prop ] = $r[ $prop ]; 
  294.  
  295. return $r; 
  296.  
  297.  
  298. /** 
  299. * Return the markup for the generated button. 
  300. * @since 1.2.6 
  301. * @return string Button markup. 
  302. */ 
  303. public function contents() { 
  304. return $this->contents; 
  305.  
  306. /** 
  307. * Output the markup of button. 
  308. * @since 1.2.6 
  309. */ 
  310. public function display() { 
  311. if ( !empty( $this->contents ) ) 
  312. echo $this->contents;