PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/buddypress/bp-friends/bp-friends-template.php

https://bitbucket.org/codemen_iftekhar/codemen
PHP | 434 lines | 300 code | 60 blank | 74 comment | 29 complexity | 1f540c7bc1ab4b33fd23f5bb6a6d3b31 MD5 | raw file
  1. <?php
  2. /**
  3. * BuddyPress Friends Template Functions
  4. *
  5. * @package BuddyPress
  6. * @subpackage FriendsTemplate
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. /**
  11. * Output the friends component slug
  12. *
  13. * @package BuddyPress
  14. * @subpackage Friends Template
  15. * @since BuddyPress (1.5)
  16. *
  17. * @uses bp_get_friends_slug()
  18. */
  19. function bp_friends_slug() {
  20. echo bp_get_friends_slug();
  21. }
  22. /**
  23. * Return the friends component slug
  24. *
  25. * @package BuddyPress
  26. * @subpackage Friends Template
  27. * @since BuddyPress (1.5)
  28. */
  29. function bp_get_friends_slug() {
  30. global $bp;
  31. return apply_filters( 'bp_get_friends_slug', $bp->friends->slug );
  32. }
  33. /**
  34. * Output the friends component root slug
  35. *
  36. * @package BuddyPress
  37. * @subpackage Friends Template
  38. * @since BuddyPress (1.5)
  39. *
  40. * @uses bp_get_friends_root_slug()
  41. */
  42. function bp_friends_root_slug() {
  43. echo bp_get_friends_root_slug();
  44. }
  45. /**
  46. * Return the friends component root slug
  47. *
  48. * @package BuddyPress
  49. * @subpackage Friends Template
  50. * @since BuddyPress (1.5)
  51. */
  52. function bp_get_friends_root_slug() {
  53. global $bp;
  54. return apply_filters( 'bp_get_friends_root_slug', $bp->friends->root_slug );
  55. }
  56. function bp_friends_random_friends() {
  57. if ( !$friend_ids = wp_cache_get( 'friends_friend_ids_' . bp_displayed_user_id(), 'bp' ) ) {
  58. $friend_ids = BP_Friends_Friendship::get_random_friends( bp_displayed_user_id() );
  59. wp_cache_set( 'friends_friend_ids_' . bp_displayed_user_id(), $friend_ids, 'bp' );
  60. } ?>
  61. <div class="info-group">
  62. <h4><?php bp_word_or_name( __( "My Friends", 'buddypress' ), __( "%s's Friends", 'buddypress' ) ) ?> (<?php echo BP_Friends_Friendship::total_friend_count( bp_displayed_user_id() ) ?>) <span><a href="<?php echo trailingslashit( bp_displayed_user_domain() . bp_get_friends_slug() ) ?>"><?php _e('See All', 'buddypress') ?></a></span></h4>
  63. <?php if ( $friend_ids ) { ?>
  64. <ul class="horiz-gallery">
  65. <?php for ( $i = 0, $count = count( $friend_ids ); $i < $count; ++$i ) { ?>
  66. <li>
  67. <a href="<?php echo bp_core_get_user_domain( $friend_ids[$i] ) ?>"><?php echo bp_core_fetch_avatar( array( 'item_id' => $friend_ids[$i], 'type' => 'thumb' ) ) ?></a>
  68. <h5><?php echo bp_core_get_userlink($friend_ids[$i]) ?></h5>
  69. </li>
  70. <?php } ?>
  71. </ul>
  72. <?php } else { ?>
  73. <div id="message" class="info">
  74. <p><?php bp_word_or_name( __( "You haven't added any friend connections yet.", 'buddypress' ), __( "%s hasn't created any friend connections yet.", 'buddypress' ) ) ?></p>
  75. </div>
  76. <?php } ?>
  77. <div class="clear"></div>
  78. </div>
  79. <?php
  80. }
  81. /**
  82. * Pull up a group of random members, and display some profile data about them
  83. *
  84. * This function is no longer used by BuddyPress core.
  85. *
  86. * @package BuddyPress
  87. *
  88. * @param int $total_members The number of members to retrieve
  89. */
  90. function bp_friends_random_members( $total_members = 5 ) {
  91. if ( !$user_ids = wp_cache_get( 'friends_random_users', 'bp' ) ) {
  92. $user_ids = BP_Core_User::get_users( 'random', $total_members );
  93. wp_cache_set( 'friends_random_users', $user_ids, 'bp' );
  94. }
  95. ?>
  96. <?php if ( $user_ids['users'] ) { ?>
  97. <ul class="item-list" id="random-members-list">
  98. <?php for ( $i = 0, $count = count( $user_ids['users'] ); $i < $count; ++$i ) { ?>
  99. <li>
  100. <a href="<?php echo bp_core_get_user_domain( $user_ids['users'][$i]->id ) ?>"><?php echo bp_core_fetch_avatar( array( 'item_id' => $user_ids['users'][$i]->id, 'type' => 'thumb' ) ) ?></a>
  101. <h5><?php echo bp_core_get_userlink( $user_ids['users'][$i]->id ) ?></h5>
  102. <?php if ( bp_is_active( 'xprofile' ) ) { ?>
  103. <?php $random_data = xprofile_get_random_profile_data( $user_ids['users'][$i]->id, true ); ?>
  104. <div class="profile-data">
  105. <p class="field-name"><?php echo $random_data[0]->name ?></p>
  106. <?php echo $random_data[0]->value ?>
  107. </div>
  108. <?php } ?>
  109. <div class="action">
  110. <?php if ( bp_is_active( 'friends' ) ) { ?>
  111. <?php bp_add_friend_button( $user_ids['users'][$i]->id ) ?>
  112. <?php } ?>
  113. </div>
  114. </li>
  115. <?php } ?>
  116. </ul>
  117. <?php } else { ?>
  118. <div id="message" class="info">
  119. <p><?php _e( "There aren't enough site members to show a random sample just yet.", 'buddypress' ) ?></p>
  120. </div>
  121. <?php } ?>
  122. <?php
  123. }
  124. function bp_friend_search_form() {
  125. $action = bp_displayed_user_domain() . bp_get_friends_slug() . '/my-friends/search/';
  126. $label = __( 'Filter Friends', 'buddypress' ); ?>
  127. <form action="<?php echo $action ?>" id="friend-search-form" method="post">
  128. <label for="friend-search-box" id="friend-search-label"><?php echo $label ?></label>
  129. <input type="search" name="friend-search-box" id="friend-search-box" value="<?php echo $value ?>"<?php echo $disabled ?> />
  130. <?php wp_nonce_field( 'friends_search', '_wpnonce_friend_search' ) ?>
  131. <input type="hidden" name="initiator" id="initiator" value="<?php echo esc_attr( bp_displayed_user_id() ) ?>" />
  132. </form>
  133. <?php
  134. }
  135. function bp_member_add_friend_button() {
  136. global $members_template;
  137. if ( !isset( $members_template->member->is_friend ) || null === $members_template->member->is_friend )
  138. $friend_status = 'not_friends';
  139. else
  140. $friend_status = ( 0 == $members_template->member->is_friend ) ? 'pending' : 'is_friend';
  141. echo bp_get_add_friend_button( $members_template->member->id, $friend_status );
  142. }
  143. add_action( 'bp_directory_members_actions', 'bp_member_add_friend_button' );
  144. function bp_member_total_friend_count() {
  145. echo bp_get_member_total_friend_count();
  146. }
  147. function bp_get_member_total_friend_count() {
  148. global $members_template;
  149. if ( 1 == (int) $members_template->member->total_friend_count )
  150. return apply_filters( 'bp_get_member_total_friend_count', sprintf( __( '%d friend', 'buddypress' ), (int) $members_template->member->total_friend_count ) );
  151. else
  152. return apply_filters( 'bp_get_member_total_friend_count', sprintf( __( '%d friends', 'buddypress' ), (int) $members_template->member->total_friend_count ) );
  153. }
  154. /**
  155. * bp_potential_friend_id( $user_id )
  156. *
  157. * Outputs the ID of the potential friend
  158. *
  159. * @uses bp_get_potential_friend_id()
  160. * @param <type> $user_id
  161. */
  162. function bp_potential_friend_id( $user_id = 0 ) {
  163. echo bp_get_potential_friend_id( $user_id );
  164. }
  165. /**
  166. * bp_get_potential_friend_id( $user_id )
  167. *
  168. * Returns the ID of the potential friend
  169. *
  170. * @global object $friends_template
  171. * @param int $user_id
  172. * @return int ID of potential friend
  173. */
  174. function bp_get_potential_friend_id( $user_id = 0 ) {
  175. global $friends_template;
  176. if ( empty( $user_id ) && isset( $friends_template->friendship->friend ) )
  177. $user_id = $friends_template->friendship->friend->id;
  178. else if ( empty( $user_id ) && !isset( $friends_template->friendship->friend ) )
  179. $user_id = bp_displayed_user_id();
  180. return apply_filters( 'bp_get_potential_friend_id', (int) $user_id );
  181. }
  182. /**
  183. * bp_is_friend( $user_id )
  184. *
  185. * Returns - 'is_friend', 'not_friends', 'pending'
  186. *
  187. * @param int $potential_friend_id
  188. * @return string
  189. */
  190. function bp_is_friend( $user_id = 0 ) {
  191. if ( !is_user_logged_in() )
  192. return false;
  193. if ( empty( $user_id ) )
  194. $user_id = bp_get_potential_friend_id( $user_id );
  195. if ( bp_loggedin_user_id() == $user_id )
  196. return false;
  197. return apply_filters( 'bp_is_friend', friends_check_friendship_status( bp_loggedin_user_id(), $user_id ), $user_id );
  198. }
  199. function bp_add_friend_button( $potential_friend_id = 0, $friend_status = false ) {
  200. echo bp_get_add_friend_button( $potential_friend_id, $friend_status );
  201. }
  202. function bp_get_add_friend_button( $potential_friend_id = 0, $friend_status = false ) {
  203. if ( empty( $potential_friend_id ) )
  204. $potential_friend_id = bp_get_potential_friend_id( $potential_friend_id );
  205. $is_friend = bp_is_friend( $potential_friend_id );
  206. if ( empty( $is_friend ) )
  207. return false;
  208. switch ( $is_friend ) {
  209. case 'pending' :
  210. $button = array(
  211. 'id' => 'pending',
  212. 'component' => 'friends',
  213. 'must_be_logged_in' => true,
  214. 'block_self' => true,
  215. 'wrapper_class' => 'friendship-button pending_friend',
  216. 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
  217. 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/cancel/' . $potential_friend_id . '/', 'friends_withdraw_friendship' ),
  218. 'link_text' => __( 'Cancel Friendship Request', 'buddypress' ),
  219. 'link_title' => __( 'Cancel Friendship Requested', 'buddypress' ),
  220. 'link_id' => 'friend-' . $potential_friend_id,
  221. 'link_rel' => 'remove',
  222. 'link_class' => 'friendship-button pending_friend requested'
  223. );
  224. break;
  225. case 'is_friend' :
  226. $button = array(
  227. 'id' => 'is_friend',
  228. 'component' => 'friends',
  229. 'must_be_logged_in' => true,
  230. 'block_self' => false,
  231. 'wrapper_class' => 'friendship-button is_friend',
  232. 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
  233. 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/remove-friend/' . $potential_friend_id . '/', 'friends_remove_friend' ),
  234. 'link_text' => __( 'Cancel Friendship', 'buddypress' ),
  235. 'link_title' => __( 'Cancel Friendship', 'buddypress' ),
  236. 'link_id' => 'friend-' . $potential_friend_id,
  237. 'link_rel' => 'remove',
  238. 'link_class' => 'friendship-button is_friend remove'
  239. );
  240. break;
  241. default:
  242. $button = array(
  243. 'id' => 'not_friends',
  244. 'component' => 'friends',
  245. 'must_be_logged_in' => true,
  246. 'block_self' => true,
  247. 'wrapper_class' => 'friendship-button not_friends',
  248. 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
  249. 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/add-friend/' . $potential_friend_id . '/', 'friends_add_friend' ),
  250. 'link_text' => __( 'Add Friend', 'buddypress' ),
  251. 'link_title' => __( 'Add Friend', 'buddypress' ),
  252. 'link_id' => 'friend-' . $potential_friend_id,
  253. 'link_rel' => 'add',
  254. 'link_class' => 'friendship-button not_friends add'
  255. );
  256. break;
  257. }
  258. // Filter and return the HTML button
  259. return bp_get_button( apply_filters( 'bp_get_add_friend_button', $button ) );
  260. }
  261. function bp_get_friend_ids( $user_id = 0 ) {
  262. if ( empty( $user_id ) )
  263. $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
  264. $friend_ids = friends_get_friend_user_ids( $user_id );
  265. if ( empty( $friend_ids ) )
  266. return false;
  267. return implode( ',', friends_get_friend_user_ids( $user_id ) );
  268. }
  269. /**
  270. * Get a user's friendship requests
  271. *
  272. * Note that we return a 0 if no pending requests are found. This is necessary because of the
  273. * structure of the $include parameter in bp_has_members().
  274. *
  275. * @param int $user_id Defaults to displayed user
  276. * @return mixed Returns an array of users if found, or a 0 if none are found
  277. */
  278. function bp_get_friendship_requests( $user_id = 0 ) {
  279. if ( !$user_id ) {
  280. $user_id = bp_displayed_user_id();
  281. }
  282. if ( !$user_id ) {
  283. return 0;
  284. }
  285. $requests = friends_get_friendship_request_user_ids( $user_id );
  286. if ( !empty( $requests ) ) {
  287. $requests = implode( ',', (array) $requests );
  288. } else {
  289. $requests = 0;
  290. }
  291. return apply_filters( 'bp_get_friendship_requests', $requests );
  292. }
  293. function bp_friend_friendship_id() {
  294. echo bp_get_friend_friendship_id();
  295. }
  296. function bp_get_friend_friendship_id() {
  297. global $members_template;
  298. if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
  299. $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
  300. wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
  301. }
  302. return apply_filters( 'bp_get_friend_friendship_id', $friendship_id );
  303. }
  304. function bp_friend_accept_request_link() {
  305. echo bp_get_friend_accept_request_link();
  306. }
  307. function bp_get_friend_accept_request_link() {
  308. global $members_template;
  309. if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
  310. $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
  311. wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
  312. }
  313. return apply_filters( 'bp_get_friend_accept_request_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/accept/' . $friendship_id, 'friends_accept_friendship' ) );
  314. }
  315. function bp_friend_reject_request_link() {
  316. echo bp_get_friend_reject_request_link();
  317. }
  318. function bp_get_friend_reject_request_link() {
  319. global $members_template;
  320. if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
  321. $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
  322. wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
  323. }
  324. return apply_filters( 'bp_get_friend_reject_request_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/reject/' . $friendship_id, 'friends_reject_friendship' ) );
  325. }
  326. function bp_total_friend_count( $user_id = 0 ) {
  327. echo bp_get_total_friend_count( $user_id );
  328. }
  329. function bp_get_total_friend_count( $user_id = 0 ) {
  330. return apply_filters( 'bp_get_total_friend_count', friends_get_total_friend_count( $user_id ) );
  331. }
  332. add_filter( 'bp_get_total_friend_count', 'bp_core_number_format' );
  333. function bp_friend_total_requests_count( $user_id = 0 ) {
  334. echo bp_friend_get_total_requests_count( $user_id );
  335. }
  336. function bp_friend_get_total_requests_count( $user_id = 0 ) {
  337. if ( empty( $user_id ) )
  338. $user_id = bp_loggedin_user_id();
  339. return apply_filters( 'bp_friend_get_total_requests_count', count( BP_Friends_Friendship::get_friend_user_ids( $user_id, true ) ) );
  340. }
  341. ?>