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..7d08480 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 +Translation support - ADDED +Compliments can be deleted by the receiver - ADDED \ No newline at end of file diff --git a/includes/bp-compliments-actions.php b/includes/bp-compliments-actions.php index c385b17..95e41ab 100644 --- a/includes/bp-compliments-actions.php +++ b/includes/bp-compliments-actions.php @@ -38,4 +38,35 @@ function handle_compliments_form_data() { bp_core_redirect( $redirect ); } } -add_action( 'bp_actions', 'handle_compliments_form_data', 99 ); \ No newline at end of file +add_action( 'bp_actions', 'handle_compliments_form_data', 99 ); + +function delete_single_complement() { + if (!bp_is_user()) { + return; + } + + if ( bp_displayed_user_id() != bp_loggedin_user_id() ) { + return; + } + + if (!isset($_GET['c_id']) OR !isset($_GET['action']) ) { + return; + } + + $c_id = (int) strip_tags(esc_sql($_GET['c_id'])); + + if (!$c_id) { + return; + } + + do_action( 'bp_compliments_before_remove_compliment', $c_id ); + + BP_Compliments::delete( $c_id ); + + do_action( 'bp_compliments_after_remove_compliment', $c_id ); + + $redirect = bp_displayed_user_domain().'compliments/'; + bp_core_redirect( $redirect ); +} +add_action( 'bp_actions', 'delete_single_complement'); + diff --git a/includes/bp-compliments-activity.php b/includes/bp-compliments-activity.php new file mode 100644 index 0000000..5c047fa --- /dev/null +++ b/includes/bp-compliments-activity.php @@ -0,0 +1,172 @@ + 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; + $receiver_link = bp_core_get_userlink( $activity->user_id ); + $sender_link = bp_core_get_userlink( $activity->secondary_item_id ); + $receiver_url = bp_core_get_userlink( $activity->user_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 ), $receiver_link, $compliment_link, $sender_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 ); +} + + +function compliments_delete_activity( $c_id ) { + if ( ! bp_is_active( 'activity' ) ) { + return; + } + + bp_activity_delete( array( + 'component' => buddypress()->compliments->id, + 'item_id' => $c_id + ) ); +} +add_action('bp_compliments_after_remove_compliment', 'compliments_delete_activity'); + +function compliments_delete_activity_for_user( $user_id ) { + if ( ! bp_is_active( 'activity' ) ) { + return; + } + + bp_activity_delete( array( + 'component' => buddypress()->compliments->id, + 'user_id' => $user_id + ) ); + + bp_activity_delete( array( + 'component' => buddypress()->compliments->id, + 'secondary_item_id' => $user_id + ) ); +} +add_action('bp_compliments_after_remove_data', 'compliments_delete_activity_for_user'); \ No newline at end of file diff --git a/includes/bp-compliments-classes.php b/includes/bp-compliments-classes.php index cd540a2..271d4e2 100644 --- a/includes/bp-compliments-classes.php +++ b/includes/bp-compliments-classes.php @@ -89,24 +89,31 @@ public function save() { /** * Deletes a compliment from the database. + * @param $c_id + * @return */ - public function delete() { + public static function delete($c_id) { global $wpdb, $bp; $table_name = BP_COMPLIMENTS_TABLE; - return $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE id = %d", $this->id ) ); + return $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE id = %d", $c_id ) ); } /** - * 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..2062b2e 100644 --- a/includes/bp-compliments-functions.php +++ b/includes/bp-compliments-functions.php @@ -1,6 +1,8 @@ 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..3cb78d0 100644 --- a/includes/templates/buddypress/members/single/compliments.php +++ b/includes/templates/buddypress/members/single/compliments.php @@ -3,6 +3,7 @@