Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active March 7, 2025 12:14
Show Gist options
  • Save yuriinalivaiko/c7b285643a6c0134ab7cf5eee6f66fab to your computer and use it in GitHub Desktop.
Save yuriinalivaiko/c7b285643a6c0134ab7cf5eee6f66fab to your computer and use it in GitHub Desktop.
Code snippets for the "Real-time Notifications" extension
<?php
/**
* Add a new real-time notification type 'new_photo_comment'.
*
* @param array $array Notification types.
* @return array
*/
function um_user_photos_add_notification_type( $array ) {
$array['new_photo_comment'] = array(
'title' => __( 'User photo is commented', 'um-user-photos' ),
'account_desc' => __( 'When a member comments your photo in the album.', 'um-user-photos' ),
);
// turn it on (it will work once).
$is_enabled = UM()->options()->get( 'log_new_photo_comment' );
if ( '' === $is_enabled ) {
UM()->options()->update( 'log_new_photo_comment', 1 );
}
// set template.
$content = UM()->options()->get( 'log_new_photo_comment_template' );
if ( empty( $content ) ) {
UM()->options()->update( 'log_new_photo_comment_template', '<strong>{member}</strong> has commented your photo {post_title}.' );
}
return $array;
}
add_filter( 'um_notifications_core_log_types', 'um_user_photos_add_notification_type', 200 );
/**
* Add an icon for the real-time notification type 'new_photo_comment'.
*
* @param string $output Icon HTML.
* @param string $type Notification type.
* @return string
*/
function um_user_photos_add_notification_icon( $output, $type ) {
if ( 'new_photo_comment' === $type ) {
$output = '<i class="um-icon-images" style="color: #3ba1da"></i>';
}
return $output;
}
add_filter( 'um_notifications_get_icon', 'um_user_photos_add_notification_icon', 10, 2 );
/**
* Create real-time notification 'new_photo_comment' when a comment is added under the photo in the album.
*
* @param int $id The comment ID.
* @param WP_Comment $comment Comment object.
*/
function um_user_photos_photo_comment_notification( $id, $comment ) {
if ( ! defined( 'um_notifications_version' ) ) {
return;
}
if ( $comment && 'um-user-photos' === $comment->comment_type ) {
$photo_id = $comment->comment_post_ID;
$photo = get_post( $photo_id );
if ( empty( $photo ) ) {
return;
}
$album_id = $photo->post_parent;
$author = get_userdata( $comment->user_id );
$user = get_userdata( $comment->post_author );
$user_profile = um_user_profile_url( $user->ID );
$photo_url = add_query_arg(
array(
'profiletab' => 'photos',
'album_id' => $album_id,
'photo_id' => $photo_id,
),
$user_profile
);
$vars = array(
'notification_uri' => $photo_url, // 'notification_uri' is required!
'photo' => um_get_avatar_url( get_avatar( $author->ID, 40 ) ),
'post_title' => $photo->post_title,
'member' => $author->display_name,
);
UM()->Notifications_API()->api()->store_notification( $user->ID, 'new_photo_comment', $vars );
}
}
add_action( 'wp_insert_comment', 'um_user_photos_photo_comment_notification', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment