Advertisement
jungermedia

BP-like.php

May 23rd, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.80 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: BuddyPress Like
  4. Plugin URI: http://bplike.wordpress.com
  5. Description: Gives users the ability to 'like' content across your BuddyPress enabled site.
  6. Author: Alex Hempton-Smith
  7. Version: 0.0.8
  8. Author URI: http://bplike.wordpress.com
  9. */
  10.  
  11. /* Make sure BuddyPress is loaded before we do anything. */
  12. if ( !function_exists( 'bp_core_install' ) ) {
  13.  
  14. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  15.  
  16. if ( is_plugin_active( 'buddypress/bp-loader.php' ) ) {
  17. require_once ( WP_PLUGIN_DIR . '/buddypress/bp-loader.php' );
  18. } else {
  19. add_action( 'admin_notices', 'bp_like_install_buddypress_notice' );
  20. return;
  21. }
  22. }
  23.  
  24. define ( 'BP_LIKE_VERSION', '0.0.8' );
  25. define ( 'BP_LIKE_DB_VERSION', '10' );
  26.  
  27. /**
  28. * bp_like_install()
  29. *
  30. * Installs or upgrades the database content
  31. *
  32. */
  33. function bp_like_install() {
  34.  
  35. $default_text_strings = array(
  36. 'like' => array(
  37. 'default' => __('Like', 'buddypress-like'),
  38. 'custom' => __('Like', 'buddypress-like')
  39. ),
  40. 'unlike' => array(
  41. 'default' => __('Unlike', 'buddypress-like'),
  42. 'custom' => __('Unlike', 'buddypress-like')
  43. ),
  44. 'like_this_item' => array(
  45. 'default' => __('Like this item', 'buddypress-like'),
  46. 'custom' => __('Like this item', 'buddypress-like')
  47. ),
  48. 'unlike_this_item' => array(
  49. 'default' => __('Unlike this item', 'buddypress-like'),
  50. 'custom' => __('Unlike this item', 'buddypress-like')
  51. ),
  52. 'view_likes' => array(
  53. 'default' => __('View likes', 'buddypress-like'),
  54. 'custom' => __('View likes', 'buddypress-like')
  55. ),
  56. 'hide_likes' => array(
  57. 'default' => __('Hide likes', 'buddypress-like'),
  58. 'custom' => __('Hide likes', 'buddypress-like')
  59. ),
  60. 'show_activity_likes' => array(
  61. 'default' => __('Show Activity Likes', 'buddypress-like'),
  62. 'custom' => __('Show Activity Likes', 'buddypress-like')
  63. ),
  64. 'show_blogpost_likes' => array(
  65. 'default' => __('Show Blog Post Likes', 'buddypress-like'),
  66. 'custom' => __('Show Blog Post Likes', 'buddypress-like')
  67. ),
  68. 'must_be_logged_in' => array(
  69. 'default' => __('Sorry, you must be logged in to like that.', 'buddypress-like'),
  70. 'custom' => __('Sorry, you must be logged in to like that.', 'buddypress-like')),
  71. 'record_activity_likes_own' => array(
  72. 'default' => __('%user% likes their own <a href="%permalink%">activity</a>', 'buddypress-like'),
  73. 'custom' => __('%user% likes their own <a href="%permalink%">activity</a>', 'buddypress-like')
  74. ),
  75. 'record_activity_likes_an' => array(
  76. 'default' => __('%user% likes an <a href="%permalink%">activity</a>', 'buddypress-like'),
  77. 'custom' => __('%user% likes an <a href="%permalink%">activity</a>', 'buddypress-like')
  78. ),
  79. 'record_activity_likes_users' => array(
  80. 'default' => __('%user% likes %author%\'s <a href="%permalink%">activity</a>', 'buddypress-like'),
  81. 'custom' => __('%user% likes %author%\'s <a href="%permalink%">activity</a>', 'buddypress-like')
  82. ),
  83. 'record_activity_likes_own_blogpost' => array(
  84. 'default' => __('%user% likes their own blog post, <a href="%permalink%">%title%</a>', 'buddypress-like'),
  85. 'custom' => __('%user% likes their own blog post, <a href="%permalink%">%title%</a>', 'buddypress-like')
  86. ),
  87. 'record_activity_likes_a_blogpost' => array(
  88. 'default' => __('%user% likes a blog post, <a href="%permalink%">%title%</a>', 'buddypress-like'),
  89. 'custom' => __('%user% likes an blog post, <a href="%permalink%">%title%</a>', 'buddypress-like')
  90. ),
  91. 'record_activity_likes_users_blogpost' => array(
  92. 'default' => __('%user% likes %author%\'s blog post, <a href="%permalink%">%title%</a>', 'buddypress-like'),
  93. 'custom' => __('%user% likes %author%\'s blog post, <a href="%permalink%">%title%</a>', 'buddypress-like')
  94. ),
  95. 'get_likes_no_likes' => array(
  96. 'default' => __('Nobody likes this yet.', 'buddypress-like'),
  97. 'custom' => __('Nobody likes this yet.', 'buddypress-like')
  98. ),
  99. 'get_likes_only_liker' => array(
  100. 'default' => __('You are the only person who likes this so far.', 'buddypress-like'),
  101. 'custom' => __('You are the only person who likes this so far.', 'buddypress-like')
  102. ),
  103. 'get_likes_you_and_singular' => array(
  104. 'default' => __('You and %count% other person like this.', 'buddypress-like'),
  105. 'custom' => __('You and %count% other person like this.', 'buddypress-like')
  106. ),
  107. 'get_likes_you_and_plural' => array(
  108. 'default' => __('You and %count% other people like this', 'buddypress-like'),
  109. 'custom' => __('You and %count% other people like this', 'buddypress-like')
  110. ),
  111. 'get_likes_count_people_singular' => array(
  112. 'default' => __('%count% person likes this.', 'buddypress-like'),
  113. 'custom' => __('%count% person likes this.', 'buddypress-like')
  114. ),
  115. 'get_likes_count_people_plural' => array(
  116. 'default' => __('%count% people like this.', 'buddypress-like'),
  117. 'custom' => __('%count% people like this.', 'buddypress-like')
  118. ),
  119. 'get_likes_and_people_singular' => array(
  120. 'default' => __('and %count% other person like this.', 'buddypress-like'),
  121. 'custom' => __('and %count% other person like this.', 'buddypress-like')
  122. ),
  123. 'get_likes_and_people_plural' => array(
  124. 'default' => __('and %count% other people like this.', 'buddypress-like'),
  125. 'custom' => __('and %count% other people like this.', 'buddypress-like')
  126. ),
  127. 'get_likes_likes_this' => array(
  128. 'default' => __('likes this.', 'buddypress-like'),
  129. 'custom' => __('likes this.', 'buddypress-like')
  130. ),
  131. 'get_likes_like_this' => array(
  132. 'default' => __('like this.', 'buddypress-like'),
  133. 'custom' => __('like this.', 'buddypress-like')
  134. ),
  135. 'get_likes_no_friends_you_and_singular' => array(
  136. 'default' => __('None of your friends like this yet, but you and %count% other person does.', 'buddypress-like'),
  137. 'custom' => __('None of your friends like this yet, but you and %count% other person does.', 'buddypress-like')
  138. ),
  139. 'get_likes_no_friends_you_and_plural' => array(
  140. 'default' => __('None of your friends like this yet, but you and %count% other people do.', 'buddypress-like'),
  141. 'custom' => __('None of your friends like this yet, but you and %count% other people do.', 'buddypress-like')
  142. ),
  143. 'get_likes_no_friends_singular' => array(
  144. 'default' => __('None of your friends like this yet, but %count% other person does.', 'buddypress-like'),
  145. 'custom' => __('None of your friends like this yet, but %count% other person does.', 'buddypress-like')
  146. ),
  147. 'get_likes_no_friends_plural' => array(
  148. 'default' => __('None of your friends like this yet, but %count% other people do.', 'buddypress-like'),
  149. 'custom' => __('None of your friends like this yet, but %count% other people do.', 'buddypress-like')
  150. )
  151. );
  152.  
  153. $current_settings = get_site_option('bp_like_settings');
  154.  
  155. if ( $current_settings['post_to_activity_stream'] )
  156. $post_to_activity_stream = $current_settings['post_to_activity_stream'];
  157. else
  158. $post_to_activity_stream = 1;
  159.  
  160. if ( $current_settings['show_excerpt'] )
  161. $show_excerpt = $current_settings['show_excerpt'];
  162. else
  163. $show_excerpt = 0;
  164.  
  165. if ( $current_settings['excerpt_length'] )
  166. $excerpt_length = $current_settings['excerpt_length'];
  167. else
  168. $excerpt_length = 140;
  169.  
  170. if ( $current_settings['likers_visibility'] )
  171. $likers_visibility = $current_settings['likers_visibility'];
  172. else
  173. $likers_visibility = 'show_all';
  174.  
  175. if ( $current_settings['name_or_avatar'] )
  176. $name_or_avatar = $current_settings['name_or_avatar'];
  177. else
  178. $name_or_avatar = 'name';
  179.  
  180. if ( $current_settings['text_strings'] ) :
  181.  
  182. $current_text_strings = $current_settings['text_strings'];
  183.  
  184. /* Go through each string and update the default to the current default, keep the custom settings */
  185. foreach( $default_text_strings as $string_name => $string_contents ) :
  186.  
  187. $default = $default_text_strings[$string_name]['default'];
  188. $custom = $current_settings['text_strings'][$string_name]['custom'];
  189.  
  190. if ( empty( $custom ) )
  191. $custom = $default;
  192.  
  193. $text_strings[$string_name] = array('default' => $default, 'custom' => $custom);
  194.  
  195. endforeach;
  196.  
  197. else :
  198. $text_strings = $default_text_strings;
  199. endif;
  200.  
  201. $settings = array(
  202. 'likers_visibility' => $likers_visibility,
  203. 'post_to_activity_stream' => $post_to_activity_stream,
  204. 'show_excerpt' => $show_excerpt,
  205. 'excerpt_length' => $excerpt_length,
  206. 'text_strings' => $text_strings,
  207. 'name_or_avatar' => $name_or_avatar
  208. );
  209.  
  210. update_site_option( 'bp_like_db_version', BP_LIKE_DB_VERSION );
  211. update_site_option( 'bp_like_settings', $settings );
  212.  
  213. add_action( 'admin_notices', 'bp_like_updated_notice' );
  214. }
  215.  
  216. /* The notice we show when the plugin is installed. */
  217. function bp_like_install_buddypress_notice() {
  218.  
  219. echo '<div id="message" class="error fade"><p style="line-height: 150%">';
  220. _e('<strong>BuddyPress Like</strong></a> requires the BuddyPress plugin to work. Please <a href="http://buddypress.org">install BuddyPress</a> first, or <a href="plugins.php">deactivate BuddyPress Like</a>.', 'buddypress-like');
  221. echo '</p></div>';
  222.  
  223. }
  224.  
  225. /* The notice we show if the plugin is updated. */
  226. function bp_like_updated_notice() {
  227.  
  228. if ( !is_site_admin() )
  229. return false;
  230.  
  231. echo '<div id="message" class="updated fade"><p style="line-height: 150%">';
  232. printf(__('<strong>BuddyPress Like</strong> has been successfully updated to version %s.', 'buddypress-like'), BP_LIKE_VERSION);
  233. echo '</p></div>';
  234.  
  235. }
  236.  
  237. /**
  238. * bp_like_check_installed()
  239. *
  240. * Checks to see if the DB tables exist or if you are running an old version
  241. * of the component. If it matches, it will run the installation function.
  242. * This means we don't have to deactivate and then reactivate.
  243. *
  244. */
  245. function bp_like_check_installed() {
  246. global $wpdb;
  247.  
  248. if ( !is_site_admin() )
  249. return false;
  250.  
  251. if ( !get_site_option( 'bp_like_settings' ) || get_site_option( 'bp-like-db-version' ) )
  252. bp_like_install();
  253.  
  254. if ( get_site_option( 'bp_like_db_version' ) < BP_LIKE_DB_VERSION )
  255. bp_like_install();
  256. }
  257. add_action( 'admin_menu', 'bp_like_check_installed' );
  258.  
  259. /**
  260. * bp_like_get_settings()
  261. *
  262. * Returns settings from the database
  263. *
  264. */
  265. function bp_like_get_settings( $option = false ) {
  266.  
  267. $settings = get_site_option( 'bp_like_settings' );
  268.  
  269. if (!$option)
  270. return $settings;
  271.  
  272. else
  273. return $settings[$option];
  274. }
  275.  
  276. /**
  277. * bp_like_get_text()
  278. *
  279. * Returns a custom text string from the database
  280. *
  281. */
  282. function bp_like_get_text( $text = false, $type = 'custom' ) {
  283.  
  284. $settings = get_site_option( 'bp_like_settings' );
  285. $text_strings = $settings['text_strings'];
  286. $string = $text_strings[$text];
  287.  
  288. return $string[$type];
  289. }
  290.  
  291. /**
  292. * bp_like_process_ajax()
  293. *
  294. * Runs the relevant function depending on what Ajax call has been made.
  295. *
  296. */
  297. function bp_like_process_ajax() {
  298. global $bp;
  299.  
  300. $id = preg_replace( "/\D/", "", $_POST['id'] );
  301.  
  302. if ( $_POST['type'] == 'like' )
  303. bp_like_add_user_like( $id, 'activity' );
  304.  
  305. if ( $_POST['type'] == 'unlike' )
  306. bp_like_remove_user_like( $id, 'activity' );
  307.  
  308. if ( $_POST['type'] == 'view-likes' )
  309. bp_like_get_likes( $id, 'activity' );
  310.  
  311. if ( $_POST['type'] == 'like_blogpost' )
  312. bp_like_add_user_like( $id, 'blogpost' );
  313.  
  314. if ( $_POST['type'] == 'unlike_blogpost' )
  315. bp_like_remove_user_like( $id, 'blogpost' );
  316.  
  317. die();
  318. }
  319. add_action( 'wp_ajax_activity_like', 'bp_like_process_ajax' );
  320.  
  321. /**
  322. * bp_like_is_liked()
  323. *
  324. * Checks to see whether the user has liked a given item.
  325. *
  326. */
  327. function bp_like_is_liked( $item_id = '', $type = '', $user_id = '' ) {
  328. global $bp;
  329.  
  330. if ( !$type )
  331. return false;
  332.  
  333. if ( !$item_id )
  334. return false;
  335.  
  336. if ( !$user_id )
  337. $user_id = $bp->loggedin_user->id;
  338.  
  339. if ( $type == 'activity' )
  340. $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  341.  
  342. if ( $type == 'blogpost' )
  343. $user_likes = get_user_meta( $user_id, 'bp_liked_blogposts', true );
  344.  
  345. if ( !$user_likes ){
  346. return false;
  347. } elseif ( !array_key_exists( $item_id, $user_likes ) ) {
  348. return false;
  349. } else {
  350. return true;
  351. };
  352. }
  353.  
  354. /**
  355. * bp_like_add_user_like()
  356. *
  357. * Registers that the user likes a given item.
  358. *
  359. */
  360. function bp_like_add_user_like( $item_id = '', $type = 'activity' ) {
  361. global $bp;
  362.  
  363. if ( !$item_id )
  364. return false;
  365.  
  366. if ( !$user_id )
  367. $user_id = $bp->loggedin_user->id;
  368.  
  369. if ( $user_id == 0 ) {
  370. echo bp_like_get_text( 'must_be_logged_in' );
  371. return false;
  372. }
  373.  
  374. if ( $type == 'activity' ) :
  375.  
  376. /* Add to the users liked activities. */
  377. $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  378. $user_likes[$item_id] = 'activity_liked';
  379. update_user_meta( $user_id, 'bp_liked_activities', $user_likes );
  380.  
  381. /* Add to the total likes for this activity. */
  382. $users_who_like = bp_activity_get_meta( $item_id, 'liked_count', true );
  383. $users_who_like[$user_id] = 'user_likes';
  384. bp_activity_update_meta( $item_id, 'liked_count', $users_who_like );
  385.  
  386. $liked_count = count( $users_who_like );
  387.  
  388. /* Publish to the activity stream if we're allowed to. */
  389. if ( bp_like_get_settings( 'post_to_activity_stream' ) == 1 ) {
  390.  
  391. $activity = bp_activity_get_specific( array( 'activity_ids' => $item_id, 'component' => 'bp-like' ) );
  392. $author_id = $activity['activities'][0]->user_id;
  393.  
  394. if ($user_id == $author_id)
  395. $action = bp_like_get_text( 'record_activity_likes_own' );
  396. elseif ($user_id == 0)
  397. $action = bp_like_get_text( 'record_activity_likes_an' );
  398. else
  399. $action = bp_like_get_text( 'record_activity_likes_users' );
  400.  
  401. $liker = bp_core_get_userlink( $user_id );
  402. $author = bp_core_get_userlink( $author_id );
  403. $activity_url = bp_activity_get_permalink( $item_id );
  404.  
  405. /* Grab the content and make it into an excerpt of 140 chars if we're allowed */
  406. if ( bp_like_get_settings( 'show_excerpt' ) == 1 ) {
  407. $content = $activity['activities'][0]->content;
  408. if ( strlen( $content ) > bp_like_get_settings( 'excerpt_length' ) ) {
  409. $content = substr( $content, 0, bp_like_get_settings( 'excerpt_length' ) );
  410. $content = $content.'...';
  411. }
  412. };
  413.  
  414.  
  415. /* Filter out the placeholders */
  416. $action = str_replace( '%user%', $liker, $action );
  417. $action = str_replace( '%permalink%', $activity_url, $action );
  418. $action = str_replace( '%author%', $author, $action );
  419.  
  420. bp_activity_add(
  421. array(
  422. 'action' => $action,
  423. 'content' => $content,
  424. 'primary_link' => $activity_url,
  425. 'component' => 'bp-like',
  426. 'type' => 'activity_liked',
  427. 'user_id' => $user_id,
  428. 'item_id' => $item_id
  429. )
  430. );
  431.  
  432. };
  433.  
  434. elseif ( $type == 'blogpost' ) :
  435.  
  436. /* Add to the users liked blog posts. */
  437. $user_likes = get_user_meta( $user_id, 'bp_liked_blogposts', true);
  438. $user_likes[$item_id] = 'blogpost_liked';
  439. update_user_meta( $user_id, 'bp_liked_blogposts', $user_likes );
  440.  
  441. /* Add to the total likes for this blog post. */
  442. $users_who_like = get_post_meta( $item_id, 'liked_count', true );
  443. $users_who_like[$user_id] = 'user_likes';
  444. update_post_meta( $item_id, 'liked_count', $users_who_like );
  445.  
  446. $liked_count = count( $users_who_like );
  447.  
  448. if ( bp_like_get_settings( 'post_to_activity_stream' ) == 1 ) {
  449. $post = get_post($item_id);
  450. $author_id = $post->post_author;
  451.  
  452. $liker = bp_core_get_userlink( $user_id );
  453. $permalink = get_permalink( $item_id );
  454. $title = $post->post_title;
  455. $author = bp_core_get_userlink( $post->post_author );
  456.  
  457. if ($user_id == $author_id)
  458. $action = bp_like_get_text( 'record_activity_likes_own_blogpost' );
  459. elseif ($user_id == 0)
  460. $action = bp_like_get_text( 'record_activity_likes_a_blogpost' );
  461. else
  462. $action = bp_like_get_text( 'record_activity_likes_users_blogpost' );
  463.  
  464. /* Filter out the placeholders */
  465. $action = str_replace( '%user%', $liker, $action );
  466. $action = str_replace( '%permalink%', $permalink, $action );
  467. $action = str_replace( '%title%', $title, $action );
  468. $action = str_replace( '%author%', $author, $action );
  469.  
  470. /* Grab the content and make it into an excerpt of 140 chars if we're allowed */
  471. if ( bp_like_get_settings( 'show_excerpt' ) == 1 ) {
  472. $content = $post->post_content;
  473. if ( strlen( $content ) > bp_like_get_settings( 'excerpt_length' ) ) {
  474. $content = substr( $content, 0, bp_like_get_settings( 'excerpt_length' ) );
  475. $content = $content.'...';
  476. }
  477. };
  478.  
  479. bp_activity_add(
  480. array(
  481. 'action' => $action,
  482. 'content' => $content,
  483. 'component' => 'bp-like',
  484. 'type' => 'blogpost_liked',
  485. 'user_id' => $user_id,
  486. 'item_id' => $item_id,
  487. 'primary_link' => $permalink
  488. )
  489. );
  490.  
  491. };
  492.  
  493. endif;
  494.  
  495. echo bp_like_get_text( 'unlike' );
  496. if ($liked_count)
  497. echo ' (' . $liked_count . ')';
  498. }
  499.  
  500. /**
  501. * bp_like_remove_user_like()
  502. *
  503. * Registers that the user has unliked a given item.
  504. *
  505. */
  506. function bp_like_remove_user_like( $item_id = '', $type = 'activity') {
  507. global $bp;
  508.  
  509. if ( !$item_id )
  510. return false;
  511.  
  512. if ( !$user_id )
  513. $user_id = $bp->loggedin_user->id;
  514.  
  515. if ( $user_id == 0 ) {
  516. echo bp_like_get_text( 'must_be_logged_in' );
  517. return false;
  518. }
  519.  
  520. if ( $type == 'activity' ) :
  521.  
  522. /* Remove this from the users liked activities. */
  523. $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  524. unset( $user_likes[$item_id] );
  525. update_user_meta( $user_id, 'bp_liked_activities', $user_likes );
  526.  
  527. /* Update the total number of users who have liked this activity. */
  528. $users_who_like = bp_activity_get_meta( $item_id, 'liked_count', true );
  529. unset( $users_who_like[$user_id] );
  530.  
  531. /* If nobody likes the activity, delete the meta for it to save space, otherwise, update the meta */
  532. if ( empty( $users_who_like ) )
  533. bp_activity_delete_meta( $item_id, 'liked_count' );
  534. else
  535. bp_activity_update_meta( $item_id, 'liked_count', $users_who_like );
  536.  
  537. $liked_count = count( $users_who_like );
  538.  
  539. /* Remove the update on the users profile from when they liked the activity. */
  540. $update_id = bp_activity_get_activity_id(
  541. array(
  542. 'item_id' => $item_id,
  543. 'component' => 'bp-like',
  544. 'type' => 'activity_liked',
  545. 'user_id' => $user_id
  546. )
  547. );
  548.  
  549. bp_activity_delete(
  550. array(
  551. 'id' => $update_id,
  552. 'item_id' => $item_id,
  553. 'component' => 'bp-like',
  554. 'type' => 'activity_liked',
  555. 'user_id' => $user_id
  556. )
  557. );
  558.  
  559. elseif ( $type == 'blogpost' ) :
  560.  
  561. /* Remove this from the users liked activities. */
  562. $user_likes = get_user_meta( $user_id, 'bp_liked_blogposts', true );
  563. unset( $user_likes[$item_id] );
  564. update_user_meta( $user_id, 'bp_liked_blogposts', $user_likes );
  565.  
  566. /* Update the total number of users who have liked this blog post. */
  567. $users_who_like = get_post_meta( $item_id, 'liked_count', true );
  568. unset( $users_who_like[$user_id] );
  569.  
  570. /* If nobody likes the blog post, delete the meta for it to save space, otherwise, update the meta */
  571. if ( empty( $users_who_like ) )
  572. delete_post_meta( $item_id, 'liked_count' );
  573. else
  574. update_post_meta( $item_id, 'liked_count', $users_who_like );
  575.  
  576. $liked_count = count( $users_who_like );
  577.  
  578. /* Remove the update on the users profile from when they liked the activity. */
  579. $update_id = bp_activity_get_activity_id(
  580. array(
  581. 'item_id' => $item_id,
  582. 'component' => 'bp-like',
  583. 'type' => 'blogpost_liked',
  584. 'user_id' => $user_id
  585. )
  586. );
  587.  
  588. bp_activity_delete(
  589. array(
  590. 'id' => $update_id,
  591. 'item_id' => $item_id,
  592. 'component' => 'bp-like',
  593. 'type' => 'blogpost_liked',
  594. 'user_id' => $user_id
  595. )
  596. );
  597.  
  598. endif;
  599.  
  600. echo bp_like_get_text( 'like' );
  601. if ($liked_count)
  602. echo ' (' . $liked_count . ')';
  603. }
  604.  
  605. /**
  606. * bp_like_get_likes()
  607. *
  608. * Outputs a list of users who have liked a given item.
  609. *
  610. */
  611. function bp_like_get_likes( $item_id = '', $type = '', $user_id = '' ) {
  612. global $bp;
  613.  
  614. if ( !$type || !$item_id )
  615. return false;
  616.  
  617. if ( !$user_id )
  618. $user_id = $bp->loggedin_user->id;
  619.  
  620. if ( $type == 'activity' ) :
  621.  
  622. /* Grab some core data we will need later on, specific to activities */
  623. $users_who_like = array_keys( bp_activity_get_meta( $item_id, 'liked_count' ) );
  624. $liked_count = count( bp_activity_get_meta( $item_id, 'liked_count' ) );
  625.  
  626. /* Intercept any messages if nobody likes it, just incase the button was clicked when it shouldn't be */
  627. if ( $liked_count == 0 ) :
  628.  
  629. $output .= bp_like_get_text( 'get_likes_no_likes' );
  630.  
  631. /* We should show information about all likers */
  632. elseif ( bp_like_get_settings( 'likers_visibility' ) == 'show_all' ) :
  633.  
  634. /* Settings say we should show their name. */
  635. if ( bp_like_get_settings( 'name_or_avatar' ) == 'name' ) :
  636.  
  637. /* Current user likes it too, remove them from the liked count and output appropriate message */
  638. if ( bp_like_is_liked( $item_id, 'activity', $user_id ) ) :
  639.  
  640. $liked_count = $liked_count-1;
  641.  
  642. if ( $liked_count == 1 )
  643. $output .= bp_like_get_text( 'get_likes_you_and_singular' );
  644.  
  645. elseif ( $liked_count == 0 )
  646. $output .= bp_like_get_text('get_likes_only_liker');
  647.  
  648. else
  649. $output .= bp_like_get_text( 'get_likes_you_and_plural' );
  650.  
  651. else :
  652.  
  653. if ( $liked_count == 1 )
  654. $output .= bp_like_get_text( 'get_likes_count_people_singular' );
  655.  
  656. else
  657. $output .= bp_like_get_text( 'get_likes_count_people_plural' );
  658.  
  659. endif;
  660.  
  661. /* Now output the name of each person who has liked it (except the current user obviously) */
  662. foreach( $users_who_like as $id ) :
  663.  
  664. if ( $id != $user_id )
  665. $output .= ' &middot <a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . bp_core_get_user_displayname( $id ) . '</a>';
  666.  
  667. endforeach;
  668.  
  669. /* Settings say we should show their avatar. */
  670. elseif ( bp_like_get_settings( 'name_or_avatar' ) == 'avatar' ) :
  671.  
  672. /* Output the avatar of each person who has liked it. */
  673. foreach( $users_who_like as $id ) :
  674.  
  675. $output .= '<a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . bp_core_fetch_avatar( array( 'item_id' => $id, 'object' => 'user', 'type' => 'thumb', 'width' => 30, 'height' => 30 ) ) . '</a> ';
  676.  
  677. endforeach;
  678.  
  679. endif;
  680.  
  681. /* We should show the information of friends, but only the number of non-friends. */
  682. elseif ( bp_like_get_settings( 'likers_visibility' ) == 'friends_names_others_numbers' && bp_is_active( 'friends' ) ) :
  683.  
  684. /* Grab some information about their friends. */
  685. $users_friends = friends_get_friend_user_ids( $user_id );
  686. if ( !empty( $users_friends ) )
  687. $friends_who_like = array_intersect( $users_who_like, $users_friends );
  688.  
  689. /* Current user likes it, so reduce the liked count by 1, to get the number of other people who like it. */
  690. if ( bp_like_is_liked( $item_id, 'activity', $user_id ) )
  691. $liked_count = $liked_count-1;
  692.  
  693. /* Settings say we should show their names. */
  694. if ( bp_like_get_settings( 'name_or_avatar' ) == 'name' ) :
  695.  
  696. /* Current user likes it too, tell them. */
  697. if ( bp_like_is_liked( $item_id, 'activity', $user_id ) )
  698. $output .= 'You ';
  699.  
  700. /* Output the name of each friend who has liked it. */
  701. foreach( $users_who_like as $id ) :
  702.  
  703. if ( in_array( $id, $friends_who_like ) ) {
  704. $output .= ' &middot <a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . bp_core_get_user_displayname( $id ) . '</a> ';
  705.  
  706. $liked_count = $liked_count-1;
  707. }
  708.  
  709. endforeach;
  710.  
  711. /* If non-friends like it, say so. */
  712. if ( $liked_count == 1 )
  713. $output .= bp_like_get_text( 'get_likes_and_people_singular' );
  714.  
  715. elseif ( $liked_count > 1 )
  716. $output .= bp_like_get_text( 'get_likes_and_people_plural' );
  717.  
  718. else
  719. $output .= bp_like_get_text( 'get_likes_like_this' );
  720.  
  721. /* Settings say we should show their avatar. */
  722. elseif ( bp_like_get_settings( 'name_or_avatar' ) == 'avatar' ) :
  723.  
  724. /* Output the avatar of each friend who has liked it, as well as the current users' if they have. */
  725. if ( !empty( $friends_who_like ) ) :
  726.  
  727. foreach( $users_who_like as $id ) :
  728.  
  729. if ( $id == $user_id || in_array( $id, $friends_who_like ) ) {
  730. $user_info = get_userdata( $id );
  731. $output .= '<a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . get_avatar( $user_info->user_email, 30 ) . '</a> ';
  732. }
  733.  
  734. endforeach;
  735.  
  736. endif;
  737.  
  738. endif;
  739.  
  740. elseif ( bp_like_get_settings( 'likers_visibility' ) == 'friends_names_others_numbers' && !bp_is_active( 'friends' ) ||bp_like_get_settings( 'likers_visibility' ) == 'just_numbers' ) :
  741.  
  742. /* Current user likes it too, remove them from the liked count and output appropriate message */
  743. if ( bp_like_is_liked( $item_id, 'activity', $user_id ) ) :
  744.  
  745. $liked_count = $liked_count-1;
  746.  
  747. if ( $liked_count == 1 )
  748. $output .= bp_like_get_text( 'get_likes_you_and_singular' );
  749.  
  750. elseif ( $liked_count == 0 )
  751. $output .= bp_like_get_text('get_likes_only_liker');
  752.  
  753. else
  754. $output .= bp_like_get_text( 'get_likes_you_and_plural' );
  755.  
  756. else :
  757.  
  758. if ( $liked_count == 1 )
  759. $output .= bp_like_get_text( 'get_likes_count_people_singular' );
  760.  
  761. else
  762. $output .= bp_like_get_text( 'get_likes_count_people_plural' );
  763.  
  764. endif;
  765.  
  766. endif;
  767.  
  768. endif;
  769.  
  770. /* Filter out the placeholder. */
  771. $output = str_replace( '%count%', $liked_count, $output );
  772.  
  773. echo $output;
  774.  
  775. }
  776.  
  777. /**
  778. * bp_like_button()
  779. *
  780. * Outputs the 'Like/Unlike' and 'View likes/Hide likes' buttons.
  781. *
  782. */
  783. function bp_like_button( $id = '', $type = '' ) {
  784.  
  785. $users_who_like = 0;
  786. $liked_count = 0;
  787.  
  788. /* Set the type if not already set, and check whether we are outputting the button on a blogpost or not. */
  789. if ( !$type && !is_single() )
  790. $type = 'activity';
  791. elseif ( !$type && is_single() )
  792. $type = 'blogpost';
  793.  
  794. if ( $type == 'activity' ) :
  795.  
  796. $activity = bp_activity_get_specific( array( 'activity_ids' => bp_get_activity_id() ) );
  797. $activity_type = $activity['activities'][0]->type;
  798.  
  799. if ( is_user_logged_in() && $activity_type !== 'activity_liked' ) :
  800.  
  801. if ( bp_activity_get_meta( bp_get_activity_id(), 'liked_count', true )) {
  802. $users_who_like = array_keys( bp_activity_get_meta( bp_get_activity_id(), 'liked_count', true ) );
  803. $liked_count = count( $users_who_like );
  804. }
  805.  
  806. if ( !bp_like_is_liked( bp_get_activity_id(), 'activity' ) ) : ?>
  807. <a href="#" class="like" id="like-activity-<?php bp_activity_id(); ?>" title="<?php echo bp_like_get_text( 'like_this_item' ); ?>"><?php echo bp_like_get_text( 'like' ); if ( $liked_count ) echo ' (' . $liked_count . ')'; ?></a>
  808. <?php else : ?>
  809. <a href="#" class="unlike" id="unlike-activity-<?php bp_activity_id(); ?>" title="<?php echo bp_like_get_text( 'unlike_this_item' ); ?>"><?php echo bp_like_get_text( 'unlike' ); if ( $liked_count ) echo ' (' . $liked_count . ')'; ?></a>
  810. <?php endif;
  811.  
  812. if ( $users_who_like ): ?>
  813. <a href="#" class="view-likes" id="view-likes-<?php bp_activity_id(); ?>"><?php echo bp_like_get_text( 'view_likes' ); ?></a>
  814. <p class="users-who-like" id="users-who-like-<?php bp_activity_id(); ?>"></p>
  815. <?php
  816. endif;
  817. endif;
  818.  
  819. elseif ( $type == 'blogpost' ) :
  820. global $post;
  821.  
  822. if ( !$id && is_single() )
  823. $id = $post->ID;
  824.  
  825. if ( is_user_logged_in() && get_post_meta( $id, 'liked_count', true ) ) {
  826. $liked_count = count( get_post_meta( $id, 'liked_count', true ) );
  827. }
  828.  
  829. if ( !bp_like_is_liked( $id, 'blogpost' ) ) : ?>
  830.  
  831. <div class="like-box"><a href="#" class="like_blogpost" id="like-blogpost-<?php echo $id; ?>" title="<?php echo bp_like_get_text( 'like_this_item' ); ?>"><?php echo bp_like_get_text( 'like' ); if ( $liked_count ) echo ' (' . $liked_count . ')'; ?></a></div>
  832.  
  833. <?php else : ?>
  834.  
  835. <div class="like-box"><a href="#" class="unlike_blogpost" id="unlike-blogpost-<?php echo $id; ?>" title="<?php echo bp_like_get_text( 'unlike_this_item' ); ?>"><?php echo bp_like_get_text( 'unlike' ); if ( $liked_count ) echo ' (' . $liked_count . ')'; ?></a></div>
  836. <?php endif;
  837.  
  838. endif;
  839. };
  840. add_filter( 'bp_activity_entry_meta', 'bp_like_button' );
  841. add_action( 'bp_before_blog_single_post', 'bp_like_button' );
  842.  
  843. /**
  844. * bp_like_activity_filter()
  845. *
  846. * Adds 'Show Activity Likes' to activity stream filters.
  847. *
  848. */
  849. function bp_like_activity_filter() {
  850. echo '<option value="activity_liked">' . bp_like_get_text( 'show_activity_likes' ) . '</option>';
  851. echo '<option value="blogpost_liked">Show Blog Post Likes</option>';
  852. }
  853. add_action( 'bp_activity_filter_options', 'bp_like_activity_filter' );
  854. add_action( 'bp_member_activity_filter_options', 'bp_like_activity_filter' );
  855. add_action( 'bp_group_activity_filter_options', 'bp_like_activity_filter' );
  856.  
  857. /**
  858. * bp_like_list_scripts()
  859. *
  860. * Includes the Javascript required for Ajax etc.
  861. *
  862. */
  863. function bp_like_list_scripts() {
  864. wp_enqueue_script(
  865. "bp-like",
  866. path_join( WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/_inc/js/bp-like.min.js" ),
  867. array( 'jquery' )
  868. );
  869. }
  870. add_action( 'wp_print_scripts', 'bp_like_list_scripts' );
  871.  
  872. /**
  873. * bp_like_insert_head()
  874. *
  875. * Includes any CSS and/or Javascript needed in the <head>.
  876. *
  877. */
  878. function bp_like_insert_head() {
  879. ?>
  880. <style type="text/css">
  881. .bp-like.activity_liked .activity-meta,
  882. .bp-like.blogpost_liked .activity-meta,
  883. .users-who-like,
  884. .mini a.view-likes,
  885. .mini a.hide-likes {
  886. display: none;
  887. }
  888.  
  889. /* To match the default theme */
  890. #bp-default .users-who-like {
  891. margin: 10px 0 -10px 0;
  892. background: #F5F5F5;
  893. -moz-border-radius: 4px;
  894. -webkit-border-radius: 4px;
  895. border-radius: 4px;
  896. padding: 8px 8px 0px 12px;
  897. color: #8C8A8F;
  898. }
  899. #bp-default .users-who-like a {
  900. color: #777;
  901. padding: 0;
  902. background: none;
  903. border: none;
  904. text-decoration: underline;
  905. font-size: 12px;
  906. }
  907. #bp-default .users-who-like a:hover { color: #222; }
  908. #bp-default .mini .users-who-like {
  909. width: 100%;
  910. position: absolute;
  911. top: 0;
  912. left: 0;
  913. }
  914. #bp-default .users-who-like img.avatar {
  915. float: none;
  916. border: none;
  917. width: 30px;
  918. height: 30px;
  919. padding: 0;
  920. margin: 0;
  921. }
  922. ul#activity-stream li.bp-like .activity-inner {
  923. border-left: 3px solid #ddd;
  924. color: #888;
  925. padding-left: 15px;
  926. font-style: italic;
  927. }
  928. #bp-default div.post div.author-box, div.comment-avatar-box {
  929. position: relative;
  930. }
  931. #bp-default div.like-box {
  932. background: #f0f0f0;
  933. width: 90px;
  934. position: absolute;
  935. bottom: -40px;
  936. left: 0;
  937. font-family: georgia, times, serif;
  938. font-style: italic;
  939. text-align: center;
  940. padding: 5px 0;
  941. -moz-border-radius: 3px;
  942. -webkit-border-radius: 3px;
  943. border-radius: 3px;
  944. }
  945. </style>
  946. <script type="text/javascript">
  947. /* <![CDATA[ */
  948. var bp_like_terms_like = '<?php echo bp_like_get_text( 'like' ); ?>';
  949. var bp_like_terms_like_message = '<?php echo bp_like_get_text( 'like_this_item' ); ?>';
  950. var bp_like_terms_unlike_message = '<?php echo bp_like_get_text( 'unlike_this_item' ); ?>';
  951. var bp_like_terms_view_likes = '<?php echo bp_like_get_text( 'view_likes' ); ?>';
  952. var bp_like_terms_hide_likes = '<?php echo bp_like_get_text( 'hide_likes' ); ?>';
  953. var bp_like_terms_unlike_1 = '<?php echo bp_like_get_text( 'unlike' ); ?> (1)';
  954. /* ]]> */
  955. </script>
  956. <?php
  957. }
  958. add_action( 'wp_head', 'bp_like_insert_head' );
  959.  
  960. /**
  961. * bp_like_add_admin_page_menu()
  962. *
  963. * Adds "BuddyPress Like" to the main BuddyPress admin menu.
  964. *
  965. */
  966. function bp_like_add_admin_page_menu() {
  967. add_submenu_page(
  968. 'bp-general-settings',
  969. 'BuddyPress Like',
  970. 'BuddyPress Like',
  971. 'manage_options',
  972. 'bp-like-settings',
  973. 'bp_like_admin_page'
  974. );
  975. }
  976. add_action( 'admin_menu', 'bp_like_add_admin_page_menu' );
  977.  
  978. /**
  979. * bp_like_admin_page_verify_nonce()
  980. *
  981. * When the settings form is submitted, verifies the nonce to ensure security.
  982. *
  983. */
  984. function bp_like_admin_page_verify_nonce() {
  985. if( isset( $_POST['_wpnonce'] ) && isset( $_POST['bp_like_updated'] ) ) {
  986. $nonce = $_REQUEST['_wpnonce'];
  987. if ( !wp_verify_nonce( $nonce, 'bp-like-admin' ) )
  988. wp_die( __('You do not have permission to do that.') );
  989. }
  990. }
  991. add_action( 'init', 'bp_like_admin_page_verify_nonce' );
  992.  
  993. /**
  994. * bp_like_admin_page()
  995. *
  996. * Outputs the admin settings page.
  997. *
  998. */
  999. function bp_like_admin_page() {
  1000. global $current_user;
  1001.  
  1002. wp_get_current_user();
  1003.  
  1004. /* Update our options if the form has been submitted */
  1005. if( isset( $_POST['_wpnonce'] ) && isset( $_POST['bp_like_updated'] ) ) {
  1006.  
  1007. /* Add each text string to the $strings_to_save array */
  1008. foreach ( $_POST as $key => $value ) {
  1009. if ( preg_match( "/text_string_/i", $key )) {
  1010. $default = bp_like_get_text( str_replace( 'bp_like_admin_text_string_', '', $key), 'default' );
  1011. $strings_to_save[str_replace( 'bp_like_admin_text_string_', '', $key )] = array('default' => $default, 'custom' => stripslashes( $value ));
  1012. }
  1013. }
  1014.  
  1015. /* Now actually save the data to the options table */
  1016. update_site_option(
  1017. 'bp_like_settings',
  1018. array(
  1019. 'likers_visibility' => $_POST['bp_like_admin_likers_visibility'],
  1020. 'post_to_activity_stream' => $_POST['bp_like_admin_post_to_activity_stream'],
  1021. 'show_excerpt' => $_POST['bp_like_admin_show_excerpt'],
  1022. 'excerpt_length' => $_POST['bp_like_admin_excerpt_length'],
  1023. 'text_strings' => $strings_to_save,
  1024. 'translate_nag' => bp_like_get_settings( 'translate_nag' ),
  1025. 'name_or_avatar' => $_POST['name_or_avatar']
  1026. )
  1027. );
  1028.  
  1029. /* Let the user know everything's cool */
  1030. echo '<div class="updated"><p><strong>';
  1031. _e('Settings saved.', 'wordpress');
  1032. echo '</strong></p></div>';
  1033. }
  1034.  
  1035. $text_strings = bp_like_get_settings( 'text_strings' );
  1036.  
  1037. ?>
  1038. <style type="text/css">
  1039. #icon-bp-like-settings { background: url('<?php echo plugins_url('/_inc/img/bp-like-icon32.png', __FILE__); ?>') no-repeat top left; }
  1040. table input { width: 100%; }
  1041. table label { display: block; }
  1042. </style>
  1043. <script type="text/javascript">
  1044. jQuery(document).ready( function() {
  1045. jQuery('select.name-or-avatar').change(function(){
  1046. var value = jQuery(this).val();
  1047. jQuery('select.name-or-avatar').val(value);
  1048. });
  1049. });
  1050. </script>
  1051.  
  1052. <div class="wrap">
  1053. <div id="icon-bp-like-settings" class="icon32"><br /></div>
  1054. <h2><?php _e('BuddyPress Like Settings', 'buddypress-like'); ?></h2>
  1055. <form action="" method="post" id="bp-like-admin-form">
  1056. <input type="hidden" name="bp_like_updated" value="updated">
  1057.  
  1058. <h3><?php _e('General Settings', 'buddypress-like'); ?></h3>
  1059. <p><input type="checkbox" id="bp_like_admin_post_to_activity_stream" name="bp_like_admin_post_to_activity_stream" value="1"<?php if (bp_like_get_settings( 'post_to_activity_stream' ) == 1) echo ' checked="checked"'?>> <label for="bp_like_admin_post_activity_updates"><?php _e("Post an activity update when something is liked", 'buddypress-like'); ?>, (e.g. "<?php echo $current_user->display_name; ?> likes Bob's activity")</label></p>
  1060. <p><input type="checkbox" id="bp_like_admin_show_excerpt" name="bp_like_admin_show_excerpt" value="1"<?php if (bp_like_get_settings( 'show_excerpt' ) == 1) echo ' checked="checked"'?>> <label for="bp_like_admin_show_excerpt"><?php _e("Show a short excerpt of the activity that has been liked", 'buddypress-like'); ?></label>; limit to <input type="text" maxlength="3" style="width: 40px" value="<?php echo bp_like_get_settings( 'excerpt_length' ); ?>" name="bp_like_admin_excerpt_length" /> characters.</p>
  1061.  
  1062. <h3><?php _e("'View Likes' Visibility", "buddypress-like"); ?></h3>
  1063. <p><?php _e("Choose how much information about the 'likers' of a particular item is shown;", "buddypress-like"); ?></p>
  1064. <p style="line-height: 200%;">
  1065. <input type="radio" name="bp_like_admin_likers_visibility" value="show_all"<?php if ( bp_like_get_settings( 'likers_visibility' ) == 'show_all' ) { echo ' checked="checked""'; }; ?> /> Show <select name="name_or_avatar" class="name-or-avatar"><option value="name"<?php if ( bp_like_get_settings( 'name_or_avatar' ) == 'name' ) { echo ' selected="selected""'; }; ?>>names</option><option value="avatar"<?php if ( bp_like_get_settings( 'name_or_avatar' ) == 'avatar' ) { echo ' selected="selected""'; }; ?>>avatars</option></select> of all likers<br />
  1066. <?php if ( bp_is_active( 'friends' ) ) { ?>
  1067. <input type="radio" name="bp_like_admin_likers_visibility" value="friends_names_others_numbers"<?php if ( bp_like_get_settings( 'likers_visibility' ) == 'friends_names_others_numbers' ) { echo ' checked="checked""'; }; ?> /> Show <select name="name_or_avatar" class="name-or-avatar"><option value="name"<?php if ( bp_like_get_settings( 'name_or_avatar' ) == 'name' ) { echo ' selected="selected""'; }; ?>>names</option><option value="avatar"<?php if ( bp_like_get_settings( 'name_or_avatar' ) == 'avatar' ) { echo ' selected="selected""'; }; ?>>avatars</option></select> of friends, and the number of non-friends<br />
  1068. <?php }; ?>
  1069. <input type="radio" name="bp_like_admin_likers_visibility" value="just_numbers"<?php if ( bp_like_get_settings( 'likers_visibility' ) == 'just_numbers' ) { echo ' checked="checked""'; }; ?> /> <?php _e('Show only the number of likers', 'buddypress-like'); ?>
  1070. </p>
  1071. <h3><?php _e('Custom Messages', 'buddypress-like'); ?></h3>
  1072. <p><?php _e("Change what messages are shown to users. For example, they can 'love' or 'dig' items instead of liking them.", "buddypress-like"); ?><br /><br /></p>
  1073.  
  1074. <table class="widefat fixed" cellspacing="0">
  1075. <thead>
  1076. <tr>
  1077. <th scope="col" id="default" class="column-name" style="width: 43%;"><?php _e('Default', 'buddypress-like'); ?></th>
  1078. <th scope="col" id="custom" class="column-name" style=""><?php _e('Custom', 'buddypress-like'); ?></th>
  1079. </tr>
  1080. </thead>
  1081. <tfoot>
  1082. <tr>
  1083. <th colspan="2" id="default" class="column-name"></th>
  1084. </tr>
  1085. </tfoot>
  1086.  
  1087. <?php foreach ( $text_strings as $key => $string ) : ?>
  1088. <tr valign="top">
  1089. <th scope="row" style="width:400px;"><label for="bp_like_admin_text_string_<?php echo $key; ?>"><?php echo htmlspecialchars( $string['default'] ); ?></label></th>
  1090. <td><input name="bp_like_admin_text_string_<?php echo $key; ?>" id="bp_like_admin_text_string_<?php echo $key; ?>" value="<?php echo htmlspecialchars( $string['custom'] ); ?>" class="regular-text" type="text"></td>
  1091. </tr>
  1092. <?php endforeach; ?>
  1093. </tbody>
  1094. </table>
  1095.  
  1096. <p class="submit">
  1097. <input class="button-primary" type="submit" name="bp-like-admin-submit" id="bp-like-admin-submit" value="<?php _e('Save Changes', 'wordpress'); ?>"/>
  1098. </p>
  1099. <?php wp_nonce_field( 'bp-like-admin' ) ?>
  1100. </form>
  1101. </div>
  1102. <?php
  1103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement