Skip to content

Commit

Permalink
Merge pull request #1 from mistergiri/master
Browse files Browse the repository at this point in the history
Translation support, activity support, receiver can delete their compliments
  • Loading branch information
NomadDevs authored and NomadDevs committed Jun 24, 2015
2 parents 6537eaf + ce8c399 commit a6a888b
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 14 deletions.
1 change: 1 addition & 0 deletions bp-compliments-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

Expand Down
5 changes: 3 additions & 2 deletions change_log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 32 additions & 1 deletion includes/bp-compliments-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,35 @@ function handle_compliments_form_data() {
bp_core_redirect( $redirect );
}
}
add_action( 'bp_actions', 'handle_compliments_form_data', 99 );
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');

172 changes: 172 additions & 0 deletions includes/bp-compliments-activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;

function compliments_record_activity( $args = '' ) {

if ( ! bp_is_active( 'activity' ) ) {
return false;
}

$r = wp_parse_args( $args, array(
'user_id' => 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 = '<a href="'.$compliment_url.'">'.__("compliment").'</a>';

$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 = '<a href="'.$compliment_url.'">'.__("compliment").'</a>';

$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');
17 changes: 12 additions & 5 deletions includes/bp-compliments-classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}

/**
Expand Down
15 changes: 14 additions & 1 deletion includes/bp-compliments-functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
/**
* Start compliment.
* @param string $args
* @return bool
*/
function bp_compliments_start_compliment( $args = '' ) {
global $bp;
Expand Down Expand Up @@ -59,4 +61,15 @@ function bp_compliments_total_counts( $args = '' ) {
}

return apply_filters( 'bp_compliments_total_counts', $count, $r['user_id'] );
}
}

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' );
17 changes: 16 additions & 1 deletion includes/bp-compliments-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
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' );
5 changes: 3 additions & 2 deletions includes/bp-compliments-templatetags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) );
}
20 changes: 18 additions & 2 deletions includes/templates/buddypress/members/single/compliments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<div class="bp-compliments-wrap">
<?php
$c_id = false;
$count_args = array(
'user_id' => bp_displayed_user_id()
);
Expand All @@ -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;
Expand All @@ -40,6 +48,14 @@ class='preview-upload'/>
<em>
<?php echo date_i18n(get_option('date_format'), strtotime($comp->created_at)); ?>
</em>
<?php
global $bp;
if (is_user_logged_in() && ($bp->loggedin_user->id == $bp->displayed_user->id)) {
$receiver_url = bp_core_get_userlink( $comp->receiver_id, false, true );
$compliment_url = $receiver_url . $bp->compliments->id . '/?c_id='.$comp->id.'&action=delete';
?>
<a href="<?php echo $compliment_url; ?>" class="button item-button confirm" style="float: right;">Delete</a>
<?php } ?>
</div>
<div class="comp-user-msg-wrap">
<div class="comp-user-message">
Expand Down Expand Up @@ -67,10 +83,10 @@ class='preview-upload'/>
</ul>
</div>
<?php
if ($total > $items_per_page) { ?>
if (($total > $items_per_page) && !$c_id) { ?>
<div id="pag-top" class="pagination">
<div class="pag-count" id="member-dir-count-top">
<?php echo sprintf(_n('1 of 1', '%1$s to %2$s of %3$s', $total, 'buddypress'), $start, $end, $total); ?>
<?php echo sprintf(_n('1 of 1', '%1$s to %2$s of %3$s', $total, BP_COMP_TEXTDOMAIN), $start, $end, $total); ?>
</div>
<div class="pagination-links">
<span class="bp-comp-pagination-text"><?php echo __('Go to Page', BP_COMP_TEXTDOMAIN) ?></span>
Expand Down
Binary file added languages/bp-compliments-en_US.mo
Binary file not shown.
Loading

0 comments on commit a6a888b

Please sign in to comment.