function bp_core_get_notifications_for_user($user_id) { global $bp; $notifications = BP_Core_Notification::get_all_for_user($user_id); /* Group notifications by component and component_action and provide totals */ for ($i = 0; $i < count($notifications); $i++) { $notification = $notifications[$i]; $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification; } if (!$grouped_notifications) { return false; } /* Calculated a renderable outcome for each notification type */ foreach ($grouped_notifications as $component_name => $action_arrays) { if (!$action_arrays) { continue; } foreach ($action_arrays as $component_action_name => $component_action_items) { $action_item_count = count($component_action_items); if ($action_item_count < 1) { continue; } $item_id = 1 == $action_item_count ? $component_action_items[0]->item_id : false; $secondary_item_id = 1 == $action_item_count ? $component_action_items[0]->secondary_item_id : false; if (function_exists($bp->{$component_name}->format_notification_function)) { $renderable[] = call_user_func($bp->{$component_name}->format_notification_function, $component_action_name, $item_id, $secondary_item_id, $action_item_count); } } } return $renderable; }
/** * Sends an email notification and a BP notification when someone mentions you in an update * * @since BuddyPress (1.2) * * @param int $activity_id The id of the activity update * @param int $receiver_user_id The unique user_id of the user who is receiving the update * * @uses bp_core_add_notification() * @uses bp_get_user_meta() * @uses bp_core_get_user_displayname() * @uses bp_activity_get_permalink() * @uses bp_core_get_user_domain() * @uses bp_get_settings_slug() * @uses bp_activity_filter_kses() * @uses bp_core_get_core_userdata() * @uses wp_specialchars_decode() * @uses get_blog_option() * @uses bp_is_active() * @uses bp_is_group() * @uses bp_get_current_group_name() * @uses apply_filters() To call the 'bp_activity_at_message_notification_to' hook * @uses apply_filters() To call the 'bp_activity_at_message_notification_subject' hook * @uses apply_filters() To call the 'bp_activity_at_message_notification_message' hook * @uses wp_mail() * @uses do_action() To call the 'bp_activity_sent_mention_email' hook */ function bp_activity_at_message_notification($activity_id, $receiver_user_id) { // Don't leave multiple notifications for the same activity item $notifications = BP_Core_Notification::get_all_for_user($receiver_user_id, 'all'); foreach ($notifications as $notification) { if ($activity_id == $notification->item_id) { return; } } $activity = new BP_Activity_Activity($activity_id); $subject = ''; $message = ''; $content = ''; // Add the BP notification bp_core_add_notification($activity_id, $receiver_user_id, 'activity', 'new_at_mention', $activity->user_id); // Now email the user with the contents of the message (if they have enabled email notifications) if ('no' != bp_get_user_meta($receiver_user_id, 'notification_activity_new_mention', true)) { $poster_name = bp_core_get_user_displayname($activity->user_id); $message_link = bp_activity_get_permalink($activity_id); $settings_slug = function_exists('bp_get_settings_slug') ? bp_get_settings_slug() : 'settings'; $settings_link = bp_core_get_user_domain($receiver_user_id) . $settings_slug . '/notifications/'; $poster_name = stripslashes($poster_name); $content = bp_activity_filter_kses(strip_tags(stripslashes($activity->content))); // Set up and send the message $ud = bp_core_get_core_userdata($receiver_user_id); $to = $ud->user_email; $subject = bp_get_email_subject(array('text' => sprintf(__('%s mentioned you in an update', 'buddypress'), $poster_name))); if (bp_is_active('groups') && bp_is_group()) { $message = sprintf(__('%1$s mentioned you in the group "%2$s": "%3$s" To view and respond to the message, log in and visit: %4$s --------------------- ', 'buddypress'), $poster_name, bp_get_current_group_name(), $content, $message_link); } else { $message = sprintf(__('%1$s mentioned you in an update: "%2$s" To view and respond to the message, log in and visit: %3$s --------------------- ', 'buddypress'), $poster_name, $content, $message_link); } // Only show the disable notifications line if the settings component is enabled if (bp_is_active('settings')) { $message .= sprintf(__('To disable these notifications please log in and go to: %s', 'buddypress'), $settings_link); } /* Send the message */ $to = apply_filters('bp_activity_at_message_notification_to', $to); $subject = apply_filters('bp_activity_at_message_notification_subject', $subject, $poster_name); $message = apply_filters('bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link); wp_mail($to, $subject, $message); } do_action('bp_activity_sent_mention_email', $activity, $subject, $message, $content); }
function bp_core_get_notifications_for_user($user_id, $format = 'simple') { global $bp; $notifications = BP_Core_Notification::get_all_for_user($user_id); // Group notifications by component and component_action and provide totals for ($i = 0, $count = count($notifications); $i < $count; ++$i) { $notification = $notifications[$i]; $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification; } if (empty($grouped_notifications)) { return false; } $renderable = array(); // Calculate a renderable output for each notification type foreach ((array) $grouped_notifications as $component_name => $action_arrays) { if (!$action_arrays) { continue; } foreach ((array) $action_arrays as $component_action_name => $component_action_items) { $action_item_count = count($component_action_items); if ($action_item_count < 1) { continue; } // @deprecated format_notification_function - 1.5 if (isset($bp->{$component_name}->format_notification_function) && function_exists($bp->{$component_name}->format_notification_function)) { $renderable[] = call_user_func($bp->{$component_name}->format_notification_function, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count); } elseif (isset($bp->{$component_name}->notification_callback) && function_exists($bp->{$component_name}->notification_callback)) { if ('object' == $format) { $content = call_user_func($bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count, 'array'); $notification_object = new stdClass(); // Minimal backpat with non-compatible notification // callback functions if (is_string($content)) { $notification_object->content = $content; $notification_object->href = bp_loggedin_user_domain(); } else { $notification_object->content = $content['text']; $notification_object->href = $content['link']; } $notification_object->id = $component_action_items[0]->id; $renderable[] = $notification_object; } else { $content = call_user_func($bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count); $renderable[] = $content; } } } } return isset($renderable) ? $renderable : false; }
/** * Send email and BP notifications when a user is mentioned in an update. * * @since 1.2.0 * * @uses bp_notifications_add_notification() * @uses bp_get_user_meta() * @uses bp_core_get_user_displayname() * @uses bp_activity_get_permalink() * @uses bp_core_get_user_domain() * @uses bp_get_settings_slug() * @uses bp_activity_filter_kses() * @uses bp_core_get_core_userdata() * @uses wp_specialchars_decode() * @uses get_blog_option() * @uses bp_is_active() * @uses bp_is_group() * @uses bp_get_current_group_name() * @uses apply_filters() To call the 'bp_activity_at_message_notification_to' hook. * @uses apply_filters() To call the 'bp_activity_at_message_notification_subject' hook. * @uses apply_filters() To call the 'bp_activity_at_message_notification_message' hook. * @uses wp_mail() * @uses do_action() To call the 'bp_activity_sent_mention_email' hook. * * @param int $activity_id The ID of the activity update. * @param int $receiver_user_id The ID of the user who is receiving the update. */ function bp_activity_at_message_notification($activity_id, $receiver_user_id) { $notifications = BP_Core_Notification::get_all_for_user($receiver_user_id, 'all'); // Don't leave multiple notifications for the same activity item. foreach ($notifications as $notification) { if ($activity_id == $notification->item_id) { return; } } $activity = new BP_Activity_Activity($activity_id); $email_type = 'activity-at-message'; $group_name = ''; $message_link = bp_activity_get_permalink($activity_id); $poster_name = bp_core_get_user_displayname($activity->user_id); remove_filter('bp_get_activity_content_body', 'convert_smilies'); remove_filter('bp_get_activity_content_body', 'wpautop'); remove_filter('bp_get_activity_content_body', 'bp_activity_truncate_entry', 5); /** This filter is documented in bp-activity/bp-activity-template.php */ $content = apply_filters('bp_get_activity_content_body', $activity->content); add_filter('bp_get_activity_content_body', 'convert_smilies'); add_filter('bp_get_activity_content_body', 'wpautop'); add_filter('bp_get_activity_content_body', 'bp_activity_truncate_entry', 5); // Now email the user with the contents of the message (if they have enabled email notifications). if ('no' != bp_get_user_meta($receiver_user_id, 'notification_activity_new_mention', true)) { if (bp_is_active('groups') && bp_is_group()) { $email_type = 'groups-at-message'; $group_name = bp_get_current_group_name(); } $args = array('tokens' => array('activity' => $activity, 'usermessage' => wp_strip_all_tags($content), 'group.name' => $group_name, 'mentioned.url' => $message_link, 'poster.name' => $poster_name, 'receiver-user.id' => $receiver_user_id)); bp_send_email($email_type, $receiver_user_id, $args); } /** * Fires after the sending of an @mention email notification. * * @since 1.5.0 * @since 2.5.0 $subject, $message, $content arguments unset and deprecated. * * @param BP_Activity_Activity $activity Activity Item object. * @param string $deprecated Removed in 2.5; now an empty string. * @param string $deprecated Removed in 2.5; now an empty string. * @param string $deprecated Removed in 2.5; now an empty string. * @param int $receiver_user_id The ID of the user who is receiving the update. */ do_action('bp_activity_sent_mention_email', $activity, '', '', '', $receiver_user_id); }
/** * Send email and BP notifications when a user is mentioned in an update. * * @since 1.2.0 * * @uses bp_notifications_add_notification() * @uses bp_get_user_meta() * @uses bp_core_get_user_displayname() * @uses bp_activity_get_permalink() * @uses bp_core_get_user_domain() * @uses bp_get_settings_slug() * @uses bp_activity_filter_kses() * @uses bp_core_get_core_userdata() * @uses wp_specialchars_decode() * @uses get_blog_option() * @uses bp_is_active() * @uses bp_is_group() * @uses bp_get_current_group_name() * @uses apply_filters() To call the 'bp_activity_at_message_notification_to' hook. * @uses apply_filters() To call the 'bp_activity_at_message_notification_subject' hook. * @uses apply_filters() To call the 'bp_activity_at_message_notification_message' hook. * @uses wp_mail() * @uses do_action() To call the 'bp_activity_sent_mention_email' hook. * * @param int $activity_id The ID of the activity update. * @param int $receiver_user_id The ID of the user who is receiving the update. */ function bp_activity_at_message_notification($activity_id, $receiver_user_id) { // Don't leave multiple notifications for the same activity item. $notifications = BP_Core_Notification::get_all_for_user($receiver_user_id, 'all'); foreach ($notifications as $notification) { if ($activity_id == $notification->item_id) { return; } } $activity = new BP_Activity_Activity($activity_id); $subject = ''; $message = ''; $content = ''; // Now email the user with the contents of the message (if they have enabled email notifications). if ('no' != bp_get_user_meta($receiver_user_id, 'notification_activity_new_mention', true)) { $poster_name = bp_core_get_user_displayname($activity->user_id); $message_link = bp_activity_get_permalink($activity_id); $settings_slug = function_exists('bp_get_settings_slug') ? bp_get_settings_slug() : 'settings'; $settings_link = bp_core_get_user_domain($receiver_user_id) . $settings_slug . '/notifications/'; $poster_name = stripslashes($poster_name); $content = bp_activity_filter_kses(strip_tags(stripslashes($activity->content))); // Set up and send the message. $ud = bp_core_get_core_userdata($receiver_user_id); $to = $ud->user_email; $subject = bp_get_email_subject(array('text' => sprintf(__('%s mentioned you in an update', 'buddypress'), $poster_name))); if (bp_is_active('groups') && bp_is_group()) { $message = sprintf(__('%1$s mentioned you in the group "%2$s": "%3$s" To view and respond to the message, log in and visit: %4$s --------------------- ', 'buddypress'), $poster_name, bp_get_current_group_name(), $content, $message_link); } else { $message = sprintf(__('%1$s mentioned you in an update: "%2$s" To view and respond to the message, log in and visit: %3$s --------------------- ', 'buddypress'), $poster_name, $content, $message_link); } // Only show the disable notifications line if the settings component is enabled. if (bp_is_active('settings')) { $message .= sprintf(__('To disable these notifications please log in and go to: %s', 'buddypress'), $settings_link); } /** * Filters the user email that the @mention notification will be sent to. * * @since 1.2.0 * * @param string $to User email the notification is being sent to. */ $to = apply_filters('bp_activity_at_message_notification_to', $to); /** * Filters the @mention notification subject that will be sent to user. * * @since 1.2.0 * * @param string $subject Email notification subject text. * @param string $poster_name Name of the person who made the @mention. */ $subject = apply_filters('bp_activity_at_message_notification_subject', $subject, $poster_name); /** * Filters the @mention notification message that will be sent to user. * * @since 1.2.0 * * @param string $message Email notification message text. * @param string $poster_name Name of the person who made the @mention. * @param string $content Content of the @mention. * @param string $message_link URL permalink for the activity message. * @param string $settings_link URL permalink for the user's notification settings area. */ $message = apply_filters('bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link); wp_mail($to, $subject, $message); } /** * Fires after the sending of an @mention email notification. * * @since 1.5.0 * * @param BP_Activity_Activity $activity Activity Item object. * @param string $subject Email notification subject text. * @param string $message Email notification message text. * @param string $content Content of the @mention. * @param int $receiver_user_id The ID of the user who is receiving the update. */ do_action('bp_activity_sent_mention_email', $activity, $subject, $message, $content, $receiver_user_id); }
/** * Get notifications for a specific user * * @since BuddyPress (1.0) * @global BuddyPress $bp * @param int $user_id * @param string $format * @return boolean Object or array on success, false on fail */ function bp_core_get_notifications_for_user($user_id, $format = 'simple') { global $bp; $notifications = BP_Core_Notification::get_all_for_user($user_id); $grouped_notifications = array(); // Notification groups $renderable = array(); // Renderable notifications // Group notifications by component and component_action and provide totals for ($i = 0, $count = count($notifications); $i < $count; ++$i) { $notification = $notifications[$i]; $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification; } // Bail if no notification groups if (empty($grouped_notifications)) { return false; } // Calculate a renderable output for each notification type foreach ($grouped_notifications as $component_name => $action_arrays) { // Skip if group is empty if (empty($action_arrays)) { continue; } // Skip inactive components if (!bp_is_active($component_name)) { continue; } // Loop through each actionable item and try to map it to a component foreach ((array) $action_arrays as $component_action_name => $component_action_items) { // Get the number of actionable items $action_item_count = count($component_action_items); // Skip if the count is less than 1 if ($action_item_count < 1) { continue; } // Callback function exists if (isset($bp->{$component_name}->notification_callback) && is_callable($bp->{$component_name}->notification_callback)) { // Function should return an object if ('object' == $format) { // Retrieve the content of the notification using the callback $content = call_user_func($bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count, 'array'); // Create the object to be returned $notification_object = new stdClass(); // Minimal backpat with non-compatible notification // callback functions if (is_string($content)) { $notification_object->content = $content; $notification_object->href = bp_loggedin_user_domain(); } else { $notification_object->content = $content['text']; $notification_object->href = $content['link']; } $notification_object->id = $component_action_items[0]->id; $renderable[] = $notification_object; // Return an array of content strings } else { $content = call_user_func($bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count); $renderable[] = $content; } // @deprecated format_notification_function - 1.5 } elseif (isset($bp->{$component_name}->format_notification_function) && function_exists($bp->{$component_name}->format_notification_function)) { $renderable[] = call_user_func($bp->{$component_name}->format_notification_function, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count); } } } // If renderable is empty array, set to false if (empty($renderable)) { $renderable = false; } // Filter and return return apply_filters('bp_core_get_notifications_for_user', $renderable, $user_id, $format); }
/** * Send email and BP notifications when a users update is liked. * * @since 0.4 * * @uses bp_notifications_add_notification() * @uses bp_get_user_meta() * @uses bp_core_get_user_displayname() * @uses bp_activity_get_permalink() * @uses bp_core_get_user_domain() * @uses bp_get_settings_slug() * @uses bp_activity_filter_kses() * @uses bp_core_get_core_userdata() * @uses wp_specialchars_decode() * @uses get_blog_option() * @uses bp_is_active() * @uses bp_is_group() * @uses bp_get_current_group_name() * @uses apply_filters() To call the 'bp_like_update_liked_notification_to' hook. * @uses apply_filters() To call the 'bp_like_update_liked_notification_subject' hook. * @uses apply_filters() To call the 'bp_like_update_liked_notification_message' hook. * @uses wp_mail() * @uses do_action() To call the 'bp_activity_sent_mention_email' hook. * * @param int $activity_id The ID of the activity update. * @param int $receiver_user_id The ID of the user who is receiving the notification. */ function bp_like_activity_update_notification($activity_id, $receiver_user_id) { // Don't leave multiple notifications for the same activity item. $notifications = BP_Core_Notification::get_all_for_user($receiver_user_id, 'all'); foreach ($notifications as $notification) { if ($activity_id == $notification->item_id) { return; } } $activity = new BP_Activity_Activity($activity_id); $subject = ''; $message = ''; $content = ''; // Now email the user with the contents of the message (if they have enabled email notifications). // TODO change 'notification_activity_new_mention' to bp like notification settings options if ('no' != bp_get_user_meta($receiver_user_id, 'notification_activity_new_mention', true)) { // TODO change this to user who liked activty update $users_who_like = array_keys((array) bp_activity_get_meta($bp_like_id, 'liked_count', true)); if (count($users_who_like) == 1) { // If only one person likes the current item. if ($receiver_user_id == $users_who_like[0]) { // if the user liked their own update we should do nothing. } else { $liker_name = bp_core_get_user_displayname($users_who_like[0]); $subject = bp_get_email_subject(array('text' => sprintf(__('%s liked your update', 'buddypress-like'), $liker_name))); } } elseif (count($users_who_like) == 2) { $liker_one = bp_core_get_user_displayname($users_who_like[0]); $liker_two = bp_core_get_user_displayname($users_who_like[1]); $subject = bp_get_email_subject(array('text' => sprintf(__('%s and %s liked your update', 'buddypress-like'), $liker_one, $liker_two))); } elseif (count($users_who_like) > 2) { $others = count($users_who_like); $liker_one = bp_core_get_user_displayname($users_who_like[$others - 1]); $liker_two = bp_core_get_user_displayname($users_who_like[$others - 2]); // TODO comment this better // $users_who_like will always be greater than 2 in here if ($users_who_like == 3) { // if 3 users like an update we remove 1 as we output 2 user names // to match the format of - "User1, User2 and 1 other like this" $others = $others - 1; } else { // remove the two named users from the count $others = $others - 2; } // $string .= '%s, %s and %d ' . _n( 'other', 'others', $others ); $subject = bp_get_email_subject(array('text' => sprintf(__('%s, %s and %d ' . _n('other', 'others', $others), 'buddypress-like'), $liker_one, $liker_two))); // printf( $string , $one , $two , $others ); } $likers_text = __('%s and %s like this.', 'buddypress-like'); // $poster_name = bp_core_get_user_displayname( $activity->user_id ); $message_link = bp_activity_get_permalink($activity_id); $settings_slug = function_exists('bp_get_settings_slug') ? bp_get_settings_slug() : 'settings'; $settings_link = bp_core_get_user_domain($receiver_user_id) . $settings_slug . '/notifications/'; // $poster_name = stripslashes( $poster_name ); $content = bp_activity_filter_kses(strip_tags(stripslashes($activity->content))); // Set up and send the message. $ud = bp_core_get_core_userdata($receiver_user_id); $to = $ud->user_email; //$subject has been declared and assigned its value previously //$subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s liked your update', 'buddypress-like' ), $liker_names ) ) ); if (bp_is_active('groups') && bp_is_group()) { $message = sprintf(__('%1$s liked your update in the group "%2$s": "%3$s" To view your liked update, log in and visit: %4$s --------------------- ', 'buddypress-like'), $poster_name, bp_get_current_group_name(), $content, $message_link); } else { $message = sprintf(__('%1$s liked your update: "%2$s" To view your liked update, log in and visit: %3$s --------------------- ', 'buddypress-like'), $poster_name, $content, $message_link); } // Only show the disable notifications line if the settings component is enabled. if (bp_is_active('settings')) { $message .= sprintf(__('To disable these notifications please log in and go to: %s', 'buddypress'), $settings_link); } /** * Filters the user email that the @mention notification will be sent to. * * @since 0.4 * * @param string $to User email the notification is being sent to. */ $to = apply_filters('bp_like_update_liked_notification_to', $to); /** * Filters the @mention notification subject that will be sent to user. * * @since 0.4 * * @param string $subject Email notification subject text. * @param string $poster_name Name of the person who made the @mention. */ $subject = apply_filters('bp_like_update_liked_notification_subject', $subject, $poster_name); /** * Filters the @mention notification message that will be sent to user. * * @since 0.4 * * @param string $message Email notification message text. * @param string $poster_name Name of the person who made the @mention. * @param string $content Content of the liked update. * @param string $message_link URL permalink for the liked activity update. * @param string $settings_link URL permalink for the user's notification settings area. */ $message = apply_filters('bp_like_update_liked_notification_message', $message, $poster_name, $content, $message_link, $settings_link); wp_mail($to, $subject, $message); } /** * Fires after the sending of an @mention email notification. * * @since 1.5.0 * * @param BP_Activity_Activity $activity Activity Item object. * @param string $subject Email notification subject text. * @param string $message Email notification message text. * @param string $content Content of the @mention. * @param int $receiver_user_id The ID of the user who is receiving the update. */ do_action('bp_like_sent_update_email', $activity, $subject, $message, $content, $receiver_user_id); }