-
-
Save raviousprime/40e78466f9350b571d13473aa02a229d to your computer and use it in GitHub Desktop.
Code snippet for sending local notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @copyright https://gist.github.com/modemlooper/9693b591d8c36288496d | |
*/ | |
/** | |
* Component | |
* | |
* @param array $component_names Component names. | |
* | |
* @return array | |
*/ | |
function buddydev_notification_register_component( $component_names = array() ) { | |
if ( ! is_array( $component_names ) ) { | |
$component_names = array(); | |
} | |
array_push( $component_names, 'wp_post' ); | |
return $component_names; | |
} | |
add_filter( 'bp_notifications_get_registered_components', 'buddydev_notification_register_component' ); | |
// this gets the saved item id, compiles some data and then displays the notification | |
function buddydev_format_post_published_notification( $action, $item_id, $secondary_item_id, $total_items, $format, $component_action, $component_name ) { | |
if ( 'wp_post_published' != $component_action || 'wp_post' != $component_name ) { | |
return $action; | |
} | |
$post = get_post( $item_id ); | |
$post_link = get_permalink( $post ); | |
$text = bp_core_get_user_displayname( $post->post_author ) . __( ' has published a new post.' ); | |
if ( 'string' === $format ) { | |
$return = sprintf( '<a href="' . esc_url( $post_link ) . '">' . esc_html( $text ) . '</a>', $text, $post_link ); | |
} else { | |
$return = array( | |
'text' => $text, | |
'link' => $post_link, | |
); | |
} | |
return $return; | |
} | |
add_filter( 'bp_notifications_get_notifications_for_user', 'buddydev_format_post_published_notification', 10, 7 ); | |
/** | |
* Add custom notifiction on post publish | |
* | |
* @param string $new_status New post status. | |
* @param string $old_status Old post status. | |
* @param WP_Post $post Post object. | |
*/ | |
function buddydev_notify_friends_on_post_published( $new_status, $old_status, $post ) { | |
if ( ! function_exists( 'bp_is_active' ) || ! bp_is_active( 'notifications' ) ) { | |
return; | |
} | |
if ( 'publish' !== $new_status || 'publish' === $old_status ) { | |
return; | |
} | |
$friend_ids = friends_get_friend_user_ids( $post->post_author ); | |
if ( ! $friend_ids ) { | |
return; | |
} | |
foreach ( $friend_ids as $friend_id ) { | |
bp_notifications_add_notification( | |
array( | |
'user_id' => $friend_id, | |
'item_id' => $post->ID, | |
'secondary_item_id' => $post->post_author, | |
'component_name' => 'wp_post', | |
'component_action' => 'wp_post_published', | |
'date_notified' => bp_core_current_time(), | |
'is_new' => 1, | |
) | |
); | |
} | |
} | |
add_action( 'transition_post_status', 'buddydev_notify_friends_on_post_published', 99, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment