From e578d2813e5142329fbb1e9404c8873e76236d7d Mon Sep 17 00:00:00 2001 From: Giri Date: Wed, 24 Jun 2015 12:33:59 +0530 Subject: [PATCH 1/2] activity support added --- bp-compliments-core.php | 1 + change_log.txt | 1 + includes/bp-compliments-activity.php | 142 ++++++++++++++++++ includes/bp-compliments-classes.php | 11 +- includes/bp-compliments-functions.php | 13 +- includes/bp-compliments-notifications.php | 17 ++- includes/bp-compliments-templatetags.php | 5 +- .../buddypress/members/single/compliments.php | 10 +- 8 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 includes/bp-compliments-activity.php diff --git a/bp-compliments-core.php b/bp-compliments-core.php index 312af53..9c837fc 100644 --- a/bp-compliments-core.php +++ b/bp-compliments-core.php @@ -41,6 +41,7 @@ public function includes( $includes = array() ) { require( $this->path . '/bp-compliments-templatetags.php' ); require( $this->path . '/bp-compliments-actions.php' ); require( $this->path . '/bp-compliments-notifications.php' ); + require( $this->path . '/bp-compliments-activity.php' ); require( $this->path . '/bp-compliments-forms.php' ); } diff --git a/change_log.txt b/change_log.txt index 17d43cb..21b46d7 100644 --- a/change_log.txt +++ b/change_log.txt @@ -2,5 +2,6 @@ v0.0.2 BP compliments now supports notification component - ADDED Send compliment modal form z-index bug - FIXED Compliment Icon upload form uses latest media uploader - CHANGED +BP compliments now supports activity component - ADDED diff --git a/includes/bp-compliments-activity.php b/includes/bp-compliments-activity.php new file mode 100644 index 0000000..6fa112e --- /dev/null +++ b/includes/bp-compliments-activity.php @@ -0,0 +1,142 @@ + bp_loggedin_user_id(), + 'action' => '', + 'content' => '', + 'primary_link' => '', + 'component' => buddypress()->compliments->id, + 'type' => false, + 'item_id' => false, + 'secondary_item_id' => false, + 'recorded_time' => bp_core_current_time(), + 'hide_sitewide' => false + ) ); + + return bp_activity_add( $r ); +} + +function compliments_record_sent_activity( BP_Compliments $compliment ) { + if ( ! bp_is_active( 'activity' ) ) { + return; + } + + // Record in activity streams for the sender + compliments_record_activity( array( + 'user_id' => $compliment->sender_id, + 'type' => 'compliment_sent', + 'item_id' => $compliment->id, + 'secondary_item_id' => $compliment->receiver_id + ) ); + + // Record in activity streams for the receiver + compliments_record_activity( array( + 'user_id' => $compliment->receiver_id, + 'type' => 'compliment_received', + 'item_id' => $compliment->id, + 'secondary_item_id' => $compliment->sender_id + ) ); +} +add_action( 'bp_compliments_start_compliment', 'compliments_record_sent_activity' ); + + +/** + * Register the activity actions. + */ +function compliments_register_activity_actions() { + + if ( !bp_is_active( 'activity' ) ) { + return false; + } + + $bp = buddypress(); + + bp_activity_set_action( + $bp->compliments->id, + 'compliment_received', + __( 'Compliment Received', BP_COMP_TEXTDOMAIN ), + 'compliments_format_activity_action_compliment_received', + __( 'Compliments', BP_COMP_TEXTDOMAIN ), + array( 'activity' ) + ); + + bp_activity_set_action( + $bp->compliments->id, + 'compliment_sent', + __( 'Compliment Sent', BP_COMP_TEXTDOMAIN ), + 'compliments_format_activity_action_compliment_sent', + __( 'Compliments', BP_COMP_TEXTDOMAIN ), + array( 'activity' ) + ); + + do_action( 'compliments_register_activity_actions' ); +} +add_action( 'bp_register_activity_actions', 'compliments_register_activity_actions' ); + +/** + * Format 'compliment_received' activity actions. + * + * @since 0.0.2 + * + * @param object $activity Activity data. + * @return string $action Formatted activity action. + */ +function compliments_format_activity_action_compliment_received( $action, $activity ) { + global $bp; + $sender_link = bp_core_get_userlink( $activity->user_id ); + $receiver_link = bp_core_get_userlink( $activity->secondary_item_id ); + $receiver_url = bp_core_get_userlink( $activity->secondary_item_id, false, true ); + $compliment_url = $receiver_url . $bp->compliments->id . '/?c_id='.$activity->item_id; + $compliment_link = ''.__("compliment").''; + + $action = sprintf( __( '%1$s has received a %2$s from %3$s', BP_COMP_TEXTDOMAIN ), $sender_link, $compliment_link, $receiver_link ); + + + /** + * Filters the 'compliment_received' activity action format. + * + * @since 0.0.2 + * + * @param string $action String text for the 'compliment_received' action. + * @param object $activity Activity data. + */ + return apply_filters( 'compliments_format_activity_action_compliment_received', $action, $activity ); +} + +/** + * Format 'compliment_sent' activity actions. + * + * @since 0.0.2 + * + * @param string $action Static activity action. + * @param object $activity Activity data. + * @return string $action Formatted activity action. + */ +function compliments_format_activity_action_compliment_sent( $action, $activity ) { + global $bp; + $sender_link = bp_core_get_userlink( $activity->user_id ); + $receiver_link = bp_core_get_userlink( $activity->secondary_item_id ); + $receiver_url = bp_core_get_userlink( $activity->secondary_item_id, false, true ); + $compliment_url = $receiver_url . $bp->compliments->id . '/?c_id='.$activity->item_id; + $compliment_link = ''.__("compliment").''; + + $action = sprintf( __( '%1$s has sent a %2$s to %3$s', BP_COMP_TEXTDOMAIN ), $sender_link, $compliment_link, $receiver_link ); + + /** + * Filters the 'compliment_sent' activity action format. + * + * @since 0.0.2 + * + * @param string $action String text for the 'compliment_sent' action. + * @param object $activity Activity data. + */ + return apply_filters( 'compliments_format_activity_action_compliment_sent', $action, $activity ); +} diff --git a/includes/bp-compliments-classes.php b/includes/bp-compliments-classes.php index cd540a2..c86d89d 100644 --- a/includes/bp-compliments-classes.php +++ b/includes/bp-compliments-classes.php @@ -97,16 +97,21 @@ public function delete() { } /** - * Get the sender IDs for a given user. + * Get the compliments for a given user. * @param $user_id * @param $offset * @param $limit + * @param bool|int $c_id * @return mixed */ - public static function get_compliments( $user_id, $offset, $limit ) { + public static function get_compliments( $user_id, $offset, $limit, $c_id = false ) { global $bp, $wpdb; $table_name = BP_COMPLIMENTS_TABLE; - return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE receiver_id = %d ORDER BY created_at DESC LIMIT %d, %d", $user_id, $offset, $limit ) ); + if ($c_id) { + return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE receiver_id = %d AND id = %d ORDER BY created_at DESC LIMIT %d, %d", $user_id, $c_id, $offset, $limit ) ); + } else { + return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE receiver_id = %d ORDER BY created_at DESC LIMIT %d, %d", $user_id, $offset, $limit ) ); + } } /** diff --git a/includes/bp-compliments-functions.php b/includes/bp-compliments-functions.php index 11df2a4..9145b40 100644 --- a/includes/bp-compliments-functions.php +++ b/includes/bp-compliments-functions.php @@ -59,4 +59,15 @@ function bp_compliments_total_counts( $args = '' ) { } return apply_filters( 'bp_compliments_total_counts', $count, $r['user_id'] ); -} \ No newline at end of file +} + +function bp_compliments_remove_data( $user_id ) { + do_action( 'bp_compliments_before_remove_data', $user_id ); + + BP_Compliments::delete_all_for_user( $user_id ); + + do_action( 'bp_compliments_after_remove_data', $user_id ); +} +add_action( 'wpmu_delete_user', 'bp_compliments_remove_data' ); +add_action( 'delete_user', 'bp_compliments_remove_data' ); +add_action( 'make_spam_user', 'bp_compliments_remove_data' ); \ No newline at end of file diff --git a/includes/bp-compliments-notifications.php b/includes/bp-compliments-notifications.php index 0d10994..92bf473 100644 --- a/includes/bp-compliments-notifications.php +++ b/includes/bp-compliments-notifications.php @@ -197,4 +197,19 @@ function bp_compliments_notifications_mark_compliments_as_read() { // Redirect bp_core_redirect( bp_displayed_user_domain() . BP_COMPLIMENTS_SLUG . '/' ); } -add_action( 'bp_actions', 'bp_compliments_notifications_mark_compliments_as_read' ); \ No newline at end of file +add_action( 'bp_actions', 'bp_compliments_notifications_mark_compliments_as_read' ); + + +function bp_compliments_remove_notifications_for_user( $user_id = 0 ) { + // BP 1.9+ + if ( bp_is_active( 'notifications' ) ) { + bp_notifications_delete_all_notifications_by_type( $user_id, buddypress()->compliments->id, 'new_compliment' ); + + // BP < 1.9 - delete notifications the old way + } elseif ( ! class_exists( 'BP_Core_Login_Widget' ) ) { + global $bp; + + bp_core_delete_notifications_from_user( $user_id, $bp->compliments->id, 'new_compliment' ); + } +} +add_action( 'bp_compliments_after_remove_data', 'bp_compliments_remove_notifications_for_user' ); \ No newline at end of file diff --git a/includes/bp-compliments-templatetags.php b/includes/bp-compliments-templatetags.php index 25db982..96edfac 100644 --- a/includes/bp-compliments-templatetags.php +++ b/includes/bp-compliments-templatetags.php @@ -98,8 +98,9 @@ function bp_compliments_get_compliments( $args = '' ) { $r = wp_parse_args( $args, array( 'user_id' => bp_displayed_user_id(), 'offset' => 0, - 'limit' => 100 + 'limit' => 100, + 'c_id' => false ) ); - return apply_filters( 'bp_compliments_get_compliments', BP_Compliments::get_compliments( $r['user_id'], $r['offset'], $r['limit'] ) ); + return apply_filters( 'bp_compliments_get_compliments', BP_Compliments::get_compliments( $r['user_id'], $r['offset'], $r['limit'], $r['c_id'] ) ); } \ No newline at end of file diff --git a/includes/templates/buddypress/members/single/compliments.php b/includes/templates/buddypress/members/single/compliments.php index 5bc78b3..a8ff6bb 100644 --- a/includes/templates/buddypress/members/single/compliments.php +++ b/includes/templates/buddypress/members/single/compliments.php @@ -3,6 +3,7 @@
bp_displayed_user_id() ); @@ -15,6 +16,13 @@ 'offset' => $offset, 'limit' => $items_per_page ); + if (isset($_GET['c_id'])) { + $c_id = (int) strip_tags(esc_sql($_GET['c_id'])); + if ($c_id) { + $args['c_id'] = $c_id; + } + + } $compliments = bp_compliments_get_compliments($args); $start = $offset ? $offset : 1; $end = $offset + $items_per_page; @@ -67,7 +75,7 @@ class='preview-upload'/>
$items_per_page) { ?> + if (($total > $items_per_page) && !$c_id) { ?>