Advertisement
Guest User

Buddypress like plugin notification hack

a guest
Sep 18th, 2013
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.20 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.     add_action( 'network_admin_menu', 'bp_like_check_installed' );
  259.    
  260. /**
  261.  * for notifications
  262.  */
  263.  
  264. function bp_like_setup_globals() {
  265.     global $bp, $current_blog;
  266.     $bp->bp_like=new stdClass();
  267.     $bp->bp_like->id = 'bp-like';
  268.     $bp->bp_like->slug = 'bp_like';
  269.     $bp->bp_like->notification_callback = 'bp_like_format_notifications';
  270.     $bp->active_components[$bp->bp_like->slug] = $bp->bp_like->id;
  271.     do_action( 'bp_like_setup_globals' );
  272. }
  273.     add_action( 'bp_setup_globals', 'bp_like_setup_globals' );
  274.  
  275.  
  276. function bp_like_format_notifications( $action, $item_id, $secondary_item_id, $total_items,$format='string') {
  277.     global $bp;
  278.     $glue='';
  279.     $user_names=array();
  280.     $activity = new BP_Activity_Activity( $item_id );
  281.     $link=like_notifier_activity_get_permalink( $item_id );
  282.  
  283.     if($activity->user_id==$bp->loggedin_user->id){
  284.         $text=__("your");
  285.         $also="";
  286.     }
  287.     else{
  288.         $text=sprintf(__("%s's"),  bp_core_get_user_displayname ($activity->user_id));//somone's
  289.         $also=" also";
  290.     }
  291.  
  292.     $users = array_keys( bp_activity_get_meta( $item_id, 'liked_count' ) );
  293.     $total_user= $count=count($users);
  294.    
  295.     if ($count==0){
  296.     bp_core_delete_notifications_by_item_id(  $bp->loggedin_user->id, $activity->id, $bp->bp_like->slug,  'new_bp_like_'.$activity->id);
  297.     }
  298.        
  299.     if($count>2){
  300.         $users=array_slice($users, $count-2);
  301.         $count=$count-2;
  302.         $glue=", ";
  303.     }
  304.  
  305.     else if($total_user==2)
  306.         $glue=" and ";
  307.  
  308.     foreach((array)$users as $id)
  309.         $user_names[]=bp_core_get_user_displayname ($id);
  310.                
  311.     if(!empty($user_names))
  312.         $commenting_users=join ($glue, $user_names);
  313.     else
  314.         $commenting_users="Somebody remove it after";
  315.                            
  316.     if($total_user>2)
  317.         $text=$commenting_users." and ".$count." others".$also." liked on ".$text. " post";
  318.     else
  319.         $text=$commenting_users.$also ." liked on ".$text. " post";
  320.        
  321.        
  322.    
  323.     return array('link'=>$link,
  324.         'text'=>$text);
  325.  
  326. }
  327.  
  328. function like_notifier_activity_get_permalink( $activity_id, $activity_obj = false ) {
  329.     global $bp;
  330.  
  331.     if ( !$activity_obj )
  332.         $activity_obj = new BP_Activity_Activity( $activity_id );
  333.                    
  334.             $link = bp_get_activity_directory_permalink() . 'p/' . $activity_obj->id . '/';
  335.    
  336.  
  337.     return apply_filters( 'like_notifier_activity_get_permalink', $link );
  338. }
  339.  
  340. function like_notifier_remove_notification($activity,$has_access){
  341.     global $bp;
  342.     if($has_access)
  343.         bp_core_delete_notifications_by_item_id(  $bp->loggedin_user->id, $activity->id, $bp->bp_like->slug,  'new_bp_like_'.$activity->id);
  344.    
  345. }
  346. add_action("bp_activity_screen_single_activity_permalink","like_notifier_remove_notification",10,2);
  347.  
  348.  
  349. function bp_like_get_settings( $option = false ) {
  350.    
  351.     $settings = get_site_option( 'bp_like_settings' );
  352.    
  353.     if (!$option)
  354.         return $settings;
  355.        
  356.     else
  357.         return $settings[$option];
  358. }
  359.  
  360. /**
  361.  * bp_like_get_text()
  362.  *
  363.  * Returns a custom text string from the database
  364.  *
  365.  */
  366. function bp_like_get_text( $text = false, $type = 'custom' ) {
  367.    
  368.     $settings = get_site_option( 'bp_like_settings' );
  369.     $text_strings = $settings['text_strings'];
  370.     $string = $text_strings[$text];
  371.        
  372.     return $string[$type];
  373. }
  374.  
  375. /**
  376.  * bp_like_process_ajax()
  377.  *
  378.  * Runs the relevant function depending on what Ajax call has been made.
  379.  *
  380.  */
  381. function bp_like_process_ajax() {
  382.     global $bp;
  383.  
  384.     $id = preg_replace( "/\D/", "", $_POST['id'] );
  385.    
  386.     if ( $_POST['type'] == 'like' )
  387.         bp_like_add_user_like( $id, 'activity' );
  388.    
  389.     if ( $_POST['type'] == 'unlike' )
  390.         bp_like_remove_user_like( $id, 'activity' );
  391.  
  392.     if ( $_POST['type'] == 'view-likes' )
  393.         bp_like_get_likes( $id, 'activity' );
  394.  
  395.     if ( $_POST['type'] == 'like_blogpost' )
  396.         bp_like_add_user_like( $id, 'blogpost' );
  397.  
  398.     if ( $_POST['type'] == 'unlike_blogpost' )
  399.         bp_like_remove_user_like( $id, 'blogpost' );
  400.  
  401.     die();
  402. }
  403. add_action( 'wp_ajax_activity_like', 'bp_like_process_ajax' );
  404.  
  405. /**
  406.  * bp_like_is_liked()
  407.  *
  408.  * Checks to see whether the user has liked a given item.
  409.  *
  410.  */
  411. function bp_like_is_liked( $item_id = '', $type = '', $user_id = '' ) {
  412.     global $bp;
  413.    
  414.     if ( !$type )
  415.         return false;
  416.    
  417.     if ( !$item_id )
  418.         return false;
  419.    
  420.     if ( !$user_id )
  421.         $user_id = $bp->loggedin_user->id;
  422.    
  423.     if ( $type == 'activity' )
  424.         $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  425.    
  426.     if ( $type == 'blogpost' )
  427.         $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  428.    
  429.     if ( !$user_likes ){
  430.         return false;
  431.     } elseif ( !array_key_exists( $item_id, $user_likes ) ) {
  432.         return false;
  433.     } else {
  434.         return true;
  435.     };
  436. }
  437.  
  438. /**
  439.  * bp_like_add_user_like()
  440.  *
  441.  * Registers that the user likes a given item.
  442.  *
  443.  */
  444. function bp_like_add_user_like( $item_id = '', $type = 'activity' ) {
  445.     global $bp;
  446.    
  447.     if ( !$item_id )
  448.         return false;
  449.  
  450.     if ( !$user_id )
  451.         $user_id = $bp->loggedin_user->id;
  452.    
  453.     if ( $user_id == 0 ) {
  454.         echo bp_like_get_text( 'must_be_logged_in' );
  455.         return false;
  456.     }
  457.    
  458.     if ( $type == 'activity' ) :
  459.    
  460.         /* Add to the users liked activities. */
  461.         $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  462.         $user_likes[$item_id] = 'activity_liked';
  463.         update_user_meta( $user_id, 'bp_liked_activities', $user_likes );
  464.    
  465.         /* Add to the total likes for this activity. */
  466.         $users_who_like = bp_activity_get_meta( $item_id, 'liked_count', true );
  467.         $users_who_like[$user_id] = 'user_likes';
  468.         bp_activity_update_meta( $item_id, 'liked_count', $users_who_like );
  469.        
  470.         //var_dump($users_who_like);
  471.    
  472.         $liked_count = count( $users_who_like );
  473.        
  474.         /* Publish to the activity stream if we're allowed to. */
  475.         if ( bp_like_get_settings( 'post_to_activity_stream' ) == 1 ) {
  476.        
  477.             $activity = bp_activity_get_specific( array( 'activity_ids' => $item_id, 'component' => 'bp-like' ) );
  478.             $author_id = $activity['activities'][0]->user_id;
  479.    
  480.             if ($user_id == $author_id)
  481.                 $action = bp_like_get_text( 'record_activity_likes_own' );
  482.             elseif ($user_id == 0)
  483.                 $action = bp_like_get_text( 'record_activity_likes_an' );
  484.             else
  485.                 $action = bp_like_get_text( 'record_activity_likes_users' );
  486.  
  487.             $liker = bp_core_get_userlink( $user_id );
  488.             $author = bp_core_get_userlink( $author_id );
  489.             $activity_url = bp_activity_get_permalink( $item_id );
  490.            
  491.             /* Grab the content and make it into an excerpt of 140 chars if we're allowed */
  492.             if ( bp_like_get_settings( 'show_excerpt' ) == 1 ) {
  493.                 $content = $activity['activities'][0]->content;
  494.                 if ( strlen( $content ) > bp_like_get_settings( 'excerpt_length' ) ) {
  495.                     $content = substr( $content, 0, bp_like_get_settings( 'excerpt_length' ) );
  496.                     $content = $content.'...';
  497.                 }
  498.             };
  499.        
  500.  
  501.             /* Filter out the placeholders */
  502.             $action = str_replace( '%user%', $liker, $action );
  503.             $action = str_replace( '%permalink%', $activity_url, $action );
  504.             $action = str_replace( '%author%', $author, $action );
  505.  
  506.             $act_id = bp_activity_add(
  507.                 array(
  508.                     'action' => $action,
  509.                     'content' => $content,
  510.                     'primary_link' => $activity_url,
  511.                     'component' => 'bp-like',
  512.                     'type' => 'activity_liked'.$item_id,
  513.                     'user_id' => $user_id,
  514.                     'item_id' => $item_id
  515.                 )
  516.                
  517.             );
  518.            
  519.             $visibility ='followers';
  520.             bp_activity_update_meta( $act_id, 'activity-privacy', $visibility );
  521.            
  522.            
  523.         if ($bp->loggedin_user->id != $activity['activities'][0]->user_id){
  524.         bp_core_add_notification( $item_id, $activity['activities'][0]->user_id, $bp->bp_like->slug, 'new_bp_like_'.$item_id );
  525.         }
  526.            
  527.         };
  528.    
  529.     elseif ( $type == 'blogpost' ) :
  530.        
  531.  
  532.         $user_likes = get_user_meta( $user_id, 'bp_liked_blogposts', true);
  533.         $user_likes[$item_id] = 'blogpost_liked';
  534.         update_user_meta( $user_id, 'bp_liked_blogposts', $user_likes );
  535.  
  536.  
  537.         $users_who_like = get_post_meta( $item_id, 'liked_count', true );
  538.         $users_who_like[$user_id] = 'user_likes';
  539.         update_post_meta( $item_id, 'liked_count', $users_who_like );
  540.        
  541.         $liked_count = count( $users_who_like );
  542.        
  543.         if ( bp_like_get_settings( 'post_to_activity_stream' ) == 1 ) {
  544.             $post = get_post($item_id);
  545.             $author_id = $post->post_author;
  546.    
  547.             $liker = bp_core_get_userlink( $user_id );
  548.             $permalink = get_permalink( $item_id );
  549.             $title = $post->post_title;
  550.             $author = bp_core_get_userlink( $post->post_author );
  551.  
  552.             if ($user_id == $author_id)
  553.                 $action = bp_like_get_text( 'record_activity_likes_own_blogpost' );
  554.             elseif ($user_id == 0)
  555.                 $action = bp_like_get_text( 'record_activity_likes_a_blogpost' );
  556.             else
  557.                 $action = bp_like_get_text( 'record_activity_likes_users_blogpost' );
  558.    
  559.  
  560.             $action = str_replace( '%user%', $liker, $action );
  561.             $action = str_replace( '%permalink%', $permalink, $action );
  562.             $action = str_replace( '%title%', $title, $action );
  563.             $action = str_replace( '%author%', $author, $action );
  564.            
  565.  
  566.             if ( bp_like_get_settings( 'show_excerpt' ) == 1 ) {
  567.                 $content = $post->post_content;
  568.                 if ( strlen( $content ) > bp_like_get_settings( 'excerpt_length' ) ) {
  569.                     $content = substr( $content, 0, bp_like_get_settings( 'excerpt_length' ) );
  570.                     $content = $content.'...';
  571.                 }
  572.             };
  573.    
  574.             bp_activity_add(
  575.                 array(
  576.                     'action' => $action,
  577.                     'content' => $content,
  578.                     'component' => 'bp-like',
  579.                     'type' => 'blogpost_liked',
  580.                     'user_id' => $user_id,
  581.                     'item_id' => $item_id,
  582.                     'primary_link' => $permalink
  583.                 )
  584.             );
  585.             if ($bp->loggedin_user->id != $post->post_author){
  586.             bp_core_add_notification( $item_id, $post->post_author, $bp->bp_like->slug, 'new_bp_like_'.$item_id ); 
  587.             }
  588.            
  589.         };
  590.        
  591.     endif;
  592.    
  593.     if ($liked_count!==0){
  594.     echo '<span class="like_count">'.$liked_count.'</span><span class="like_img"><img class="like_drw" src="/image/drwmsg_unlike.png"></span>';
  595.     }else{
  596.     echo '<span class="like_count"></span><span class="like_img"><img class="like_drw" src="/image/drwmsg_unlike.png"></span>';
  597.     }
  598.    
  599.     /*$new_comment = array(
  600.         'content'     => 'i like this',
  601.         'activity_id' => $item_id
  602.     );
  603.    
  604.     $new_comment_id = bp_activity_new_comment($new_comment);*/
  605.  
  606. }
  607.  
  608. /**
  609.  * bp_like_remove_user_like()
  610.  *
  611.  * Registers that the user has unliked a given item.
  612.  *
  613.  */
  614. function bp_like_remove_user_like( $item_id = '', $type = 'activity') {
  615.     global $bp;
  616.    
  617.     if ( !$item_id )
  618.         return false;
  619.  
  620.     if ( !$user_id )
  621.         $user_id = $bp->loggedin_user->id;
  622.    
  623.     if ( $user_id == 0 ) {
  624.         echo bp_like_get_text( 'must_be_logged_in' );
  625.         return false;
  626.     }
  627.  
  628.     if ( $type == 'activity' ) :
  629.  
  630.         /* Remove this from the users liked activities. */
  631.         $user_likes = get_user_meta( $user_id, 'bp_liked_activities', true );
  632.         unset( $user_likes[$item_id] );
  633.         update_user_meta( $user_id, 'bp_liked_activities', $user_likes );
  634.  
  635.         /* Update the total number of users who have liked this activity. */
  636.         $users_who_like = bp_activity_get_meta( $item_id, 'liked_count', true );
  637.         unset( $users_who_like[$user_id] );
  638.        
  639.         /* If nobody likes the activity, delete the meta for it to save space, otherwise, update the meta */
  640.         if ( empty( $users_who_like ) )
  641.             bp_activity_delete_meta( $item_id, 'liked_count' );
  642.         else
  643.             bp_activity_update_meta( $item_id, 'liked_count', $users_who_like );
  644.    
  645.         $liked_count = count( $users_who_like );
  646.  
  647.         /* Remove the update on the users profile from when they liked the activity. */
  648.         $update_id = bp_activity_get_activity_id(
  649.             array(
  650.                 'item_id' => $item_id,
  651.                 'component' => 'bp-like',
  652.                 'type' => 'activity_liked'.$item_id,
  653.                 'user_id' => $user_id
  654.             )
  655.         );
  656.    
  657.         bp_activity_delete(
  658.             array(
  659.                 'id' => $update_id,
  660.                 'item_id' => $item_id,
  661.                 'component' => 'bp-like',
  662.                 'type' => 'activity_liked'.$item_id,
  663.                 'user_id' => $user_id
  664.             )
  665.         );
  666.        
  667.     elseif ( $type == 'blogpost' ) :
  668.        
  669.         /* Remove this from the users liked activities. */
  670.         $user_likes = get_user_meta( $user_id, 'bp_liked_blogposts', true );
  671.         unset( $user_likes[$item_id] );
  672.         update_user_meta( $user_id, 'bp_liked_blogposts', $user_likes );
  673.  
  674.         /* Update the total number of users who have liked this blog post. */
  675.         $users_who_like = get_post_meta( $item_id, 'liked_count', true );
  676.         unset( $users_who_like[$user_id] );
  677.        
  678.         /* If nobody likes the blog post, delete the meta for it to save space, otherwise, update the meta */
  679.         if ( empty( $users_who_like ) )
  680.             delete_post_meta( $item_id, 'liked_count' );
  681.         else
  682.             update_post_meta( $item_id, 'liked_count', $users_who_like );
  683.  
  684.         $liked_count = count( $users_who_like );
  685.  
  686.         /* Remove the update on the users profile from when they liked the activity. */
  687.         $update_id = bp_activity_get_activity_id(
  688.             array(
  689.                 'item_id' => $item_id,
  690.                 'component' => 'bp-like',
  691.                 'type' => 'blogpost_liked',
  692.                 'user_id' => $user_id
  693.             )
  694.         );
  695.    
  696.         bp_activity_delete(
  697.             array(
  698.                 'id' => $update_id,
  699.                 'item_id' => $item_id,
  700.                 'component' => 'bp-like',
  701.                 'type' => 'blogpost_liked',
  702.                 'user_id' => $user_id
  703.             )
  704.         );
  705.        
  706.     endif;
  707.  
  708.     //echo bp_like_get_text( 'like' );
  709.    
  710.     if ($liked_count!==0){
  711.     echo '<span class="like_count">'.$liked_count.'</span><span class="like_img"><img class="like_drw" src="/image/drwmsg_like.png"></span>';
  712.     }else{
  713.     echo '<span class="like_count"></span><span class="like_img"><img class="like_drw" src="/image/drwmsg_like.png"></span>';
  714.     }
  715.        
  716. }
  717.  
  718. /**
  719.  * bp_like_get_likes()
  720.  *
  721.  * Outputs a list of users who have liked a given item.
  722.  *
  723.  */
  724. function bp_like_get_likes( $item_id = '', $type = '', $user_id = '' ) {
  725.     global $bp;
  726.    
  727.     if ( !$type || !$item_id )
  728.         return false;
  729.    
  730.     if ( !$user_id )
  731.         $user_id = $bp->loggedin_user->id;
  732.  
  733.     if ( $type == 'activity' ) :
  734.        
  735.         /* Grab some core data we will need later on, specific to activities */
  736.         $users_who_like     = array_keys( bp_activity_get_meta( $item_id, 'liked_count' ) );
  737.         $liked_count        = count( bp_activity_get_meta( $item_id, 'liked_count' ) );
  738.        
  739.         /* Intercept any messages if nobody likes it, just incase the button was clicked when it shouldn't be */
  740.         if ( $liked_count == 0 ) :
  741.            
  742.             $output .= bp_like_get_text( 'get_likes_no_likes' );
  743.        
  744.         /* We should show information about all likers */
  745.         elseif ( bp_like_get_settings( 'likers_visibility' ) == 'show_all' ) :
  746.            
  747.             /* Settings say we should show their name. */
  748.             if ( bp_like_get_settings( 'name_or_avatar' ) == 'name' ) :
  749.                
  750.                 /* Current user likes it too, remove them from the liked count and output appropriate message */
  751.                 if ( bp_like_is_liked( $item_id, 'activity', $user_id ) ) :
  752.                    
  753.                     $liked_count = $liked_count-1;
  754.                    
  755.                     if ( $liked_count == 1 )
  756.                         $output .= bp_like_get_text( 'get_likes_you_and_singular' );
  757.  
  758.                     elseif ( $liked_count == 0 )
  759.                         $output .= bp_like_get_text('get_likes_only_liker');
  760.  
  761.                     else
  762.                         $output .= bp_like_get_text( 'get_likes_you_and_plural' );
  763.                
  764.                 else :
  765.                    
  766.                     if ( $liked_count == 1 )
  767.                         $output .= bp_like_get_text( 'get_likes_count_people_singular' );
  768.                    
  769.                     else
  770.                         $output .= bp_like_get_text( 'get_likes_count_people_plural' );
  771.                    
  772.                 endif;
  773.                    
  774.                 /* Now output the name of each person who has liked it (except the current user obviously) */
  775.                 foreach( $users_who_like as $id ) :
  776.                    
  777.                     if ( $id != $user_id )
  778.                         $output .= ' &middot <a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . bp_core_get_user_displayname( $id ) . '</a>';
  779.                    
  780.                 endforeach;
  781.            
  782.             /* Settings say we should show their avatar. */
  783.             elseif ( bp_like_get_settings( 'name_or_avatar' ) == 'avatar' ) :
  784.                
  785.                 /* Output the avatar of each person who has liked it. */
  786.                 foreach( $users_who_like as $id ) :
  787.                    
  788.                     $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> ';
  789.  
  790.                 endforeach;
  791.                
  792.             endif;
  793.        
  794.         /* We should show the information of friends, but only the number of non-friends. */
  795.         elseif ( bp_like_get_settings( 'likers_visibility' ) == 'friends_names_others_numbers' && bp_is_active( 'friends' ) ) :
  796.            
  797.             /* Grab some information about their friends. */
  798.             $users_friends = friends_get_friend_user_ids( $user_id );
  799.             if ( !empty( $users_friends ) )
  800.                 $friends_who_like = array_intersect( $users_who_like, $users_friends );
  801.            
  802.             /* Current user likes it, so reduce the liked count by 1, to get the number of other people who like it. */
  803.             if ( bp_like_is_liked( $item_id, 'activity', $user_id ) )
  804.                 $liked_count = $liked_count-1;
  805.            
  806.             /* Settings say we should show their names. */
  807.             if ( bp_like_get_settings( 'name_or_avatar' ) == 'name' ) :
  808.                    
  809.                     /* Current user likes it too, tell them. */
  810.                     if ( bp_like_is_liked( $item_id, 'activity', $user_id ) )
  811.                         $output .= 'You ';
  812.                
  813.                     /* Output the name of each friend who has liked it. */
  814.                     foreach( $users_who_like as $id ) :
  815.                    
  816.                         if ( in_array( $id, $friends_who_like ) ) {
  817.                             $output .= ' &middot <a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . bp_core_get_user_displayname( $id ) . '</a> ';
  818.                        
  819.                             $liked_count = $liked_count-1;
  820.                         }
  821.                    
  822.                     endforeach;
  823.                    
  824.                     /* If non-friends like it, say so. */
  825.                     if ( $liked_count == 1 )
  826.                         $output .= bp_like_get_text( 'get_likes_and_people_singular' );
  827.  
  828.                     elseif ( $liked_count > 1 )
  829.                         $output .= bp_like_get_text( 'get_likes_and_people_plural' );
  830.                    
  831.                     else
  832.                         $output .= bp_like_get_text( 'get_likes_like_this' );
  833.                
  834.             /* Settings say we should show their avatar. */
  835.             elseif ( bp_like_get_settings( 'name_or_avatar' ) == 'avatar' ) :
  836.                
  837.                 /* Output the avatar of each friend who has liked it, as well as the current users' if they have. */
  838.                 if ( !empty( $friends_who_like ) ) :
  839.            
  840.                     foreach( $users_who_like as $id ) :
  841.                    
  842.                         if ( $id == $user_id || in_array( $id, $friends_who_like ) ) {
  843.                             $user_info = get_userdata( $id );
  844.                             $output .= '<a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . get_avatar( $user_info->user_email, 30 ) . '</a> ';
  845.                         }
  846.                    
  847.                     endforeach;
  848.                
  849.                 endif;
  850.                
  851.             endif;
  852.        
  853.         elseif ( bp_like_get_settings( 'likers_visibility' ) == 'friends_names_others_numbers' && !bp_is_active( 'friends' ) ||bp_like_get_settings( 'likers_visibility' ) == 'just_numbers' ) :
  854.            
  855.                 /* Current user likes it too, remove them from the liked count and output appropriate message */
  856.                 if ( bp_like_is_liked( $item_id, 'activity', $user_id ) ) :
  857.                    
  858.                     $liked_count = $liked_count-1;
  859.                    
  860.                     if ( $liked_count == 1 )
  861.                         $output .= bp_like_get_text( 'get_likes_you_and_singular' );
  862.                    
  863.                     elseif ( $liked_count == 0 )
  864.                         $output .= bp_like_get_text('get_likes_only_liker');
  865.                    
  866.                     else
  867.                         $output .= bp_like_get_text( 'get_likes_you_and_plural' );
  868.                
  869.                 else :
  870.                    
  871.                     if ( $liked_count == 1 )
  872.                         $output .= bp_like_get_text( 'get_likes_count_people_singular' );
  873.                    
  874.                     else
  875.                         $output .= bp_like_get_text( 'get_likes_count_people_plural' );
  876.                    
  877.                 endif;
  878.        
  879.         endif;
  880.    
  881.     endif;
  882.    
  883.     /* Filter out the placeholder. */
  884.     $output = str_replace( '%count%', $liked_count, $output );
  885.    
  886.     echo $output;
  887.        
  888. }
  889.  
  890. /**
  891.  * bp_like_button()
  892.  *
  893.  * Outputs the 'Like/Unlike' and 'View likes/Hide likes' buttons.
  894.  *
  895.  */
  896. function bp_like_button( $id = '', $type = '' ) {
  897.    
  898.     $users_who_like = 0;
  899.     $liked_count = 0;
  900.    
  901.     /* Set the type if not already set, and check whether we are outputting the button on a blogpost or not. */
  902.     if ( !$type && !is_single() )
  903.         $type = 'activity';
  904.     elseif ( !$type && is_single() )
  905.         $type = 'blogpost';
  906.    
  907.     if ( $type == 'activity' ) :
  908.    
  909.         $activity = bp_activity_get_specific( array( 'activity_ids' => bp_get_activity_id() ) );
  910.         $activity_type = $activity['activities'][0]->type;
  911.        
  912.         if ( is_user_logged_in() && $activity_type !== 'activity_liked' && bp_get_activity_type() !== 'activity_comment' ) :
  913.        
  914.        
  915.             if ( bp_activity_get_meta( bp_get_activity_id(), 'liked_count', true )) {
  916.                 $users_who_like = array_keys( bp_activity_get_meta( bp_get_activity_id(), 'liked_count', true ) );
  917.                 $liked_count = count( $users_who_like );
  918.                 //$liked_count = $users_who_like;
  919.             }
  920.            
  921.             if ( !bp_like_is_liked( bp_get_activity_id(), 'activity' ) ) : ?>
  922.                 <a href="#" class="like" id="like-activity-<?php bp_activity_id(); ?>" title="<?php echo bp_like_get_text( 'like_this_item' ); ?>"><span class="like_count"><?php echo bp_like_get_text( '' ); if ( $liked_count ) echo ' ' . $liked_count . ''; ?></span><span class="like_img"><img class="like_drw" src="/image/drwmsg_like.png"></span></a>
  923.             <?php else : ?>
  924.                 <a href="#" class="unlike" id="unlike-activity-<?php bp_activity_id(); ?>" title="<?php echo bp_like_get_text( 'unlike_this_item' ); ?>"><span class="like_count"><?php echo bp_like_get_text( '' ); if ( $liked_count ) echo ' ' . $liked_count . ''; ?></span><span class="like_img"><img class="like_drw" src="/image/drwmsg_unlike.png"></span></a>
  925.             <?php endif;
  926.            
  927.             if ( $users_who_like ): ?>
  928.                 <a href="#" class="view-likes" id="view-likes-<?php bp_activity_id(); ?>"><?php echo bp_like_get_text( 'view_likes' ); ?></a>
  929.                 <p class="users-who-like" id="users-who-like-<?php bp_activity_id(); ?>"></p>
  930.             <?php
  931.             endif;
  932.         endif;
  933.    
  934.     elseif ( $type == 'blogpost' ) :
  935.    
  936.         $activity = bp_activity_get_specific( array( 'activity_ids' => bp_get_activity_id() ) );
  937.         $activity_type = $activity['activities'][0]->type;
  938.         global $post;
  939.        
  940.         if ( !$id && is_single() )
  941.             $id = $post->ID;
  942.        
  943.         if ( is_user_logged_in() && get_post_meta( $id, 'liked_count', true ) ) {
  944.             $liked_count = count( get_post_meta( $id, 'liked_count', true ) );
  945.         }
  946.        
  947.             if ( bp_activity_get_meta( bp_get_activity_id(), 'liked_count', true )) {
  948.                 $users_who_like = array_keys( bp_activity_get_meta( bp_get_activity_id(), 'liked_count', true ) );
  949.                 $liked_count = count( $users_who_like );
  950.                 //$liked_count = $users_who_like;
  951.             }
  952.        
  953.         if ( !bp_like_is_liked( bp_get_activity_id(), 'activity' ) ) : ?>
  954.        
  955.         <a href="#" class="like" id="like-activity-<?php bp_activity_id(); ?>" title="<?php echo bp_like_get_text( 'like_this_item' ); ?>"><span class="like_count"><?php echo bp_like_get_text( '' ); if ( $liked_count ) echo ' ' . $liked_count . ''; ?></span><span class="like_img"><img class="like_drw" src="/image/drwmsg_like.png"></span></a>
  956.        
  957.         <?php else : ?>
  958.        
  959.         <a href="#" class="unlike" id="unlike-activity-<?php bp_activity_id(); ?>" title="<?php echo bp_like_get_text( 'unlike_this_item' ); ?>"><span class="like_count"><?php echo bp_like_get_text( '' ); if ( $liked_count ) echo ' ' . $liked_count . ''; ?></span><span class="like_img"><img class="like_drw" src="/image/drwmsg_unlike.png"></span></a>
  960.         <?php endif;
  961.  
  962.     endif;
  963. };
  964. add_filter( 'bp_activity_entry_meta', 'bp_like_button' );
  965. add_action( 'bp_before_blog_single_post', 'bp_like_button' );
  966.  
  967. /**
  968.  * bp_like_activity_filter()
  969.  *
  970.  * Adds 'Show Activity Likes' to activity stream filters.
  971.  *
  972.  */
  973. function bp_like_activity_filter() {
  974.     echo '<option value="activity_liked">' . bp_like_get_text( 'show_activity_likes' ) . '</option>';
  975.     echo '<option value="blogpost_liked">Show Blog Post Likes</option>';
  976. }
  977. add_action( 'bp_activity_filter_options', 'bp_like_activity_filter' );
  978. add_action( 'bp_member_activity_filter_options', 'bp_like_activity_filter' );
  979. add_action( 'bp_group_activity_filter_options', 'bp_like_activity_filter' );
  980.  
  981. /**
  982.  * bp_like_list_scripts()
  983.  *
  984.  * Includes the Javascript required for Ajax etc.
  985.  *
  986.  */
  987. function bp_like_list_scripts() {
  988.     wp_enqueue_script(
  989.         "bp-like",
  990.         path_join( WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/_inc/js/bp-like.min.js" ),
  991.         array( 'jquery' )
  992.     );
  993. }
  994. add_action( 'wp_print_scripts', 'bp_like_list_scripts' );
  995.  
  996. /**
  997.  * bp_like_insert_head()
  998.  *
  999.  * Includes any CSS and/or Javascript needed in the <head>.
  1000.  *
  1001.  */
  1002. function bp_like_insert_head() {
  1003. ?>
  1004. <style type="text/css">
  1005.     .bp-like.activity_liked .activity-meta,
  1006.     .bp-like.blogpost_liked .activity-meta,
  1007.     .users-who-like,
  1008.     .mini a.view-likes,
  1009.     .mini a.hide-likes {
  1010.         display: none;
  1011.     }
  1012.    
  1013.     /* To match the default theme */
  1014.     #bp-default .users-who-like {
  1015.         margin: 10px 0 -10px 0;
  1016.         background: #F5F5F5;
  1017.         -moz-border-radius: 4px;
  1018.         -webkit-border-radius: 4px;
  1019.         border-radius: 4px;
  1020.         padding: 8px 8px 0px 12px;
  1021.         color: #8C8A8F;
  1022.     }
  1023.     #bp-default .users-who-like a {
  1024.         color: #777;
  1025.         padding: 0;
  1026.         background: none;
  1027.         border: none;
  1028.         text-decoration: underline;
  1029.         font-size: 12px;
  1030.         background: #ededed;
  1031.     }
  1032.     #bp-default .users-who-like a:hover { color: #222; }
  1033.     #bp-default .mini .users-who-like {
  1034.         width: 100%;
  1035.         position: absolute;
  1036.         top: 0;
  1037.         left: 0;
  1038.     }
  1039.     #bp-default .users-who-like img.avatar {
  1040.         float: none;
  1041.         border: none;
  1042.         width: 30px;
  1043.         height: 30px;
  1044.         padding: 0;
  1045.         margin: 0;
  1046.     }
  1047.     ul#activity-stream li.bp-like .activity-inner {
  1048.         border-left: 3px solid #ddd;
  1049.         color: #888;
  1050.         padding-left: 15px;
  1051.         font-style: italic;
  1052.     }
  1053.     #bp-default div.post div.author-box, div.comment-avatar-box {
  1054.     position: relative;
  1055.     }
  1056.     #bp-default div.like-box {
  1057.         background: #f0f0f0;
  1058.         width: 90px;
  1059.         position: absolute;
  1060.         bottom: -40px;
  1061.         left: 0;
  1062.         font-family: georgia, times, serif;
  1063.         font-style: italic;
  1064.         text-align: center;
  1065.         padding: 5px 0;
  1066.         -moz-border-radius: 3px;
  1067.         -webkit-border-radius: 3px;
  1068.         border-radius: 3px;
  1069.     }
  1070. </style>
  1071. <script type="text/javascript">
  1072. /* <![CDATA[ */
  1073.     var bp_like_terms_like = '<?php echo bp_like_get_text( 'like' ); ?>';
  1074.     var bp_like_terms_like_message = '<?php echo bp_like_get_text( 'like_this_item' ); ?>';
  1075.     var bp_like_terms_unlike_message = '<?php echo bp_like_get_text( 'unlike_this_item' ); ?>';
  1076.     var bp_like_terms_view_likes = '<?php echo bp_like_get_text( 'view_likes' ); ?>';
  1077.     var bp_like_terms_hide_likes = '<?php echo bp_like_get_text( 'hide_likes' ); ?>';
  1078.     var bp_like_terms_unlike_1 = '<?php echo bp_like_get_text( 'unlike' ); ?> (1)';
  1079. /* ]]> */
  1080. </script>
  1081. <?php  
  1082. }
  1083. add_action( 'wp_head', 'bp_like_insert_head' );
  1084.  
  1085. /**
  1086.  * bp_like_add_admin_page_menu()
  1087.  *
  1088.  * Adds "BuddyPress Like" to the main BuddyPress admin menu.
  1089.  *
  1090.  */
  1091. function bp_like_add_admin_page_menu() {
  1092.     add_submenu_page(
  1093.         'bp-general-settings',
  1094.         'BuddyPress Like',
  1095.         'BuddyPress Like',
  1096.         'manage_options',
  1097.         'bp-like-settings',
  1098.         'bp_like_admin_page'
  1099.     );
  1100. }
  1101. add_action( 'admin_menu', 'bp_like_add_admin_page_menu' );
  1102.  
  1103. /**
  1104.  * bp_like_admin_page_verify_nonce()
  1105.  *
  1106.  * When the settings form is submitted, verifies the nonce to ensure security.
  1107.  *
  1108.  */
  1109. function bp_like_admin_page_verify_nonce() {
  1110.     if( isset( $_POST['_wpnonce'] ) && isset( $_POST['bp_like_updated'] ) ) {
  1111.         $nonce = $_REQUEST['_wpnonce'];
  1112.         if ( !wp_verify_nonce( $nonce, 'bp-like-admin' ) )
  1113.             wp_die( __('You do not have permission to do that.') );
  1114.     }
  1115. }
  1116. add_action( 'init', 'bp_like_admin_page_verify_nonce' );
  1117.  
  1118. /**
  1119.  * bp_like_admin_page()
  1120.  *
  1121.  * Outputs the admin settings page.
  1122.  *
  1123.  */
  1124. function bp_like_admin_page() {
  1125.     global $current_user;
  1126.  
  1127.     wp_get_current_user();
  1128.  
  1129.     /* Update our options if the form has been submitted */
  1130.     if( isset( $_POST['_wpnonce'] ) && isset( $_POST['bp_like_updated'] ) ) {
  1131.        
  1132.         /* Add each text string to the $strings_to_save array */
  1133.         foreach ( $_POST as $key => $value ) {
  1134.             if ( preg_match( "/text_string_/i", $key )) {
  1135.                 $default = bp_like_get_text( str_replace( 'bp_like_admin_text_string_', '', $key), 'default' );
  1136.                 $strings_to_save[str_replace( 'bp_like_admin_text_string_', '', $key )] = array('default' => $default, 'custom' => stripslashes( $value ));
  1137.             }
  1138.         }
  1139.        
  1140.         /* Now actually save the data to the options table */
  1141.         update_site_option(
  1142.             'bp_like_settings',
  1143.             array(
  1144.                 'likers_visibility' => $_POST['bp_like_admin_likers_visibility'],
  1145.                 'post_to_activity_stream' => $_POST['bp_like_admin_post_to_activity_stream'],
  1146.                 'show_excerpt' => $_POST['bp_like_admin_show_excerpt'],
  1147.                 'excerpt_length' => $_POST['bp_like_admin_excerpt_length'],
  1148.                 'text_strings' => $strings_to_save,
  1149.                 'translate_nag' => bp_like_get_settings( 'translate_nag' ),
  1150.                 'name_or_avatar' => $_POST['name_or_avatar']
  1151.             )
  1152.         );
  1153.        
  1154.         /* Let the user know everything's cool */
  1155.         echo '<div class="updated"><p><strong>';
  1156.         _e('Settings saved.', 'wordpress');
  1157.         echo '</strong></p></div>';
  1158.     }
  1159.    
  1160.     $text_strings = bp_like_get_settings( 'text_strings' );
  1161.  
  1162. ?>
  1163. <style type="text/css">
  1164. #icon-bp-like-settings { background: url('<?php echo plugins_url('/_inc/img/bp-like-icon32.png', __FILE__); ?>') no-repeat top left; }
  1165. table input { width: 100%; }
  1166. table label { display: block; }
  1167. </style>
  1168. <script type="text/javascript">
  1169. jQuery(document).ready( function() {
  1170.     jQuery('select.name-or-avatar').change(function(){
  1171.         var value = jQuery(this).val();
  1172.         jQuery('select.name-or-avatar').val(value);
  1173.     });
  1174. });
  1175. </script>
  1176.  
  1177. <div class="wrap">
  1178.   <div id="icon-bp-like-settings" class="icon32"><br /></div>
  1179.   <h2><?php _e('BuddyPress Like Settings', 'buddypress-like'); ?></h2>
  1180.   <form action="" method="post" id="bp-like-admin-form">
  1181.     <input type="hidden" name="bp_like_updated" value="updated">
  1182.    
  1183.     <h3><?php _e('General Settings', 'buddypress-like'); ?></h3>
  1184.     <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>
  1185.     <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>
  1186.    
  1187.     <h3><?php _e("'View Likes' Visibility", "buddypress-like"); ?></h3>
  1188.     <p><?php _e("Choose how much information about the 'likers' of a particular item is shown;", "buddypress-like"); ?></p>
  1189.     <p style="line-height: 200%;">
  1190.       <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 />
  1191.       <?php if ( bp_is_active( 'friends' ) ) { ?>
  1192.       <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 />
  1193.       <?php }; ?>
  1194.       <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'); ?>
  1195.     </p>
  1196.     <h3><?php _e('Custom Messages', 'buddypress-like'); ?></h3>
  1197.     <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>
  1198.    
  1199.     <table class="widefat fixed" cellspacing="0">
  1200.       <thead>
  1201.         <tr>
  1202.           <th scope="col" id="default" class="column-name" style="width: 43%;"><?php _e('Default', 'buddypress-like'); ?></th>
  1203.           <th scope="col" id="custom" class="column-name" style=""><?php _e('Custom', 'buddypress-like'); ?></th>
  1204.         </tr>
  1205.       </thead>
  1206.       <tfoot>
  1207.         <tr>
  1208.           <th colspan="2" id="default" class="column-name"></th>
  1209.         </tr>
  1210.       </tfoot>
  1211.  
  1212.       <?php foreach ( $text_strings as $key => $string ) : ?>
  1213.       <tr valign="top">
  1214.           <th scope="row" style="width:400px;"><label for="bp_like_admin_text_string_<?php echo $key; ?>"><?php echo htmlspecialchars( $string['default'] ); ?></label></th>
  1215.           <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>
  1216.         </tr>
  1217.       <?php endforeach; ?>
  1218.       </tbody>
  1219.     </table>
  1220.    
  1221.     <p class="submit">
  1222.       <input class="button-primary" type="submit" name="bp-like-admin-submit" id="bp-like-admin-submit" value="<?php _e('Save Changes', 'wordpress'); ?>"/>
  1223.     </p>
  1224.     <?php wp_nonce_field( 'bp-like-admin' ) ?>
  1225.   </form>
  1226. </div>
  1227. <?php
  1228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement